Compressed Images in PostScript and PDF with LaTeX HOWTO

Daniel Käps

last revised: 2002-11-24
Abstract

It is a common problem that PostScript files (as generated by TeX/LaTeX users) become really large when users try to include pixel mapped images (also referred to as ``sampled images'', ``pixmaps'' or ``bitmaps''). The huge size of the resulting PostScript files is due to the fact, that many converters to Encapsulated PostScript decompress the images.

However, with the right tools and specifying the correct options it is possible to include certain image types (such as JPEG or PNG) retaining compression. This can greatly reduce the size of the resulting PostScript files and thus speed up downloading and displaying.

The article explains which tools and image formats can be used to put images into PostScript without decompression. Moreover, some considerations have been made about which image formats can be used when generating other output formats, namely PDF (Portable Document Format) and HTML, from a LaTeX source.

Contents

  Generation of DVI/PS Files (e.g. using 'latex')
   Inclusion of JPEG images
    JPEG-to-EPS conversion using 'jpeg2ps'
   Inclusion of PNG and TIFF images
    PNG-to-EPS and TIFF-to-EPS conversion using 'bmeps'
    PNG-to-EPS and TIFF-to-EPS conversion using ImageMagick's 'convert'
    TIFF-to-EPS conversion using libtiff's 'tiff2ps'
  Generation of PDF Files (using 'pdflatex')
  Generation of HTML Files (using 'tex4ht')
  Conclusions
  Compilation Platform

Generation of DVI/PS Files (e.g. using 'latex')

Pixel mapped images can be included via LaTeX's '\includeimage' (see [1]) into PostScript output without problems when they are available in (Encapsulated) PostScript format.

Anyway, to put compressed images into the (Encapsulated) PostScript Format, (at least) PostScript Level II must be used. Decompression has to be done by the PostScript viewer/interpreter. Many PostScript viewers seem to be able to do this. Specifically, the following programs/versions were tested. All of them were able to display the PostScript files containing the compressed images (or the DVI files referencing the EPS files):

Basically, the conversion of the images to Encapsulated PostScript can be accomplished in different ways:

The following will concentrate on the usage of different external tools to convert images to PostScript Level II or III.

Inclusion of JPEG images

PostScript Level II supports direct inclusion of JPEG images using the 'DCT' (discrete cosine transform) filter [3]. To display a PostScript file containing images represented with the DCT-filter, the PostScript interpreter must support (at least) PostScript Level 2.

JPEG-to-EPS conversion using 'jpeg2ps'

JPEG images can be converted to PostScript with the 'jpeg2ps' program [4]:

jpeg2ps IMAGE.jpg >IMAGE.jpg.eps

This way the compressed image data is included into the PostScript file as is (without decompressing it), which reduces the size of the resulting PostScript file considerably.

Inclusion of PNG and TIFF images

PostScript Level 3 supports inclusion of TIFF and PNG images by the following means: The 'LZW' (Lempel-Ziv-Welch) and 'Flate' (zlib/deflate) filters are available for (de)compression. The PNG and TIFF pixel-predictor functions are supported [5]. However, while e.g. PNG images can have Alpha-transparency, PostScript seems to support only Bi-level transparency (using the 'imagemask'-operator).

In contrast, PostScript Level 2 only supports 'LZW'-compression and apparently it doesn't have support for pixel-predictor-functions. Thus, many images won't be compressed as well as it is possible with Level 3's features. Also note that the 'LZW' algorithm is patented, and using it may require paying license fees to the company holding the patent.

PNG-to-EPS and TIFF-to-EPS conversion using 'bmeps'

PNG and TIFF images can be converted into PostScript Level I, II or III with the program 'bmeps' [6]. To create an EPS file with maximum compression, use:

bmeps -p3 -c -e8f -tpng IMAGE.png >IMAGE.png.eps

which creates a Level 3 EPS file using 'flate'-compression for a colored PNG file. Only some small problems occured with the used version of 'bmeps':

For a TIFF file, the call is simply:

bmeps -p3 -c -e8f -ttiff IMAGE.tiff >IMAGE.tiff.eps

'bmeps' is also capable of mapping TIFF and PNG alpha channels to EPS level 3 (Bi-level) image masks.

PNG-to-EPS and TIFF-to-EPS conversion using ImageMagick's 'convert'

PNG and TIFF images can be converted into PostScript Level III using the program 'convert' which is part of the ImageMagick graphics tools [7]:

convert -compress zip IMAGE.png eps2:IMAGE.png.eps

This includes the PNG file without decompressing it into the PostScript file using 'flate'-compression. However, things are not as straightforward as they are for 'jpeg2ps' or 'bmeps':

To reduce the size of a PNG image itself, and to convert from a RGB-type to a palette-type PNG, the program 'pngquant' [9] can be used.

For a TIFF file, the call is:

