Chapter 24
Image Internals

The primary use of a fz_image is to allow a rendered pixmap to be retrieved. This is done by calling:

/* 
   fz_get_pixmap_from_image: Called to get a handle to a pixmap from 
   an image. 
 
   image: The image to retrieve a pixmap from. 
 
   subarea: The subarea of the image that we actually care about (or 
   NULL to indicate the whole image). 
 
   trans: Optional, unless subarea is given. If given, then on entry 
   this is the transform that will be applied to the complete image. 
   It should be updated on exit to the transform to apply to the given 
   subarea of the image. This is used to calculate the desired 
   width/height for subsampling. 
 
   w: If non-NULL, a pointer to an int to be updated on exit to the 
   width (in pixels) that the scaled output will cover. 
 
   h: If non-NULL, a pointer to an int to be updated on exit to the 
   height (in pixels) that the scaled output will cover. 
 
   Returns a non NULL pixmap pointer. May throw exceptions. 
*/ 
fz_pixmap *fz_get_pixmap_from_image(fz_context *ctx, fz_image *image, const fz_irect *subarea, fz_matrix *trans, int *w, int *h);

Frequently this will involve decoding the image from its source data, so should be considered a potentially expensive call, both in terms of CPU time, and memory usage.

To minimise the impact of such decodes, fz_images make use of the Store (see chapter 7 Memory Management and The Store) to cache decoded versions in. This means that (subject to enough memory being available) repeated calls to get a fz_pixmap from the same fz_image (with the same parameters) will return the same fz_pixmap each time, with no further decode being required.

The usual reference counting behaviour applies to fz_images, with fz_keep_image and fz_drop_image claiming and releasing references respectively.

Depending on the size at which a fz_image is to be used, it may not be worth decoding it at full resolution; instead, decoding it at a smaller size can save memory (and frequently time). In addition, subsequent rendering operations can often be faster due to having to handle fewer pixels for no quality loss in the final output.

To facilitate this, fz_images will subsample images as appropriate. Subsampling involves an image being decoded to a size an integer power of 2 smaller than their native size. For instance, if an image has a native size of 400x300, and is to be rendered to a final size of 40x30, fz_get_pixmap_from_image may subsample the returned image by up to 8 in each direction, resulting in a 50x37 image.

Subsequent operations (such as smooth scaling and rendering) will proceed much faster due to fewer pixels being involved, and around one sixteenth of the memory will be required.

Various different implementations of fz_image exist within MuPDF.

 24.1 Compressed Images
 24.2 Pixmap Images