@poe-platform/safe-bash 0.1.834 → 0.1.836

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.
@@ -11,6 +11,8 @@ export declare class GifPalette {
11
11
  private readonly colors;
12
12
  private readonly used;
13
13
  private readonly missing;
14
+ private readonly histogram;
15
+ private quantized?;
14
16
  constructor();
15
17
  add(r: number, g: number, b: number, a: number): void;
16
18
  finish(): void;
@@ -1,4 +1,4 @@
1
1
  import type { ImageByteStorage, StoredRgbaImage } from "./png-storage.js";
2
2
  import { type GifOptions } from "./gif-output-parts.js";
3
- /** Multipass GIF encoding with a fixed palette and pull-driven owned subblocks. */
3
+ /** Multipass GIF encoding with a source-derived palette and pull-driven owned subblocks. */
4
4
  export declare function encodeGifFromStorage(image: StoredRgbaImage, storage: ImageByteStorage, signal: AbortSignal, options?: GifOptions): AsyncGenerator<Uint8Array<ArrayBufferLike>, void, unknown>;
@@ -11,6 +11,8 @@ export declare class GifPalette {
11
11
  private readonly colors;
12
12
  private readonly used;
13
13
  private readonly missing;
14
+ private readonly histogram;
15
+ private quantized?;
14
16
  constructor();
15
17
  add(r: number, g: number, b: number, a: number): void;
16
18
  finish(): void;
@@ -1,4 +1,4 @@
1
1
  import type { ImageByteStorage, StoredRgbaImage } from "./png-storage.js";
2
2
  import { type GifOptions } from "./gif-output-parts.js";
3
- /** Multipass GIF encoding with a fixed palette and pull-driven owned subblocks. */
3
+ /** Multipass GIF encoding with a source-derived palette and pull-driven owned subblocks. */
4
4
  export declare function encodeGifFromStorage(image: StoredRgbaImage, storage: ImageByteStorage, signal: AbortSignal, options?: GifOptions): AsyncGenerator<Uint8Array<ArrayBufferLike>, void, unknown>;
@@ -5216,7 +5216,7 @@ function readWebpMetadata(bytes) {
5216
5216
  }
5217
5217
  function createWebpEncoder(img, hasAlpha, options) {
5218
5218
  let { width, height } = img, numPixels = width * height;
5219
- if (!Number.isSafeInteger(width) || width <= 0 || !Number.isSafeInteger(height) || height <= 0 || !Number.isSafeInteger(numPixels))
5219
+ if (!Number.isSafeInteger(width) || width <= 0 || width > 16384 || !Number.isSafeInteger(height) || height <= 0 || height > 16384 || !Number.isSafeInteger(numPixels))
5220
5220
  throw new RangeError("Invalid WebP dimensions");
5221
5221
  let vp8lBuf = new Uint8Array(4096);
5222
5222
  vp8lBuf[0] = 47;
@@ -5394,12 +5394,66 @@ function decodeWebpImage(bytes) {
5394
5394
  };
5395
5395
  }
5396
5396
 
5397
+ // src/codecs/gif-quantization.ts
5398
+ function quantizeGif(histogram, palette) {
5399
+ let bins = [];
5400
+ for (let id = 0; id < 32768; id++) {
5401
+ let at = id * 4, count = histogram[at + 3];
5402
+ count && bins.push([
5403
+ histogram[at] / count,
5404
+ histogram[at + 1] / count,
5405
+ histogram[at + 2] / count,
5406
+ count,
5407
+ id
5408
+ ]);
5409
+ }
5410
+ let box2 = (colors) => {
5411
+ let sum = [0, 0, 0], squares = [0, 0, 0], count = 0;
5412
+ for (let color of colors) {
5413
+ count += color[3];
5414
+ for (let c = 0; c < 3; c++)
5415
+ sum[c] += color[c] * color[3], squares[c] += color[c] ** 2 * color[3];
5416
+ }
5417
+ let variance = squares.map((value, c) => Math.max(0, value - sum[c] ** 2 / count)), axis = variance.indexOf(Math.max(...variance));
5418
+ return { colors, count, sum, axis, error: colors.length > 1 ? variance[axis] : 0 };
5419
+ }, boxes = [box2(bins)];
5420
+ for (; boxes.length < 254; ) {
5421
+ let best = 0;
5422
+ for (let i = 1; i < boxes.length; i++) boxes[i].error > boxes[best].error && (best = i);
5423
+ let current = boxes[best];
5424
+ if (current.error <= 0) break;
5425
+ current.colors.sort((a, b) => a[current.axis] - b[current.axis]);
5426
+ let count = 0, split = 0;
5427
+ do
5428
+ count += current.colors[split++][3];
5429
+ while (count < current.count / 2 && split < current.colors.length - 1);
5430
+ boxes[best] = box2(current.colors.slice(0, split)), boxes.push(box2(current.colors.slice(split)));
5431
+ }
5432
+ palette.fill(0, 0, 762);
5433
+ for (let i = 0; i < boxes.length; i++)
5434
+ for (let c = 0; c < 3; c++)
5435
+ palette[i * 3 + c] = Math.round(boxes[i].sum[c] / boxes[i].count);
5436
+ let indices2 = new Uint8Array(32768);
5437
+ for (let color of bins) {
5438
+ let distance = 1 / 0, nearest = 0;
5439
+ for (let i = 0; i <= boxes.length; i++) {
5440
+ let index = i === boxes.length ? 254 : i, error = 0;
5441
+ for (let c = 0; c < 3; c++) error += (color[c] - palette[index * 3 + c]) ** 2;
5442
+ error < distance && (distance = error, nearest = index);
5443
+ }
5444
+ indices2[color[4]] = nearest;
5445
+ }
5446
+ return indices2;
5447
+ }
5448
+
5397
5449
  // src/codecs/gif-output-parts.ts
5398
5450
  var GifPalette = class {
5399
5451
  bytes = new Uint8Array(768);
5400
5452
  colors = /* @__PURE__ */ new Map();
5401
5453
  used = /* @__PURE__ */ new Set();
5402
5454
  missing = /* @__PURE__ */ new Set();
5455
+ histogram = new Float64Array(32768 * 4);
5456
+ quantized;
5403
5457
  constructor() {
5404
5458
  for (let i = 0; i < 254; i++) {
5405
5459
  let r = Math.round((i >>> 5 & 7) * 255 / 7), g = Math.round((i >>> 2 & 7) * 255 / 7), b = Math.round((i & 3) * 255 / 3);
@@ -5409,6 +5463,10 @@ var GifPalette = class {
5409
5463
  }
5410
5464
  add(r, g, b, a) {
5411
5465
  if (a < 128) return;
5466
+ if (r !== 255 || g !== 255 || b !== 255) {
5467
+ let at = (r >>> 3 << 10 | g >>> 3 << 5 | b >>> 3) * 4;
5468
+ this.histogram[at] += r, this.histogram[at + 1] += g, this.histogram[at + 2] += b, this.histogram[at + 3]++;
5469
+ }
5412
5470
  let key = r << 16 | g << 8 | b, existing = this.colors.get(key);
5413
5471
  existing !== void 0 ? this.used.add(existing) : this.missing.size < 255 && this.missing.add(key);
5414
5472
  }
@@ -5420,10 +5478,13 @@ var GifPalette = class {
5420
5478
  if (probe >= 254) break;
5421
5479
  this.used.add(probe), this.colors.set(key, probe), this.bytes.set([key >>> 16 & 255, key >>> 8 & 255, key & 255], probe * 3);
5422
5480
  }
5423
- }
5481
+ } else
5482
+ this.quantized = quantizeGif(this.histogram, this.bytes);
5424
5483
  }
5425
5484
  index(r, g, b, a) {
5426
5485
  if (a < 128) return 255;
5486
+ if (this.quantized)
5487
+ return r === 255 && g === 255 && b === 255 ? 254 : this.quantized[r >>> 3 << 10 | g >>> 3 << 5 | b >>> 3];
5427
5488
  let exact = this.colors.get(r << 16 | g << 8 | b);
5428
5489
  if (exact !== void 0) return exact;
5429
5490
  let value = r >>> 5 << 5 | g >>> 5 << 2 | b >>> 6;
@@ -6631,7 +6692,7 @@ var Pixels = class {
6631
6692
  async function* encodeWebpFromStorage(image, storage, signal, options) {
6632
6693
  signal.throwIfAborted();
6633
6694
  let length = image.width * image.height * 4;
6634
- if (!Number.isSafeInteger(image.width) || image.width <= 0 || !Number.isSafeInteger(image.height) || image.height <= 0 || !Number.isSafeInteger(image.position) || image.position < 0 || !Number.isSafeInteger(length) || !Number.isSafeInteger(image.position + length))
6695
+ if (!Number.isSafeInteger(image.width) || image.width <= 0 || image.width > 16384 || !Number.isSafeInteger(image.height) || image.height <= 0 || image.height > 16384 || !Number.isSafeInteger(image.position) || image.position < 0 || !Number.isSafeInteger(length) || !Number.isSafeInteger(image.position + length))
6635
6696
  throw new RangeError("Invalid WebP backing allocation");
6636
6697
  let pixels = new Pixels(image, storage, signal), hasAlpha = !1;
6637
6698
  for (let at = 0; at < length && !hasAlpha; at += 4096) {