Skip to content

Conversation

@eirikur-nc
Copy link

@eirikur-nc eirikur-nc commented Dec 25, 2025

What

Primary

  • Add compression support for RGBA images

Secondary

  • Fix a bug which caused the alpha channel to be ignored.
  • Improve filter performance by using typed arrays.
  • Omit softmask if all pixels are opaque.
  • Modernize the code in rgba_support.js a bit.

Closes #3933

Why

To provide a more performant path for adding images that use lossless compression.
To measure the impact of these changes, I created simple benchmark that converted a dummy html report into an image (via html2canvas) and then added that to a PDF.

image-format-benchmark.html

Here are the results of running that benchmark before and after at MEDIUM compression.

Before

Screenshot 2025-12-29 at 11 24 24

After

Screenshot 2025-12-29 at 11 24 36

This shows that it is now possible to use the RGBA path to add lossless images at file sizes that are comparable to PNGs while taking roughly 30% less time. This time difference is attributable to the PNG path having an additional decode stage which the RGBA path does not.

The PNG path also benefits from the filter optimizations, yielding a ~8% reduction in execution time at medium compression.

The JPEG path is still much faster, as it performs minimal processing of images. It would be interesting to see if a similar approach would be feasible for PNGs.

How

By

  • Extracting compression functions from png_support.js into a new module named compression_utils.js so that they can be re-used without duplication.
  • Modifying rgba_support.js to compress the image data when applicable.
  • Returning the alpha channel using the sMask property, rather than as alpha which was never read.

Comment on lines 76 to -83
it("with alpha", () => {
const c = document.createElement("canvas");
const ctx = c.getContext("2d");
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0, 0, 150, 60);
ctx.fillStyle = "#AA00FF77";
ctx.fillRect(10, 10, 130, 40);
const dataFromCanvas = ctx.getImageData(0, 0, 150, 60);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was flawed. It specified alpha values when drawing on the canvas object. The resulting image however only had fully opaque pixels, with the color blending taking place within the canvas element. I therefore changed it to explicitly set alpha values.

@eirikur-nc eirikur-nc marked this pull request as ready for review December 29, 2025 12: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.

request: Support compression for RGBA images

1 participant