问题描述
我有一个unsigned字符数组,定义如下:
I have an unsigned char array that is defined like so:
unsigned char **chars = new unsigned char *[H];
for(int i = 0; i < H; i++)
{
chars[i] = new unsigned char[W*3];
}
其中H是图像的高度,W是宽度,字符填充在该函数的其余部分,循环在输入图像的行x列。 Chars是按照红绿蓝的顺序填充的
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. Chars is populated in the order Red Green Blue
我想用这样的读取:
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());
其中chars返回到imageData数组中的此调用函数。
where chars was returned to this calling function in the imageData array.
我做错了什么?我应该使用不同的格式,即使我明确分配3个unsigned chars每个像素(这就是为什么我选择RGB888格式)。
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). This code as posted returns an image, but it is displaying incorrectly - partially scrambled, washed out, etc
感谢
推荐答案
您可以创建,而不复制每个像素。
You can create a QImage directly from a block of data without copying every pixel.
由于您的图片是以不同的列储存,您需要像
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);
}
如果您使用RGB32,则需要手动设置每个像素的Alpha通道到0xff - 只是memset()将整个数据0xff开头
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
这篇关于如何在Qt中输出这个24位图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!