convert -compress zip IMAGE.tiff eps2:IMAGE.tiff.eps
TIFF-to-EPS conversion using libtiff's 'tiff2ps'

TIFF images can be put compressed into a PostScript Level II file with the program 'tiff2ps', which is part of the libtiff tools [10]:

tiff2ps -2 -e -s IMAGE.tif >IMAGE.tif.eps

However, the following things should be taken into account:

Generation of PDF Files (using 'pdflatex')

As with 'latex', pixel mapped images can be included with '\includeimage' (see [1]). 'pdflatex' supports inclusion of JPEG, PNG and TIFF directly, without the need to convert them before to something like Encapsulated PostScript [11].

Moreover, PDF can include EPDF (Encapsulated PDF), but cannot include EPS files. However, EPS files may be converted to EPDF using external tools, such as 'ps2pdf' (TODO: must test this).

Generation of HTML Files (using 'tex4ht')

Using 'tex4ht', an image can be embedded using the following LaTeX code (see [12]):

\Picture[ALT.-TEXT]{IMAGEFILENAME HTML-IMAGE-ATTR.}

Note that the HTML image attributes should define 'width' and 'height' so that Web-browsers can render the page correctly before all images have been loaded.

Most Web browsers can display JPEG and GIF, newer browsers can also display PNG (see [13]). To achieve compatibility also with older browsers, PNG images could be converted into GIF images. (Note that the 'LZW'-algorithm used to compress GIF images is patented). Formats like TIFF, EPS, EPDF and anything else usually cannot be displayed by Web browsers.

Conclusions

JPEG images can be included without decompression into PostScript Level II, III and PDF and can be displayed by most Web-Browsers.

PNG and TIFF images can be included without de-compression into PostScript Level III and PDF (and with lower compression also in PostScript Level II), but only newer Web-Browsers support displaying PNG images.

TIFF images can be included into PDF, but normally cannot be displayed by Web browsers.

Other image formats may require conversion to be usable for inclusion into PostScript, PDF or HTML. Conversion between various image formats can be done e.g. using ImageMagick's 'convert', which supports hundreds of different graphics formats.

JPEG and PNG generally provide better compression than most other image formats, so it seems reasonable to keep the 'source' images in either of these two formats, depending on which is better suited.

Note that some older printers may not be capable of directly printing PostScript files making use of Level III features. To print Level III files on such a printer, e.g. 'GSView' can be used.

For HTML output, one may choose to use GIF instead of PNG for compatibility with older Web-Browsers.

Compilation Platform

The versions of the tools to convert images were:

The used TeX versions were:

References

[1]  Packages in the `Graphics' Bundle,
http://www.ctan.org/tex-archive/macros/latex/required/graphics/grfguide.ps
[2]  Keith Reckdahl: Using Imported Graphics in LaTeX 2e (Version 2.0, December 15, 1997),
http://www.ctan.org/tex-archive/info/epslatex.pdf   (1.5 MB)
[3]  Adobe PostScript Language Reference Manual, Second Edition, 1990,
http://partners.adobe.com/asn/developer/pdfs/tn/psrefman.pdf   (3.3 MB)
Sections 3.8.4 (Filters) and 3.13.3 (Details of individual filters, p. 127)
[4]  jpeg2ps utility - a JPEG to PostScript Level 2 (or PostScript 3) wrapper,
http://www.pdflib.com/jpeg2ps/index.html
[5]  Adobe PostScript Language Reference, Third Edition, 1999,
http://partners.adobe.com/asn/developer/pdfs/tn/PLRM.pdf   (7.4 MB)
Section 3.13.3 (Details of individual filters, p. 129)
[6]  bmeps - command line tool, library and dvips patch for bitmap-to-EPS conversion,
http://www.e-technik.fh-schmalkalden.de/personen/dhp/krause/frames/index.php3?fc=entire&f=/personen/dhp/krause/pages/software/bmeps/index.html
[7]  ImageMagick - Convert, Edit, and Compose Images,
http://www.imagemagick.org/
[8]  libpng - A Basic Introduction to PNG Features,
http://www.libpng.org/pub/png/pngintro.html
[9]  pngquant - utility to quantize and dither 32-bit RGBA PNGs down to 8-bit (or smaller) RGBA-palette PNGs,
http://www.libpng.org/pub/png/apps/pngquant.html
[10]  TIFF Software - library, tools for TIFF images,
http://www.libtiff.org/
[11]  The pdfTeX manual,
http://www.tug.org/applications/pdftex/pdftex-a.pdf
[12]  tex4ht - From LaTeX to HTML - Pictures,
http://www.cis.ohio-state.edu/%7Egurari/TeX4ht/mn8.html
[13]  libpng - Browsers with PNG Support,
http://www.libpng.org/pub/png/pngapbr.html
_________________________________________________________________________________

If you have comments or suggestions, mail to:
kaeps@-spam-away-.informatik.uni-leipzig.de
(remove the ``-spam-away-.'' spam protection).