[Tex/LaTex] Caching includegraphics{} image files for speed

graphics

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?

advice appreciated.

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

Best Answer

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.