agglomerative-clustering 4.0.0-beta.1 → 4.0.0-beta.3
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 +5 -5
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +37 -63
- package/dist/index.d.cts +15 -76
- package/dist/index.d.ts +15 -76
- package/dist/index.mjs +36 -57
- package/dist/shared.cjs +73 -79
- package/dist/shared.mjs +68 -73
- package/dist/worker-thread.mjs +15 -18
- package/dist/worker.cjs +139 -84
- package/dist/worker.d.cts +0 -32
- package/dist/worker.d.ts +0 -32
- package/dist/worker.mjs +138 -78
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,8 @@ export type ImageFormat = 'rgb' | 'rgba' | 'bgra' | 'rgbx' | 'bgrx';
|
|
|
17
17
|
* newer format version or one using a colour space it doesn't know, or an older one that lacks what was asked for),
|
|
18
18
|
* `out-of-memory`, `wasm-load-failed` (the WebAssembly module could not be loaded or compiled: a missing or blocked file, a
|
|
19
19
|
* browser without SIMD, or a `.wasm` from another version of the library), and in the worker entry
|
|
20
|
-
* `worker-terminated`, `worker-failed`
|
|
20
|
+
* `worker-terminated`, `worker-failed` (the worker could not start, crashed, or runs a script from another version of the
|
|
21
|
+
* package) and `invalid-handle`.
|
|
21
22
|
*/
|
|
22
23
|
export type ErrorCode = 'invalid-argument' | 'invalid-clustering' | 'unsupported-version' | 'out-of-memory' | 'wasm-load-failed'
|
|
23
24
|
| 'worker-terminated' | 'worker-failed' | 'invalid-handle';
|
|
@@ -165,7 +166,7 @@ export class Palette extends Uint8Array {
|
|
|
165
166
|
* `Uint8Array`, which functions still accept at run time; `new Clustering(bytes)` gives them the type back (TypeScript
|
|
166
167
|
* wants it) and checks they are a clustering.
|
|
167
168
|
*
|
|
168
|
-
* It has methods for the cluster-once workflow: `clustering.getPalette(k)`
|
|
169
|
+
* It has methods for the cluster-once workflow: `clustering.getPalette(k)` cuts a palette from it, and
|
|
169
170
|
* so on. They run on the calling thread with the main entry, so in a worker-only app they compile but reject with
|
|
170
171
|
* `wasm-load-failed`. They exist only on values from this library or `new Clustering(bytes)`: slices and clones are plain
|
|
171
172
|
* bytes, and a cast (`bytes as Clustering`) or a `structuredClone` keeps the type without them: wrap those with
|
|
@@ -274,7 +275,7 @@ export interface ClusteringOptions extends ProgressOptions {
|
|
|
274
275
|
* PNG), and it gives the same palette everywhere: `buildPalette(image, k, { transparent })` is the palette
|
|
275
276
|
* `quantizeToIndices(image, k, { transparent })` uses, apart from that function's extra transparent entry.
|
|
276
277
|
* Cluster with it to get the palette you will map with (see `IndexOptions.transparent`). The cutoff is recorded in the
|
|
277
|
-
* clustering (see `ClusteringInfo.transparent`), and `
|
|
278
|
+
* clustering (see `ClusteringInfo.transparent`), and `clustering.quantizeToIndices(image, k)` uses it unless you name one.
|
|
278
279
|
* It replaces `alpha` (with a cutoff, pixels count whole or not at all), so giving both is an `invalid-argument` error.
|
|
279
280
|
*/
|
|
280
281
|
transparent?: boolean | number | undefined;
|
|
@@ -305,11 +306,11 @@ export type DitherOptions =
|
|
|
305
306
|
| { method?: 'floyd-steinberg' | 'atkinson' | undefined; strength?: number | undefined; steps?: undefined }
|
|
306
307
|
| { method: 'ordered'; strength?: number | undefined; steps?: DitherSteps | undefined };
|
|
307
308
|
|
|
308
|
-
/** Options for mapping pixels to a palette: `
|
|
309
|
+
/** Options for mapping pixels to a palette: `clustering.quantize`, `quantizeWithPalette`, and the mapping half of `quantize` and `quantizeToIndices`. With `signal` or `onProgress`, mapping runs in slices, as clustering does. */
|
|
309
310
|
export interface MappingOptions extends ProgressOptions {
|
|
310
311
|
/**
|
|
311
312
|
* The space pixels are matched to their nearest palette colour in. The default is the space the colours were made in:
|
|
312
|
-
* the clustering's in `
|
|
313
|
+
* the clustering's in `clustering.quantize`, a `Palette`'s own in `quantizeWithPalette` and
|
|
313
314
|
* `quantizeToIndicesWithPalette`; `'rgb'` for plain bytes, and in `quantize` and `quantizeToIndices`, which match in the
|
|
314
315
|
* space they cluster in.
|
|
315
316
|
*/
|
|
@@ -343,14 +344,14 @@ export interface IndexOptions {
|
|
|
343
344
|
* this function builds the palette itself it also leaves the pixels below the cutoff out of clustering (see
|
|
344
345
|
* `ClusteringOptions.transparent`), so its palette is `buildPalette(image, k - 1, { transparent })`'s. With a palette
|
|
345
346
|
* you supply (`quantizeToIndicesWithPalette`) there is no `k`: the entry is added to your palette as it is. In
|
|
346
|
-
* `
|
|
347
|
+
* `clustering.quantizeToIndices` the default is the cutoff the clustering was made with.
|
|
347
348
|
*/
|
|
348
349
|
transparent?: boolean | number | undefined;
|
|
349
350
|
}
|
|
350
351
|
|
|
351
352
|
/** The options of `quantizeToIndices`: clustering, mapping and indexed-output options. */
|
|
352
353
|
export type QuantizeIndexOptions = QuantizeOptions & IndexOptions;
|
|
353
|
-
/** The options of `
|
|
354
|
+
/** The options of `clustering.quantizeToIndices` and `quantizeToIndicesWithPalette`: mapping and indexed-output options. */
|
|
354
355
|
export type MappingIndexOptions = MappingOptions & IndexOptions;
|
|
355
356
|
|
|
356
357
|
interface IndexedImageFields {
|
|
@@ -401,10 +402,10 @@ export interface InitOptions {
|
|
|
401
402
|
export const version: string;
|
|
402
403
|
|
|
403
404
|
/**
|
|
404
|
-
* The version of the data this library stores: the format of the clusterings `buildClustering` writes (`getClusteringInfo`
|
|
405
|
+
* The version of the data this library stores: the format of the clusterings `buildClustering` writes (`clustering.getClusteringInfo()`
|
|
405
406
|
* reports a clustering's own as its `formatVersion`). One integer for the library's stored data, unrelated to `version`: it
|
|
406
407
|
* changes only when the stored format does, so a new release that keeps the format keeps this number, and a minor release
|
|
407
|
-
* may raise it. Every earlier format from 2 on stays readable (see `getClusteringInfo` for what 2.x clusterings lack).
|
|
408
|
+
* may raise it. Every earlier format from 2 on stays readable (see `clustering.getClusteringInfo()` for what 2.x clusterings lack).
|
|
408
409
|
*/
|
|
409
410
|
export const formatVersion: number;
|
|
410
411
|
|
|
@@ -417,12 +418,10 @@ export function init(options?: InitOptions): Promise<void>;
|
|
|
417
418
|
|
|
418
419
|
/**
|
|
419
420
|
* Builds the colour hierarchy for an image: the expensive step, done once. The result is a `Clustering` (opaque bytes) you can
|
|
420
|
-
* cache or store next to the image. Cut it to a palette of any size with `
|
|
421
|
-
* with `
|
|
421
|
+
* cache or store next to the image. Cut it to a palette of any size with `clustering.getPalette(k)`, or map pixels
|
|
422
|
+
* with `clustering.quantize(image, k)`. An image with no visible pixels gives a clustering that cuts to an empty palette.
|
|
422
423
|
*/
|
|
423
424
|
export function buildClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
|
|
424
|
-
/** @deprecated Renamed {@link buildClustering} in 3.4.0 (the same function). Removed in 4.0. */
|
|
425
|
-
export function getClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
|
|
426
425
|
|
|
427
426
|
/**
|
|
428
427
|
* A palette of at most `k` colours for `image` (fewer if it has fewer distinct colours), as flat RGB bytes in split
|
|
@@ -433,20 +432,6 @@ export function getClustering(image: ImageLike, options?: ClusteringOptions): Pr
|
|
|
433
432
|
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
434
433
|
*/
|
|
435
434
|
export function buildPalette(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Palette>;
|
|
436
|
-
/** @deprecated Renamed {@link buildPalette} in 3.4.0 (the same function). Removed in 4.0. */
|
|
437
|
-
export function getPalette(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Palette>;
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* Cuts a clustering to at most `k` colours, in split order: index 0 is the dominant colour at two colours and keeps its
|
|
441
|
-
* place as the palette grows (it is not always the heaviest entry of a larger one), and the palette for
|
|
442
|
-
* `k + 1` is the palette for `k` with one entry refined and one colour appended.
|
|
443
|
-
*
|
|
444
|
-
* @param k The most palette entries in the result (`k` means this in every function). A flat palette has no transparent
|
|
445
|
-
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
446
|
-
*
|
|
447
|
-
* @deprecated Use `clustering.getPalette(k)`; off the main thread, `(await worker.load(clustering)).getPalette(k)` (`worker.load` is in the worker entry). Deprecated in 3.8, removed in 4.0; no warning at run time.
|
|
448
|
-
*/
|
|
449
|
-
export function getPaletteFromClustering(clustering: Clustering, k: number): Promise<Palette>;
|
|
450
435
|
|
|
451
436
|
/**
|
|
452
437
|
* A palette with how much of the image each of its colours covers, for swatches with percentages. The three arrays come
|
|
@@ -473,21 +458,8 @@ export interface Swatches {
|
|
|
473
458
|
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
474
459
|
*/
|
|
475
460
|
export function buildSwatches(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Swatches>;
|
|
476
|
-
/** @deprecated Renamed {@link buildSwatches} in 3.4.0 (the same function). Removed in 4.0. */
|
|
477
|
-
export function getSwatches(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Swatches>;
|
|
478
461
|
|
|
479
|
-
/**
|
|
480
|
-
* The same as `buildSwatches` from a clustering you already have, so trying another `k` costs well under a millisecond.
|
|
481
|
-
* Needs a clustering from 3.x; a 2.x clustering has no weights and is rejected.
|
|
482
|
-
*
|
|
483
|
-
* @param k The most palette entries in the result (`k` means this in every function). A flat palette has no transparent
|
|
484
|
-
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
485
|
-
*
|
|
486
|
-
* @deprecated Use `clustering.getSwatches(k)`; off the main thread, `(await worker.load(clustering)).getSwatches(k)` (`worker.load` is in the worker entry). Deprecated in 3.8, removed in 4.0; no warning at run time.
|
|
487
|
-
*/
|
|
488
|
-
export function getSwatchesFromClustering(clustering: Clustering, k: number): Promise<Swatches>;
|
|
489
|
-
|
|
490
|
-
/** What a clustering holds (`getClusteringInfo`). */
|
|
462
|
+
/** What a clustering holds (`clustering.getClusteringInfo()`). */
|
|
491
463
|
export interface ClusteringInfo {
|
|
492
464
|
/**
|
|
493
465
|
* The format of the clustering's bytes: the data version (compare it with the exported `formatVersion`, the one this
|
|
@@ -499,7 +471,7 @@ export interface ClusteringInfo {
|
|
|
499
471
|
precision: number;
|
|
500
472
|
/**
|
|
501
473
|
* The alpha cutoff (1-255) the clustering was made with (`transparent` in `ClusteringOptions`), or `false` for none. It is
|
|
502
|
-
* the default `transparent` of `
|
|
474
|
+
* the default `transparent` of `clustering.quantizeToIndices`, so clustering and mapping agree unless you name another.
|
|
503
475
|
*/
|
|
504
476
|
transparent: false | number;
|
|
505
477
|
/** The largest palette it can be cut to: its number of distinct clustered colours (fewer than the image's own when `precision` is below 8). */
|
|
@@ -523,15 +495,6 @@ export interface ClusteringInfo {
|
|
|
523
495
|
explained: Float64Array;
|
|
524
496
|
}
|
|
525
497
|
|
|
526
|
-
/**
|
|
527
|
-
* Reads what a clustering holds, including its error curve for choosing a palette size. Needs a clustering from 3.x: a 2.x
|
|
528
|
-
* clustering recorded no weights, so it is rejected with `unsupported-version` (as `clustering.getSwatches(k)` rejects it);
|
|
529
|
-
* the palette and mapping functions still read it.
|
|
530
|
-
*
|
|
531
|
-
* @deprecated Use `clustering.getClusteringInfo()`; off the main thread, `(await worker.load(clustering)).getClusteringInfo()` (`worker.load` is in the worker entry). Deprecated in 3.8, removed in 4.0; no warning at run time.
|
|
532
|
-
*/
|
|
533
|
-
export function getClusteringInfo(clustering: Clustering): Promise<ClusteringInfo>;
|
|
534
|
-
|
|
535
498
|
/**
|
|
536
499
|
* Reduces `image` to at most `k` colours and returns the mapped image as the same kind of thing you gave: a `Uint8Array`
|
|
537
500
|
* for a `Uint8Array`, `ImageData` for `ImageData`, an object with the same `format` for a `RawImage` (`ImageResult`; a
|
|
@@ -542,22 +505,10 @@ export function getClusteringInfo(clustering: Clustering): Promise<ClusteringInf
|
|
|
542
505
|
*/
|
|
543
506
|
export function quantize<I extends ImageLike>(image: I, k: number, options?: QuantizeOptions): Promise<ImageResult<I>>;
|
|
544
507
|
|
|
545
|
-
/**
|
|
546
|
-
* Maps `image` to the `k`-colour cut of a clustering you already have, so trying another `k` or another image mapping
|
|
547
|
-
* costs milliseconds. Matches in the clustering's colour space unless `colorSpace` is given. Returns the same kind of
|
|
548
|
-
* thing as `quantize`.
|
|
549
|
-
*
|
|
550
|
-
* @param k The most palette entries in the result (`k` means this in every function). A flat palette has no transparent
|
|
551
|
-
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
552
|
-
*
|
|
553
|
-
* @deprecated Use `clustering.quantize(image, k, options)`; off the main thread, `(await worker.load(clustering)).quantize(image, k, options)` (`worker.load` is in the worker entry). Deprecated in 3.8, removed in 4.0; no warning at run time.
|
|
554
|
-
*/
|
|
555
|
-
export function quantizeFromClustering<I extends ImageLike>(image: I, clustering: Clustering, k: number, options?: MappingOptions): Promise<ImageResult<I>>;
|
|
556
|
-
|
|
557
508
|
/**
|
|
558
509
|
* Maps each pixel of `image` to its nearest colour in a palette you supply (an edited swatch, a brand palette, one cut
|
|
559
510
|
* from a clustering). Pixels are matched in the palette's own colour space if it is a `Palette` (one this library made),
|
|
560
|
-
* so an OKLab palette maps the way `
|
|
511
|
+
* so an OKLab palette maps the way `clustering.quantize` does; plain bytes carry no space and are matched in RGB. Name a
|
|
561
512
|
* `colorSpace` to override either. Returns the same kind of thing as `quantize`.
|
|
562
513
|
*/
|
|
563
514
|
export function quantizeWithPalette<I extends ImageLike>(image: I, palette: PaletteLike, options?: MappingOptions): Promise<ImageResult<I>>;
|
|
@@ -574,18 +525,6 @@ export function quantizeWithPalette<I extends ImageLike>(image: I, palette: Pale
|
|
|
574
525
|
*/
|
|
575
526
|
export function quantizeToIndices(image: ImageLike, k: number, options?: QuantizeIndexOptions): Promise<IndexedImage>;
|
|
576
527
|
|
|
577
|
-
/**
|
|
578
|
-
* Like `quantizeFromClustering`, but returns palette indices. The palette is `clustering.getPalette(k)`'s.
|
|
579
|
-
*
|
|
580
|
-
* @param k The most palette entries in the result (`k` means this in every function), counting the transparent entry
|
|
581
|
-
* if there is one, so `256` is the most a GIF or palette PNG holds. Fewer come back when the image has fewer distinct colours.
|
|
582
|
-
* A colour is kept besides the transparent entry whenever the image has visible pixels, so with `transparent`,
|
|
583
|
-
* `k = 1` can give 2 entries; an image with no visible pixels gives just the transparent entry.
|
|
584
|
-
*
|
|
585
|
-
* @deprecated Use `clustering.quantizeToIndices(image, k, options)`; off the main thread, `(await worker.load(clustering)).quantizeToIndices(image, k, options)` (`worker.load` is in the worker entry). Deprecated in 3.8, removed in 4.0; no warning at run time.
|
|
586
|
-
*/
|
|
587
|
-
export function quantizeToIndicesFromClustering(image: ImageLike, clustering: Clustering, k: number, options?: MappingIndexOptions): Promise<IndexedImage>;
|
|
588
|
-
|
|
589
528
|
/** Like `quantizeWithPalette`, but returns indices into `palette`, which is returned too. */
|
|
590
529
|
export function quantizeToIndicesWithPalette(image: ImageLike, palette: PaletteLike, options?: MappingIndexOptions): Promise<IndexedImage>;
|
|
591
530
|
export {};
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import createWasmModule from './clustering.js';
|
|
2
2
|
import { choose } from "./choose.mjs";
|
|
3
|
-
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,
|
|
3
|
+
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, bareImageError, checkImageBody, imageCopy, checkSize } from "./shared.mjs";
|
|
4
4
|
|
|
5
5
|
// The library's version, the stored-data format version, and the Palette and Clustering types (see shared.js).
|
|
6
6
|
export { version, Palette, Clustering, errorCodes, isCodedError, ditherMethods, ditherSteps, colorSpaces, imageFormats, alphaModes } from "./shared.mjs";
|
|
@@ -90,7 +90,7 @@ const hasSimd = () => {
|
|
|
90
90
|
}
|
|
91
91
|
};
|
|
92
92
|
// Node itself (or Bun), not a runtime that imitates it: Cloudflare Workers with nodejs_compat and Deno also define
|
|
93
|
-
// process.versions.node, but their navigator.userAgent says what they are (Node
|
|
93
|
+
// process.versions.node, but their navigator.userAgent says what they are (old Node versions have no navigator at all).
|
|
94
94
|
const isNodeLike = !!(globalThis.process && globalThis.process.versions && globalThis.process.versions.node) && (typeof navigator === 'undefined' || /^(Node\.js|Bun)\//.test(String(navigator.userAgent)));
|
|
95
95
|
// A browser page or worker, as opposed to a server-side runtime that isn't Node (Cloudflare Workers, Vercel's edge runtime,
|
|
96
96
|
// Deno Deploy): only those have a document, a worker scope or a browser's user agent.
|
|
@@ -404,23 +404,10 @@ const bytes = data => isUint8Array(data) ? data : new Uint8Array(data.buffer, da
|
|
|
404
404
|
// or change the array as soon as the call returns. Once the module is loaded the bytes go into its memory before the
|
|
405
405
|
// call returns; until then (the first call) a copy is kept for when it has loaded.
|
|
406
406
|
const held = data => Module ? data : data.slice();
|
|
407
|
-
|
|
408
|
-
// 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,
|
|
409
|
-
// just before it starts work, so the one warning isn't spent on a call that fails anyway (PRINCIPLES "Warnings").
|
|
410
|
-
const noWarning = () => {};
|
|
411
|
-
// A call whose signal is already aborted fails before any work, so it doesn't use the warning up either.
|
|
412
|
-
const warningFor = image => {
|
|
413
|
-
const warning = bareImageWarning(image);
|
|
414
|
-
return warning ? signal => {
|
|
415
|
-
if (!(signal && signal.aborted)) {
|
|
416
|
-
warnOnce(warning, image.byteLength);
|
|
417
|
-
}
|
|
418
|
-
} : noWarning;
|
|
419
|
-
};
|
|
420
407
|
const load = image => {
|
|
421
408
|
if (isClustering(image) || isPalette(image)) {
|
|
422
409
|
const what = isClustering(image) ? 'a clustering (from buildClustering)' : 'a palette';
|
|
423
|
-
const fix = isClustering(image) ? 'To cut a palette from it, use
|
|
410
|
+
const fix = isClustering(image) ? 'To cut a palette from it, use clustering.getPalette(k) (or clustering.quantize(image, k) to map an image with it).' : 'To map an image to it, use quantizeWithPalette(image, palette).';
|
|
424
411
|
throw fail('invalid-argument', `Invalid image: this is ${what}, not pixels. ${fix}`, 'image');
|
|
425
412
|
}
|
|
426
413
|
if (ArrayBuffer.isView(image) && isDetached(image)) {
|
|
@@ -434,6 +421,10 @@ const load = image => {
|
|
|
434
421
|
if (image.length % 4 !== 0) {
|
|
435
422
|
throw fail('invalid-argument', "Invalid image data: length must be a multiple of 4 for 'rgba'.", 'image');
|
|
436
423
|
}
|
|
424
|
+
const bare = bareImageError(image);
|
|
425
|
+
if (bare) {
|
|
426
|
+
throw bare;
|
|
427
|
+
}
|
|
437
428
|
return {
|
|
438
429
|
data: held(image),
|
|
439
430
|
format: 'rgba',
|
|
@@ -458,13 +449,7 @@ const load = image => {
|
|
|
458
449
|
if (typeof image !== 'object' || !image || !image.data || !image.format) {
|
|
459
450
|
throw fail('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${imageHint(image)}`, 'image');
|
|
460
451
|
}
|
|
461
|
-
|
|
462
|
-
throw fail('invalid-argument', "Invalid image data: must be Uint8Array or Uint8ClampedArray.", 'image');
|
|
463
|
-
}
|
|
464
|
-
if (isDetached(image.data)) {
|
|
465
|
-
throw detachedError('data');
|
|
466
|
-
}
|
|
467
|
-
const premultiplied = checkImageObject(image);
|
|
452
|
+
const premultiplied = checkImageBody(image);
|
|
468
453
|
return {
|
|
469
454
|
data: held(bytes(image.data)),
|
|
470
455
|
format: image.format,
|
|
@@ -629,6 +614,7 @@ const parseOptions = (options, allowed, image) => {
|
|
|
629
614
|
if (!Number.isInteger(transparent) || transparent < 0 || transparent > 255 || transparent === 0 && transparentOption !== false) {
|
|
630
615
|
throw fail('invalid-argument', "Invalid transparent: must be a boolean or an integer alpha cutoff from 1 to 255.", 'options.transparent');
|
|
631
616
|
}
|
|
617
|
+
checkTransparentFormat(image, options);
|
|
632
618
|
|
|
633
619
|
// A cutoff replaces opacity weighting (pixels count whole or not at all), so naming both would leave one of them ignored.
|
|
634
620
|
if (transparent > 0 && options.alpha !== undefined) {
|
|
@@ -650,6 +636,15 @@ const parseOptions = (options, allowed, image) => {
|
|
|
650
636
|
};
|
|
651
637
|
};
|
|
652
638
|
const controlOptions = ['signal', 'onProgress'];
|
|
639
|
+
// `transparent: true` or a cutoff on a format with no alpha channel can't mean anything (no pixel is transparent), so it is
|
|
640
|
+
// an error rather than a silent no-op. Only the call's own options count: a clustering's stored cutoff never reaches here.
|
|
641
|
+
const checkTransparentFormat = (image, options) => {
|
|
642
|
+
if (!image || hasAlpha(image.format) || options.transparent === undefined || options.transparent === false) {
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
const hint = image.format === 'rgbx' || image.format === 'bgrx' ? " If the fourth byte really is alpha, use 'rgba' or 'bgra'." : '';
|
|
646
|
+
throw fail('invalid-argument', `Invalid transparent: '${image.format}' has no alpha, so no pixel can be transparent. Leave transparent out (false is accepted).${hint}`, 'options.transparent');
|
|
647
|
+
};
|
|
653
648
|
const clusterOptions = ['colorSpace', 'precision', 'alpha', 'transparent', ...controlOptions];
|
|
654
649
|
const quantizeOptions = [...clusterOptions, 'dither', 'width'];
|
|
655
650
|
const mapOptions = ['colorSpace', 'dither', 'width', ...controlOptions];
|
|
@@ -930,12 +925,11 @@ const mapInSlices = async (wasm, image, source, colorSpace, indices, cutoff, o,
|
|
|
930
925
|
|
|
931
926
|
// The functions that read a clustering take no options: a clustering already says how it was made. An options object passed
|
|
932
927
|
// anyway (say { colorSpace: 'oklab' }, expecting it to apply) would be silently ignored, so it is an error. undefined is fine.
|
|
933
|
-
// `name` is how the caller wrote the call
|
|
934
|
-
//
|
|
928
|
+
// `name` is how the caller wrote the call (the method, `clustering.getPalette`), whose message points at the method form
|
|
929
|
+
// of mapping.
|
|
935
930
|
const noOptions = (name, extra) => {
|
|
936
931
|
if (extra.some(value => value !== undefined)) {
|
|
937
|
-
|
|
938
|
-
throw fail('invalid-argument', `Invalid arguments: ${name} takes no options (a clustering already records its colour space, precision and cutoff). To map pixels with other options, use ${alternative}.`, 'options');
|
|
932
|
+
throw fail('invalid-argument', `Invalid arguments: ${name} takes no options (a clustering already records its colour space, precision and cutoff). To map pixels with other options, use clustering.quantize(image, k, options).`, 'options');
|
|
939
933
|
}
|
|
940
934
|
};
|
|
941
935
|
|
|
@@ -944,10 +938,9 @@ const noOptions = (name, extra) => {
|
|
|
944
938
|
// happens before the call returns.
|
|
945
939
|
|
|
946
940
|
export const buildClustering = async (image, options) => {
|
|
947
|
-
|
|
941
|
+
image = imageCopy(image);
|
|
948
942
|
image = load(image);
|
|
949
943
|
const o = parseOptions(options, clusterOptions, image);
|
|
950
|
-
warn(o.signal);
|
|
951
944
|
const wasm = Module || (await acquire());
|
|
952
945
|
if (isControlled(o)) {
|
|
953
946
|
const report = reporter(o);
|
|
@@ -957,14 +950,11 @@ export const buildClustering = async (image, options) => {
|
|
|
957
950
|
}
|
|
958
951
|
return markClustering(call(wasm, [image.data], imagePointer => wasm._get_clustering(imagePointer, image.data.length, codes[image.format], o.colorSpace, o.precision, o.alpha, o.transparent), o.precision, image.premultiplied));
|
|
959
952
|
};
|
|
960
|
-
/** @deprecated The same function as buildClustering, its name before 3.4.0. Removed in 4.0. */
|
|
961
|
-
export const getClustering = buildClustering;
|
|
962
953
|
export const buildPalette = async (image, k, options) => {
|
|
963
|
-
|
|
954
|
+
image = imageCopy(image);
|
|
964
955
|
image = load(image);
|
|
965
956
|
k = check(k);
|
|
966
957
|
const o = parseOptions(options, clusterOptions, image);
|
|
967
|
-
warn(o.signal);
|
|
968
958
|
const wasm = Module || (await acquire());
|
|
969
959
|
if (isControlled(o)) {
|
|
970
960
|
const report = reporter(o);
|
|
@@ -976,10 +966,8 @@ export const buildPalette = async (image, k, options) => {
|
|
|
976
966
|
}
|
|
977
967
|
return markPalette(call(wasm, [image.data], imagePointer => wasm._get_palette(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.transparent), o.precision, image.premultiplied), colorSpaceName(o.colorSpace));
|
|
978
968
|
};
|
|
979
|
-
/** @deprecated The same function as buildPalette, its name before 3.4.0. Removed in 4.0. */
|
|
980
|
-
export const getPalette = buildPalette;
|
|
981
969
|
|
|
982
|
-
// The cuts take the name they were called by, for their messages: the
|
|
970
|
+
// The cuts take the name they were called by, for their messages: the methods on Clustering (or the function that cuts).
|
|
983
971
|
const cutPalette = async (name, clustering, k, extra) => {
|
|
984
972
|
const header = readHeader(clustering);
|
|
985
973
|
k = check(k);
|
|
@@ -988,7 +976,6 @@ const cutPalette = async (name, clustering, k, extra) => {
|
|
|
988
976
|
const wasm = Module || (await acquire());
|
|
989
977
|
return markPalette(call(wasm, [clustering], clusteringPointer => wasm._get_palette_from_clustering(clusteringPointer, clustering.length, k)), colorSpaceName(header.colorSpace));
|
|
990
978
|
};
|
|
991
|
-
export const getPaletteFromClustering = (clustering, k, ...extra) => cutPalette('getPaletteFromClustering', clustering, k, extra);
|
|
992
979
|
|
|
993
980
|
// The weight of each colour in the k-colour cut, in pixels (opacity-weighted), read from the clustering's records.
|
|
994
981
|
const weightsOf = (wasm, clustering, header, k) => {
|
|
@@ -1035,17 +1022,16 @@ const cutSwatches = async (name, clustering, k, extra) => {
|
|
|
1035
1022
|
shares
|
|
1036
1023
|
};
|
|
1037
1024
|
};
|
|
1038
|
-
export const getSwatchesFromClustering = (clustering, k, ...extra) => cutSwatches('getSwatchesFromClustering', clustering, k, extra);
|
|
1039
1025
|
|
|
1040
|
-
// The same for an image: clusters it, then cuts, like
|
|
1026
|
+
// The same for an image: clusters it, then cuts, like buildPalette.
|
|
1041
1027
|
export const buildSwatches = async (image, k, options) => {
|
|
1028
|
+
image = imageCopy(image);
|
|
1042
1029
|
// Arguments are checked in the order they are given (image, then k, then options), as in every other function.
|
|
1030
|
+
// `load` only validates here (buildClustering loads again); both see the copy, so the caller's fields are read once.
|
|
1043
1031
|
load(image);
|
|
1044
1032
|
k = check(k);
|
|
1045
|
-
return
|
|
1033
|
+
return cutSwatches('buildSwatches', await buildClustering(image, options), k, []);
|
|
1046
1034
|
};
|
|
1047
|
-
/** @deprecated The same function as buildSwatches, its name before 3.4.0. Removed in 4.0. */
|
|
1048
|
-
export const getSwatches = buildSwatches;
|
|
1049
1035
|
|
|
1050
1036
|
// What a clustering holds, read from its header and records without the WASM module. `mse[k - 1]` is the mean
|
|
1051
1037
|
// squared error per channel of the k-colour clustering: each pixel's squared distance to its cluster's colour,
|
|
@@ -1093,14 +1079,12 @@ const readInfo = async (name, clustering, extra) => {
|
|
|
1093
1079
|
}
|
|
1094
1080
|
return info;
|
|
1095
1081
|
};
|
|
1096
|
-
export const getClusteringInfo = (clustering, ...extra) => readInfo('getClusteringInfo', clustering, extra);
|
|
1097
1082
|
export const quantize = async (image, k, options) => {
|
|
1098
|
-
|
|
1083
|
+
image = imageCopy(image);
|
|
1099
1084
|
const wrap = getWrapper(image);
|
|
1100
1085
|
image = load(image);
|
|
1101
1086
|
k = check(k);
|
|
1102
1087
|
const o = parseOptions(options, quantizeOptions, image);
|
|
1103
|
-
warn(o.signal);
|
|
1104
1088
|
const wasm = Module || (await acquire());
|
|
1105
1089
|
if (isControlled(o)) {
|
|
1106
1090
|
const report = reporter(o);
|
|
@@ -1115,15 +1099,14 @@ export const quantize = async (image, k, options) => {
|
|
|
1115
1099
|
}
|
|
1116
1100
|
return wrap(call(wasm, [image.data], imagePointer => wasm._quantize(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.dither, o.ditherMethod, o.ditherSteps, o.width, o.transparent), o.precision, image.premultiplied, true));
|
|
1117
1101
|
};
|
|
1118
|
-
|
|
1119
|
-
|
|
1102
|
+
const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
1103
|
+
image = imageCopy(image);
|
|
1120
1104
|
const wrap = getWrapper(image);
|
|
1121
1105
|
image = load(image);
|
|
1122
1106
|
readHeader(clustering);
|
|
1123
1107
|
k = check(k);
|
|
1124
1108
|
const o = parseOptions(options, mapOptions, image);
|
|
1125
1109
|
clustering = held(clustering);
|
|
1126
|
-
warn(o.signal);
|
|
1127
1110
|
const wasm = Module || (await acquire());
|
|
1128
1111
|
checkAborted(o.signal);
|
|
1129
1112
|
const colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
|
|
@@ -1140,7 +1123,7 @@ export const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
|
1140
1123
|
return result;
|
|
1141
1124
|
};
|
|
1142
1125
|
export const quantizeWithPalette = async (image, palette, options) => {
|
|
1143
|
-
|
|
1126
|
+
image = imageCopy(image);
|
|
1144
1127
|
const wrap = getWrapper(image);
|
|
1145
1128
|
image = load(image);
|
|
1146
1129
|
checkPalette(palette);
|
|
@@ -1148,7 +1131,6 @@ export const quantizeWithPalette = async (image, palette, options) => {
|
|
|
1148
1131
|
checkNotEmpty(palette, image, 1);
|
|
1149
1132
|
const space = matchingSpace(o, palette);
|
|
1150
1133
|
palette = held(palette);
|
|
1151
|
-
warn(o.signal);
|
|
1152
1134
|
const wasm = Module || (await acquire());
|
|
1153
1135
|
checkAborted(o.signal);
|
|
1154
1136
|
if (isControlled(o)) {
|
|
@@ -1163,12 +1145,11 @@ export const quantizeWithPalette = async (image, palette, options) => {
|
|
|
1163
1145
|
return result;
|
|
1164
1146
|
};
|
|
1165
1147
|
export const quantizeToIndices = async (image, k, options) => {
|
|
1166
|
-
|
|
1148
|
+
image = imageCopy(image);
|
|
1167
1149
|
const original = image;
|
|
1168
1150
|
image = load(image);
|
|
1169
1151
|
k = check(k);
|
|
1170
1152
|
const o = parseOptions(options, quantizeIndexOptions, image);
|
|
1171
|
-
warn(o.signal);
|
|
1172
1153
|
const wasm = Module || (await acquire());
|
|
1173
1154
|
if (isControlled(o)) {
|
|
1174
1155
|
const report = reporter(o);
|
|
@@ -1183,15 +1164,14 @@ export const quantizeToIndices = async (image, k, options) => {
|
|
|
1183
1164
|
}
|
|
1184
1165
|
return unpackIndices(call(wasm, [image.data], imagePointer => wasm._quantize_indices(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.dither, o.ditherMethod, o.ditherSteps, o.width, o.transparent), o.precision, image.premultiplied), original, colorSpaceName(o.colorSpace));
|
|
1185
1166
|
};
|
|
1186
|
-
|
|
1187
|
-
|
|
1167
|
+
const quantizeToIndicesFromClustering = async (image, clustering, k, options) => {
|
|
1168
|
+
image = imageCopy(image);
|
|
1188
1169
|
const original = image;
|
|
1189
1170
|
image = load(image);
|
|
1190
1171
|
const header = readHeader(clustering);
|
|
1191
1172
|
k = check(k);
|
|
1192
1173
|
const o = parseOptions(options, mapIndexOptions, image);
|
|
1193
1174
|
clustering = held(clustering);
|
|
1194
|
-
warn(o.signal);
|
|
1195
1175
|
const wasm = Module || (await acquire());
|
|
1196
1176
|
checkAborted(o.signal);
|
|
1197
1177
|
const colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
|
|
@@ -1211,7 +1191,7 @@ export const quantizeToIndicesFromClustering = async (image, clustering, k, opti
|
|
|
1211
1191
|
return result;
|
|
1212
1192
|
};
|
|
1213
1193
|
export const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
1214
|
-
|
|
1194
|
+
image = imageCopy(image);
|
|
1215
1195
|
const original = image;
|
|
1216
1196
|
image = load(image);
|
|
1217
1197
|
checkPalette(palette);
|
|
@@ -1220,7 +1200,6 @@ export const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
|
1220
1200
|
const space = matchingSpace(o, palette);
|
|
1221
1201
|
const paletteSpace = isPalette(palette) ? palette.colorSpace : colorSpaceName(space);
|
|
1222
1202
|
palette = held(palette);
|
|
1223
|
-
warn(o.signal);
|
|
1224
1203
|
const wasm = Module || (await acquire());
|
|
1225
1204
|
checkAborted(o.signal);
|
|
1226
1205
|
if (isControlled(o)) {
|