imagescript 4.0.0-rc.2 → 4.0.0-rc.4
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 +10 -1
- package/dist/animation-encoder.d.ts +14 -6
- package/dist/animation-encoder.js +24 -9
- package/dist/animation.d.ts +68 -28
- package/dist/animation.js +82 -27
- package/dist/codecs/gif.js +16 -5
- package/dist/codecs/jpeg.js +2 -0
- package/dist/codecs/jxl/frame-header.js +6 -4
- package/dist/codecs/jxl/frame.js +4 -1
- package/dist/codecs/png.js +46 -20
- package/dist/codecs/quantize.js +25 -13
- package/dist/codecs/tiff.js +7 -0
- package/dist/codecs/webp.js +19 -18
- package/dist/colors.js +25 -17
- package/dist/frame.d.ts +18 -5
- package/dist/gainmap.d.ts +43 -14
- package/dist/gainmap.js +25 -16
- package/dist/image.d.ts +430 -173
- package/dist/image.js +109 -101
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/metadata.d.ts +11 -5
- package/dist/metadata.js +1 -1
- package/dist/package.json +1 -1
- package/dist/render/svg.js +172 -64
- package/dist/render/text.js +10 -8
- package/dist/text-layout.d.ts +28 -12
- package/dist/text-layout.js +17 -30
- package/dist/util/limits.d.ts +40 -0
- package/dist/util/limits.js +5 -0
- package/dist/util/options.js +44 -3
- package/dist/util/rotate.js +43 -0
- package/package.json +1 -2
|
@@ -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/jxl/frame.js
CHANGED
|
@@ -18,8 +18,11 @@ class Sections {
|
|
|
18
18
|
constructor(d, t) {
|
|
19
19
|
this.d = d;
|
|
20
20
|
this.t = t;
|
|
21
|
-
if (t.offsets.length === 1)
|
|
21
|
+
if (t.offsets.length === 1) {
|
|
22
|
+
if (t.offsets[0] + t.sizes[0] > d.length)
|
|
23
|
+
throw new Error('JPEG XL: truncated data');
|
|
22
24
|
this.single = new BitReader(d, t.offsets[0], t.offsets[0] + t.sizes[0]);
|
|
25
|
+
}
|
|
23
26
|
}
|
|
24
27
|
get(i) {
|
|
25
28
|
if (this.single)
|
package/dist/codecs/png.js
CHANGED
|
@@ -355,6 +355,7 @@ function parse(array) {
|
|
|
355
355
|
let current = null;
|
|
356
356
|
let sequence = 0;
|
|
357
357
|
let animationError = false;
|
|
358
|
+
let ended = false, truncated = false;
|
|
358
359
|
let offset = 8;
|
|
359
360
|
while (offset + 8 <= array.length) {
|
|
360
361
|
const length = view.getUint32(offset);
|
|
@@ -438,8 +439,10 @@ function parse(array) {
|
|
|
438
439
|
srgb = true;
|
|
439
440
|
else if (type === tEXt || type === zTXt || type === iTXt)
|
|
440
441
|
text.push({ type, chunk });
|
|
441
|
-
else if (type === IEND)
|
|
442
|
+
else if (type === IEND) {
|
|
443
|
+
ended = true;
|
|
442
444
|
break;
|
|
445
|
+
}
|
|
443
446
|
offset = end + 4;
|
|
444
447
|
}
|
|
445
448
|
if (!header)
|
|
@@ -459,10 +462,15 @@ function parse(array) {
|
|
|
459
462
|
if (animation) {
|
|
460
463
|
if (frames.length > animation.frames)
|
|
461
464
|
frames.length = animation.frames;
|
|
462
|
-
if (!
|
|
465
|
+
if (!ended) {
|
|
466
|
+
truncated = true;
|
|
467
|
+
while (frames.length && !frames[frames.length - 1].data.length)
|
|
468
|
+
frames.pop();
|
|
469
|
+
}
|
|
470
|
+
if (!frames.length || (frames.length < animation.frames && !truncated) || frames.some(f => !f.data.length))
|
|
463
471
|
animationError = true;
|
|
464
472
|
}
|
|
465
|
-
return { header, palette, transparency, idat, exif, iccp: srgb ? null : iccp, text, animation, frames, animationError };
|
|
473
|
+
return { header, palette, transparency, idat, exif, iccp: srgb ? null : iccp, text, animation, frames, animationError, truncated };
|
|
466
474
|
}
|
|
467
475
|
function passesOf(p, width, height) {
|
|
468
476
|
const bitsPerPixel = COLOR_TYPE_CHANNELS[p.header.colorType] * p.header.depth;
|
|
@@ -547,27 +555,45 @@ export async function decodeAnimation(array, firstFrameOnly = false, maxPixels =
|
|
|
547
555
|
checkPixels(width, height, maxPixels, !p.animation || p.animationError || firstFrameOnly ? 1 : Math.max(1, p.frames.length));
|
|
548
556
|
if (!p.animation || p.animationError) {
|
|
549
557
|
const pixels = await decodeImageData(p, p.idat, width, height);
|
|
550
|
-
|
|
558
|
+
const still = async function* () {
|
|
559
|
+
yield { x: 0, y: 0, width, height, duration: 0, dispose: 0, blend: 0, pixels };
|
|
560
|
+
};
|
|
561
|
+
return { width, height, plays: 0, frames: still(), icc: await profile(p.iccp), orientation };
|
|
551
562
|
}
|
|
552
|
-
const
|
|
553
|
-
for (const { control: c, data } of p.frames) {
|
|
563
|
+
for (const { control: c } of p.frames)
|
|
554
564
|
if (!c.width || !c.height || c.x + c.width > width || c.y + c.height > height)
|
|
555
565
|
throw new Error('PNG: invalid APNG frame region');
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
566
|
+
async function* frames() {
|
|
567
|
+
let count = 0;
|
|
568
|
+
for (const { control: c, data } of p.frames) {
|
|
569
|
+
let pixels;
|
|
570
|
+
try {
|
|
571
|
+
pixels = await decodeImageData(p, data, c.width, c.height);
|
|
572
|
+
}
|
|
573
|
+
catch (error) {
|
|
574
|
+
if (p.truncated && count)
|
|
575
|
+
return;
|
|
576
|
+
throw error;
|
|
577
|
+
}
|
|
578
|
+
yield {
|
|
579
|
+
x: c.x, y: c.y, width: c.width, height: c.height, duration: c.delay,
|
|
580
|
+
dispose: !count++ && c.dispose === 2 ? 1 : c.dispose,
|
|
581
|
+
blend: c.blend,
|
|
582
|
+
pixels
|
|
583
|
+
};
|
|
584
|
+
if (firstFrameOnly)
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
564
587
|
}
|
|
565
|
-
return { width, height, plays: p.animation.plays, frames, icc: await profile(p.iccp), orientation };
|
|
588
|
+
return { width, height, plays: p.animation.plays, frames: frames(), icc: await profile(p.iccp), orientation };
|
|
566
589
|
}
|
|
567
590
|
export function compositeAnimation({ width, height, frames }) {
|
|
591
|
+
const step = compositor(width, height);
|
|
592
|
+
return frames.map(f => ({ pixels: step(f), duration: f.duration }));
|
|
593
|
+
}
|
|
594
|
+
export function compositor(width, height) {
|
|
568
595
|
const canvas = new Uint8Array(width * height * 4);
|
|
569
|
-
|
|
570
|
-
for (const f of frames) {
|
|
596
|
+
return f => {
|
|
571
597
|
const previous = f.dispose === 2 ? canvas.slice() : null;
|
|
572
598
|
for (let y = 0; y < f.height; y++) {
|
|
573
599
|
if (f.blend === 0) {
|
|
@@ -593,14 +619,14 @@ export function compositeAnimation({ width, height, frames }) {
|
|
|
593
619
|
}
|
|
594
620
|
}
|
|
595
621
|
}
|
|
596
|
-
|
|
622
|
+
const shown = canvas.slice();
|
|
597
623
|
if (f.dispose === 1)
|
|
598
624
|
for (let y = 0; y < f.height; y++)
|
|
599
625
|
canvas.fill(0, ((f.y + y) * width + f.x) * 4, ((f.y + y) * width + f.x + f.width) * 4);
|
|
600
626
|
else if (previous)
|
|
601
627
|
canvas.set(previous);
|
|
602
|
-
|
|
603
|
-
|
|
628
|
+
return shown;
|
|
629
|
+
};
|
|
604
630
|
}
|
|
605
631
|
const RAW_EXIF = /^Raw profile type (exif|APP1)$/;
|
|
606
632
|
const XMP = 'XML:com.adobe.xmp';
|
package/dist/codecs/quantize.js
CHANGED
|
@@ -1,31 +1,43 @@
|
|
|
1
|
+
import { oneOf, finite } 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);
|
|
4
5
|
export function paletteOptions(colors, dither, ditherStrength) {
|
|
5
6
|
let count;
|
|
6
7
|
if (colors !== undefined) {
|
|
7
|
-
count =
|
|
8
|
+
count = finite(colors, 'colors');
|
|
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
|
-
|
|
13
|
-
const strength = Number(ditherStrength);
|
|
14
|
-
if (!Number.isFinite(strength))
|
|
15
|
-
throw new RangeError(`Invalid ditherStrength: ${ditherStrength}`);
|
|
12
|
+
oneOf(dither, DITHER_METHODS, 'dither');
|
|
13
|
+
const strength = finite(ditherStrength, 'ditherStrength');
|
|
16
14
|
if (strength < 0 || strength > 1)
|
|
17
15
|
throw new RangeError(`ditherStrength must be 0-1, not ${ditherStrength}`);
|
|
18
16
|
return { colors: count, dither: dither, strength };
|
|
19
17
|
}
|
|
20
|
-
export function dominantColor(pixels, include
|
|
21
|
-
const
|
|
22
|
-
|
|
18
|
+
export function dominantColor(pixels, include) {
|
|
19
|
+
const counts = new Uint32Array(4096), sums = new Float64Array(4096 * 3);
|
|
20
|
+
let found = false;
|
|
21
|
+
for (let i = 0; i < pixels.length; i += 4) {
|
|
22
|
+
if (pixels[i + 3] < 128)
|
|
23
|
+
continue;
|
|
24
|
+
const r = pixels[i], g = pixels[i + 1], b = pixels[i + 2];
|
|
25
|
+
if (!include(r, g, b))
|
|
26
|
+
continue;
|
|
27
|
+
const bin = (r >> 4) << 8 | (g >> 4) << 4 | b >> 4;
|
|
28
|
+
counts[bin]++;
|
|
29
|
+
sums[bin * 3] += r;
|
|
30
|
+
sums[bin * 3 + 1] += g;
|
|
31
|
+
sums[bin * 3 + 2] += b;
|
|
32
|
+
found = true;
|
|
33
|
+
}
|
|
34
|
+
if (!found)
|
|
23
35
|
return null;
|
|
24
36
|
let best = 0;
|
|
25
|
-
for (let
|
|
26
|
-
if (
|
|
27
|
-
best =
|
|
28
|
-
return [Math.round(
|
|
37
|
+
for (let bin = 1; bin < 4096; bin++)
|
|
38
|
+
if (counts[bin] > counts[best])
|
|
39
|
+
best = bin;
|
|
40
|
+
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
41
|
}
|
|
30
42
|
export function quantize(pixels, width, { maxColors = 256, dither = 'floyd-steinberg', ditherStrength = 0.85, transparent = false } = {}) {
|
|
31
43
|
const length = pixels.length >> 2;
|
package/dist/codecs/tiff.js
CHANGED
|
@@ -118,6 +118,8 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
|
|
|
118
118
|
const samplesPerPixel = value(TAGS.SamplesPerPixel, 1);
|
|
119
119
|
const bitsPerSample = values(TAGS.BitsPerSample, [1]);
|
|
120
120
|
const bps = bitsPerSample[0];
|
|
121
|
+
if (typeof bps !== 'number')
|
|
122
|
+
throw new Error('TIFF: invalid BitsPerSample');
|
|
121
123
|
if (bitsPerSample.some(b => b !== bps))
|
|
122
124
|
throw new Error('TIFF: mixed bit depths are not supported');
|
|
123
125
|
const compression = value(TAGS.Compression, 1);
|
|
@@ -160,6 +162,8 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
|
|
|
160
162
|
checkPixels(width, height, maxPixels);
|
|
161
163
|
checkPixels(segmentWidth, segmentHeight, Math.max(4 * width * height, 1 << 20));
|
|
162
164
|
checkBytes(Math.ceil(width * samplesPerPixel * bps / 8) * height, maxPixels);
|
|
165
|
+
if (samplesPerPixel > 32)
|
|
166
|
+
throw new Error('TIFF: unsupported number of samples per pixel');
|
|
163
167
|
const pixels = new Uint8Array(width * height * 4);
|
|
164
168
|
if (compression === 7) {
|
|
165
169
|
const jpegTables = tags.get(TAGS.JPEGTables);
|
|
@@ -186,8 +190,11 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
|
|
|
186
190
|
if (tiled || planar || bps !== 8)
|
|
187
191
|
throw new Error('TIFF: subsampled YCbCr is only supported for 8-bit strips');
|
|
188
192
|
const [h, v] = subsampling;
|
|
193
|
+
if (![1, 2, 4].includes(h) || ![1, 2, 4].includes(v))
|
|
194
|
+
throw new Error('TIFF: unsupported YCbCr subsampling');
|
|
189
195
|
const unitsPerRow = Math.ceil(width / h);
|
|
190
196
|
const unitRowBytes = unitsPerRow * (h * v + 2);
|
|
197
|
+
checkBytes(Math.ceil(height / v) * unitRowBytes, maxPixels);
|
|
191
198
|
const units = new Uint8Array(Math.ceil(height / v) * unitRowBytes);
|
|
192
199
|
let written = 0;
|
|
193
200
|
for (let s = 0; s < segmentsPerPlane && written < units.length; s++) {
|
package/dist/codecs/webp.js
CHANGED
|
@@ -22,7 +22,7 @@ function chunks(data, start, end) {
|
|
|
22
22
|
const type = view.getUint32(offset, true);
|
|
23
23
|
const size = view.getUint32(offset + 4, true);
|
|
24
24
|
const dataStart = offset + 8;
|
|
25
|
-
out.push({ type, data: data.subarray(dataStart, Math.min(end, dataStart + size)) });
|
|
25
|
+
out.push({ type, data: data.subarray(dataStart, Math.min(end, dataStart + size)), cut: dataStart + size > end });
|
|
26
26
|
offset = dataStart + size + (size & 1);
|
|
27
27
|
}
|
|
28
28
|
return out;
|
|
@@ -148,19 +148,22 @@ export function decodeAnimation(data, firstFrameOnly = false, maxPixels = MAX_PI
|
|
|
148
148
|
const width = u24(header.data, 4) + 1, height = u24(header.data, 7) + 1;
|
|
149
149
|
checkPixels(width, height, maxPixels, firstFrameOnly ? 1 : list.filter(c => c.type === ANMF).length);
|
|
150
150
|
let loopCount = 0, background = 0;
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
151
|
+
const anim = list.find(c => c.type === ANIM && c.data.length >= 6);
|
|
152
|
+
if (anim) {
|
|
153
|
+
const d = anim.data;
|
|
154
|
+
background = (d[3] << 24 | d[2] << 16 | d[1] << 8 | d[0]) >>> 0;
|
|
155
|
+
loopCount = d[4] | d[5] << 8;
|
|
156
|
+
}
|
|
157
|
+
const anmf = list.filter(c => c.type === ANMF && !c.cut);
|
|
158
|
+
if (!anmf.length && list.some(c => c.type === ANMF))
|
|
159
|
+
throw new Error('WebP: unexpected end of data');
|
|
160
|
+
for (const { data } of anmf)
|
|
161
|
+
if (data.length < 16)
|
|
162
|
+
throw new Error('WebP: invalid ANMF chunk');
|
|
163
|
+
function* frames() {
|
|
164
|
+
for (const { data: d } of anmf) {
|
|
162
165
|
const frame = decodeBitstream(chunks(d, 16, d.length), maxPixels);
|
|
163
|
-
|
|
166
|
+
yield {
|
|
164
167
|
x: u24(d, 0) * 2,
|
|
165
168
|
y: u24(d, 3) * 2,
|
|
166
169
|
width: frame.width,
|
|
@@ -169,14 +172,12 @@ export function decodeAnimation(data, firstFrameOnly = false, maxPixels = MAX_PI
|
|
|
169
172
|
blend: (d[15] & 2) === 0,
|
|
170
173
|
dispose: (d[15] & 1) === 1,
|
|
171
174
|
pixels: frame.pixels
|
|
172
|
-
}
|
|
175
|
+
};
|
|
173
176
|
if (firstFrameOnly)
|
|
174
|
-
|
|
177
|
+
return;
|
|
175
178
|
}
|
|
176
179
|
}
|
|
177
|
-
|
|
178
|
-
throw new Error('WebP: animation without frames');
|
|
179
|
-
return { width, height, loopCount, background, frames, icc: list.find(c => c.type === ICCP)?.data, orientation: orientationOf(list) };
|
|
180
|
+
return { width, height, loopCount, background, frames: frames(), icc: list.find(c => c.type === ICCP)?.data, orientation: orientationOf(list) };
|
|
180
181
|
}
|
|
181
182
|
function orientationOf(list) {
|
|
182
183
|
const exif = list.find(c => c.type === EXIF);
|
package/dist/colors.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { finite, color as checkColor } from "./util/options.js";
|
|
1
|
+
import { finite, checkNumber, color as checkColor } from "./util/options.js";
|
|
2
2
|
export function channel(value) {
|
|
3
3
|
return value > 255 ? 255 : value > 0 ? Math.round(value) : 0;
|
|
4
4
|
}
|
|
@@ -48,20 +48,27 @@ export function rgbaToHSLA(r, g, b, a) {
|
|
|
48
48
|
}
|
|
49
49
|
return [h, s, l, a / 255];
|
|
50
50
|
}
|
|
51
|
+
function checkPosition(position) {
|
|
52
|
+
checkNumber(position, 'gradient position');
|
|
53
|
+
if (Number.isNaN(position))
|
|
54
|
+
throw new RangeError(`Invalid gradient position: ${position}`);
|
|
55
|
+
}
|
|
51
56
|
function gradientBetween(startColor, endColor) {
|
|
52
|
-
const sr = startColor >>> 24;
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
57
|
+
const sr = startColor >>> 24, sg = startColor >> 16 & 0xff, sb = startColor >> 8 & 0xff, sa = startColor & 0xff;
|
|
58
|
+
const er = (endColor >>> 24) - sr, eg = (endColor >> 16 & 0xff) - sg, eb = (endColor >> 8 & 0xff) - sb, ea = (endColor & 0xff) - sa;
|
|
59
|
+
if (sa === 255 && ea === 0)
|
|
60
|
+
return position => {
|
|
61
|
+
const r = sr + position * er, g = sg + position * eg, b = sb + position * eb;
|
|
62
|
+
return (((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | 0xff) >>> 0;
|
|
63
|
+
};
|
|
64
|
+
const pr = sr * sa / 255, pg = sg * sa / 255, pb = sb * sa / 255;
|
|
65
|
+
const dr = (er + sr) * (ea + sa) / 255 - pr, dg = (eg + sg) * (ea + sa) / 255 - pg, db = (eb + sb) * (ea + sa) / 255 - pb;
|
|
60
66
|
return position => {
|
|
61
|
-
const r = sr + position * er;
|
|
62
|
-
const g = sg + position * eg;
|
|
63
|
-
const b = sb + position * eb;
|
|
64
67
|
const a = sa + position * ea;
|
|
68
|
+
if (a <= 0)
|
|
69
|
+
return 0;
|
|
70
|
+
const k = 255 / a;
|
|
71
|
+
const r = Math.min(255, (pr + position * dr) * k + 1e-9), g = Math.min(255, (pg + position * dg) * k + 1e-9), b = Math.min(255, (pb + position * db) * k + 1e-9);
|
|
65
72
|
return (((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | (a & 0xff)) >>> 0;
|
|
66
73
|
};
|
|
67
74
|
}
|
|
@@ -130,13 +137,15 @@ export class Colors {
|
|
|
130
137
|
if (positions.length === 0)
|
|
131
138
|
throw new RangeError('Invalid gradient point count');
|
|
132
139
|
else if (positions.length === 1) {
|
|
133
|
-
return
|
|
140
|
+
return position => {
|
|
141
|
+
checkPosition(position);
|
|
142
|
+
return values[0];
|
|
143
|
+
};
|
|
134
144
|
}
|
|
135
145
|
else if (positions.length === 2) {
|
|
136
146
|
const gradient = gradientBetween(values[0], values[1]);
|
|
137
147
|
return position => {
|
|
138
|
-
|
|
139
|
-
throw new RangeError(`Invalid gradient position: ${position}`);
|
|
148
|
+
checkPosition(position);
|
|
140
149
|
if (position <= positions[0])
|
|
141
150
|
return values[0];
|
|
142
151
|
if (position >= positions[1])
|
|
@@ -163,8 +172,7 @@ export class Colors {
|
|
|
163
172
|
}
|
|
164
173
|
if (!gradients.length)
|
|
165
174
|
return position => {
|
|
166
|
-
|
|
167
|
-
throw new RangeError(`Invalid gradient position: ${position}`);
|
|
175
|
+
checkPosition(position);
|
|
168
176
|
return position <= minDef ? values[0] : values[values.length - 1];
|
|
169
177
|
};
|
|
170
178
|
return position => {
|
package/dist/frame.d.ts
CHANGED
|
@@ -9,17 +9,30 @@ import { Image, ResizeOptions, ResizeToOptions, TrimOptions, RotateOptions, Tran
|
|
|
9
9
|
export type FrameDisposalMode = 'any' | 'keep' | 'previous' | 'background';
|
|
10
10
|
/** Options of {@link Frame} */
|
|
11
11
|
export interface FrameOptions {
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* The frame's duration in milliseconds
|
|
14
|
+
* @defaultValue `100`
|
|
15
|
+
*/
|
|
13
16
|
duration?: number;
|
|
14
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* The frame's offset on the x-axis
|
|
19
|
+
* @defaultValue `0`
|
|
20
|
+
*/
|
|
15
21
|
xOffset?: number;
|
|
16
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* The frame's offset on the y-axis
|
|
24
|
+
* @defaultValue `0`
|
|
25
|
+
*/
|
|
17
26
|
yOffset?: number;
|
|
18
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* The frame's disposal mode, see {@link Frame.disposalMode}
|
|
29
|
+
* @defaultValue `'keep'`
|
|
30
|
+
*/
|
|
19
31
|
disposalMode?: FrameDisposalMode;
|
|
20
32
|
}
|
|
21
33
|
/**
|
|
22
|
-
* A frame of an {@link Animation}: an {@link Image} ({@link Frame.image}) with a duration, offsets and a disposal mode
|
|
34
|
+
* A frame of an {@link Animation}: an {@link Image} ({@link Frame.image}) with a duration, offsets and a disposal mode
|
|
35
|
+
* @remarks
|
|
23
36
|
* The frame has the methods that change the image's size (`resize()` including its `'contain'` and `'cover'` fit
|
|
24
37
|
* modes, `scale()`, `crop()`, `trim()`, `rotate()`, `transform()`, `dropShadow()` and `outline()`), which move the
|
|
25
38
|
* offsets along so the frame stays in place in the animation; every other change goes to the image
|
package/dist/gainmap.d.ts
CHANGED
|
@@ -1,38 +1,63 @@
|
|
|
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
|
+
/**
|
|
7
|
+
* The JPEG quality (1-100)
|
|
8
|
+
* @defaultValue `90`
|
|
9
|
+
*/
|
|
6
10
|
quality?: number;
|
|
7
11
|
}
|
|
8
12
|
/** A gain-map parameter shared across channels or specified as `[red, green, blue]` */
|
|
9
13
|
export type GainMapChannelValues = number | readonly [red: number, green: number, blue: number];
|
|
10
14
|
/** Options of the {@link GainMap} constructor */
|
|
11
15
|
export interface GainMapOptions {
|
|
12
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Extra brightness at the map's darkest value (0); `-1` means half as bright
|
|
18
|
+
* @defaultValue `0` (no change)
|
|
19
|
+
*/
|
|
13
20
|
min?: GainMapChannelValues;
|
|
14
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Extra brightness at the map's brightest value (255); `2` means four times as bright
|
|
23
|
+
* @defaultValue `2`
|
|
24
|
+
*/
|
|
15
25
|
max?: GainMapChannelValues;
|
|
16
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* How quickly the brightness changes between `min` and `max`; `1` changes it evenly
|
|
28
|
+
* @defaultValue `1`
|
|
29
|
+
*/
|
|
17
30
|
gamma?: GainMapChannelValues;
|
|
18
31
|
/**
|
|
19
32
|
* Small brightness offset added before applying the boost, to keep black from being boosted indefinitely.
|
|
20
|
-
*
|
|
33
|
+
* @defaultValue `1/64`
|
|
21
34
|
*/
|
|
22
35
|
offsetSDR?: GainMapChannelValues;
|
|
23
|
-
/**
|
|
36
|
+
/**
|
|
37
|
+
* Small brightness offset subtracted from the result
|
|
38
|
+
* @defaultValue `1/64`
|
|
39
|
+
*/
|
|
24
40
|
offsetHDR?: GainMapChannelValues;
|
|
25
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* How bright a display must be before the map starts to apply; `0` is standard-screen brightness
|
|
43
|
+
* @defaultValue `0`
|
|
44
|
+
*/
|
|
26
45
|
hdrCapacityMin?: number;
|
|
27
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* How bright a display must be for the map to apply fully
|
|
48
|
+
* @defaultValue The largest value in `max`
|
|
49
|
+
*/
|
|
28
50
|
hdrCapacityMax?: number;
|
|
29
|
-
/**
|
|
51
|
+
/**
|
|
52
|
+
* Whether the boost uses the original image's colors (`false`: the brighter HDR colors)
|
|
53
|
+
* @defaultValue `true`
|
|
54
|
+
*/
|
|
30
55
|
useBaseColorSpace?: boolean;
|
|
31
56
|
}
|
|
32
57
|
/**
|
|
33
58
|
* A gain map is a small companion image that tells a high-dynamic-range (HDR) display how much brighter to show each
|
|
34
|
-
* part of a photo. Standard displays ignore it and show the regular-brightness image
|
|
35
|
-
*
|
|
59
|
+
* part of a photo. Standard displays ignore it and show the regular-brightness image
|
|
60
|
+
* @remarks
|
|
36
61
|
* {@link Image.decode} reads gain maps from supported JPEG, AVIF and JPEG XL images into {@link Image.gainMap};
|
|
37
62
|
* {@link Image.encodeJPEG}, {@link Image.encodeAVIF} and {@link Image.encodeJXL} can write them. The map covers the
|
|
38
63
|
* whole image, at any resolution. Each pixel's value (0-255) controls how much extra brightness to add at that
|
|
@@ -129,12 +154,16 @@ export declare class GainMap {
|
|
|
129
154
|
* Encodes the gain map alone: a JPEG with its parameters (hdrgm XMP and ISO 21496-1), like the second image in an
|
|
130
155
|
* Ultra HDR JPEG
|
|
131
156
|
* @param options See {@link GainMapEncodeOptions}
|
|
157
|
+
* @see {@link Image.encodeJPEG} for the image with its gain map embedded
|
|
132
158
|
*/
|
|
133
|
-
|
|
159
|
+
encodeJPEG(options?: GainMapEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
|
|
134
160
|
/**
|
|
135
161
|
* Decodes a gain map JPEG with its parameters (hdrgm XMP or ISO 21496-1), like the second image in an Ultra HDR
|
|
136
162
|
* JPEG, or the gain map of an Ultra HDR JPEG (or an iPhone's JPEG: its values converted to these parameters)
|
|
137
163
|
* @param data The JPEG
|
|
164
|
+
* @param options The `maxPixels` limit, like {@link DecodeOptions.maxPixels}
|
|
165
|
+
* @throws {@link LimitError} if the map has more than `options.maxPixels` pixels
|
|
166
|
+
* @throws `Error` if the data has no gain map (or no parameters for it), or is damaged
|
|
138
167
|
*/
|
|
139
|
-
static decode(data: BinaryData): Promise<GainMap>;
|
|
168
|
+
static decode(data: BinaryData, options?: Pick<DecodeOptions, 'maxPixels'>): Promise<GainMap>;
|
|
140
169
|
}
|
package/dist/gainmap.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
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) => {
|
|
8
|
+
const given = value;
|
|
9
|
+
if (typeof given !== 'number' && !Array.isArray(given))
|
|
10
|
+
throw new TypeError(`${name} must be a number or an array of 3 numbers, not ${given === null ? 'null' : typeof given}`);
|
|
7
11
|
const values = typeof value === 'number' ? [value, value, value] : Array.from(value);
|
|
8
12
|
if (values.length === 1)
|
|
9
13
|
values.push(values[0], values[0]);
|
|
@@ -62,13 +66,13 @@ export class GainMap {
|
|
|
62
66
|
this.offsetSDR = offsetSDR;
|
|
63
67
|
this.offsetHDR = offsetHDR;
|
|
64
68
|
this.hdrCapacityMin = hdrCapacityMin;
|
|
65
|
-
this.hdrCapacityMax = options.hdrCapacityMax
|
|
69
|
+
this.hdrCapacityMax = options.hdrCapacityMax === undefined ? Math.max(...this.__max__) : options.hdrCapacityMax;
|
|
66
70
|
this.useBaseColorSpace = useBaseColorSpace;
|
|
67
71
|
this.__pixels__ = image;
|
|
68
72
|
this.__check__();
|
|
69
73
|
}
|
|
70
|
-
static __from_jpeg__(map, params, apple = 0) {
|
|
71
|
-
const { width, height, pixels } = jpeg.decode(map);
|
|
74
|
+
static __from_jpeg__(map, params, apple = 0, maxPixels = MAX_PIXELS) {
|
|
75
|
+
const { width, height, pixels } = jpeg.decode(map, { maxPixels });
|
|
72
76
|
const image = new Image(width, height);
|
|
73
77
|
image.bitmap = alignedPixels(pixels);
|
|
74
78
|
if (apple) {
|
|
@@ -118,9 +122,9 @@ export class GainMap {
|
|
|
118
122
|
__check__() {
|
|
119
123
|
for (let c = 0; c < 3; c++)
|
|
120
124
|
if (this.__max__[c] < this.__min__[c])
|
|
121
|
-
throw new RangeError(
|
|
125
|
+
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
126
|
if (this.__capacityMax__ <= this.__capacityMin__)
|
|
123
|
-
throw new RangeError(
|
|
127
|
+
throw new RangeError(`The gain map's hdrCapacityMax (by default the largest max) must be above its hdrCapacityMin (${this.__capacityMax__} <= ${this.__capacityMin__})`);
|
|
124
128
|
}
|
|
125
129
|
get image() {
|
|
126
130
|
return this.__pixels__;
|
|
@@ -200,21 +204,26 @@ export class GainMap {
|
|
|
200
204
|
}
|
|
201
205
|
this.image = out;
|
|
202
206
|
}
|
|
203
|
-
async
|
|
204
|
-
checkOptions(options, '
|
|
207
|
+
async encodeJPEG(options) {
|
|
208
|
+
checkOptions(options, 'gainMap.encodeJPEG({ quality: 80 })');
|
|
205
209
|
let quality = options?.quality;
|
|
206
210
|
if (quality !== undefined)
|
|
207
211
|
quality = Math.max(1, checkQuality(quality));
|
|
208
212
|
return codec.mapWithMetadata(this.__body__(quality), this.__params__());
|
|
209
213
|
}
|
|
210
|
-
static async decode(data) {
|
|
214
|
+
static async decode(data, options = {}) {
|
|
215
|
+
checkOptions(options, 'GainMap.decode(data, { maxPixels: 1e8 })');
|
|
216
|
+
const { maxPixels = MAX_PIXELS } = options;
|
|
217
|
+
pixelLimit(maxPixels);
|
|
211
218
|
const bytes = view(data);
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
+
return decoding(() => {
|
|
220
|
+
const found = codec.find(bytes);
|
|
221
|
+
if (found)
|
|
222
|
+
return GainMap.__from_jpeg__(found.map, found.params, found.apple, maxPixels);
|
|
223
|
+
const params = codec.readParams(bytes);
|
|
224
|
+
if (!params)
|
|
225
|
+
throw new Error('No gain map (or no gain map parameters) found');
|
|
226
|
+
return GainMap.__from_jpeg__(bytes, params, 0, maxPixels);
|
|
227
|
+
});
|
|
219
228
|
}
|
|
220
229
|
}
|