![]() |
Pictures, effects and picture caches | ![]() |
The class picture, its kinds, its pixel access and its conversion functions are introduced in the renderer API (section “pictures, scalable images and off-screen rendering”). This page adds the implementation side: the portable raster pictures, the effect engine, scalable images and the three caches through which image files reach the screen.
raster<C> (
Graphics/Pictures/raster.hpp
) is a reference counted array of pixels of type C with a width w, a height h and an origin (ox, oy); the pixels are stored row by row in a. The header implements, as templates, the generic operations on such arrays: mapping a pixel operator over a raster, composing two rasters with one of the operators of
raster_operators.hpp
, shifting, magnifying, convolving with a pen, and so on. The pixel type used in practice is true_color (
Graphics/Colors/true_color.hpp
), four doubles with non-premultiplied alpha.
raster_picture_rep<C> (
raster_picture.hpp
) wraps a raster as a picture. Pixels outside the raster read as transparent black and writes to them are ignored. Two helpers convert between the representations: raster_picture (raster<C>) wraps a raster, and as_raster<C> (picture) returns the raster of a picture, copying it pixel by pixel into a new raster picture if the picture is of another kind (for instance a native Qt picture). The non template raster_picture (w, h, ox, oy) creates an empty true_color picture.
raster_picture.cpp
implements all the picture operations declared in
picture.hpp
(compose, draw_on, shift, magnify, blur, thicken, color_matrix, make_transparent, …) by converting their arguments with as_raster<true_color>, calling the raster template and wrapping the result. Every operation therefore returns a fresh raster picture, whatever the kind of its arguments.
raster_random.cpp
contains the random functions behind turbulence, fractal_noise, degrade, distort and gnaw.
Native pictures are cheaper to draw on the screen: as_native_picture converts a raster picture back into a picture of the GUI back-end (qt_picture_rep wrapping a QImage). A typical effect thus converts native pictures to rasters, computes on rasters, and lets the renderer convert the result back when it draws it.
An effect is a function from a list of pictures to a picture. It is described in documents by a tree built from the eff-* tags, for instance
<eff-blur||<eff-gaussian|2px>> |
which blurs its first argument with a Gaussian pen. Effects are used in two places: as the last argument of the gr-effect primitive, which applies them to typeset content (through effect_box, see the renderer API), and as the effect of a background pattern (see images in documents).
Declared in
Graphics/Pictures/effect.hpp
; effect is the corresponding ABSTRACT_NULL handle. Its methods are
The logical extents of the result, given those of the arguments; by default the extents of the first argument.
The ink extents of the result; for instance a blur makes the result larger than its argument by the radius of the pen.
Compute the resulting picture. pixel is the size of a pixel of the pictures in logical units, which is needed to convert lengths of the effect (such as the radius of a pen) into pixels.
build_effect (tree t) (
effect.cpp
) translates a description into an effect object. The empty string stands for the first argument (argument_effect (0)) and an integer \(i\) for the argument number \(i\); compound trees are dispatched on their label:
eff-move, eff-magnify, eff-bubble, eff-crop.
eff-gaussian, eff-oval, eff-rectangular (with one radius, or two radii and an angle) and eff-motion. Pens are effects without arguments which produce the pen picture.
eff-blur, eff-outline, eff-thicken, eff-erode, each taking an effect and a pen.
eff-turbulence, eff-fractal-noise, eff-hatch, eff-dots, eff-degrade, eff-distort, eff-gnaw.
eff-superpose, eff-add, eff-sub, eff-mul, eff-min, eff-max (any number of effects) and eff-mix.
eff-normalize, eff-monochrome, eff-color-matrix, eff-gradient, eff-make-transparent, eff-make-opaque, eff-recolor, eff-skin. eff-monochrome and eff-gradient are translated into color matrices.
An unrecognized tree yields the first argument unchanged, so a mistyped effect is silently ignored rather than reported.
Each effect class (blur_effect_rep, compose_effect_rep, …) first applies its sub-effects to the argument pictures and then calls the corresponding picture operation of
raster_picture.cpp
.
A scalable (
Graphics/Pictures/scalable.hpp
) is described in the renderer API. Its only implementation, scalable_image_rep (
scalable.cpp
), holds an image file u, a size (w, h) in logical units, an effect eff and the pixel size px at which it was last drawn. Its logical and physical extents are those of the effect applied to the rectangle (0, 0, w, h), computed once with build_effect.
Its draw (ren, x, y, alpha) method loads the picture at the size w/ren->pixel \(\times\) h/ren->pixel pixels with cached_load_picture and draws it with draw_picture. Because the size in pixels depends on the zoom factor, each zoom level produces a different picture. The constructor and destructor of scalable_image_rep, and draw when the pixel size changes, reserve and release the corresponding cache entry (see below).
Three caches are involved when an image file is drawn on the screen:
The table img_box of
System/Files/image_files.cpp
maps the tree of an image URL to its size in points and, for PostScript files, the origin of its bounding box. It is filled by image_size, ps_bounding_box and gs_image_size and cleared entry by entry with clear_imgbox_cache, or completely with clearall_imgbox_cache.
The tables of
picture.cpp
map a key (url, w, h, effect), where w and h are sizes in pixels, to the loaded picture and to the modification time of the file when it was loaded (picture_stamp). A reference count per key (picture_count), maintained by picture_cache_reserve and picture_cache_release, tells which pictures are still used by some image box; keys whose count dropped to zero are put on a black list.
The table qt_pic_cache of
Plugins/Qt/qt_picture.cpp
maps (url, w, h) (plus the effect and the pixel size when there is an effect) to the QImage read from disk.
cached_load_picture (u, w, h, eff, pixel, permanent) returns the cached picture if there is one and the file has not been modified since it was loaded; if the file has changed, it also clears the size cache entry of the file, since its size may have changed. Otherwise it calls the back-end function load_picture (u, w, h, eff, pixel) and stores the result if permanent is set or if the key is reserved. scalable_image_rep::draw passes permanent= false, so that only pictures of image boxes which still exist are kept.
picture_cache_clean () is called after each typesetting pass (edit_typeset_rep::typeset_sub, in
Edit/Editor/edit_typeset.cpp
). At most once per minute, it removes the black listed entries whose count is still zero. picture_cache_reset () empties the picture cache, the size cache and the Qt image cache; it is exported as picture-cache-reset and called by picture-gc in
texmacs/texmacs/tm-tools.scm
.
With Qt, load_picture (
qt_picture.cpp
) calls get_image, which looks in qt_pic_cache and otherwise calls get_image_for_real:
for SVG files with USE_RESVG, the file is rendered by resvg directly at the requested size;
otherwise, a format which Qt can read (qt_supports) is loaded into a QImage; any other format is first converted to a temporary PNG file with image_to_png;
the image is scaled to the requested size (ignoring the aspect ratio, which the typesetter has already taken into account);
if there is an effect, it is built with build_effect and applied, with the image as its only argument.
If the image cannot be read, load_picture returns error_picture (w, h), a translucent red rectangle. The X11 version of load_picture (
Plugins/X11/x_picture.cpp
) renders the file into a pixmap with Imlib2 if possible and with Ghostscript otherwise (ghostscript_run, which converts the image to PostScript first).
The icons of menus and toolbars are loaded with load_xpm (url), which caches them by name. With Qt it calls qt_load_xpm, which prefers a PNG version of the icon (_x2.png or _x4.png on high density screens) in $TEXMACS_PIXMAP_PATH, falls back on the xpm file and finally on TeXmacs.xpm. With a style sheet, the icons are rescaled to the interface scale, and with a dark style sheet their colors are inverted (except for the flags of languages). Without Qt, load_xpm parses the xpm file itself (xpm_load in
image_files.cpp
).
save_picture (dest, p) writes a picture to a file in the format given by the suffix (with Qt and Cocoa; the MuPDF version of SDL and Vue only writes PNG, and the X11 version fails). picture_as_eps (p, dpi) encodes a picture as an EPS image with an ASCIIHexDecode data stream and, if the picture has transparent pixels, a one bit mask (pixels with an opacity of at most 32 are masked); colors are first blended with white according to their opacity. It is used by the PostScript printer to draw pictures. apply_effect (eff, src, dest, w, h) (
image_files.cpp
, exported as apply-effect) loads image files at a given size, applies an effect to them and saves the result; it only works with Qt (qt_apply_effect).
The Qt image cache qt_pic_cache has no expiry and does not record modification times. When an image file is modified, picture_is_cached notices it and calls load_picture again, but get_image then returns the old QImage from its own cache. The new contents only appear after picture-gc. The cache also keeps one QImage per size in pixels, that is, per zoom level, for the whole session.
In build_effect, the test for eff-color-matrix reads NR (m) != 4 && NC (m) != 5 (
Graphics/Pictures/effect.cpp:783
); it should use ||. A matrix with four rows and fewer than five columns passes the test and is then read out of bounds.
The cases for eff-make-transparent, eff-make-opaque, eff-recolor, eff-skin and eff-normalize (effect.cpp:764-822) do not check the arity of the tag before reading t[1] (or t[0]), so a tag with too few children makes the effect parser read past the end of the tree.
effect.hpp
declares gaussian_pen, oval_pen, rectangular_pen, motion_pen and outline, but
effect.cpp
defines gaussian_pen_effect, …, motion_pen_effect and outlines instead; the declared names have no definition, and a call to them fails at link time.
Effects are computed on true_color rasters, pixel by pixel, by portable code; large blurred or shadowed areas are therefore slow, especially at high zoom factors and in printers, which rasterize shadows at a fixed high resolution.