agglomerative-clustering 3.8.0 → 4.0.0-beta.2
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.cjs +1 -1
- package/dist/clustering.js +1 -1
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +64 -68
- package/dist/index.d.cts +43 -9
- package/dist/index.d.ts +43 -9
- package/dist/index.mjs +64 -68
- package/dist/shared.cjs +161 -3
- package/dist/shared.mjs +153 -1
- package/dist/worker.cjs +31 -14
- package/dist/worker.mjs +32 -15
- package/package.json +2 -2
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 = '
|
|
7
|
+
export const version = '4.0.0-beta.2';
|
|
8
8
|
export const colorSpaceCodes = {
|
|
9
9
|
'rgb': 0,
|
|
10
10
|
'oklab': 1
|
|
@@ -160,6 +160,158 @@ export const imageHint = image => {
|
|
|
160
160
|
return '';
|
|
161
161
|
};
|
|
162
162
|
|
|
163
|
+
// An image object has only these keys; any other own enumerable key (a decoder's result spread in) is an error, with a hint where
|
|
164
|
+
// the mistake is a common one. Inherited keys are not checked.
|
|
165
|
+
const imageKeys = ['data', 'format', 'width', 'height', 'premultiplied'];
|
|
166
|
+
const withinOne = (a, b) => {
|
|
167
|
+
if (a === b) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
if (Math.abs(a.length - b.length) > 1) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
let i = 0;
|
|
174
|
+
while (i < a.length && i < b.length && a[i] === b[i]) {
|
|
175
|
+
i++;
|
|
176
|
+
}
|
|
177
|
+
if (a.length === b.length) {
|
|
178
|
+
// One substitution, or two neighbouring letters swapped (widht).
|
|
179
|
+
return a.slice(i + 1) === b.slice(i + 1) || a[i] === b[i + 1] && a[i + 1] === b[i] && a.slice(i + 2) === b.slice(i + 2);
|
|
180
|
+
}
|
|
181
|
+
const [long, short] = a.length > b.length ? [a, b] : [b, a];
|
|
182
|
+
return long.slice(i + 1) === short.slice(i);
|
|
183
|
+
};
|
|
184
|
+
export const checkImageKeys = image => {
|
|
185
|
+
for (const key of Object.keys(image)) {
|
|
186
|
+
if (imageKeys.includes(key)) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
let hint = '';
|
|
190
|
+
if (key === 'premultiply') {
|
|
191
|
+
hint = ' Did you mean premultiplied: true?';
|
|
192
|
+
} else if (key === 'channels' || key === 'size' || key === 'depth') {
|
|
193
|
+
hint = " Take only width and height from a decoder's result.";
|
|
194
|
+
} else {
|
|
195
|
+
const near = imageKeys.find(known => key.toLowerCase() === known || withinOne(key, known));
|
|
196
|
+
if (near) {
|
|
197
|
+
hint = ` Did you mean '${near}'?`;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
throw fail('invalid-argument', `Invalid image: unknown key '${key}'. An image object has data, format, width, height and premultiplied: build it from those rather than spreading a decoder's result.${hint}`, 'image');
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
// A hint for a format name that isn't one: the right case, the name other libraries use for the same bytes, or the nearest
|
|
205
|
+
// supported layout.
|
|
206
|
+
export const formatHint = format => {
|
|
207
|
+
if (typeof format !== 'string') {
|
|
208
|
+
return '';
|
|
209
|
+
}
|
|
210
|
+
const lower = format.toLowerCase();
|
|
211
|
+
if (lower !== format && imageFormats.includes(lower)) {
|
|
212
|
+
return ` Format names are lower case: '${lower}'.`;
|
|
213
|
+
}
|
|
214
|
+
if (lower === 'xrgb' || lower === 'rgb24') {
|
|
215
|
+
return " Cairo RGB24 (xRGB) is BGRX in memory on little-endian machines: pass 'bgrx'.";
|
|
216
|
+
}
|
|
217
|
+
if (lower === 'argb' || lower === 'argb32') {
|
|
218
|
+
return " Cairo and node-canvas ARGB32 is BGRA in memory on little-endian machines, and premultiplied: pass it as 'bgra' with premultiplied: true (see Input in the API docs).";
|
|
219
|
+
}
|
|
220
|
+
if (lower === 'bgr' || lower === 'bgr24') {
|
|
221
|
+
return " 24-bit BGR isn't supported: widen each pixel to 4 bytes and pass 'bgrx'.";
|
|
222
|
+
}
|
|
223
|
+
return '';
|
|
224
|
+
};
|
|
225
|
+
// width and height are copied to results and used for dithering, so each must make sense on its own: a whole number of
|
|
226
|
+
// pixels that divides the pixel count (and, when both are given, account for exactly the pixels in the data).
|
|
227
|
+
export const checkSize = (pixels, size, name, argument) => {
|
|
228
|
+
if (size === undefined) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (!Number.isInteger(size) || size < 0) {
|
|
232
|
+
throw fail('invalid-argument', `Invalid ${name}: must be a whole number of pixels.`, argument);
|
|
233
|
+
}
|
|
234
|
+
if (pixels > 0 && (size === 0 || pixels % size !== 0)) {
|
|
235
|
+
throw fail('invalid-argument', `Invalid ${name}: ${size} does not divide the image's ${pixels} pixels into whole rows.`, argument);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
// `premultiplied` on an image object: true only for 'rgba' and 'bgra' (the formats with alpha); false is accepted on any format.
|
|
240
|
+
const checkPremultiplied = image => {
|
|
241
|
+
const value = image.premultiplied;
|
|
242
|
+
if (value === undefined || value === false) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
if (value !== true) {
|
|
246
|
+
throw fail('invalid-argument', `Invalid image: premultiplied must be true or false, got ${describe(value)}.`, 'image');
|
|
247
|
+
}
|
|
248
|
+
if (image.format !== 'rgba' && image.format !== 'bgra') {
|
|
249
|
+
throw fail('invalid-argument', `Invalid image: premultiplied is for 'rgba' and 'bgra'; '${image.format}' has no alpha, so remove it.`, 'image');
|
|
250
|
+
}
|
|
251
|
+
return true;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// Everything checked about an image object whose data is a Uint8Array or Uint8ClampedArray, in this order: format, data length,
|
|
255
|
+
// width and height, unknown keys, premultiplied. The main entry and the worker entry (before it posts) both run it, so their errors
|
|
256
|
+
// are identical. Returns the premultiplied flag.
|
|
257
|
+
export const checkImageObject = image => {
|
|
258
|
+
if (!imageFormats.includes(image.format)) {
|
|
259
|
+
throw fail('invalid-argument', `Invalid format: got ${describe(image.format)}; it must be ${formatNames}.${formatHint(image.format)}`, 'image');
|
|
260
|
+
}
|
|
261
|
+
const stride = channels(image.format);
|
|
262
|
+
if (image.data.length % stride !== 0) {
|
|
263
|
+
throw fail('invalid-argument', `Invalid image data: length must be a multiple of ${stride} for '${image.format}'.`, 'image');
|
|
264
|
+
}
|
|
265
|
+
const pixels = image.data.length / stride;
|
|
266
|
+
checkSize(pixels, image.width, 'image width', 'image');
|
|
267
|
+
checkSize(pixels, image.height, 'image height', 'image');
|
|
268
|
+
if (image.width !== undefined && image.height !== undefined && image.width * image.height !== pixels) {
|
|
269
|
+
throw fail('invalid-argument', `Invalid image: width x height (${image.width} x ${image.height}) does not match its ${pixels} pixels.`, 'image');
|
|
270
|
+
}
|
|
271
|
+
checkImageKeys(image);
|
|
272
|
+
return checkPremultiplied(image);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// The image object's own fields, read once: `data`, `format`, `width`, `height` and `premultiplied` are taken from it here, and
|
|
276
|
+
// everything after (validation and use) reads the copy, so a getter that answers differently on a later read changes nothing.
|
|
277
|
+
// The copy also names the object's other own enumerable keys (with no value, never read), in order, for the unknown-key check.
|
|
278
|
+
// Anything that isn't an object with `data` and `format` is returned as it is (a bare array, ImageData, a function...), or, for
|
|
279
|
+
// an object without them, rejected here with the shape error the main entry gives.
|
|
280
|
+
export const imageCopy = image => {
|
|
281
|
+
if (typeof image !== 'object' || image === null || ArrayBuffer.isView(image) || isImageData(image)) {
|
|
282
|
+
return image;
|
|
283
|
+
}
|
|
284
|
+
const data = image.data;
|
|
285
|
+
const format = image.format;
|
|
286
|
+
if (!data || !format) {
|
|
287
|
+
throw fail('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${imageHint(image)}`, 'image');
|
|
288
|
+
}
|
|
289
|
+
const copy = Object.create(null);
|
|
290
|
+
copy.data = data;
|
|
291
|
+
copy.format = format;
|
|
292
|
+
copy.width = image.width;
|
|
293
|
+
copy.height = image.height;
|
|
294
|
+
copy.premultiplied = image.premultiplied;
|
|
295
|
+
for (const key of Object.keys(image)) {
|
|
296
|
+
if (!imageKeys.includes(key)) {
|
|
297
|
+
copy[key] = undefined;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return copy;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
// What the main entry's `load` checks about an image object, from the copy imageCopy made, in its order: data's type, whether its
|
|
304
|
+
// buffer is detached, then checkImageObject. The worker entry runs the same before it posts an image. Returns the premultiplied flag.
|
|
305
|
+
export const checkImageBody = image => {
|
|
306
|
+
if (!isUint8Array(image.data) && !isUint8ClampedArray(image.data)) {
|
|
307
|
+
throw fail('invalid-argument', "Invalid image data: must be Uint8Array or Uint8ClampedArray.", 'image');
|
|
308
|
+
}
|
|
309
|
+
if (isDetached(image.data)) {
|
|
310
|
+
throw detachedError('data');
|
|
311
|
+
}
|
|
312
|
+
return checkImageObject(image);
|
|
313
|
+
};
|
|
314
|
+
|
|
163
315
|
// An options object is an ordinary object. A Map, Date, ArrayBuffer or typed array has no own keys and would pass as
|
|
164
316
|
// `{}`, silently ignoring what the caller meant. Checked by tag rather than prototype so objects from another realm
|
|
165
317
|
// (an iframe, a test sandbox) are fine.
|
package/dist/worker.cjs
CHANGED
|
@@ -324,8 +324,38 @@ const send = image => {
|
|
|
324
324
|
if (notPixels(image)) {
|
|
325
325
|
throw (0, _shared.fail)('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${(0, _shared.imageHint)(image)}`, 'image');
|
|
326
326
|
}
|
|
327
|
+
// An image object goes through the main entry's own checks here (its fields read once, then shape, data, detached buffer, format,
|
|
328
|
+
// sizes, unknown keys, premultiplied), so what can't be sent is reported as the main entry reports it, before anything is posted.
|
|
329
|
+
// A function, a Symbol or a thenable gets the shape error rather than the structured clone's.
|
|
330
|
+
if (!ArrayBuffer.isView(image) && !(0, _shared.isImageData)(image)) {
|
|
331
|
+
const copy = (0, _shared.imageCopy)(image);
|
|
332
|
+
if (copy === image) {
|
|
333
|
+
if (typeof image !== 'object' || image === null) {
|
|
334
|
+
throw (0, _shared.fail)('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${(0, _shared.imageHint)(image)}`, 'image');
|
|
335
|
+
}
|
|
336
|
+
} else {
|
|
337
|
+
const premultiplied = (0, _shared.checkImageBody)(copy);
|
|
338
|
+
if (copy.data.byteLength >= _shared.maxBytes) {
|
|
339
|
+
throw (0, _shared.tooLarge)(copy.data.byteLength);
|
|
340
|
+
}
|
|
341
|
+
const sent = {
|
|
342
|
+
data: copy.data,
|
|
343
|
+
format: copy.format
|
|
344
|
+
};
|
|
345
|
+
if (copy.premultiplied !== undefined) {
|
|
346
|
+
sent.premultiplied = premultiplied;
|
|
347
|
+
}
|
|
348
|
+
if (copy.width !== undefined) {
|
|
349
|
+
sent.width = copy.width;
|
|
350
|
+
}
|
|
351
|
+
if (copy.height !== undefined) {
|
|
352
|
+
sent.height = copy.height;
|
|
353
|
+
}
|
|
354
|
+
return sent;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
327
357
|
// A detached buffer (transferred to a worker created with transfer: true, say) is reported as the main entry reports it.
|
|
328
|
-
const pixels = ArrayBuffer.isView(image) ? image :
|
|
358
|
+
const pixels = ArrayBuffer.isView(image) ? image : (0, _shared.isImageData)(image) && ArrayBuffer.isView(image.data) ? image.data : null;
|
|
329
359
|
if (pixels && (0, _shared.isDetached)(pixels)) {
|
|
330
360
|
throw (0, _shared.detachedError)(ArrayBuffer.isView(image) ? 'bare' : (0, _shared.isImageData)(image) ? 'imagedata' : 'data');
|
|
331
361
|
}
|
|
@@ -343,19 +373,6 @@ const send = image => {
|
|
|
343
373
|
height: image.height
|
|
344
374
|
};
|
|
345
375
|
}
|
|
346
|
-
if (image && typeof image === 'object' && !ArrayBuffer.isView(image) && ArrayBuffer.isView(image.data)) {
|
|
347
|
-
const sent = {
|
|
348
|
-
data: image.data,
|
|
349
|
-
format: image.format
|
|
350
|
-
};
|
|
351
|
-
if (image.width !== undefined) {
|
|
352
|
-
sent.width = image.width;
|
|
353
|
-
}
|
|
354
|
-
if (image.height !== undefined) {
|
|
355
|
-
sent.height = image.height;
|
|
356
|
-
}
|
|
357
|
-
return sent;
|
|
358
|
-
}
|
|
359
376
|
return image;
|
|
360
377
|
};
|
|
361
378
|
|
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, isUint8Array, isUint8ClampedArray, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge, bareImageWarning, warnOnce, readHeader } from "./shared.mjs";
|
|
7
|
+
import { fail, isCodedError, detachedError, isOptionsObject, isUint8Array, isUint8ClampedArray, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge, bareImageWarning, warnOnce, readHeader, checkImageBody, imageCopy } 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";
|
|
@@ -255,8 +255,38 @@ const send = image => {
|
|
|
255
255
|
if (notPixels(image)) {
|
|
256
256
|
throw fail('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${imageHint(image)}`, 'image');
|
|
257
257
|
}
|
|
258
|
+
// An image object goes through the main entry's own checks here (its fields read once, then shape, data, detached buffer, format,
|
|
259
|
+
// sizes, unknown keys, premultiplied), so what can't be sent is reported as the main entry reports it, before anything is posted.
|
|
260
|
+
// A function, a Symbol or a thenable gets the shape error rather than the structured clone's.
|
|
261
|
+
if (!ArrayBuffer.isView(image) && !isImageData(image)) {
|
|
262
|
+
const copy = imageCopy(image);
|
|
263
|
+
if (copy === image) {
|
|
264
|
+
if (typeof image !== 'object' || image === null) {
|
|
265
|
+
throw fail('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${imageHint(image)}`, 'image');
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
const premultiplied = checkImageBody(copy);
|
|
269
|
+
if (copy.data.byteLength >= maxBytes) {
|
|
270
|
+
throw tooLarge(copy.data.byteLength);
|
|
271
|
+
}
|
|
272
|
+
const sent = {
|
|
273
|
+
data: copy.data,
|
|
274
|
+
format: copy.format
|
|
275
|
+
};
|
|
276
|
+
if (copy.premultiplied !== undefined) {
|
|
277
|
+
sent.premultiplied = premultiplied;
|
|
278
|
+
}
|
|
279
|
+
if (copy.width !== undefined) {
|
|
280
|
+
sent.width = copy.width;
|
|
281
|
+
}
|
|
282
|
+
if (copy.height !== undefined) {
|
|
283
|
+
sent.height = copy.height;
|
|
284
|
+
}
|
|
285
|
+
return sent;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
258
288
|
// A detached buffer (transferred to a worker created with transfer: true, say) is reported as the main entry reports it.
|
|
259
|
-
const pixels = ArrayBuffer.isView(image) ? image : image &&
|
|
289
|
+
const pixels = ArrayBuffer.isView(image) ? image : isImageData(image) && ArrayBuffer.isView(image.data) ? image.data : null;
|
|
260
290
|
if (pixels && isDetached(pixels)) {
|
|
261
291
|
throw detachedError(ArrayBuffer.isView(image) ? 'bare' : isImageData(image) ? 'imagedata' : 'data');
|
|
262
292
|
}
|
|
@@ -274,19 +304,6 @@ const send = image => {
|
|
|
274
304
|
height: image.height
|
|
275
305
|
};
|
|
276
306
|
}
|
|
277
|
-
if (image && typeof image === 'object' && !ArrayBuffer.isView(image) && ArrayBuffer.isView(image.data)) {
|
|
278
|
-
const sent = {
|
|
279
|
-
data: image.data,
|
|
280
|
-
format: image.format
|
|
281
|
-
};
|
|
282
|
-
if (image.width !== undefined) {
|
|
283
|
-
sent.width = image.width;
|
|
284
|
-
}
|
|
285
|
-
if (image.height !== undefined) {
|
|
286
|
-
sent.height = image.height;
|
|
287
|
-
}
|
|
288
|
-
return sent;
|
|
289
|
-
}
|
|
290
307
|
return image;
|
|
291
308
|
};
|
|
292
309
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agglomerative-clustering",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-beta.2",
|
|
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",
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"README.md"
|
|
82
82
|
],
|
|
83
83
|
"scripts": {
|
|
84
|
-
"build:wasm": "node scripts/check-emsdk.mjs && mkdirp dist && em++ src/clustering.cpp -O3 -msimd128 -s WASM=1 -s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME='createWasmModule' -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=32MB -s MAXIMUM_MEMORY=2147483648 -s EXPORTED_FUNCTIONS=\"['_library_version','_get_clustering','_clustering_job_start','_clustering_job_step','_clustering_job_result','_clustering_job_free','_mapping_job_start','_mapping_job_step','_mapping_job_result','_mapping_job_free','_get_palette','_get_palette_from_clustering','_get_palette_weights','_quantize','_quantize_with_clustering','_quantize_with_palette','_quantize_indices','_quantize_indices_with_clustering','_quantize_indices_with_palette','_malloc','_free']\" -s EXPORTED_RUNTIME_METHODS=\"['HEAPU8']\" -o dist/clustering.js && node scripts/patch-glue.mjs",
|
|
84
|
+
"build:wasm": "node scripts/check-emsdk.mjs && mkdirp dist && em++ src/clustering.cpp -O3 -msimd128 -s WASM=1 -s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME='createWasmModule' -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=32MB -s MAXIMUM_MEMORY=2147483648 -s EXPORTED_FUNCTIONS=\"['_library_version','_unpremultiply','_premultiply','_get_clustering','_clustering_job_start','_clustering_job_step','_clustering_job_result','_clustering_job_free','_mapping_job_start','_mapping_job_step','_mapping_job_result','_mapping_job_free','_get_palette','_get_palette_from_clustering','_get_palette_weights','_quantize','_quantize_with_clustering','_quantize_with_palette','_quantize_indices','_quantize_indices_with_clustering','_quantize_indices_with_palette','_malloc','_free']\" -s EXPORTED_RUNTIME_METHODS=\"['HEAPU8']\" -o dist/clustering.js && node scripts/patch-glue.mjs",
|
|
85
85
|
"build:cjs": "cross-env BABEL_ENV=cjs babel src --out-dir dist --extensions \".js\" --ignore \"src/worker-thread.js\" --out-file-extension .cjs && node scripts/patch-cjs.mjs",
|
|
86
86
|
"build:esm": "cross-env BABEL_ENV=esm babel src --out-dir dist --extensions \".js\" --out-file-extension .mjs",
|
|
87
87
|
"build:types": "mkdirp dist && node -e \"const fs = require('fs'); for (const f of ['index.d.ts', 'worker.d.ts']) {fs.copyFileSync('src/' + f, 'dist/' + f);} fs.copyFileSync('src/index.d.ts', 'dist/index.d.cts'); fs.writeFileSync('dist/worker.d.cts', fs.readFileSync('src/worker.d.ts', 'utf8').replaceAll(\\\"'./index.js'\\\", \\\"'./index.cjs'\\\"));\"",
|