GNU TeXmacsUser manualReferenceAboutEnglish
Image files: sizes and conversions
System/Files/image_files.cpp

is the single entry point for everything that concerns image files: their original size and their conversion into the three formats which the rest of TeXmacs needs (PNG for the screen, PDF and EPS for printing). Each function tries the available tools in a fixed order, depending on the build options QTTEXMACS, MUPDF_RENDERER, USE_RESVG, USE_GS, PDF_RENDERER, USE_IMLIB2 and MACOSX_EXTENSIONS, and on the external programs found at run time.

1.Image sizes

void image_size (url image, int& w, int& h)
(original size in points)

Return the size of an image in points (1/72 inch), from the size cache img_box if possible. Otherwise call image_size_sub and cache the result. If no size can be determined, report an error and use \(35 \times 35\) points.

image_size_sub dispatches on the suffix and on the available tools, in this order:

  1. PDF: pdf_image_size, which uses the crop box of the first page as read by PDFHummus (hummus_pdf_image_size), or, without the PDF renderer, Ghostscript (gs_PDFimage_size), or else ImageMagick. Rotated pages have their width and height exchanged.

  2. SVG: svg_image_size, which tries resvg, then Qt, and finally reads the width and height attributes of the svg element. Both libraries assume 96 pixels per inch.

  3. PostScript and EPS: the %%BoundingBox comment (ps_bounding_box, ps_read_bbox), which also stores the origin of the box in the size cache.

  4. On macOS builds without Qt 6: mac_image_size.

  5. Formats readable by Qt (qt_supports, which excludes PDF and PostScript on purpose, because Qt renders them blurred): qt_image_size, which converts pixels to points with the resolution stored in the file (QImage::dotsPerMeterX).

  6. Imlib2 (X11 builds only).

  7. Ghostscript, for PostScript files without a bounding box: gs_image_size runs the bbox device.

  8. ImageMagick: imagemagick_image_size runs identify and converts with the density recorded in the file; an undefined density is taken as 90 pixels per inch.

Two other functions give sizes in pixels. native_image_size returns the natural size of a raster or SVG image (with resvg or Qt), and otherwise the size in points scaled to 300 pixels per inch. qt_pretty_image_size (in

Plugins/Qt/qt_utilities.cpp

) proposes a width and height for a newly inserted image: its size in points, or 1par if it is wider than the paragraph.

2.Conversions

void image_to_png (url image, url png, int w= 0, int h= 0)
(convert to a bitmap)

Convert an image to PNG at \(w \times h\) pixels. The tools are tried in this order: the macOS frameworks (builds without Qt 6), Qt (qt_convert_image, which renders SVG files with QtSvg), Ghostscript (gs_to_png, for PostScript and PDF), a Scheme converter (call_scm_converter), and finally ImageMagick. If no output file was produced, unknown.png is copied instead.

void image_to_eps (url image, url eps, int w_pt= 0, int h_pt= 0, int dpi= 0)
(convert to EPS)
void image_to_pdf (url image, url pdf, int w_pt= 0, int h_pt= 0, int dpi= 0)
(convert to PDF)

Convert an image to a vector format, sized \(w\_\operatorname{pt} \times h\_\operatorname{pt}\) points. Both functions first try to keep vector images vectorial: an SVG file which Qt cannot read goes through a Scheme converter (in practice Inkscape), and PostScript or PDF files go through Ghostscript (gs_to_eps, gs_to_pdf). Raster images are then wrapped with Qt (qt_image_to_pdf, which prints the image with a QPrinter, downsampling it to dpi if it is larger), and as a last resort converted with ImageMagick. image_to_pdf also tries a Scheme converter before ImageMagick. On failure, unknown.eps or unknown.pdf is copied to the destination.

image_to_psdoc (image) converts an image to a temporary EPS file and returns its contents as a string; ps_load (image) returns the contents of PostScript files directly and calls image_to_psdoc for other formats. image_to_psdoc is also exported to Scheme as image->psdoc, and serves as the converter from bitmap formats to postscript-document in

init-images.scm

.

3.Scheme converters

call_scm_converter (image, dest) asks the Scheme converter graph whether a converter exists between the two suffixes (file-converter-exists? on dummy names x.suf) and, if so, runs it with file-convert. It returns whether the destination exists afterwards. The converter graph itself is described in converters to other data formats.

The image formats and their converters are declared in $TEXMACS_PATH/progs/convert/images/init-images.scm with define-format and converter. Each converter has a :require clause which tests for the external program, so the graph adapts to the installation. The main ones are:

PDF

to PostScript with Ghostscript (gs-convert, device eps2write); to PNG, JPEG and TIFF with pdftocairo, Ghostscript or ImageMagick, at the resolution given by the preference texmacs->image:raster-resolution (300 by default); to SVG with pdftocairo or pdf2svg.

PostScript

to PDF with ps2pdf.

SVG

to PostScript, PDF and PNG with Inkscape; to PNG with rsvg-convert when Inkscape is absent; to a PostScript document with image->psdoc when Qt 5 or later is available.

Other vector formats

Xfig (fig2ps), Xmgrace and Geogebra, to PostScript (and SVG or PNG for Geogebra).

Bitmaps

JPEG, TIFF, GIF, PNG and PNM to a PostScript document with image->psdoc, and between each other with ImageMagick's convert.

The same file defines gs-binary, has-gs?, has-convert? and has-pdftocairo?. On Windows, convert is never used, because a system command of that name exists.

4.Ghostscript

Plugins/Ghostscript/gs_utilities.cpp

(compiled with USE_GS, which the CMake build always sets) runs the Ghostscript executable; nothing is linked. The executable is gs (on Windows, the first gswin*c.exe found under C:\Program Files*\gs), or the copy shipped with TeXmacs when GS_EXE is defined. The functions are:

has_gs, gs_prefix, eps_device

Availability, the quoted command, and the EPS device, eps2write for version 9.14 and later and epswrite before.

gs_supports

True for ps, eps and pdf.

gs_image_size, gs_PDFimage_size

Sizes: the bounding box comment or the bbox device for PostScript; the crop box (or media box) and rotation reported by pdf_info.ps for PDF.

gs_to_png, gs_to_eps, gs_to_pdf

Conversions of images. PostScript inputs are translated by the origin of their bounding box with inline PostScript code, rather than with -dEPSCrop, which mishandles boxes not starting at the origin. gs_to_eps restores the original bounding box afterwards (gs_fix_bbox), because the EPS devices compute their own.

gs_to_pdf (doc, pdf, landscape, h, w), gs_to_ps

Conversions of whole printed documents, used by edit_main_rep::print_doc when printing to the format which the native printer does not produce.

gs_PDF_EmbedAllFonts

Re-distill a PDF file with all fonts embedded (see images in PDF output).

gs_check

Run a document through the nullpage device and report errors.

pdf_version

The PDF version of a file, or the version selected in the preference texmacs->pdf:version (1.4 by default).

Plugins/Ghostscript/ghostscript.cpp

is only used by the X11 port: ghostscript_run renders a PostScript image into an X11 pixmap through the GHOSTVIEW protocol.

5.ImageMagick

ImageMagick is the last resort of all conversions. has_image_magick tests for convert in the path (always false on Windows), and call_imagemagick_convert runs convert, downsampling raster images which are larger than needed for the requested size and resolution. If neither Ghostscript nor ImageMagick is installed, the first failed conversion prints a recommendation to install them (inform_about_dependencies).

6.Pitfalls

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".