imagescript 3.5.0 → 3.7.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.
@@ -7,9 +7,16 @@ export function gaborish(channels, w, h, weights) {
7
7
  const t = mirror(y - 1, h) * w, m = y * w, b = mirror(y + 1, h) * w;
8
8
  const at = (x, l, r) => (src[m + x] + (src[t + x] + src[b + x] + src[m + l] + src[m + r]) * w0 + (src[t + l] + src[t + r] + src[b + l] + src[b + r]) * w1) * norm;
9
9
  out[m] = at(0, mirror(-1, w), mirror(1, w));
10
+ let tl = src[t], tc = src[t + 1], ml = src[m], mc = src[m + 1], bl = src[b], bc = src[b + 1];
10
11
  for (let x = 1; x < w - 1; x++) {
11
- out[m + x] = (src[m + x] + (src[t + x] + src[b + x] + src[m + x - 1] + src[m + x + 1]) * w0 +
12
- (src[t + x - 1] + src[t + x + 1] + src[b + x - 1] + src[b + x + 1]) * w1) * norm;
12
+ const tr = src[t + x + 1], mr = src[m + x + 1], br = src[b + x + 1];
13
+ out[m + x] = (mc + (tc + bc + ml + mr) * w0 + (tl + tr + bl + br) * w1) * norm;
14
+ tl = tc;
15
+ tc = tr;
16
+ ml = mc;
17
+ mc = mr;
18
+ bl = bc;
19
+ bc = br;
13
20
  }
14
21
  if (w > 1)
15
22
  out[m + w - 1] = at(w - 1, w - 2, mirror(w, w));
@@ -60,6 +67,46 @@ function outputRow(cur, out, y, w, sums, p0, pw, u, rows, xs, sigma, sigmaRow, s
60
67
  continue;
61
68
  }
62
69
  const borderInvSigma = 6.6 * (Math.SQRT1_2 - 1) / sg * (stepMul * borderMul), innerInvSigma = 6.6 * (Math.SQRT1_2 - 1) / sg * stepMul;
70
+ if (n === 2 && innerRow && x0 >= 2 && x1 <= w - 2) {
71
+ const sa = sums[0], sb = sums[1], soA = sumOff[0], soB = sumOff[1], toA = srcOff[0], toB = srcOff[1];
72
+ for (let x = x0; x < x1; x++) {
73
+ const i = row + x, p = p0 + x;
74
+ const negInvSigma = yBorder || (x & 7) === 0 || (x & 7) === 7 ? borderInvSigma : innerInvSigma;
75
+ let sumW = 1, sum0 = i0[i], sum1 = i1[i], sum2 = i2[i];
76
+ let weight = 1 + sa[p] * negInvSigma;
77
+ if (weight > 0) {
78
+ sumW += weight;
79
+ sum0 += weight * i0[i + toA];
80
+ sum1 += weight * i1[i + toA];
81
+ sum2 += weight * i2[i + toA];
82
+ }
83
+ weight = 1 + sa[p - soA] * negInvSigma;
84
+ if (weight > 0) {
85
+ sumW += weight;
86
+ sum0 += weight * i0[i - toA];
87
+ sum1 += weight * i1[i - toA];
88
+ sum2 += weight * i2[i - toA];
89
+ }
90
+ weight = 1 + sb[p] * negInvSigma;
91
+ if (weight > 0) {
92
+ sumW += weight;
93
+ sum0 += weight * i0[i + toB];
94
+ sum1 += weight * i1[i + toB];
95
+ sum2 += weight * i2[i + toB];
96
+ }
97
+ weight = 1 + sb[p - soB] * negInvSigma;
98
+ if (weight > 0) {
99
+ sumW += weight;
100
+ sum0 += weight * i0[i - toB];
101
+ sum1 += weight * i1[i - toB];
102
+ sum2 += weight * i2[i - toB];
103
+ }
104
+ o0[i] = sum0 / sumW;
105
+ o1[i] = sum1 / sumW;
106
+ o2[i] = sum2 / sumW;
107
+ }
108
+ continue;
109
+ }
63
110
  for (let x = x0; x < x1; x++) {
64
111
  const i = row + x;
65
112
  const negInvSigma = yBorder || (x & 7) === 0 || (x & 7) === 7 ? borderInvSigma : innerInvSigma;
@@ -1,4 +1,5 @@
1
1
  import { BitReader } from "./bits.js";
2
+ import { EXTRA_ALPHA } from "./headers.js";
2
3
  import { frameHeader, toc, passShifts, FRAME_LF, FRAME_REGULAR, FRAME_SKIP_PROGRESSIVE, BLEND_REPLACE, BLEND_ADD, BLEND_BLEND, BLEND_MULADD, BLEND_MUL, FLAG_PATCHES, FLAG_SPLINES, FLAG_NOISE, FLAG_USE_LF_FRAME } from "./frame-header.js";
3
4
  import { ModularImage, channel, readTree, decodeModular } from "./modular.js";
4
5
  import { upsample } from "./upsample.js";
@@ -269,8 +270,10 @@ function render(p, f, state) {
269
270
  state.references[f.saveAsReference] = planes;
270
271
  state.referenceIsXYB[f.saveAsReference] = image.xybEncoded;
271
272
  }
272
- planes = state.output.transform(planes, f.doYCbCr);
273
273
  const normal = f.type === FRAME_REGULAR || f.type === FRAME_SKIP_PROGRESSIVE;
274
+ const replaces = !f.haveCrop && f.blending.mode === BLEND_REPLACE && f.ecBlending.every(b => b.mode === BLEND_REPLACE) && p.width === image.width && p.height === image.height;
275
+ const final = normal && replaces && !canReference && image.extraChannels.every(e => e.type === EXTRA_ALPHA && !e.alphaAssociated);
276
+ planes = state.output.transform(planes, f.doYCbCr, final);
274
277
  const canvas = normal ? blend(planes, f, state) : planes;
275
278
  if (canReference && !f.saveBeforeCT) {
276
279
  state.references[f.saveAsReference] = canvas;
@@ -0,0 +1,60 @@
1
+ export function u32(w, value, dists) {
2
+ for (let s = 0; s < dists.length; s++) {
3
+ const [bits, offset] = dists[s];
4
+ if (value >= offset && value - offset < 2 ** bits) {
5
+ w.write(2, s);
6
+ w.write(bits, value - offset);
7
+ return;
8
+ }
9
+ }
10
+ throw new RangeError(`JPEG XL: ${value} out of range`);
11
+ }
12
+ const RATIOS = [[1, 1], [12, 10], [4, 3], [3, 2], [16, 9], [5, 4], [2, 1]];
13
+ export function imageHeader(w, width, height, numChans, xyb = false) {
14
+ w.write(16, 0x0aff);
15
+ let ratio = 0;
16
+ for (let r = 1; r < 8 && !ratio; r++)
17
+ if (width === Math.floor(height * RATIOS[r - 1][0] / RATIOS[r - 1][1]))
18
+ ratio = r;
19
+ const small = height <= 256 && height % 8 === 0 && (ratio !== 0 || (width <= 256 && width % 8 === 0));
20
+ const dists = [[9, 1], [13, 1], [18, 1], [30, 1]];
21
+ w.write(1, +small);
22
+ if (small)
23
+ w.write(5, height / 8 - 1);
24
+ else
25
+ u32(w, height, dists);
26
+ w.write(3, ratio);
27
+ if (!ratio) {
28
+ if (small)
29
+ w.write(5, width / 8 - 1);
30
+ else
31
+ u32(w, width, dists);
32
+ }
33
+ w.write(1, 0);
34
+ w.write(1, 0);
35
+ w.write(1, 0);
36
+ w.write(2, 0b00);
37
+ w.write(1, 1);
38
+ if (numChans === 2 || numChans === 4) {
39
+ w.write(2, 0b01);
40
+ w.write(1, 1);
41
+ }
42
+ else
43
+ w.write(2, 0b00);
44
+ w.write(1, xyb ? 1 : 0);
45
+ if (numChans > 2)
46
+ w.write(1, 1);
47
+ else {
48
+ w.write(1, 0);
49
+ w.write(1, 0);
50
+ w.write(2, 1);
51
+ w.write(2, 1);
52
+ w.write(1, 0);
53
+ w.write(2, 0b10);
54
+ w.write(4, 11);
55
+ w.write(2, 1);
56
+ }
57
+ w.write(2, 0b00);
58
+ w.write(1, 1);
59
+ w.zeroPadToByte();
60
+ }