J
J
just_asker2020-12-04 13:10:39
Qt
just_asker, 2020-12-04 13:10:39

How to create image from uint8_t array?

Hello, I want to create an image from a uint8_t array.
I'm trying to create an image in .bmp format, the file is created but the result is not the same.
5fca07e4d4c0a656974847.png
this is the correct image, which I converted via the image2cpp service to a uint8_t array.

Now through this array I want to make an image in .bmp format;

here is the code for the whole

array uint8_t *image = new uint8_t[] {
0x1f, 0xff, 0xff, 0xf8, 0x3f, 0xff, 0x7f, 0xfc, 0x7f, 0xff, 0x7f, 0xfe, 0xff, 0x7f, 0x7f, 0xff,
0xff, 0x7f, 0x7f 0xff 0xff 0x7f 0x7f 0xff 0xff 0x7f 0x7f 0xff 0xff 0x7f 0x7f 0xff
0xc0 0x01 0x40 0x03 0xff 0x7f 0x7f 0x7fff 0x7fff 0x7f 0xff, 0xff, 0x7f, 0x7f, 0xff,
0xff 0x7f 0x7f 0xff 0xff 0x7f 0x7f 0xff 0xff 0xff 0x7f 0xff 0x80 0x00 0x00 0x01
0xff 0xff 0x7f 0xff 0xdf 0xfd 0x7f 0xfb 0x7f 0xff 0xf7 0xf7 0x7f 0xff 0xfb 0xef 0x7f 0xff
0xfd 0xdf 0x7f 0xff 0xfe 0xbf 0x40 0x03 0xff
0x3f 0x40 0x03 0xfe 0xdf 0x7f 0xff 0xfd 0xef 0x7f 0xff 0xfb 0xf7 0x7f 0xff 0xf7 0xfb 0x7f
0xff 0x6f 0xfd 0x7f 0x7f0 0xfc, 0x1f, 0xff, 0xff, 0xf8
};

I'm trying to do the following through Qt:
QImage *img = new QImage(image, 32, 32, QImage::Format_Grayscale8);
image->save("out.bmp");

the file is created, the image is black and white, but the glitch effect is not pleasing to the eye

What am I doing wrong? Give advice on how to achieve the original image in .bmp format from an array.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
just_asker, 2020-12-07
@just_asker

Solution to my problem:
QImage img(QImage(bitmap, width, height, (width + 7) / 8, QImage::Format_Mono));
// display img on the form if anyone is interested:
QWidget window;
QLabel label(&window);
QPixmap image_pix(QPixmap::fromImage(image));
label.setPixmap(image_pix);
image_pix.save("out.bmp");
window.show();

A
Alexander Ananiev, 2020-12-04
@SaNNy32

Try https://doc.qt.io/qt-5/qpixmap.html#loadFromData

M
Mercury13, 2020-12-04
@Mercury13

It's not about that. The array is 16×8 (instead of 32×32) and such a picture is probably a 1-bit format.
Try Format_Mono or Format_MonoLSB.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question