-1

I generate a PDF document directly with a QPrinter (set to HighResolution) where I draw a few pixel images QImage, using QPainter::drawImage().

The images have a very small size (about 200x50 pixels) and have only 3 colors. I zoom them up in the PDF using a larger QRect as bounding box.

The QImage itself is not scaled.

I want the images clear and crisp in the PDF, however, they show scaling artifacts the same way as a bad jpg. (I want kind of png instead...).

How can I prevent this? Is it a question of QPainter or QPrinter? A question of image compression? Or is it the PDF viewer?

QImage* image;

//...

QPrinter printer(QPrinter::HighResolution);
printer.setOutputFileName("/home/xxx/test.pdf");
printer.setOutputFormat(QPrinter::PdfFormat);

QPainter painter;
painter.begin(&printer);

const int SCALING = 30;
QRect imageFrame = image->rect();    // <-- here comes my QImage

imageFrame.setSize( imageFrame.size() * SCALING );
painter.drawImage( imageFrame, *image );

//...

painter.end();

Edit: an example for clarification This is how the QImage looks on the screen:

enter image description here

When I put this image to a pdf, it appears like this, thus, with typical jpg artifacts. The bounding box of this image is enlarged to that the content can be seen in detail, but the used QImage is 1:1 (i.e. one of the text pixels is one pixel in the image).

enter image description here

When I scale the image itself up by 4 before putting it to the pdf (i.e. each text pixel is 4x4 pixels in QImage), it appears like this. Much closer to what I want, but with enlarged picture data and multiple file size.

enter image description here

My question is how I can use the original (small) QImage, which is shown with absolutlely crisp pixel squares in the PDF, without these artifacts.

Ingmar
  • 2,361
  • 3
  • 19
  • 33

1 Answers1

0

I'm not sure what you mean with "I want kind of png instead". My understanding is that you seem to have 200x50 pixels and want to draw 6000x1500 pixels. This is probably done by drawImage() with a scaling algorithm, which will create blurriness and other glitches.

If you want precise drawing, regardless of size, you need to draw primitives with QPainter. For instance, if I wrote this:

QRadialGradient radialGrad(QPointF(100, 100), 100);
radialGrad.setColorAt(0, Qt::red);
radialGrad.setColorAt(0.5, Qt::blue);
radialGrad.setColorAt(1, Qt::green);

painter.setBrush(QBrush(radialGrad));
painter.drawEllipse(160, 100, 50, 50);
painter.drawRect(100, 100, 50, 50);

this is what I get:

enter image description here

Note that I zoomed so much that you cannot even see the shapes in the preview. Still the gradient is perfect.

I'm not a PDF expert, but QPainter is probably using raster for the image and vector for the primitives: https://en.wikipedia.org/wiki/PDF#Imaging_model.

Luca Carlon
  • 9,546
  • 13
  • 59
  • 91
  • thanks for answering. Please see my edits above for clarification. My point is not to have smooth gradients, but quite the opposite, crisp pixels. – Ingmar Apr 16 '21 at 08:57
  • The circle seems pretty crisp to me. I still do not understand the problem, sorry. You seem to have a QImage of size (a, b) and expect to draw it in size (a*30, b*30) with identical quality. – Luca Carlon Apr 16 '21 at 10:48
  • Also you should probably test with QPrinter::ScreenResolution instead. The other may value may scale internally. – Luca Carlon Apr 16 '21 at 11:00
  • I don't have vector image, and I don't draw to the PDF - I insert a bitmap. My project is an LCD emulator, so I use a QImage to hold the display's content with 240x64 pixels, white on blue background. See 1st example which says "T1 test - please wait". Now I want to document these screens in a PDF, where each "screenshot" is about half a A4 page wide. It is really important that the white pixels are clean white squares, and the blue ones are blue. However, since the original bitmap is quite small, the PDF probably uses jpg internally which leads to blurry images when zoomed up (example 2). – Ingmar Apr 16 '21 at 17:17
  • Yes, as already said, the PDF will probably contain raster content, maybe jpg: https://en.wikipedia.org/wiki/PDF#Imaging_model. Scaling up will reduce quality in both cases, but even worst with the embedded encoded content. No idea if you can choose the encoding of the embedded image in Qt, sorry. – Luca Carlon Apr 16 '21 at 18:14