imagescript 3.6.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.
@@ -0,0 +1,46 @@
1
+ /*!
2
+ * The bit writer of the JPEG XL encoders, ported with the fast lossless encoder from libjxl
3
+ * (lib/jxl/enc_fast_lossless.cc). Copyright (c) the JPEG XL Project Authors, BSD-3-Clause License (see
4
+ * THIRD_PARTY_NOTICES.md)
5
+ */
6
+ export class BitWriter {
7
+ data = new Uint8Array(256);
8
+ length = 0;
9
+ buf = 0;
10
+ bits = 0;
11
+ write(count, v) {
12
+ if (count > 24) {
13
+ this.write(16, v & 0xffff);
14
+ this.write(count - 16, Math.floor(v / 65536));
15
+ return;
16
+ }
17
+ if (this.length + 4 > this.data.length) {
18
+ const data = new Uint8Array(this.data.length * 2);
19
+ data.set(this.data);
20
+ this.data = data;
21
+ }
22
+ this.buf |= v << this.bits;
23
+ this.bits += count;
24
+ while (this.bits >= 8) {
25
+ this.data[this.length++] = this.buf & 0xff;
26
+ this.buf >>>= 8;
27
+ this.bits -= 8;
28
+ }
29
+ }
30
+ zeroPadToByte() {
31
+ if (this.bits)
32
+ this.write(8 - this.bits, 0);
33
+ }
34
+ get size() {
35
+ return this.length * 8 + this.bits;
36
+ }
37
+ append(w) {
38
+ for (let i = 0; i < w.length; i++)
39
+ this.write(8, w.data[i]);
40
+ if (w.bits)
41
+ this.write(w.bits, w.buf);
42
+ }
43
+ bytes() {
44
+ return this.data.subarray(0, this.length);
45
+ }
46
+ }
@@ -9,6 +9,51 @@ const srgbEncode = (v) => {
9
9
  const x = Math.sqrt(v);
10
10
  return ((((P4 * x + P3) * x + P2) * x + P1) * x + P0) / ((((Q4 * x + Q3) * x + Q2) * x + Q1) * x + Q0);
11
11
  };
