Skip to content

add flat quantization tables to JPEG encoding - #87

Draft
matmen wants to merge 1 commit into
masterfrom
jpeg-flat-tables
Draft

matmen wants to merge 1 commit into
masterfrom
jpeg-flat-tables

Conversation

@matmen

@matmen matmen commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Adds encodeJPEG({ quantization: 'flat' }): every detail is quantized alike instead of by the JPEG standard's tables. Edges and text come out clearer, and measured by PSNR and SSIM the files are about 10% smaller on average at the same quality (more or less by image).

Refs #85.

await image.encodeJPEG({ quality: 45, quantization: 'flat' });

'standard' by default, so existing files stay byte for byte the same. The same quality makes a larger file with 'flat', so lower it to keep the size (by 15 to 25 for photos). The gain map isn't affected. The table is mozjpeg's flat one (all 16, scaled by the quality like the standard's), which mozjpeg uses for PSNR.

Measured with mozjpeg's cjpeg (no trellis) on a photo, a 2400×1600 photo and a screenshot at qualities 60, 80 and 92: at the same PSNR the files are 13% smaller on average (from 3% to 30%), at the same SSIM 9% (from 21% smaller to 15% larger, the photo at quality 60 is larger). I looked at crops at the same file size and found edges clearer and fewer artifacts.

Checked:

  • cjpeg -qtables (libjpeg-turbo 2.1.5) with sixteens gives identical files for 432 cases; tests/jpeg-quantization.js keeps 9 of them as hashes, plus one check that strokes come out with less error at the same file size.
  • Node, Bun and Deno; lint, typecheck and the suite pass.

@matmen matmen linked an issue Nov 28, 2026 that may be closed by this pull request
@github-actions

Copy link
Copy Markdown
Contributor

Test coverage

Compared with master where the branch left it (1de03d3).

Lines Branches Functions
total 97.7% 90.8% 97.9%
src/codecs/jpeg.ts (157 lines untested) 92.2% (+0.1) 90.3% (+0.2) 91.7%
src/codecs/av1/decode.ts (141 lines untested) 91.7% 84.0% 97.9%
src/render/layout.ts (105 lines untested) 93.6% 87.7% 95.7%
src/render/svg.ts (89 lines untested) 97.9% 89.9% 97.3%
src/render/shape.ts (73 lines untested) 92.8% 88.0% 98.1%
src/codecs/jxl/headers.ts (62 lines untested) 87.0% 71.0% 86.4%
src/codecs/av1/obu.ts (43 lines untested) 93.0% 73.9% 92.8%
src/codecs/jxl/modular.ts (43 lines untested) 94.5% 82.7% 100.0%
src/codecs/jxl/fast-lossless.ts (37 lines untested) 94.9% 97.8% 93.0%
src/codecs/av1/frame.ts (35 lines untested) 73.1% 33.3% 100.0%

Changed

Lines Branches Functions
src/codecs/zlib.ts 99.8% (+0.8) 92.2% (+0.4) 100.0%
src/codecs/jpeg.ts 92.2% (+0.1) 90.3% (+0.2) 91.7%

This branch has not been deployed

No deployments
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.

mozjpeg support?

1 participant