agglomerative-clustering 4.0.0-beta.1 → 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.
Binary file
package/dist/index.cjs CHANGED
@@ -534,13 +534,7 @@ const load = image => {
534
534
  if (typeof image !== 'object' || !image || !image.data || !image.format) {
535
535
  throw (0, _shared.fail)('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${(0, _shared.imageHint)(image)}`, 'image');
536
536
  }
537
- if (!(0, _shared.isUint8Array)(image.data) && !(0, _shared.isUint8ClampedArray)(image.data)) {
538
- throw (0, _shared.fail)('invalid-argument', "Invalid image data: must be Uint8Array or Uint8ClampedArray.", 'image');
539
- }
540
- if ((0, _shared.isDetached)(image.data)) {
541
- throw (0, _shared.detachedError)('data');
542
- }
543
- const premultiplied = (0, _shared.checkImageObject)(image);
537
+ const premultiplied = (0, _shared.checkImageBody)(image);
544
538
  return {
545
539
  data: held(bytes(image.data)),
546
540
  format: image.format,
@@ -1020,6 +1014,7 @@ const noOptions = (name, extra) => {
1020
1014
  // happens before the call returns.
1021
1015
 
1022
1016
  const buildClustering = async (image, options) => {
1017
+ image = (0, _shared.imageCopy)(image);
1023
1018
  const warn = warningFor(image);
1024
1019
  image = load(image);
1025
1020
  const o = parseOptions(options, clusterOptions, image);
@@ -1037,6 +1032,7 @@ const buildClustering = async (image, options) => {
1037
1032
  exports.buildClustering = buildClustering;
1038
1033
  const getClustering = exports.getClustering = buildClustering;
1039
1034
  const buildPalette = async (image, k, options) => {
1035
+ image = (0, _shared.imageCopy)(image);
1040
1036
  const warn = warningFor(image);
1041
1037
  image = load(image);
1042
1038
  k = check(k);
@@ -1119,7 +1115,9 @@ const getSwatchesFromClustering = (clustering, k, ...extra) => cutSwatches('getS
1119
1115
  // The same for an image: clusters it, then cuts, like getPalette.
1120
1116
  exports.getSwatchesFromClustering = getSwatchesFromClustering;
1121
1117
  const buildSwatches = async (image, k, options) => {
1118
+ image = (0, _shared.imageCopy)(image);
1122
1119
  // Arguments are checked in the order they are given (image, then k, then options), as in every other function.
1120
+ // `load` only validates here (buildClustering loads again); both see the copy, so the caller's fields are read once.
1123
1121
  load(image);
1124
1122
  k = check(k);
1125
1123
  return getSwatchesFromClustering(await buildClustering(image, options), k);
@@ -1177,6 +1175,7 @@ const readInfo = async (name, clustering, extra) => {
1177
1175
  const getClusteringInfo = (clustering, ...extra) => readInfo('getClusteringInfo', clustering, extra);
1178
1176
  exports.getClusteringInfo = getClusteringInfo;
1179
1177
  const quantize = async (image, k, options) => {
1178
+ image = (0, _shared.imageCopy)(image);
1180
1179
  const warn = warningFor(image);
1181
1180
  const wrap = getWrapper(image);
1182
1181
  image = load(image);
@@ -1199,6 +1198,7 @@ const quantize = async (image, k, options) => {
1199
1198
  };
1200
1199
  exports.quantize = quantize;
1201
1200
  const quantizeFromClustering = async (image, clustering, k, options) => {
1201
+ image = (0, _shared.imageCopy)(image);
1202
1202
  const warn = warningFor(image);
1203
1203
  const wrap = getWrapper(image);
1204
1204
  image = load(image);
@@ -1224,6 +1224,7 @@ const quantizeFromClustering = async (image, clustering, k, options) => {
1224
1224
  };
1225
1225
  exports.quantizeFromClustering = quantizeFromClustering;
1226
1226
  const quantizeWithPalette = async (image, palette, options) => {
1227
+ image = (0, _shared.imageCopy)(image);
1227
1228
  const warn = warningFor(image);
1228
1229
  const wrap = getWrapper(image);
1229
1230
  image = load(image);
@@ -1248,6 +1249,7 @@ const quantizeWithPalette = async (image, palette, options) => {
1248
1249
  };
1249
1250
  exports.quantizeWithPalette = quantizeWithPalette;
1250
1251
  const quantizeToIndices = async (image, k, options) => {
1252
+ image = (0, _shared.imageCopy)(image);
1251
1253
  const warn = warningFor(image);
1252
1254
  const original = image;
1253
1255
  image = load(image);
@@ -1270,6 +1272,7 @@ const quantizeToIndices = async (image, k, options) => {
1270
1272
  };
1271
1273
  exports.quantizeToIndices = quantizeToIndices;
1272
1274
  const quantizeToIndicesFromClustering = async (image, clustering, k, options) => {
1275
+ image = (0, _shared.imageCopy)(image);
1273
1276
  const warn = warningFor(image);
1274
1277
  const original = image;
1275
1278
  image = load(image);
@@ -1298,6 +1301,7 @@ const quantizeToIndicesFromClustering = async (image, clustering, k, options) =>
1298
1301
  };
1299
1302
  exports.quantizeToIndicesFromClustering = quantizeToIndicesFromClustering;
1300
1303
  const quantizeToIndicesWithPalette = async (image, palette, options) => {
1304
+ image = (0, _shared.imageCopy)(image);
1301
1305
  const warn = warningFor(image);
1302
1306
  const original = image;
1303
1307
  image = load(image);
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, bareImageWarning, warnOnce, checkImageObject, checkSize, formatHint } from "./shared.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, bareImageWarning, warnOnce, checkImageBody, imageCopy, checkSize, formatHint } 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";
@@ -458,13 +458,7 @@ const load = image => {
458
458
  if (typeof image !== 'object' || !image || !image.data || !image.format) {
459
459
  throw fail('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${imageHint(image)}`, 'image');
460
460
  }
461
- if (!isUint8Array(image.data) && !isUint8ClampedArray(image.data)) {
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);
461
+ const premultiplied = checkImageBody(image);
468
462
  return {
469
463
  data: held(bytes(image.data)),
470
464
  format: image.format,
@@ -944,6 +938,7 @@ const noOptions = (name, extra) => {
944
938
  // happens before the call returns.
945
939
 
946
940
  export const buildClustering = async (image, options) => {
941
+ image = imageCopy(image);
947
942
  const warn = warningFor(image);
948
943
  image = load(image);
949
944
  const o = parseOptions(options, clusterOptions, image);
@@ -960,6 +955,7 @@ export const buildClustering = async (image, options) => {
960
955
  /** @deprecated The same function as buildClustering, its name before 3.4.0. Removed in 4.0. */
961
956
  export const getClustering = buildClustering;
962
957
  export const buildPalette = async (image, k, options) => {
958
+ image = imageCopy(image);
963
959
  const warn = warningFor(image);
964
960
  image = load(image);
965
961
  k = check(k);
@@ -1039,7 +1035,9 @@ export const getSwatchesFromClustering = (clustering, k, ...extra) => cutSwatche
1039
1035
 
1040
1036
  // The same for an image: clusters it, then cuts, like getPalette.
1041
1037
  export const buildSwatches = async (image, k, options) => {
1038
+ image = imageCopy(image);
1042
1039
  // Arguments are checked in the order they are given (image, then k, then options), as in every other function.
1040
+ // `load` only validates here (buildClustering loads again); both see the copy, so the caller's fields are read once.
1043
1041
  load(image);
1044
1042
  k = check(k);
1045
1043
  return getSwatchesFromClustering(await buildClustering(image, options), k);
@@ -1095,6 +1093,7 @@ const readInfo = async (name, clustering, extra) => {
1095
1093
  };
1096
1094
  export const getClusteringInfo = (clustering, ...extra) => readInfo('getClusteringInfo', clustering, extra);
1097
1095
  export const quantize = async (image, k, options) => {
1096
+ image = imageCopy(image);
1098
1097
  const warn = warningFor(image);
1099
1098
  const wrap = getWrapper(image);
1100
1099
  image = load(image);
@@ -1116,6 +1115,7 @@ export const quantize = async (image, k, options) => {
1116
1115
  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
1116
  };
1118
1117
  export const quantizeFromClustering = async (image, clustering, k, options) => {
1118
+ image = imageCopy(image);
1119
1119
  const warn = warningFor(image);
1120
1120
  const wrap = getWrapper(image);
1121
1121
  image = load(image);
@@ -1140,6 +1140,7 @@ export const quantizeFromClustering = async (image, clustering, k, options) => {
1140
1140
  return result;
1141
1141
  };
1142
1142
  export const quantizeWithPalette = async (image, palette, options) => {
1143
+ image = imageCopy(image);
1143
1144
  const warn = warningFor(image);
1144
1145
  const wrap = getWrapper(image);
1145
1146
  image = load(image);
@@ -1163,6 +1164,7 @@ export const quantizeWithPalette = async (image, palette, options) => {
1163
1164
  return result;
1164
1165
  };
1165
1166
  export const quantizeToIndices = async (image, k, options) => {
1167
+ image = imageCopy(image);
1166
1168
  const warn = warningFor(image);
1167
1169
  const original = image;
1168
1170
  image = load(image);
@@ -1184,6 +1186,7 @@ export const quantizeToIndices = async (image, k, options) => {
1184
1186
  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
1187
  };
1186
1188
  export const quantizeToIndicesFromClustering = async (image, clustering, k, options) => {
1189
+ image = imageCopy(image);
1187
1190
  const warn = warningFor(image);
1188
1191
  const original = image;
1189
1192
  image = load(image);
@@ -1211,6 +1214,7 @@ export const quantizeToIndicesFromClustering = async (image, clustering, k, opti
1211
1214
  return result;
1212
1215
  };
1213
1216
  export const quantizeToIndicesWithPalette = async (image, palette, options) => {
1217
+ image = imageCopy(image);
1214
1218
  const warn = warningFor(image);
1215
1219
  const original = image;
1216
1220
  image = load(image);
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.formatHint = exports.fail = exports.errorCodes = exports.ditherSteps = exports.ditherMethods = exports.detachedError = exports.describe = exports.colorSpaces = exports.colorSpaceName = exports.colorSpaceCodes = exports.checkSize = exports.checkPalette = exports.checkImageObject = exports.checkImageKeys = exports.checkImageDataPixels = exports.channels = exports.bareLayout = exports.bareImageWarning = exports.alphaModes = void 0;
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.imageCopy = exports.hasAlpha = exports.formatVersion = exports.formatNames = exports.formatHint = exports.fail = exports.errorCodes = exports.ditherSteps = exports.ditherMethods = exports.detachedError = exports.describe = exports.colorSpaces = exports.colorSpaceName = exports.colorSpaceCodes = exports.checkSize = exports.checkPalette = exports.checkImageObject = exports.checkImageKeys = exports.checkImageDataPixels = exports.checkImageBody = 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 = '4.0.0-beta.1';
16
+ const version = exports.version = '4.0.0-beta.2';
17
17
  const colorSpaceCodes = exports.colorSpaceCodes = {
18
18
  'rgb': 0,
19
19
  'oklab': 1
@@ -300,10 +300,52 @@ const checkImageObject = image => {
300
300
  return checkPremultiplied(image);
301
301
  };
302
302
 
303
+ // The image object's own fields, read once: `data`, `format`, `width`, `height` and `premultiplied` are taken from it here, and
304
+ // everything after (validation and use) reads the copy, so a getter that answers differently on a later read changes nothing.
305
+ // The copy also names the object's other own enumerable keys (with no value, never read), in order, for the unknown-key check.
306
+ // Anything that isn't an object with `data` and `format` is returned as it is (a bare array, ImageData, a function...), or, for
307
+ // an object without them, rejected here with the shape error the main entry gives.
308
+ exports.checkImageObject = checkImageObject;
309
+ const imageCopy = image => {
310
+ if (typeof image !== 'object' || image === null || ArrayBuffer.isView(image) || isImageData(image)) {
311
+ return image;
312
+ }
313
+ const data = image.data;
314
+ const format = image.format;
315
+ if (!data || !format) {
316
+ throw fail('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${imageHint(image)}`, 'image');
317
+ }
318
+ const copy = Object.create(null);
319
+ copy.data = data;
320
+ copy.format = format;
321
+ copy.width = image.width;
322
+ copy.height = image.height;
323
+ copy.premultiplied = image.premultiplied;
324
+ for (const key of Object.keys(image)) {
325
+ if (!imageKeys.includes(key)) {
326
+ copy[key] = undefined;
327
+ }
328
+ }
329
+ return copy;
330
+ };
331
+
332
+ // What the main entry's `load` checks about an image object, from the copy imageCopy made, in its order: data's type, whether its
333
+ // buffer is detached, then checkImageObject. The worker entry runs the same before it posts an image. Returns the premultiplied flag.
334
+ exports.imageCopy = imageCopy;
335
+ const checkImageBody = image => {
336
+ if (!isUint8Array(image.data) && !isUint8ClampedArray(image.data)) {
337
+ throw fail('invalid-argument', "Invalid image data: must be Uint8Array or Uint8ClampedArray.", 'image');
338
+ }
339
+ if (isDetached(image.data)) {
340
+ throw detachedError('data');
341
+ }
342
+ return checkImageObject(image);
343
+ };
344
+
303
345
  // An options object is an ordinary object. A Map, Date, ArrayBuffer or typed array has no own keys and would pass as
304
346
  // `{}`, silently ignoring what the caller meant. Checked by tag rather than prototype so objects from another realm
305
347
  // (an iframe, a test sandbox) are fine.
306
- exports.checkImageObject = checkImageObject;
348
+ exports.checkImageBody = checkImageBody;
307
349
  const isOptionsObject = value => value !== null && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]';
308
350
 
309
351
  // One plain copy of an options object, taken once: every enumerable property, own or inherited (so options layered with
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 = '4.0.0-beta.1';
7
+ export const version = '4.0.0-beta.2';
8
8
  export const colorSpaceCodes = {
9
9
  'rgb': 0,
10
10
  'oklab': 1
@@ -272,6 +272,46 @@ export const checkImageObject = image => {
272
272
  return checkPremultiplied(image);
273
273
  };
274
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
+
275
315
  // An options object is an ordinary object. A Map, Date, ArrayBuffer or typed array has no own keys and would pass as
276
316
  // `{}`, silently ignoring what the caller meant. Checked by tag rather than prototype so objects from another realm
277
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 : image && typeof image === 'object' && ArrayBuffer.isView(image.data) ? image.data : null;
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,29 +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
- // The main entry's checks (shape, unknown keys, premultiplied) run here, in its order, before anything is posted. Data of another
348
- // type, or no format, goes on as it is for the worker's own error.
349
- const sent = {
350
- data: image.data,
351
- format: image.format
352
- };
353
- if (image.format && ((0, _shared.isUint8Array)(image.data) || (0, _shared.isUint8ClampedArray)(image.data))) {
354
- const premultiplied = (0, _shared.checkImageObject)(image);
355
- if (image.premultiplied !== undefined) {
356
- sent.premultiplied = premultiplied;
357
- }
358
- } else if (image.premultiplied !== undefined) {
359
- sent.premultiplied = image.premultiplied;
360
- }
361
- if (image.width !== undefined) {
362
- sent.width = image.width;
363
- }
364
- if (image.height !== undefined) {
365
- sent.height = image.height;
366
- }
367
- return sent;
368
- }
369
376
  return image;
370
377
  };
371
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, checkImageObject } 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 && typeof image === 'object' && ArrayBuffer.isView(image.data) ? image.data : null;
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,29 +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
- // The main entry's checks (shape, unknown keys, premultiplied) run here, in its order, before anything is posted. Data of another
279
- // type, or no format, goes on as it is for the worker's own error.
280
- const sent = {
281
- data: image.data,
282
- format: image.format
283
- };
284
- if (image.format && (isUint8Array(image.data) || isUint8ClampedArray(image.data))) {
285
- const premultiplied = checkImageObject(image);
286
- if (image.premultiplied !== undefined) {
287
- sent.premultiplied = premultiplied;
288
- }
289
- } else if (image.premultiplied !== undefined) {
290
- sent.premultiplied = image.premultiplied;
291
- }
292
- if (image.width !== undefined) {
293
- sent.width = image.width;
294
- }
295
- if (image.height !== undefined) {
296
- sent.height = image.height;
297
- }
298
- return sent;
299
- }
300
307
  return image;
301
308
  };
302
309
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agglomerative-clustering",
3
- "version": "4.0.0-beta.1",
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",