27 Declaring and Using Images

This section describes the pgfbaseimage package.

\usepackage{pgfbaseimage} % LATEX
\input pgfbaseimage.tex % plain TEX
\input pgfbaseimage.tex % ConTEXt

This package offers an abstraction of the image inclusion process. It is loaded automatically by pgf, but you can load it manually if you have only included pgfcore.

27.1 Overview

To be quite frank, LATEX’s \includegraphics is designed better than pgfbaseimage. For this reason, I recommend that you use the standard image inclusion mechanism of your format. Thus, LATEX users are encouraged to use \includegraphics to include images.

However, there are reasons why you might need to use the image inclusion facilities of PGF:

Whatever your choice, you can still use the usual image inclusion facilities of the graphics package.

The general approach taken by PGF to including an image is the following: First, \pgfdeclareimage declares the image. This must be done prior to the first use of the image. Once you have declared an image, you can insert it into the text using \pgfuseimage. The advantage of this two-phase approach is that, at least for PDF, the image data will only be included once in the file. This can drastically reduce the file size if you use an image repeatedly, for example in an overlay. However, there is also a command called \pgfimage that declares and then immediately uses the image.

To speedup the compilation, you may wish to use the following class option:

\usepackage[draft]{pgf}

In draft mode boxes showing the image name replace the images. It is checked whether the image files exist, but they are not read. If either height or width is not given, 1cm is used instead.

27.2 Declaring an Image

\pgfdeclareimage[<options>]{<image name>}{<filename>}

Declares an image, but does not paint anything. To draw the image, use \pgfuseimage{<image name>}. The <filename> may not have an extension. For PDF, the extensions .pdf, .jpg, and .png will automatically tried. For PostScript, the extensions .eps, .epsi, and .ps will be tried.

The following options are possible:


\pgfdeclareimage[interpolate=true,height=1cm]{image1}{pgf-tu-logo}
\pgfdeclareimage[interpolate=true,width=1cm,height=1cm]{image2}{pgf-tu-logo}
\pgfdeclareimage[interpolate=true,height=1cm]{image3}{pgf-tu-logo}

\pgfaliasimage{<new image name>}{<existing image name>}

The {<existing image name>} is “cloned” and the {<new image name>} can now be used whenever original image is used. This command is useful for creating aliases for alternate extensions and for accessing the last image inserted using \pgfimage.

Example: \pgfaliasimage{image.!30!white}{image.!25!white}

27.3 Using an Image

\pgfuseimage{<image name>}

Inserts a previously declared image into the normal text. If you wish to use it in a {pgfpicture} environment, you must put a \pgftext around it.

If the macro \pgfalternateextension expands to some nonempty <alternate extension>, PGF will first try to use the image names <image name>.<alternate extension>. If this image is not defined, PGF will next check whether <alternate extension> contains a ! character. If so, everything up to this exclamation mark and including it is deleted from <alternate extension> and the PGF again tries to use the image <image name>.<alternate extension>. This is repeated until <alternate extension> no longer contains a !. Then the original image is used.

The xxcolor package sets the alternate extension to the current color mixin.

 

SVG-Viewer needed.

 

\pgfdeclareimage[interpolate=true,width=1cm,height=1cm]{image1}{pgf-tu-logo}
\pgfdeclareimage[interpolate=true,width=1cm]{image2}{pgf-tu-logo}
\pgfdeclareimage[interpolate=true,height=1cm]{image3}{pgf-tu-logo}
\begin{pgfpicture}
  \pgftext[at=\pgfpoint{1cm}{5cm},left,base]{\pgfuseimage{image1}}
  \pgftext[at=\pgfpoint{1cm}{3cm},left,base]{\pgfuseimage{image2}}
  \pgftext[at=\pgfpoint{1cm}{1cm},left,base]{\pgfuseimage{image3}}

  \pgfpathrectangle{\pgfpoint{1cm}{5cm}}{\pgfpoint{1cm}{1cm}}
  \pgfpathrectangle{\pgfpoint{1cm}{3cm}}{\pgfpoint{1cm}{1cm}}
  \pgfpathrectangle{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{1cm}}
  \pgfusepath{stroke}
