0

I have created a simple application and I need export from pixmap to the 16-bit bmp image. I have several pixmap items so I have the for loop like this where I first create QImage and convert it to Format_RGB16:

    for(QList<image_handler * >::iterator it=imageItems->begin(); it!=imageItems->end(); it++)
    {
        ...
        // image_handler inherits QPixmap
        QFile export_image(path+"/img_"+code+".bmp");

        QImage export_img = (*it)->toImage().convertToFormat(QImage::Format_RGB16);
        export_img.save(&export_image, "BMP");
        ...
    }

where image_handler is my custom QPixmap. Images are exported at path given, with correct filename. However when I look at properties of file (in windows) I can see that image depth is 24-bit. Unfortunately I need them to be 16-bit.

What I am doing wrong here? Or is this a bug in Qt? Then how can I export 16-bit bmps from pixmap?

Gresthorn
  • 91
  • 1
  • 13

1 Answers1

0

Turns out, that Qt forcibly converts image, before saving it to bmp.

qt-src/src/gui/image/qbmphandler.cpp:777:

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);
        break;
    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);
        break;
    default:
        image = img;
    }
    ...

So, if you need to save bmp 16bit, you'll have to do it manually, filling header and using QImage::bits() and QImage::byteCount().

Amartel
  • 4,248
  • 2
  • 15
  • 21
  • Hmm, seems I really do not have another option than doing it manually. Do you know why are they converting it back? – Gresthorn Jun 26 '15 at 18:52
  • @Gresthorn I can only assume... may be it is easier to save 32bit bmp? Not sure though... – Amartel Jun 29 '15 at 05:40