简体   繁体   English

如何在Qt中输出这个24位图像

[英]How to output this 24 bit image in Qt

I have an unsigned char array that is defined like so: 我有一个unsigned char数组,其定义如下:

unsigned char **chars = new unsigned char *[H];
for(int i = 0; i < H; i++)
{

    chars[i] = new unsigned char[W*3];
}

Where H is the height of the image, and W is the width, and chars is populated in the rest of that function looping over rows x columns of the input image. 其中H是图像的高度,W是宽度,并且在该函数的其余部分中填充字符,循环在输入图像的行x列上。 Chars is populated in the order Red Green Blue Chars填充红绿蓝的顺序

I am trying to read it with something like this: 我想用这样的东西读它:

QImage *qi = new QImage(imwidth, imheight, QImage::Format_RGB888);
    for (int i = 0 ; i < imheight ; i++)
        for (int j = 0 ; j < imwidth ; j++)
        {      //not sure why, but have to flip j and i in the index for setPixel
               qi->setPixel(j,i,qRgb(imageData[i][j],imageData[i][j+1],imageData[i][j+2]));
               j+=3;

           }

   QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));
   QPixmap p1(p.scaled(ui->menuBar->width(),ui->menuBar->width(), Qt::KeepAspectRatio, Qt::SmoothTransformation ));
   ui->viewLabel->setPixmap(p1);
   ui->viewLabel->setFixedHeight(p1.height());
   ui->viewLabel->setFixedWidth(p1.width());

where chars was returned to this calling function in the imageData array. 其中chars返回到imageData数组中的此调用函数。

What am I doing wrong? 我究竟做错了什么? Should I use a different format, even though I am clearly allocating 3 unsigned chars per pixel (which is why I chose the RGB888 format). 我应该使用不同的格式,即使我清楚地为每个像素分配3个无符号字符(这就是我选择RGB888格式的原因)。 This code as posted returns an image, but it is displaying incorrectly - partially scrambled, washed out, etc 此代码已发布返回图像,但显示不正确 - 部分乱码,褪色等

Thanks 谢谢

You can create a QImage directly from a block of data without copying every pixel. 您可以直接从数据块创建QImage而无需复制每个像素。

Since your image is stored with separate rows, you need something like 由于您的图像存储有单独的行,您需要类似的东西

QImage image = new QImage(width,height,QImage::RGB888)

for (int h=0;h<height;h++) {
    // scanLine returns a ptr to the start of the data for that row
    memcpy(image.scanLine(h),chars[h],width*3);
}

if you use RGB32 then you need to manually set the alpha channel for each pixel to 0xff - just memset() the entire data to 0xff first 如果你使用RGB32,那么你需要手动将每个像素的alpha通道设置为0xff - 只需memset()将整个数据设置为0xff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM