imagescript 4.0.0-rc.2 → 4.0.0-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/animation-encoder.d.ts +1 -0
- package/dist/animation-encoder.js +15 -2
- package/dist/animation.d.ts +22 -22
- package/dist/animation.js +35 -12
- package/dist/codecs/jxl/frame-header.js +6 -4
- package/dist/codecs/quantize.js +23 -9
- package/dist/gainmap.d.ts +6 -3
- package/dist/gainmap.js +21 -15
- package/dist/image.d.ts +78 -67
- package/dist/image.js +69 -68
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/metadata.d.ts +5 -5
- package/dist/metadata.js +1 -1
- package/dist/render/svg.js +39 -15
- package/dist/render/text.js +10 -8
- package/dist/text-layout.d.ts +21 -11
- package/dist/text-layout.js +17 -30
- package/dist/util/limits.d.ts +35 -0
- package/dist/util/limits.js +1 -0
- package/dist/util/options.js +15 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
**ImageScript** decodes, edits and encodes images in pure TypeScript: no WebAssembly, no native binaries, and no
|
|
17
17
|
runtime dependencies. It runs in Node.js 22.13+, Deno, Bun, browsers and edge runtimes (Cloudflare Workers, Vercel's
|
|
18
18
|
Edge Runtime, Deno Deploy), works with `import` and, on supported Node.js versions, `require()`, and ships with type
|
|
19
|
-
definitions. Large AVIF and JPEG XL operations, and lossless WebP encoding, can use multiple CPU cores when the runtime
|
|
19
|
+
definitions (TypeScript 5.7 or newer: with older versions and `skipLibCheck`, the encoders' return types become `any`). Large AVIF and JPEG XL operations, and lossless WebP encoding, can use multiple CPU cores when the runtime
|
|
20
20
|
supports it.
|
|
21
21
|
|
|
22
22
|
```sh
|
|
@@ -8,6 +8,18 @@ import { Optimizer, Compositor, coverCanvas, fillUnchanged } from "./util/optimi
|
|
|
8
8
|
import { compressAsync, decompressAsync } from "./codecs/zlib.js";
|
|
9
9
|
import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
|
|
10
10
|
import { checkOptions, finite, compressionLevel, loopCount, toPlays, fitLoops, fitDuration, maxDuration } from "./util/options.js";
|
|
11
|
+
function clipToCanvas(frame, width, height) {
|
|
12
|
+
const { x, y, width: w, height: h } = frame;
|
|
13
|
+
if (x >= 0 && y >= 0 && x + w <= width && y + h <= height)
|
|
14
|
+
return frame;
|
|
15
|
+
const left = Math.max(0, x), top = Math.max(0, y), right = Math.min(width, x + w), bottom = Math.min(height, y + h);
|
|
16
|
+
if (right <= left || bottom <= top)
|
|
17
|
+
return { ...frame, x: 0, y: 0, width: 1, height: 1, disposal: 1, dispose: 0, pixels: new Uint8Array(4) };
|
|
18
|
+
const pixels = new Uint8Array((right - left) * (bottom - top) * 4);
|
|
19
|
+
for (let row = top; row < bottom; row++)
|
|
20
|
+
pixels.set(frame.pixels.subarray(((row - y) * w + left - x) * 4, ((row - y) * w + right - x) * 4), (row - top) * (right - left) * 4);
|
|
21
|
+
return { ...frame, x: left, y: top, width: right - left, height: bottom - top, pixels };
|
|
22
|
+
}
|
|
11
23
|
export class AnimationEncoder {
|
|
12
24
|
format;
|
|
13
25
|
optimizer;
|
|
@@ -25,7 +37,7 @@ export class AnimationEncoder {
|
|
|
25
37
|
get frameCount() {
|
|
26
38
|
return this.count;
|
|
27
39
|
}
|
|
28
|
-
add(frame, options = {}) {
|
|
40
|
+
async add(frame, options = {}) {
|
|
29
41
|
checkOptions(options, 'encoder.add(frame, { duration: 40 })');
|
|
30
42
|
if (this.finished)
|
|
31
43
|
throw new Error('The animation encoder is finished');
|
|
@@ -46,9 +58,10 @@ export class AnimationEncoder {
|
|
|
46
58
|
pixels: new Uint8Array(image.bitmap.buffer, image.bitmap.byteOffset, image.bitmap.byteLength).slice()
|
|
47
59
|
};
|
|
48
60
|
this.count++;
|
|
61
|
+
const given = this.optimizer ? input : clipToCanvas(input, this.width, this.height);
|
|
49
62
|
return this.queue = this.queue.then(async () => {
|
|
50
63
|
if (!this.optimizer)
|
|
51
|
-
return this.format.frame(
|
|
64
|
+
return this.format.frame(given, false);
|
|
52
65
|
this.optimizer.push(input);
|
|
53
66
|
for (const f of this.optimizer.take())
|
|
54
67
|
await this.format.frame(f, true);
|
package/dist/animation.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BinaryData } from './util/mem.js';
|
|
2
|
-
import { DecodeOptions, EncodeOptions, GIFEncodeOptions, ResizeOptions, ResizeToOptions, RotateOptions, TransformOptions, AffineTransform, ShadowOptions, OutlineOptions, TrimOptions } from './image.js';
|
|
2
|
+
import { DecodeOptions, EncodeOptions, GIFEncodeOptions, ResizeOptions, ResizeToOptions, RotateOptions, TransformOptions, AffineTransform, ShadowOptions, OutlineOptions, TrimOptions, PNGEncodeOptions, WebPEncodeOptions, FlipDirection } from './image.js';
|
|
3
3
|
import { Frame } from './frame.js';
|
|
4
4
|
import { AnimationEncoder } from './animation-encoder.js';
|
|
5
5
|
/** Options of {@link Animation} */
|
|
@@ -30,30 +30,24 @@ export interface AnimationEncodeOptions extends EncodeOptions {
|
|
|
30
30
|
*/
|
|
31
31
|
optimize?: boolean;
|
|
32
32
|
}
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
quality?: number;
|
|
40
|
-
/**
|
|
41
|
-
* Compression effort (0-6): higher effort can make smaller files but takes longer, see
|
|
42
|
-
* {@link WebPEncodeOptions.effort}. Default: `2`
|
|
43
|
-
*/
|
|
44
|
-
effort?: number;
|
|
45
|
-
/** For lossy frames, improve color detail around sharp edges. Default: `false` */
|
|
46
|
-
sharpYuv?: boolean;
|
|
47
|
-
/** For lossy frames, the quality of the transparency channel (0-100). Default: `100` */
|
|
48
|
-
alphaQuality?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Options of {@link Animation.encodeWebP} and {@link Animation.webpEncoder}: those of {@link WebPEncodeOptions} for every
|
|
35
|
+
* frame (but `threads`: the frames are encoded one at a time), and the animation options. Without `quality` the frames
|
|
36
|
+
* are lossless
|
|
37
|
+
*/
|
|
38
|
+
export interface WebPAnimationEncodeOptions extends Omit<WebPEncodeOptions, 'threads'>, AnimationEncodeOptions {
|
|
49
39
|
}
|
|
50
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Options of {@link Animation.encodeGIF} and {@link Animation.gifEncoder}: those of {@link GIFEncodeOptions} for every
|
|
42
|
+
* frame, and the animation options
|
|
43
|
+
*/
|
|
51
44
|
export interface GIFAnimationEncodeOptions extends GIFEncodeOptions, AnimationEncodeOptions {
|
|
52
45
|
}
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Options of {@link Animation.encodeAPNG} and {@link Animation.apngEncoder}: those of {@link PNGEncodeOptions}, and the
|
|
48
|
+
* animation options
|
|
49
|
+
*/
|
|
50
|
+
export interface APNGEncodeOptions extends PNGEncodeOptions, AnimationEncodeOptions {
|
|
57
51
|
}
|
|
58
52
|
/**
|
|
59
53
|
* An animation: frames ({@link Animation.frames}) that can be decoded from and encoded to GIF, APNG and WebP. It can be
|
|
@@ -127,6 +121,12 @@ export declare class Animation {
|
|
|
127
121
|
* @param height The new canvas height
|
|
128
122
|
*/
|
|
129
123
|
crop(x: number, y: number, width: number, height: number): this;
|
|
124
|
+
/**
|
|
125
|
+
* Flips / mirrors the animation horizontally or vertically, like {@link Image.flip}: every frame is flipped and
|
|
126
|
+
* moved to the mirrored place on the canvas, which keeps its size
|
|
127
|
+
* @param direction The direction to flip, see {@link FlipDirection}
|
|
128
|
+
*/
|
|
129
|
+
flip(direction: FlipDirection): this;
|
|
130
130
|
/**
|
|
131
131
|
* Scales the animation by a factor: every frame and its offsets, and the canvas
|
|
132
132
|
* @param factor The factor
|
package/dist/animation.js
CHANGED
|
@@ -6,13 +6,13 @@ import * as jxl from "./codecs/jxl.js";
|
|
|
6
6
|
import { availableThreads } from "./util/threads.js";
|
|
7
7
|
import { view } from "./util/mem.js";
|
|
8
8
|
import { GainMap } from "./gainmap.js";
|
|
9
|
-
import { Image, resizeArguments, insideSize, alignedPixels } from "./image.js";
|
|
9
|
+
import { Image, resizeArguments, insideSize, alignedPixels, FLIP_DIRECTIONS, TONE_MAPPINGS } from "./image.js";
|
|
10
10
|
import { Frame } from "./frame.js";
|
|
11
11
|
import { ImageType } from "./image-type.js";
|
|
12
12
|
import { optimize } from "./util/optimize.js";
|
|
13
13
|
import { paletteOptions } from "./codecs/quantize.js";
|
|
14
14
|
import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
|
|
15
|
-
import { checkOptions, finite, compressionLevel, loopCount, toPlays, fromPlays, fitLoops, fitDuration, maxDuration, color as checkColor } from "./util/options.js";
|
|
15
|
+
import { checkOptions, oneOf, pixelLimit, checkThreads, finite, compressionLevel, loopCount, toPlays, fromPlays, fitLoops, fitDuration, maxDuration, color as checkColor } from "./util/options.js";
|
|
16
16
|
import { MAX_PIXELS, decoding } from "./util/limits.js";
|
|
17
17
|
import { AnimationEncoder } from "./animation-encoder.js";
|
|
18
18
|
function canvasFrames(width, height, frames) {
|
|
@@ -112,10 +112,16 @@ export class Animation {
|
|
|
112
112
|
[width, height] = insideSize(originalWidth, originalHeight, givenWidth, givenHeight);
|
|
113
113
|
const canvas = canvasSize(Math.max(1, Math.round(width)), Math.max(1, Math.round(height)));
|
|
114
114
|
const sx = width / originalWidth, sy = height / originalHeight;
|
|
115
|
-
|
|
115
|
+
const changes = this.__distinct__().map(frame => {
|
|
116
116
|
const left = Math.round(frame.xOffset * sx), top = Math.round(frame.yOffset * sy);
|
|
117
117
|
const right = Math.round((frame.xOffset + frame.width) * sx), bottom = Math.round((frame.yOffset + frame.height) * sy);
|
|
118
|
-
frame
|
|
118
|
+
return { frame, left, top, width: Math.max(1, right - left), height: Math.max(1, bottom - top) };
|
|
119
|
+
});
|
|
120
|
+
const resized = new Set();
|
|
121
|
+
for (const { frame, left, top, width, height } of changes) {
|
|
122
|
+
if (!resized.has(frame.image))
|
|
123
|
+
frame.image.resize(width, height, options);
|
|
124
|
+
resized.add(frame.image);
|
|
119
125
|
frame.xOffset = left;
|
|
120
126
|
frame.yOffset = top;
|
|
121
127
|
}
|
|
@@ -129,7 +135,7 @@ export class Animation {
|
|
|
129
135
|
height = Math.min(Math.floor(finite(height, 'height')), this.height - y);
|
|
130
136
|
if (!(width >= 1 && height >= 1))
|
|
131
137
|
throw new RangeError('The crop region has to overlap the canvas');
|
|
132
|
-
for (const frame of this.
|
|
138
|
+
for (const frame of this.__distinct__()) {
|
|
133
139
|
frame.xOffset -= x;
|
|
134
140
|
frame.yOffset -= y;
|
|
135
141
|
}
|
|
@@ -144,7 +150,7 @@ export class Animation {
|
|
|
144
150
|
const [boxWidth, boxHeight] = canvasSize(Math.floor(finite(width, 'width')), Math.floor(finite(height, 'height')));
|
|
145
151
|
this.resize({ width: boxWidth, height: boxHeight, fit: 'inside', ...resizeOpts });
|
|
146
152
|
const x = (boxWidth - this.width) / 2 | 0, y = (boxHeight - this.height) / 2 | 0;
|
|
147
|
-
for (const frame of this.
|
|
153
|
+
for (const frame of this.__distinct__()) {
|
|
148
154
|
frame.xOffset += x;
|
|
149
155
|
frame.yOffset += y;
|
|
150
156
|
}
|
|
@@ -164,6 +170,23 @@ export class Animation {
|
|
|
164
170
|
this.resize(Math.max(boxWidth, Math.round(this.width * scale)), Math.max(boxHeight, Math.round(this.height * scale)), options);
|
|
165
171
|
return this.crop((this.width - boxWidth) / 2, (this.height - boxHeight) / 2, boxWidth, boxHeight);
|
|
166
172
|
}
|
|
173
|
+
flip(direction) {
|
|
174
|
+
oneOf(direction, FLIP_DIRECTIONS, 'flip direction');
|
|
175
|
+
const width = this.width, height = this.height;
|
|
176
|
+
const flipped = new Set();
|
|
177
|
+
for (const frame of this.__distinct__()) {
|
|
178
|
+
if (!flipped.has(frame.image))
|
|
179
|
+
frame.image.flip(direction);
|
|
180
|
+
flipped.add(frame.image);
|
|
181
|
+
if (direction === 'horizontal')
|
|
182
|
+
frame.xOffset = width - frame.xOffset - frame.width;
|
|
183
|
+
else
|
|
184
|
+
frame.yOffset = height - frame.yOffset - frame.height;
|
|
185
|
+
}
|
|
186
|
+
if (width && height)
|
|
187
|
+
this.__canvas__ = [width, height];
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
167
190
|
scale(factor, options) {
|
|
168
191
|
if (!(finite(factor, 'factor') > 0))
|
|
169
192
|
throw new RangeError(`Invalid scale factor: ${factor}`);
|
|
@@ -223,6 +246,9 @@ export class Animation {
|
|
|
223
246
|
this.__canvas__ = [probe.width, probe.height];
|
|
224
247
|
return this;
|
|
225
248
|
}
|
|
249
|
+
__distinct__() {
|
|
250
|
+
return [...new Set(this.__frames__)];
|
|
251
|
+
}
|
|
226
252
|
__clipped_frames__() {
|
|
227
253
|
const width = this.width, height = this.height;
|
|
228
254
|
return this.frames.map(frame => {
|
|
@@ -343,13 +369,10 @@ export class Animation {
|
|
|
343
369
|
static async decode(input, options = {}) {
|
|
344
370
|
checkOptions(options, 'Animation.decode(data, { maxPixels: 1e8 })');
|
|
345
371
|
const { maxPixels = MAX_PIXELS, threads = 1 } = options;
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
if (!(Number(maxPixels) > 0))
|
|
349
|
-
throw new RangeError(`maxPixels must be a positive number, not ${maxPixels}`);
|
|
372
|
+
checkThreads(threads);
|
|
373
|
+
pixelLimit(maxPixels);
|
|
350
374
|
const { toneMapping = 'reinhard' } = options;
|
|
351
|
-
|
|
352
|
-
throw new TypeError(`toneMapping must be 'reinhard' or 'clamp', not ${String(toneMapping)}`);
|
|
375
|
+
oneOf(toneMapping, TONE_MAPPINGS, 'toneMapping');
|
|
353
376
|
return decoding(() => Animation.__decode__(input, options));
|
|
354
377
|
}
|
|
355
378
|
static __is_animated__(animation) {
|
|
@@ -8,12 +8,14 @@ const DEFAULT_EPF = () => ({
|
|
|
8
8
|
quantMul: 0.46, pass0SigmaScale: 0.9, pass2SigmaScale: 6.5, borderSadMul: 2 / 3, sigmaForModular: 1
|
|
9
9
|
});
|
|
10
10
|
const DEFAULT_GAB = [0.115169525, 0.061248592, 0.115169525, 0.061248592, 0.115169525, 0.061248592];
|
|
11
|
-
function blendingInfo(r,
|
|
11
|
+
function blendingInfo(r, numExtra, resets, mainMode = -1) {
|
|
12
12
|
const mode = r.u32(0, 1, 2, [3, 2]);
|
|
13
13
|
if (mode > 4)
|
|
14
14
|
throw new Error('JPEG XL: invalid blend mode');
|
|
15
|
-
const usesAlpha =
|
|
15
|
+
const usesAlpha = numExtra > 0 && (mode === BLEND_BLEND || mode === BLEND_MULADD);
|
|
16
16
|
const alphaChannel = usesAlpha ? r.u32(0, 1, 2, [3, 3]) : 0;
|
|
17
|
+
if (alphaChannel >= Math.max(1, numExtra))
|
|
18
|
+
throw new Error('JPEG XL: invalid blend alpha channel');
|
|
17
19
|
const clamp = usesAlpha || mode === BLEND_MUL ? r.bool() : false;
|
|
18
20
|
const source = resets(mainMode >= 0 ? mainMode : mode) ? 0 : r.bits(2);
|
|
19
21
|
return { mode, alphaChannel, clamp, source };
|
|
@@ -100,9 +102,9 @@ export function frameHeader(r, image) {
|
|
|
100
102
|
let blending = { mode: BLEND_REPLACE, alphaChannel: 0, clamp: false, source: 0 };
|
|
101
103
|
const ecBlending = [];
|
|
102
104
|
if (!allDefault && normal) {
|
|
103
|
-
blending = blendingInfo(r, numExtra
|
|
105
|
+
blending = blendingInfo(r, numExtra, resets);
|
|
104
106
|
for (let i = 0; i < numExtra; i++)
|
|
105
|
-
ecBlending.push(blendingInfo(r,
|
|
107
|
+
ecBlending.push(blendingInfo(r, numExtra, resets, blending.mode));
|
|
106
108
|
}
|
|
107
109
|
else
|
|
108
110
|
for (let i = 0; i < numExtra; i++)
|
package/dist/codecs/quantize.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { oneOf } from "../util/options.js";
|
|
1
2
|
const WR = 3, WG = 4, WB = 2;
|
|
2
3
|
const DITHER = { 'none': true, 'floyd-steinberg': true, 'atkinson': true, 'ordered': true };
|
|
3
4
|
export const DITHER_METHODS = Object.keys(DITHER);
|
|
@@ -8,8 +9,7 @@ export function paletteOptions(colors, dither, ditherStrength) {
|
|
|
8
9
|
if (!Number.isInteger(count) || count < 2 || count > 256)
|
|
9
10
|
throw new RangeError(`colors must be an integer from 2 to 256, not ${colors}`);
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
-
throw new TypeError(`dither must be one of ${DITHER_METHODS.map(name => `'${name}'`).join(', ')}, not ${String(dither)}`);
|
|
12
|
+
oneOf(dither, DITHER_METHODS, 'dither');
|
|
13
13
|
const strength = Number(ditherStrength);
|
|
14
14
|
if (!Number.isFinite(strength))
|
|
15
15
|
throw new RangeError(`Invalid ditherStrength: ${ditherStrength}`);
|
|
@@ -17,15 +17,29 @@ export function paletteOptions(colors, dither, ditherStrength) {
|
|
|
17
17
|
throw new RangeError(`ditherStrength must be 0-1, not ${ditherStrength}`);
|
|
18
18
|
return { colors: count, dither: dither, strength };
|
|
19
19
|
}
|
|
20
|
-
export function dominantColor(pixels, include
|
|
21
|
-
const
|
|
22
|
-
|
|
20
|
+
export function dominantColor(pixels, include) {
|
|
21
|
+
const counts = new Uint32Array(4096), sums = new Float64Array(4096 * 3);
|
|
22
|
+
let found = false;
|
|
23
|
+
for (let i = 0; i < pixels.length; i += 4) {
|
|
24
|
+
if (pixels[i + 3] < 128)
|
|
25
|
+
continue;
|
|
26
|
+
const r = pixels[i], g = pixels[i + 1], b = pixels[i + 2];
|
|
27
|
+
if (!include(r, g, b))
|
|
28
|
+
continue;
|
|
29
|
+
const bin = (r >> 4) << 8 | (g >> 4) << 4 | b >> 4;
|
|
30
|
+
counts[bin]++;
|
|
31
|
+
sums[bin * 3] += r;
|
|
32
|
+
sums[bin * 3 + 1] += g;
|
|
33
|
+
sums[bin * 3 + 2] += b;
|
|
34
|
+
found = true;
|
|
35
|
+
}
|
|
36
|
+
if (!found)
|
|
23
37
|
return null;
|
|
24
38
|
let best = 0;
|
|
25
|
-
for (let
|
|
26
|
-
if (
|
|
27
|
-
best =
|
|
28
|
-
return [Math.round(
|
|
39
|
+
for (let bin = 1; bin < 4096; bin++)
|
|
40
|
+
if (counts[bin] > counts[best])
|
|
41
|
+
best = bin;
|
|
42
|
+
return [Math.round(sums[best * 3] / counts[best]), Math.round(sums[best * 3 + 1] / counts[best]), Math.round(sums[best * 3 + 2] / counts[best])];
|
|
29
43
|
}
|
|
30
44
|
export function quantize(pixels, width, { maxColors = 256, dither = 'floyd-steinberg', ditherStrength = 0.85, transparent = false } = {}) {
|
|
31
45
|
const length = pixels.length >> 2;
|
package/dist/gainmap.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Image } from './image.js';
|
|
2
|
+
import type { DecodeOptions } from './image.js';
|
|
2
3
|
import { BinaryData } from './util/mem.js';
|
|
3
|
-
/** Options of {@link GainMap.
|
|
4
|
+
/** Options of {@link GainMap.encodeJPEG} */
|
|
4
5
|
export interface GainMapEncodeOptions {
|
|
5
6
|
/** The JPEG quality (1-100). Default: `90` */
|
|
6
7
|
quality?: number;
|
|
@@ -130,11 +131,13 @@ export declare class GainMap {
|
|
|
130
131
|
* Ultra HDR JPEG
|
|
131
132
|
* @param options See {@link GainMapEncodeOptions}
|
|
132
133
|
*/
|
|
133
|
-
|
|
134
|
+
encodeJPEG(options?: GainMapEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
|
|
134
135
|
/**
|
|
135
136
|
* Decodes a gain map JPEG with its parameters (hdrgm XMP or ISO 21496-1), like the second image in an Ultra HDR
|
|
136
137
|
* JPEG, or the gain map of an Ultra HDR JPEG (or an iPhone's JPEG: its values converted to these parameters)
|
|
137
138
|
* @param data The JPEG
|
|
139
|
+
* @param options The `maxPixels` limit, like {@link DecodeOptions.maxPixels} (a map larger than that throws a
|
|
140
|
+
* {@link LimitError}); a damaged JPEG throws an error saying so, like {@link Image.decode}
|
|
138
141
|
*/
|
|
139
|
-
static decode(data: BinaryData): Promise<GainMap>;
|
|
142
|
+
static decode(data: BinaryData, options?: Pick<DecodeOptions, 'maxPixels'>): Promise<GainMap>;
|
|
140
143
|
}
|
package/dist/gainmap.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Image, alignedPixels } from "./image.js";
|
|
2
|
+
import { MAX_PIXELS, decoding } from "./util/limits.js";
|
|
2
3
|
import * as jpeg from "./codecs/jpeg.js";
|
|
3
4
|
import * as codec from "./codecs/gainmap.js";
|
|
4
5
|
import { view } from "./util/mem.js";
|
|
5
|
-
import { finite, checkOptions, quality as checkQuality } from "./util/options.js";
|
|
6
|
+
import { finite, checkOptions, pixelLimit, quality as checkQuality } from "./util/options.js";
|
|
6
7
|
const perChannel = (value, name) => {
|
|
7
8
|
const values = typeof value === 'number' ? [value, value, value] : Array.from(value);
|
|
8
9
|
if (values.length === 1)
|
|
@@ -67,8 +68,8 @@ export class GainMap {
|
|
|
67
68
|
this.__pixels__ = image;
|
|
68
69
|
this.__check__();
|
|
69
70
|
}
|
|
70
|
-
static __from_jpeg__(map, params, apple = 0) {
|
|
71
|
-
const { width, height, pixels } = jpeg.decode(map);
|
|
71
|
+
static __from_jpeg__(map, params, apple = 0, maxPixels = MAX_PIXELS) {
|
|
72
|
+
const { width, height, pixels } = jpeg.decode(map, { maxPixels });
|
|
72
73
|
const image = new Image(width, height);
|
|
73
74
|
image.bitmap = alignedPixels(pixels);
|
|
74
75
|
if (apple) {
|
|
@@ -118,9 +119,9 @@ export class GainMap {
|
|
|
118
119
|
__check__() {
|
|
119
120
|
for (let c = 0; c < 3; c++)
|
|
120
121
|
if (this.__max__[c] < this.__min__[c])
|
|
121
|
-
throw new RangeError(
|
|
122
|
+
throw new RangeError(`The gain map's max must be at least its min (${this.__max__[c]} < ${this.__min__[c]}; the values are checked together when the map is cloned or encoded)`);
|
|
122
123
|
if (this.__capacityMax__ <= this.__capacityMin__)
|
|
123
|
-
throw new RangeError(
|
|
124
|
+
throw new RangeError(`The gain map's hdrCapacityMax (by default the largest max) must be above its hdrCapacityMin (${this.__capacityMax__} <= ${this.__capacityMin__})`);
|
|
124
125
|
}
|
|
125
126
|
get image() {
|
|
126
127
|
return this.__pixels__;
|
|
@@ -200,21 +201,26 @@ export class GainMap {
|
|
|
200
201
|
}
|
|
201
202
|
this.image = out;
|
|
202
203
|
}
|
|
203
|
-
async
|
|
204
|
-
checkOptions(options, '
|
|
204
|
+
async encodeJPEG(options) {
|
|
205
|
+
checkOptions(options, 'gainMap.encodeJPEG({ quality: 80 })');
|
|
205
206
|
let quality = options?.quality;
|
|
206
207
|
if (quality !== undefined)
|
|
207
208
|
quality = Math.max(1, checkQuality(quality));
|
|
208
209
|
return codec.mapWithMetadata(this.__body__(quality), this.__params__());
|
|
209
210
|
}
|
|
210
|
-
static async decode(data) {
|
|
211
|
+
static async decode(data, options = {}) {
|
|
212
|
+
checkOptions(options, 'GainMap.decode(data, { maxPixels: 1e8 })');
|
|
213
|
+
const { maxPixels = MAX_PIXELS } = options;
|
|
214
|
+
pixelLimit(maxPixels);
|
|
211
215
|
const bytes = view(data);
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
216
|
+
return decoding(() => {
|
|
217
|
+
const found = codec.find(bytes);
|
|
218
|
+
if (found)
|
|
219
|
+
return GainMap.__from_jpeg__(found.map, found.params, found.apple, maxPixels);
|
|
220
|
+
const params = codec.readParams(bytes);
|
|
221
|
+
if (!params)
|
|
222
|
+
throw new Error('No gain map (or no gain map parameters) found');
|
|
223
|
+
return GainMap.__from_jpeg__(bytes, params, 0, maxPixels);
|
|
224
|
+
});
|
|
219
225
|
}
|
|
220
226
|
}
|