Qt中QImage类像素的颜色分量排布验证

  • Post author:
  • Post category:其他


在创建QImage时可以设置其format,例如有Format_ARGB32、Format_RGB888等,在程序中使用了Format_ARGB32和Format_RGB888两种格式,说明下这两种格式下像素中每个颜色分量的分布情况。

Format_ARGB32格式下,一个像素占用4个字节,每个像素的四个字节分别代表该像素的BGRA值。

验证方法:

int imgH = 720;
int imgW = 1280;
int iChannel = 4;
QImage imageData = new QImage(imgW, imgH, QImage::Format_ARGB32);
//给像素赋值
uchar picData = imageData->bits();
for (int i = 0; i < imgH; i++) {
for (int j = 0; j < imgW; j++) {
        picData[i * imgW * iChannel + j * iChannel + 0] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 1] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 2] = 0xFF;//R
        picData[i * imgW * iChannel + j * iChannel + 3] = 0xFF;//A
        
        /*
        picData[i * imgW * iChannel + j * iChannel + 0] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 1] = 0xFF;//G
        picData[i * imgW * iChannel + j * iChannel + 2] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 3] = 0xFF;//A

        picData[i * imgW * iChannel + j * iChannel + 0] = 0xFF;//B
        picData[i * imgW * iChannel + j * iChannel + 1] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 2] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 3] = 0xFF;//A
        */
}
}

上述代码可以生成红绿蓝三张图片,通过QLabel控件可以显示出来,代码如下:

//Qt上显示
QPixmap pixmap1   = QPixmap::fromImage(*imageData);
QLabel *pic_show = ui->showWindow;
pixmap1.scaled(pic_show->geometry().width(),
               pic_show->geometry().height(),
               Qt::KeepAspectRatio);
           pic_show->setPixmap(pixmap1);
pic_show->setScaledContents(true);

同样方法也可以验证Format_RGB888格式的像素排布,每一个像素占用三个字节,每个像素的三个字节分别表示R、G、B颜色分量,验证代码如下:

int imgH = 720;
int imgW = 1280;
int iChannel = 3;
QImage imageData = new QImage(imgW, imgH, QImage::Format_RGB888);
//给像素赋值
uchar picData = imageData->bits();
for (int i = 0; i < imgH; i++) {
for (int j = 0; j < imgW; j++) {
        picData[i * imgW * iChannel + j * iChannel + 0] = 0xFF;//R
        picData[i * imgW * iChannel + j * iChannel + 1] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 2] = 0x00;
        
        /*
        picData[i * imgW * iChannel + j * iChannel + 0] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 1] = 0xFF;//G
        picData[i * imgW * iChannel + j * iChannel + 2] = 0x00;

        picData[i * imgW * iChannel + j * iChannel + 0] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 1] = 0x00;
        picData[i * imgW * iChannel + j * iChannel + 2] = 0xFF;//B
        */
}
}

上述代码可以生成红绿蓝三张图片,通过QLabel来测试。



版权声明:本文为hanxv_1987原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。