# PNG: importing it with latex | pdflatex | xelatex

I never understood how (pdf)latex determines the size of imported bitmaps, but many years ago I experimentally elaborated simple rule that allowed me to get same output by running either latex or pdflatex compiler over same source. By this rule, natural height and width of a PNG graphics (number of pixels in height and width) should be indicated in optional argument of \includegraphics suffuxed with bp units, e.g.,

\includegraphics[natwidth=162bp,natheight=227bp]{Aston.png}


Imported graphics can then be scaled to desired size by adding the scale or width options. Most importantly is that that the result did not depend on the resolution of PNG graphics. That rule, however, appeared to not work with xelatex. Expected size (I mean the size of imported graphics produced by latex or pdflatex) is obtained only if DPI is 72. Here is my test source:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
[natwidth=162bp, natheight=227bp, width=70bp]; DPI=72;\\
\fbox{\includegraphics[natwidth=162bp,natheight=227bp, width=70bp]{Aston.png}}

\medskip
[natwidth=200bp, natheight=287bp, width=70bp]; DPI=120; DPI=96; DPI=72;\\
\fbox{\includegraphics[natwidth=200bp,natheight=287bp, width=70bp]{200px-Hans_Bethe.png}}
\fbox{\includegraphics[natwidth=200bp,natheight=287bp, width=70bp]{Bethe-DPI96.png}}
\fbox{\includegraphics[natwidth=200bp,natheight=287bp, width=70bp]{Bethe-DPI72.png}}
\end{document}


Here is output produced by xelatex:

So. I am in doubts now what to do with appendix to my book? It contains more than a hundred of bitmaps.

-
The size of EPS files is determined by graphics/x by reading the BoundingBox ASCII header. The size of PDF, PNG or JPG is read in binary by pdftex (I assume). This file formats hold the size as part of the binary header. –  Martin Scharrer Mar 8 '11 at 11:00
Sorry, Igor, I have problems to understand the problem. Could you explain why a simple \includegraphics[width=70bp] does not work? Modern versions of (pdf)latex and graphic/x shouldn't need natwidth and natheight. –  Martin Scharrer Mar 8 '11 at 11:02
@Martin: I use natwidth because I want the source be also suitable for latex.exe. I've check again that latex complains if natural size of png is not somehow speifiied. –  Igor Kotelnikov Mar 8 '11 at 11:52
@Martin: latex still cannot read size from png. –  Igor Kotelnikov Mar 8 '11 at 11:53
Ok, so far I was under the impression that latex doesn't support PNG at all. This probably changed now because it is actually pdflatex in DVI mode. But it is funny that it doesn't read the size of PNGs. You could use graphic rules and put the size as bounding box into .bb files. –  Martin Scharrer Mar 8 '11 at 13:39

The size of EPS files is determined by graphics/x by reading the BoundingBox ASCII header. The size of PDF, PNG or JPG is read in binary by pdftex (I assume). This file formats hold the size as part of the binary header. For XeLaTeX it should be the same as for PDF-LaTeX, I guess.

Nowadays latex actually runs pdflatex in DVI mode. Apparently it doesn't read the size of PNGs from the files in this mode while it works well in PDF mode. One way to do this is to create .bb files for all you PNGs, e.g. foo.png.bb for foo.png, which holds the line (shown here for a 200x287 bp image):

%%BoundingBox: 0 0 200 287


Then create a graphics rule for these as described in the Graphics Guide (grfguide):

\DeclareGraphicsRule{.png}{png}{.png.bb}{}


You should be able to generate these files using a script automatically. You then don't need to declare the size manually using natwidth and natheight every time you use them.

However, I didn't tested this throughly. It might be not a good idea to use PNGs with latex at all.

-
PDFLaTeX and XeLaTeX are diiferent in interpreting natwidth+natheight or (I guess) bb parameters given in options to includegraphics. PDFLaTeX assumes that [natwith=72,natheight=72] stands for bitmap image of size of 72bp*72bp independantly of actual DPI encoded into the bitmat. On the contrary, XeLaTeX respects DPI. I don't know which of the two methods is more correct. The fact is that they are different. –  Igor Kotelnikov Sep 18 '11 at 15:48