pixel-data-js 0.15.0 → 0.16.0

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.
@@ -20,19 +20,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- BlendMode: () => BlendMode,
24
- FAST_BLENDER_REGISTRY: () => FAST_BLENDER_REGISTRY,
25
- FAST_BLEND_MODES: () => FAST_BLEND_MODES,
26
- FAST_BLEND_MODE_BY_NAME: () => FAST_BLEND_MODE_BY_NAME,
27
- FAST_BLEND_TO_INDEX: () => FAST_BLEND_TO_INDEX,
28
- INDEX_TO_FAST_BLEND: () => INDEX_TO_FAST_BLEND,
29
- INDEX_TO_PERFECT_BLEND: () => INDEX_TO_PERFECT_BLEND,
23
+ BASE_FAST_BLEND_MODE_FUNCTIONS: () => BASE_FAST_BLEND_MODE_FUNCTIONS,
24
+ BASE_PERFECT_BLEND_MODE_FUNCTIONS: () => BASE_PERFECT_BLEND_MODE_FUNCTIONS,
25
+ BaseBlendMode: () => BaseBlendMode,
30
26
  IndexedImage: () => IndexedImage,
31
27
  MaskType: () => MaskType,
32
- PERFECT_BLENDER_REGISTRY: () => PERFECT_BLENDER_REGISTRY,
33
- PERFECT_BLEND_MODES: () => PERFECT_BLEND_MODES,
34
- PERFECT_BLEND_MODE_BY_NAME: () => PERFECT_BLEND_MODE_BY_NAME,
35
- PERFECT_BLEND_TO_INDEX: () => PERFECT_BLEND_TO_INDEX,
36
28
  PixelData: () => PixelData,
37
29
  UnsupportedFormatError: () => UnsupportedFormatError,
38
30
  applyMaskToPixelData: () => applyMaskToPixelData,
