agglomerative-clustering 4.0.0-beta.2 → 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 +26 -56
- package/dist/index.d.cts +15 -76
- package/dist/index.d.ts +15 -76
- package/dist/index.mjs +25 -50
- package/dist/shared.cjs +30 -78
- package/dist/shared.mjs +28 -73
- package/dist/worker-thread.mjs +15 -18
- package/dist/worker.cjs +108 -60
- package/dist/worker.d.cts +0 -32
- package/dist/worker.d.ts +0 -32
- package/dist/worker.mjs +107 -54
- 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',
|
|
@@ -623,6 +614,7 @@ const parseOptions = (options, allowed, image) => {
|
|
|
623
614
|
if (!Number.isInteger(transparent) || transparent < 0 || transparent > 255 || transparent === 0 && transparentOption !== false) {
|
|
624
615
|
throw fail('invalid-argument', "Invalid transparent: must be a boolean or an integer alpha cutoff from 1 to 255.", 'options.transparent');
|
|
625
616
|
}
|
|
617
|
+
checkTransparentFormat(image, options);
|
|
626
618
|
|
|
627
619
|
// A cutoff replaces opacity weighting (pixels count whole or not at all), so naming both would leave one of them ignored.
|
|
628
620
|
if (transparent > 0 && options.alpha !== undefined) {
|
|
@@ -644,6 +636,15 @@ const parseOptions = (options, allowed, image) => {
|
|
|
644
636
|
};
|
|
645
637
|
};
|
|
646
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
|
+
};
|
|
647
648
|
const clusterOptions = ['colorSpace', 'precision', 'alpha', 'transparent', ...controlOptions];
|
|
648
649
|
const quantizeOptions = [...clusterOptions, 'dither', 'width'];
|
|
649
650
|
const mapOptions = ['colorSpace', 'dither', 'width', ...controlOptions];
|
|
@@ -924,12 +925,11 @@ const mapInSlices = async (wasm, image, source, colorSpace, indices, cutoff, o,
|
|
|
924
925
|
|
|
925
926
|
// The functions that read a clustering take no options: a clustering already says how it was made. An options object passed
|
|
926
927
|
// anyway (say { colorSpace: 'oklab' }, expecting it to apply) would be silently ignored, so it is an error. undefined is fine.
|
|
927
|
-
// `name` is how the caller wrote the call
|
|
928
|
-
//
|
|
928
|
+
// `name` is how the caller wrote the call (the method, `clustering.getPalette`), whose message points at the method form
|
|
929
|
+
// of mapping.
|
|
929
930
|
const noOptions = (name, extra) => {
|
|
930
931
|
if (extra.some(value => value !== undefined)) {
|
|
931
|
-
|
|
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 ${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');
|
|
933
933
|
}
|
|
934
934
|
};
|
|
935
935
|
|
|
@@ -939,10 +939,8 @@ const noOptions = (name, extra) => {
|
|
|
939
939
|
|
|
940
940
|
export const buildClustering = async (image, options) => {
|
|
941
941
|
image = imageCopy(image);
|
|
942
|
-
const warn = warningFor(image);
|
|
943
942
|
image = load(image);
|
|
944
943
|
const o = parseOptions(options, clusterOptions, image);
|
|
945
|
-
warn(o.signal);
|
|
946
944
|
const wasm = Module || (await acquire());
|
|
947
945
|
if (isControlled(o)) {
|
|
948
946
|
const report = reporter(o);
|
|
@@ -952,15 +950,11 @@ export const buildClustering = async (image, options) => {
|
|
|
952
950
|
}
|
|
953
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));
|
|
954
952
|
};
|
|
955
|
-
/** @deprecated The same function as buildClustering, its name before 3.4.0. Removed in 4.0. */
|
|
956
|
-
export const getClustering = buildClustering;
|
|
957
953
|
export const buildPalette = async (image, k, options) => {
|
|
958
954
|
image = imageCopy(image);
|
|
959
|
-
const warn = warningFor(image);
|
|
960
955
|
image = load(image);
|
|
961
956
|
k = check(k);
|
|
962
957
|
const o = parseOptions(options, clusterOptions, image);
|
|
963
|
-
warn(o.signal);
|
|
964
958
|
const wasm = Module || (await acquire());
|
|
965
959
|
if (isControlled(o)) {
|
|
966
960
|
const report = reporter(o);
|
|
@@ -972,10 +966,8 @@ export const buildPalette = async (image, k, options) => {
|
|
|
972
966
|
}
|
|
973
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));
|
|
974
968
|
};
|
|
975
|
-
/** @deprecated The same function as buildPalette, its name before 3.4.0. Removed in 4.0. */
|
|
976
|
-
export const getPalette = buildPalette;
|
|
977
969
|
|
|
978
|
-
// 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).
|
|
979
971
|
const cutPalette = async (name, clustering, k, extra) => {
|
|
980
972
|
const header = readHeader(clustering);
|
|
981
973
|
k = check(k);
|
|
@@ -984,7 +976,6 @@ const cutPalette = async (name, clustering, k, extra) => {
|
|
|
984
976
|
const wasm = Module || (await acquire());
|
|
985
977
|
return markPalette(call(wasm, [clustering], clusteringPointer => wasm._get_palette_from_clustering(clusteringPointer, clustering.length, k)), colorSpaceName(header.colorSpace));
|
|
986
978
|
};
|
|
987
|
-
export const getPaletteFromClustering = (clustering, k, ...extra) => cutPalette('getPaletteFromClustering', clustering, k, extra);
|
|
988
979
|
|
|
989
980
|
// The weight of each colour in the k-colour cut, in pixels (opacity-weighted), read from the clustering's records.
|
|
990
981
|
const weightsOf = (wasm, clustering, header, k) => {
|
|
@@ -1031,19 +1022,16 @@ const cutSwatches = async (name, clustering, k, extra) => {
|
|
|
1031
1022
|
shares
|
|
1032
1023
|
};
|
|
1033
1024
|
};
|
|
1034
|
-
export const getSwatchesFromClustering = (clustering, k, ...extra) => cutSwatches('getSwatchesFromClustering', clustering, k, extra);
|
|
1035
1025
|
|
|
1036
|
-
// The same for an image: clusters it, then cuts, like
|
|
1026
|
+
// The same for an image: clusters it, then cuts, like buildPalette.
|
|
1037
1027
|
export const buildSwatches = async (image, k, options) => {
|
|
1038
1028
|
image = imageCopy(image);
|
|
1039
1029
|
// Arguments are checked in the order they are given (image, then k, then options), as in every other function.
|
|
1040
1030
|
// `load` only validates here (buildClustering loads again); both see the copy, so the caller's fields are read once.
|
|
1041
1031
|
load(image);
|
|
1042
1032
|
k = check(k);
|
|
1043
|
-
return
|
|
1033
|
+
return cutSwatches('buildSwatches', await buildClustering(image, options), k, []);
|
|
1044
1034
|
};
|
|
1045
|
-
/** @deprecated The same function as buildSwatches, its name before 3.4.0. Removed in 4.0. */
|
|
1046
|
-
export const getSwatches = buildSwatches;
|
|
1047
1035
|
|
|
1048
1036
|
// What a clustering holds, read from its header and records without the WASM module. `mse[k - 1]` is the mean
|
|
1049
1037
|
// squared error per channel of the k-colour clustering: each pixel's squared distance to its cluster's colour,
|
|
@@ -1091,15 +1079,12 @@ const readInfo = async (name, clustering, extra) => {
|
|
|
1091
1079
|
}
|
|
1092
1080
|
return info;
|
|
1093
1081
|
};
|
|
1094
|
-
export const getClusteringInfo = (clustering, ...extra) => readInfo('getClusteringInfo', clustering, extra);
|
|
1095
1082
|
export const quantize = async (image, k, options) => {
|
|
1096
1083
|
image = imageCopy(image);
|
|
1097
|
-
const warn = warningFor(image);
|
|
1098
1084
|
const wrap = getWrapper(image);
|
|
1099
1085
|
image = load(image);
|
|
1100
1086
|
k = check(k);
|
|
1101
1087
|
const o = parseOptions(options, quantizeOptions, image);
|
|
1102
|
-
warn(o.signal);
|
|
1103
1088
|
const wasm = Module || (await acquire());
|
|
1104
1089
|
if (isControlled(o)) {
|
|
1105
1090
|
const report = reporter(o);
|
|
@@ -1114,16 +1099,14 @@ export const quantize = async (image, k, options) => {
|
|
|
1114
1099
|
}
|
|
1115
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));
|
|
1116
1101
|
};
|
|
1117
|
-
|
|
1102
|
+
const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
1118
1103
|
image = imageCopy(image);
|
|
1119
|
-
const warn = warningFor(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;
|
|
@@ -1141,7 +1124,6 @@ export const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
|
1141
1124
|
};
|
|
1142
1125
|
export const quantizeWithPalette = async (image, palette, options) => {
|
|
1143
1126
|
image = imageCopy(image);
|
|
1144
|
-
const warn = warningFor(image);
|
|
1145
1127
|
const wrap = getWrapper(image);
|
|
1146
1128
|
image = load(image);
|
|
1147
1129
|
checkPalette(palette);
|
|
@@ -1149,7 +1131,6 @@ export const quantizeWithPalette = async (image, palette, options) => {
|
|
|
1149
1131
|
checkNotEmpty(palette, image, 1);
|
|
1150
1132
|
const space = matchingSpace(o, palette);
|
|
1151
1133
|
palette = held(palette);
|
|
1152
|
-
warn(o.signal);
|
|
1153
1134
|
const wasm = Module || (await acquire());
|
|
1154
1135
|
checkAborted(o.signal);
|
|
1155
1136
|
if (isControlled(o)) {
|
|
@@ -1165,12 +1146,10 @@ export const quantizeWithPalette = async (image, palette, options) => {
|
|
|
1165
1146
|
};
|
|
1166
1147
|
export const quantizeToIndices = async (image, k, options) => {
|
|
1167
1148
|
image = imageCopy(image);
|
|
1168
|
-
const warn = warningFor(image);
|
|
1169
1149
|
const original = image;
|
|
1170
1150
|
image = load(image);
|
|
1171
1151
|
k = check(k);
|
|
1172
1152
|
const o = parseOptions(options, quantizeIndexOptions, image);
|
|
1173
|
-
warn(o.signal);
|
|
1174
1153
|
const wasm = Module || (await acquire());
|
|
1175
1154
|
if (isControlled(o)) {
|
|
1176
1155
|
const report = reporter(o);
|
|
@@ -1185,16 +1164,14 @@ export const quantizeToIndices = async (image, k, options) => {
|
|
|
1185
1164
|
}
|
|
1186
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));
|
|
1187
1166
|
};
|
|
1188
|
-
|
|
1167
|
+
const quantizeToIndicesFromClustering = async (image, clustering, k, options) => {
|
|
1189
1168
|
image = imageCopy(image);
|
|
1190
|
-
const warn = warningFor(image);
|
|
1191
1169
|
const original = image;
|
|
1192
1170
|
image = load(image);
|
|
1193
1171
|
const header = readHeader(clustering);
|
|
1194
1172
|
k = check(k);
|
|
1195
1173
|
const o = parseOptions(options, mapIndexOptions, image);
|
|
1196
1174
|
clustering = held(clustering);
|
|
1197
|
-
warn(o.signal);
|
|
1198
1175
|
const wasm = Module || (await acquire());
|
|
1199
1176
|
checkAborted(o.signal);
|
|
1200
1177
|
const colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
|
|
@@ -1215,7 +1192,6 @@ export const quantizeToIndicesFromClustering = async (image, clustering, k, opti
|
|
|
1215
1192
|
};
|
|
1216
1193
|
export const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
1217
1194
|
image = imageCopy(image);
|
|
1218
|
-
const warn = warningFor(image);
|
|
1219
1195
|
const original = image;
|
|
1220
1196
|
image = load(image);
|
|
1221
1197
|
checkPalette(palette);
|
|
@@ -1224,7 +1200,6 @@ export const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
|
1224
1200
|
const space = matchingSpace(o, palette);
|
|
1225
1201
|
const paletteSpace = isPalette(palette) ? palette.colorSpace : colorSpaceName(space);
|
|
1226
1202
|
palette = held(palette);
|
|
1227
|
-
warn(o.signal);
|
|
1228
1203
|
const wasm = Module || (await acquire());
|
|
1229
1204
|
checkAborted(o.signal);
|
|
1230
1205
|
if (isControlled(o)) {
|