imagescript 3.4.0 → 3.5.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.
@@ -5,10 +5,14 @@ export function gaborish(channels, w, h, weights) {
5
5
  const out = new Float32Array(w * h);
6
6
  for (let y = 0; y < h; y++) {
7
7
  const t = mirror(y - 1, h) * w, m = y * w, b = mirror(y + 1, h) * w;
8
- for (let x = 0; x < w; x++) {
9
- const l = mirror(x - 1, w), r = mirror(x + 1, w);
10
- out[m + x] = (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;
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
+ out[m] = at(0, mirror(-1, w), mirror(1, w));
10
+ 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;
11
13
  }
14
+ if (w > 1)
15
+ out[m + w - 1] = at(w - 1, w - 2, mirror(w, w));
12
16
  }
13
17
  return out;
14
18
  });
@@ -41,43 +45,73 @@ function outputRow(cur, out, y, w, sums, p0, pw, u, rows, xs, sigma, sigmaRow, s
41
45
  const [i0, i1, i2] = cur, [o0, o1, o2] = out, n = u.length / 2, yBorder = ((y + 1) & 6) === 0;
42
46
  const innerRow = rows[0] === (y - 2) * w && rows[4] === (y + 2) * w;
43
47
  const row = y * w;
44
- for (let x = 0; x < w; x++) {
45
- const i = row + x;
46
- const sg = sigma ? sigma[sigmaRow + (x >> 3)] : sigmaModular;
48
+ const sumOff = TAPS.subarray(0, n), srcOff = TAPS.subarray(8, 8 + n);
49
+ for (let k = 0; k < n; k++) {
50
+ sumOff[k] = u[2 * k + 1] * pw + u[2 * k];
51
+ srcOff[k] = u[2 * k + 1] * w + u[2 * k];
52
+ }
53
+ for (let x0 = 0; x0 < w; x0 += 8) {
54
+ const x1 = Math.min(x0 + 8, w);
55
+ const sg = sigma ? sigma[sigmaRow + (x0 >> 3)] : sigmaModular;
47
56
  if (sg < 0.3) {
48
- o0[i] = i0[i];
49
- o1[i] = i1[i];
50
- o2[i] = i2[i];
57
+ o0.set(i0.subarray(row + x0, row + x1), row + x0);
58
+ o1.set(i1.subarray(row + x0, row + x1), row + x0);
59
+ o2.set(i2.subarray(row + x0, row + x1), row + x0);
51
60
  continue;
52
61
  }
53
- const sm = yBorder || (x & 7) === 0 || (x & 7) === 7 ? stepMul * borderMul : stepMul;
54
- const negInvSigma = 6.6 * (Math.SQRT1_2 - 1) / sg * sm;
55
- let sumW = 1, sum0 = i0[i], sum1 = i1[i], sum2 = i2[i];
56
- const p = p0 + x, inner = innerRow && x >= 2 && x < w - 2;
57
- for (let k = 0; k < n; k++) {
58
- const ux = u[2 * k], uy = u[2 * k + 1], sum = sums[k];
59
- let weight = 1 + sum[p] * negInvSigma;
60
- if (weight > 0) {
61
- const t = inner ? i + uy * w + ux : rows[2 + uy] + xs[x + 5 + ux];
62
- sumW += weight;
63
- sum0 += weight * i0[t];
64
- sum1 += weight * i1[t];
65
- sum2 += weight * i2[t];
62
+ const borderInvSigma = 6.6 * (Math.SQRT1_2 - 1) / sg * (stepMul * borderMul), innerInvSigma = 6.6 * (Math.SQRT1_2 - 1) / sg * stepMul;
63
+ for (let x = x0; x < x1; x++) {
64
+ const i = row + x;
65
+ const negInvSigma = yBorder || (x & 7) === 0 || (x & 7) === 7 ? borderInvSigma : innerInvSigma;
66
+ let sumW = 1, sum0 = i0[i], sum1 = i1[i], sum2 = i2[i];
67
+ const p = p0 + x;
68
+ if (innerRow && x >= 2 && x < w - 2) {
69
+ for (let k = 0; k < n; k++) {
70
+ const sum = sums[k], so = sumOff[k], to = srcOff[k];
71
+ let weight = 1 + sum[p] * negInvSigma;
72
+ if (weight > 0) {
73
+ sumW += weight;
74
+ sum0 += weight * i0[i + to];
75
+ sum1 += weight * i1[i + to];
76
+ sum2 += weight * i2[i + to];
77
+ }
78
+ weight = 1 + sum[p - so] * negInvSigma;
79
+ if (weight > 0) {
80
+ sumW += weight;
81
+ sum0 += weight * i0[i - to];
82
+ sum1 += weight * i1[i - to];
83
+ sum2 += weight * i2[i - to];
84
+ }
85
+ }
66
86
  }
67
- weight = 1 + sum[p - uy * pw - ux] * negInvSigma;
68
- if (weight > 0) {
69
- const t = inner ? i - uy * w - ux : rows[2 - uy] + xs[x + 5 - ux];
70
- sumW += weight;
71
- sum0 += weight * i0[t];
72
- sum1 += weight * i1[t];
73
- sum2 += weight * i2[t];
87
+ else {
88
+ for (let k = 0; k < n; k++) {
89
+ const ux = u[2 * k], uy = u[2 * k + 1], sum = sums[k];
90
+ let weight = 1 + sum[p] * negInvSigma;
91
+ if (weight > 0) {
92
+ const t = rows[2 + uy] + xs[x + 5 + ux];
93
+ sumW += weight;
94
+ sum0 += weight * i0[t];
95
+ sum1 += weight * i1[t];
96
+ sum2 += weight * i2[t];
97
+ }
98
+ weight = 1 + sum[p - uy * pw - ux] * negInvSigma;
99
+ if (weight > 0) {
100
+ const t = rows[2 - uy] + xs[x + 5 - ux];
101
+ sumW += weight;
102
+ sum0 += weight * i0[t];
103
+ sum1 += weight * i1[t];
104
+ sum2 += weight * i2[t];
105
+ }
106
+ }
74
107
  }
108
+ o0[i] = sum0 / sumW;
109
+ o1[i] = sum1 / sumW;
110
+ o2[i] = sum2 / sumW;
75
111
  }
76
- o0[i] = sum0 / sumW;
77
- o1[i] = sum1 / sumW;
78
- o2[i] = sum2 / sumW;
79
112
  }
80
113
  }
114
+ const TAPS = new Int32Array(16);
81
115
  export function epf(channels, w, h, e, sigma, bw) {
82
116
  let cur = channels;
83
117
  const steps = e.iters === 3 ? [0, 1, 2] : e.iters === 2 ? [1, 2] : [1];
@@ -13,31 +13,43 @@ function mulShift24(a, b) {
13
13
  return Math.floor(a * b / 16777216);
14
14
  return Number((BigInt(a) * BigInt(b)) >> 24n);
15
15
  }
16
+ function weight(w, e, posN, posNE, posNW) {
17
+ const sum = (e[posN] + e[posNE] + e[posNW]) >>> 0;
18
+ const shift = sum === 0xffffffff ? 27 : Math.max(floorLog2(sum + 1) - 5, 0);
19
+ return 4 + ((w * DIV_LOOKUP[sum >>> shift]) >>> shift);
20
+ }
16
21
  class WeightedPredictor {
17
22
  header;
18
23
  xsize;
19
- predErrors;
24
+ e0;
25
+ e1;
26
+ e2;
27
+ e3;
20
28
  error;
21
- prediction = new Float64Array(4);
29
+ w0;
30
+ w1;
31
+ w2;
32
+ w3;
33
+ p0 = 0;
34
+ p1 = 0;
35
+ p2 = 0;
36
+ p3 = 0;
22
37
  pred = 0;
23
38
  maxError = 0;
24
39
  constructor(header, xsize) {
25
40
  this.header = header;
26
41
  this.xsize = xsize;
27
- this.predErrors = [0, 1, 2, 3].map(() => new Uint32Array((xsize + 2) * 2));
28
- this.error = new Int32Array((xsize + 2) * 2);
42
+ const n = (xsize + 2) * 2;
43
+ [this.e0, this.e1, this.e2, this.e3] = [0, 1, 2, 3].map(() => new Uint32Array(n));
44
+ this.error = new Int32Array(n);
45
+ [this.w0, this.w1, this.w2, this.w3] = header.w;
29
46
  }
30
47
  predict(x, y, N, W, NE, NW, NN) {
31
48
  const h = this.header, xsize = this.xsize;
32
49
  const cur = y & 1 ? 0 : xsize + 2, prev = y & 1 ? xsize + 2 : 0;
33
50
  const posN = prev + x, posNE = x < xsize - 1 ? posN + 1 : posN, posNW = x > 0 ? posN - 1 : posN;
34
- const weights = [0, 0, 0, 0];
35
- for (let i = 0; i < 4; i++) {
36
- const e = this.predErrors[i];
37
- const sum = (e[posN] + e[posNE] + e[posNW]) >>> 0;
38
- const shift = Math.max((sum === 0xffffffff ? 32 : floorLog2(sum + 1)) - 5, 0);
39
- weights[i] = 4 + ((h.w[i] * DIV_LOOKUP[Math.floor(sum / 2 ** shift)]) >>> shift);
40
- }
51
+ let w0 = weight(this.w0, this.e0, posN, posNE, posNW), w1 = weight(this.w1, this.e1, posN, posNE, posNW);
52
+ let w2 = weight(this.w2, this.e2, posN, posNE, posNW), w3 = weight(this.w3, this.e3, posN, posNE, posNW);
41
53
  N *= 8;
42
54
  W *= 8;
43
55
  NE *= 8;
@@ -53,35 +65,41 @@ class WeightedPredictor {
53
65
  if (Math.abs(teNE) > Math.abs(p))
54
66
  p = teNE;
55
67
  this.maxError = p;
56
- const sumWN = teN + teW, pr = this.prediction;
57
- pr[0] = W + NE - N;
58
- pr[1] = N - Math.floor((sumWN + teNE) * h.p1 / 32);
59
- pr[2] = W - Math.floor((sumWN + teNW) * h.p2 / 32);
60
- pr[3] = N - Math.floor((teNW * h.p3a + teN * h.p3b + teNE * h.p3c + (NN - N) * h.p3d + (NW - W) * h.p3e) / 32);
61
- let weightSum = weights[0] + weights[1] + weights[2] + weights[3];
62
- const logWeight = floorLog2(weightSum) - 4;
63
- weightSum = 0;
64
- for (let i = 0; i < 4; i++)
65
- weightSum += weights[i] >>= logWeight;
66
- let sum = (weightSum >> 1) - 1;
67
- for (let i = 0; i < 4; i++)
68
- sum += pr[i] * weights[i];
68
+ const sumWN = teN + teW;
69
+ const p0 = this.p0 = W + NE - N;
70
+ const p1 = this.p1 = N - Math.floor((sumWN + teNE) * h.p1 / 32);
71
+ const p2 = this.p2 = W - Math.floor((sumWN + teNW) * h.p2 / 32);
72
+ const p3 = this.p3 = N - Math.floor((teNW * h.p3a + teN * h.p3b + teNE * h.p3c + (NN - N) * h.p3d + (NW - W) * h.p3e) / 32);
73
+ const logWeight = floorLog2(w0 + w1 + w2 + w3) - 4;
74
+ w0 >>= logWeight;
75
+ w1 >>= logWeight;
76
+ w2 >>= logWeight;
77
+ w3 >>= logWeight;
78
+ const weightSum = w0 + w1 + w2 + w3;
79
+ const sum = (weightSum >> 1) - 1 + p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3;
69
80
  let pred = mulShift24(sum, DIV_LOOKUP[weightSum - 1]);
70
- if (((teN ^ teW) | (teN ^ teNW)) <= 0)
71
- pred = Math.min(Math.max(pred, Math.min(W, NE, N)), Math.max(W, NE, N));
81
+ if (((teN ^ teW) | (teN ^ teNW)) <= 0) {
82
+ const lo = W < NE ? (W < N ? W : N) : (NE < N ? NE : N), hi = W > NE ? (W > N ? W : N) : (NE > N ? NE : N);
83
+ pred = pred < lo ? lo : pred > hi ? hi : pred;
84
+ }
72
85
  this.pred = pred;
73
86
  return Math.floor((pred + 3) / 8);
74
87
  }
75
88
  update(value, x, y) {
76
- const xsize = this.xsize, cur = y & 1 ? 0 : xsize + 2, prev = y & 1 ? xsize + 2 : 0;
89
+ const xsize = this.xsize, cur = y & 1 ? 0 : xsize + 2, i = cur + x, j = (y & 1 ? xsize + 2 : 0) + x + 1;
77
90
  const val = value * 8;
78
- this.error[cur + x] = this.pred - val;
79
- for (let i = 0; i < 4; i++) {
80
- const e = Math.floor((Math.abs(this.prediction[i] - val) + 3) / 8) >>> 0;
81
- const errors = this.predErrors[i];
82
- errors[cur + x] = e;
83
- errors[prev + x + 1] = (errors[prev + x + 1] + e) >>> 0;
84
- }
91
+ this.error[i] = this.pred - val;
92
+ const e0 = Math.floor((Math.abs(this.p0 - val) + 3) / 8) >>> 0, e1 = Math.floor((Math.abs(this.p1 - val) + 3) / 8) >>> 0;
93
+ const e2 = Math.floor((Math.abs(this.p2 - val) + 3) / 8) >>> 0, e3 = Math.floor((Math.abs(this.p3 - val) + 3) / 8) >>> 0;
94
+ const E0 = this.e0, E1 = this.e1, E2 = this.e2, E3 = this.e3;
95
+ E0[i] = e0;
96
+ E0[j] = (E0[j] + e0) >>> 0;
97
+ E1[i] = e1;
98
+ E1[j] = (E1[j] + e1) >>> 0;
99
+ E2[i] = e2;
100
+ E2[j] = (E2[j] + e2) >>> 0;
101
+ E3[i] = e3;
102
+ E3[j] = (E3[j] + e3) >>> 0;
85
103
  }
86
104
  }
87
105
  export const clampedGradient = (n, w, l) => {
@@ -591,6 +609,21 @@ export function decodeModular(r, image, stream, global, maxChanSize, undo) {
591
609
  if (undo)
592
610
  image.undo();
593
611
  }
612
+ function decodeZero(r, dec, cluster, data, mult, offset, distMultiplier) {
613
+ for (let i = 0; i < data.length; i++)
614
+ data[i] = (Math.imul(unpackSigned(dec.readClustered(r, cluster, distMultiplier)), mult) + offset) | 0;
615
+ }
616
+ function decodeGradient(r, dec, cluster, data, w, h, distMultiplier) {
617
+ for (let x = 0; x < w; x++)
618
+ data[x] = (unpackSigned(dec.readClustered(r, cluster, distMultiplier)) + (x ? data[x - 1] : 0)) | 0;
619
+ for (let y = 1, i = w; y < h; y++) {
620
+ data[i] = (unpackSigned(dec.readClustered(r, cluster, distMultiplier)) + data[i - w]) | 0;
621
+ i++;
622
+ for (let x = 1; x < w; x++, i++) {
623
+ data[i] = (unpackSigned(dec.readClustered(r, cluster, distMultiplier)) + clampedGradient(data[i - w], data[i - 1], data[i - w - 1])) | 0;
624
+ }
625
+ }
626
+ }
594
627
  function decodeChannel(r, dec, globalTree, image, chan, stream, distMultiplier) {
595
628
  const c = image.channels[chan], { w, h } = c, data = c.data;
596
629
  const t = specialize(globalTree, chan, stream);
@@ -614,17 +647,11 @@ function decodeChannel(r, dec, globalTree, image, chan, stream, distMultiplier)
614
647
  data.fill((Math.imul(unpackSigned(single), mult) + offset) | 0);
615
648
  return;
616
649
  }
617
- for (let i = 0; i < w * h; i++)
618
- data[i] = (Math.imul(unpackSigned(dec.readClustered(r, cluster, distMultiplier)), mult) + offset) | 0;
650
+ decodeZero(r, dec, cluster, data, mult, offset, distMultiplier);
619
651
  return;
620
652
  }
621
653
  if (pred === 5 && mult === 1 && offset === 0) {
622
- for (let y = 0, i = 0; y < h; y++) {
623
- for (let x = 0; x < w; x++, i++) {
624
- const left = x ? data[i - 1] : y ? data[i - w] : 0, top = y ? data[i - w] : left, topleft = x && y ? data[i - w - 1] : left;
625
- data[i] = (unpackSigned(dec.readClustered(r, cluster, distMultiplier)) + clampedGradient(top, left, topleft)) | 0;
626
- }
627
- }
654
+ decodeGradient(r, dec, cluster, data, w, h, distMultiplier);
628
655
  return;
629
656
  }
630
657
  }
@@ -4,6 +4,7 @@ import { imageHeader, skipICC } from "./jxl/headers.js";
4
4
  import { frameHeader, toc } from "./jxl/frame-header.js";
5
5
  import { decodeFrame } from "./jxl/frame.js";
6
6
  import { ColorOutput } from "./jxl/color.js";
7
+ export { encodeRGBA as encode } from "./jxl/fast-lossless.js";
7
8
  const CONTAINER = [0, 0, 0, 12, 0x4a, 0x58, 0x4c, 0x20, 0x0d, 0x0a, 0x87, 0x0a];
8
9
  export function isJXL(d) {
9
10
  if (d.length >= 2 && d[0] === 0xff && d[1] === 0x0a)
package/dist/image.d.ts CHANGED
@@ -132,6 +132,17 @@ export interface DecodeOptions {
132
132
  */
133
133
  toneMapping?: ToneMapping;
134
134
  }
135
+ /** Options of {@link Image.encodeAVIF} */
136
+ export interface AVIFEncodeOptions {
137
+ /** 0-100, like avifenc's: higher is better and larger, 100 is lossless. Default: `60` */
138
+ quality?: number;
139
+ /** The quality of the alpha channel (if the image has transparency), like avifenc's. Default: `100` (lossless) */
140
+ alphaQuality?: number;
141
+ /** Chroma subsampling: `'4:4:4'` keeps the full color resolution, `'4:2:0'` halves it (smaller). Default: `'4:4:4'` */
142
+ subsampling?: '4:4:4' | '4:2:0';
143
+ /** How hard the encoder searches for the best modes: 0-2, faster to smaller. Default: `1` */
144
+ effort?: number;
145
+ }
135
146
  /** Text style options of {@link Image.renderText} */
136
147
  export interface TextStyle {
137
148
  /** Width of an outline around the glyphs in pixels. Default: `0` */
@@ -754,6 +765,17 @@ export declare class Image {
754
765
  * Encodes the image as QOI (3 channels if the image is opaque)
755
766
  */
756
767
  encodeQOI(): Promise<Uint8Array>;
768
+ /**
769
+ * Encodes the image as AVIF (AV1, with an alpha image if the image has transparency)
770
+ * @param options See {@link AVIFEncodeOptions}
771
+ */
772
+ encodeAVIF({ quality, alphaQuality, subsampling, effort }?: AVIFEncodeOptions): Promise<Uint8Array>;
773
+ /**
774
+ * Encodes the image as lossless JPEG XL, byte for byte like libjxl's `cjxl -e 1` on CPUs with AVX2 or NEON (its
775
+ * fast lossless mode: the gradient predictor, a palette if the image has at most 511 colors besides black, else
776
+ * YCoCg); greyscale if the image is, with alpha if it has transparency
777
+ */
778
+ encodeJXL(): Promise<Uint8Array>;
757
779
  /**
758
780
  * Encodes the image as the smallest fitting Netpbm format: PBM for black and white images, PGM for greyscale ones,
759
781
  * PPM for others, PAM if the image has transparency
package/dist/image.js CHANGED
@@ -9,6 +9,7 @@ import * as qoi from "./codecs/qoi.js";
9
9
  import * as netpbm from "./codecs/netpbm.js";
10
10
  import * as hdr from "./codecs/hdr.js";
11
11
  import * as avif from "./codecs/avif.js";
12
+ import * as avifEnc from "./codecs/avif-enc.js";
12
13
  import * as jxl from "./codecs/jxl.js";
13
14
  import * as quantize from "./codecs/quantize.js";
14
15
  import * as textRenderer from "./render/text.js";
@@ -1024,6 +1025,12 @@ export class Image {
1024
1025
  async encodeQOI() {
1025
1026
  return qoi.encode(this.__bytes__(), this.width, this.height);
1026
1027
  }
1028
+ async encodeAVIF({ quality = 60, alphaQuality = 100, subsampling = '4:4:4', effort = 1 } = {}) {
1029
+ return avifEnc.encode(this.__bytes__(), this.width, this.height, { quality: finite(quality, 'quality'), alphaQuality: finite(alphaQuality, 'alphaQuality'), subsampling, effort });
1030
+ }
1031
+ async encodeJXL() {
1032
+ return jxl.encode(this.__bytes__(), this.width, this.height);
1033
+ }
1027
1034
  async encodePNM() {
1028
1035
  return netpbm.encode(this.__bytes__(), this.width, this.height);
1029
1036
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.4.0';
1
+ export const VERSION = '3.5.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "zero-dependency javascript image manipulation",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",