Removed old answer.
Why not simply do this:
QImage img2 = img->convertToFormat(QImage::Format_RGB888);
Question
I try to convert the QImage
with RGB32 to QImage
with RGB24.
My code doesn't work in this case:
When the image has an additional 1024 bytes in the BMP header, the size of the image is X*Y*3+54+an unknown 1024 byte.
The strange thing is that the code works if the image resolution x == resolution y, for example: 512x512 or 1024x1024. Whatever the situation: when I have or don't have the additional 1024 Byte in the header.
If the BMP does not have the additional 1024 byte in the header, every thing works fine even if resolution x is different than resolution y. My code :
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];
}
Solution
Removed old answer.
Why not simply do this:
QImage img2 = img->convertToFormat(QImage::Format_RGB888);