1-2、合并图像渲染后保存

#include "sdlqtrgb.h"
#include <sdl/SDL.h>
#include <QMessageBox>

#pragma comment(lib, "SDL2.lib")

static SDL_Window* sdl_win = NULL;
static SDL_Renderer* sdl_render = NULL;
static SDL_Texture* sdl_texture = NULL;

static int sdl_width = 0;
static int sdl_height = 0;
static int pix_size = 4;

static unsigned char* rgb = NULL;   // 存放材质

void SdlQtRgb::timerEvent(QTimerEvent* ev)
{
    static int temp = 255;
    temp--;
    for (int j = 0; j < sdl_height; j++) {  // 写入rgb数据
        int b = j * sdl_width * pix_size;
        for (int i = 0; i < sdl_width * pix_size; i += pix_size) {
            //rgb[b + i] = 0;           //B
            //rgb[b + i + 1] = 0;       //G
            //rgb[b + i + 2] = temp;    //R
            //rgb[b + i + 3] = 0;       //A
        }
    }

    SDL_UpdateTexture(sdl_texture, NULL, rgb, sdl_width * pix_size);    // rgb数据写入材质

    // 5、渲染
    SDL_RenderClear(sdl_render);    // 清理屏幕

    SDL_Rect sdl_rect;              // 目标尺寸
    sdl_rect.x = 0;
    sdl_rect.y = 0;
    sdl_rect.w = sdl_width;
    sdl_rect.h = sdl_height;

    SDL_RenderCopy(sdl_render, sdl_texture, NULL/*原图位置和尺寸*/, &sdl_rect/*目标位置和尺寸*/); // 复制材质到渲染器

    SDL_RenderPresent(sdl_render);  // 渲染
}

SdlQtRgb::SdlQtRgb(QWidget* parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    sdl_width = ui.label->width();
    sdl_height = ui.label->height();

    // 1、初始化SDL
    SDL_Init(SDL_INIT_VIDEO);

    // 2、创建一个窗口
    sdl_win = SDL_CreateWindowFrom((void*)ui.label->winId());

    // 3、创建一个渲染器
    sdl_render = SDL_CreateRenderer(sdl_win, -1, SDL_RENDERER_ACCELERATED);

    QImage img1("001.png");
    QImage img2("002.png");

    if (img1.isNull() || img2.isNull()) {
        QMessageBox::information(this, "", "Open image fail!");
        return;
    }

    int out_w = img1.width() + img2.width();
    int out_h = img1.height();
    if (out_h < img2.height()) {
        out_h = img2.height();
    }

    sdl_width = out_w;
    sdl_height = out_h;

    resize(sdl_width, sdl_height);
    ui.label->move(0, 0);
    ui.label->resize(sdl_width, sdl_height);

    // 4、创建材质
    sdl_texture = SDL_CreateTexture(sdl_render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, sdl_width, sdl_height);

    rgb = new unsigned char[sdl_width * sdl_height * pix_size]; // 分配材质内存

    // 默认设置透明颜色
    memset(rgb, 0, sdl_width * sdl_height * pix_size);

    // 合并两幅图像
    for (int j = 0; j < sdl_height; j++) {
        int b = j * sdl_width * pix_size;   // 第一列像素位置
        if (j < img1.height())
            memcpy(rgb + b, img1.scanLine(j), img1.width() * pix_size);

        b += img1.width() * pix_size;
        if (j < img2.height())
            memcpy(rgb + b, img2.scanLine(j), img2.width() * pix_size);
    }

    QImage out(rgb, sdl_width, sdl_height, QImage::Format_ARGB32);
    out.save("out.png");

    startTimer(20);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值