imagescript 3.20.1 → 4.0.0-rc.1
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 +21 -12
- package/dist/animation-encoder.d.ts +1 -34
- package/dist/animation-encoder.js +24 -23
- package/dist/animation.d.ts +108 -122
- package/dist/animation.js +274 -107
- package/dist/codecs/gif.js +11 -9
- package/dist/codecs/hdr.d.ts +1 -1
- package/dist/codecs/jpeg.js +13 -13
- package/dist/codecs/jxl/headers.js +2 -2
- package/dist/codecs/jxl.js +5 -5
- package/dist/codecs/netpbm.js +7 -7
- package/dist/codecs/png.js +15 -15
- package/dist/codecs/quantize.js +34 -8
- package/dist/codecs/tiff.js +1 -1
- package/dist/codecs/webp.js +3 -3
- package/dist/colors.d.ts +8 -7
- package/dist/colors.js +13 -3
- package/dist/frame.d.ts +93 -102
- package/dist/frame.js +89 -62
- package/dist/gainmap.d.ts +88 -46
- package/dist/gainmap.js +73 -49
- package/dist/image-type.d.ts +6 -1
- package/dist/image-type.js +4 -1
- package/dist/image.d.ts +287 -732
- package/dist/image.js +255 -404
- package/dist/index.d.ts +15 -25
- package/dist/index.js +15 -14
- package/dist/render/svg.js +7 -2
- package/dist/text-layout.d.ts +5 -35
- package/dist/text-layout.js +9 -9
- package/dist/util/gain.js +12 -12
- package/dist/util/mem.d.ts +1 -1
- package/dist/util/options.js +20 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/dist/gif.d.ts +0 -36
- package/dist/gif.js +0 -22
- package/dist/util/rotate.js +0 -43
package/dist/gainmap.js
CHANGED
|
@@ -2,7 +2,7 @@ import { Image } from "./image.js";
|
|
|
2
2
|
import * as jpeg from "./codecs/jpeg.js";
|
|
3
3
|
import * as codec from "./codecs/gainmap.js";
|
|
4
4
|
import { view } from "./util/mem.js";
|
|
5
|
-
import { finite,
|
|
5
|
+
import { finite, checkOptions, quality as checkQuality } from "./util/options.js";
|
|
6
6
|
const perChannel = (value, name) => {
|
|
7
7
|
const values = typeof value === 'number' ? [value, value, value] : Array.from(value);
|
|
8
8
|
if (values.length === 1)
|
|
@@ -12,27 +12,58 @@ const perChannel = (value, name) => {
|
|
|
12
12
|
return values.map(v => finite(v, name));
|
|
13
13
|
};
|
|
14
14
|
export class GainMap {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
__min__;
|
|
16
|
+
__max__;
|
|
17
|
+
__gamma__;
|
|
18
|
+
__offsetSDR__;
|
|
19
|
+
__offsetHDR__;
|
|
20
|
+
__capacityMin__;
|
|
21
|
+
__capacityMax__;
|
|
22
|
+
__useBaseColorSpace__;
|
|
23
|
+
get min() { return [this.__min__[0], this.__min__[1], this.__min__[2]]; }
|
|
24
|
+
set min(value) { this.__min__ = perChannel(value, 'min'); }
|
|
25
|
+
get max() { return [this.__max__[0], this.__max__[1], this.__max__[2]]; }
|
|
26
|
+
set max(value) { this.__max__ = perChannel(value, 'max'); }
|
|
27
|
+
get gamma() { return [this.__gamma__[0], this.__gamma__[1], this.__gamma__[2]]; }
|
|
28
|
+
set gamma(value) {
|
|
29
|
+
const gamma = perChannel(value, 'gamma');
|
|
30
|
+
if (gamma.some(g => g <= 0))
|
|
31
|
+
throw new RangeError('The gain map\'s gamma must be above 0');
|
|
32
|
+
this.__gamma__ = gamma;
|
|
33
|
+
}
|
|
34
|
+
get offsetSDR() { return [this.__offsetSDR__[0], this.__offsetSDR__[1], this.__offsetSDR__[2]]; }
|
|
35
|
+
set offsetSDR(value) { this.__offsetSDR__ = perChannel(value, 'offsetSDR'); }
|
|
36
|
+
get offsetHDR() { return [this.__offsetHDR__[0], this.__offsetHDR__[1], this.__offsetHDR__[2]]; }
|
|
37
|
+
set offsetHDR(value) { this.__offsetHDR__ = perChannel(value, 'offsetHDR'); }
|
|
38
|
+
get hdrCapacityMin() { return this.__capacityMin__; }
|
|
39
|
+
set hdrCapacityMin(value) {
|
|
40
|
+
const capacity = finite(value, 'hdrCapacityMin');
|
|
41
|
+
if (capacity < 0)
|
|
42
|
+
throw new RangeError('The gain map\'s hdrCapacityMin can\'t be negative');
|
|
43
|
+
this.__capacityMin__ = capacity;
|
|
44
|
+
}
|
|
45
|
+
get hdrCapacityMax() { return this.__capacityMax__; }
|
|
46
|
+
set hdrCapacityMax(value) { this.__capacityMax__ = finite(value, 'hdrCapacityMax'); }
|
|
47
|
+
get useBaseColorSpace() { return this.__useBaseColorSpace__; }
|
|
48
|
+
set useBaseColorSpace(value) {
|
|
49
|
+
if (typeof value !== 'boolean')
|
|
50
|
+
throw new TypeError('useBaseColorSpace must be a boolean');
|
|
51
|
+
this.__useBaseColorSpace__ = value;
|
|
52
|
+
}
|
|
23
53
|
__pixels__ = null;
|
|
24
54
|
constructor(image, options = {}) {
|
|
55
|
+
checkOptions(options, 'new GainMap(image, { max: 2 })');
|
|
25
56
|
if (!(image instanceof Image))
|
|
26
57
|
throw new TypeError('The gain map has to be an Image');
|
|
27
|
-
const {
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
this.gamma =
|
|
31
|
-
this.offsetSDR =
|
|
32
|
-
this.offsetHDR =
|
|
33
|
-
this.hdrCapacityMin =
|
|
34
|
-
this.hdrCapacityMax =
|
|
35
|
-
this.useBaseColorSpace =
|
|
58
|
+
const { min = 0, max = 2, gamma = 1, offsetSDR = 1 / 64, offsetHDR = 1 / 64, hdrCapacityMin = 0, useBaseColorSpace = true } = options;
|
|
59
|
+
this.min = min;
|
|
60
|
+
this.max = max;
|
|
61
|
+
this.gamma = gamma;
|
|
62
|
+
this.offsetSDR = offsetSDR;
|
|
63
|
+
this.offsetHDR = offsetHDR;
|
|
64
|
+
this.hdrCapacityMin = hdrCapacityMin;
|
|
65
|
+
this.hdrCapacityMax = options.hdrCapacityMax ?? Math.max(...this.__max__);
|
|
66
|
+
this.useBaseColorSpace = useBaseColorSpace;
|
|
36
67
|
this.__pixels__ = image;
|
|
37
68
|
this.__check__();
|
|
38
69
|
}
|
|
@@ -52,9 +83,9 @@ export class GainMap {
|
|
|
52
83
|
}
|
|
53
84
|
return GainMap.__from_pixels__(image, params, false);
|
|
54
85
|
}
|
|
55
|
-
static __from_pixels__(image, params,
|
|
86
|
+
static __from_pixels__(image, params, gray = true) {
|
|
56
87
|
const px = image.bitmap;
|
|
57
|
-
if (
|
|
88
|
+
if (gray) {
|
|
58
89
|
let spread = 0;
|
|
59
90
|
for (let i = 0; i < px.length && spread <= 2; i += 4)
|
|
60
91
|
spread = Math.max(spread, Math.abs(px[i] - px[i + 1]), Math.abs(px[i] - px[i + 2]));
|
|
@@ -68,36 +99,28 @@ export class GainMap {
|
|
|
68
99
|
return gainMap;
|
|
69
100
|
}
|
|
70
101
|
__set_params__(p) {
|
|
71
|
-
this.
|
|
72
|
-
this.
|
|
73
|
-
this.
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
76
|
-
this.
|
|
77
|
-
this.
|
|
78
|
-
this.
|
|
102
|
+
this.__min__ = p.min.slice();
|
|
103
|
+
this.__max__ = p.max.slice();
|
|
104
|
+
this.__gamma__ = p.gamma.slice();
|
|
105
|
+
this.__offsetSDR__ = p.offsetSDR.slice();
|
|
106
|
+
this.__offsetHDR__ = p.offsetHDR.slice();
|
|
107
|
+
this.__capacityMin__ = p.capacityMin;
|
|
108
|
+
this.__capacityMax__ = p.capacityMax;
|
|
109
|
+
this.__useBaseColorSpace__ = p.useBaseColorSpace;
|
|
79
110
|
}
|
|
80
111
|
__params__() {
|
|
81
112
|
this.__check__();
|
|
82
113
|
return {
|
|
83
|
-
min: this.
|
|
84
|
-
offsetHDR: this.
|
|
114
|
+
min: this.__min__.slice(), max: this.__max__.slice(), gamma: this.__gamma__.slice(), offsetSDR: this.__offsetSDR__.slice(),
|
|
115
|
+
offsetHDR: this.__offsetHDR__.slice(), capacityMin: this.__capacityMin__, capacityMax: this.__capacityMax__, useBaseColorSpace: this.__useBaseColorSpace__
|
|
85
116
|
};
|
|
86
117
|
}
|
|
87
118
|
__check__() {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
for (let c = 0; c < 3; c++) {
|
|
94
|
-
if (p.gamma[c] <= 0)
|
|
95
|
-
throw new RangeError('The gain map\'s gamma must be above 0');
|
|
96
|
-
if (p.max[c] < p.min[c])
|
|
97
|
-
throw new RangeError('The gain map\'s gainMapMax must be at least its gainMapMin');
|
|
98
|
-
}
|
|
99
|
-
if (p.capacityMin < 0 || p.capacityMax <= p.capacityMin)
|
|
100
|
-
throw new RangeError('The gain map\'s hdrCapacityMax must be above its hdrCapacityMin, which can\'t be negative');
|
|
119
|
+
for (let c = 0; c < 3; c++)
|
|
120
|
+
if (this.__max__[c] < this.__min__[c])
|
|
121
|
+
throw new RangeError('The gain map\'s max must be at least its min');
|
|
122
|
+
if (this.__capacityMax__ <= this.__capacityMin__)
|
|
123
|
+
throw new RangeError('The gain map\'s hdrCapacityMax must be above its hdrCapacityMin');
|
|
101
124
|
}
|
|
102
125
|
get image() {
|
|
103
126
|
return this.__pixels__;
|
|
@@ -132,14 +155,14 @@ export class GainMap {
|
|
|
132
155
|
image = image.clone().resize(width, height);
|
|
133
156
|
const bitmap = image.bitmap;
|
|
134
157
|
const pixels = new Uint8Array(bitmap.buffer, bitmap.byteOffset, bitmap.byteLength);
|
|
135
|
-
const
|
|
136
|
-
return jpeg.encode(pixels, image.width, image.height, quality ?? 90, {
|
|
158
|
+
const gray = this.channels === 1;
|
|
159
|
+
return jpeg.encode(pixels, image.width, image.height, quality ?? 90, { gray, fullChroma: !gray });
|
|
137
160
|
}
|
|
138
161
|
__neutral__() {
|
|
139
162
|
return [0, 1, 2].map(c => {
|
|
140
|
-
const min = this.
|
|
163
|
+
const min = this.__min__[c], max = this.__max__[c];
|
|
141
164
|
const t = max > min ? Math.min(1, Math.max(0, -min / (max - min))) : 0;
|
|
142
|
-
return Math.round(255 * t ** this.
|
|
165
|
+
return Math.round(255 * t ** this.__gamma__[c]);
|
|
143
166
|
});
|
|
144
167
|
}
|
|
145
168
|
__rescale__(oldWidth, oldHeight, width, height) {
|
|
@@ -177,8 +200,9 @@ export class GainMap {
|
|
|
177
200
|
}
|
|
178
201
|
this.image = out;
|
|
179
202
|
}
|
|
180
|
-
async encode(
|
|
181
|
-
|
|
203
|
+
async encode(options) {
|
|
204
|
+
checkOptions(options, 'encode({ quality: 80 })');
|
|
205
|
+
let quality = options?.quality;
|
|
182
206
|
if (quality !== undefined)
|
|
183
207
|
quality = Math.max(1, checkQuality(quality));
|
|
184
208
|
return codec.mapWithMetadata(this.__body__(quality), this.__params__());
|
package/dist/image-type.d.ts
CHANGED
|
@@ -51,10 +51,15 @@ export declare class ImageType {
|
|
|
51
51
|
*/
|
|
52
52
|
static isBMP(data: BinaryData): boolean;
|
|
53
53
|
/**
|
|
54
|
-
* Whether the data is a Windows icon
|
|
54
|
+
* Whether the data is a Windows icon (ICO)
|
|
55
55
|
* @param data The file's data
|
|
56
56
|
*/
|
|
57
57
|
static isICO(data: BinaryData): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Whether the data is a Windows cursor (CUR)
|
|
60
|
+
* @param data The file's data
|
|
61
|
+
*/
|
|
62
|
+
static isCUR(data: BinaryData): boolean;
|
|
58
63
|
/**
|
|
59
64
|
* Whether the data starts like a QOI
|
|
60
65
|
* @param data The file's data
|
package/dist/image-type.js
CHANGED
|
@@ -91,7 +91,10 @@ export class ImageType {
|
|
|
91
91
|
return isBMP(u8(dataView(data)));
|
|
92
92
|
}
|
|
93
93
|
static isICO(data) {
|
|
94
|
-
return iconType(u8(dataView(data)))
|
|
94
|
+
return iconType(u8(dataView(data))) === 'ico';
|
|
95
|
+
}
|
|
96
|
+
static isCUR(data) {
|
|
97
|
+
return iconType(u8(dataView(data))) === 'cur';
|
|
95
98
|
}
|
|
96
99
|
static isQOI(data) {
|
|
97
100
|
return isQOI(u8(dataView(data)));
|