# Caching includegraphics{} image files for speed

our graphics files were created by third parties. I don't have much control over them. I do have access to Imagemagick, so I can shrink and/or manipulate them.

for example, I have a 200KB (modest-file size) png image that uses unreasonably large canvas sizes:

``````  Image Width: 10200 Image Length: 13200
Bitdepth (Bits/Sample): 4
Channels (Samples/Pixel): 1
Pixel depth (Pixel Depth): 4
Colour Type (Photometric Interpretation): PALETTED COLOUR with alpha (5 colours, 1 transparent)
Image filter: Single row per byte filter
Interlacing: No interlacing
Compression Scheme: Deflate method 8, 32k window
Resolution: 47245, 47245 (pixels per meter)
FillOrder: msb-to-lsb
Byte Order: Network (Big Endian)
Number of text strings: 0 of 0
``````

This png file will be enclosed with

``````  \includegraphics[width=0.33\textwidth]{image.png}
``````

this works out great...except it takes pdflatex about 10 seconds to do this. I have similar issues with large-size .jpg canvas images, too. Multiply the delays by 20-50 images, and you see my point.

I do not want draft mode. I would like to see my images and I don't want them degraded. Is there a way to cache the png file into what pdflatex actually includes, so that on subsequent runs the pdflatex compilation is a lot faster?

EDIT: More complex:

1. I think fast png inclusion does not work with transparency in the image. so the following fails:

pngtopnm f.png | pnmtopng > f.png ; convert f.png -transparent white f.png

2. conversion of my file with Imagemagick worries me. latex tells me the pt size of the image that it scales to, but this can have many or few pixels, depending on the output destination resolution.

so, the two simple solutions are not working in my case, I think.

/iaw

• Something about this is said in the PDFTeX documentation, section 8, the paragraph starting with “Embedding PNG images”. – egreg May 16 '14 at 17:47
• Not sure if I'm reading this right, but if you have a 10200x13200 PNG, that's a 17x22 inch area on the page at 600 dots per inch (34x44 at 300 dpi). If you're already scaling down by 2/3 when you include them, why not reduce the original image size by 2/3 with imagemagick or similar tools? – Mike Renfro May 16 '14 at 17:51
• According to David's comment at tex.stackexchange.com/questions/175443/…, pdflatex will cache `\includegraphics` images automatically. – Steven B. Segletes May 16 '14 at 17:54
• @StevenB.Segletes I read that as "It caches the image if you use the image more than once in your document" -- doesn't help the OP in this case, it would seem, as he wants to cache between runs of pdflatex. – cslstr May 16 '14 at 19:16
• pdftex manual: ctan.tug.org/tex-archive/systems/pdftex/manual/pdftex-l.pdf . fast pdf embedding: tex.stackexchange.com/questions/39929/… . Steven = yes, I want caching between runs. Mike = if I scale down, I may inadvertently get image degradation due to non-alignment. (I could take out \textwidth sizing, and then use ImageMagick convert...) – ivo Welch May 16 '14 at 20:00

## Repeated use of an image

The driver `pdftex.def` for package `graphics` reuses an image, if it was used before. Thus there is no much room for optimization left. A reason for using `\includegraphics` rather than primitive commands.

## PNG files

There are two ways, PNG images are included by pdfTeX:

• The image data are uncompressed and compressed again in a PDF image object. This is slow depending on `\pdfcompresslevel` or faster, but with large file size.

• Sometimes pdfTeX can just copy the PNG image data. Then you can find "`(PNG copy)`" next to the image name in the console/`.log` file output. But there are lots of requirements:

• PDF minor version must be greater than 1 (default is greater).
• Image must be non-interlaced.
• The only supported color types are "gray" and "RGB".
• The "gamma" value is restricted to 100000 if present.
• And the image should not contain any of these chunks: `cHRM`, `iCCP`, `sBIT`, `sRGB`, `bKGD`, `tRNS`, `sPLT`.
• Transparency or alpha channel is not supported.

I derived the reasons from the source file `writepng.c`, function `write_png`.

Thus you can try to optimize the PNG generation that pdfTeX can use the method "PNG copy" for including the image files.

## JPEG files

The PDF format supports the compression method in JPEG files, thus pdfTeX just copies the data.