agglomerative-clustering 2.0.0 → 3.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.
@@ -0,0 +1,395 @@
1
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
2
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
4
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
5
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
6
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
7
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
8
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
9
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
10
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
11
+ function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
12
+ function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
13
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
14
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
15
+ // Shared by both entries (index.js and worker.js) and the worker thread: errors, option snapshots, reading a clustering's
16
+ // header, the Palette and Clustering types, and the markers that carry those types across the worker boundary. Imported as
17
+ // './shared.js'; the build rewrites that to './shared.mjs' or './shared.cjs' (scripts/babel-import-extensions.cjs).
18
+
19
+ // The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
20
+ // is built from, src/version.hpp) in step with it. Do not edit it by hand.
21
+ export var version = '3.0.0-beta.2';
22
+ export var colorSpaces = {
23
+ 'rgb': 0,
24
+ 'oklab': 1
25
+ };
26
+ export var colorSpaceName = function colorSpaceName(code) {
27
+ return code === 1 ? 'oklab' : 'rgb';
28
+ };
29
+
30
+ // Every error this library throws is an Error with a `code` saying what went wrong, so callers needn't match on message
31
+ // text: 'invalid-argument' (image, k, options, palette), 'invalid-clustering', 'unsupported-version' (a clustering from a
32
+ // newer format, or an older one without what was asked for), 'out-of-memory', 'wasm-load-failed', and in the worker entry
33
+ // 'worker-terminated', 'worker-failed' and 'invalid-handle'. When the error is about one argument, `argument` names it
34
+ // for programs (the message is for people): 'image', 'k', 'clustering', 'palette', 'options', or 'options.<key>' for
35
+ // one option.
36
+ export var fail = function fail(code, message, argument) {
37
+ return Object.assign(new Error(message), argument === undefined ? {
38
+ code: code
39
+ } : {
40
+ code: code,
41
+ argument: argument
42
+ });
43
+ };
44
+
45
+ // Type checks by tag rather than `instanceof`, so values from another realm (an iframe, a vm context, a test sandbox,
46
+ // whose Uint8Array is a different constructor) are recognised. A Node Buffer is a Uint8Array by tag too.
47
+ var tag = function tag(value) {
48
+ return Object.prototype.toString.call(value);
49
+ };
50
+ export var isUint8Array = function isUint8Array(value) {
51
+ return ArrayBuffer.isView(value) && tag(value) === '[object Uint8Array]';
52
+ };
53
+ export var isUint8ClampedArray = function isUint8ClampedArray(value) {
54
+ return ArrayBuffer.isView(value) && tag(value) === '[object Uint8ClampedArray]';
55
+ };
56
+ export var isArrayBufferLike = function isArrayBufferLike(value) {
57
+ return tag(value) === '[object ArrayBuffer]' || tag(value) === '[object SharedArrayBuffer]';
58
+ };
59
+
60
+ // An options object is an ordinary object. A Map, Date, ArrayBuffer or typed array has no own keys and would pass as
61
+ // `{}`, silently ignoring what the caller meant. Checked by tag rather than prototype so objects from another realm
62
+ // (an iframe, a test sandbox) are fine.
63
+ export var isOptionsObject = function isOptionsObject(value) {
64
+ return value !== null && _typeof(value) === 'object' && Object.prototype.toString.call(value) === '[object Object]';
65
+ };
66
+
67
+ // One plain copy of an options object, taken once: every enumerable property, own or inherited (so options layered with
68
+ // Object.create(defaults) work and an inherited typo is still caught), each read a single time. Everything after this
69
+ // uses the copy, so what is validated is what is used, even if a property is a getter that answers differently each time.
70
+ // The copy has no prototype, so a key like '__proto__' is just a name.
71
+ export var snapshot = function snapshot(options) {
72
+ var copy = Object.create(null);
73
+ for (var key in options) {
74
+ copy[key] = options[key];
75
+ }
76
+ return copy;
77
+ };
78
+
79
+ // ----- Palette and Clustering ----------------------------------------------------------------------------------------
80
+ // Palettes and clusterings are byte arrays, and nothing about a Uint8Array says which one it is, or (for a palette) which
81
+ // colour space its colours were made in. So the ones this library returns are ordinary Uint8Arrays carrying two hidden,
82
+ // non-enumerable tags: what they are (a symbol property) and, for a palette, `colorSpace`. Being plain Uint8Arrays is the
83
+ // point: everything that takes bytes takes them (indexing, `subarray`, files, encoders), `constructor` is Uint8Array, so
84
+ // `toEqual(new Uint8Array(...))` in a test still passes, and `slice`, `subarray` and structured clones (postMessage,
85
+ // IndexedDB) give plain untagged Uint8Arrays. `Palette` and `Clustering` are how to make one by hand, and `instanceof`
86
+ // recognises them. The tags are found by a symbol registered with Symbol.for, so a copy of the library loaded twice (the
87
+ // ES module and CommonJS builds, a bundler's duplicate, the worker entry's own copy) recognises the other's.
88
+ var paletteBrand = Symbol["for"]('agglomerative-clustering.palette');
89
+ var clusteringBrand = Symbol["for"]('agglomerative-clustering.clustering');
90
+ export var isPalette = function isPalette(value) {
91
+ return isUint8Array(value) && value[paletteBrand] === true;
92
+ };
93
+ export var isClustering = function isClustering(value) {
94
+ return isUint8Array(value) && value[clusteringBrand] === true;
95
+ };
96
+ // Node's console.log and util.inspect call this (other environments ignore it), so the tag shows when debugging:
97
+ // `Palette(oklab) [ 12, 34, ... ]` rather than a plain `Uint8Array(…)`.
98
+ var inspect = Symbol["for"]('nodejs.util.inspect.custom');
99
+ var view = function view(label) {
100
+ return function (depth, options, nodeInspect) {
101
+ var plain = new Uint8Array(this.buffer, this.byteOffset, this.length);
102
+ return "".concat(label(this), " ").concat(typeof nodeInspect === 'function' ? nodeInspect(plain, options) : "[".concat(plain.length, " bytes]"));
103
+ };
104
+ };
105
+ var paletteView = view(function (palette) {
106
+ return "Palette(".concat(palette.colorSpace, ")");
107
+ });
108
+ var clusteringView = view(function () {
109
+ return 'Clustering';
110
+ });
111
+ export var markPalette = function markPalette(bytes, colorSpace) {
112
+ Object.defineProperty(bytes, paletteBrand, {
113
+ value: true
114
+ });
115
+ Object.defineProperty(bytes, 'colorSpace', {
116
+ value: colorSpace
117
+ });
118
+ Object.defineProperty(bytes, inspect, {
119
+ value: paletteView
120
+ });
121
+ return bytes;
122
+ };
123
+ export var markClustering = function markClustering(bytes) {
124
+ // Made by this library (or checked by `Clustering`), so its checksum needn't be checked again (see readHeader).
125
+ checked.add(bytes);
126
+ Object.defineProperty(bytes, clusteringBrand, {
127
+ value: true
128
+ });
129
+ Object.defineProperty(bytes, inspect, {
130
+ value: clusteringView
131
+ });
132
+ return bytes;
133
+ };
134
+
135
+ // Flat RGB bytes with the colour space they were made in, so mapping with one matches the way it was made. `new Palette(bytes,
136
+ // 'oklab')` copies `bytes` (or makes `length` zeros, or takes an array of byte values) and returns a Uint8Array tagged so.
137
+ // A function and not a `class`, because it returns a plain Uint8Array and answers `instanceof` itself, which transpiled class
138
+ // checks would trip over.
139
+ export function Palette(source) {
140
+ var colorSpace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'rgb';
141
+ if (!Object.keys(colorSpaces).includes(colorSpace)) {
142
+ throw fail('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'colorSpace');
143
+ }
144
+ if (source === undefined) {
145
+ source = 0;
146
+ }
147
+ // The same rules as for a palette passed to a function, so a Palette can't hold what they would reject: a number coerced to a
148
+ // byte (300 would become 44), a string turned into 0, or a length that isn't whole colours.
149
+ if (typeof source === 'number') {
150
+ if (!Number.isInteger(source) || source < 0 || source % 3 !== 0) {
151
+ throw fail('invalid-argument', "Invalid palette: a length must be a whole number of bytes, a multiple of 3 (flat RGB bytes).", 'palette');
152
+ }
153
+ } else {
154
+ checkPalette(source);
155
+ }
156
+ return markPalette(new Uint8Array(source), colorSpace);
157
+ }
158
+ Object.defineProperty(Palette, Symbol.hasInstance, {
159
+ value: isPalette
160
+ });
161
+
162
+ // The bytes of a clustering, checked: `new Clustering(bytes)` is how stored clusterings get their type back. Arguments are
163
+ // those of a Uint8Array (bytes to copy, or a buffer with an offset and length to view).
164
+ export function Clustering() {
165
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
166
+ args[_key] = arguments[_key];
167
+ }
168
+ var source = args[0];
169
+ // Bytes, or a buffer to view: never a number (a length would allocate and make an empty clustering) or anything a Uint8Array
170
+ // would coerce. A plain array must hold byte values, as for a palette.
171
+ var isBuffer = isArrayBufferLike(source);
172
+ if (!isUint8Array(source) && !isUint8ClampedArray(source) && !isBuffer && !Array.isArray(source)) {
173
+ throw fail('invalid-clustering', "Invalid clustering: expected its bytes (a Uint8Array, or an ArrayBuffer with an optional offset and length) from getClustering.", 'clustering');
174
+ }
175
+ if (Array.isArray(source) && !source.every(function (value) {
176
+ return Number.isInteger(value) && value >= 0 && value <= 255;
177
+ })) {
178
+ throw fail('invalid-clustering', "Invalid clustering: a plain array must hold byte values, integers from 0 to 255.", 'clustering');
179
+ }
180
+ var bytes;
181
+ try {
182
+ bytes = _construct(Uint8Array, args);
183
+ } catch (error) {
184
+ throw fail('invalid-clustering', "Invalid clustering: ".concat(error.message, "."), 'clustering');
185
+ }
186
+ readHeader(bytes);
187
+ return markClustering(bytes);
188
+ }
189
+ Object.defineProperty(Clustering, Symbol.hasInstance, {
190
+ value: isClustering
191
+ });
192
+
193
+ // Reads a clustering's header, or throws if it isn't a clustering this version can read. Clusterings start with the
194
+ // magic byte 0xAC and their version number (2 or 3). Clusterings from 1.x had no header and could only be recognised by
195
+ // their length, which many other byte strings share (a palette of the right size passed as a clustering was accepted
196
+ // and gave nonsense), so they are no longer read.
197
+ // version 2: [0xAC, 2, colour space, precision], then 9-byte records of colours
198
+ // version 3: [0xAC, 3, header length, record length, colour space, precision, weight unit, weight shift,
199
+ // total weight (u32 LE), 0, grouped error (f32 LE, from header length 17), ...], then records of
200
+ // colours, parent index, lighter weight and cost; from 3.0.0-beta.2 the header holds a checksum (bytes 17-20)
201
+ export var latestVersion = 3;
202
+
203
+ // The version of stored data (clusterings) this build writes. It reads this one and format 2 (see readHeader). Unlike
204
+ // `version`, it changes only when the stored format does, so a 3.x release that keeps the format keeps this number.
205
+ export var formatVersion = latestVersion;
206
+
207
+ // ----- Checksums -----------------------------------------------------------------------------------------------------
208
+ // From 3.0.0-beta.2, a version 3 header holds the CRC-32 of the whole clustering (bytes 17-20, read as zeros while it is
209
+ // computed; see src/clustering.cpp). 0 means none: beta.1 wrote zeros there. A clustering is checked the first time it
210
+ // is used, and the arrays that passed (and the ones this library made) are remembered, so cutting one clustering
211
+ // again and again checks it once. Changing a clustering's bytes after that is not detected here, but the records are
212
+ // also checked as they are replayed, so it still can't give a palette of the wrong size.
213
+ var checksumAt = 17;
214
+ var crcTable = null;
215
+ var crc32 = function crc32(bytes) {
216
+ if (!crcTable) {
217
+ crcTable = new Uint32Array(256);
218
+ for (var n = 0; n < 256; n++) {
219
+ var c = n;
220
+ for (var b = 0; b < 8; b++) {
221
+ c = c & 1 ? 0xEDB88320 ^ c >>> 1 : c >>> 1;
222
+ }
223
+ crcTable[n] = c >>> 0;
224
+ }
225
+ }
226
+ var crc = 0xFFFFFFFF;
227
+ for (var i = 0; i < bytes.length; i++) {
228
+ var _byte = i >= checksumAt && i < checksumAt + 4 ? 0 : bytes[i];
229
+ crc = crcTable[(crc ^ _byte) & 0xFF] ^ crc >>> 8;
230
+ }
231
+ return (crc ^ 0xFFFFFFFF) >>> 0;
232
+ };
233
+ var checked = new WeakSet();
234
+ var damaged = function damaged() {
235
+ return fail('invalid-clustering', "Invalid clustering: its bytes are damaged (the checksum doesn't match): it was truncated, changed or mixed up with other data after getClustering made it. Cluster the image again.", 'clustering');
236
+ };
237
+
238
+ // The message for bytes that aren't a clustering: 1.x clusterings are mentioned only for bytes that could be one (no header,
239
+ // and a length of 0 or 3 more than a multiple of 9).
240
+ var notAClustering = function notAClustering(clustering) {
241
+ var length = clustering.length;
242
+ if (clustering[0] !== 0xAC && (length % 9 === 0 || length % 9 === 3)) {
243
+ return fail('invalid-clustering', "Invalid clustering: not a clustering from getClustering. If it was stored by agglomerative-clustering 1.x, that format is no longer read: cluster the image again.", 'clustering');
244
+ }
245
+ if (clustering[0] === 0xAC && length >= 2 && (clustering[1] === 2 || clustering[1] === 3)) {
246
+ return fail('invalid-clustering', "Invalid clustering: it starts like a format ".concat(clustering[1], " clustering, but its length or header doesn't fit one (").concat(length, " bytes): it was probably truncated or damaged. Cluster the image again."), 'clustering');
247
+ }
248
+ return fail('invalid-clustering', "Invalid clustering: not a clustering from getClustering (".concat(length, " bytes that don't start with a clustering header)."), 'clustering');
249
+ };
250
+ export var readHeader = function readHeader(clustering) {
251
+ if (isPalette(clustering)) {
252
+ throw fail('invalid-clustering', "Invalid clustering: this is a palette, not a clustering. Palettes come from getPalette and getPaletteFromClustering; a clustering comes from getClustering.", 'clustering');
253
+ }
254
+ if (!isUint8Array(clustering)) {
255
+ throw fail('invalid-clustering', "Invalid clustering: must be a Uint8Array from getClustering.", 'clustering');
256
+ }
257
+ var length = clustering.length;
258
+ if (length >= 4 && clustering[0] === 0xAC) {
259
+ var _version = clustering[1];
260
+ if (_version === 2 && clustering[2] <= 1 && (length === 4 || length % 9 === 7)) {
261
+ return {
262
+ version: _version,
263
+ colorSpace: clustering[2],
264
+ precision: clustering[3],
265
+ transparent: 0,
266
+ headerLength: 4,
267
+ recordLength: 9,
268
+ merges: length === 4 ? 0 : (length - 7) / 9
269
+ };
270
+ }
271
+ if (_version === 3 && length >= 13) {
272
+ var headerLength = clustering[2],
273
+ recordLength = clustering[3];
274
+ if (headerLength >= 13 && recordLength >= 21 && headerLength <= length) {
275
+ // A header field with a fixed set of meanings (colour space, weight unit) that holds a value this version
276
+ // doesn't know is how a later release would add one, so it asks for an upgrade rather than misreading it.
277
+ if (clustering[4] > 1 || clustering[6] > 1) {
278
+ throw fail('unsupported-version', "Invalid clustering: it uses a colour space or weight unit that this version of agglomerative-clustering does not know. If it came from a newer version, upgrade the package; otherwise it is damaged.", 'clustering');
279
+ }
280
+ var merges = length < headerLength + 3 ? 0 : Math.floor((length - headerLength - 3) / recordLength);
281
+ var padding = length < headerLength + 3 ? length - headerLength : length - headerLength - 3 - merges * recordLength;
282
+ if (padding < 9) {
283
+ // Headers shorter than 21 bytes (from before 3.0.0-beta.1) have no checksum field. A damaged header length that
284
+ // makes one look that short skips this check, but then the records don't line up, which the replay reports.
285
+ if (!checked.has(clustering) && headerLength >= checksumAt + 4) {
286
+ var stored = (clustering[checksumAt] | clustering[checksumAt + 1] << 8 | clustering[checksumAt + 2] << 16 | clustering[checksumAt + 3] << 24) >>> 0;
287
+ if (stored !== 0 && stored !== crc32(clustering)) {
288
+ throw damaged();
289
+ }
290
+ checked.add(clustering);
291
+ }
292
+ return {
293
+ version: _version,
294
+ colorSpace: clustering[4],
295
+ precision: clustering[5],
296
+ transparent: clustering[12],
297
+ headerLength: headerLength,
298
+ recordLength: recordLength,
299
+ merges: merges
300
+ };
301
+ }
302
+ }
303
+ }
304
+ if (_version > latestVersion) {
305
+ throw fail('unsupported-version', "Invalid clustering: format version ".concat(_version, " is newer than this version of agglomerative-clustering reads. Upgrade the package."), 'clustering');
306
+ }
307
+ }
308
+ throw notAClustering(clustering);
309
+ };
310
+
311
+ // Palettes are flat RGB bytes. An empty palette is allowed: it is what getPalette gives for a fully transparent
312
+ // image, and quantize maps such an image the same way.
313
+ export var checkPalette = function checkPalette(palette) {
314
+ if (isClustering(palette)) {
315
+ throw fail('invalid-argument', "Invalid palette: this is a clustering (from getClustering), not a palette. Cut a palette from it with getPaletteFromClustering, or map with quantizeFromClustering.", 'palette');
316
+ }
317
+ if (!isUint8Array(palette) && !isUint8ClampedArray(palette) && !Array.isArray(palette)) {
318
+ throw fail('invalid-argument', "Invalid palette: must be a Uint8Array of RGB bytes.", 'palette');
319
+ }
320
+ if (palette.length % 3 !== 0) {
321
+ throw fail('invalid-argument', "Invalid palette: length must be a multiple of 3 (flat RGB bytes).", 'palette');
322
+ }
323
+ // Typed arrays are bytes already. A plain array is copied into bytes, which would turn an array of [r, g, b] colours
324
+ // into zeros, strings into numbers and 300 into 44 without a word, so it must hold byte values.
325
+ if (Array.isArray(palette)) {
326
+ var _iterator = _createForOfIteratorHelper(palette),
327
+ _step;
328
+ try {
329
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
330
+ var value = _step.value;
331
+ if (!Number.isInteger(value) || value < 0 || value > 255) {
332
+ throw fail('invalid-argument', "Invalid palette: a plain array must be flat RGB bytes, integers from 0 to 255 (not an array of [r, g, b] colours).", 'palette');
333
+ }
334
+ }
335
+ } catch (err) {
336
+ _iterator.e(err);
337
+ } finally {
338
+ _iterator.f();
339
+ }
340
+ }
341
+ };
342
+
343
+ // ----- Across the worker boundary ------------------------------------------------------------------------------------
344
+ // A structured clone drops the tags (a palette's colour space with them), so palettes and clusterings cross to and from the
345
+ // worker thread as small markers and become typed arrays again on the other side: `mark` before posting (an argument, a
346
+ // result, or a property of a result), `revive` after receiving.
347
+ export var MARK = '__agglomerativeClustering';
348
+ var markOne = function markOne(value) {
349
+ if (isPalette(value)) {
350
+ return _defineProperty(_defineProperty(_defineProperty({}, MARK, 'palette'), "bytes", new Uint8Array(value.buffer, value.byteOffset, value.length)), "colorSpace", value.colorSpace);
351
+ }
352
+ if (isClustering(value)) {
353
+ return _defineProperty(_defineProperty({}, MARK, 'clustering'), "bytes", new Uint8Array(value.buffer, value.byteOffset, value.length));
354
+ }
355
+ return value;
356
+ };
357
+ export var mark = function mark(value) {
358
+ if (!value || _typeof(value) !== 'object' || ArrayBuffer.isView(value)) {
359
+ return markOne(value);
360
+ }
361
+ if (!isOptionsObject(value)) {
362
+ return value;
363
+ }
364
+ return Object.fromEntries(Object.entries(value).map(function (_ref3) {
365
+ var _ref4 = _slicedToArray(_ref3, 2),
366
+ key = _ref4[0],
367
+ part = _ref4[1];
368
+ return [key, markOne(part)];
369
+ }));
370
+ };
371
+ var reviveOne = function reviveOne(value) {
372
+ if (!value || _typeof(value) !== 'object' || !value[MARK]) {
373
+ return value;
374
+ }
375
+ if (value[MARK] === 'palette') {
376
+ return markPalette(new Uint8Array(value.bytes.buffer, value.bytes.byteOffset, value.bytes.length), value.colorSpace);
377
+ }
378
+ return markClustering(new Uint8Array(value.bytes.buffer, value.bytes.byteOffset, value.bytes.length));
379
+ };
380
+ export var revive = function revive(value) {
381
+ if (!value || _typeof(value) !== 'object' || ArrayBuffer.isView(value)) {
382
+ return value;
383
+ }
384
+ if (value[MARK]) {
385
+ return reviveOne(value);
386
+ }
387
+ if (!isOptionsObject(value)) {
388
+ return value;
389
+ }
390
+ for (var _i = 0, _Object$keys = Object.keys(value); _i < _Object$keys.length; _i++) {
391
+ var key = _Object$keys[_i];
392
+ value[key] = reviveOne(value[key]);
393
+ }
394
+ return value;
395
+ };