@@ -66,6 +58,7 @@ __export(src_exports, {
66
58
  exclusionPerfect: () => exclusionPerfect,
67
59
  extractImageDataBuffer: () => extractImageDataBuffer,
68
60
  extractMask: () => extractMask,
61
+ extractPixelData: () => extractPixelData,
69
62
  extractPixelDataBuffer: () => extractPixelDataBuffer,
70
63
  fileInputChangeToImageData: () => fileInputChangeToImageData,
71
64
  fileToImageData: () => fileToImageData,
@@ -101,6 +94,9 @@ __export(src_exports, {
101
94
  linearDodgePerfect: () => linearDodgePerfect,
102
95
  linearLightFast: () => linearLightFast,
103
96
  linearLightPerfect: () => linearLightPerfect,
97
+ makeBlendModeRegistry: () => makeBlendModeRegistry,
98
+ makeFastBlendModeRegistry: () => makeFastBlendModeRegistry,
99
+ makePerfectBlendModeRegistry: () => makePerfectBlendModeRegistry,
104
100
  makePixelCanvas: () => makePixelCanvas,
105
101
  makeReusableCanvas: () => makeReusableCanvas,
106
102
  makeReusableImageData: () => makeReusableImageData,
@@ -109,6 +105,7 @@ __export(src_exports, {
109
105
  multiplyPerfect: () => multiplyPerfect,
110
106
  overlayFast: () => overlayFast,
111
107
  overlayPerfect: () => overlayPerfect,
108
+ overwriteBase: () => overwriteBase,
112
109
  overwriteFast: () => overwriteFast,
113
110
  overwritePerfect: () => overwritePerfect,
114
111
  packColor: () => packColor,
@@ -150,35 +147,68 @@ __export(src_exports, {
150
147
  module.exports = __toCommonJS(src_exports);
151
148
 
152
149
  // src/BlendModes/blend-modes.ts
153
- var BlendMode = /* @__PURE__ */ ((BlendMode2) => {
154
- BlendMode2[BlendMode2["overwrite"] = 0] = "overwrite";
155
- BlendMode2[BlendMode2["sourceOver"] = 1] = "sourceOver";
156
- BlendMode2[BlendMode2["darken"] = 2] = "darken";
157
- BlendMode2[BlendMode2["multiply"] = 3] = "multiply";
158
- BlendMode2[BlendMode2["colorBurn"] = 4] = "colorBurn";
159
- BlendMode2[BlendMode2["linearBurn"] = 5] = "linearBurn";
160
- BlendMode2[BlendMode2["darkerColor"] = 6] = "darkerColor";
161
- BlendMode2[BlendMode2["lighten"] = 7] = "lighten";
162
- BlendMode2[BlendMode2["screen"] = 8] = "screen";
163
- BlendMode2[BlendMode2["colorDodge"] = 9] = "colorDodge";
164
- BlendMode2[BlendMode2["linearDodge"] = 10] = "linearDodge";
165
- BlendMode2[BlendMode2["lighterColor"] = 11] = "lighterColor";
166
- BlendMode2[BlendMode2["overlay"] = 12] = "overlay";
167
- BlendMode2[BlendMode2["softLight"] = 13] = "softLight";
168
- BlendMode2[BlendMode2["hardLight"] = 14] = "hardLight";
169
- BlendMode2[BlendMode2["vividLight"] = 15] = "vividLight";
170
- BlendMode2[BlendMode2["linearLight"] = 16] = "linearLight";
171
- BlendMode2[BlendMode2["pinLight"] = 17] = "pinLight";
172
- BlendMode2[BlendMode2["hardMix"] = 18] = "hardMix";
173
- BlendMode2[BlendMode2["difference"] = 19] = "difference";
174
- BlendMode2[BlendMode2["exclusion"] = 20] = "exclusion";
175
- BlendMode2[BlendMode2["subtract"] = 21] = "subtract";
176
- BlendMode2[BlendMode2["divide"] = 22] = "divide";
177
- return BlendMode2;
178
- })(BlendMode || {});
150
+ var BaseBlendMode = {
151
+ overwrite: 0,
152
+ sourceOver: 1,
153
+ darken: 2,
154
+ multiply: 3,
155
+ colorBurn: 4,
156
+ linearBurn: 5,
157
+ darkerColor: 6,
158
+ lighten: 7,
159
+ screen: 8,
160
+ colorDodge: 9,
161
+ linearDodge: 10,
162
+ lighterColor: 11,
163
+ overlay: 12,
164
+ softLight: 13,
165
+ hardLight: 14,
166
+ vividLight: 15,
167
+ linearLight: 16,
168
+ pinLight: 17,
169
+ hardMix: 18,
170
+ difference: 19,
171
+ exclusion: 20,
172
+ subtract: 21,
173
+ divide: 22
174
+ };
179
175
  var overwriteBase = (src, _dst) => src;
180
176
  overwriteBase.isOverwrite = true;
181
177
 
178
+ // src/BlendModes/BlendModeRegistry.ts
179
+ function makeBlendModeRegistry(blendModes, initialEntries) {
180
+ const modes = [];
181
+ const toIndex = /* @__PURE__ */ new Map();
182
+ const fromIndex = /* @__PURE__ */ new Map();
183
+ const byName = {};
184
+ const add = (name, index, blendFn) => {
185
+ if (modes[index]) {
186
+ throw new Error(`Blend Mode index: ${index} is already used`);
187
+ }
188
+ if (byName[name]) {
189
+ throw new Error(`Blend Mode name: "${name}" is already used`);
190
+ }
191
+ const idx = index;
192
+ modes[idx] = blendFn;
193
+ toIndex.set(blendFn, idx);
194
+ fromIndex.set(idx, blendFn);
195
+ byName[name] = blendFn;
196
+ };
197
+ for (const [name, index] of Object.entries(blendModes)) {
198
+ const blend = initialEntries[index];
199
+ add(name, index, blend);
200
+ }
201
+ return {
202
+ modes,
203
+ byName,
204
+ toIndex,
205
+ fromIndex,
206
+ add,
207
+ indexType: null,
208
+ nameType: null
209
+ };
210
+ }
211
+
182
212
  // src/BlendModes/blend-modes-fast.ts
183
213
  var overwriteFast = overwriteBase;
184
214
  var sourceOverFast = (src, dst) => {
@@ -564,76 +594,34 @@ var divideFast = (src, dst) => {
564
594
  const a = 255 * sa + (dst >>> 24 & 255) * invA >> 8;
565
595
  return (a << 24 | b << 16 | g << 8 | r) >>> 0;
566
596
  };
567
- var FAST_BLENDER_REGISTRY = [
568
- [0 /* overwrite */, overwriteFast],
569
- [1 /* sourceOver */, sourceOverFast],
570
- [2 /* darken */, darkenFast],
571
- [3 /* multiply */, multiplyFast],
572
- [4 /* colorBurn */, colorBurnFast],
573
- [5 /* linearBurn */, linearBurnFast],
574
- [6 /* darkerColor */, darkerFast],
575
- [7 /* lighten */, lightenFast],
576
- [8 /* screen */, screenFast],
577
- [9 /* colorDodge */, colorDodgeFast],
578
- [10 /* linearDodge */, linearDodgeFast],
579
- [11 /* lighterColor */, lighterFast],
580
- [12 /* overlay */, overlayFast],
581
- [13 /* softLight */, softLightFast],
582
- [14 /* hardLight */, hardLightFast],
583
- [15 /* vividLight */, vividLightFast],
584
- [16 /* linearLight */, linearLightFast],
585
- [17 /* pinLight */, pinLightFast],
586
- [18 /* hardMix */, hardMixFast],
587
- [19 /* difference */, differenceFast],
588
- [20 /* exclusion */, exclusionFast],
589
- [21 /* subtract */, subtractFast],
590
- [22 /* divide */, divideFast]
591
- ];
592
- var FAST_BLEND_MODES = [];
593
- for (const [index, blend] of FAST_BLENDER_REGISTRY) {
594
- FAST_BLEND_MODES[index] = blend;
595
- }
596
- var FAST_BLEND_TO_INDEX = new Map(
597
- FAST_BLENDER_REGISTRY.map((entry, index) => {
598
- return [
599
- entry[1],
600
- index
601
- ];
602
- })
603
- );
604
- var INDEX_TO_FAST_BLEND = new Map(
605
- FAST_BLENDER_REGISTRY.map((entry, index) => {
606
- return [
607
- index,
608
- entry[1]
609
- ];
610
- })
611
- );
612
- var FAST_BLEND_MODE_BY_NAME = {
613
- overwrite: overwriteFast,
614
- sourceOver: sourceOverFast,
615
- darken: darkenFast,
616
- multiply: multiplyFast,
617
- colorBurn: colorBurnFast,
618
- linearBurn: linearBurnFast,
619
- darkerColor: darkerFast,
620
- lighten: lightenFast,
621
- screen: screenFast,
622
- colorDodge: colorDodgeFast,
623
- linearDodge: linearDodgeFast,
624
- lighterColor: lighterFast,
625
- overlay: overlayFast,
626
- softLight: softLightFast,
627
- hardLight: hardLightFast,
628
- vividLight: vividLightFast,
629
- linearLight: linearLightFast,
630
- pinLight: pinLightFast,
631
- hardMix: hardMixFast,
632
- difference: differenceFast,
633
- exclusion: exclusionFast,
634
- subtract: subtractFast,
635
- divide: divideFast
597
+ var BASE_FAST_BLEND_MODE_FUNCTIONS = {
598
+ [BaseBlendMode.overwrite]: overwriteFast,
599
+ [BaseBlendMode.sourceOver]: sourceOverFast,
600
+ [BaseBlendMode.darken]: darkenFast,
601
+ [BaseBlendMode.multiply]: multiplyFast,
602
+ [BaseBlendMode.colorBurn]: colorBurnFast,
603
+ [BaseBlendMode.linearBurn]: linearBurnFast,
604
+ [BaseBlendMode.darkerColor]: darkerFast,
605
+ [BaseBlendMode.lighten]: lightenFast,
606
+ [BaseBlendMode.screen]: screenFast,
607
+ [BaseBlendMode.colorDodge]: colorDodgeFast,
608
+ [BaseBlendMode.linearDodge]: linearDodgeFast,
609
+ [BaseBlendMode.lighterColor]: lighterFast,
610
+ [BaseBlendMode.overlay]: overlayFast,
611
+ [BaseBlendMode.softLight]: softLightFast,
612
+ [BaseBlendMode.hardLight]: hardLightFast,
613
+ [BaseBlendMode.vividLight]: vividLightFast,
614
+ [BaseBlendMode.linearLight]: linearLightFast,
615
+ [BaseBlendMode.pinLight]: pinLightFast,
616
+ [BaseBlendMode.hardMix]: hardMixFast,
617
+ [BaseBlendMode.difference]: differenceFast,
618
+ [BaseBlendMode.exclusion]: exclusionFast,
619
+ [BaseBlendMode.subtract]: subtractFast,
620
+ [BaseBlendMode.divide]: divideFast
636
621
  };
622
+ function makeFastBlendModeRegistry() {
623
+ return makeBlendModeRegistry(BaseBlendMode, BASE_FAST_BLEND_MODE_FUNCTIONS);
624
+ }
637
625
 
638
626
  // src/_types.ts
639
627
  var MaskType = /* @__PURE__ */ ((MaskType2) => {
@@ -1394,76 +1382,34 @@ var dividePerfect = (src, dst) => {
1394
1382
  const a = (255 * sa + (dst >>> 24 & 255) * invA) / 255 | 0;
1395
1383
  return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1396
1384
  };
1397
- var PERFECT_BLENDER_REGISTRY = [
1398
- [0 /* overwrite */, overwritePerfect],
1399
- [1 /* sourceOver */, sourceOverPerfect],
1400
- [2 /* darken */, darkenPerfect],
1401
- [3 /* multiply */, multiplyPerfect],
1402
- [4 /* colorBurn */, colorBurnPerfect],
1403
- [5 /* linearBurn */, linearBurnPerfect],
1404
- [6 /* darkerColor */, darkerPerfect],
1405
- [7 /* lighten */, lightenPerfect],
1406
- [8 /* screen */, screenPerfect],
1407
- [9 /* colorDodge */, colorDodgePerfect],
1408
- [10 /* linearDodge */, linearDodgePerfect],
1409
- [11 /* lighterColor */, lighterPerfect],
1410
- [12 /* overlay */, overlayPerfect],
1411
- [13 /* softLight */, softLightPerfect],
1412
- [14 /* hardLight */, hardLightPerfect],
1413
- [15 /* vividLight */, vividLightPerfect],
1414
- [16 /* linearLight */, linearLightPerfect],
1415
- [17 /* pinLight */, pinLightPerfect],
1416
- [18 /* hardMix */, hardMixPerfect],
1417
- [19 /* difference */, differencePerfect],
1418
- [20 /* exclusion */, exclusionPerfect],
1419
- [21 /* subtract */, subtractPerfect],
1420
- [22 /* divide */, dividePerfect]
1421
- ];
1422
- var PERFECT_BLEND_MODES = [];
1423
- for (const [index, blend] of PERFECT_BLENDER_REGISTRY) {
1424
- PERFECT_BLEND_MODES[index] = blend;
1425
- }
1426
- var PERFECT_BLEND_TO_INDEX = new Map(
1427
- PERFECT_BLENDER_REGISTRY.map((entry, index) => {
1428
- return [
1429
- entry[1],
1430
- index
1431
- ];
1432
- })
1433
- );
1434
- var INDEX_TO_PERFECT_BLEND = new Map(
1435
- PERFECT_BLENDER_REGISTRY.map((entry, index) => {
1436
- return [
1437
- index,
1438
- entry[1]
1439
- ];
1440
- })
1441
- );
1442
- var PERFECT_BLEND_MODE_BY_NAME = {
1443
- overwrite: overwritePerfect,
1444
- sourceOver: sourceOverPerfect,
1445
- darken: darkenPerfect,
1446
- multiply: multiplyPerfect,
1447
- colorBurn: colorBurnPerfect,
1448
- linearBurn: linearBurnPerfect,
1449
- darkerColor: darkerPerfect,
1450
- lighten: lightenPerfect,
1451
- screen: screenPerfect,
1452
- colorDodge: colorDodgePerfect,
1453
- linearDodge: linearDodgePerfect,
1454
- lighterColor: lighterPerfect,
1455
- overlay: overlayPerfect,
1456
- softLight: softLightPerfect,
1457
- hardLight: hardLightPerfect,
1458
- vividLight: vividLightPerfect,
1459
- linearLight: linearLightPerfect,
1460
- pinLight: pinLightPerfect,
1461
- hardMix: hardMixPerfect,
1462
- difference: differencePerfect,
1463
- exclusion: exclusionPerfect,
1464
- subtract: subtractPerfect,
1465
- divide: dividePerfect
1385
+ var BASE_PERFECT_BLEND_MODE_FUNCTIONS = {
1386
+ [BaseBlendMode.overwrite]: overwritePerfect,
1387
+ [BaseBlendMode.sourceOver]: sourceOverPerfect,
1388
+ [BaseBlendMode.darken]: darkenPerfect,
1389
+ [BaseBlendMode.multiply]: multiplyPerfect,
1390
+ [BaseBlendMode.colorBurn]: colorBurnPerfect,
1391
+ [BaseBlendMode.linearBurn]: linearBurnPerfect,
1392
+ [BaseBlendMode.darkerColor]: darkerPerfect,
1393
+ [BaseBlendMode.lighten]: lightenPerfect,
1394
+ [BaseBlendMode.screen]: screenPerfect,
1395
+ [BaseBlendMode.colorDodge]: colorDodgePerfect,
1396
+ [BaseBlendMode.linearDodge]: linearDodgePerfect,
1397
+ [BaseBlendMode.lighterColor]: lighterPerfect,
1398
+ [BaseBlendMode.overlay]: overlayPerfect,
1399
+ [BaseBlendMode.softLight]: softLightPerfect,
1400
+ [BaseBlendMode.hardLight]: hardLightPerfect,
1401
+ [BaseBlendMode.vividLight]: vividLightPerfect,
1402
+ [BaseBlendMode.linearLight]: linearLightPerfect,
1403
+ [BaseBlendMode.pinLight]: pinLightPerfect,
1404
+ [BaseBlendMode.hardMix]: hardMixPerfect,
1405
+ [BaseBlendMode.difference]: differencePerfect,
1406
+ [BaseBlendMode.exclusion]: exclusionPerfect,
1407
+ [BaseBlendMode.subtract]: subtractPerfect,
1408
+ [BaseBlendMode.divide]: dividePerfect
1466
1409
  };
1410
+ function makePerfectBlendModeRegistry() {
1411
+ return makeBlendModeRegistry(BaseBlendMode, BASE_PERFECT_BLEND_MODE_FUNCTIONS);
1412
+ }
1467
1413
 
1468
1414
  // src/Canvas/_constants.ts
1469
1415
  var OFFSCREEN_CANVAS_CTX_FAILED = "Failed to create OffscreenCanvas context";
@@ -2428,7 +2374,7 @@ function blendPixelData(dst, src, opts) {
2428
2374
  w: width = src.width,
2429
2375
  h: height = src.height,
2430
2376
  alpha: globalAlpha = 255,
2431
- blendFn = FAST_BLEND_MODES[1 /* sourceOver */],
2377
+ blendFn = sourceOverFast,
2432
2378
  mask,
2433
2379
  maskType = 0 /* ALPHA */,
2434
2380
  mw,
@@ -2640,6 +2586,15 @@ function extractPixelDataBuffer(source, _x, _y, _w, _h) {
2640
2586
  return dstData;
2641
2587
  }
2642
2588
 
2589
+ // src/PixelData/extractPixelData.ts
2590
+ function extractPixelData(source, _x, _y, _w, _h) {
2591
+ const { x, y, w, h } = typeof _x === "object" ? _x : { x: _x, y: _y, w: _w, h: _h };
2592
+ const result = new PixelData(new ImageData(w, h));
2593
+ const buffer = extractPixelDataBuffer(source, x, y, w, h);
2594
+ result.data32.set(buffer);
2595
+ return result;
2596
+ }
2597
+
2643
2598
  // src/PixelData/invertPixelData.ts
2644
2599
  function invertPixelData(pixelData) {
2645
2600
  const data32 = pixelData.data32;
@@ -2742,19 +2697,11 @@ function rotateSquareInPlace(pixelData) {
2742
2697
  }
2743
2698
  // Annotate the CommonJS export names for ESM import in node:
2744
2699
  0 && (module.exports = {
2745
- BlendMode,
2746
- FAST_BLENDER_REGISTRY,
2747
- FAST_BLEND_MODES,
2748
- FAST_BLEND_MODE_BY_NAME,
2749
- FAST_BLEND_TO_INDEX,
2750
- INDEX_TO_FAST_BLEND,
2751
- INDEX_TO_PERFECT_BLEND,
2700
+ BASE_FAST_BLEND_MODE_FUNCTIONS,
2701
+ BASE_PERFECT_BLEND_MODE_FUNCTIONS,
2702
+ BaseBlendMode,
2752
2703
  IndexedImage,
2753
2704
  MaskType,
2754
- PERFECT_BLENDER_REGISTRY,
2755
- PERFECT_BLEND_MODES,
2756
- PERFECT_BLEND_MODE_BY_NAME,
2757
- PERFECT_BLEND_TO_INDEX,
2758
2705
  PixelData,
2759
2706
  UnsupportedFormatError,
2760
2707
  applyMaskToPixelData,
@@ -2788,6 +2735,7 @@ function rotateSquareInPlace(pixelData) {
2788
2735
  exclusionPerfect,
2789
2736
  extractImageDataBuffer,
2790
2737
  extractMask,
2738
+ extractPixelData,
2791
2739
  extractPixelDataBuffer,
2792
2740
  fileInputChangeToImageData,
2793
2741
  fileToImageData,
@@ -2823,6 +2771,9 @@ function rotateSquareInPlace(pixelData) {
2823
2771
  linearDodgePerfect,
2824
2772
  linearLightFast,
2825
2773
  linearLightPerfect,
2774
+ makeBlendModeRegistry,
2775
+ makeFastBlendModeRegistry,
2776
+ makePerfectBlendModeRegistry,
2826
2777
  makePixelCanvas,
2827
2778
  makeReusableCanvas,
2828
2779
  makeReusableImageData,
@@ -2831,6 +2782,7 @@ function rotateSquareInPlace(pixelData) {
2831
2782
  multiplyPerfect,
2832
2783
  overlayFast,
2833
2784
  overlayPerfect,
2785
+ overwriteBase,
2834
2786
  overwriteFast,
2835
2787
  overwritePerfect,
2836
2788
  packColor,