|
1 | 1 | import { Image, alignedPixels } from './image.ts'; |
| 2 | +import type { DecodeOptions } from './image.ts'; |
| 3 | +import { MAX_PIXELS, decoding } from './util/limits.ts'; |
2 | 4 | import * as jpeg from './codecs/jpeg.ts'; |
3 | 5 | import * as codec from './codecs/gainmap.ts'; |
4 | 6 | import { BinaryData, view } from './util/mem.ts'; |
@@ -171,8 +173,8 @@ export class GainMap { |
171 | 173 | * @internal from a decoded file: the map's JPEG, decoded now (a damaged one throws here, not in a later |
172 | 174 | * operation); `apple`: an Apple gain map's headroom, its values converted to the parameters' scale |
173 | 175 | */ |
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 }); |
176 | 178 | const image = new Image(width, height); |
177 | 179 | image.bitmap = alignedPixels(pixels); |
178 | 180 | if (apple) { |
@@ -354,13 +356,20 @@ export class GainMap { |
354 | 356 | * Decodes a gain map JPEG with its parameters (hdrgm XMP or ISO 21496-1), like the second image in an Ultra HDR |
355 | 357 | * JPEG, or the gain map of an Ultra HDR JPEG (or an iPhone's JPEG: its values converted to these parameters) |
356 | 358 | * @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} |
357 | 361 | */ |
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}`); |
359 | 366 | 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 | + }); |
365 | 374 | } |
366 | 375 | } |
0 commit comments