gpt4 book ai didi

c++ - QImage RGB32 到 QImage RGB24,某种图像的结果不好

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:49:25 26 4
gpt4 key购买 nike

我尝试将具有 RGB32 的 QImage 转换为具有 RGB24 的 QImage。我的代码在这种情况下不起作用:

当图片在BMP头中多出1024字节时,图片的大小为X*Y*3+54+未知的1024字节。

奇怪的是,如果图像分辨率 x == 分辨率 y,代码会起作用,例如:512x512 或 1024x1024。无论情况如何:当我在 header 中有或没有额外的 1024 字节时。

如果 BMP header 中没有额外的 1024 字节,即使分辨率 x 与分辨率 y 不同,一切都可以正常工作。我的代码:

QImage * img=new QImage("test.jpg");//load img as RGB32 32bit per pixel whatever the format of input

QImage * tmp_img=new QImage(img->width(),img->height(),QImage::Format_RGB888);// image dest 24bit per pixel

uchar * ptr1=img->bits();
uchar * ptr2=tmp_img->bits();

for( int k1=0,k2=0;k1<img->width()*img->height()*4;k1+=4,k2+=3)//increment k1 by 4 because img format is RGB32
//increment k2 by 3 because tmp_img format is RGB888
{
ptr2[k2]=ptr1[k1];
ptr2[k2+1]=ptr1[k1+1];
ptr2[k2+2]=ptr1[k1];
}

最佳答案

删除旧答案。

为什么不简单地这样做:

QImage img2 = img->convertToFormat(QImage::Format_RGB888);

关于c++ - QImage RGB32 到 QImage RGB24,某种图像的结果不好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18105871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com