Display image from raw bitmap file to bmp file - qt

I am trying to create a QT application which will display a image (bmp) from a raw bitmap file. The raw bitmap file is of .panel file format. Its in RGB565 format.
How do we use/convert the raw bitmap (img.panel) into a bmp file, so I can use it Qimage.
QFile file("/usr/bitmap.bmp");
if (!file.open(QFile::ReadOnly))
return 0;
QByteArray array = file.readAll();
QImage image((const uchar*)array.data(), h_bitmap, v_bitmap, QImage::Format_RGB16);
image.save("/usr/test_qimg_16.bmp","BMP");
image = image.convertToFormat(QImage::Format_RGB16);
image.load("/usr/test_qimg_16.bmp");
QPixmap pixmap;
This is the image I get after this operation.
http://tinypic.com/r/kb4pd4/8
But I am expecting something like this :
http://tinypic.com/r/dptpq1/8
Thank you.
Any help is appreciated.

Related

Use QPixmap instead of QImage?

I have an application where I copy some raw image data into a QImage directly:
QImage* img = new QImage(desc.Width, desc.Height, QImage::Format_RGB32);
for (y = 0; y < img->height(); y++)
{
memcpy(img->scanLine(y), &rawData[y * pRes->RowPitch], pRes->RowPitch);
}
return img;
Later this QImage is drawn via a call
painter.drawPixmap();
Unfortunately drawPixmap() cannot handle a QImage directly, so it first has to be converted:
m_bgImage = new QPixmap();
m_bgImage->convertFromImage(image);
Due to timing reasons I would like to drop this additional conversion step.
Thus my question: are there any function in QPixmap that allow direct image data manipulation right as in QImage?
My idea would be to start with a QPixmap from the very beginning, copy the raw image data into the QPixmap object and then use it directly.
Thanks :-)
First of all you won't need that loop to create the QImage. You can:
QImage* img = new QImage(&rawData, desc.Width, desc.Height, pRes->RowPitch * 4, QImage::Format_RGB32);
Then you can
painter.drawImage(QPointF(0,0),*img);
If there is any specific reason to use QPixmap (like QPixmap caching) you will have no other choice than convert it to QPixmap first.

How to copy the one QImage into another QImage?

How to copy one QImage into another QImage?
I do not want to change the size of image
QImage src=function_name();
QImage dst;
You can use the QImage::copy() function. For example:
QImage src = function_name();
QImage dst = src.copy();

Convert jpeg image to thumbnail image in Qt

A QImage needs to be finally shown as a thumbnail image in a QLabel. In between it's serialized in QByteArray and sent over the network. So, can we convert the QImage to QByteArray for thumbnail? Basically I want to avoid the QImage::scale() method as it's quite CPU consuming.
Like QImage converted to a QByteArray which will hold data for thumbnail images.
On this case you cannot avoid QImage::scale. Why do you want?
This is the answer to your question how to save QImage to QByteArray.
QFile file(imagePath);
if (file.open(QIODevice::ReadOnly)) {
QByteArray bytes;
QImage img;
img = img.fromData(file.readAll());
img = img.scaled(200, 100, Qt::IgnoreAspectRatio, Qt::FastTransformation);
SetLabelImage(img);
QBuffer buffer(&bytes);
img.save(&buffer, "PNG");
}

Qt - Cannot export QImage to 16bit bmp

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?
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().

set image raw data buffer to QMediaPlayer

I want to get a screen shot of the widget application and then set its raw data buffer to QMeidaPlayer with setMedia(). What I have done so far is to receive the image, SAVE it, and then read from it. However, I would like to ask you how to read raw data directly without saving it into media player:
QPixmap originalPixmap = QPixmap::grabWidget(this);
QImage *image = new QImage(originalPixmap.toImage());
QByteArray ba;
QBuffer buffer(&ba);
buffer.setBuffer(&ba);
buffer.open(QIODevice::WriteOnly);
image->save(&buffer); // writes image into ba in PNG format
image->save(image Path);
mediaPlayer.setMedia(QUrl::fromLocalFile(image path)); //I want this to read from raw data
mediaPlayer.play();
I want this to take the minimum CPU usage. However, saving and reading from file consumes lots of CPU,47%.
Cheers,
Update: I tested the program with this code snippet as well. But it does not draw the buffer contents on video widget.
QBuffer *buffer = new QBuffer(image_ba);
QMediaContent media;
mediaPlayer.setMedia(media, buffer);
mediaPlayer.play();
Any ideas how I can resolve this to input image raw data to video widget?
here's how you can do it:
QPixmap originalPixmap = QPixmap::grabWidget(this);
QBuffer *buffer = new QBuffer(); // make sure that your buffer has the same life span as your media player, or otherwise the media player would try to read data from non-existing buffer. (the setMedia() and play() methods are non-blocking)
buffer.open(QIODevice::ReadWrite);
originalPixmap.save(buffer, "PNG"); // can be any other format, not just PNG
mediaPlayer.setMedia(QUrl(), buffer);
mediaPlayer.play();

Resources