imagescript 4.0.0 → 4.1.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 +9 -10
- package/THIRD_PARTY_NOTICES.md +6 -4
- package/dist/animation-encoder.js +15 -9
- package/dist/animation.d.ts +4 -3
- package/dist/animation.js +5 -3
- package/dist/codecs/jpeg.js +928 -37
- package/dist/codecs/png.js +100 -23
- package/dist/codecs/quantize-rgba.js +315 -0
- package/dist/codecs/quantize.js +31 -11
- package/dist/codecs/tiff.js +127 -45
- package/dist/codecs/vp8.js +341 -20
- package/dist/codecs/vp8l.js +288 -86
- package/dist/codecs/zlib.js +45 -0
- package/dist/image.d.ts +41 -2
- package/dist/image.js +18 -5
- package/package.json +1 -1
package/dist/codecs/vp8l.js
CHANGED
|
@@ -56,7 +56,7 @@ export class BitReader {
|
|
|
56
56
|
return this.pos - (this.count >> 3) > this.data.length;
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
-
const ROOT_BITS = 8;
|
|
59
|
+
const ROOT_BITS = 8, ROOT_MASK = (1 << ROOT_BITS) - 1;
|
|
60
60
|
const SUBTABLE = 0x40000000;
|
|
61
61
|
export function buildCode(lengths, size) {
|
|
62
62
|
let nonZero = 0, last = 0, maxLength = 0;
|
|
@@ -201,13 +201,6 @@ function readCode(reader, alphabetSize) {
|
|
|
201
201
|
return buildCode(lengths, alphabetSize);
|
|
202
202
|
}
|
|
203
203
|
const subSize = (size, bits) => (size + (1 << bits) - 1) >> bits;
|
|
204
|
-
function prefixValue(reader, symbol) {
|
|
205
|
-
if (symbol < 4)
|
|
206
|
-
return symbol + 1;
|
|
207
|
-
const extra = (symbol - 2) >> 1;
|
|
208
|
-
const offset = (2 + (symbol & 1)) << extra;
|
|
209
|
-
return offset + reader.read(extra) + 1;
|
|
210
|
-
}
|
|
211
204
|
function decodeImage(reader, width, height, isMain) {
|
|
212
205
|
let cacheBits = 0;
|
|
213
206
|
if (reader.read(1)) {
|
|
@@ -245,55 +238,204 @@ function decodeImage(reader, width, height, isMain) {
|
|
|
245
238
|
let cached = 0;
|
|
246
239
|
const prefixMask = (1 << prefixBits) - 1;
|
|
247
240
|
let group = groups[0];
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
if (
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
else if (green < NUM_LITERAL_CODES + NUM_LENGTH_CODES) {
|
|
263
|
-
const length = prefixValue(reader, green - NUM_LITERAL_CODES);
|
|
264
|
-
const distanceSymbol = readSymbol(reader, group[4]);
|
|
265
|
-
let distance = prefixValue(reader, distanceSymbol);
|
|
266
|
-
if (distance > 120)
|
|
267
|
-
distance -= 120;
|
|
241
|
+
const data = reader.data, length = data.length;
|
|
242
|
+
let pos = reader.pos, buffer = reader.buffer, count = reader.count;
|
|
243
|
+
const sync = () => {
|
|
244
|
+
reader.pos = pos;
|
|
245
|
+
reader.buffer = buffer;
|
|
246
|
+
reader.count = count;
|
|
247
|
+
};
|
|
248
|
+
try {
|
|
249
|
+
for (let i = 0, x = 0, y = 0; i < total;) {
|
|
250
|
+
if (groupImage && (x & prefixMask) === 0)
|
|
251
|
+
group = groups[groupImage[(y >> prefixBits) * prefixWidth + (x >> prefixBits)]];
|
|
252
|
+
let green;
|
|
253
|
+
if (group[0].single >= 0)
|
|
254
|
+
green = group[0].single;
|
|
268
255
|
else {
|
|
269
|
-
|
|
270
|
-
|
|
256
|
+
if (count < 24) {
|
|
257
|
+
while (count <= 24) {
|
|
258
|
+
const byte = pos < length ? data[pos] : 0;
|
|
259
|
+
if (pos > length + 8)
|
|
260
|
+
throw new Error('VP8L: unexpected end of data');
|
|
261
|
+
pos++;
|
|
262
|
+
buffer |= byte << count;
|
|
263
|
+
count += 8;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
let entry = group[0].table[buffer & ROOT_MASK];
|
|
267
|
+
if (entry & SUBTABLE)
|
|
268
|
+
entry = group[0].table[((entry & ~SUBTABLE) >> 4) + (buffer >> ROOT_BITS & (1 << (entry & 15)) - 1)];
|
|
269
|
+
buffer >>>= entry & 15;
|
|
270
|
+
count -= entry & 15;
|
|
271
|
+
green = entry >> 4;
|
|
271
272
|
}
|
|
272
|
-
if (
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
273
|
+
if (green < NUM_LITERAL_CODES) {
|
|
274
|
+
let red;
|
|
275
|
+
if (group[1].single >= 0)
|
|
276
|
+
red = group[1].single;
|
|
277
|
+
else {
|
|
278
|
+
if (count < 24) {
|
|
279
|
+
while (count <= 24) {
|
|
280
|
+
const byte = pos < length ? data[pos] : 0;
|
|
281
|
+
if (pos > length + 8)
|
|
282
|
+
throw new Error('VP8L: unexpected end of data');
|
|
283
|
+
pos++;
|
|
284
|
+
buffer |= byte << count;
|
|
285
|
+
count += 8;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
let entry = group[1].table[buffer & ROOT_MASK];
|
|
289
|
+
if (entry & SUBTABLE)
|
|
290
|
+
entry = group[1].table[((entry & ~SUBTABLE) >> 4) + (buffer >> ROOT_BITS & (1 << (entry & 15)) - 1)];
|
|
291
|
+
buffer >>>= entry & 15;
|
|
292
|
+
count -= entry & 15;
|
|
293
|
+
red = entry >> 4;
|
|
294
|
+
}
|
|
295
|
+
let blue;
|
|
296
|
+
if (group[2].single >= 0)
|
|
297
|
+
blue = group[2].single;
|
|
298
|
+
else {
|
|
299
|
+
if (count < 24) {
|
|
300
|
+
while (count <= 24) {
|
|
301
|
+
const byte = pos < length ? data[pos] : 0;
|
|
302
|
+
if (pos > length + 8)
|
|
303
|
+
throw new Error('VP8L: unexpected end of data');
|
|
304
|
+
pos++;
|
|
305
|
+
buffer |= byte << count;
|
|
306
|
+
count += 8;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
let entry = group[2].table[buffer & ROOT_MASK];
|
|
310
|
+
if (entry & SUBTABLE)
|
|
311
|
+
entry = group[2].table[((entry & ~SUBTABLE) >> 4) + (buffer >> ROOT_BITS & (1 << (entry & 15)) - 1)];
|
|
312
|
+
buffer >>>= entry & 15;
|
|
313
|
+
count -= entry & 15;
|
|
314
|
+
blue = entry >> 4;
|
|
315
|
+
}
|
|
316
|
+
let alpha;
|
|
317
|
+
if (group[3].single >= 0)
|
|
318
|
+
alpha = group[3].single;
|
|
319
|
+
else {
|
|
320
|
+
if (count < 24) {
|
|
321
|
+
while (count <= 24) {
|
|
322
|
+
const byte = pos < length ? data[pos] : 0;
|
|
323
|
+
if (pos > length + 8)
|
|
324
|
+
throw new Error('VP8L: unexpected end of data');
|
|
325
|
+
pos++;
|
|
326
|
+
buffer |= byte << count;
|
|
327
|
+
count += 8;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
let entry = group[3].table[buffer & ROOT_MASK];
|
|
331
|
+
if (entry & SUBTABLE)
|
|
332
|
+
entry = group[3].table[((entry & ~SUBTABLE) >> 4) + (buffer >> ROOT_BITS & (1 << (entry & 15)) - 1)];
|
|
333
|
+
buffer >>>= entry & 15;
|
|
334
|
+
count -= entry & 15;
|
|
335
|
+
alpha = entry >> 4;
|
|
336
|
+
}
|
|
337
|
+
pixels[i++] = (alpha << 24 | red << 16 | green << 8 | blue) >>> 0;
|
|
338
|
+
if (++x === width) {
|
|
339
|
+
x = 0;
|
|
340
|
+
y++;
|
|
341
|
+
}
|
|
280
342
|
}
|
|
281
|
-
if (
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
343
|
+
else if (green < NUM_LITERAL_CODES + NUM_LENGTH_CODES) {
|
|
344
|
+
const lengthSymbol = green - NUM_LITERAL_CODES;
|
|
345
|
+
let run;
|
|
346
|
+
if (lengthSymbol < 4)
|
|
347
|
+
run = lengthSymbol + 1;
|
|
348
|
+
else {
|
|
349
|
+
const extra = (lengthSymbol - 2) >> 1;
|
|
350
|
+
if (count < extra) {
|
|
351
|
+
while (count <= 24) {
|
|
352
|
+
const byte = pos < length ? data[pos] : 0;
|
|
353
|
+
if (pos > length + 8)
|
|
354
|
+
throw new Error('VP8L: unexpected end of data');
|
|
355
|
+
pos++;
|
|
356
|
+
buffer |= byte << count;
|
|
357
|
+
count += 8;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
run = ((2 + (lengthSymbol & 1)) << extra) + (buffer & (1 << extra) - 1) + 1;
|
|
361
|
+
buffer >>>= extra;
|
|
362
|
+
count -= extra;
|
|
363
|
+
}
|
|
364
|
+
let distanceSymbol;
|
|
365
|
+
if (group[4].single >= 0)
|
|
366
|
+
distanceSymbol = group[4].single;
|
|
367
|
+
else {
|
|
368
|
+
if (count < 24) {
|
|
369
|
+
while (count <= 24) {
|
|
370
|
+
const byte = pos < length ? data[pos] : 0;
|
|
371
|
+
if (pos > length + 8)
|
|
372
|
+
throw new Error('VP8L: unexpected end of data');
|
|
373
|
+
pos++;
|
|
374
|
+
buffer |= byte << count;
|
|
375
|
+
count += 8;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
let entry = group[4].table[buffer & ROOT_MASK];
|
|
379
|
+
if (entry & SUBTABLE)
|
|
380
|
+
entry = group[4].table[((entry & ~SUBTABLE) >> 4) + (buffer >> ROOT_BITS & (1 << (entry & 15)) - 1)];
|
|
381
|
+
buffer >>>= entry & 15;
|
|
382
|
+
count -= entry & 15;
|
|
383
|
+
distanceSymbol = entry >> 4;
|
|
384
|
+
}
|
|
385
|
+
let distance;
|
|
386
|
+
if (distanceSymbol < 4)
|
|
387
|
+
distance = distanceSymbol + 1;
|
|
388
|
+
else {
|
|
389
|
+
const extra = (distanceSymbol - 2) >> 1;
|
|
390
|
+
if (count < extra) {
|
|
391
|
+
while (count <= 24) {
|
|
392
|
+
const byte = pos < length ? data[pos] : 0;
|
|
393
|
+
if (pos > length + 8)
|
|
394
|
+
throw new Error('VP8L: unexpected end of data');
|
|
395
|
+
pos++;
|
|
396
|
+
buffer |= byte << count;
|
|
397
|
+
count += 8;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
distance = ((2 + (distanceSymbol & 1)) << extra) + (buffer & (1 << extra) - 1) + 1;
|
|
401
|
+
buffer >>>= extra;
|
|
402
|
+
count -= extra;
|
|
403
|
+
}
|
|
404
|
+
if (distance > 120)
|
|
405
|
+
distance -= 120;
|
|
406
|
+
else {
|
|
407
|
+
const dx = DISTANCE_MAP[2 * distance - 2], dy = DISTANCE_MAP[2 * distance - 1];
|
|
408
|
+
distance = Math.max(1, dy * width + dx);
|
|
409
|
+
}
|
|
410
|
+
if (distance > i || i + run > total)
|
|
411
|
+
throw new Error('VP8L: invalid backward reference');
|
|
412
|
+
for (let k = 0; k < run; k++, i++)
|
|
413
|
+
pixels[i] = pixels[i - distance];
|
|
414
|
+
x += run;
|
|
415
|
+
while (x >= width) {
|
|
416
|
+
x -= width;
|
|
417
|
+
y++;
|
|
418
|
+
}
|
|
419
|
+
if (groupImage)
|
|
420
|
+
group = groups[groupImage[(y >> prefixBits) * prefixWidth + (x >> prefixBits)]];
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
if (!cache)
|
|
424
|
+
throw new Error('VP8L: invalid color cache symbol');
|
|
425
|
+
for (; cached < i; cached++)
|
|
426
|
+
cache[Math.imul(pixels[cached], 0x1e35a7bd) >>> cacheShift] = pixels[cached];
|
|
427
|
+
pixels[i++] = cache[green - NUM_LITERAL_CODES - NUM_LENGTH_CODES];
|
|
428
|
+
if (++x === width) {
|
|
429
|
+
x = 0;
|
|
430
|
+
y++;
|
|
431
|
+
}
|
|
293
432
|
}
|
|
433
|
+
if (pos - (count >> 3) > length)
|
|
434
|
+
throw new Error('VP8L: unexpected end of data');
|
|
294
435
|
}
|
|
295
|
-
|
|
296
|
-
|
|
436
|
+
}
|
|
437
|
+
finally {
|
|
438
|
+
sync();
|
|
297
439
|
}
|
|
298
440
|
return pixels;
|
|
299
441
|
}
|
|
@@ -320,81 +462,139 @@ function select(l, t, tl) {
|
|
|
320
462
|
}
|
|
321
463
|
const addPixels = (a, b) => ((a & 0xff00ff00) + (b & 0xff00ff00) & 0xff00ff00 | (a & 0x00ff00ff) + (b & 0x00ff00ff) & 0x00ff00ff) >>> 0;
|
|
322
464
|
function predictRun(mode, p, i, n, width, out, o) {
|
|
323
|
-
const restore = out === p;
|
|
324
465
|
const end = i + n;
|
|
325
466
|
switch (mode) {
|
|
326
467
|
case 1:
|
|
327
468
|
for (; i < end; i++, o++)
|
|
328
|
-
out[o] =
|
|
469
|
+
out[o] = p[i - 1];
|
|
329
470
|
return;
|
|
330
471
|
case 2:
|
|
331
472
|
for (; i < end; i++, o++)
|
|
332
|
-
out[o] =
|
|
473
|
+
out[o] = p[i - width];
|
|
333
474
|
return;
|
|
334
475
|
case 3:
|
|
335
476
|
for (; i < end; i++, o++)
|
|
336
|
-
out[o] =
|
|
477
|
+
out[o] = p[i - width + 1];
|
|
337
478
|
return;
|
|
338
479
|
case 4:
|
|
339
480
|
for (; i < end; i++, o++)
|
|
340
|
-
out[o] =
|
|
481
|
+
out[o] = p[i - width - 1];
|
|
482
|
+
return;
|
|
483
|
+
case 5:
|
|
484
|
+
for (; i < end; i++, o++)
|
|
485
|
+
out[o] = average2(average2(p[i - 1], p[i - width + 1]), p[i - width]);
|
|
486
|
+
return;
|
|
487
|
+
case 6:
|
|
488
|
+
for (; i < end; i++, o++)
|
|
489
|
+
out[o] = average2(p[i - 1], p[i - width - 1]);
|
|
490
|
+
return;
|
|
491
|
+
case 7:
|
|
492
|
+
for (; i < end; i++, o++)
|
|
493
|
+
out[o] = average2(p[i - 1], p[i - width]);
|
|
494
|
+
return;
|
|
495
|
+
case 8:
|
|
496
|
+
for (; i < end; i++, o++)
|
|
497
|
+
out[o] = average2(p[i - width - 1], p[i - width]);
|
|
498
|
+
return;
|
|
499
|
+
case 9:
|
|
500
|
+
for (; i < end; i++, o++)
|
|
501
|
+
out[o] = average2(p[i - width], p[i - width + 1]);
|
|
502
|
+
return;
|
|
503
|
+
case 10:
|
|
504
|
+
for (; i < end; i++, o++)
|
|
505
|
+
out[o] = average2(average2(p[i - 1], p[i - width - 1]), average2(p[i - width], p[i - width + 1]));
|
|
506
|
+
return;
|
|
507
|
+
case 11:
|
|
508
|
+
for (; i < end; i++, o++)
|
|
509
|
+
out[o] = select(p[i - 1], p[i - width], p[i - width - 1]);
|
|
510
|
+
return;
|
|
511
|
+
case 12:
|
|
512
|
+
for (; i < end; i++, o++)
|
|
513
|
+
out[o] = clampAddSubtractFull(p[i - 1], p[i - width], p[i - width - 1]);
|
|
514
|
+
return;
|
|
515
|
+
case 13:
|
|
516
|
+
for (; i < end; i++, o++)
|
|
517
|
+
out[o] = clampAddSubtractHalf(average2(p[i - 1], p[i - width]), p[i - width - 1]);
|
|
518
|
+
return;
|
|
519
|
+
default: for (; i < end; i++, o++)
|
|
520
|
+
out[o] = 0xff000000;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
function restoreRun(mode, p, i, n, width) {
|
|
524
|
+
const end = i + n;
|
|
525
|
+
switch (mode) {
|
|
526
|
+
case 1:
|
|
527
|
+
for (; i < end; i++)
|
|
528
|
+
p[i] = addPixels(p[i], p[i - 1]);
|
|
529
|
+
return;
|
|
530
|
+
case 2:
|
|
531
|
+
for (; i < end; i++)
|
|
532
|
+
p[i] = addPixels(p[i], p[i - width]);
|
|
533
|
+
return;
|
|
534
|
+
case 3:
|
|
535
|
+
for (; i < end; i++)
|
|
536
|
+
p[i] = addPixels(p[i], p[i - width + 1]);
|
|
537
|
+
return;
|
|
538
|
+
case 4:
|
|
539
|
+
for (; i < end; i++)
|
|
540
|
+
p[i] = addPixels(p[i], p[i - width - 1]);
|
|
341
541
|
return;
|
|
342
542
|
case 5:
|
|
343
|
-
for (; i < end; i
|
|
543
|
+
for (; i < end; i++) {
|
|
344
544
|
const v = average2(average2(p[i - 1], p[i - width + 1]), p[i - width]);
|
|
345
|
-
|
|
545
|
+
p[i] = addPixels(p[i], v);
|
|
346
546
|
}
|
|
347
547
|
return;
|
|
348
548
|
case 6:
|
|
349
|
-
for (; i < end; i
|
|
549
|
+
for (; i < end; i++) {
|
|
350
550
|
const v = average2(p[i - 1], p[i - width - 1]);
|
|
351
|
-
|
|
551
|
+
p[i] = addPixels(p[i], v);
|
|
352
552
|
}
|
|
353
553
|
return;
|
|
354
554
|
case 7:
|
|
355
|
-
for (; i < end; i
|
|
555
|
+
for (; i < end; i++) {
|
|
356
556
|
const v = average2(p[i - 1], p[i - width]);
|
|
357
|
-
|
|
557
|
+
p[i] = addPixels(p[i], v);
|
|
358
558
|
}
|
|
359
559
|
return;
|
|
360
560
|
case 8:
|
|
361
|
-
for (; i < end; i
|
|
561
|
+
for (; i < end; i++) {
|
|
362
562
|
const v = average2(p[i - width - 1], p[i - width]);
|
|
363
|
-
|
|
563
|
+
p[i] = addPixels(p[i], v);
|
|
364
564
|
}
|
|
365
565
|
return;
|
|
366
566
|
case 9:
|
|
367
|
-
for (; i < end; i
|
|
567
|
+
for (; i < end; i++) {
|
|
368
568
|
const v = average2(p[i - width], p[i - width + 1]);
|
|
369
|
-
|
|
569
|
+
p[i] = addPixels(p[i], v);
|
|
370
570
|
}
|
|
371
571
|
return;
|
|
372
572
|
case 10:
|
|
373
|
-
for (; i < end; i
|
|
573
|
+
for (; i < end; i++) {
|
|
374
574
|
const v = average2(average2(p[i - 1], p[i - width - 1]), average2(p[i - width], p[i - width + 1]));
|
|
375
|
-
|
|
575
|
+
p[i] = addPixels(p[i], v);
|
|
376
576
|
}
|
|
377
577
|
return;
|
|
378
578
|
case 11:
|
|
379
|
-
for (; i < end; i
|
|
579
|
+
for (; i < end; i++) {
|
|
380
580
|
const v = select(p[i - 1], p[i - width], p[i - width - 1]);
|
|
381
|
-
|
|
581
|
+
p[i] = addPixels(p[i], v);
|
|
382
582
|
}
|
|
383
583
|
return;
|
|
384
584
|
case 12:
|
|
385
|
-
for (; i < end; i
|
|
585
|
+
for (; i < end; i++) {
|
|
386
586
|
const v = clampAddSubtractFull(p[i - 1], p[i - width], p[i - width - 1]);
|
|
387
|
-
|
|
587
|
+
p[i] = addPixels(p[i], v);
|
|
388
588
|
}
|
|
389
589
|
return;
|
|
390
590
|
case 13:
|
|
391
|
-
for (; i < end; i
|
|
591
|
+
for (; i < end; i++) {
|
|
392
592
|
const v = clampAddSubtractHalf(average2(p[i - 1], p[i - width]), p[i - width - 1]);
|
|
393
|
-
|
|
593
|
+
p[i] = addPixels(p[i], v);
|
|
394
594
|
}
|
|
395
595
|
return;
|
|
396
|
-
default: for (; i < end; i
|
|
397
|
-
|
|
596
|
+
default: for (; i < end; i++)
|
|
597
|
+
p[i] = addPixels(p[i], 0xff000000);
|
|
398
598
|
}
|
|
399
599
|
}
|
|
400
600
|
function inversePredictor(p, width, height, bits, modes) {
|
|
@@ -408,24 +608,26 @@ function inversePredictor(p, width, height, bits, modes) {
|
|
|
408
608
|
const modeRow = (y >> bits) * blocksPerRow;
|
|
409
609
|
for (let x = 1; x < width;) {
|
|
410
610
|
const end = Math.min(width, (x >> bits) + 1 << bits);
|
|
411
|
-
|
|
611
|
+
restoreRun(modes[modeRow + (x >> bits)] >> 8 & 15, p, row + x, end - x, width);
|
|
412
612
|
x = end;
|
|
413
613
|
}
|
|
414
614
|
}
|
|
415
615
|
}
|
|
416
|
-
const colorDelta = (t, c) => ((t << 24 >> 24) * (c << 24 >> 24)) >> 5;
|
|
417
616
|
function inverseColorTransform(p, width, height, bits, elements) {
|
|
418
617
|
const blocksPerRow = subSize(width, bits);
|
|
419
618
|
for (let y = 0; y < height; y++) {
|
|
420
619
|
const elementRow = (y >> bits) * blocksPerRow;
|
|
421
|
-
for (let x = 0; x < width;
|
|
620
|
+
for (let x = 0; x < width;) {
|
|
422
621
|
const e = elements[elementRow + (x >> bits)];
|
|
423
|
-
const greenToRed = e
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
622
|
+
const greenToRed = e << 24 >> 24, greenToBlue = e << 16 >> 24, redToBlue = e << 8 >> 24;
|
|
623
|
+
for (let i = y * width + x, end = y * width + Math.min(width, (x >> bits) + 1 << bits); i < end; i++) {
|
|
624
|
+
const argb = p[i];
|
|
625
|
+
const green = argb << 16 >> 24;
|
|
626
|
+
const red = (argb >> 16) + (greenToRed * green >> 5) & 255;
|
|
627
|
+
const blue = (argb & 255) + (greenToBlue * green >> 5) + (redToBlue * (red << 24 >> 24) >> 5) & 255;
|
|
628
|
+
p[i] = (argb & 0xff00ff00 | red << 16 | blue) >>> 0;
|
|
629
|
+
}
|
|
630
|
+
x = (x >> bits) + 1 << bits;
|
|
429
631
|
}
|
|
430
632
|
}
|
|
431
633
|
}
|
package/dist/codecs/zlib.js
CHANGED
|
@@ -605,6 +605,51 @@ export async function compressAsync(data, level) {
|
|
|
605
605
|
}
|
|
606
606
|
return compress(data, level);
|
|
607
607
|
}
|
|
608
|
+
export async function limited(count, limit, run) {
|
|
609
|
+
const results = new Array(count);
|
|
610
|
+
let next = 0;
|
|
611
|
+
await Promise.all(Array.from({ length: Math.min(limit, count) }, async () => {
|
|
612
|
+
for (let i; (i = next++) < count;)
|
|
613
|
+
results[i] = await run(i);
|
|
614
|
+
}));
|
|
615
|
+
return results;
|
|
616
|
+
}
|
|
617
|
+
const PIECE = 1 << 20, WINDOW = 1 << 15;
|
|
618
|
+
export async function compressParallel(data, level, threads = Infinity) {
|
|
619
|
+
level = Math.min(9, Math.max(0, level | 0));
|
|
620
|
+
const zlib = nodeZlib;
|
|
621
|
+
if (!zlib || !zlib.deflate || !zlib.deflateRaw)
|
|
622
|
+
return compressAsync(data, level);
|
|
623
|
+
const done = (resolve, reject) => (error, result) => error ? reject(error) : resolve(plain(result));
|
|
624
|
+
if (data.length < 2 * PIECE)
|
|
625
|
+
return new Promise((resolve, reject) => zlib.deflate?.(data, { level }, done(resolve, reject)));
|
|
626
|
+
const count = Math.ceil(data.length / PIECE);
|
|
627
|
+
const pieces = await limited(count, threads, i => {
|
|
628
|
+
const start = i * PIECE;
|
|
629
|
+
const options = { level, finishFlush: i === count - 1 ? zlib.constants.Z_FINISH : zlib.constants.Z_SYNC_FLUSH };
|
|
630
|
+
if (i)
|
|
631
|
+
options.dictionary = data.subarray(start - WINDOW, start);
|
|
632
|
+
return new Promise((resolve, reject) => zlib.deflateRaw?.(data.subarray(start, start + PIECE), options, done(resolve, reject)));
|
|
633
|
+
});
|
|
634
|
+
let length = 6;
|
|
635
|
+
for (const piece of pieces)
|
|
636
|
+
length += piece.length;
|
|
637
|
+
const out = new Uint8Array(length);
|
|
638
|
+
const hint = level === 0 ? 0 : level < 6 ? 1 : level === 9 ? 3 : 2;
|
|
639
|
+
out[0] = 120;
|
|
640
|
+
out[1] = hint << 6 | (hint ? 32 - 2 * hint : 1);
|
|
641
|
+
let offset = 2;
|
|
642
|
+
for (const piece of pieces) {
|
|
643
|
+
out.set(piece, offset);
|
|
644
|
+
offset += piece.length;
|
|
645
|
+
}
|
|
646
|
+
const checksum = adler32(data);
|
|
647
|
+
out[offset] = checksum >>> 24;
|
|
648
|
+
out[offset + 1] = checksum >> 16 & 255;
|
|
649
|
+
out[offset + 2] = checksum >> 8 & 255;
|
|
650
|
+
out[offset + 3] = checksum & 255;
|
|
651
|
+
return out;
|
|
652
|
+
}
|
|
608
653
|
export async function decompressAsync(data, limit) {
|
|
609
654
|
if (nodeZlib) {
|
|
610
655
|
try {
|
package/dist/image.d.ts
CHANGED
|
@@ -296,13 +296,34 @@ export interface GainMapEmbedOptions {
|
|
|
296
296
|
*/
|
|
297
297
|
scale?: number;
|
|
298
298
|
}
|
|
299
|
-
/** Options of {@link Image.encodePNG} */
|
|
300
|
-
export interface PNGEncodeOptions extends EncodeOptions {
|
|
299
|
+
/** Options of {@link Image.encodePNG}. `dither` and `ditherStrength` are only used with `colors` */
|
|
300
|
+
export interface PNGEncodeOptions extends EncodeOptions, DitherOptions {
|
|
301
301
|
/**
|
|
302
302
|
* The compression level (0-9), higher is smaller but slower
|
|
303
303
|
* @defaultValue `4`
|
|
304
304
|
*/
|
|
305
305
|
compression?: number;
|
|
306
|
+
/**
|
|
307
|
+
* Maximum threads for compression. Small images (under 2 MiB of pixel data) and runtimes without node:zlib (such as
|
|
308
|
+
* browsers) use one thread
|
|
309
|
+
* @remarks
|
|
310
|
+
* The image is compressed in pieces of 1 MiB on node:zlib's thread pool, not in workers, so the file is the same
|
|
311
|
+
* for any number of threads. Node's pool has 4 threads unless `UV_THREADPOOL_SIZE` says otherwise
|
|
312
|
+
* @defaultValue The available processors, up to 8
|
|
313
|
+
*/
|
|
314
|
+
threads?: number;
|
|
315
|
+
/**
|
|
316
|
+
* Lossy: the maximum colors in the palette (2-256, including transparency), each with its own transparency, so soft
|
|
317
|
+
* edges and shadows stay soft. Makes photos with transparency and graphics with gradients much smaller. Images with
|
|
318
|
+
* no more colors keep them exactly (the file is the same as without this option); otherwise ImageScript chooses
|
|
319
|
+
* representative colors and applies `dither`. Fully transparent pixels lose their hidden color. The image itself
|
|
320
|
+
* stays unchanged
|
|
321
|
+
* @defaultValue None: the PNG is lossless
|
|
322
|
+
* @remarks
|
|
323
|
+
* Flat or smoothly shaded graphics without noise can be smaller as lossless PNGs, and dithering makes them larger:
|
|
324
|
+
* try `dither: 'none'`, and compare
|
|
325
|
+
*/
|
|
326
|
+
colors?: number;
|
|
306
327
|
}
|
|
307
328
|
/** Options of {@link Image.encodeTGA}: the metadata, which TGA keeps in its extension area */
|
|
308
329
|
export interface TGAEncodeOptions extends EncodeOptions {
|
|
@@ -329,6 +350,24 @@ export interface JPEGEncodeOptions extends EncodeOptions {
|
|
|
329
350
|
* @defaultValue `'4:2:0'` up to quality 90, `'4:4:4'` above
|
|
330
351
|
*/
|
|
331
352
|
subsampling?: '4:4:4' | '4:2:0';
|
|
353
|
+
/**
|
|
354
|
+
* Whether to write a progressive JPEG: the picture first appears blurry as a whole and sharpens while it loads, and
|
|
355
|
+
* the file is about 3% smaller for photos. The pixels are the same, and encoding takes 1.1 to 1.3 times as long.
|
|
356
|
+
* An embedded gain map stays a baseline JPEG
|
|
357
|
+
* @defaultValue `false`
|
|
358
|
+
*/
|
|
359
|
+
progressive?: boolean;
|
|
360
|
+
/**
|
|
361
|
+
* The quantization tables, which decide how coarsely each frequency is stored (not the colors, as in
|
|
362
|
+
* {@link Image.quantize}): `'standard'` (the JPEG standard's) stores fine detail more coarsely than smooth areas,
|
|
363
|
+
* which softens sharp edges and text first; `'flat'` uses the same step for every frequency, which keeps edges and
|
|
364
|
+
* text clearer with fewer visible artifacts. At the same PSNR, files with `'flat'` were 3 to 30% smaller in a test of
|
|
365
|
+
* photos and a screenshot; at the same SSIM they ranged from 21% smaller to 15% larger (a photo at a low quality).
|
|
366
|
+
* The same `quality` makes a larger file with `'flat'`: lower it until the size matches (by about 15 to 25 around
|
|
367
|
+
* quality 60 for photos). Qualities 96 to 100 give the same `'flat'` file. Not used for the gain map
|
|
368
|
+
* @defaultValue `'standard'`
|
|
369
|
+
*/
|
|
370
|
+
quantization?: 'standard' | 'flat';
|
|
332
371
|
/**
|
|
333
372
|
* The background color to combine with transparent pixels, since JPEG doesn't support transparency. Opaque images
|
|
334
373
|
* are encoded as they are
|
package/dist/image.js
CHANGED
|
@@ -39,6 +39,7 @@ const TRANSFORM_MODES = ['nearest-neighbor', 'bilinear', 'bicubic'];
|
|
|
39
39
|
const RESIZE_FITS = ['fill', 'inside', 'contain', 'cover'];
|
|
40
40
|
const CIRCLE_FITS = ['contain', 'cover'];
|
|
41
41
|
const SUBSAMPLINGS = ['4:4:4', '4:2:0'];
|
|
42
|
+
const QUANTIZATIONS = ['standard', 'flat'];
|
|
42
43
|
export const TONE_MAPPINGS = ['reinhard', 'clamp'];
|
|
43
44
|
export const FLIP_DIRECTIONS = ['horizontal', 'vertical'];
|
|
44
45
|
function featureList(features) {
|
|
@@ -488,11 +489,18 @@ export class Image {
|
|
|
488
489
|
const right = Math.min(this.width, x + source.width), bottom = Math.min(this.height, y + source.height);
|
|
489
490
|
const dst = this.bitmap;
|
|
490
491
|
const src = source.bitmap.buffer === dst.buffer ? source.bitmap.slice() : source.bitmap;
|
|
492
|
+
const words = opacity === 1 && !(src.byteOffset & 3) && !(dst.byteOffset & 3);
|
|
493
|
+
const srcWords = words ? new Uint32Array(src.buffer, src.byteOffset, src.length >> 2) : null;
|
|
494
|
+
const dstWords = words ? new Uint32Array(dst.buffer, dst.byteOffset, dst.length >> 2) : null;
|
|
491
495
|
for (let row = top; row < bottom; row++) {
|
|
492
496
|
for (let column = left, s = 4 * ((row - y) * source.width + left - x), d = 4 * (row * this.width + left); column < right; column++, s += 4, d += 4) {
|
|
493
497
|
const sa = src[s + 3];
|
|
494
498
|
if (sa === 0)
|
|
495
499
|
continue;
|
|
500
|
+
if (sa === 255 && dstWords && srcWords) {
|
|
501
|
+
dstWords[d >> 2] = srcWords[s >> 2];
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
496
504
|
const da = dst[d + 3];
|
|
497
505
|
if ((sa === 255 && opacity === 1) || da === 0) {
|
|
498
506
|
dst[d] = src[s];
|
|
@@ -1193,15 +1201,20 @@ export class Image {
|
|
|
1193
1201
|
}
|
|
1194
1202
|
async encodePNG(options = {}) {
|
|
1195
1203
|
checkOptions(options, 'encodePNG({ compression: 9, metadata: { author: \'me\' } })');
|
|
1196
|
-
const { compression = 4, metadata = {} } = options;
|
|
1197
|
-
|
|
1204
|
+
const { compression = 4, threads = defaultThreads(), colors, dither = 'floyd-steinberg', ditherStrength = 0.85, metadata = {} } = options;
|
|
1205
|
+
checkThreads(threads);
|
|
1206
|
+
const palette = quantize.paletteOptions(colors, dither, ditherStrength);
|
|
1207
|
+
return png.encodeRGBA(this.__bytes__(), this.width, this.height, compressionLevel(compression), pngText(resolveMetadata(metadata)), threads, palette.colors === undefined ? undefined : { colors: palette.colors, dither: palette.dither, strength: palette.strength });
|
|
1198
1208
|
}
|
|
1199
1209
|
async encodeJPEG(options = {}) {
|
|
1200
1210
|
checkOptions(options, 'encodeJPEG({ quality: 80 })');
|
|
1201
|
-
const { quality: qualityOption = 90, subsampling, background = 0x000000ff, metadata = {}, gainMap: gainMapOption = true } = options;
|
|
1211
|
+
const { quality: qualityOption = 90, subsampling, quantization, progressive = false, background = 0x000000ff, metadata = {}, gainMap: gainMapOption = true } = options;
|
|
1202
1212
|
const quality = checkQuality(qualityOption);
|
|
1203
1213
|
if (subsampling !== undefined)
|
|
1204
1214
|
oneOf(subsampling, SUBSAMPLINGS, 'subsampling');
|
|
1215
|
+
if (quantization !== undefined)
|
|
1216
|
+
oneOf(quantization, QUANTIZATIONS, 'quantization');
|
|
1217
|
+
boolean(progressive, 'progressive');
|
|
1205
1218
|
const backgroundColor = checkColor(background, 'background');
|
|
1206
1219
|
const embed = gainMapEmbed(gainMapOption);
|
|
1207
1220
|
const m = resolveMetadata(metadata);
|
|
@@ -1213,14 +1226,14 @@ export class Image {
|
|
|
1213
1226
|
checkFormatSize('JPEG', this.width, this.height, 0xffff);
|
|
1214
1227
|
if (!this.__gain_map__) {
|
|
1215
1228
|
const flat = this.__flatRows__(backgroundColor);
|
|
1216
|
-
const base = { exif, comment, subsampling };
|
|
1229
|
+
const base = { exif, comment, subsampling, quantization, progressive };
|
|
1217
1230
|
if (!flat)
|
|
1218
1231
|
return jpeg.encode(this.__bytes__(), this.width, this.height, quality, { ...base, gray: this.__is_gray__() });
|
|
1219
1232
|
return jpeg.encode(flat.pixels, this.width, this.height, quality, { ...base, gray: flat.gray(), rows: flat.row });
|
|
1220
1233
|
}
|
|
1221
1234
|
const source = this.__flattened__(backgroundColor, embed !== null) ?? this;
|
|
1222
1235
|
const gainMap = source.__gain_map__;
|
|
1223
|
-
const base = { exif, comment, subsampling, gray: source.__is_gray__() };
|
|
1236
|
+
const base = { exif, comment, subsampling, quantization, progressive, gray: source.__is_gray__() };
|
|
1224
1237
|
if (!gainMap || !embed)
|
|
1225
1238
|
return jpeg.encode(source.__bytes__(), source.width, source.height, quality, base);
|
|
1226
1239
|
const { quality: mapQuality, scale } = embed;
|