agglomerative-clustering 3.5.0 → 3.6.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 +1 -1
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +4 -4
- package/dist/index.mjs +5 -5
- package/dist/shared.cjs +88 -49
- package/dist/shared.mjs +84 -46
- package/dist/worker.cjs +3 -3
- package/dist/worker.mjs +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ Most colour quantizers answer one question: *give me k colours*. Ask for a diffe
|
|
|
16
16
|
- **Transparency-aware.** A logo's transparent background never ends up in its palette. GIF and palette PNG output are built in.
|
|
17
17
|
- **Off the main thread.** `agglomerative-clustering/worker` runs the same API in a Web Worker or Node worker thread.
|
|
18
18
|
- **Accurate and fast.** The lowest average error of the six JavaScript quantizers we benchmarked, at about the speed of libimagequant ([benchmarks](#benchmarks)).
|
|
19
|
-
- **Small and portable.** About
|
|
19
|
+
- **Small and portable.** About 82 KB of WebAssembly (40 KB gzipped)<!-- bench/spikes/wasm-size/results-size.txt -->, Node and browsers, TypeScript types, no runtime dependencies, deterministic output.
|
|
20
20
|
|
|
21
21
|
## Install
|
|
22
22
|
|
package/dist/clustering.wasm
CHANGED
|
Binary file
|
package/dist/index.cjs
CHANGED
|
@@ -489,15 +489,15 @@ const checkSize = (pixels, size, name, argument) => {
|
|
|
489
489
|
}
|
|
490
490
|
};
|
|
491
491
|
|
|
492
|
-
// The rgb-as-rgba
|
|
492
|
+
// The bare-image warning (rgb-as-rgba, gray-as-rgba) for an image, decided before load() and given only once the call's own checks have passed,
|
|
493
493
|
// just before it starts work, so the one warning isn't spent on a call that fails anyway (PRINCIPLES "Warnings").
|
|
494
494
|
const noWarning = () => {};
|
|
495
495
|
// A call whose signal is already aborted fails before any work, so it doesn't use the warning up either.
|
|
496
496
|
const warningFor = image => {
|
|
497
|
-
const
|
|
498
|
-
return
|
|
497
|
+
const warning = (0, _shared.bareImageWarning)(image);
|
|
498
|
+
return warning ? signal => {
|
|
499
499
|
if (!(signal && signal.aborted)) {
|
|
500
|
-
(0, _shared.warnOnce)(
|
|
500
|
+
(0, _shared.warnOnce)(warning, image.byteLength);
|
|
501
501
|
}
|
|
502
502
|
} : noWarning;
|
|
503
503
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import createWasmModule from './clustering.js';
|
|
2
|
-
import { parseDither, version, fail, colorSpaceCodes, colorSpaces, colorSpaceName, isUint8Array, isUint8ClampedArray, isArrayBufferLike, isOptionsObject, snapshot, isPalette, isClustering, markPalette, markClustering, setEngine, imageHint, latestVersion, imageFormats, channels, hasAlpha, formatNames, detachedError, describe, readHeader, checkPalette, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, sizeText, tooLarge, sniffImageFormat,
|
|
2
|
+
import { parseDither, version, fail, colorSpaceCodes, colorSpaces, colorSpaceName, isUint8Array, isUint8ClampedArray, isArrayBufferLike, isOptionsObject, snapshot, isPalette, isClustering, markPalette, markClustering, setEngine, imageHint, latestVersion, imageFormats, channels, hasAlpha, formatNames, detachedError, describe, readHeader, checkPalette, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, sizeText, tooLarge, sniffImageFormat, bareImageWarning, warnOnce } from "./shared.mjs";
|
|
3
3
|
|
|
4
4
|
// The library's version, the stored-data format version, and the Palette and Clustering types (see shared.js).
|
|
5
5
|
export { version, Palette, Clustering, errorCodes, isCodedError, ditherMethods, ditherSteps, colorSpaces, imageFormats, alphaModes } from "./shared.mjs";
|
|
@@ -413,15 +413,15 @@ const checkSize = (pixels, size, name, argument) => {
|
|
|
413
413
|
}
|
|
414
414
|
};
|
|
415
415
|
|
|
416
|
-
// The rgb-as-rgba
|
|
416
|
+
// The bare-image warning (rgb-as-rgba, gray-as-rgba) for an image, decided before load() and given only once the call's own checks have passed,
|
|
417
417
|
// just before it starts work, so the one warning isn't spent on a call that fails anyway (PRINCIPLES "Warnings").
|
|
418
418
|
const noWarning = () => {};
|
|
419
419
|
// A call whose signal is already aborted fails before any work, so it doesn't use the warning up either.
|
|
420
420
|
const warningFor = image => {
|
|
421
|
-
const
|
|
422
|
-
return
|
|
421
|
+
const warning = bareImageWarning(image);
|
|
422
|
+
return warning ? signal => {
|
|
423
423
|
if (!(signal && signal.aborted)) {
|
|
424
|
-
warnOnce(
|
|
424
|
+
warnOnce(warning, image.byteLength);
|
|
425
425
|
}
|
|
426
426
|
} : noWarning;
|
|
427
427
|
};
|
package/dist/shared.cjs
CHANGED
|
@@ -6,14 +6,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.Clustering = Clustering;
|
|
7
7
|
exports.MARK = void 0;
|
|
8
8
|
exports.Palette = Palette;
|
|
9
|
-
exports.warnOnce = exports.version = exports.tooLarge = exports.sniffImageFormat = exports.snapshot = exports.sizeText = exports.silenceWarnings = exports.setEngine = exports.revive = exports.resetWarnings = exports.readHeader = exports.parseDither = exports.maxBytes = exports.markPalette = exports.markClustering = exports.mark = exports.looksLikeRgb = exports.latestVersion = exports.isUint8ClampedArray = exports.isUint8Array = exports.isPalette = exports.isOptionsObject = exports.isImageData = exports.isDetached = exports.isCodedError = exports.isClustering = exports.isArrayBufferLike = exports.imageHint = exports.imageFormats = exports.imageDataLike = exports.hasAlpha = exports.formatVersion = exports.formatNames = exports.fail = exports.errorCodes = exports.ditherSteps = exports.ditherMethods = exports.detachedError = exports.describe = exports.colorSpaces = exports.colorSpaceName = exports.colorSpaceCodes = exports.checkPalette = exports.checkImageDataPixels = exports.channels = exports.
|
|
9
|
+
exports.warnOnce = exports.version = exports.tooLarge = exports.sniffImageFormat = exports.snapshot = exports.sizeText = exports.silenceWarnings = exports.setEngine = exports.revive = exports.resetWarnings = exports.readHeader = exports.parseDither = exports.maxBytes = exports.markPalette = exports.markClustering = exports.mark = exports.looksLikeRgb = exports.latestVersion = exports.isUint8ClampedArray = exports.isUint8Array = exports.isPalette = exports.isOptionsObject = exports.isImageData = exports.isDetached = exports.isCodedError = exports.isClustering = exports.isArrayBufferLike = exports.imageHint = exports.imageFormats = exports.imageDataLike = exports.hasAlpha = exports.formatVersion = exports.formatNames = exports.fail = exports.errorCodes = exports.ditherSteps = exports.ditherMethods = exports.detachedError = exports.describe = exports.colorSpaces = exports.colorSpaceName = exports.colorSpaceCodes = exports.checkPalette = exports.checkImageDataPixels = exports.channels = exports.bareLayout = exports.bareImageWarning = exports.alphaModes = void 0;
|
|
10
10
|
// Shared by both entries (index.js and worker.js) and the worker thread: errors, option snapshots, reading a clustering's
|
|
11
11
|
// header, the Palette and Clustering types, and the markers that carry those types across the worker boundary. Imported as
|
|
12
12
|
// './shared.js'; the build rewrites that to './shared.mjs' or './shared.cjs' (scripts/babel-import-extensions.cjs).
|
|
13
13
|
|
|
14
14
|
// The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
|
|
15
15
|
// is built from, src/version.hpp) in step with it. Do not edit it by hand.
|
|
16
|
-
const version = exports.version = '3.
|
|
16
|
+
const version = exports.version = '3.6.0';
|
|
17
17
|
const colorSpaceCodes = exports.colorSpaceCodes = {
|
|
18
18
|
'rgb': 0,
|
|
19
19
|
'oklab': 1
|
|
@@ -371,31 +371,45 @@ const sniffImageFormat = bytes => {
|
|
|
371
371
|
return null;
|
|
372
372
|
};
|
|
373
373
|
|
|
374
|
-
// ----- RGB bytes passed bare
|
|
375
|
-
// The other common way to get a bare array wrong is RGB
|
|
376
|
-
//
|
|
377
|
-
//
|
|
374
|
+
// ----- RGB and grey bytes passed bare -------------------------------------------------------------------------------
|
|
375
|
+
// The other common way to get a bare array wrong is bytes of another layout: RGB (sharp's plain .raw()), or grey and grey
|
|
376
|
+
// with alpha (fast-png's decode() of a grey PNG). Read as RGBA, every pixel takes bytes from its neighbours and one of them
|
|
377
|
+
// as its alpha, and nothing fails while the length divides by 4. They are never reinterpreted (a bare array is RGBA,
|
|
378
|
+
// PRINCIPLES), only reported: a warning in 3.x, an error from 4.0.
|
|
378
379
|
//
|
|
379
|
-
// The
|
|
380
|
-
//
|
|
381
|
-
//
|
|
382
|
-
//
|
|
383
|
-
//
|
|
384
|
-
//
|
|
385
|
-
// (
|
|
380
|
+
// The rules use E(k), the mean change between a byte and the one k before it (bench/spikes/bare-rgb/ and
|
|
381
|
+
// bench/spikes/bare-grey/: no false positives on 2,984 real RGBA images).
|
|
382
|
+
// - RGB, decided first: real RGBA changes least at a step of 4, RGB at a step of 3. A premultiplied white mask,
|
|
383
|
+
// (a, a, a, a), also scores high, so pixels with four equal bytes overrule it. Grey RGB is too smooth for that score,
|
|
384
|
+
// but where it changes its bytes come in equal triples at a step of 3 and not of 4.
|
|
385
|
+
// - Grey and alpha (2 bytes a pixel): smooth at a step of 2 and rough at a step of 1, since grey and alpha bytes alternate.
|
|
386
|
+
// - Grey (1 byte a pixel): smoothest at a step of 1, unless most changing pixels have four equal bytes, which is real
|
|
387
|
+
// RGBA (grey with alpha = grey). The RGB rule goes first because near-grey RGB can look the same; it then keeps the
|
|
388
|
+
// rgb-as-rgba code, whose text covers grey too.
|
|
389
|
+
// Units equal to the one before are skipped by the equal-byte counts, since flat areas read the same either way. It
|
|
390
|
+
// samples 512 windows of 96 bytes (each on a multiple of 12, a pixel under every reading, one in each 512th of the
|
|
391
|
+
// array), so it costs about 0.35 ms at any size. Returns 'rgb', 'grey', 'grey-alpha' or null.
|
|
386
392
|
exports.sniffImageFormat = sniffImageFormat;
|
|
387
|
-
const
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
const
|
|
391
|
-
if (d.length <
|
|
392
|
-
return
|
|
393
|
+
const bareFloor = 192,
|
|
394
|
+
bareWindow = 96,
|
|
395
|
+
bareWindows = 512;
|
|
396
|
+
const bareLayout = d => {
|
|
397
|
+
if (d.length < bareFloor) {
|
|
398
|
+
return null;
|
|
393
399
|
}
|
|
394
|
-
const whole = d.length <=
|
|
395
|
-
const windows = whole ? 1 :
|
|
396
|
-
size = whole ? d.length :
|
|
397
|
-
const step = whole ? 0 : Math.floor((d.length -
|
|
398
|
-
|
|
400
|
+
const whole = d.length <= bareWindow * bareWindows;
|
|
401
|
+
const windows = whole ? 1 : bareWindows,
|
|
402
|
+
size = whole ? d.length : bareWindow;
|
|
403
|
+
const step = whole ? 0 : Math.floor((d.length - bareWindow) / (bareWindows - 1) / 12) * 12;
|
|
404
|
+
// Each window starts at a fixed pseudo-random multiple of 12 within its slot. Evenly spaced starts would alias with
|
|
405
|
+
// the image's rows (768×512 RGBA spaces them exactly one row apart), so every window would see only the left edge,
|
|
406
|
+
// and a cutout's transparent margin would hide the rest of the image.
|
|
407
|
+
const slots = step / 12,
|
|
408
|
+
last = d.length - size;
|
|
409
|
+
const startOf = w => whole ? 0 : Math.min(w * step + (Math.imul(w + 1, 0x9E3779B1) >>> 0) % slots * 12, last - last % 12);
|
|
410
|
+
let e1 = 0,
|
|
411
|
+
e2 = 0,
|
|
412
|
+
e3 = 0,
|
|
399
413
|
e4 = 0,
|
|
400
414
|
n = 0,
|
|
401
415
|
g3 = 0,
|
|
@@ -404,14 +418,18 @@ const looksLikeRgb = d => {
|
|
|
404
418
|
q4 = 0,
|
|
405
419
|
n4 = 0;
|
|
406
420
|
for (let w = 0; w < windows; w++) {
|
|
407
|
-
const start = w
|
|
421
|
+
const start = startOf(w),
|
|
408
422
|
end = start + size;
|
|
409
423
|
for (let i = start + 4; i < end; i++) {
|
|
410
424
|
const v = d[i],
|
|
411
|
-
a = v - d[i -
|
|
412
|
-
b = v - d[i -
|
|
413
|
-
|
|
414
|
-
|
|
425
|
+
a = v - d[i - 1],
|
|
426
|
+
b = v - d[i - 2],
|
|
427
|
+
c = v - d[i - 3],
|
|
428
|
+
e = v - d[i - 4];
|
|
429
|
+
e1 += a < 0 ? -a : a;
|
|
430
|
+
e2 += b < 0 ? -b : b;
|
|
431
|
+
e3 += c < 0 ? -c : c;
|
|
432
|
+
e4 += e < 0 ? -e : e;
|
|
415
433
|
}
|
|
416
434
|
n += size - 4;
|
|
417
435
|
for (let i = start + 3; i + 2 < end; i += 3) {
|
|
@@ -436,17 +454,21 @@ const looksLikeRgb = d => {
|
|
|
436
454
|
}
|
|
437
455
|
}
|
|
438
456
|
}
|
|
439
|
-
const
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
457
|
+
const E1 = e1 / n + 0.05,
|
|
458
|
+
E2 = e2 / n + 0.05,
|
|
459
|
+
E3 = e3 / n + 0.05,
|
|
460
|
+
E4 = e4 / n + 0.05;
|
|
461
|
+
const fewQuads = q4 < 0.5 * Math.max(1, n4);
|
|
462
|
+
const layout = E4 / E3 > 1.25 && fewQuads || g3 > 0.9 * Math.max(1, n3) && g4 < 0.6 * Math.max(1, n4) ? 'rgb' : E4 / E2 > 1.1 && E2 / E1 < 0.5 ? 'grey-alpha' : E4 / E1 > 1.5 && fewQuads ? 'grey' : null;
|
|
463
|
+
if (layout === null) {
|
|
464
|
+
return null;
|
|
443
465
|
}
|
|
444
|
-
// Bytes that mostly rise by one fixed step ((i * k) & 255, the usual fake image in tests) are pixels of
|
|
445
|
-
//
|
|
446
|
-
// so RGBA (the usual input) doesn't pay for it.
|
|
466
|
+
// Bytes that mostly rise by one fixed step ((i * k) & 255, the usual fake image in tests) are pixels of no layout, and
|
|
467
|
+
// score 4/3 as RGB or 4 as grey whatever the step. Counted only for arrays about to be flagged, a second pass over the
|
|
468
|
+
// same windows, so RGBA (the usual input) doesn't pay for it.
|
|
447
469
|
let ramp = 0;
|
|
448
470
|
for (let w = 0; w < windows; w++) {
|
|
449
|
-
const start = w
|
|
471
|
+
const start = startOf(w),
|
|
450
472
|
end = start + size;
|
|
451
473
|
for (let i = start + 4; i < end; i++) {
|
|
452
474
|
const rise = d[i] - d[i - 1] & 255;
|
|
@@ -455,24 +477,36 @@ const looksLikeRgb = d => {
|
|
|
455
477
|
}
|
|
456
478
|
}
|
|
457
479
|
}
|
|
458
|
-
return ramp <= 0.5 * n;
|
|
480
|
+
return ramp <= 0.5 * n ? layout : null;
|
|
459
481
|
};
|
|
482
|
+
exports.bareLayout = bareLayout;
|
|
483
|
+
const looksLikeRgb = d => bareLayout(d) === 'rgb';
|
|
460
484
|
|
|
461
485
|
// Codes already warned in this copy of the library (PRINCIPLES: once per code, per copy).
|
|
462
486
|
exports.looksLikeRgb = looksLikeRgb;
|
|
463
487
|
const warned = new Set();
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
488
|
+
const bareCodes = {
|
|
489
|
+
rgb: 'rgb-as-rgba',
|
|
490
|
+
grey: 'gray-as-rgba',
|
|
491
|
+
'grey-alpha': 'gray-as-rgba'
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// The warning a bare image calls for, as {code, layout}, or null. It skips everything the main entry rejects (palettes
|
|
495
|
+
// and clusterings passed as the image, encoded files, lengths that aren't a multiple of 4), so a call never both warns
|
|
496
|
+
// about its image and rejects it, and both entries share this one order. An array of one layout never takes another's
|
|
497
|
+
// code because its own was already used: it is classified first, and only then is its code checked.
|
|
498
|
+
const bareImageWarning = image => {
|
|
499
|
+
if (warned.has('rgb-as-rgba') && warned.has('gray-as-rgba') || !isUint8Array(image) || isPalette(image) || isClustering(image)) {
|
|
470
500
|
return null;
|
|
471
501
|
}
|
|
472
502
|
if (sniffImageFormat(image) !== null || image.length % 4 !== 0) {
|
|
473
503
|
return null;
|
|
474
504
|
}
|
|
475
|
-
|
|
505
|
+
const layout = bareLayout(image);
|
|
506
|
+
return layout === null || warned.has(bareCodes[layout]) ? null : {
|
|
507
|
+
code: bareCodes[layout],
|
|
508
|
+
layout
|
|
509
|
+
};
|
|
476
510
|
};
|
|
477
511
|
|
|
478
512
|
// The text of each warning. Delivered through process.emitWarning in Node, Bun and Deno (--no-warnings,
|
|
@@ -480,17 +514,22 @@ const bareRgbWarning = image => {
|
|
|
480
514
|
// emitWarning that may print nothing (Cloudflare Workers with nodejs_compat), so they are told apart by
|
|
481
515
|
// navigator.userAgent, as worker.js does; a bundler's `process` polyfill has no emitWarning, and `globalThis.process`
|
|
482
516
|
// keeps Parcel from adding one.
|
|
483
|
-
exports.
|
|
517
|
+
exports.bareImageWarning = bareImageWarning;
|
|
484
518
|
const nodeLike = () => typeof navigator === 'undefined' || /^(Node\.js|Bun|Deno)\//.test(String(navigator.userAgent));
|
|
485
519
|
const warningText = {
|
|
486
|
-
'rgb-as-rgba': byteLength => `This image (${byteLength.toLocaleString('en-US')} bytes) looks like RGB bytes (3 per pixel) passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. If it is RGB, pass { data, format: 'rgb' } (sharp's .raw() gives RGB for an image without alpha unless you add .ensureAlpha()). If it really is RGBA, pass { data, format: 'rgba' } to say so. From 4.0 this is an error. Shown once
|
|
520
|
+
'rgb-as-rgba': byteLength => `This image (${byteLength.toLocaleString('en-US')} bytes) looks like RGB bytes (3 per pixel), or grey, passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. If it is RGB, pass { data, format: 'rgb' } (sharp's .raw() gives RGB for an image without alpha unless you add .ensureAlpha()). If it is grey (fast-png's decode() of a grey PNG), expand it to RGBA; format: 'rgb' would misread it too. If it really is RGBA, pass { data, format: 'rgba' } to say so. From 4.0 this is an error. Shown once.`,
|
|
521
|
+
'gray-as-rgba': (byteLength, layout) => (layout === 'grey-alpha' ? `This image (${byteLength.toLocaleString('en-US')} bytes) looks like grey and alpha bytes (2 per pixel) passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. fast-png's decode() gives this for a grey PNG with alpha.` : `This image (${byteLength.toLocaleString('en-US')} bytes) looks like grey bytes (1 per pixel) passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. fast-png's decode() gives this for a grey PNG (rows below 8 bits stay packed) and for a palette PNG's indices.`) + ` Expand ${layout === 'grey-alpha' ? 'it' : '8-bit grey'} to RGBA, or decode with something that gives RGBA (pngjs, or sharp with .ensureAlpha()). There is no grey format to pass. If it really is RGBA, pass { data, format: 'rgba' } to say so. From 4.0 this is an error. Shown once.`
|
|
487
522
|
};
|
|
488
|
-
|
|
523
|
+
// `warning` is what bareImageWarning returned: {code, layout}.
|
|
524
|
+
const warnOnce = ({
|
|
525
|
+
code,
|
|
526
|
+
layout
|
|
527
|
+
}, byteLength) => {
|
|
489
528
|
if (warned.has(code)) {
|
|
490
529
|
return;
|
|
491
530
|
}
|
|
492
531
|
warned.add(code);
|
|
493
|
-
const message = warningText[code](byteLength);
|
|
532
|
+
const message = warningText[code](byteLength, layout);
|
|
494
533
|
const runtime = globalThis.process;
|
|
495
534
|
if (runtime && typeof runtime.emitWarning === 'function' && nodeLike()) {
|
|
496
535
|
runtime.emitWarning(message, {
|
package/dist/shared.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
// The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
|
|
6
6
|
// is built from, src/version.hpp) in step with it. Do not edit it by hand.
|
|
7
|
-
export const version = '3.
|
|
7
|
+
export const version = '3.6.0';
|
|
8
8
|
export const colorSpaceCodes = {
|
|
9
9
|
'rgb': 0,
|
|
10
10
|
'oklab': 1
|
|
@@ -340,30 +340,44 @@ export const sniffImageFormat = bytes => {
|
|
|
340
340
|
return null;
|
|
341
341
|
};
|
|
342
342
|
|
|
343
|
-
// ----- RGB bytes passed bare
|
|
344
|
-
// The other common way to get a bare array wrong is RGB
|
|
345
|
-
//
|
|
346
|
-
//
|
|
343
|
+
// ----- RGB and grey bytes passed bare -------------------------------------------------------------------------------
|
|
344
|
+
// The other common way to get a bare array wrong is bytes of another layout: RGB (sharp's plain .raw()), or grey and grey
|
|
345
|
+
// with alpha (fast-png's decode() of a grey PNG). Read as RGBA, every pixel takes bytes from its neighbours and one of them
|
|
346
|
+
// as its alpha, and nothing fails while the length divides by 4. They are never reinterpreted (a bare array is RGBA,
|
|
347
|
+
// PRINCIPLES), only reported: a warning in 3.x, an error from 4.0.
|
|
347
348
|
//
|
|
348
|
-
// The
|
|
349
|
-
//
|
|
350
|
-
//
|
|
351
|
-
//
|
|
352
|
-
//
|
|
353
|
-
//
|
|
354
|
-
// (
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
349
|
+
// The rules use E(k), the mean change between a byte and the one k before it (bench/spikes/bare-rgb/ and
|
|
350
|
+
// bench/spikes/bare-grey/: no false positives on 2,984 real RGBA images).
|
|
351
|
+
// - RGB, decided first: real RGBA changes least at a step of 4, RGB at a step of 3. A premultiplied white mask,
|
|
352
|
+
// (a, a, a, a), also scores high, so pixels with four equal bytes overrule it. Grey RGB is too smooth for that score,
|
|
353
|
+
// but where it changes its bytes come in equal triples at a step of 3 and not of 4.
|
|
354
|
+
// - Grey and alpha (2 bytes a pixel): smooth at a step of 2 and rough at a step of 1, since grey and alpha bytes alternate.
|
|
355
|
+
// - Grey (1 byte a pixel): smoothest at a step of 1, unless most changing pixels have four equal bytes, which is real
|
|
356
|
+
// RGBA (grey with alpha = grey). The RGB rule goes first because near-grey RGB can look the same; it then keeps the
|
|
357
|
+
// rgb-as-rgba code, whose text covers grey too.
|
|
358
|
+
// Units equal to the one before are skipped by the equal-byte counts, since flat areas read the same either way. It
|
|
359
|
+
// samples 512 windows of 96 bytes (each on a multiple of 12, a pixel under every reading, one in each 512th of the
|
|
360
|
+
// array), so it costs about 0.35 ms at any size. Returns 'rgb', 'grey', 'grey-alpha' or null.
|
|
361
|
+
const bareFloor = 192,
|
|
362
|
+
bareWindow = 96,
|
|
363
|
+
bareWindows = 512;
|
|
364
|
+
export const bareLayout = d => {
|
|
365
|
+
if (d.length < bareFloor) {
|
|
366
|
+
return null;
|
|
361
367
|
}
|
|
362
|
-
const whole = d.length <=
|
|
363
|
-
const windows = whole ? 1 :
|
|
364
|
-
size = whole ? d.length :
|
|
365
|
-
const step = whole ? 0 : Math.floor((d.length -
|
|
366
|
-
|
|
368
|
+
const whole = d.length <= bareWindow * bareWindows;
|
|
369
|
+
const windows = whole ? 1 : bareWindows,
|
|
370
|
+
size = whole ? d.length : bareWindow;
|
|
371
|
+
const step = whole ? 0 : Math.floor((d.length - bareWindow) / (bareWindows - 1) / 12) * 12;
|
|
372
|
+
// Each window starts at a fixed pseudo-random multiple of 12 within its slot. Evenly spaced starts would alias with
|
|
373
|
+
// the image's rows (768×512 RGBA spaces them exactly one row apart), so every window would see only the left edge,
|
|
374
|
+
// and a cutout's transparent margin would hide the rest of the image.
|
|
375
|
+
const slots = step / 12,
|
|
376
|
+
last = d.length - size;
|
|
377
|
+
const startOf = w => whole ? 0 : Math.min(w * step + (Math.imul(w + 1, 0x9E3779B1) >>> 0) % slots * 12, last - last % 12);
|
|
378
|
+
let e1 = 0,
|
|
379
|
+
e2 = 0,
|
|
380
|
+
e3 = 0,
|
|
367
381
|
e4 = 0,
|
|
368
382
|
n = 0,
|
|
369
383
|
g3 = 0,
|
|
@@ -372,14 +386,18 @@ export const looksLikeRgb = d => {
|
|
|
372
386
|
q4 = 0,
|
|
373
387
|
n4 = 0;
|
|
374
388
|
for (let w = 0; w < windows; w++) {
|
|
375
|
-
const start = w
|
|
389
|
+
const start = startOf(w),
|
|
376
390
|
end = start + size;
|
|
377
391
|
for (let i = start + 4; i < end; i++) {
|
|
378
392
|
const v = d[i],
|
|
379
|
-
a = v - d[i -
|
|
380
|
-
b = v - d[i -
|
|
381
|
-
|
|
382
|
-
|
|
393
|
+
a = v - d[i - 1],
|
|
394
|
+
b = v - d[i - 2],
|
|
395
|
+
c = v - d[i - 3],
|
|
396
|
+
e = v - d[i - 4];
|
|
397
|
+
e1 += a < 0 ? -a : a;
|
|
398
|
+
e2 += b < 0 ? -b : b;
|
|
399
|
+
e3 += c < 0 ? -c : c;
|
|
400
|
+
e4 += e < 0 ? -e : e;
|
|
383
401
|
}
|
|
384
402
|
n += size - 4;
|
|
385
403
|
for (let i = start + 3; i + 2 < end; i += 3) {
|
|
@@ -404,17 +422,21 @@ export const looksLikeRgb = d => {
|
|
|
404
422
|
}
|
|
405
423
|
}
|
|
406
424
|
}
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
425
|
+
const E1 = e1 / n + 0.05,
|
|
426
|
+
E2 = e2 / n + 0.05,
|
|
427
|
+
E3 = e3 / n + 0.05,
|
|
428
|
+
E4 = e4 / n + 0.05;
|
|
429
|
+
const fewQuads = q4 < 0.5 * Math.max(1, n4);
|
|
430
|
+
const layout = E4 / E3 > 1.25 && fewQuads || g3 > 0.9 * Math.max(1, n3) && g4 < 0.6 * Math.max(1, n4) ? 'rgb' : E4 / E2 > 1.1 && E2 / E1 < 0.5 ? 'grey-alpha' : E4 / E1 > 1.5 && fewQuads ? 'grey' : null;
|
|
431
|
+
if (layout === null) {
|
|
432
|
+
return null;
|
|
411
433
|
}
|
|
412
|
-
// Bytes that mostly rise by one fixed step ((i * k) & 255, the usual fake image in tests) are pixels of
|
|
413
|
-
//
|
|
414
|
-
// so RGBA (the usual input) doesn't pay for it.
|
|
434
|
+
// Bytes that mostly rise by one fixed step ((i * k) & 255, the usual fake image in tests) are pixels of no layout, and
|
|
435
|
+
// score 4/3 as RGB or 4 as grey whatever the step. Counted only for arrays about to be flagged, a second pass over the
|
|
436
|
+
// same windows, so RGBA (the usual input) doesn't pay for it.
|
|
415
437
|
let ramp = 0;
|
|
416
438
|
for (let w = 0; w < windows; w++) {
|
|
417
|
-
const start = w
|
|
439
|
+
const start = startOf(w),
|
|
418
440
|
end = start + size;
|
|
419
441
|
for (let i = start + 4; i < end; i++) {
|
|
420
442
|
const rise = d[i] - d[i - 1] & 255;
|
|
@@ -423,23 +445,34 @@ export const looksLikeRgb = d => {
|
|
|
423
445
|
}
|
|
424
446
|
}
|
|
425
447
|
}
|
|
426
|
-
return ramp <= 0.5 * n;
|
|
448
|
+
return ramp <= 0.5 * n ? layout : null;
|
|
427
449
|
};
|
|
450
|
+
export const looksLikeRgb = d => bareLayout(d) === 'rgb';
|
|
428
451
|
|
|
429
452
|
// Codes already warned in this copy of the library (PRINCIPLES: once per code, per copy).
|
|
430
453
|
const warned = new Set();
|
|
454
|
+
const bareCodes = {
|
|
455
|
+
rgb: 'rgb-as-rgba',
|
|
456
|
+
grey: 'gray-as-rgba',
|
|
457
|
+
'grey-alpha': 'gray-as-rgba'
|
|
458
|
+
};
|
|
431
459
|
|
|
432
|
-
// The warning a bare image calls for, or null. It skips everything the main entry rejects (palettes
|
|
433
|
-
// passed as the image, encoded files, lengths that aren't a multiple of 4), so a call never both warns
|
|
434
|
-
// rejects it, and both entries share this one order.
|
|
435
|
-
|
|
436
|
-
|
|
460
|
+
// The warning a bare image calls for, as {code, layout}, or null. It skips everything the main entry rejects (palettes
|
|
461
|
+
// and clusterings passed as the image, encoded files, lengths that aren't a multiple of 4), so a call never both warns
|
|
462
|
+
// about its image and rejects it, and both entries share this one order. An array of one layout never takes another's
|
|
463
|
+
// code because its own was already used: it is classified first, and only then is its code checked.
|
|
464
|
+
export const bareImageWarning = image => {
|
|
465
|
+
if (warned.has('rgb-as-rgba') && warned.has('gray-as-rgba') || !isUint8Array(image) || isPalette(image) || isClustering(image)) {
|
|
437
466
|
return null;
|
|
438
467
|
}
|
|
439
468
|
if (sniffImageFormat(image) !== null || image.length % 4 !== 0) {
|
|
440
469
|
return null;
|
|
441
470
|
}
|
|
442
|
-
|
|
471
|
+
const layout = bareLayout(image);
|
|
472
|
+
return layout === null || warned.has(bareCodes[layout]) ? null : {
|
|
473
|
+
code: bareCodes[layout],
|
|
474
|
+
layout
|
|
475
|
+
};
|
|
443
476
|
};
|
|
444
477
|
|
|
445
478
|
// The text of each warning. Delivered through process.emitWarning in Node, Bun and Deno (--no-warnings,
|
|
@@ -449,14 +482,19 @@ export const bareRgbWarning = image => {
|
|
|
449
482
|
// keeps Parcel from adding one.
|
|
450
483
|
const nodeLike = () => typeof navigator === 'undefined' || /^(Node\.js|Bun|Deno)\//.test(String(navigator.userAgent));
|
|
451
484
|
const warningText = {
|
|
452
|
-
'rgb-as-rgba': byteLength => `This image (${byteLength.toLocaleString('en-US')} bytes) looks like RGB bytes (3 per pixel) passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. If it is RGB, pass { data, format: 'rgb' } (sharp's .raw() gives RGB for an image without alpha unless you add .ensureAlpha()). If it really is RGBA, pass { data, format: 'rgba' } to say so. From 4.0 this is an error. Shown once
|
|
485
|
+
'rgb-as-rgba': byteLength => `This image (${byteLength.toLocaleString('en-US')} bytes) looks like RGB bytes (3 per pixel), or grey, passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. If it is RGB, pass { data, format: 'rgb' } (sharp's .raw() gives RGB for an image without alpha unless you add .ensureAlpha()). If it is grey (fast-png's decode() of a grey PNG), expand it to RGBA; format: 'rgb' would misread it too. If it really is RGBA, pass { data, format: 'rgba' } to say so. From 4.0 this is an error. Shown once.`,
|
|
486
|
+
'gray-as-rgba': (byteLength, layout) => (layout === 'grey-alpha' ? `This image (${byteLength.toLocaleString('en-US')} bytes) looks like grey and alpha bytes (2 per pixel) passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. fast-png's decode() gives this for a grey PNG with alpha.` : `This image (${byteLength.toLocaleString('en-US')} bytes) looks like grey bytes (1 per pixel) passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. fast-png's decode() gives this for a grey PNG (rows below 8 bits stay packed) and for a palette PNG's indices.`) + ` Expand ${layout === 'grey-alpha' ? 'it' : '8-bit grey'} to RGBA, or decode with something that gives RGBA (pngjs, or sharp with .ensureAlpha()). There is no grey format to pass. If it really is RGBA, pass { data, format: 'rgba' } to say so. From 4.0 this is an error. Shown once.`
|
|
453
487
|
};
|
|
454
|
-
|
|
488
|
+
// `warning` is what bareImageWarning returned: {code, layout}.
|
|
489
|
+
export const warnOnce = ({
|
|
490
|
+
code,
|
|
491
|
+
layout
|
|
492
|
+
}, byteLength) => {
|
|
455
493
|
if (warned.has(code)) {
|
|
456
494
|
return;
|
|
457
495
|
}
|
|
458
496
|
warned.add(code);
|
|
459
|
-
const message = warningText[code](byteLength);
|
|
497
|
+
const message = warningText[code](byteLength, layout);
|
|
460
498
|
const runtime = globalThis.process;
|
|
461
499
|
if (runtime && typeof runtime.emitWarning === 'function' && nodeLike()) {
|
|
462
500
|
runtime.emitWarning(message, {
|
package/dist/worker.cjs
CHANGED
|
@@ -907,10 +907,10 @@ const makeWorker = (options, allowScript) => {
|
|
|
907
907
|
}
|
|
908
908
|
throw abortReason(signal);
|
|
909
909
|
}
|
|
910
|
-
// The
|
|
910
|
+
// The bare-image warning is decided here, on the caller's side and before a transfer can empty the
|
|
911
911
|
// array, and given once the call succeeds: the thread checks the other arguments, and its own copy is
|
|
912
912
|
// silenced (PRINCIPLES "Warnings").
|
|
913
|
-
const warning = (0, _shared.
|
|
913
|
+
const warning = (0, _shared.bareImageWarning)(image);
|
|
914
914
|
const byteLength = warning ? image.byteLength : 0;
|
|
915
915
|
let sent = send(image);
|
|
916
916
|
if (!ready()) {
|
|
@@ -973,7 +973,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
973
973
|
throw abortReason(signal);
|
|
974
974
|
}
|
|
975
975
|
const id = nextHandle++;
|
|
976
|
-
const warning = (0, _shared.
|
|
976
|
+
const warning = (0, _shared.bareImageWarning)(image);
|
|
977
977
|
const byteLength = warning ? image.byteLength : 0;
|
|
978
978
|
const sent = ready() ? send(image) : take(send(image));
|
|
979
979
|
// Counted from before the call, so an abandoned worker isn't stopped between the call settling and the handle
|
package/dist/worker.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// handle, so re-cutting and re-mapping it copies nothing across. The functions exported directly use one shared
|
|
5
5
|
// worker, started on first use.
|
|
6
6
|
|
|
7
|
-
import { fail, isCodedError, detachedError, isOptionsObject, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge,
|
|
7
|
+
import { fail, isCodedError, detachedError, isOptionsObject, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge, bareImageWarning, warnOnce } from "./shared.mjs";
|
|
8
8
|
|
|
9
9
|
// The same version, types and data-format version as the main entry (one implementation, in shared.js).
|
|
10
10
|
export { version, Palette, Clustering, errorCodes, isCodedError, ditherMethods, ditherSteps, colorSpaces, imageFormats, alphaModes } from "./shared.mjs";
|
|
@@ -838,10 +838,10 @@ const makeWorker = (options, allowScript) => {
|
|
|
838
838
|
}
|
|
839
839
|
throw abortReason(signal);
|
|
840
840
|
}
|
|
841
|
-
// The
|
|
841
|
+
// The bare-image warning is decided here, on the caller's side and before a transfer can empty the
|
|
842
842
|
// array, and given once the call succeeds: the thread checks the other arguments, and its own copy is
|
|
843
843
|
// silenced (PRINCIPLES "Warnings").
|
|
844
|
-
const warning =
|
|
844
|
+
const warning = bareImageWarning(image);
|
|
845
845
|
const byteLength = warning ? image.byteLength : 0;
|
|
846
846
|
let sent = send(image);
|
|
847
847
|
if (!ready()) {
|
|
@@ -904,7 +904,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
904
904
|
throw abortReason(signal);
|
|
905
905
|
}
|
|
906
906
|
const id = nextHandle++;
|
|
907
|
-
const warning =
|
|
907
|
+
const warning = bareImageWarning(image);
|
|
908
908
|
const byteLength = warning ? image.byteLength : 0;
|
|
909
909
|
const sent = ready() ? send(image) : take(send(image));
|
|
910
910
|
// Counted from before the call, so an abandoned worker isn't stopped between the call settling and the handle
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agglomerative-clustering",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "Colour palette extraction and image quantization with exact Ward clustering, in WebAssembly. Cluster an image once, then cut it to a palette of any size.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Rohan Fredriksson",
|