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
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { Image } from "../image.js";
|
|
2
|
+
export const LINEAR = new Float32Array(256).map((_, v) => (v /= 255) <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
|
|
3
|
+
export const encodeSRGB = (v) => 255 * (v <= 0.0031308 ? 12.92 * Math.max(0, v) : 1.055 * Math.min(1, v) ** (1 / 2.4) - 0.055);
|
|
4
|
+
const LUMA = [0.2126, 0.7152, 0.0722];
|
|
5
|
+
export function decodeGains(map) {
|
|
6
|
+
const { bitmap } = map.image;
|
|
7
|
+
const gains = new Float32Array(bitmap.length / 4 * 3);
|
|
8
|
+
const tables = [0, 1, 2].map(c => {
|
|
9
|
+
const min = map.gainMapMin[c], max = map.gainMapMax[c], gamma = map.gamma[c];
|
|
10
|
+
return new Float32Array(256).map((_, v) => min + (max - min) * (v / 255) ** (1 / gamma));
|
|
11
|
+
});
|
|
12
|
+
for (let i = 0, g = 0; i < bitmap.length; i += 4, g += 3) {
|
|
13
|
+
gains[g] = tables[0][bitmap[i]];
|
|
14
|
+
gains[g + 1] = tables[1][bitmap[i + 1]];
|
|
15
|
+
gains[g + 2] = tables[2][bitmap[i + 2]];
|
|
16
|
+
}
|
|
17
|
+
return gains;
|
|
18
|
+
}
|
|
19
|
+
export function encodeGains(map, gains, width, height, single) {
|
|
20
|
+
const min = map.gainMapMin.slice(), max = map.gainMapMax.slice();
|
|
21
|
+
for (let g = 0; g < gains.length; g += 3)
|
|
22
|
+
for (let c = 0; c < 3; c++) {
|
|
23
|
+
const v = gains[g + c];
|
|
24
|
+
if (v < min[c])
|
|
25
|
+
min[c] = v;
|
|
26
|
+
if (v > max[c])
|
|
27
|
+
max[c] = v;
|
|
28
|
+
}
|
|
29
|
+
if (single) {
|
|
30
|
+
const lo = Math.min(...min), hi = Math.max(...max);
|
|
31
|
+
min.fill(lo);
|
|
32
|
+
max.fill(hi);
|
|
33
|
+
const gamma = map.gamma[0];
|
|
34
|
+
map.gamma = [gamma, gamma, gamma];
|
|
35
|
+
}
|
|
36
|
+
map.gainMapMin = min;
|
|
37
|
+
map.gainMapMax = max;
|
|
38
|
+
const image = map.image.width === width && map.image.height === height ? map.image : new Image(width, height);
|
|
39
|
+
const out = image.bitmap;
|
|
40
|
+
for (let i = 0, g = 0; i < out.length; i += 4, g += 3) {
|
|
41
|
+
for (let c = 0; c < 3; c++) {
|
|
42
|
+
const t = max[c] > min[c] ? (gains[g + c] - min[c]) / (max[c] - min[c]) : 0;
|
|
43
|
+
out[i + c] = 255 * Math.min(1, Math.max(0, t)) ** map.gamma[c];
|
|
44
|
+
}
|
|
45
|
+
out[i + 3] = 255;
|
|
46
|
+
}
|
|
47
|
+
map.image = image;
|
|
48
|
+
}
|
|
49
|
+
export function neutralMap(like, likeWidth, width, height) {
|
|
50
|
+
const scale = like.width / likeWidth;
|
|
51
|
+
const size = (n) => Math.max(1, Math.round(n * scale));
|
|
52
|
+
const map = like.clone();
|
|
53
|
+
const image = new Image(size(width), size(height));
|
|
54
|
+
const neutral = like.__neutral__();
|
|
55
|
+
const bitmap = image.bitmap;
|
|
56
|
+
for (let i = 0; i < bitmap.length; i += 4) {
|
|
57
|
+
bitmap[i] = neutral[0];
|
|
58
|
+
bitmap[i + 1] = neutral[1];
|
|
59
|
+
bitmap[i + 2] = neutral[2];
|
|
60
|
+
bitmap[i + 3] = 255;
|
|
61
|
+
}
|
|
62
|
+
map.image = image;
|
|
63
|
+
map.gainMapMin = map.gainMapMin.map(v => Math.min(v, 0));
|
|
64
|
+
map.gainMapMax = map.gainMapMax.map(v => Math.max(v, 0));
|
|
65
|
+
return map;
|
|
66
|
+
}
|
|
67
|
+
const sample = (gains, width, height, x, y, out) => {
|
|
68
|
+
const u = Math.min(width - 1, Math.max(0, x - 0.5)), v = Math.min(height - 1, Math.max(0, y - 0.5));
|
|
69
|
+
const x0 = Math.floor(u), y0 = Math.floor(v), x1 = Math.min(width - 1, x0 + 1), y1 = Math.min(height - 1, y0 + 1);
|
|
70
|
+
const fx = u - x0, fy = v - y0;
|
|
71
|
+
const a = 3 * (y0 * width + x0), b = 3 * (y0 * width + x1), c = 3 * (y1 * width + x0), d = 3 * (y1 * width + x1);
|
|
72
|
+
for (let k = 0; k < 3; k++)
|
|
73
|
+
out[k] = (gains[a + k] * (1 - fx) + gains[b + k] * fx) * (1 - fy) + (gains[c + k] * (1 - fx) + gains[d + k] * fx) * fy;
|
|
74
|
+
};
|
|
75
|
+
export function redraw(image, map, left, top, right, bottom, light, single = map.channels === 1, extraMax = 0) {
|
|
76
|
+
const { width, height, bitmap } = image;
|
|
77
|
+
if (right <= left || bottom <= top)
|
|
78
|
+
return;
|
|
79
|
+
const mapWidth = map.width, mapHeight = map.height;
|
|
80
|
+
const sx = mapWidth / width, sy = mapHeight / height;
|
|
81
|
+
const gains = decodeGains(map);
|
|
82
|
+
const oS = map.offsetSDR, oH = map.offsetHDR;
|
|
83
|
+
const lo = Math.min(0, ...map.gainMapMin), hi = Math.max(0, ...map.gainMapMax, extraMax);
|
|
84
|
+
const S = [0, 0, 0], H = [0, 0, 0], sumS = [0, 0, 0], sumH = [0, 0, 0];
|
|
85
|
+
const i0 = Math.max(0, Math.floor(left * sx)), i1 = Math.min(mapWidth, Math.ceil(right * sx));
|
|
86
|
+
const j0 = Math.max(0, Math.floor(top * sy)), j1 = Math.min(mapHeight, Math.ceil(bottom * sy));
|
|
87
|
+
for (let j = j0; j < j1; j++) {
|
|
88
|
+
const y0 = Math.max(0, Math.floor(j / sy)), y1 = Math.min(height, Math.max(y0 + 1, Math.floor((j + 1) / sy)));
|
|
89
|
+
for (let i = i0; i < i1; i++) {
|
|
90
|
+
const x0 = Math.max(0, Math.floor(i / sx)), x1 = Math.min(width, Math.max(x0 + 1, Math.floor((i + 1) / sx)));
|
|
91
|
+
const g = 3 * (j * mapWidth + i);
|
|
92
|
+
const boost = [2 ** gains[g], 2 ** gains[g + 1], 2 ** gains[g + 2]];
|
|
93
|
+
sumS.fill(0);
|
|
94
|
+
sumH.fill(0);
|
|
95
|
+
for (let y = y0; y < y1; y++)
|
|
96
|
+
for (let x = x0, p = 4 * (y * width + x0); x < x1; x++, p += 4) {
|
|
97
|
+
for (let c = 0; c < 3; c++)
|
|
98
|
+
S[c] = LINEAR[bitmap[p + c]];
|
|
99
|
+
if (x >= left && x < right && y >= top && y < bottom)
|
|
100
|
+
light(x, y, S, boost, H);
|
|
101
|
+
else
|
|
102
|
+
for (let c = 0; c < 3; c++)
|
|
103
|
+
H[c] = (S[c] + oS[c]) * boost[c] - oH[c];
|
|
104
|
+
for (let c = 0; c < 3; c++) {
|
|
105
|
+
sumS[c] += S[c];
|
|
106
|
+
sumH[c] += H[c];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const n = (y1 - y0) * (x1 - x0);
|
|
110
|
+
if (single) {
|
|
111
|
+
let s = 0, h = 0;
|
|
112
|
+
for (let c = 0; c < 3; c++) {
|
|
113
|
+
s += LUMA[c] * (sumS[c] / n + oS[c]);
|
|
114
|
+
h += LUMA[c] * (sumH[c] / n + oH[c]);
|
|
115
|
+
}
|
|
116
|
+
gains[g] = gains[g + 1] = gains[g + 2] = ratio(h, s, lo, hi);
|
|
117
|
+
}
|
|
118
|
+
else
|
|
119
|
+
for (let c = 0; c < 3; c++)
|
|
120
|
+
gains[g + c] = ratio(sumH[c] / n + oH[c], sumS[c] / n + oS[c], lo, hi);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
encodeGains(map, gains, mapWidth, mapHeight, single);
|
|
124
|
+
}
|
|
125
|
+
const ratio = (h, s, lo, hi) => {
|
|
126
|
+
if (!(s > 1e-3))
|
|
127
|
+
return h > 1e-3 ? hi : 0;
|
|
128
|
+
const g = Math.log2(Math.max(h, 1e-6) / s);
|
|
129
|
+
return Number.isFinite(g) ? Math.min(hi + 4, Math.max(lo - 4, g)) : 0;
|
|
130
|
+
};
|
|
131
|
+
export function gainSampler(source) {
|
|
132
|
+
const map = source.gainMap;
|
|
133
|
+
if (!map)
|
|
134
|
+
return null;
|
|
135
|
+
const gains = decodeGains(map), width = map.width, height = map.height;
|
|
136
|
+
const sx = width / source.width, sy = height / source.height;
|
|
137
|
+
return (x, y, out) => sample(gains, width, height, (x + 0.5) * sx, (y + 0.5) * sy, out);
|
|
138
|
+
}
|
|
139
|
+
const encodeExtended = (v) => v <= 0.0031308 ? 12.92 * Math.max(0, v) : 1.055 * v ** (1 / 2.4) - 0.055;
|
|
140
|
+
const decodeExtended = (e) => e <= 0.04045 ? Math.max(0, e) / 12.92 : ((e + 0.055) / 1.055) ** 2.4;
|
|
141
|
+
export function recolor(map, before, apply, exact) {
|
|
142
|
+
const gains = decodeGains(map);
|
|
143
|
+
const b = before.bitmap;
|
|
144
|
+
const sdr = new Float32Array(gains.length);
|
|
145
|
+
if (exact)
|
|
146
|
+
for (let i = 0, g = 0; i < b.length; i += 4, g += 3)
|
|
147
|
+
for (let c = 0; c < 3; c++)
|
|
148
|
+
sdr[g + c] = decodeExtended(Math.min(1, exact(b[i + c] / 255)));
|
|
149
|
+
else {
|
|
150
|
+
const sdrImage = before.clone();
|
|
151
|
+
apply(sdrImage);
|
|
152
|
+
const a = sdrImage.bitmap;
|
|
153
|
+
for (let i = 0, g = 0; i < a.length; i += 4, g += 3)
|
|
154
|
+
for (let c = 0; c < 3; c++)
|
|
155
|
+
sdr[g + c] = LINEAR[a[i + c]];
|
|
156
|
+
}
|
|
157
|
+
const oS = map.offsetSDR, oH = map.offsetHDR;
|
|
158
|
+
const light = new Float32Array(gains.length), encoded = new Float32Array(gains.length), scale = new Float32Array(gains.length / 3);
|
|
159
|
+
let peak = 1;
|
|
160
|
+
for (let i = 0, g = 0; i < b.length; i += 4, g += 3) {
|
|
161
|
+
let m = 1;
|
|
162
|
+
for (let c = 0; c < 3; c++) {
|
|
163
|
+
const h = light[g + c] = Math.max(0, (LINEAR[b[i + c]] + oS[c]) * 2 ** gains[g + c] - oH[c]);
|
|
164
|
+
peak = Math.max(peak, h);
|
|
165
|
+
m = Math.max(m, encoded[g + c] = encodeExtended(h));
|
|
166
|
+
}
|
|
167
|
+
scale[g / 3] = m;
|
|
168
|
+
}
|
|
169
|
+
const changed = new Float32Array(gains.length);
|
|
170
|
+
if (exact)
|
|
171
|
+
for (let g = 0; g < gains.length; g++)
|
|
172
|
+
changed[g] = decodeExtended(exact(encoded[g]));
|
|
173
|
+
else {
|
|
174
|
+
const proxy = new Image(before.width, before.height), p = proxy.bitmap;
|
|
175
|
+
for (let i = 0, g = 0; i < p.length; i += 4, g += 3) {
|
|
176
|
+
for (let c = 0; c < 3; c++)
|
|
177
|
+
p[i + c] = Math.round(255 * encoded[g + c] / scale[g / 3]);
|
|
178
|
+
p[i + 3] = b[i + 3];
|
|
179
|
+
}
|
|
180
|
+
const original = p.slice();
|
|
181
|
+
apply(proxy);
|
|
182
|
+
for (let i = 0, g = 0; i < p.length; i += 4, g += 3) {
|
|
183
|
+
const m = scale[g / 3];
|
|
184
|
+
for (let c = 0; c < 3; c++) {
|
|
185
|
+
const was = original[i + c], is = p[i + c];
|
|
186
|
+
changed[g + c] = was === is ? light[g + c] : was >= 32 ? light[g + c] * decodeExtended(is * m / 255) / decodeExtended(was * m / 255) : decodeExtended(is * m / 255);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
let changedPeak = 1;
|
|
191
|
+
for (let g = 0; g < changed.length; g++)
|
|
192
|
+
changedPeak = Math.max(changedPeak, changed[g]);
|
|
193
|
+
const lo = Math.min(0, ...map.gainMapMin), hi = Math.max(Math.log2(Math.max(peak, changedPeak)), ...map.gainMapMax);
|
|
194
|
+
const single = map.channels === 1;
|
|
195
|
+
for (let g = 0; g < gains.length; g += 3) {
|
|
196
|
+
if (single) {
|
|
197
|
+
let s = 0, h = 0;
|
|
198
|
+
for (let c = 0; c < 3; c++) {
|
|
199
|
+
s += LUMA[c] * (sdr[g + c] + oS[c]);
|
|
200
|
+
h += LUMA[c] * (changed[g + c] + oH[c]);
|
|
201
|
+
}
|
|
202
|
+
gains[g] = gains[g + 1] = gains[g + 2] = ratio(h, s, lo, hi);
|
|
203
|
+
}
|
|
204
|
+
else
|
|
205
|
+
for (let c = 0; c < 3; c++)
|
|
206
|
+
gains[g + c] = ratio(changed[g + c] + oH[c], sdr[g + c] + oS[c], lo, hi);
|
|
207
|
+
}
|
|
208
|
+
encodeGains(map, gains, map.width, map.height, single);
|
|
209
|
+
}
|
package/dist/util/resample.js
CHANGED
|
@@ -57,44 +57,66 @@ function coefficients(inSize, outSize, filter, bits, pad = false) {
|
|
|
57
57
|
}
|
|
58
58
|
return { start, weights, taps };
|
|
59
59
|
}
|
|
60
|
+
function sliced(inSize, outSize, filter, bits, pad, from, count) {
|
|
61
|
+
const c = coefficients(inSize, outSize, filter, bits, pad);
|
|
62
|
+
if (from === 0 && count === outSize)
|
|
63
|
+
return c;
|
|
64
|
+
return { start: c.start.slice(from, from + count), weights: c.weights.slice(from * c.taps, (from + count) * c.taps), taps: c.taps };
|
|
65
|
+
}
|
|
66
|
+
function window(horizontal, region) {
|
|
67
|
+
if (!horizontal)
|
|
68
|
+
return { x0: region.x, x1: region.x + region.width };
|
|
69
|
+
const { start, taps } = horizontal, x0 = start[0], x1 = start[start.length - 1] + taps;
|
|
70
|
+
for (let i = 0; i < start.length; i++)
|
|
71
|
+
start[i] -= x0;
|
|
72
|
+
return { x0, x1 };
|
|
73
|
+
}
|
|
60
74
|
const FAST_TAPS = 4;
|
|
61
|
-
export function resample(src, width, height, outWidth, outHeight, filter, out, fast = false) {
|
|
75
|
+
export function resample(src, width, height, outWidth, outHeight, filter, out, fast = false, region = { x: 0, y: 0, width: outWidth, height: outHeight }) {
|
|
76
|
+
const partial = region.width !== outWidth || region.height !== outHeight;
|
|
77
|
+
const opaque = partial ? isOpaque(src) : undefined;
|
|
62
78
|
if (littleEndian && filter.support <= 1) {
|
|
63
|
-
if (fast && resampleFast(src, width, height, outWidth, outHeight, filter, out))
|
|
79
|
+
if (fast && resampleFast(src, width, height, outWidth, outHeight, filter, out, region, opaque))
|
|
64
80
|
return;
|
|
65
|
-
if (resampleOpaque(src, width, height, outWidth, outHeight, filter, out))
|
|
81
|
+
if (opaque !== false && resampleOpaque(src, width, height, outWidth, outHeight, filter, out, region))
|
|
66
82
|
return;
|
|
67
83
|
}
|
|
68
|
-
|
|
84
|
+
else if (opaque !== false && resampleOpaqueSigned(src, width, height, outWidth, outHeight, filter, out, region))
|
|
85
|
+
return;
|
|
86
|
+
resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out, region);
|
|
69
87
|
}
|
|
70
|
-
function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
88
|
+
function resampleOpaque(src, width, height, outWidth, outHeight, filter, out, region) {
|
|
71
89
|
const pixels = new Int32Array(src.buffer, src.byteOffset, width * height);
|
|
72
90
|
let all = -1;
|
|
73
91
|
for (let x = 0, last = (height - 1) * width; x < width; x++)
|
|
74
92
|
all &= pixels[x] & pixels[last + x];
|
|
75
93
|
if (all >>> 24 !== 255)
|
|
76
94
|
return false;
|
|
77
|
-
const
|
|
95
|
+
const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, BITS, false, region.x, region.width);
|
|
96
|
+
const { x0, x1 } = window(horizontal, region), cw = x1 - x0;
|
|
97
|
+
const rh = region.height;
|
|
98
|
+
const lanes = new Int32Array(cw * rh * 2);
|
|
78
99
|
if (outHeight === height) {
|
|
79
|
-
for (let
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
100
|
+
for (let y = 0, o = 0; y < rh; y++)
|
|
101
|
+
for (let i = (region.y + y) * width + x0, end = i + cw; i < end; i++, o += 2) {
|
|
102
|
+
const p = pixels[i];
|
|
103
|
+
all &= p;
|
|
104
|
+
lanes[o] = (p & 0xff00ff) << 8;
|
|
105
|
+
lanes[o + 1] = (p & 0xff00) | 0;
|
|
106
|
+
}
|
|
85
107
|
}
|
|
86
108
|
else {
|
|
87
|
-
let coefficientsV =
|
|
109
|
+
let coefficientsV = sliced(height, outHeight, filter, 8, false, region.y, rh);
|
|
88
110
|
const swar = coefficientsV.taps <= 4;
|
|
89
111
|
if (!swar)
|
|
90
|
-
coefficientsV =
|
|
112
|
+
coefficientsV = sliced(height, outHeight, filter, BITS, false, region.y, rh);
|
|
91
113
|
const { start, weights, taps } = coefficientsV;
|
|
92
|
-
for (let y = 0, o = 0; y <
|
|
93
|
-
const w = y * taps, r0 = start[y] * width;
|
|
114
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
115
|
+
const w = y * taps, r0 = start[y] * width + x0;
|
|
94
116
|
if (swar && taps === 4) {
|
|
95
117
|
const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
|
|
96
118
|
const r1 = r0 + width, r2 = r1 + width, r3 = r2 + width;
|
|
97
|
-
for (let x = 0; x <
|
|
119
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
98
120
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x], p3 = pixels[r3 + x];
|
|
99
121
|
all &= p0 & p1 & p2 & p3;
|
|
100
122
|
lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2) + Math.imul(p3 & 0xff00ff, w3);
|
|
@@ -103,7 +125,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
103
125
|
}
|
|
104
126
|
else if (swar && taps === 3) {
|
|
105
127
|
const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], r1 = r0 + width, r2 = r1 + width;
|
|
106
|
-
for (let x = 0; x <
|
|
128
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
107
129
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x];
|
|
108
130
|
all &= p0 & p1 & p2;
|
|
109
131
|
lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2);
|
|
@@ -112,7 +134,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
112
134
|
}
|
|
113
135
|
else if (swar && taps === 2) {
|
|
114
136
|
const w0 = weights[w], w1 = weights[w + 1], r1 = r0 + width;
|
|
115
|
-
for (let x = 0; x <
|
|
137
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
116
138
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x];
|
|
117
139
|
all &= p0 & p1;
|
|
118
140
|
lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1);
|
|
@@ -120,7 +142,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
120
142
|
}
|
|
121
143
|
}
|
|
122
144
|
else if (swar) {
|
|
123
|
-
for (let x = 0; x <
|
|
145
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
124
146
|
let rb = 0, g = 0;
|
|
125
147
|
for (let k = 0, s = r0 + x; k < taps; k++, s += width) {
|
|
126
148
|
const p = pixels[s], wk = weights[w + k];
|
|
@@ -133,7 +155,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
133
155
|
}
|
|
134
156
|
}
|
|
135
157
|
else {
|
|
136
|
-
for (let x = 0; x <
|
|
158
|
+
for (let x = 0; x < cw; x++, o += 2) {
|
|
137
159
|
let r = 0, g = 0, b = 0, k = 0, s = r0 + x;
|
|
138
160
|
for (; k + 1 < taps; k += 2, s += width * 2) {
|
|
139
161
|
const p = pixels[s], q = pixels[s + width], w0 = weights[w + k], w1 = weights[w + k + 1];
|
|
@@ -157,8 +179,8 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
157
179
|
}
|
|
158
180
|
if (all >>> 24 !== 255)
|
|
159
181
|
return false;
|
|
160
|
-
const result = new Int32Array(out.buffer, out.byteOffset,
|
|
161
|
-
if (
|
|
182
|
+
const result = new Int32Array(out.buffer, out.byteOffset, region.width * rh);
|
|
183
|
+
if (!horizontal) {
|
|
162
184
|
for (let i = 0, o = 0; i < result.length; i++, o += 2) {
|
|
163
185
|
const rb = lanes[o], g = lanes[o + 1];
|
|
164
186
|
result[i] = ((rb & 0xffff) + 128 >> 8) | (g + 128 >> 8) << 8 | ((rb >>> 16) + 128 >> 8) << 16 | 0xff000000;
|
|
@@ -166,11 +188,11 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
166
188
|
return true;
|
|
167
189
|
}
|
|
168
190
|
const R = 1 << 7 + BITS, S = 8 + BITS;
|
|
169
|
-
const { start, weights, taps } =
|
|
170
|
-
for (let y = 0, o = 0; y <
|
|
171
|
-
const row = y *
|
|
191
|
+
const { start, weights, taps } = horizontal, rw = region.width;
|
|
192
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
193
|
+
const row = y * cw * 2;
|
|
172
194
|
if (taps === 4) {
|
|
173
|
-
for (let x = 0; x <
|
|
195
|
+
for (let x = 0; x < rw; x++, o++) {
|
|
174
196
|
const s = row + start[x] * 2, w = x * 4;
|
|
175
197
|
const a0 = lanes[s], a1 = lanes[s + 2], a2 = lanes[s + 4], a3 = lanes[s + 6];
|
|
176
198
|
const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
|
|
@@ -181,7 +203,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
181
203
|
}
|
|
182
204
|
}
|
|
183
205
|
else if (taps === 2) {
|
|
184
|
-
for (let x = 0; x <
|
|
206
|
+
for (let x = 0; x < rw; x++, o++) {
|
|
185
207
|
const s = row + start[x] * 2, w = x * 2;
|
|
186
208
|
const a0 = lanes[s], a1 = lanes[s + 2], w0 = weights[w], w1 = weights[w + 1];
|
|
187
209
|
const r = (a0 & 0xffff) * w0 + (a1 & 0xffff) * w1;
|
|
@@ -191,7 +213,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
191
213
|
}
|
|
192
214
|
}
|
|
193
215
|
else {
|
|
194
|
-
for (let x = 0; x <
|
|
216
|
+
for (let x = 0; x < rw; x++, o++) {
|
|
195
217
|
let r = 0, g = 0, b = 0, k = 0, s = row + start[x] * 2;
|
|
196
218
|
const w = x * taps;
|
|
197
219
|
for (; k + 1 < taps; k += 2, s += 4) {
|
|
@@ -221,14 +243,14 @@ function packedSum(pixels, s, step, weights, w, taps) {
|
|
|
221
243
|
}
|
|
222
244
|
return (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
|
|
223
245
|
}
|
|
224
|
-
function verticalFast(pixels, rows, width,
|
|
246
|
+
function verticalFast(pixels, rows, width, x0, cw, { start, weights, taps }) {
|
|
225
247
|
let all = -1;
|
|
226
|
-
for (let y = 0, o = 0; y <
|
|
227
|
-
const w = y * taps, r0 = start[y] * width;
|
|
248
|
+
for (let y = 0, o = 0; y < start.length; y++) {
|
|
249
|
+
const w = y * taps, r0 = start[y] * width + x0;
|
|
228
250
|
if (taps === 4) {
|
|
229
251
|
const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
|
|
230
252
|
const r1 = r0 + width, r2 = r1 + width, r3 = r2 + width;
|
|
231
|
-
for (let x = 0; x <
|
|
253
|
+
for (let x = 0; x < cw; x++, o++) {
|
|
232
254
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x], p3 = pixels[r3 + x];
|
|
233
255
|
all &= p0 & p1 & p2 & p3;
|
|
234
256
|
const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2) + Math.imul(p3 & 0xff00ff, w3) + 0x800080;
|
|
@@ -238,7 +260,7 @@ function verticalFast(pixels, rows, width, outHeight, { start, weights, taps })
|
|
|
238
260
|
}
|
|
239
261
|
else if (taps === 2) {
|
|
240
262
|
const w0 = weights[w], w1 = weights[w + 1], r1 = r0 + width;
|
|
241
|
-
for (let x = 0; x <
|
|
263
|
+
for (let x = 0; x < cw; x++, o++) {
|
|
242
264
|
const p0 = pixels[r0 + x], p1 = pixels[r1 + x];
|
|
243
265
|
all &= p0 & p1;
|
|
244
266
|
const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + 0x800080;
|
|
@@ -247,7 +269,7 @@ function verticalFast(pixels, rows, width, outHeight, { start, weights, taps })
|
|
|
247
269
|
}
|
|
248
270
|
}
|
|
249
271
|
else {
|
|
250
|
-
for (let x = 0; x <
|
|
272
|
+
for (let x = 0; x < cw; x++, o++) {
|
|
251
273
|
for (let k = 0, s = r0 + x; k < taps; k++, s += width)
|
|
252
274
|
all &= pixels[s];
|
|
253
275
|
rows[o] = packedSum(pixels, r0 + x, width, weights, w, taps);
|
|
@@ -284,14 +306,17 @@ function horizontalFast(rows, result, width, outWidth, outHeight, { start, weigh
|
|
|
284
306
|
}
|
|
285
307
|
}
|
|
286
308
|
}
|
|
287
|
-
function resampleFast(src, width, height, outWidth, outHeight, filter, out) {
|
|
288
|
-
const
|
|
289
|
-
|
|
309
|
+
function resampleFast(src, width, height, outWidth, outHeight, filter, out, region, known) {
|
|
310
|
+
const { width: rw, height: rh } = region;
|
|
311
|
+
const vertical = sliced(height, outHeight, filter, 8, true, region.y, rh);
|
|
312
|
+
const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, 8, true, region.x, rw);
|
|
313
|
+
if (vertical.taps > FAST_TAPS || horizontal && horizontal.taps > FAST_TAPS)
|
|
290
314
|
return false;
|
|
315
|
+
const { x0, x1 } = window(horizontal, region), cw = x1 - x0;
|
|
291
316
|
let pixels = new Int32Array(src.buffer, src.byteOffset, width * height), all = -1;
|
|
292
317
|
for (let x = 0, last = (height - 1) * width; x < width; x++)
|
|
293
318
|
all &= pixels[x] & pixels[last + x];
|
|
294
|
-
let opaque = all >>> 24 === 255 && (outHeight !== height || isOpaque(src));
|
|
319
|
+
let opaque = known ?? (all >>> 24 === 255 && (outHeight !== height || isOpaque(src)));
|
|
295
320
|
const premultiplied = () => {
|
|
296
321
|
const bitmap = new Uint8ClampedArray(src.length);
|
|
297
322
|
premultiply8(src, bitmap);
|
|
@@ -301,38 +326,155 @@ function resampleFast(src, width, height, outWidth, outHeight, filter, out) {
|
|
|
301
326
|
pixels = premultiplied();
|
|
302
327
|
let rows = pixels;
|
|
303
328
|
if (outHeight !== height) {
|
|
304
|
-
rows = new Int32Array(
|
|
305
|
-
if (verticalFast(pixels, rows, width,
|
|
329
|
+
rows = new Int32Array(cw * rh);
|
|
330
|
+
if (verticalFast(pixels, rows, width, x0, cw, vertical) >>> 24 !== 255 && opaque) {
|
|
306
331
|
opaque = false;
|
|
307
|
-
verticalFast(premultiplied(), rows, width,
|
|
332
|
+
verticalFast(premultiplied(), rows, width, x0, cw, vertical);
|
|
308
333
|
}
|
|
309
334
|
}
|
|
310
|
-
|
|
311
|
-
|
|
335
|
+
else if (cw !== width || rh !== height) {
|
|
336
|
+
rows = new Int32Array(cw * rh);
|
|
337
|
+
for (let y = 0; y < rh; y++)
|
|
338
|
+
rows.set(pixels.subarray((region.y + y) * width + x0, (region.y + y) * width + x1), y * cw);
|
|
339
|
+
}
|
|
340
|
+
const result = new Int32Array(out.buffer, out.byteOffset, rw * rh);
|
|
341
|
+
if (!horizontal)
|
|
312
342
|
result.set(rows);
|
|
313
343
|
else
|
|
314
|
-
horizontalFast(rows, result,
|
|
344
|
+
horizontalFast(rows, result, cw, rw, rh, horizontal);
|
|
315
345
|
if (!opaque)
|
|
316
346
|
unpremultiply8(out);
|
|
317
347
|
return true;
|
|
318
348
|
}
|
|
319
|
-
function
|
|
320
|
-
const
|
|
349
|
+
function resampleOpaqueSigned(src, width, height, outWidth, outHeight, filter, out, region) {
|
|
350
|
+
const pixels = new Int32Array(src.buffer, src.byteOffset, width * height);
|
|
351
|
+
let all = -1;
|
|
352
|
+
for (let x = 0, last = (height - 1) * width; x < width; x++)
|
|
353
|
+
all &= pixels[x] & pixels[last + x];
|
|
354
|
+
if (all >>> 24 !== 255)
|
|
355
|
+
return false;
|
|
356
|
+
const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, BITS, false, region.x, region.width);
|
|
357
|
+
const { x0, x1 } = window(horizontal, region), cw = x1 - x0, rh = region.height;
|
|
358
|
+
const R = new Int32Array(cw * rh), G = new Int32Array(cw * rh), B = new Int32Array(cw * rh);
|
|
321
359
|
if (outHeight === height) {
|
|
322
|
-
for (let
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
360
|
+
for (let y = 0, o = 0; y < rh; y++)
|
|
361
|
+
for (let i = (region.y + y) * width + x0, end = i + cw; i < end; i++, o++) {
|
|
362
|
+
const p = pixels[i];
|
|
363
|
+
all &= p;
|
|
364
|
+
R[o] = (p & 255) * 255;
|
|
365
|
+
G[o] = (p >> 8 & 255) * 255;
|
|
366
|
+
B[o] = (p >> 16 & 255) * 255;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
const { start, weights, taps } = sliced(height, outHeight, filter, BITS, false, region.y, rh);
|
|
371
|
+
const H = ONE >> 1;
|
|
372
|
+
for (let y = 0, o = 0; y < rh; y++, o += cw) {
|
|
373
|
+
const w = y * taps;
|
|
374
|
+
for (let k = 0; k + 1 < taps; k += 2) {
|
|
375
|
+
const wa = weights[w + k], wb = weights[w + k + 1], ra = (start[y] + k) * width + x0, rb = ra + width;
|
|
376
|
+
if (k === 0) {
|
|
377
|
+
for (let x = 0; x < cw; x++) {
|
|
378
|
+
const p = pixels[ra + x], q = pixels[rb + x];
|
|
379
|
+
all &= p & q;
|
|
380
|
+
R[o + x] = (p & 255) * wa + (q & 255) * wb;
|
|
381
|
+
G[o + x] = (p >> 8 & 255) * wa + (q >> 8 & 255) * wb;
|
|
382
|
+
B[o + x] = (p >> 16 & 255) * wa + (q >> 16 & 255) * wb;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
for (let x = 0; x < cw; x++) {
|
|
387
|
+
const p = pixels[ra + x], q = pixels[rb + x];
|
|
388
|
+
all &= p & q;
|
|
389
|
+
R[o + x] += (p & 255) * wa + (q & 255) * wb;
|
|
390
|
+
G[o + x] += (p >> 8 & 255) * wa + (q >> 8 & 255) * wb;
|
|
391
|
+
B[o + x] += (p >> 16 & 255) * wa + (q >> 16 & 255) * wb;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (taps & 1) {
|
|
396
|
+
const k = taps - 1, wk = weights[w + k], r = (start[y] + k) * width + x0;
|
|
397
|
+
for (let x = 0; x < cw; x++) {
|
|
398
|
+
const p = pixels[r + x];
|
|
399
|
+
all &= p;
|
|
400
|
+
if (k === 0) {
|
|
401
|
+
R[o + x] = (p & 255) * wk;
|
|
402
|
+
G[o + x] = (p >> 8 & 255) * wk;
|
|
403
|
+
B[o + x] = (p >> 16 & 255) * wk;
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
R[o + x] += (p & 255) * wk;
|
|
407
|
+
G[o + x] += (p >> 8 & 255) * wk;
|
|
408
|
+
B[o + x] += (p >> 16 & 255) * wk;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
for (let x = o, end = o + cw; x < end; x++) {
|
|
413
|
+
R[x] = R[x] * 255 + H >> BITS;
|
|
414
|
+
G[x] = G[x] * 255 + H >> BITS;
|
|
415
|
+
B[x] = B[x] * 255 + H >> BITS;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (all >>> 24 !== 255)
|
|
420
|
+
return false;
|
|
421
|
+
const f = 255 / (65025 * ONE), rw = region.width;
|
|
422
|
+
const { start, weights, taps } = horizontal ?? { start: null, weights: null, taps: 0 };
|
|
423
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
424
|
+
const row = y * cw;
|
|
425
|
+
if (!start || !weights) {
|
|
426
|
+
for (let x = 0; x < rw; x++, o += 4) {
|
|
427
|
+
const s = row + x;
|
|
428
|
+
out[o] = R[s] * ONE * f;
|
|
429
|
+
out[o + 1] = G[s] * ONE * f;
|
|
430
|
+
out[o + 2] = B[s] * ONE * f;
|
|
431
|
+
out[o + 3] = 255;
|
|
432
|
+
}
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
for (let x = 0; x < rw; x++, o += 4) {
|
|
436
|
+
let r = 0, g = 0, b = 0, k = 0, s = row + start[x];
|
|
437
|
+
const w = x * taps;
|
|
438
|
+
for (; k + 1 < taps; k += 2, s += 2) {
|
|
439
|
+
const w0 = weights[w + k], w1 = weights[w + k + 1];
|
|
440
|
+
r += R[s] * w0 + R[s + 1] * w1;
|
|
441
|
+
g += G[s] * w0 + G[s + 1] * w1;
|
|
442
|
+
b += B[s] * w0 + B[s + 1] * w1;
|
|
443
|
+
}
|
|
444
|
+
if (k < taps) {
|
|
445
|
+
const w0 = weights[w + k];
|
|
446
|
+
r += R[s] * w0;
|
|
447
|
+
g += G[s] * w0;
|
|
448
|
+
b += B[s] * w0;
|
|
449
|
+
}
|
|
450
|
+
out[o] = r * f;
|
|
451
|
+
out[o + 1] = g * f;
|
|
452
|
+
out[o + 2] = b * f;
|
|
453
|
+
out[o + 3] = 255;
|
|
328
454
|
}
|
|
329
455
|
}
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
function resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out, region) {
|
|
459
|
+
const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, BITS, false, region.x, region.width);
|
|
460
|
+
const { x0, x1 } = window(horizontal, region), cw = x1 - x0, rh = region.height;
|
|
461
|
+
const plane = new Int32Array(cw * rh * 4);
|
|
462
|
+
if (outHeight === height) {
|
|
463
|
+
for (let y = 0, o = 0; y < rh; y++)
|
|
464
|
+
for (let s = ((region.y + y) * width + x0) * 4, end = s + cw * 4; s < end; s += 4, o += 4) {
|
|
465
|
+
const a = src[s + 3];
|
|
466
|
+
plane[o] = src[s] * a;
|
|
467
|
+
plane[o + 1] = src[s + 1] * a;
|
|
468
|
+
plane[o + 2] = src[s + 2] * a;
|
|
469
|
+
plane[o + 3] = a * 255;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
330
472
|
else {
|
|
331
|
-
const { start, weights, taps } =
|
|
473
|
+
const { start, weights, taps } = sliced(height, outHeight, filter, BITS, false, region.y, rh);
|
|
332
474
|
const stride = width * 4, H = ONE >> 1;
|
|
333
|
-
for (let y = 0, o = 0; y <
|
|
334
|
-
const w = y * taps, r0 = start[y] * stride;
|
|
335
|
-
for (let x = 0; x <
|
|
475
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
476
|
+
const w = y * taps, r0 = start[y] * stride + x0 * 4;
|
|
477
|
+
for (let x = 0; x < cw * 4; x += 4, o += 4) {
|
|
336
478
|
let r = 0, g = 0, b = 0, a = 0, k = 0, s = r0 + x;
|
|
337
479
|
for (; k + 1 < taps; k += 2, s += stride * 2) {
|
|
338
480
|
const t = s + stride, wa = weights[w + k] * src[s + 3], wb = weights[w + k + 1] * src[t + 3];
|
|
@@ -355,10 +497,10 @@ function resamplePremultiplied(src, width, height, outWidth, outHeight, filter,
|
|
|
355
497
|
}
|
|
356
498
|
}
|
|
357
499
|
}
|
|
358
|
-
const { start, weights, taps } =
|
|
359
|
-
for (let y = 0, o = 0; y <
|
|
360
|
-
const row = y *
|
|
361
|
-
for (let x = 0; x <
|
|
500
|
+
const { start, weights, taps } = horizontal ?? { start: null, weights: null, taps: 0 };
|
|
501
|
+
for (let y = 0, o = 0; y < rh; y++) {
|
|
502
|
+
const row = y * cw * 4;
|
|
503
|
+
for (let x = 0; x < region.width; x++, o += 4) {
|
|
362
504
|
let r = 0, g = 0, b = 0, a = 0;
|
|
363
505
|
if (!start || !weights) {
|
|
364
506
|
const s = row + x * 4;
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '3.
|
|
1
|
+
export const VERSION = '3.16.0';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "imagescript",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.16.0",
|
|
4
4
|
"description": "Pure TypeScript image processing that runs everywhere",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -63,6 +63,9 @@
|
|
|
63
63
|
"av1",
|
|
64
64
|
"jxl",
|
|
65
65
|
"jpeg xl",
|
|
66
|
+
"hdr",
|
|
67
|
+
"ultra hdr",
|
|
68
|
+
"gain map",
|
|
66
69
|
"apng",
|
|
67
70
|
"tiff",
|
|
68
71
|
"tga",
|