imagescript 3.14.0 → 3.16.0
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 +14 -0
- package/dist/animation-encoder.d.ts +1 -1
- package/dist/animation-encoder.js +1 -1
- package/dist/animation.d.ts +1 -1
- package/dist/animation.js +1 -1
- package/dist/codecs/avif-enc.js +48 -19
- package/dist/codecs/avif.js +33 -4
- package/dist/codecs/gainmap.js +446 -0
- package/dist/codecs/jpeg.js +78 -16
- package/dist/codecs/jxl.js +57 -15
- package/dist/codecs/vp8l.js +65 -27
- package/dist/codecs/webp.js +7 -7
- package/dist/frame.js +4 -0
- package/dist/gainmap.d.ts +82 -0
- package/dist/gainmap.js +195 -0
- package/dist/image.d.ts +46 -4
- package/dist/image.js +278 -31
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/util/gain.js +209 -0
- package/dist/util/resample.js +203 -61
- package/dist/version.js +1 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -23,6 +23,19 @@ npm install imagescript # Node.js, Bun and bundlers
|
|
|
23
23
|
deno add jsr:@matmen/imagescript # Deno
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
+
Without a bundler, import it from jsDelivr in the browser:
|
|
27
|
+
|
|
28
|
+
```html
|
|
29
|
+
<script type="module">
|
|
30
|
+
import { Image } from 'https://cdn.jsdelivr.net/npm/imagescript@3/+esm';
|
|
31
|
+
|
|
32
|
+
const image = await Image.decode(await fetch('photo.jpg').then(r => r.arrayBuffer()));
|
|
33
|
+
image.resize(256, Image.RESIZE_AUTO);
|
|
34
|
+
const url = URL.createObjectURL(new Blob([await image.encode()], { type: 'image/png' }));
|
|
35
|
+
document.body.append(Object.assign(document.createElement('img'), { src: url }));
|
|
36
|
+
</script>
|
|
37
|
+
```
|
|
38
|
+
|
|
26
39
|
---
|
|
27
40
|
|
|
28
41
|
### Example
|
|
@@ -58,6 +71,7 @@ await writeFile('output.png', await image.encode());
|
|
|
58
71
|
| GIF, APNG (animations) | ✓ | ✓ |
|
|
59
72
|
| BMP, QOI, TGA, Netpbm | ✓ | ✓ |
|
|
60
73
|
| TIFF, ICO/CUR, Radiance HDR | ✓ | |
|
|
74
|
+
| [HDR gain maps](https://imagescript.dev/classes/GainMap.html) (Ultra HDR and iPhone JPEGs, AVIF, JPEG XL) | ✓ | ✓, kept through edits |
|
|
61
75
|
|
|
62
76
|
[Animations](https://imagescript.dev/classes/Animation.html) can be decoded from GIF, APNG, WebP and JPEG XL,
|
|
63
77
|
and encoded as GIF, APNG or WebP, all at once or
|
|
@@ -17,7 +17,7 @@ export interface AnimationEncoderOptions {
|
|
|
17
17
|
export interface WebPAnimationEncoderOptions extends AnimationEncoderOptions {
|
|
18
18
|
/** Lossy frames (0-100), see {@link WebPAnimationEncodeOptions.quality}. Without it the frames are lossless */
|
|
19
19
|
quality?: number;
|
|
20
|
-
/** How much effort
|
|
20
|
+
/** How much effort the frames take (0-6, lossy and lossless), see {@link WebPEncodeOptions.effort}. Default: `2` */
|
|
21
21
|
effort?: number;
|
|
22
22
|
/** Lossy frames: libwebp's sharp YUV conversion, see {@link WebPEncodeOptions.sharpYuv}. Default: `false` */
|
|
23
23
|
sharpYuv?: boolean;
|
|
@@ -77,7 +77,7 @@ export class AnimationEncoder {
|
|
|
77
77
|
let full = false;
|
|
78
78
|
const add = async (f) => {
|
|
79
79
|
alpha ||= webp.needsAlpha(f, !chunks.length, width, height);
|
|
80
|
-
chunks.push(await webp.frameChunk(f, lossy));
|
|
80
|
+
chunks.push(await webp.frameChunk(f, lossy, effort));
|
|
81
81
|
};
|
|
82
82
|
return new AnimationEncoder(width, height, {
|
|
83
83
|
optimize: { replace: true, even: true, alpha: 'frame', tolerance: lossy ? webp.lossyTolerance(lossy.quality) : undefined },
|
package/dist/animation.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export interface WebPAnimationEncodeOptions extends AnimationEncodeOptions {
|
|
|
42
42
|
* up to 3 per channel at quality 85, 1 at 100)
|
|
43
43
|
*/
|
|
44
44
|
quality?: number;
|
|
45
|
-
/** How much effort
|
|
45
|
+
/** How much effort the frames take (0-6, lossy and lossless), see {@link WebPEncodeOptions.effort}. Default: `2` */
|
|
46
46
|
effort?: number;
|
|
47
47
|
/** Lossy frames: libwebp's sharp YUV conversion, see {@link WebPEncodeOptions.sharpYuv}. Default: `false` */
|
|
48
48
|
sharpYuv?: boolean;
|
package/dist/animation.js
CHANGED
|
@@ -134,7 +134,7 @@ export class Animation extends Array {
|
|
|
134
134
|
const lossy = webp.lossyOptions(quality, effort, sharpYuv, alphaQuality);
|
|
135
135
|
const tolerance = lossy ? webp.lossyTolerance(lossy.quality) : undefined;
|
|
136
136
|
const frames = optimize ? this.__optimized__({ replace: true, even: true, alpha: 'frame', tolerance }) : this.__webpFrames__();
|
|
137
|
-
return webp.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), exifData(resolveMetadata(metadata)), lossy);
|
|
137
|
+
return webp.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), exifData(resolveMetadata(metadata)), lossy, effort);
|
|
138
138
|
}
|
|
139
139
|
__webpFrames__() {
|
|
140
140
|
const width = this.width, height = this.height;
|
package/dist/codecs/avif-enc.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { encodeAV1, encodeAV1Threaded } from "./av1/encode.js";
|
|
2
2
|
import { startThreads } from "../util/threads.js";
|
|
3
|
+
import { writeISO } from "./gainmap.js";
|
|
3
4
|
const MIN_THREADED_PIXELS = [640 * 640, 448 * 448, 320 * 320], PIXELS_PER_THREAD = [512 << 10, 256 << 10, 256 << 10];
|
|
4
5
|
const QINDEX = [
|
|
5
6
|
0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 108, 112,
|
|
@@ -8,6 +9,7 @@ const QINDEX = [
|
|
|
8
9
|
];
|
|
9
10
|
const qindex = (quality) => QINDEX[Math.round((100 - Math.max(0, Math.min(100, quality))) * 63 / 100)];
|
|
10
11
|
const KR = 0.299, KB = 0.114, KG = 1 - KR - KB, CICP = [1, 13, 6];
|
|
12
|
+
const MAP_CICP = [2, 2, 6];
|
|
11
13
|
function toYUV(rgba, width, height, subsampled) {
|
|
12
14
|
const n = width * height, y = new Uint8Array(n);
|
|
13
15
|
const u = new Float64Array(n), v = new Float64Array(n);
|
|
@@ -151,7 +153,7 @@ function seqMonochrome(d, o) {
|
|
|
151
153
|
read(1);
|
|
152
154
|
return !!read(1);
|
|
153
155
|
}
|
|
154
|
-
export async function encode(rgba, width, height, { quality, alphaQuality, subsampling, effort, exif, threads: maxThreads }) {
|
|
156
|
+
export async function encode(rgba, width, height, { quality, alphaQuality, subsampling, effort, exif, threads: maxThreads, gainMap }) {
|
|
155
157
|
if (width < 1 || height < 1)
|
|
156
158
|
throw new RangeError('AVIF: dimensions must be at least 1');
|
|
157
159
|
if (width > 65536 || height > 65536)
|
|
@@ -194,7 +196,14 @@ export async function encode(rgba, width, height, { quality, alphaQuality, subsa
|
|
|
194
196
|
}
|
|
195
197
|
return encodeAV1(image, params);
|
|
196
198
|
};
|
|
197
|
-
let color, alpha = null;
|
|
199
|
+
let color, alpha = null, map = null;
|
|
200
|
+
let mapGray = true;
|
|
201
|
+
if (gainMap)
|
|
202
|
+
for (let i = 0; i < gainMap.pixels.length; i += 4)
|
|
203
|
+
if (gainMap.pixels[i] !== gainMap.pixels[i + 1] || gainMap.pixels[i] !== gainMap.pixels[i + 2]) {
|
|
204
|
+
mapGray = false;
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
198
207
|
try {
|
|
199
208
|
color = await av1({ planes, width, height, layout: gray ? 0 : subsampled ? 1 : 3, cicp }, { qidx: qindex(quality), effort });
|
|
200
209
|
if (!opaque) {
|
|
@@ -203,36 +212,56 @@ export async function encode(rgba, width, height, { quality, alphaQuality, subsa
|
|
|
203
212
|
a[i] = original[4 * i + 3];
|
|
204
213
|
alpha = await av1({ planes: [a], width, height, layout: 0, cicp: [2, 2, 2] }, { qidx: qindex(alphaQuality), effort });
|
|
205
214
|
}
|
|
215
|
+
if (gainMap) {
|
|
216
|
+
const { pixels, width: w, height: h } = gainMap;
|
|
217
|
+
const mapPlanes = mapGray ? [Uint8Array.from({ length: w * h }, (_, i) => pixels[4 * i])] : toYUV(pixels, w, h, false);
|
|
218
|
+
map = await av1({ planes: mapPlanes, width: w, height: h, layout: mapGray ? 0 : 3, cicp: MAP_CICP }, { qidx: qindex(gainMap.quality), effort });
|
|
219
|
+
}
|
|
206
220
|
}
|
|
207
221
|
finally {
|
|
208
222
|
stop();
|
|
209
223
|
}
|
|
210
224
|
const profile = subsampled || gray ? 'MA1B' : 'MA1A';
|
|
211
|
-
const ftyp = box('ftyp', ascii('avif'), u32(0), ascii('avif'), ascii('mif1'), ascii('miaf'), ascii(profile));
|
|
225
|
+
const ftyp = box('ftyp', ascii('avif'), u32(0), ascii('avif'), ascii('mif1'), ascii('miaf'), ascii(profile), map ? ascii('tmap') : []);
|
|
212
226
|
const hdlr = fullBox('hdlr', 0, 0, u32(0), ascii('pict'), u32(0), u32(0), u32(0), [0]);
|
|
213
227
|
const pitm = fullBox('pitm', 0, 0, u16(1));
|
|
214
|
-
const items = [
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
items.push({ type: 'Exif', name: 'Exif', data: [u32(0), exif] });
|
|
219
|
-
const sizes = items.map(item => item.data.reduce((n, d) => n + d.length, 0));
|
|
220
|
-
const infe = (id, type, name) => fullBox('infe', 2, 0, u16(id), u16(0), ascii(type), ascii(name), [0]);
|
|
221
|
-
const iinf = fullBox('iinf', 0, 0, u16(items.length), ...items.map((item, i) => infe(i + 1, item.type, item.name)));
|
|
222
|
-
const exifId = exif?.length ? items.length : 0;
|
|
223
|
-
const iref = alpha || exifId ? fullBox('iref', 0, 0, alpha ? box('auxl', u16(2), u16(1), u16(1)) : [], exifId ? box('cdsc', u16(exifId), u16(1), u16(1)) : []) : [];
|
|
224
|
-
const ispe = fullBox('ispe', 0, 0, u32(width), u32(height));
|
|
225
|
-
const colr = box('colr', ascii('nclx'), u16(cicp[0]), u16(cicp[1]), u16(cicp[2]), [0x80]);
|
|
228
|
+
const items = [];
|
|
229
|
+
const properties = [];
|
|
230
|
+
const property = (p) => properties.push(p);
|
|
231
|
+
const nclx = (c) => box('colr', ascii('nclx'), u16(c[0]), u16(c[1]), u16(c[2]), [0x80]);
|
|
226
232
|
const pixi = (channels) => fullBox('pixi', 0, 0, [channels], new Array(channels).fill(8));
|
|
227
|
-
const
|
|
228
|
-
const
|
|
229
|
-
|
|
233
|
+
const ispe = property(fullBox('ispe', 0, 0, u32(width), u32(height)));
|
|
234
|
+
const colorConfig = property(av1C(color)) | 0x80, colr = property(nclx(cicp));
|
|
235
|
+
items.push({ type: 'av01', name: 'Color', data: [color], props: [ispe, colorConfig, colr, property(pixi(gray ? 1 : 3))] });
|
|
236
|
+
const alphaId = alpha ? items.push({
|
|
237
|
+
type: 'av01', name: 'Alpha', data: [alpha],
|
|
238
|
+
props: [ispe, property(av1C(alpha)) | 0x80, property(pixi(1)), property(fullBox('auxC', 0, 0, ascii('urn:mpeg:mpegB:cicp:systems:auxiliary:alpha'), [0]))]
|
|
239
|
+
}) : 0;
|
|
240
|
+
let mapId = 0, tmapId = 0;
|
|
241
|
+
if (map && gainMap) {
|
|
242
|
+
mapId = items.push({
|
|
243
|
+
type: 'av01', name: 'GMap', hidden: true, data: [map],
|
|
244
|
+
props: [property(fullBox('ispe', 0, 0, u32(gainMap.width), u32(gainMap.height))), property(av1C(map)) | 0x80, property(nclx(MAP_CICP)), property(pixi(mapGray ? 1 : 3))]
|
|
245
|
+
});
|
|
246
|
+
tmapId = items.push({ type: 'tmap', name: 'TMap', data: [[0], writeISO(gainMap.params)], props: [ispe, property(nclx([cicp[0], 16, cicp[2]]))] });
|
|
247
|
+
}
|
|
248
|
+
const exifId = exif?.length ? items.push({ type: 'Exif', name: 'Exif', data: [u32(0), exif], props: [] }) : 0;
|
|
249
|
+
const sizes = items.map(item => item.data.reduce((n, d) => n + d.length, 0));
|
|
250
|
+
const infe = (id, item) => fullBox('infe', 2, item.hidden ? 1 : 0, u16(id), u16(0), ascii(item.type), ascii(item.name), [0]);
|
|
251
|
+
const iinf = fullBox('iinf', 0, 0, u16(items.length), ...items.map((item, i) => infe(i + 1, item)));
|
|
252
|
+
const refs = [alphaId ? box('auxl', u16(alphaId), u16(1), u16(1)) : [], tmapId ? box('dimg', u16(tmapId), u16(2), u16(1), u16(mapId)) : [],
|
|
253
|
+
exifId ? box('cdsc', u16(exifId), u16(1), u16(1)) : []];
|
|
254
|
+
const iref = refs.some(r => r.length) ? fullBox('iref', 0, 0, ...refs) : [];
|
|
255
|
+
const ipco = box('ipco', ...properties);
|
|
256
|
+
const associated = items.map((item, i) => [i + 1, item.props]).filter(([, props]) => props.length);
|
|
257
|
+
const ipma = fullBox('ipma', 0, 0, u32(associated.length), ...associated.map(([id, props]) => [...u16(id), props.length, ...props]));
|
|
230
258
|
const iprp = box('iprp', ipco, ipma);
|
|
259
|
+
const grpl = tmapId ? box('grpl', fullBox('altr', 0, 0, u32(items.length + 1), u32(2), u32(tmapId), u32(1))) : [];
|
|
231
260
|
const ilocFor = (offset) => fullBox('iloc', 0, 0, [0x44, 0x00], u16(items.length), ...items.map((_, i) => {
|
|
232
261
|
const at = offset + sizes.slice(0, i).reduce((n, size) => n + size, 0);
|
|
233
262
|
return [...u16(i + 1), ...u16(0), ...u16(1), ...u32(at), ...u32(sizes[i])];
|
|
234
263
|
}));
|
|
235
|
-
const metaFor = (offset) => fullBox('meta', 0, 0, hdlr, pitm, ilocFor(offset), iinf, iref, iprp);
|
|
264
|
+
const metaFor = (offset) => fullBox('meta', 0, 0, hdlr, pitm, ilocFor(offset), iinf, iref, iprp, grpl);
|
|
236
265
|
const headerSize = ftyp.length + metaFor(0).length + 8;
|
|
237
266
|
const head = [...ftyp, ...metaFor(headerSize)];
|
|
238
267
|
const mdatSize = 8 + sizes.reduce((n, size) => n + size, 0);
|
package/dist/codecs/avif.js
CHANGED
|
@@ -9,6 +9,7 @@ import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
|
9
9
|
import { decodeAV1 } from "./av1/frame.js";
|
|
10
10
|
import { toSRGB, hdrToLinear } from "./hdr.js";
|
|
11
11
|
import { runChunks, sharedOf, startThreads } from "../util/threads.js";
|
|
12
|
+
import { readISO } from "./gainmap.js";
|
|
12
13
|
const fourcc = (d, o) => String.fromCharCode(d[o], d[o + 1], d[o + 2], d[o + 3]);
|
|
13
14
|
function* boxes(d, start, end) {
|
|
14
15
|
const view = new DataView(d.buffer, d.byteOffset, d.byteLength);
|
|
@@ -472,7 +473,7 @@ async function toRgba(y, alpha, toneMapping, premultiplied = false, threads = nu
|
|
|
472
473
|
}
|
|
473
474
|
const MIN_THREADED_PIXELS = 4 << 20, PIXELS_PER_THREAD = 1 << 20;
|
|
474
475
|
const TILES_PER_THREAD = 4;
|
|
475
|
-
export async function decode(d, maxPixels = MAX_PIXELS, toneMapping = 'reinhard', threads = 1) {
|
|
476
|
+
export async function decode(d, maxPixels = MAX_PIXELS, toneMapping = 'reinhard', threads = 1, gainMap = false) {
|
|
476
477
|
if (!isAVIF(d))
|
|
477
478
|
throw new Error('AVIF: invalid header');
|
|
478
479
|
let meta, moov;
|
|
@@ -503,7 +504,10 @@ export async function decode(d, maxPixels = MAX_PIXELS, toneMapping = 'reinhard'
|
|
|
503
504
|
const n = Math.min(threads, Math.floor(pixels / PIXELS_PER_THREAD));
|
|
504
505
|
const pool = n > 1 && pixels >= MIN_THREADED_PIXELS && pixels <= maxPixels ? startThreads(n) : null;
|
|
505
506
|
try {
|
|
506
|
-
|
|
507
|
+
const image = await decodeImage(d, it, items, idat, alphaItem, alphaOf, maxPixels, toneMapping, pool);
|
|
508
|
+
if (gainMap)
|
|
509
|
+
image.gainMap = await decodeGainMap(d, primary, items, idat, maxPixels).catch(() => undefined);
|
|
510
|
+
return image;
|
|
507
511
|
}
|
|
508
512
|
finally {
|
|
509
513
|
if (pool)
|
|
@@ -531,7 +535,12 @@ async function decodeImage(d, it, items, idat, alphaItem, alphaOf, maxPixels, to
|
|
|
531
535
|
alpha = await decodeGrid(d, it, items, idat, maxPixels, threads, tiles.map(t => t.id));
|
|
532
536
|
}
|
|
533
537
|
let pixels = await toRgba(color, alpha, toneMapping, alphaItem && (it.refs.get('prem') ?? []).includes(alphaItem.id), threads);
|
|
534
|
-
|
|
538
|
+
const transformed = transform(d, it, color.width, color.height, pixels);
|
|
539
|
+
const { width, height } = transformed;
|
|
540
|
+
pixels = transformed.pixels;
|
|
541
|
+
return { width, height, pixels, icc, cicp: icc ? undefined : [color.primaries, color.transfer] };
|
|
542
|
+
}
|
|
543
|
+
function transform(d, it, width, height, pixels) {
|
|
535
544
|
for (const { box } of it.props) {
|
|
536
545
|
if (box.type === 'clap')
|
|
537
546
|
({ width, height, pixels } = clap(d, box, width, height, pixels));
|
|
@@ -540,7 +549,27 @@ async function decodeImage(d, it, items, idat, alphaItem, alphaOf, maxPixels, to
|
|
|
540
549
|
else if (box.type === 'imir')
|
|
541
550
|
pixels = mirror(pixels, width, height, d[box.start] & 1);
|
|
542
551
|
}
|
|
543
|
-
return { width, height, pixels
|
|
552
|
+
return { width, height, pixels };
|
|
553
|
+
}
|
|
554
|
+
async function decodeGainMap(d, primary, items, idat, maxPixels) {
|
|
555
|
+
for (const tmap of items.values()) {
|
|
556
|
+
const inputs = tmap.refs.get('dimg') ?? [];
|
|
557
|
+
if (tmap.type !== 'tmap' || inputs.length !== 2 || inputs[0] !== primary)
|
|
558
|
+
continue;
|
|
559
|
+
const data = itemData(d, tmap, idat);
|
|
560
|
+
if (data[0] !== 0)
|
|
561
|
+
return undefined;
|
|
562
|
+
const params = readISO(data.subarray(1));
|
|
563
|
+
const mapItem = items.get(inputs[1]);
|
|
564
|
+
if (!params || !mapItem)
|
|
565
|
+
return undefined;
|
|
566
|
+
const yuv = await decodeItem(d, mapItem, items, idat, maxPixels, null);
|
|
567
|
+
applyColr(d, mapItem, yuv);
|
|
568
|
+
yuv.transfer = 13;
|
|
569
|
+
const { width, height, pixels } = transform(d, mapItem, yuv.width, yuv.height, await toRgba(yuv, null, 'clamp'));
|
|
570
|
+
return { width, height, pixels, params };
|
|
571
|
+
}
|
|
572
|
+
return undefined;
|
|
544
573
|
}
|
|
545
574
|
function iccOf(d, it) {
|
|
546
575
|
for (const { box } of it.props) {
|