12
+ const THRESHOLDS = new Float64Array(257), LINEAR_BYTE = new Uint8Array(4097);
13
+ {
14
+ const f = (v) => byte(Math.fround(srgbEncode(v)));
15
+ THRESHOLDS[0] = -Infinity;
16
+ THRESHOLDS[256] = Infinity;
17
+ for (let k = 1; k < 256; k++) {
18
+ let lo = 0, hi = 1;
19
+ for (let i = 0; i < 80; i++) {
20
+ const mid = (lo + hi) / 2;
21
+ if (mid === lo || mid === hi)
22
+ break;
23
+ if (f(mid) >= k)
24
+ hi = mid;
25
+ else
26
+ lo = mid;
27
+ }
28
+ THRESHOLDS[k] = hi;
29
+ }
30
+ for (let i = 0, k = 0; i <= 4096; i++) {
31
+ while (k < 255 && THRESHOLDS[k + 1] <= i / 4096)
32
+ k++;
33
+ LINEAR_BYTE[i] = k;
34
+ }
35
+ }
36
+ const linearByte = (v) => {
37
+ if (!(v > 0))
38
+ return 0;
39
+ if (v >= 1)
40
+ return 255;
41
+ let k = LINEAR_BYTE[(v * 4096) | 0];
42
+ while (v >= THRESHOLDS[k + 1])
43
+ k++;
44
+ return k;
45
+ };
46
+ function xybToBytes(x, y, b, m, bias, k, out) {
47
+ const m0 = m[0], m1 = m[1], m2 = m[2], m3 = m[3], m4 = m[4], m5 = m[5], m6 = m[6], m7 = m[7], m8 = m[8];
48
+ const b0 = bias[0], b1 = bias[1], b2 = bias[2], c0 = Math.cbrt(b0), c1 = Math.cbrt(b1), c2 = Math.cbrt(b2);
49
+ for (let i = 0, o = 0; i < x.length; i++, o += 4) {
50
+ const gr = y[i] + x[i] - c0, gg = y[i] - x[i] - c1, gb = b[i] - c2;
51
+ const mr = gr * gr * gr + b0, mg = gg * gg * gg + b1, mb = gb * gb * gb + b2;
52
+ out[o] = linearByte((m0 * mr + m1 * mg + m2 * mb) * k);
53
+ out[o + 1] = linearByte((m3 * mr + m4 * mg + m5 * mb) * k);
54
+ out[o + 2] = linearByte((m6 * mr + m7 * mg + m8 * mb) * k);
55
+ }
56
+ }
12
57
  export class ColorOutput {
13
58
  image;
14
59
  toneMapping;
@@ -23,7 +68,7 @@ export class ColorOutput {
23
68
  this.hdrSignal = !image.xybEncoded && hdr;
24
69
  this.scale = this.linear ? image.toneMapping.intensityTarget / REFERENCE_WHITE : 1;
25
70
  }
26
- transform(p, ycbcr) {
71
+ transform(p, ycbcr, final = false) {
27
72
  const image = this.image, n = p.width * p.height, ch = p.channels.slice();
28
73
  if (ycbcr) {
29
74
  const [cb, y, cr] = ch, r = new Float32Array(n), g = new Float32Array(n), b = new Float32Array(n);
@@ -38,6 +83,11 @@ export class ColorOutput {
38
83
  else if (image.xybEncoded) {
39
84
  const [x, y, b] = ch, m = image.opsinInverse, bias = image.opsinBias, cbrt = bias.map(Math.cbrt);
40
85
  const k = 255 / image.toneMapping.intensityTarget * this.scale;
86
+ if (final && !this.linear) {
87
+ const rgba = new Uint8Array(4 * n);
88
+ xybToBytes(x, y, b, m, bias, k, rgba);
89
+ return { width: p.width, height: p.height, channels: ch.slice(3), rgba };
90
+ }
41
91
  const R = new Float32Array(n), G = new Float32Array(n), B = new Float32Array(n);
42
92
  for (let i = 0; i < n; i++) {
43
93
  const gr = y[i] + x[i] - cbrt[0], gg = y[i] - x[i] - cbrt[1], gb = b[i] - cbrt[2];
@@ -58,6 +108,16 @@ export class ColorOutput {
58
108
  }
59
109
  toRGBA(p) {
60
110
  const n = p.width * p.height, ch = p.channels, image = this.image;
111
+ if (p.rgba) {
112
+ const out = p.rgba, alpha = ch.find((_, i) => image.extraChannels[i].type === EXTRA_ALPHA);
113
+ if (alpha)
114
+ for (let i = 0, o = 3; i < n; i++, o += 4)
115
+ out[o] = byte(alpha[i]);
116
+ else
117
+ for (let o = 3; o < out.length; o += 4)
118
+ out[o] = 255;
119
+ return out;
120
+ }
61
121
  const numColor = ch.length - image.extraChannels.length, grey = numColor === 1;
62
122
  let [r, g, b] = grey ? [ch[0], ch[0], ch[0]] : ch;
63
123
  const black = image.extraChannels.findIndex(e => e.type === EXTRA_BLACK);
@@ -1,4 +1,4 @@
1
- import { idct16, idct32 } from "./idct.js";
1
+ import { idct16, idct32, idct64 } from "./idct.js";
2
2
  const halfSecants = new Map();
3
3
  function secHalf(n) {
4
4
  let t = halfSecants.get(n);
@@ -43,8 +43,8 @@ export function dct(io, o, n, scratch, s, inverse) {
43
43
  dct4(io, o, inverse);
44
44
  return;
45
45
  }
46
- if (inverse && (n === 16 || n === 32)) {
47
- (n === 16 ? idct16 : idct32)(io, o, 1);
46
+ if (inverse && (n === 16 || n === 32 || n === 64)) {
47
+ (n === 16 ? idct16 : n === 32 ? idct32 : idct64)(io, o, 1);
48
48
  return;
49
49
  }
50
50
  const half = n / 2, sec = secHalf(n);
@@ -103,8 +103,8 @@ function idct8(buf, o, step) {
103
103
  buf[o + 4 * step] = a3 - b3;
104
104
  }
105
105
  function dctRow(buf, o, n, inverse) {
106
- if (inverse && n <= 32 && n >= 8) {
107
- (n === 8 ? idct8 : n === 16 ? idct16 : idct32)(buf, o, 1);
106
+ if (inverse && n <= 64 && n >= 8) {
107
+ (n === 8 ? idct8 : n === 16 ? idct16 : n === 32 ? idct32 : idct64)(buf, o, 1);
108
108
  return;
109
109
  }
110
110
  const row = work.row;
@@ -115,8 +115,8 @@ function dctRow(buf, o, n, inverse) {
115
115
  buf[o + x] = row[x];
116
116
  }
117
117
  function dctColumn(buf, o, stride, n, inverse) {
118
- if (inverse && n <= 32 && n >= 8) {
119
- (n === 8 ? idct8 : n === 16 ? idct16 : idct32)(buf, o, stride);
118
+ if (inverse && n <= 64 && n >= 8) {
119
+ (n === 8 ? idct8 : n === 16 ? idct16 : n === 32 ? idct32 : idct64)(buf, o, stride);
120
120
  return;
121
121
  }
122
122
  const row = work.row;