valerybokov opened a new pull request, #491:
URL: https://github.com/apache/pdfbox/pull/491

   PDFBOX-6077: Fix stencil masks filled with a pattern
   
   Summary
   
   A PDF image used as a stencil mask (an ImageMask) can be filled with a 
pattern instead of a solid color. PDFBox renders this case specially: it draws 
the pattern's paint and the mask into two separate scratch images, then 
combines them pixel-by-pixel before compositing the result onto the page. Two 
independent bugs in that combine step caused patterns used this way to render 
incorrectly.
   
   Bug 1 — mask alpha overwrote the paint's own alpha
   
   The combine step did:
   rasterPixel[3] = alphaPixel[0];
   unconditionally replacing the paint's alpha with the mask's alpha. Any pixel 
the pattern itself never painted into — for example the gaps between tiles of a 
tiling pattern — has alpha 0 in the paint image, but that got overwritten with 
the mask's (opaque) alpha, turning transparent gaps into solid black.
   
   Fix: combine the two alphas by multiplication instead of overwriting:
   rasterPixel[3] = rasterPixel[3] * alphaPixel[0] / 255;
   so a pixel is only visible where both the pattern painted something and the 
mask allows it through.
   
   Bug 2 — soft-masked patterns rendered fully transparent
   
   A pattern can itself have a soft mask applied to it (SoftMask, wrapping the 
pattern's own Paint). SoftMask looks up its backing (grayscale) raster using 
absolute page-device pixel coordinates, fixed at the point the soft mask's 
transparency group was rendered. Because the stencil-mask-with-pattern code 
fills into an isolated scratch image — not the real page Graphics2D — those 
coordinates no longer lined up with anything, and the soft mask silently 
applied zero alpha everywhere, making the pattern disappear entirely.
   
   Fix: unwrap the soft mask, fill the scratch image with its plain underlying 
paint, then apply the soft mask's own alpha afterward via a new 
PageDrawer.applySoftMaskAlpha(), which looks up the mask's backing raster 
directly using a per-pixel device-coordinate transform — correct regardless of 
the scratch image's resolution — rather than relying on the Paint/PaintContext 
machinery that assumes it's rendering onto the real page raster. SoftMask gains 
a few narrow package-private accessors for this.
   
   Commits
   
   1. PDFBOX-6077: combine stencil mask alpha with the pattern's own alpha
   2. PDFBOX-6077: fix soft-masked patterns used as a stencil mask fill
   
   ---------------
   Per https://www.apache.org/legal/generative-tooling.html: portions of this 
PR were
   produced with assistance from Claude Code (Anthropic), based on a bug was 
described in Apache PDFBOX Issue Tracker.
   
   I've reviewed the generated code and confirm to the best of my knowledge 
that the output does not include any
   third-party copyrighted material and is compatible with the Apache License 
2.0.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to