Skip to content

Commit da38bc3

Browse files
committed
take maxPixels in GainMap.decode
1 parent 1d76007 commit da38bc3

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

‎src/gainmap.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Image, alignedPixels } from './image.ts';
2+
import type { DecodeOptions } from './image.ts';
3+
import { MAX_PIXELS, decoding } from './util/limits.ts';
24
import * as jpeg from './codecs/jpeg.ts';
35
import * as codec from './codecs/gainmap.ts';
46
import { BinaryData, view } from './util/mem.ts';
@@ -171,8 +173,8 @@ export class GainMap {
171173
* @internal from a decoded file: the map's JPEG, decoded now (a damaged one throws here, not in a later
172174
* operation); `apple`: an Apple gain map's headroom, its values converted to the parameters' scale
173175
*/
174-
static __from_jpeg__(map: Uint8Array, params: codec.Params, apple = 0): GainMap {
175-
const { width, height, pixels } = jpeg.decode(map);
176+
static __from_jpeg__(map: Uint8Array, params: codec.Params, apple = 0, maxPixels = MAX_PIXELS): GainMap {
177+
const { width, height, pixels } = jpeg.decode(map, { maxPixels });
176178
const image = new Image(width, height);
177179
image.bitmap = alignedPixels(pixels);
178180
if (apple) {
@@ -354,13 +356,20 @@ export class GainMap {
354356
* Decodes a gain map JPEG with its parameters (hdrgm XMP or ISO 21496-1), like the second image in an Ultra HDR
355357
* JPEG, or the gain map of an Ultra HDR JPEG (or an iPhone's JPEG: its values converted to these parameters)
356358
* @param data The JPEG
359+
* @param options The `maxPixels` limit, like {@link DecodeOptions.maxPixels} (a map larger than that throws a
360+
* {@link LimitError}); a damaged JPEG throws an error saying so, like {@link Image.decode}
357361
*/
358-
static async decode(data: BinaryData): Promise<GainMap> {
362+
static async decode(data: BinaryData, options: Pick<DecodeOptions, 'maxPixels'> = {}): Promise<GainMap> {
363+
checkOptions(options, 'GainMap.decode(data, { maxPixels: 1e8 })');
364+
const { maxPixels = MAX_PIXELS } = options;
365+
if (!(Number(maxPixels) > 0)) throw new RangeError(`maxPixels must be a positive number, not ${maxPixels}`);
359366
const bytes = view(data);
360-
const found = codec.find(bytes);
361-
if (found) return GainMap.__from_jpeg__(found.map, found.params, found.apple);
362-
const params = codec.readParams(bytes);
363-
if (!params) throw new Error('No gain map (or no gain map parameters) found');
364-
return GainMap.__from_jpeg__(bytes, params);
367+
return decoding(() => {
368+
const found = codec.find(bytes);
369+
if (found) return GainMap.__from_jpeg__(found.map, found.params, found.apple, maxPixels);
370+
const params = codec.readParams(bytes);
371+
if (!params) throw new Error('No gain map (or no gain map parameters) found');
372+
return GainMap.__from_jpeg__(bytes, params, 0, maxPixels);
373+
});
365374
}
366375
}

‎src/image.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ export class Image {
26672667
try {
26682668
const found = gainMapCodec.find(data);
26692669
const size = found && gainMapCodec.dimensions(found.map);
2670-
if (found && size && size.width * size.height <= maxPixels) image.__gain_map__ = GainMap.__from_jpeg__(found.map, found.params, found.apple);
2670+
if (found && size && size.width * size.height <= maxPixels) image.__gain_map__ = GainMap.__from_jpeg__(found.map, found.params, found.apple, maxPixels);
26712671
} catch {
26722672
image.__gain_map__ = null;
26732673
}

‎tests/gainmap.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ const meanValue = (image, left, top, right, bottom) => {
8787
panic(`XMP only map: ${map.min} ${map.max} ${map.hdrCapacityMax} vs ${single.gainMap.min} ${single.gainMap.max}`);
8888
}
8989

90+
// decoding a map alone takes the limit of the decode options, and a damaged one says so (like Image.decode)
91+
{
92+
const alone = await single.gainMap.encode();
93+
const lib = require('..');
94+
const map = await GainMap.decode(alone, {maxPixels: single.gainMap.width * single.gainMap.height});
95+
if (map.width !== single.gainMap.width) panic('GainMap.decode with a limit that fits');
96+
let error = null;
97+
try { await GainMap.decode(alone, {maxPixels: 10}); } catch (err) { error = err; }
98+
if (!(error instanceof lib.LimitError)) panic(`GainMap.decode over the limit: ${error}`);
99+
error = null;
100+
try { await GainMap.decode(new Uint8Array(100)); } catch (err) { error = err; }
101+
if (!error || error instanceof lib.LimitError || !/damaged or truncated|No gain map/.test(error.message)) panic(`GainMap.decode of garbage: ${error && error.message}`);
102+
for (const bad of [5, 'x', null]) {
103+
error = null;
104+
try { await GainMap.decode(alone, bad); } catch (err) { error = err; }
105+
if (!(error instanceof TypeError)) panic(`GainMap.decode(data, ${bad}) should throw a TypeError`);
106+
}
107+
}
108+
90109
// made by hand: parameters checked, written and read back; per channel values
91110
{
92111
const image = new Image(64, 48).fill(0x808080ff);

0 commit comments

Comments
 (0)