View Full Version : convert 16 bit raw data to 24bit bitmap image

23rd September 2011, 08:33
Hi everyone,
I have a raw data of an image(width = 127,height = 64,16 bit per pixel).I want to save and convert it into a 24 bit BMP image.For saving I am using the following code.

QImage img(rawData,width,height,width * 2,QImage::Format_RGB16);

But the saved file was a 24bit BMP image.
For converting i am using the following code.

memcpy(buf,rawData + j,2);
unsigned char red = (num & 0xf800) >> 11;
unsigned char green = (num & 0x07c0) >> 5;
unsigned char blue = num & 0x003f;

The saved image is a 24bit BMP.

But the first one is better than 2nd.
Can any one told me when i am trying to save in 16 bit why it saved in 24 bit and how i will convert the 16bit raw data to 24 bit raw data.


25th September 2011, 18:58
1. Why it's saved in 24bit?
I guess that's the answer:

bool QBmpHandler::write(const QImage &img)
QImage image;
switch (img.format()) {
case QImage::Format_ARGB8565_Premultiplied:
case QImage::Format_ARGB8555_Premultiplied:
case QImage::Format_ARGB6666_Premultiplied:
case QImage::Format_ARGB4444_Premultiplied:
image = img.convertToFormat(QImage::Format_ARGB32);
case QImage::Format_RGB16:
case QImage::Format_RGB888:
case QImage::Format_RGB666:
case QImage::Format_RGB555:
case QImage::Format_RGB444:
image = img.convertToFormat(QImage::Format_RGB32);
image = img;

It seems Qt's built-in code usually saves color BMP's as 32bit.

2. How to convert?
I guess the simple and reliable way is in using QImage::convertToFormat method:

QImage QImage::convertToFormat ( Format format, Qt::ImageConversionFlags flags = Qt::AutoColor ) const
QImage QImage::convertToFormat ( Format format, const QVector<QRgb> & colorTable, Qt::ImageConversionFlags flags = Qt::AutoColor ) const