PDA

View Full Version : Accessing a QBitmap's underlying bits



sternocera
5th April 2009, 13:22
Hello,

I'm writing an application that must print arbitrary images onto a monochrome receipt printer, that behaves like an old dot matrix printer (I think). I think that I can use a QBitmap to do so - I'd certainly need to use some form of dithering, because I'm converting full colour images into greyscale bitmaps. I need to get 8 pixels at a time, in the form of a single byte (1 bit per pixel), and then feed them to the printer in what is perhaps a slightly unusual order.

So, I'd instantiate a QBitmap, and use its toData() method to create a bitmap from a PNG file buffer, at the same time resizing it to conform it to the correct resolution, and then somehow retrieve those raw bits from the QBitmap.

Is this general approach sound? How can I retrieve those raw bits?

Regards,
Sternocera

wysota
5th April 2009, 18:57
What you want is QImage not QBitmap.

sternocera
6th April 2009, 00:32
But how can I conform the image to the 1-bit palette, using a dithering algorithm to preserve details? I've looked at QImage, and I see that it's the only class Qt provides to access underlying bits.

Should I instantiate a QBitmap, load a PNG image into it and then convert it to a QImage in order to get the image as a dithered?

In any case, if I access raw bits using any of QImage's methods, I'm unsure if can get them as an actual bitmap - i.e. one bit per pixel, rather than some other representation that represents a greater color depth, or transparency or whatever else, where each pixel takes up more than a bit.

What approach is suggested?

Regards,
Sternocera

wysota
6th April 2009, 00:35
Use QImage with Format_Mono. And please read the docs about it and different colour formats. This will answer your questions.

sternocera
6th April 2009, 01:03
Alright, I see that by specifying the Format as QImage::Format_Mono the image is stored using 1-bit per pixel. But, shouldn't I somehow dither the image? The image will be taken from a png image. How will QImage conform the image to QImage::Format_Mono? Will it use dithering? The documentation is not clear on that.

I don't have a compiler in front of me at the moment to test it myself.

Regards,
Sternocera

wysota
6th April 2009, 07:41
The documentation is very clear about it. See Qt::ImageConversionFlag.