\end{pgfpicture}

The following example demonstrates the effect of using \pgfuseimage inside a color mixin environment.

 

SVG-Viewer needed.

 

\pgfdeclareimage[interpolate=true,width=1cm,height=1cm]
  {image1.!25!white}{pgf-tu-logo.25}
\pgfdeclareimage[interpolate=true,width=1cm]
  {image2.25!white}{pgf-tu-logo.25}
\pgfdeclareimage[interpolate=true,height=1cm]
  {image3.white}{pgf-tu-logo.25}
\begin{colormixin}{25!white}
\begin{pgfpicture}
  \pgftext[at=\pgfpoint{1cm}{5cm},left,base]{\pgfuseimage{image1}}
  \pgftext[at=\pgfpoint{1cm}{3cm},left,base]{\pgfuseimage{image2}}
  \pgftext[at=\pgfpoint{1cm}{1cm},left,base]{\pgfuseimage{image3}}

  \pgfpathrectangle{\pgfpoint{1cm}{5cm}}{\pgfpoint{1cm}{1cm}}
  \pgfpathrectangle{\pgfpoint{1cm}{3cm}}{\pgfpoint{1cm}{1cm}}
  \pgfpathrectangle{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{1cm}}
  \pgfusepath{stroke}
\end{pgfpicture}
\end{colormixin}

\pgfalternateextension

You should redefine this command to install a different alternate extension.

Example: \def\pgfalternateextension{!25!white}

\pgfimage[<options>]{<filename>}

Declares the image under the name pgflastimage and immediately uses it. You can “save” the image for later usage by invoking \pgfaliasimage on pgflastimage.

 

SVG-Viewer needed.

 

\begin{colormixin}{25!white}
\begin{pgfpicture}
  \pgftext[at=\pgfpoint{1cm}{5cm},left,base]
    {\pgfimage[interpolate=true,width=1cm,height=1cm]{pgf-tu-logo}}
  \pgftext[at=\pgfpoint{1cm}{3cm},left,base]
    {\pgfimage[interpolate=true,width=1cm]{pgf-tu-logo}}
  \pgftext[at=\pgfpoint{1cm}{1cm},left,base]
    {\pgfimage[interpolate=true,height=1cm]{pgf-tu-logo}}

  \pgfpathrectangle{\pgfpoint{1cm}{5cm}}{\pgfpoint{1cm}{1cm}}
  \pgfpathrectangle{\pgfpoint{1cm}{3cm}}{\pgfpoint{1cm}{1cm}}
  \pgfpathrectangle{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{1cm}}
  \pgfusepath{stroke}
\end{pgfpicture}
\end{colormixin}

27.4 Masking an Image

\pgfdeclaremask[<options>]{<mask name>}{<filename>}

Declares a transparency mask named <mask name> (called a soft mask in the PDF specification). This mask is read from the file <filename>. This file should contain a grayscale image that is as large as the actual image. A white pixel in the mask will correspond to “transparent,” a black pixel to “solid,” and gray values correspond to intermediate values. The mask must have a single “color channel.” This means that the mask must be a “real” grayscale image, not an RGB-image in which all RGB-triples happen to have the same components.

You can only mask images the are in a “pixel format.” These are .jpg and .png. You cannot mask .pdf images in this way. Also, again, the mask file and the image file must have the same size.

The following options may be given:

Example:

SVG-Viewer needed.

pgflastimage ____________pgflastimage ____________pgflastimage

%% Draw a large colorful background
\pgfdeclarehorizontalshading{colorful}{5cm}{color(0cm)=(red);
color(2cm)=(green); color(4cm)=(blue); color(6cm)=(red);
color(8cm)=(green); color(10cm)=(blue); color(12cm)=(red);
color(14cm)=(green)}
\hbox{\pgfuseshading{colorful}\hskip-14cm\hskip1cm
\pgfimage[height=4cm]{pgf-apple}\hskip1cm
\pgfimage[height=4cm]{pgf-apple.mask}\hskip1cm
\pgfdeclaremask{mymask}{pgf-apple.mask}
\pgfimage[mask=mymask,height=4cm,interpolate=true]{pgf-apple}}