imagescript 4.2.0 → 4.2.2

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.
@@ -62,11 +62,14 @@ export function encodeFrame(frame, colors, transparent) {
62
62
  writeSubBlocks(out, lzwEncode(indices, minCodeSize));
63
63
  return out.finish();
64
64
  }
65
+ let dictionary;
66
+ const keys = new Int32Array(4096);
67
+ let used = 0;
65
68
  function lzwEncode(indices, minCodeSize) {
66
69
  const out = new GIFWriter(indices.length);
67
70
  const clear = 1 << minCodeSize;
68
71
  const eoi = clear + 1;
69
- const dictionary = new Int16Array(4096 * 256);
72
+ const table = dictionary ??= new Int16Array(4096 * 256);
70
73
  let codeSize = 0, next = 0, buffer = 0, bufferBits = 0;
71
74
  const emit = (code) => {
72
75
  buffer |= code << bufferBits;
@@ -78,7 +81,9 @@ function lzwEncode(indices, minCodeSize) {
78
81
  }
79
82
  };
80
83
  const reset = () => {
81
- dictionary.fill(0);
84
+ for (let i = 0; i < used; i++)
85
+ table[keys[i]] = 0;
86
+ used = 0;
82
87
  codeSize = minCodeSize + 1;
83
88
  next = eoi + 1;
84
89
  };
@@ -94,14 +99,15 @@ function lzwEncode(indices, minCodeSize) {
94
99
  for (let i = 1; i < indices.length; i++) {
95
100
  const index = indices[i];
96
101
  const key = prefix << 8 | index;
97
- const code = dictionary[key];
102
+ const code = table[key];
98
103
  if (code) {
99
104
  prefix = code;
100
105
  continue;
101
106
  }
102
107
  emit(prefix);
103
108
  if (next <= MAX_CODE) {
104
- dictionary[key] = next;
109
+ table[key] = next;
110
+ keys[used++] = key;
105
111
  if (next === 1 << codeSize && codeSize < 12)
106
112
  codeSize++;
107
113
  next++;
@@ -56,8 +56,7 @@ function textChunks(text) {
56
56
  }
57
57
  return chunks;
58
58
  }
59
- function filter(data, rowBytes, y0, y1, bpp) {
60
- const out = new Uint8Array((y1 - y0) * (rowBytes + 1));
59
+ function filter(data, rowBytes, y0, y1, bpp, out = new Uint8Array((y1 - y0) * (rowBytes + 1))) {
61
60
  for (let y = y0; y < y1; y++) {
62
61
  const row = y * rowBytes, o = (y - y0) * (rowBytes + 1) + 1;
63
62
  out[o - 1] = 4;
@@ -68,6 +67,10 @@ function filter(data, rowBytes, y0, y1, bpp) {
68
67
  }
69
68
  for (let i = 0; i < bpp; i++)
70
69
  out[o + i] = data[row + i] - data[row + i - rowBytes];
70
+ if (bpp === 3 || bpp === 4) {
71
+ paethRowFilter(data, row, rowBytes, out, o, bpp);
72
+ continue;
73
+ }
71
74
  for (let i = bpp; i < rowBytes; i++) {
72
75
  const a = data[row + i - bpp], b = data[row + i - rowBytes], c = data[row + i - rowBytes - bpp];
73
76
  const pa = Math.abs(b - c), pb = Math.abs(a - c), pc = Math.abs(a + b - 2 * c);
@@ -77,6 +80,50 @@ function filter(data, rowBytes, y0, y1, bpp) {
77
80
  }
78
81
  return out;
79
82
  }
83
+ function paethRowFilter(data, row, rowBytes, out, o, bpp) {
84
+ const up = row - rowBytes;
85
+ let a0 = data[row], a1 = data[row + 1], a2 = data[row + 2], a3 = bpp === 4 ? data[row + 3] : 0;
86
+ let c0 = data[up], c1 = data[up + 1], c2 = data[up + 2], c3 = bpp === 4 ? data[up + 3] : 0;
87
+ for (let i = bpp; i < rowBytes; i += bpp) {
88
+ let x = data[row + i], b = data[up + i], pa = Math.abs(b - c0), pb = Math.abs(a0 - c0), pc = Math.abs(a0 + b - 2 * c0);
89
+ let notA = (pb - pa | pc - pa) >> 31, takeC = (pc - pb) >> 31;
90
+ out[o + i] = x - (a0 ^ (a0 ^ b ^ (b ^ c0) & takeC) & notA);
91
+ a0 = x;
92
+ c0 = b;
93
+ x = data[row + i + 1];
94
+ b = data[up + i + 1];
95
+ pa = Math.abs(b - c1);
96
+ pb = Math.abs(a1 - c1);
97
+ pc = Math.abs(a1 + b - 2 * c1);
98
+ notA = (pb - pa | pc - pa) >> 31;
99
+ takeC = (pc - pb) >> 31;
100
+ out[o + i + 1] = x - (a1 ^ (a1 ^ b ^ (b ^ c1) & takeC) & notA);
101
+ a1 = x;
102
+ c1 = b;
103
+ x = data[row + i + 2];
104
+ b = data[up + i + 2];
105
+ pa = Math.abs(b - c2);
106
+ pb = Math.abs(a2 - c2);
107
+ pc = Math.abs(a2 + b - 2 * c2);
108
+ notA = (pb - pa | pc - pa) >> 31;
109
+ takeC = (pc - pb) >> 31;
110
+ out[o + i + 2] = x - (a2 ^ (a2 ^ b ^ (b ^ c2) & takeC) & notA);
111
+ a2 = x;
112
+ c2 = b;
113
+ if (bpp === 4) {
114
+ x = data[row + i + 3];
115
+ b = data[up + i + 3];
116
+ pa = Math.abs(b - c3);
117
+ pb = Math.abs(a3 - c3);
118
+ pc = Math.abs(a3 + b - 2 * c3);
119
+ notA = (pb - pa | pc - pa) >> 31;
120
+ takeC = (pc - pb) >> 31;
121
+ out[o + i + 3] = x - (a3 ^ (a3 ^ b ^ (b ^ c3) & takeC) & notA);
122
+ a3 = x;
123
+ c3 = b;
124
+ }
125
+ }
126
+ }
80
127
  function prefix(data, rowBytes, y0, y1) {
81
128
  const out = new Uint8Array((y1 - y0) * (rowBytes + 1));
82
129
  for (let y = y0; y < y1; y++)
@@ -99,7 +146,16 @@ async function compressRows(data, width, height, channels, depth, level, filterR
99
146
  filteredSamples.push(filter(data, rowBytes, y, end, bpp));
100
147
  }
101
148
  const [unfiltered, filtered] = await limited(2, threads, i => compressParallel(concat(i ? filteredSamples : unfilteredSamples), level, Math.max(1, Math.floor(threads / 2))));
102
- return compressParallel(unfiltered.length < filtered.length ? prefix(data, rowBytes, 0, height) : filter(data, rowBytes, 0, height, bpp), level, threads);
149
+ if (unfiltered.length < filtered.length)
150
+ return compressParallel(prefix(data, rowBytes, 0, height), level, threads);
151
+ const stride = rowBytes + 1, all = new Uint8Array(height * stride);
152
+ let rows = 0;
153
+ return compressParallel(all, level, threads, (_from, to) => {
154
+ const end = Math.min(height, Math.ceil(to / stride));
155
+ if (end > rows)
156
+ filter(data, rowBytes, rows, end, bpp, all.subarray(rows * stride, end * stride));
157
+ rows = Math.max(rows, end);
158
+ });
103
159
  }
104
160
  function chunk(type, body) {
105
161
  const out = new Uint8Array(body.length + 12);
@@ -536,6 +592,8 @@ function imagePixels(p, data, width, height) {
536
592
  const { passes, expected } = passesOf(p, width, height);
537
593
  if (data.length < expected)
538
594
  throw new Error('PNG: image data is truncated');
595
+ if (!interlace && colorType === 6 && depth === 8)
596
+ return unfilter(data, 0, height, width * 4, 4, true);
539
597
  const rgba = new Uint8Array(width * height * 4);
540
598
  const toRGBA = createConverter(colorType, depth, p.palette, p.transparency);
541
599
  let dataOffset = 0;
@@ -209,8 +209,29 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
209
209
  }
210
210
  const rowBytes = Math.ceil(width * samplesPerRow * bps / 8);
211
211
  const planeBytes = rowBytes * height;
212
- const raw = new Uint8Array(planeBytes * planes);
213
212
  const segmentRowBytes = Math.ceil(segmentWidth * samplesPerRow * bps / 8);
213
+ const sampleLittle = predictor === 3 ? true : little;
214
+ const common = !planar && (bps === 8 || bps === 16) && sampleFormat !== 2 && sampleFormat !== 3 && photometric <= 2;
215
+ const high = bps === 16 ? (sampleLittle ? 1 : 0) : -1;
216
+ if (common && !tiled) {
217
+ for (let s = 0; s < segmentsPerPlane; s++) {
218
+ const sy = s * segmentHeight, rows = Math.min(segmentHeight, height - sy);
219
+ if (rows <= 0)
220
+ break;
221
+ const expected = rows * rowBytes, stored = segment(s);
222
+ const inPlace = fillOrder === 2 || predictor === 2 || predictor === 3;
223
+ const bytes = compression === 1 && !inPlace && stored.length >= expected ? stored : await decompressSegment(stored, compression, expected);
224
+ if (fillOrder === 2)
225
+ reverseBits(bytes);
226
+ if (predictor === 2)
227
+ undoHorizontalPredictor(bytes, rowBytes, rows, samplesPerRow, bps, little);
228
+ else if (predictor === 3)
229
+ undoFloatingPointPredictor(bytes, rowBytes, rows, samplesPerRow, bps);
230
+ rowsToRGBA(bytes, width, rows, rowBytes, samplesPerPixel, high, photometric, alphaChannel, alphaType === 1, pixels.subarray(sy * width * 4));
231
+ }
232
+ return { width, height, pixels, orientation, icc };
233
+ }
234
+ const raw = new Uint8Array(planeBytes * planes);
214
235
  for (let plane = 0; plane < planes; plane++) {
215
236
  for (let s = 0; s < segmentsPerPlane; s++) {
216
237
  const sx = (s % segmentsAcross) * segmentWidth;
@@ -238,9 +259,8 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
238
259
  }
239
260
  }
240
261
  }
241
- const sampleLittle = predictor === 3 ? true : little;
242
- if (!planar && (bps === 8 || bps === 16) && sampleFormat !== 2 && sampleFormat !== 3 && photometric <= 2) {
243
- rowsToRGBA(raw, width, height, rowBytes, samplesPerPixel, bps === 16 ? (sampleLittle ? 1 : 0) : -1, photometric, alphaChannel, alphaType === 1, pixels);
262
+ if (common) {
263
+ rowsToRGBA(raw, width, height, rowBytes, samplesPerPixel, high, photometric, alphaChannel, alphaType === 1, pixels);
244
264
  return { width, height, pixels, orientation, icc };
245
265
  }
246
266
  const readSample = sampleReader(raw, bps, sampleFormat, sampleLittle, sampleFormat === 3 ? floatRange(raw, bps, sampleLittle) : null);
@@ -518,8 +518,8 @@ function deflate(dat, lvl, plvl, pre, post) {
518
518
  }
519
519
  return o.subarray(0, pre + shft(pos) + post);
520
520
  }
521
- function adler32(data) {
522
- let a = 1, b = 0;
521
+ function adler32(data, previous = 1) {
522
+ let a = previous & 65535, b = previous >>> 16;
523
523
  for (let i = 0; i < data.length;) {
524
524
  const e = Math.min(i + 2655, data.length);
525
525
  for (; i < e; ++i)
@@ -615,17 +615,30 @@ export async function limited(count, limit, run) {
615
615
  return results;
616
616
  }
617
617
  const PIECE = 1 << 20, WINDOW = 1 << 15;
618
- export async function compressParallel(data, level, threads = Infinity) {
618
+ export async function compressParallel(data, level, threads = Infinity, fill) {
619
619
  level = Math.min(9, Math.max(0, level | 0));
620
620
  const zlib = nodeZlib;
621
- if (!zlib || !zlib.deflate || !zlib.deflateRaw)
621
+ if (!zlib || !zlib.deflate || !zlib.deflateRaw) {
622
+ fill?.(0, data.length);
622
623
  return compressAsync(data, level);
624
+ }
623
625
  const done = (resolve, reject) => (error, result) => error ? reject(error) : resolve(plain(result));
624
- if (data.length < 2 * PIECE)
626
+ if (data.length < 2 * PIECE) {
627
+ fill?.(0, data.length);
625
628
  return new Promise((resolve, reject) => zlib.deflate?.(data, { level }, done(resolve, reject)));
629
+ }
626
630
  const count = Math.ceil(data.length / PIECE);
631
+ let made = 0, checksum = 1;
632
+ const make = (end) => {
633
+ if (end <= made)
634
+ return;
635
+ fill?.(made, end);
636
+ checksum = adler32(data.subarray(made, end), checksum);
637
+ made = end;
638
+ };
627
639
  const pieces = await limited(count, threads, i => {
628
640
  const start = i * PIECE;
641
+ make(Math.min(data.length, start + PIECE));
629
642
  const options = { level, finishFlush: i === count - 1 ? zlib.constants.Z_FINISH : zlib.constants.Z_SYNC_FLUSH };
630
643
  if (i)
631
644
  options.dictionary = data.subarray(start - WINDOW, start);
@@ -643,7 +656,6 @@ export async function compressParallel(data, level, threads = Infinity) {
643
656
  out.set(piece, offset);
644
657
  offset += piece.length;
645
658
  }
646
- const checksum = adler32(data);
647
659
  out[offset] = checksum >>> 24;
648
660
  out[offset + 1] = checksum >> 16 & 255;
649
661
  out[offset + 2] = checksum >> 8 & 255;
package/dist/image.js CHANGED
@@ -1263,6 +1263,16 @@ export class Image {
1263
1263
  return null;
1264
1264
  const width = this.width, bgR = background >>> 24, bgG = background >>> 16 & 255, bgB = background >>> 8 & 255;
1265
1265
  const scratch = new Uint8ClampedArray(width * 4);
1266
+ const mixed = new Uint8ClampedArray(3 << 16), known = new Uint8Array(256);
1267
+ const mix = (sa) => {
1268
+ known[sa] = 1;
1269
+ const fa = sa / 255, ba = 1 - fa, a = fa + ba, r = bgR * ba, g = bgG * ba, b = bgB * ba;
1270
+ for (let c = 0, o = sa << 8; c < 256; c++, o++) {
1271
+ mixed[o] = (c * fa + r) / a;
1272
+ mixed[o + 65536] = (c * fa + g) / a;
1273
+ mixed[o + 131072] = (c * fa + b) / a;
1274
+ }
1275
+ };
1266
1276
  const row = (y) => {
1267
1277
  for (let x = 0, s = 4 * y * width, d = 0; x < width; x++, s += 4, d += 4) {
1268
1278
  const sa = src[s + 3];
@@ -1278,10 +1288,12 @@ export class Image {
1278
1288
  scratch[d + 2] = src[s + 2];
1279
1289
  continue;
1280
1290
  }
1281
- const fa = sa / 255, ba = 1 - fa, a = fa + ba;
1282
- scratch[d] = (src[s] * fa + bgR * ba) / a;
1283
- scratch[d + 1] = (src[s + 1] * fa + bgG * ba) / a;
1284
- scratch[d + 2] = (src[s + 2] * fa + bgB * ba) / a;
1291
+ if (!known[sa])
1292
+ mix(sa);
1293
+ const o = sa << 8;
1294
+ scratch[d] = mixed[o | src[s]];
1295
+ scratch[d + 1] = mixed[o + 65536 | src[s + 1]];
1296
+ scratch[d + 2] = mixed[o + 131072 | src[s + 2]];
1285
1297
  }
1286
1298
  };
1287
1299
  const gray = () => {
@@ -1022,10 +1022,14 @@ function fromWOFF(data) {
1022
1022
  const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
1023
1023
  const count = view.getUint16(12);
1024
1024
  const tables = [];
1025
+ let budget = 1 << 27;
1025
1026
  for (let i = 0; i < count; i++) {
1026
1027
  const e = 44 + i * 20, offset = view.getUint32(e + 4), compressed = view.getUint32(e + 8), length = view.getUint32(e + 12);
1027
1028
  if (offset + compressed > data.length)
1028
1029
  throw new Error('Font: damaged or truncated font data');
1030
+ if (length > budget)
1031
+ throw new Error('Font: WOFF tables are too large');
1032
+ budget -= length;
1029
1033
  const raw = data.subarray(offset, offset + compressed);
1030
1034
  let table = raw;
1031
1035
  if (compressed < length) {
@@ -1035,6 +1039,8 @@ function fromWOFF(data) {
1035
1039
  catch (err) {
1036
1040
  throw new Error('Font: damaged WOFF table', { cause: err });
1037
1041
  }
1042
+ if (table.length !== length)
1043
+ throw new Error('Font: damaged WOFF table');
1038
1044
  }
1039
1045
  tables.push({ tag: view.getUint32(e), checksum: view.getUint32(e + 16), data: table });
1040
1046
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "4.2.0",
3
+ "version": "4.2.2",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",