两大小通道均不同的Qimage合成一个

两大小通道均不同的Qimage合成一个


此项目中image1为大图作为背景,image2是拥有alpha通道的透明图,大小都是1080*1920,但要实现将image2缩小后与图一叠加。
分别获取两图的数据给buffer、buffer2再进行计算,注意:Qimage需要对存储数据进行对齐。

	m_buffer_cur= new unsigned char[1920*1080*4*4];
    m_buffer_cur1= new unsigned char[1920*1080*4*4];

    m_buffer1 = image1.bits();
    m_img1_w = image1.width();
    m_img1_h = image1.height();
    m_img1_channel = image1.depth() / 8;          //每像素字节数
    bytes_pre_line1 = image1.bytesPerLine();      //每行字节数

    int w3 = (double)image1.width() * m_gifLbl->width() / m_previewWgt->width();
    int h3 = (double)image1.height() * m_gifLbl->height() / m_previewWgt->height();
    qDebug() << m_gifLbl->width() << m_gifLbl->height();
    QImage image3 = image2.scaled(w3, h3);
    QImage image4 = image1.scaled(m_resultWgt->width(),m_resultWgt->height());
    
	m_buffer2 = image3.bits();
    m_img2_w = image3.width();
    m_img2_h = image3.height();
    m_img2_channel = image3.depth() / 8;
    bytes_pre_line2 = image3.bytesPerLine();
    
    bytePerLine=(m_img1_w * 24 + 31) / 32 * 4; //存储数据32对齐
    offset_X = 10;
    offset_Y = 10;
    end_x = offset_X + m_img2_w;
    end_y = offset_Y + m_img2_h;
    
	copyImage2buffer(m_buffer_cur, 3, m_buffer1, m_img1_w, m_img1_h, m_img1_channel);
    memcpy(m_buffer_cur1, m_buffer2, bytes_pre_line2 * m_img2_h);
    CompositeImage(m_buffer_cur, m_img1_w, m_img1_h, 3,
                   m_buffer_cur1, m_img2_w, m_img2_h, m_img2_channel, 0, 0);

关键函数copyImage2buffer():

void LiveSystem::copyImage2buffer(unsigned char* dst,int channel_dst,
                                  unsigned char* src, int src_w, int src_h,
                                  int channel_src)
{
    unsigned char r = 0;
    unsigned char g = 0;
    unsigned char b = 0;

    int pix_index_dst = 0;
    int pix_index_src = 0;

//    int line_step_dst = (src_w * 24 + 31) / 8; //存储数据32对齐
    int line_step_dst = (src_w * channel_dst * 8 + 31) / 32 * 4; //存储数据32对齐
    int line_step_src = (src_w * channel_src * 8 + 31) / 32 * 4;
//    line_step_dst = src_w * 3;

    qDebug() <<"--->"<< channel_dst<<line_step_dst<<channel_src<< line_step_src;

    for (int i = 0; i < src_h ; i++ ) {
        for (int j = 0; j < src_w ; j++ ) {
            pix_index_dst = i * line_step_dst + j * channel_dst;
            pix_index_src = i * line_step_src + j * channel_src;

            b = src[pix_index_src + 0];
            g = src[pix_index_src + 1];
            r = src[pix_index_src + 2];
            //
            dst[pix_index_dst + 0] = r;
            dst[pix_index_dst + 1] = g;
            dst[pix_index_dst + 2] = b;
        }
    }

}

CompositeImage():

void LiveSystem::CompositeImage(unsigned char* dst, int dst_w, int dst_h, int channel_dst,
                                unsigned char* src, int src_w, int src_h, int channel_src,
                                int offset_x, int offset_y)
{
    unsigned char r = 0;
    unsigned char g = 0;
    unsigned char b = 0;
    float alphal = 1.0;

    int pix_index_dst = 0;
    int pix_index_src = 0;

    int line_step_dst = (dst_w * channel_dst * 8 + 31) / 32 * 4; //存储数据32对齐
    int line_step_src = (src_w * channel_src * 8 + 31) / 32 * 4;
//    line_step_dst = src_w * 3;

    qDebug() <<"--->"<< channel_dst<<line_step_dst<<channel_src<< line_step_src;
    qDebug() <<"1--->"<< offset_x<<offset_y;

    for (int i = 0; i < src_h; i++) {
        if (i + offset_y >= dst_h) {
            break;
        }

        for (int j = 0; j < src_w ; j++) {
            if (j + offset_x >= dst_w) {
                break;
            }

            pix_index_src = i * line_step_src + j * channel_src;
            pix_index_dst = (i + offset_y) * line_step_dst + (j + offset_x) * channel_dst;

            b = src[pix_index_src + 0];
            g = src[pix_index_src + 1];
            r = src[pix_index_src + 2];
            alphal = src[pix_index_src + 3]  / 255;

            dst[pix_index_dst + 0] = dst[pix_index_dst + 0] * (1 - alphal) + r * alphal;
            dst[pix_index_dst + 1] = dst[pix_index_dst + 1] * (1 - alphal) + g * alphal;
            dst[pix_index_dst + 2] = dst[pix_index_dst + 2] * (1 - alphal) + b * alphal;
        } 

    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值