Skip to content

Send an image to the backend as an image - #6

Merged
certik merged 2 commits into
mainfrom
backend2
Aug 15, 2026
Merged

certik merged 2 commits into
mainfrom
backend2

Conversation

@certik

@certik certik commented Aug 15, 2026

Copy link
Copy Markdown
Member

imshow was drawing one rectangle per sample, so draw_image had no callers and two of its three implementations were empty. The image is now one draw_image call: SVG embeds a PNG data URI, PDF writes an image XObject sharing the PNG backend's compressor. On the suite's small images that is 12049 bytes of SVG down to 6532 and 14433 of PDF down to 4828, and the ratio grows with the image.

The raster is sent at roughly the size it is drawn, because a renderer asked to enlarge a 16x8 image usually interpolates it into a blur; repeating each sample a whole number of times is exactly nearest neighbour. That took the imshow case through cairosvg from 5.3 to 1.6 against matplotlib.

A blit assumes evenly spaced samples, which a log axis breaks, so the per-cell path stays for that case and there is now a log-axis imshow case covering it (1.4 against matplotlib).

Also: test_plots saved three formats and announced only the SVG. The four repeated lines per case are now one call to a local helper, a hundred lines shorter.

44/44 PNG at 1.88, 44/44 PDF at 2.08, 132/132 outputs byte-identical across flang and LFortran.

certik added 2 commits August 15, 2026 15:15
Each case saved an SVG, a PDF and a PNG and then announced only the SVG, so
the log read as though two thirds of the work had not happened.

The line was wrong in the same way 43 times over, which is the more useful
thing the report exposed: the four lines that end every case were copied out
by hand, and the three savefig calls could disagree with each other or with
the message without anything noticing. They are one call to a local helper
now, and the options are forwarded rather than repeated, so an absent
optional stays absent and a plain case still says nothing about facecolor or
dpi. The file is a hundred lines shorter and each case ends with the name it
is saved under.
imshow was drawing one rectangle per sample. That was written when SVG was
the only backend and the API had no way to say "raster", and it stayed after
draw_image was added, so draw_image had no callers and two of its three
implementations were empty.

The cost is per sample: an 8x16 image is 128 elements, and a 200x200 image
would be 40000. Every format can place a raster in one object, so the whole
image is now one draw_image call. SVG embeds a PNG in a data URI, which is
what matplotlib does, and PDF writes an image XObject with the samples as a
FlateDecode stream, sharing the compressor the PNG backend already has. On
the small images in the test suite that is 12049 bytes of SVG down to 6532
and 14433 of PDF down to 4828; the ratio grows with the image.

The raster is sent at roughly the size it is drawn rather than at data
resolution. This is not a detail: a renderer asked to enlarge a 16x8 image
will usually interpolate it, and at these sizes interpolation turns data into
a blur. Repeating each sample a whole number of times is exactly nearest
neighbour, so it invents nothing, and it took the imshow case rendered
through cairosvg from 5.3 to 1.6 against matplotlib. image-rendering is set
as well, for renderers that honour it.

A blit assumes the samples are evenly spaced on the canvas, which stops being
true the moment an axis is log, so the per-cell path stays for that case. It
was about to become code nothing reached, so there is now a log-axis imshow
case in the suite; it lands at 1.4 against matplotlib, which also warps the
image rather than stretching it.

The three backends agree on where the image goes and which way up it is,
which is worth stating because it is the easy thing to get wrong: PDF draws
an image into the unit square with the first row at the top, under a page
matrix that has already flipped y, and fplot's own rows run up the extent,
with origin="upper" expressed as a descending axis rather than as reversed
data. Checked by comparing each backend against a vertically flipped
reference: every one of them is ~1.5 the right way up and ~23 upside down.
@certik
certik merged commit 97fae13 into main Aug 15, 2026
1 check passed
@certik
certik deleted the backend2 branch August 15, 2026 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant