Skip to content

Zorg.Pdf's gaps: memory inputs, ShrinkTo, Convert, IsOpaque, and an experimental canvas - #19

Merged
alftore merged 9 commits into
mainfrom
canvas-images
Nov 14, 2026
Merged

alftore merged 9 commits into
mainfrom
canvas-images

Conversation

@alftore

@alftore alftore commented Nov 14, 2026

Copy link
Copy Markdown
Owner

What Zorg.Pdf's report asked of the library (its 05-zorg-imaging-gaps.md): ways to read an image inside a larger buffer, shrink on load without the pipeline, change pixel layout, tell whether alpha is used, and a canvas to draw page previews. Nothing the library already did runs differently; the everyday paths were timed before and after (docs/performance.md).

New calls

  • Image.Decode, Image.Identify, AnimationReader.Open and ImagePipeline.Load take ReadOnlyMemory<byte>; the byte[] versions forward to them.
  • DecodeOptions.ShrinkTo: shrink-on-load for Image.Decode. A 4000 x 3000 JPEG to 400 x 300: 43 ms instead of 132. A pipeline ignores it; its own steps decide.
  • Image.Convert(PixelFormat) (refuses to drop alpha that's in use, pointing to Flatten) and Image.IsOpaque(). RGBA to RGB at 12 MP: 5.9 ms.

The canvas (Zorg.Imaging.Drawing, marked [Experimental("ZORGDRAW")] so it can still change or move to its own package)

  • Fills of lines and curves with exact pixel coverage, both fill rules; round-pen strokes with exact corners; straight rules with a minimum hairline width; images through any affine transform, pre-shrunk with Resize.
  • Colours blend in the stored values, as PDF viewers do. Working memory at most 4 MB at any canvas size.
  • An A4 page at 150 dpi with 3,000 glyphs: 14.7 ms, nothing allocated (SkiaSharp: 15.6 ms). With synthetic bold: 55 ms (Skia: 48.7).
  • Trimmed away when unused: the AOT smoke binary grows 40 KiB once it draws.

Checks

  • Coverage tests against areas worked out by formula; hostile shapes draw or throw an ArgumentException.
  • The Alpine gate gains rsvg-convert: fills and strokes against cairo (total ink within 0.3 % and 0.5 %, every 4 x 4 block within a third of a pixel for fills). ImageMagick stays as the loose check.
  • A review pass fixed ShrinkTo leaking into crop-first pipelines, row drift on long edges, a dot for a lone MoveTo, hairlines under stretched transforms and inconsistent overflow handling, each with a test.

Local runs: 2,632 tests on Windows; the Linux gate with AOT (1,955 tests, no skips, 36 AOT checks).

A file inside a larger buffer no longer has to be copied into its own array. The byte[] methods forward to the new ones; RS0026 is suppressed with the reason, since no argument converts equally well to both. Doc crefs name the byte[] overloads.
Maps a public box to the internal shrink target the pipeline already sets, using the same Inside fit, so Decode with ShrinkTo then Fit gives the pipeline's pixels. The pipeline's own target still wins. Decoding never resizes: formats without a reduced decode come out at full size.
Convert changes the pixel layout: 8 to 16 bits and back exactly, grey by Rec. 709 luma, alpha dropped only when every pixel is opaque (otherwise it throws and points at Flatten). Vector paths for the common 8-bit pairs, 16-bit RGBA rows for the rest. IsOpaque scans alpha, stopping at the first chunk with a translucent pixel. Nothing in the library calls either, so no existing output or timing changes.
Zorg.Imaging.Drawing: Shape (lines, quadratic and cubic curves), Canvas with Fill, FillRectangle, Clear, a transform and a clip rectangle, both fill rules. Coverage is exact pixel area by signed-area accumulation; colours blend in the stored values, as PDF viewers do. Marked [Experimental("ZORGDRAW")] so it can still change or move to its own package. Nothing existing calls it; the AOT smoke binary is unchanged in size.
Stroke builds the area a round pen covers: a rectangle per line, a wedge on the outer side of each corner and half a disc at each end, wound alike and filled non-zero. The wedges' straight edges lie on the rectangles' ends and cancel them, so the outer edge doesn't double-count coverage. Widths scale with the transform; MinimumStrokeWidth keeps hairlines visible.
DrawImage fills the image's transformed rectangle, so its edges are anti-aliased, and samples each covered pixel through the inverse transform: bilinear and premultiplied, or nearest as blocks. An image drawn smaller is resized first with the core Resize. The image must share the canvas's colour space.
The smoke test draws a shape, a stroke and an image under Native AOT. A/B timings of the JPEG thumbnail, PNG decode and JPEG and PNG encodes on main and this branch are within noise.
… oracle

- Clear copies whole rows; 8-bit layouts blend in their own loops; rows of
  coverage reach the painter through a generic struct, so a page of glyphs
  allocates nothing. A4 page of 3,000 glyphs: 22.8 ms to 14.7 ms (Skia 15.6).
- Tall fills render in bands: working memory at most 4 MB at any size.
- A stroke's segments end at their inner corner instead of overlapping, as an
  overlap was counted twice in the pixels it crossed.
- rsvg-convert in the Alpine gate: fills and strokes checked against cairo's
  coverage (ink within 0.3 % and 0.5 %, every 4 x 4 block within a third of a
  pixel for fills).
- CanvasBenchmarks times the page against SkiaSharp.
- The pipeline ignores DecodeOptions.ShrinkTo: its own steps decide, so a
  pipeline that crops first decodes at full size again (it cropped the
  wrong region, or threw).
- Rows of a fill take their x from the line's equation, not by stepping:
  long edges were off by up to 2.6/255 at their far end, and differed
  between bands and clips.
- Stroke: a lone MoveTo paints nothing, as in PDF and SVG; points too close
  for a float to give a direction make a dot instead of throwing; a pen
  narrower than MinimumStrokeWidth in every direction is drawn that wide in
  pixels, so stretched transforms keep hairlines one pixel wide.
- Gentle corners (curves cut into lines) are mitred on the outer side too,
  without a round wedge: the bold page takes 55 ms instead of 81.5 (Skia
  48.7).
- An overflowing transform is refused whether it ends in an infinity or
  NaN, FillRectangle included; DrawImage refuses the canvas's own image;
  Clear and Convert check for disposal before anything else.
@alftore
alftore merged commit 03c63a8 into main Nov 14, 2026
6 checks passed
@alftore
alftore deleted the canvas-images branch November 14, 2026 19:48
@alftore alftore mentioned this pull request Nov 14, 2026
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