May 2, 2011

DeVeL-2010 DeVeL-2010
Lab Rat
24 posts

Drawing QImage to PDF-Document

 

Hi,

I’ve some problems with the output of an QImage to a PDF-Document, here is the relevant program code:

  1. QImage testbild("pic_47.bmp");
  2. QPrinter printer(QPrinter::ScreenResolution);
  3. printer.setOutputFormat(QPrinter::PdfFormat);
  4. printer.setColorMode(QPrinter::Color);
  5. printer.setOutputFileName(filename);
  6. printer.setPageSize(QPrinter::A0);
  7. printer.setOrientation(QPrinter::Portrait);
  8.  
  9. QPainter painter(&printer);
  10. painter.drawImage(QPoint(0, 0), testbild);
  11. painter.end();

There seems to happen some kind of interpolation in the resulting output document (independed of ScreenResolution/HighResolution). However, if I prevent loading of the library qjpeg4.dll the output looks like my expectations. Though, I need the library qjpeg4.dll in this program. Adding lines like

  1. ...
  2. painter.setWindow(0, 0, testbild.width(), testbild.height());
  3. painter.setViewport(0, 0, testbild.width(), testbild.height());
  4. painter.drawImage(QPoint(0, 0), testbild);

don’t change the behaviour.

At present the only alternative seems to be converting any pixel of the QImage into a QRect and drawing all of them with painter.drawRect(). However, this increase the size of the PDF-Document and needs more time for displaying.

I would be happy for any suggestion.

11 replies

May 2, 2011

unclewerner unclewerner
Lab Rat
141 posts

Sounds like you have to resolve problems because of linking incompatible versions of libraries (like libjpeg). You tried to play around with LD_PRELOAD ?

May 2, 2011

DeVeL-2010 DeVeL-2010
Lab Rat
24 posts

The process explorer (I tried this under Windows XP) say there is no additional jpeg library, except qjpegd4.dll. Actually, in the example I don’t use explicitly any jpeg functions. Presumably, Qt convert the QImage into a jpeg-bitmap for the PDF-Document.
The Qt libraries are of course all of the same version (4.7.2) but I get this behavior with older versions, too.

May 2, 2011

unclewerner unclewerner
Lab Rat
141 posts

Hmm… Windows and dlls…. Have you tried it on Ubuntu?

May 3, 2011

DeVeL-2010 DeVeL-2010
Lab Rat
24 posts

I just tried it under openSuse, the result is similar.

I upload an example:
Different results with/without jpeg library
This are always small parts of the graphics, copied from the PDF and zoomed.

May 3, 2011

unclewerner unclewerner
Lab Rat
141 posts

I have no clue what the problem could be. Maybe someone else knows. Maybe changing the render hints has an influence (QPainter::renderHints)?

May 3, 2011

Johan Solo Johan Solo
Ant Farmer
120 posts

From your four examples I would say that the jpeg quality is poor… What value are you using?
It may just be that the default quality is low (maybe around 40-50 percents).

 Signature 

Linux : you can find worse, but it’s more expensive.

May 3, 2011

DeVeL-2010 DeVeL-2010
Lab Rat
24 posts

@unclewerner
Default renderHints() is just QPainter::TextAntialiasing which should have no effect. I used some other values, but this has no effect.

@Johan Solo
The source in the example above is a bmp-bitmap, so there is no poor quality in the origin.
Actually I use some kind of measurement values and convert them into a QImage for drawing, therefore there should be no interpolation and so on, like in the left figures above.

May 3, 2011

Johan Solo Johan Solo
Ant Farmer
120 posts
DeVeL-2010 wrote:
@Johan Solo The source in the example above is a bmp-bitmap, so there is no poor quality in the origin. Actually I use some kind of measurement values and convert them into a QImage for drawing, therefore there should be no interpolation and so on, like in the left figures above.

But you cannot store a bmp in a PDF, it’s converted into jpeg when the PDF is created.

Edit : png can also be used I think, but from I don’t think it is the case in your example

 Signature 

Linux : you can find worse, but it’s more expensive.

May 3, 2011

DeVeL-2010 DeVeL-2010
Lab Rat
24 posts

Johan Solo wrote:

But you cannot store a bmp in a PDF, it’s converted into jpeg when the PDF is created.

Edit : png can also be used I think, but from I don’t think it is the case in your example

Well, even if my QImage is embedded as jpeg, how I can I get the result on the left side of the figures above, even if qjpeg4.dll is loaded? In other words, why is there a difference when qjpeg4.dll is loaded or not?

May 3, 2011

Johan Solo Johan Solo
Ant Farmer
120 posts

I cannot tell for sure, but it seems be that the default quality value in qjpeg4 is different from the one in the libjpeg (which is be used when you prevent qjpeg4 to be loaded).
Unfortunately I cannot tell more, I’m not smart enough. Is there a way to select the jpeg quality created by qjpeg4 library?

 Signature 

Linux : you can find worse, but it’s more expensive.

May 3, 2011

DeVeL-2010 DeVeL-2010
Lab Rat
24 posts

I dig into the Qt sources and found in qprintengine_pdf.cpp the problem. If jpeg is in supportedImageFormats() the class QImageWrite ist used, otherwise an internal conversion happens (no other library like libjpeg is involved). My problem occur already in the QImageWrite conversion.

Thanks for your help, now I understand the problem at least.

 
  ‹‹ QImage color painting question      not able to create my desired sizes and positions with QT Creator layouts ››

You must log in to post a reply. Not a member yet? Register here!