Skip to content

add flat quantization tables to JPEG encoding - #87

Merged
matmen merged 2 commits into
masterfrom
jpeg-flat-tables
Nov 29, 2026
Merged

matmen merged 2 commits into
masterfrom
jpeg-flat-tables

Conversation

@matmen

@matmen matmen commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Adds encodeJPEG({ quantization: 'flat' }): the same quantization step for every frequency instead of the JPEG standard's tables. Edges and text come out clearer with fewer visible artifacts.

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 until the size matches (by about 15 to 25 around quality 60 for photos); qualities 96 to 100 give the same file. The gain map isn't affected. The table is mozjpeg's flat one (all 16, scaled by the quality like the standard's).

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, checks that strokes come out with less error at the same file size, and that the option reaches transparent images and the primary image of a gain map JPEG but not the gain map.
  • 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

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Test coverage

Compared with master where the branch left it (07793ce).

Lines Branches Functions
total 97.7% 90.8% 98.0%
src/codecs/jpeg.ts (145 lines untested) 94.1% 91.7% 94.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.0% (-0.8) 91.7% (-0.4) 100.0%

@matmen
matmen marked this pull request as ready for review November 29, 2026 17:02
@matmen
matmen merged commit d9e90a7 into master Nov 29, 2026
17 checks passed
@matmen
matmen deleted the jpeg-flat-tables branch November 29, 2026 20:17
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