Skip to content

Commit 6ee1bf4

Browse files
committed
address the review of the flat JPEG tables
1 parent 1fdcb97 commit 6ee1bf4

4 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
### New
66

77
- `encodeJPEG({ progressive: true })` writes a progressive JPEG: browsers show the whole picture blurry first and sharpen it while the file loads, and the file is about 3% smaller for photos with the same pixels (a fixed set of scans from the ones mozjpeg tries, each with Huffman tables made for it). Encoding takes 1.1 to 1.3 times as long (a 2400x1600 photo at quality 80: 75 to 95 ms)
8-
- `encodeJPEG({ quantization: 'flat' })` keeps edges and text clearer with fewer visible artifacts: it quantizes every detail alike instead of by the JPEG standard's tables. Measured by PSNR and SSIM the files are about 10% smaller on average at the same quality (more or less by image). They are larger at the same `quality`, so lower that to keep the size (by 15 to 25 for photos). The default stays `'standard'`, and its files don't change
8+
- `encodeJPEG({ quantization: 'flat' })` keeps edges and text clearer with fewer visible artifacts: it uses the same quantization step for every frequency instead of the JPEG standard's tables. At the same PSNR the files were 3 to 30% smaller in a test of photos and a screenshot; at the same SSIM they ranged from 21% smaller to 15% larger. They are larger at the same `quality`, so lower that until the size matches (by about 15 to 25 around quality 60 for photos). The default stays `'standard'`, and its files don't change
99

1010
## 4.0.1
1111

‎src/codecs/jpeg.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,8 @@ const CHROMA_QUANTIZATION = [
14821482
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99
14831483
];
14841484

1485-
// the flat tables of mozjpeg (jcparam.c, quant_tbl_master[1]): every coefficient is quantized alike
1485+
// the flat tables of mozjpeg (jcparam.c, std_luminance_quant_tbl[1] and std_chrominance_quant_tbl[1]): every
1486+
// coefficient is quantized alike
14861487
const FLAT_QUANTIZATION = new Array<number>(64).fill(16);
14871488

14881489
// libjpeg's fixed point RGB -> YCbCr tables (jccolor.c): Y = (Y_R[r] + Y_G[g] + Y_B[b]) >> 16, Cb with CB_R, CB_G and

‎src/image.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,13 @@ export interface JPEGEncodeOptions extends EncodeOptions {
417417
*/
418418
progressive?: boolean;
419419
/**
420-
* How the quality is spent: `'standard'` keeps fine detail the eye notices less (the JPEG standard's tables, which
421-
* blur sharp edges first); `'flat'` treats every detail alike, which keeps edges and text clearer and, measured by
422-
* PSNR and SSIM, makes files about 10% smaller on average at the same quality (more or less by image). The same
423-
* `quality` makes a larger file with `'flat'`: to get the size of `'standard'` lower it (by 15 to 25 for photos). Not
424-
* used for the gain map
420+
* The quantization tables, which decide how coarsely each frequency is stored (not the colors, as in
421+
* {@link Image.quantize}): `'standard'` (the JPEG standard's) stores fine detail more coarsely than smooth areas,
422+
* which softens sharp edges and text first; `'flat'` uses the same step for every frequency, which keeps edges and
423+
* text clearer with fewer visible artifacts. At the same PSNR, files with `'flat'` were 3 to 30% smaller in a test of
424+
* photos and a screenshot; at the same SSIM they ranged from 21% smaller to 15% larger (a photo at a low quality).
425+
* The same `quality` makes a larger file with `'flat'`: lower it until the size matches (by about 15 to 25 around
426+
* quality 60 for photos). Qualities 96 to 100 give the same `'flat'` file. Not used for the gain map
425427
* @defaultValue `'standard'`
426428
*/
427429
quantization?: 'standard' | 'flat';

‎tests/jpeg-quantization.js‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const sha = data => crypto.createHash('sha256').update(data).digest('hex');
4141
// the default is the standard's tables, and the option names them
4242
const standard = Buffer.from(await image.encodeJPEG({quality, subsampling}));
4343
if (!standard.equals(Buffer.from(await image.encodeJPEG({quality, subsampling, quantization: 'standard'})))) panic(`${key}: 'standard' isn't the default`);
44-
// (quality 1 makes both tables 255s, quality 100 ones)
44+
// (quality 1 makes both tables 255s, quality 100 ones; the flat ones are ones from 96)
4545
if (quality > 1 && quality < 100 && standard.equals(flat)) panic(`${key}: 'flat' writes the standard's file`);
4646
await Image.decode(flat);
4747
}
@@ -52,11 +52,29 @@ const sha = data => crypto.createHash('sha256').update(data).digest('hex');
5252
const data = await image.encodeJPEG({subsampling: '4:4:4', ...options});
5353
const decoded = await Image.decode(data);
5454
let error = 0;
55-
for (let i = 0; i < image.bitmap.length; i += 4) error += Math.abs(image.bitmap[i] - decoded.bitmap[i]);
55+
for (let i = 0; i < image.bitmap.length; i++) if (i % 4 !== 3) error += Math.abs(image.bitmap[i] - decoded.bitmap[i]);
5656
return {size: data.length, error};
5757
};
5858
const standard = await measure({quality: 60});
5959
let quality = 60, flat = await measure({quality, quantization: 'flat'});
6060
while (flat.size > standard.size && quality > 1) flat = await measure({quality: --quality, quantization: 'flat'});
61+
if (flat.size > standard.size) panic(`flat tables reach no size below the standard's ${standard.size} bytes`);
6162
if (flat.error >= standard.error) panic(`flat tables' error ${flat.error} isn't below the standard's ${standard.error} on strokes`);
63+
64+
// qualities 96 to 100 make every flat step 1: the same file
65+
const top = await Promise.all([96, 100].map(quality => image.encodeJPEG({quality, quantization: 'flat'}).then(data => withoutExif(Buffer.from(data)))));
66+
if (!top[0].equals(top[1])) panic('flat tables at quality 96 and 100 differ');
67+
68+
// transparent pixels go to the encoder row by row: the option reaches it there too
69+
const translucent = image.clone();
70+
for (let i = 3; i < translucent.bitmap.length; i += 8) translucent.bitmap[i] = 128;
71+
const flattened = new Image(image.width, image.height).fill(0x000000ff).composite(translucent);
72+
const rows = withoutExif(Buffer.from(await translucent.encodeJPEG({quality: 70, quantization: 'flat'})));
73+
if (!rows.equals(withoutExif(Buffer.from(await flattened.encodeJPEG({quality: 70, quantization: 'flat'}))))) panic('a transparent image ignores the flat tables');
74+
75+
// with a gain map: the primary image takes the option, the gain map doesn't
76+
const hdr = await Image.decode(require('fs').readFileSync('./tests/gainmap/multi-channel.jpg'));
77+
const a = await Image.decode(await hdr.encodeJPEG({quality: 70, quantization: 'flat'})), b = await Image.decode(await hdr.encodeJPEG({quality: 70}));
78+
if (!a.gainMap || Buffer.from(a.bitmap).equals(Buffer.from(b.bitmap))) panic('with a gain map, the primary image ignores the flat tables');
79+
if (!Buffer.from(a.gainMap.image.bitmap).equals(Buffer.from(b.gainMap.image.bitmap))) panic('the flat tables change the gain map');
6280
})();

0 commit comments

Comments
 (0)