pixel-data-js 0.27.0 → 0.29.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.
Files changed (148) hide show
  1. package/README.md +12 -2
  2. package/dist/index.prod.cjs +2355 -1124
  3. package/dist/index.prod.cjs.map +1 -1
  4. package/dist/index.prod.d.ts +558 -424
  5. package/dist/index.prod.js +2304 -1115
  6. package/dist/index.prod.js.map +1 -1
  7. package/package.json +11 -11
  8. package/src/Algorithm/floodFillSelection.ts +8 -6
  9. package/src/Algorithm/forEachLinePoint.ts +6 -6
  10. package/src/{Internal/resample32.ts → Algorithm/resampleUint32Array.ts} +11 -21
  11. package/src/BlendModes/blend-modes-fast.ts +169 -0
  12. package/src/BlendModes/blend-modes-perfect.ts +207 -0
  13. package/src/BlendModes/blend-modes.ts +9 -0
  14. package/src/Canvas/CanvasFrameRenderer.ts +20 -28
  15. package/src/Canvas/CanvasPixelDataRenderer.ts +23 -0
  16. package/src/Canvas/PixelCanvas.ts +2 -7
  17. package/src/Canvas/ReusableCanvas.ts +4 -12
  18. package/src/Canvas/_canvas-types.ts +26 -0
  19. package/src/History/PixelAccumulator.ts +17 -17
  20. package/src/History/PixelEngineConfig.ts +3 -3
  21. package/src/History/PixelMutator/mutatorApplyAlphaMask.ts +4 -3
  22. package/src/History/PixelMutator/mutatorApplyBinaryMask.ts +4 -3
  23. package/src/History/PixelMutator/mutatorApplyMask.ts +4 -3
  24. package/src/History/PixelMutator/mutatorBlendAlphaMask.ts +6 -4
  25. package/src/History/PixelMutator/mutatorBlendBinaryMask.ts +6 -4
  26. package/src/History/PixelMutator/mutatorBlendColor.ts +2 -2
  27. package/src/History/PixelMutator/mutatorBlendColorPaintAlphaMask.ts +2 -1
  28. package/src/History/PixelMutator/mutatorBlendColorPaintBinaryMask.ts +2 -1
  29. package/src/History/PixelMutator/mutatorBlendColorPaintMask.ts +3 -1
  30. package/src/History/PixelMutator/mutatorBlendColorPaintRect.ts +3 -3
  31. package/src/History/PixelMutator/mutatorBlendMask.ts +6 -4
  32. package/src/History/PixelMutator/mutatorBlendPixelData.ts +5 -4
  33. package/src/History/PixelMutator/mutatorClear.ts +4 -3
  34. package/src/History/PixelMutator/mutatorFill.ts +5 -4
  35. package/src/History/PixelMutator/mutatorFillBinaryMask.ts +2 -1
  36. package/src/History/PixelMutator/mutatorInvert.ts +2 -2
  37. package/src/History/PixelMutator.ts +1 -1
  38. package/src/History/PixelPatchTiles.ts +7 -7
  39. package/src/History/PixelWriter.ts +12 -63
  40. package/src/ImageData/ImageDataLike.ts +1 -1
  41. package/src/ImageData/_ImageData-types.ts +13 -0
  42. package/src/ImageData/copyImageData.ts +1 -1
  43. package/src/ImageData/extractImageDataBuffer.ts +3 -2
  44. package/src/ImageData/imageDataToUint32Array.ts +18 -0
  45. package/src/ImageData/resampleImageData.ts +3 -3
  46. package/src/ImageData/resizeImageData.ts +1 -1
  47. package/src/ImageData/serialization.ts +1 -1
  48. package/src/ImageData/uInt32ArrayToImageData.ts +1 -1
  49. package/src/ImageData/writeImageData.ts +2 -2
  50. package/src/ImageData/writeImageDataBuffer.ts +2 -2
  51. package/src/IndexedImage/IndexedImage.ts +56 -98
  52. package/src/IndexedImage/_indexedImage-types.ts +18 -0
  53. package/src/IndexedImage/getIndexedImageColorCounts.ts +3 -3
  54. package/src/IndexedImage/indexedImageToAverageColor.ts +1 -1
  55. package/src/IndexedImage/indexedImageToImageData.ts +4 -6
  56. package/src/IndexedImage/resampleIndexedImage.ts +7 -15
  57. package/src/Input/fileToImageData.ts +1 -1
  58. package/src/Internal/_constants.ts +3 -0
  59. package/src/Internal/_errors.ts +2 -0
  60. package/src/Internal/macros.ts +14 -0
  61. package/src/Mask/AlphaMask.ts +1 -1
  62. package/src/Mask/BinaryMask/makeBinaryMaskFromAlphaMask.ts +23 -0
  63. package/src/Mask/BinaryMask/makeBinaryMaskOutline.ts +88 -0
  64. package/src/Mask/BinaryMask/makeCircleBinaryMaskOutline.ts +104 -0
  65. package/src/Mask/BinaryMask/makeRectBinaryMaskOutline.ts +34 -0
  66. package/src/Mask/BinaryMask.ts +1 -1
  67. package/src/Mask/_mask-types.ts +73 -0
  68. package/src/Mask/applyBinaryMaskToAlphaMask.ts +2 -1
  69. package/src/Mask/copyMask.ts +1 -1
  70. package/src/Mask/extractMask.ts +2 -1
  71. package/src/Mask/extractMaskBuffer.ts +1 -1
  72. package/src/Mask/mergeAlphaMasks.ts +6 -3
  73. package/src/Mask/mergeBinaryMasks.ts +2 -1
  74. package/src/Mask/setMaskData.ts +1 -1
  75. package/src/MaskRect/merge2BinaryMaskRects.ts +2 -2
  76. package/src/MaskRect/mergeBinaryMaskRects.ts +1 -1
  77. package/src/MaskRect/subtractBinaryMaskRects.ts +1 -1
  78. package/src/Paint/AlphaMaskPaintBuffer.ts +283 -0
  79. package/src/Paint/BinaryMaskPaintBuffer.ts +198 -0
  80. package/src/Paint/{PaintBuffer.ts → ColorPaintBuffer.ts} +95 -77
  81. package/src/Paint/Commit/AlphaMaskPaintBufferCommitter.ts +26 -0
  82. package/src/Paint/Commit/AlphaMaskPaintBufferManager.ts +34 -0
  83. package/src/Paint/Commit/BinaryMaskPaintBufferCommitter.ts +26 -0
  84. package/src/Paint/Commit/BinaryMaskPaintBufferManager.ts +31 -0
  85. package/src/Paint/Commit/ColorPaintBufferCommitter.ts +23 -0
  86. package/src/Paint/Commit/ColorPaintBufferManager.ts +34 -0
  87. package/src/Paint/Commit/commitColorPaintBuffer.ts +55 -0
  88. package/src/Paint/Commit/commitMaskPaintBuffer.ts +78 -0
  89. package/src/Paint/Render/AlphaMaskPaintBufferCanvasRenderer.ts +78 -0
  90. package/src/Paint/Render/BinaryMaskPaintBufferCanvasRenderer.ts +67 -0
  91. package/src/Paint/{PaintBufferCanvasRenderer.ts → Render/ColorPaintBufferCanvasRenderer.ts} +13 -14
  92. package/src/Paint/Render/PaintCursorRenderer.ts +118 -0
  93. package/src/Paint/_paint-types.ts +22 -0
  94. package/src/Paint/eachTileInBounds.ts +45 -0
  95. package/src/Paint/makeCirclePaintMask.ts +74 -0
  96. package/src/Paint/makePaintMask.ts +5 -2
  97. package/src/Paint/makeRectFalloffPaintAlphaMask.ts +4 -2
  98. package/src/PixelData/PixelData.ts +15 -19
  99. package/src/PixelData/ReusablePixelData.ts +36 -0
  100. package/src/PixelData/_pixelData-types.ts +17 -0
  101. package/src/PixelData/applyAlphaMaskToPixelData.ts +80 -43
  102. package/src/PixelData/applyBinaryMaskToPixelData.ts +10 -8
  103. package/src/PixelData/applyMaskToPixelData.ts +4 -9
  104. package/src/PixelData/blendColorPixelData.ts +9 -8
  105. package/src/PixelData/blendColorPixelDataAlphaMask.ts +9 -7
  106. package/src/PixelData/blendColorPixelDataBinaryMask.ts +9 -7
  107. package/src/PixelData/blendColorPixelDataMask.ts +4 -2
  108. package/src/PixelData/blendColorPixelDataPaintAlphaMask.ts +4 -2
  109. package/src/PixelData/blendColorPixelDataPaintBinaryMask.ts +4 -2
  110. package/src/PixelData/blendColorPixelDataPaintMask.ts +5 -2
  111. package/src/PixelData/blendPixel.ts +6 -5
  112. package/src/PixelData/blendPixelData.ts +14 -13
  113. package/src/PixelData/blendPixelDataAlphaMask.ts +15 -13
  114. package/src/PixelData/blendPixelDataBinaryMask.ts +15 -13
  115. package/src/PixelData/blendPixelDataMask.ts +5 -3
  116. package/src/PixelData/blendPixelDataPaintBuffer.ts +5 -4
  117. package/src/PixelData/clearPixelDataFast.ts +4 -2
  118. package/src/PixelData/copyPixelData.ts +14 -0
  119. package/src/PixelData/extractPixelData.ts +8 -7
  120. package/src/PixelData/extractPixelDataBuffer.ts +9 -8
  121. package/src/PixelData/fillPixelData.ts +16 -14
  122. package/src/PixelData/fillPixelDataBinaryMask.ts +10 -8
  123. package/src/PixelData/fillPixelDataFast.ts +16 -14
  124. package/src/PixelData/invertPixelData.ts +9 -8
  125. package/src/PixelData/pixelDataToAlphaMask.ts +9 -8
  126. package/src/PixelData/reflectPixelData.ts +9 -9
  127. package/src/PixelData/resamplePixelData.ts +20 -9
  128. package/src/PixelData/rotatePixelData.ts +8 -7
  129. package/src/PixelData/uInt32ArrayToPixelData.ts +15 -0
  130. package/src/PixelData/writePaintBufferToPixelData.ts +5 -5
  131. package/src/PixelData/writePixelDataBuffer.ts +10 -9
  132. package/src/Rect/_rect-types.ts +7 -0
  133. package/src/Rect/getRectsBounds.ts +1 -1
  134. package/src/Rect/trimMaskRectBounds.ts +2 -1
  135. package/src/Rect/trimRectBounds.ts +1 -1
  136. package/src/Tile/MaskTile.ts +40 -0
  137. package/src/Tile/PixelTile.ts +23 -0
  138. package/src/{PixelTile/PixelTilePool.ts → Tile/TilePool.ts} +9 -9
  139. package/src/Tile/_tile-types.ts +33 -0
  140. package/src/_errors.ts +1 -0
  141. package/src/_types.ts +2 -118
  142. package/src/index.ts +57 -21
  143. package/src/ImageData/imageDataToUInt32Array.ts +0 -13
  144. package/src/Internal/helpers.ts +0 -5
  145. package/src/Paint/makeCirclePaintAlphaMask.ts +0 -41
  146. package/src/Paint/makeCirclePaintBinaryMask.ts +0 -29
  147. package/src/PixelTile/PixelTile.ts +0 -21
  148. /package/src/{Internal → Rect}/resolveClipping.ts +0 -0
@@ -20,20 +20,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ AlphaMaskPaintBuffer: () => AlphaMaskPaintBuffer,
23
24
  BASE_FAST_BLEND_MODE_FUNCTIONS: () => BASE_FAST_BLEND_MODE_FUNCTIONS,
24
25
  BASE_PERFECT_BLEND_MODE_FUNCTIONS: () => BASE_PERFECT_BLEND_MODE_FUNCTIONS,
25
26
  BaseBlendMode: () => BaseBlendMode,
27
+ BinaryMaskPaintBuffer: () => BinaryMaskPaintBuffer,
26
28
  CANVAS_COMPOSITE_MAP: () => CANVAS_COMPOSITE_MAP,
29
+ ColorPaintBuffer: () => ColorPaintBuffer,
30
+ ERRORS: () => errors_exports,
27
31
  HistoryManager: () => HistoryManager,
28
- IndexedImage: () => IndexedImage,
29
32
  MaskType: () => MaskType,
30
- PaintBuffer: () => PaintBuffer,
33
+ PaintMaskOutline: () => PaintMaskOutline,
31
34
  PixelAccumulator: () => PixelAccumulator,
32
- PixelData: () => PixelData,
33
35
  PixelEngineConfig: () => PixelEngineConfig,
34
- PixelTile: () => PixelTile,
35
- PixelTilePool: () => PixelTilePool,
36
36
  PixelWriter: () => PixelWriter,
37
+ TilePool: () => TilePool,
38
+ TileType: () => TileType,
37
39
  UnsupportedFormatError: () => UnsupportedFormatError,
38
40
  applyAlphaMaskToPixelData: () => applyAlphaMaskToPixelData,
39
41
  applyBinaryMaskToAlphaMask: () => applyBinaryMaskToAlphaMask,
@@ -63,9 +65,12 @@ __export(src_exports, {
63
65
  colorDistance: () => colorDistance,
64
66
  colorDodgeFast: () => colorDodgeFast,
65
67
  colorDodgePerfect: () => colorDodgePerfect,
68
+ commitColorPaintBuffer: () => commitColorPaintBuffer,
69
+ commitMaskPaintBuffer: () => commitMaskPaintBuffer,
66
70
  copyImageData: () => copyImageData,
67
71
  copyImageDataLike: () => copyImageDataLike,
68
72
  copyMask: () => copyMask,
73
+ copyPixelData: () => copyPixelData,
69
74
  darkenFast: () => darkenFast,
70
75
  darkenPerfect: () => darkenPerfect,
71
76
  darkerFast: () => darkerFast,
@@ -73,10 +78,19 @@ __export(src_exports, {
73
78
  deserializeImageData: () => deserializeImageData,
74
79
  deserializeNullableImageData: () => deserializeNullableImageData,
75
80
  deserializeRawImageData: () => deserializeRawImageData,
81
+ destinationAtopFast: () => destinationAtopFast,
82
+ destinationAtopPerfect: () => destinationAtopPerfect,
83
+ destinationInFast: () => destinationInFast,
84
+ destinationInPerfect: () => destinationInPerfect,
85
+ destinationOutFast: () => destinationOutFast,
86
+ destinationOutPerfect: () => destinationOutPerfect,
87
+ destinationOverFast: () => destinationOverFast,
88
+ destinationOverPerfect: () => destinationOverPerfect,
76
89
  differenceFast: () => differenceFast,
77
90
  differencePerfect: () => differencePerfect,
78
91
  divideFast: () => divideFast,
79
92
  dividePerfect: () => dividePerfect,
93
+ eachTileInBounds: () => eachTileInBounds,
80
94
  exclusionFast: () => exclusionFast,
81
95
  exclusionPerfect: () => exclusionPerfect,
82
96
  extractImageDataBuffer: () => extractImageDataBuffer,
@@ -92,6 +106,7 @@ __export(src_exports, {
92
106
  floodFillSelection: () => floodFillSelection,
93
107
  forEachLinePoint: () => forEachLinePoint,
94
108
  getImageDataFromClipboard: () => getImageDataFromClipboard,
109
+ getIndexedImageColor: () => getIndexedImageColor,
95
110
  getIndexedImageColorCounts: () => getIndexedImageColorCounts,
96
111
  getRectsBounds: () => getRectsBounds,
97
112
  getSupportedPixelFormats: () => getSupportedPixelFormats,
@@ -102,7 +117,7 @@ __export(src_exports, {
102
117
  imageDataToAlphaMaskBuffer: () => imageDataToAlphaMaskBuffer,
103
118
  imageDataToDataUrl: () => imageDataToDataUrl,
104
119
  imageDataToImgBlob: () => imageDataToImgBlob,
105
- imageDataToUInt32Array: () => imageDataToUInt32Array,
120
+ imageDataToUint32Array: () => imageDataToUint32Array,
106
121
  imgBlobToImageData: () => imgBlobToImageData,
107
122
  indexedImageToAverageColor: () => indexedImageToAverageColor,
108
123
  indexedImageToImageData: () => indexedImageToImageData,
@@ -123,26 +138,48 @@ __export(src_exports, {
123
138
  linearLightFast: () => linearLightFast,
124
139
  linearLightPerfect: () => linearLightPerfect,
125
140
  makeAlphaMask: () => makeAlphaMask,
141
+ makeAlphaMaskPaintBufferCanvasRenderer: () => makeAlphaMaskPaintBufferCanvasRenderer,
142
+ makeAlphaMaskPaintBufferCommitter: () => makeAlphaMaskPaintBufferCommitter,
143
+ makeAlphaMaskPaintBufferManager: () => makeAlphaMaskPaintBufferManager,
144
+ makeAlphaMaskTile: () => makeAlphaMaskTile,
126
145
  makeBinaryMask: () => makeBinaryMask,
146
+ makeBinaryMaskFromAlphaMask: () => makeBinaryMaskFromAlphaMask,
147
+ makeBinaryMaskOutline: () => makeBinaryMaskOutline,
148
+ makeBinaryMaskPaintBufferCanvasRenderer: () => makeBinaryMaskPaintBufferCanvasRenderer,
149
+ makeBinaryMaskPaintBufferCommitter: () => makeBinaryMaskPaintBufferCommitter,
150
+ makeBinaryMaskPaintBufferManager: () => makeBinaryMaskPaintBufferManager,
151
+ makeBinaryMaskTile: () => makeBinaryMaskTile,
127
152
  makeBlendModeRegistry: () => makeBlendModeRegistry,
128
153
  makeCanvasFrameRenderer: () => makeCanvasFrameRenderer,
154
+ makeCanvasPixelDataRenderer: () => makeCanvasPixelDataRenderer,
155
+ makeCircleBinaryMaskOutline: () => makeCircleBinaryMaskOutline,
129
156
  makeCirclePaintAlphaMask: () => makeCirclePaintAlphaMask,
130
157
  makeCirclePaintBinaryMask: () => makeCirclePaintBinaryMask,
131
158
  makeClippedBlit: () => makeClippedBlit,
132
159
  makeClippedRect: () => makeClippedRect,
160
+ makeColorPaintBufferCanvasRenderer: () => makeColorPaintBufferCanvasRenderer,
161
+ makeColorPaintBufferCommitter: () => makeColorPaintBufferCommitter,
162
+ makeColorPaintBufferManager: () => makeColorPaintBufferManager,
133
163
  makeFastBlendModeRegistry: () => makeFastBlendModeRegistry,
134
164
  makeFullPixelMutator: () => makeFullPixelMutator,
135
165
  makeHistoryAction: () => makeHistoryAction,
136
166
  makeImageDataLike: () => makeImageDataLike,
167
+ makeIndexedImage: () => makeIndexedImage,
168
+ makeIndexedImageFromImageData: () => makeIndexedImageFromImageData,
169
+ makeIndexedImageFromImageDataRaw: () => makeIndexedImageFromImageDataRaw,
137
170
  makePaintAlphaMask: () => makePaintAlphaMask,
138
171
  makePaintBinaryMask: () => makePaintBinaryMask,
139
- makePaintBufferCanvasRenderer: () => makePaintBufferCanvasRenderer,
172
+ makePaintCursorRenderer: () => makePaintCursorRenderer,
140
173
  makePerfectBlendModeRegistry: () => makePerfectBlendModeRegistry,
141
174
  makePixelCanvas: () => makePixelCanvas,
175
+ makePixelData: () => makePixelData,
176
+ makePixelTile: () => makePixelTile,
177
+ makeRectBinaryMaskOutline: () => makeRectBinaryMaskOutline,
142
178
  makeRectFalloffPaintAlphaMask: () => makeRectFalloffPaintAlphaMask,
143
179
  makeReusableCanvas: () => makeReusableCanvas,
144
180
  makeReusableImageData: () => makeReusableImageData,
145
181
  makeReusableOffscreenCanvas: () => makeReusableOffscreenCanvas,
182
+ makeReusablePixelData: () => makeReusablePixelData,
146
183
  merge2BinaryMaskRects: () => merge2BinaryMaskRects,
147
184
  mergeAlphaMasks: () => mergeAlphaMasks,
148
185
  mergeBinaryMaskRects: () => mergeBinaryMaskRects,
@@ -179,10 +216,11 @@ __export(src_exports, {
179
216
  pixelDataToAlphaMask: () => pixelDataToAlphaMask,
180
217
  reflectPixelDataHorizontal: () => reflectPixelDataHorizontal,
181
218
  reflectPixelDataVertical: () => reflectPixelDataVertical,
182
- resample32: () => resample32,
183
219
  resampleImageData: () => resampleImageData,
184
220
  resampleIndexedImage: () => resampleIndexedImage,
185
221
  resamplePixelData: () => resamplePixelData,
222
+ resamplePixelDataInPlace: () => resamplePixelDataInPlace,
223
+ resampleUint32Array: () => resampleUint32Array,
186
224
  resizeImageData: () => resizeImageData,
187
225
  resolveBlitClipping: () => resolveBlitClipping,
188
226
  resolveRectClipping: () => resolveRectClipping,
@@ -192,8 +230,15 @@ __export(src_exports, {
192
230
  serializeImageData: () => serializeImageData,
193
231
  serializeNullableImageData: () => serializeNullableImageData,
194
232
  setMaskData: () => setMaskData,
233
+ setPixelData: () => setPixelData,
195
234
  softLightFast: () => softLightFast,
196
235
  softLightPerfect: () => softLightPerfect,
236
+ sourceAtopFast: () => sourceAtopFast,
237
+ sourceAtopPerfect: () => sourceAtopPerfect,
238
+ sourceInFast: () => sourceInFast,
239
+ sourceInPerfect: () => sourceInPerfect,
240
+ sourceOutFast: () => sourceOutFast,
241
+ sourceOutPerfect: () => sourceOutPerfect,
197
242
  sourceOverFast: () => sourceOverFast,
198
243
  sourceOverPerfect: () => sourceOverPerfect,
199
244
  subtractBinaryMaskRects: () => subtractBinaryMaskRects,
@@ -204,6 +249,7 @@ __export(src_exports, {
204
249
  trimRectBounds: () => trimRectBounds,
205
250
  uInt32ArrayToImageData: () => uInt32ArrayToImageData,
206
251
  uInt32ArrayToImageDataLike: () => uInt32ArrayToImageDataLike,
252
+ uInt32ArrayToPixelData: () => uInt32ArrayToPixelData,
207
253
  unpackAlpha: () => unpackAlpha,
208
254
  unpackBlue: () => unpackBlue,
209
255
  unpackColor: () => unpackColor,
@@ -217,16 +263,20 @@ __export(src_exports, {
217
263
  writeImageDataToClipboard: () => writeImageDataToClipboard,
218
264
  writeImgBlobToClipboard: () => writeImgBlobToClipboard,
219
265
  writePaintBufferToPixelData: () => writePaintBufferToPixelData,
220
- writePixelDataBuffer: () => writePixelDataBuffer
266
+ writePixelDataBuffer: () => writePixelDataBuffer,
267
+ xorFast: () => xorFast,
268
+ xorPerfect: () => xorPerfect
221
269
  });
222
270
  module.exports = __toCommonJS(src_exports);
223
271
 
224
- // src/_types.ts
225
- var MaskType = /* @__PURE__ */ ((MaskType2) => {
226
- MaskType2[MaskType2["ALPHA"] = 0] = "ALPHA";
227
- MaskType2[MaskType2["BINARY"] = 1] = "BINARY";
228
- return MaskType2;
229
- })(MaskType || {});
272
+ // src/Internal/_errors.ts
273
+ var errors_exports = {};
274
+ __export(errors_exports, {
275
+ CANVAS_CTX_FAILED: () => CANVAS_CTX_FAILED,
276
+ OFFSCREEN_CANVAS_CTX_FAILED: () => OFFSCREEN_CANVAS_CTX_FAILED
277
+ });
278
+ var OFFSCREEN_CANVAS_CTX_FAILED = "Failed to create OffscreenCanvas context";
279
+ var CANVAS_CTX_FAILED = "Failed to create Canvas context";
230
280
 
231
281
  // src/color.ts
232
282
  function packColor(r, g, b, a) {
@@ -301,7 +351,7 @@ function color32ToCssRGBA(color) {
301
351
  return `rgba(${r},${g},${b},${alpha})`;
302
352
  }
303
353
 
304
- // src/Internal/resolveClipping.ts
354
+ // src/Rect/resolveClipping.ts
305
355
  var makeClippedRect = () => ({
306
356
  x: 0,
307
357
  y: 0,
@@ -419,6 +469,13 @@ function extractImageDataBuffer(imageData, _x, _y, _w, _h) {
419
469
  return out;
420
470
  }
421
471
 
472
+ // src/Mask/_mask-types.ts
473
+ var MaskType = /* @__PURE__ */ ((MaskType2) => {
474
+ MaskType2[MaskType2["ALPHA"] = 0] = "ALPHA";
475
+ MaskType2[MaskType2["BINARY"] = 1] = "BINARY";
476
+ return MaskType2;
477
+ })(MaskType || {});
478
+
422
479
  // src/Mask/extractMaskBuffer.ts
423
480
  function extractMaskBuffer(maskBuffer, maskWidth, xOrRect, y, w, h) {
424
481
  let finalX;
@@ -521,9 +578,9 @@ function trimMaskRectBounds(target, bounds) {
521
578
 
522
579
  // src/Algorithm/floodFillSelection.ts
523
580
  function floodFillSelection(target, startX, startY, contiguous = true, tolerance = 0, bounds, out) {
524
- const data32 = target.data32;
525
- const width = target.width;
526
- const height = target.height;
581
+ const data32 = target.data;
582
+ const width = target.w;
583
+ const height = target.h;
527
584
  const lx = bounds?.x ?? 0;
528
585
  const ly = bounds?.y ?? 0;
529
586
  const lw = bounds?.w ?? width;
@@ -637,13 +694,13 @@ function floodFillSelection(target, startX, startY, contiguous = true, tolerance
637
694
 
638
695
  // src/Algorithm/forEachLinePoint.ts
639
696
  function forEachLinePoint(x0, y0, x1, y1, callback) {
640
- const dx = x1 - x0;
641
- const dy = y1 - y0;
642
- const steps = Math.max(Math.abs(dx), Math.abs(dy));
643
- if (steps === 0) {
697
+ if (x0 === x1 && y0 === y1) {
644
698
  callback(x0, y0);
645
699
  return;
646
700
  }
701
+ const dx = x1 - x0;
702
+ const dy = y1 - y0;
703
+ const steps = Math.max(Math.abs(dx), Math.abs(dy));
647
704
  const xInc = dx / steps;
648
705
  const yInc = dy / steps;
649
706
  let curX = x0;
@@ -655,6 +712,66 @@ function forEachLinePoint(x0, y0, x1, y1, callback) {
655
712
  }
656
713
  }
657
714
 
715
+ // src/Algorithm/resampleUint32Array.ts
716
+ function resampleUint32Array(srcData32, srcW, srcH, factor, out) {
717
+ const dstW = Math.max(1, srcW * factor | 0);
718
+ const dstH = Math.max(1, srcH * factor | 0);
719
+ const dstData = new Uint32Array(dstW * dstH);
720
+ const scaleX = srcW / dstW;
721
+ const scaleY = srcH / dstH;
722
+ for (let y = 0; y < dstH; y++) {
723
+ const srcY = Math.min(srcH - 1, y * scaleY | 0);
724
+ const srcRowOffset = srcY * srcW;
725
+ const dstRowOffset = y * dstW;
726
+ for (let x = 0; x < dstW; x++) {
727
+ const srcX = Math.min(srcW - 1, x * scaleX | 0);
728
+ dstData[dstRowOffset + x] = srcData32[srcRowOffset + srcX];
729
+ }
730
+ }
731
+ out = out ?? {};
732
+ out.data = dstData;
733
+ out.w = dstW;
734
+ out.h = dstH;
735
+ return out;
736
+ }
737
+
738
+ // src/BlendModes/blend-modes.ts
739
+ var BaseBlendMode = {
740
+ overwrite: 0,
741
+ sourceOver: 1,
742
+ darken: 2,
743
+ multiply: 3,
744
+ colorBurn: 4,
745
+ linearBurn: 5,
746
+ darkerColor: 6,
747
+ lighten: 7,
748
+ screen: 8,
749
+ colorDodge: 9,
750
+ linearDodge: 10,
751
+ lighterColor: 11,
752
+ overlay: 12,
753
+ softLight: 13,
754
+ hardLight: 14,
755
+ vividLight: 15,
756
+ linearLight: 16,
757
+ pinLight: 17,
758
+ hardMix: 18,
759
+ difference: 19,
760
+ exclusion: 20,
761
+ subtract: 21,
762
+ divide: 22,
763
+ sourceIn: 23,
764
+ sourceOut: 24,
765
+ sourceAtop: 25,
766
+ destinationOver: 26,
767
+ destinationIn: 27,
768
+ destinationOut: 28,
769
+ destinationAtop: 29,
770
+ xor: 30
771
+ };
772
+ var overwriteBase = (src, _dst) => src;
773
+ overwriteBase.isOverwrite = true;
774
+
658
775
  // src/BlendModes/BlendModeRegistry.ts
659
776
  function makeBlendModeRegistry(blendModes, initialEntries, registryName = "anonymous") {
660
777
  const blendToName = /* @__PURE__ */ new Map();
@@ -694,37 +811,134 @@ function makeBlendModeRegistry(blendModes, initialEntries, registryName = "anony
694
811
  };
695
812
  }
696
813
 
697
- // src/BlendModes/blend-modes.ts
698
- var BaseBlendMode = {
699
- overwrite: 0,
700
- sourceOver: 1,
701
- darken: 2,
702
- multiply: 3,
703
- colorBurn: 4,
704
- linearBurn: 5,
705
- darkerColor: 6,
706
- lighten: 7,
707
- screen: 8,
708
- colorDodge: 9,
709
- linearDodge: 10,
710
- lighterColor: 11,
711
- overlay: 12,
712
- softLight: 13,
713
- hardLight: 14,
714
- vividLight: 15,
715
- linearLight: 16,
716
- pinLight: 17,
717
- hardMix: 18,
718
- difference: 19,
719
- exclusion: 20,
720
- subtract: 21,
721
- divide: 22
722
- };
723
- var overwriteBase = (src, _dst) => src;
724
- overwriteBase.isOverwrite = true;
725
-
726
814
  // src/BlendModes/blend-modes-fast.ts
727
815
  var overwriteFast = overwriteBase;
816
+ var sourceInFast = (src, dst) => {
817
+ const da = dst >>> 24 & 255;
818
+ if (da === 0) return 0;
819
+ if (da === 255) return src;
820
+ const sa = src >>> 24 & 255;
821
+ const sr = src & 255;
822
+ const sg = src >>> 8 & 255;
823
+ const sb = src >>> 16 & 255;
824
+ const r = sr * da >> 8;
825
+ const g = sg * da >> 8;
826
+ const b = sb * da >> 8;
827
+ const a = sa * da >> 8;
828
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
829
+ };
830
+ var sourceOutFast = (src, dst) => {
831
+ const da = dst >>> 24 & 255;
832
+ if (da === 255) return 0;
833
+ if (da === 0) return src;
834
+ const sa = src >>> 24 & 255;
835
+ const sr = src & 255;
836
+ const sg = src >>> 8 & 255;
837
+ const sb = src >>> 16 & 255;
838
+ const invDa = 255 - da;
839
+ const r = sr * invDa >> 8;
840
+ const g = sg * invDa >> 8;
841
+ const b = sb * invDa >> 8;
842
+ const a = sa * invDa >> 8;
843
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
844
+ };
845
+ var sourceAtopFast = (src, dst) => {
846
+ const sa = src >>> 24 & 255;
847
+ const da = dst >>> 24 & 255;
848
+ if (da === 0) return 0;
849
+ const sr = src & 255;
850
+ const sg = src >>> 8 & 255;
851
+ const sb = src >>> 16 & 255;
852
+ const dr = dst & 255;
853
+ const dg = dst >>> 8 & 255;
854
+ const db = dst >>> 16 & 255;
855
+ const invSa = 255 - sa;
856
+ const r = sr * da + dr * invSa >> 8;
857
+ const g = sg * da + dg * invSa >> 8;
858
+ const b = sb * da + db * invSa >> 8;
859
+ return (da << 24 | b << 16 | g << 8 | r) >>> 0;
860
+ };
861
+ var destinationOverFast = (src, dst) => {
862
+ const da = dst >>> 24 & 255;
863
+ if (da === 255) return dst;
864
+ if (da === 0) return src;
865
+ const sa = src >>> 24 & 255;
866
+ const sr = src & 255;
867
+ const sg = src >>> 8 & 255;
868
+ const sb = src >>> 16 & 255;
869
+ const dr = dst & 255;
870
+ const dg = dst >>> 8 & 255;
871
+ const db = dst >>> 16 & 255;
872
+ const invDa = 255 - da;
873
+ const r = dr * 255 + sr * invDa >> 8;
874
+ const g = dg * 255 + sg * invDa >> 8;
875
+ const b = db * 255 + sb * invDa >> 8;
876
+ const a = da * 255 + sa * invDa >> 8;
877
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
878
+ };
879
+ var destinationInFast = (src, dst) => {
880
+ const sa = src >>> 24 & 255;
881
+ if (sa === 0) return 0;
882
+ if (sa === 255) return dst;
883
+ const da = dst >>> 24 & 255;
884
+ const dr = dst & 255;
885
+ const dg = dst >>> 8 & 255;
886
+ const db = dst >>> 16 & 255;
887
+ const r = dr * sa >> 8;
888
+ const g = dg * sa >> 8;
889
+ const b = db * sa >> 8;
890
+ const a = da * sa >> 8;
891
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
892
+ };
893
+ var destinationOutFast = (src, dst) => {
894
+ const sa = src >>> 24 & 255;
895
+ if (sa === 255) return 0;
896
+ if (sa === 0) return dst;
897
+ const da = dst >>> 24 & 255;
898
+ const dr = dst & 255;
899
+ const dg = dst >>> 8 & 255;
900
+ const db = dst >>> 16 & 255;
901
+ const invSa = 255 - sa;
902
+ const r = dr * invSa >> 8;
903
+ const g = dg * invSa >> 8;
904
+ const b = db * invSa >> 8;
905
+ const a = da * invSa >> 8;
906
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
907
+ };
908
+ var destinationAtopFast = (src, dst) => {
909
+ const sa = src >>> 24 & 255;
910
+ if (sa === 0) return 0;
911
+ const da = dst >>> 24 & 255;
912
+ if (da === 0) return 0;
913
+ const sr = src & 255;
914
+ const sg = src >>> 8 & 255;
915
+ const sb = src >>> 16 & 255;
916
+ const dr = dst & 255;
917
+ const dg = dst >>> 8 & 255;
918
+ const db = dst >>> 16 & 255;
919
+ const invDa = 255 - da;
920
+ const r = dr * sa + sr * invDa >> 8;
921
+ const g = dg * sa + sg * invDa >> 8;
922
+ const b = db * sa + sb * invDa >> 8;
923
+ return (sa << 24 | b << 16 | g << 8 | r) >>> 0;
924
+ };
925
+ var xorFast = (src, dst) => {
926
+ const sa = src >>> 24 & 255;
927
+ const da = dst >>> 24 & 255;
928
+ const sr = src & 255;
929
+ const sg = src >>> 8 & 255;
930
+ const sb = src >>> 16 & 255;
931
+ const dr = dst & 255;
932
+ const dg = dst >>> 8 & 255;
933
+ const db = dst >>> 16 & 255;
934
+ const invDa = 255 - da;
935
+ const invSa = 255 - sa;
936
+ const r = sr * invDa + dr * invSa >> 8;
937
+ const g = sg * invDa + dg * invSa >> 8;
938
+ const b = sb * invDa + db * invSa >> 8;
939
+ const a = sa * invDa + da * invSa >> 8;
940
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
941
+ };
728
942
  var sourceOverFast = (src, dst) => {
729
943
  const sa = src >>> 24 & 255;
730
944
  if (sa === 255) return src;
@@ -1110,6 +1324,14 @@ var divideFast = (src, dst) => {
1110
1324
  };
1111
1325
  var BASE_FAST_BLEND_MODE_FUNCTIONS = {
1112
1326
  [BaseBlendMode.overwrite]: overwriteFast,
1327
+ [BaseBlendMode.sourceIn]: sourceInFast,
1328
+ [BaseBlendMode.sourceOut]: sourceOutFast,
1329
+ [BaseBlendMode.sourceAtop]: sourceAtopFast,
1330
+ [BaseBlendMode.destinationOver]: destinationOverFast,
1331
+ [BaseBlendMode.destinationIn]: destinationInFast,
1332
+ [BaseBlendMode.destinationOut]: destinationOutFast,
1333
+ [BaseBlendMode.destinationAtop]: destinationAtopFast,
1334
+ [BaseBlendMode.xor]: xorFast,
1113
1335
  [BaseBlendMode.sourceOver]: sourceOverFast,
1114
1336
  [BaseBlendMode.darken]: darkenFast,
1115
1337
  [BaseBlendMode.multiply]: multiplyFast,
@@ -1139,118 +1361,274 @@ function makeFastBlendModeRegistry(name = "fast") {
1139
1361
 
1140
1362
  // src/BlendModes/blend-modes-perfect.ts
1141
1363
  var overwritePerfect = overwriteBase;
1142
- var sourceOverPerfect = (src, dst) => {
1143
- const sa = src >>> 24 & 255;
1144
- if (sa === 255) return src;
1145
- if (sa === 0) return dst;
1364
+ var sourceInPerfect = (src, dst) => {
1146
1365
  const da = dst >>> 24 & 255;
1147
- if (da === 0) return src;
1148
- const sr = src & 255, sg = src >>> 8 & 255, sb = src >>> 16 & 255;
1149
- const dr = dst & 255, dg = dst >>> 8 & 255, db = dst >>> 16 & 255;
1150
- const invA = 255 - sa;
1151
- const tR = sr * sa + dr * invA;
1366
+ if (da === 0) return 0;
1367
+ if (da === 255) return src;
1368
+ const sa = src >>> 24 & 255;
1369
+ const sr = src & 255;
1370
+ const sg = src >>> 8 & 255;
1371
+ const sb = src >>> 16 & 255;
1372
+ const tR = sr * da;
1152
1373
  const r = tR + 1 + (tR >> 8) >> 8;
1153
- const tG = sg * sa + dg * invA;
1374
+ const tG = sg * da;
1154
1375
  const g = tG + 1 + (tG >> 8) >> 8;
1155
- const tB = sb * sa + db * invA;
1376
+ const tB = sb * da;
1156
1377
  const b = tB + 1 + (tB >> 8) >> 8;
1157
- const tA = 255 * sa + da * invA;
1378
+ const tA = sa * da;
1158
1379
  const a = tA + 1 + (tA >> 8) >> 8;
1159
1380
  return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1160
1381
  };
1161
- var darkenPerfect = (src, dst) => {
1162
- const sa = src >>> 24 & 255;
1163
- if (sa === 0) return dst;
1164
- const sr = src & 255, sg = src >>> 8 & 255, sb = src >>> 16 & 255;
1165
- const dr = dst & 255, dg = dst >>> 8 & 255, db = dst >>> 16 & 255;
1166
- const br = sr < dr ? sr : dr;
1167
- const bg = sg < dg ? sg : dg;
1168
- const bb = sb < db ? sb : db;
1169
- if (sa === 255) return (4278190080 | bb << 16 | bg << 8 | br) >>> 0;
1170
- const invA = 255 - sa;
1382
+ var sourceOutPerfect = (src, dst) => {
1171
1383
  const da = dst >>> 24 & 255;
1172
- const tR = br * sa + dr * invA;
1384
+ if (da === 255) return 0;
1385
+ if (da === 0) return src;
1386
+ const sa = src >>> 24 & 255;
1387
+ const sr = src & 255;
1388
+ const sg = src >>> 8 & 255;
1389
+ const sb = src >>> 16 & 255;
1390
+ const invDa = 255 - da;
1391
+ const tR = sr * invDa;
1173
1392
  const r = tR + 1 + (tR >> 8) >> 8;
1174
- const tG = bg * sa + dg * invA;
1393
+ const tG = sg * invDa;
1175
1394
  const g = tG + 1 + (tG >> 8) >> 8;
1176
- const tB = bb * sa + db * invA;
1395
+ const tB = sb * invDa;
1177
1396
  const b = tB + 1 + (tB >> 8) >> 8;
1178
- const tA = 255 * sa + da * invA;
1397
+ const tA = sa * invDa;
1179
1398
  const a = tA + 1 + (tA >> 8) >> 8;
1180
1399
  return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1181
1400
  };
1182
- var multiplyPerfect = (src, dst) => {
1401
+ var sourceAtopPerfect = (src, dst) => {
1183
1402
  const sa = src >>> 24 & 255;
1184
- if (sa === 0) return dst;
1403
+ const da = dst >>> 24 & 255;
1404
+ if (da === 0) return 0;
1405
+ const sr = src & 255;
1406
+ const sg = src >>> 8 & 255;
1407
+ const sb = src >>> 16 & 255;
1185
1408
  const dr = dst & 255;
1186
1409
  const dg = dst >>> 8 & 255;
1187
1410
  const db = dst >>> 16 & 255;
1411
+ const invSa = 255 - sa;
1412
+ const tR = sr * da + dr * invSa;
1413
+ const r = tR + 1 + (tR >> 8) >> 8;
1414
+ const tG = sg * da + dg * invSa;
1415
+ const g = tG + 1 + (tG >> 8) >> 8;
1416
+ const tB = sb * da + db * invSa;
1417
+ const b = tB + 1 + (tB >> 8) >> 8;
1418
+ return (da << 24 | b << 16 | g << 8 | r) >>> 0;
1419
+ };
1420
+ var destinationOverPerfect = (src, dst) => {
1188
1421
  const da = dst >>> 24 & 255;
1422
+ if (da === 255) return dst;
1423
+ if (da === 0) return src;
1424
+ const sa = src >>> 24 & 255;
1189
1425
  const sr = src & 255;
1190
1426
  const sg = src >>> 8 & 255;
1191
1427
  const sb = src >>> 16 & 255;
1192
- const mR = sr * dr;
1193
- const br = mR + 1 + (mR >> 8) >> 8;
1194
- const mG = sg * dg;
1195
- const bg = mG + 1 + (mG >> 8) >> 8;
1196
- const mB = sb * db;
1197
- const bb = mB + 1 + (mB >> 8) >> 8;
1198
- if (sa === 255) return (4278190080 | bb << 16 | bg << 8 | br) >>> 0;
1199
- const invA = 255 - sa;
1200
- const tR = br * sa + dr * invA;
1428
+ const dr = dst & 255;
1429
+ const dg = dst >>> 8 & 255;
1430
+ const db = dst >>> 16 & 255;
1431
+ const invDa = 255 - da;
1432
+ const tR = dr * 255 + sr * invDa;
1201
1433
  const r = tR + 1 + (tR >> 8) >> 8;
1202
- const tG = bg * sa + dg * invA;
1434
+ const tG = dg * 255 + sg * invDa;
1203
1435
  const g = tG + 1 + (tG >> 8) >> 8;
1204
- const tB = bb * sa + db * invA;
1436
+ const tB = db * 255 + sb * invDa;
1205
1437
  const b = tB + 1 + (tB >> 8) >> 8;
1206
- const tA = 255 * sa + da * invA;
1438
+ const tA = da * 255 + sa * invDa;
1207
1439
  const a = tA + 1 + (tA >> 8) >> 8;
1208
1440
  return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1209
1441
  };
1210
- var colorBurnPerfect = (src, dst) => {
1442
+ var destinationInPerfect = (src, dst) => {
1211
1443
  const sa = src >>> 24 & 255;
1212
- if (sa === 0) return dst;
1444
+ if (sa === 0) return 0;
1445
+ if (sa === 255) return dst;
1446
+ const da = dst >>> 24 & 255;
1213
1447
  const dr = dst & 255;
1214
1448
  const dg = dst >>> 8 & 255;
1215
1449
  const db = dst >>> 16 & 255;
1216
- const sr = src & 255;
1217
- const sg = src >>> 8 & 255;
1218
- const sb = src >>> 16 & 255;
1219
- const resR = dr === 255 ? 255 : sr === 0 ? 0 : 255 - ((255 - dr) * 255 / sr | 0);
1220
- const br = resR < 0 ? 0 : resR;
1221
- const resG = dg === 255 ? 255 : sg === 0 ? 0 : 255 - ((255 - dg) * 255 / sg | 0);
1222
- const bg = resG < 0 ? 0 : resG;
1223
- const resB = db === 255 ? 255 : sb === 0 ? 0 : 255 - ((255 - db) * 255 / sb | 0);
1224
- const bb = resB < 0 ? 0 : resB;
1225
- if (sa === 255) return (4278190080 | bb << 16 | bg << 8 | br) >>> 0;
1226
- const invA = 255 - sa;
1227
- const da = dst >>> 24 & 255;
1228
- const tR = br * sa + dr * invA;
1450
+ const tR = dr * sa;
1229
1451
  const r = tR + 1 + (tR >> 8) >> 8;
1230
- const tG = bg * sa + dg * invA;
1452
+ const tG = dg * sa;
1231
1453
  const g = tG + 1 + (tG >> 8) >> 8;
1232
- const tB = bb * sa + db * invA;
1454
+ const tB = db * sa;
1233
1455
  const b = tB + 1 + (tB >> 8) >> 8;
1234
- const tA = 255 * sa + da * invA;
1456
+ const tA = da * sa;
1235
1457
  const a = tA + 1 + (tA >> 8) >> 8;
1236
1458
  return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1237
1459
  };
1238
- var linearBurnPerfect = (src, dst) => {
1460
+ var destinationOutPerfect = (src, dst) => {
1239
1461
  const sa = src >>> 24 & 255;
1462
+ if (sa === 255) return 0;
1240
1463
  if (sa === 0) return dst;
1241
- const dr = dst & 255, dg = dst >>> 8 & 255, db = dst >>> 16 & 255;
1242
- const sr = src & 255, sg = src >>> 8 & 255, sb = src >>> 16 & 255;
1243
- const brU = dr + sr - 255;
1244
- const br = brU < 0 ? 0 : brU;
1245
- const bgU = dg + sg - 255;
1246
- const bg = bgU < 0 ? 0 : bgU;
1247
- const bbU = db + sb - 255;
1248
- const bb = bbU < 0 ? 0 : bbU;
1249
- if (sa === 255) return (4278190080 | bb << 16 | bg << 8 | br) >>> 0;
1250
- const invA = 255 - sa;
1251
1464
  const da = dst >>> 24 & 255;
1252
- const tR = br * sa + dr * invA;
1253
- const r = tR + 1 + (tR >> 8) >> 8;
1465
+ const dr = dst & 255;
1466
+ const dg = dst >>> 8 & 255;
1467
+ const db = dst >>> 16 & 255;
1468
+ const invSa = 255 - sa;
1469
+ const tR = dr * invSa;
1470
+ const r = tR + 1 + (tR >> 8) >> 8;
1471
+ const tG = dg * invSa;
1472
+ const g = tG + 1 + (tG >> 8) >> 8;
1473
+ const tB = db * invSa;
1474
+ const b = tB + 1 + (tB >> 8) >> 8;
1475
+ const tA = da * invSa;
1476
+ const a = tA + 1 + (tA >> 8) >> 8;
1477
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1478
+ };
1479
+ var destinationAtopPerfect = (src, dst) => {
1480
+ const sa = src >>> 24 & 255;
1481
+ if (sa === 0) return 0;
1482
+ const da = dst >>> 24 & 255;
1483
+ if (da === 0) return 0;
1484
+ const sr = src & 255;
1485
+ const sg = src >>> 8 & 255;
1486
+ const sb = src >>> 16 & 255;
1487
+ const dr = dst & 255;
1488
+ const dg = dst >>> 8 & 255;
1489
+ const db = dst >>> 16 & 255;
1490
+ const invDa = 255 - da;
1491
+ const tR = dr * sa + sr * invDa;
1492
+ const r = tR + 1 + (tR >> 8) >> 8;
1493
+ const tG = dg * sa + sg * invDa;
1494
+ const g = tG + 1 + (tG >> 8) >> 8;
1495
+ const tB = db * sa + sb * invDa;
1496
+ const b = tB + 1 + (tB >> 8) >> 8;
1497
+ return (sa << 24 | b << 16 | g << 8 | r) >>> 0;
1498
+ };
1499
+ var xorPerfect = (src, dst) => {
1500
+ const sa = src >>> 24 & 255;
1501
+ const da = dst >>> 24 & 255;
1502
+ const sr = src & 255;
1503
+ const sg = src >>> 8 & 255;
1504
+ const sb = src >>> 16 & 255;
1505
+ const dr = dst & 255;
1506
+ const dg = dst >>> 8 & 255;
1507
+ const db = dst >>> 16 & 255;
1508
+ const invDa = 255 - da;
1509
+ const invSa = 255 - sa;
1510
+ const tR = sr * invDa + dr * invSa;
1511
+ const r = tR + 1 + (tR >> 8) >> 8;
1512
+ const tG = sg * invDa + dg * invSa;
1513
+ const g = tG + 1 + (tG >> 8) >> 8;
1514
+ const tB = sb * invDa + db * invSa;
1515
+ const b = tB + 1 + (tB >> 8) >> 8;
1516
+ const tA = sa * invDa + da * invSa;
1517
+ const a = tA + 1 + (tA >> 8) >> 8;
1518
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1519
+ };
1520
+ var sourceOverPerfect = (src, dst) => {
1521
+ const sa = src >>> 24 & 255;
1522
+ if (sa === 255) return src;
1523
+ if (sa === 0) return dst;
1524
+ const da = dst >>> 24 & 255;
1525
+ if (da === 0) return src;
1526
+ const sr = src & 255, sg = src >>> 8 & 255, sb = src >>> 16 & 255;
1527
+ const dr = dst & 255, dg = dst >>> 8 & 255, db = dst >>> 16 & 255;
1528
+ const invA = 255 - sa;
1529
+ const tR = sr * sa + dr * invA;
1530
+ const r = tR + 1 + (tR >> 8) >> 8;
1531
+ const tG = sg * sa + dg * invA;
1532
+ const g = tG + 1 + (tG >> 8) >> 8;
1533
+ const tB = sb * sa + db * invA;
1534
+ const b = tB + 1 + (tB >> 8) >> 8;
1535
+ const tA = 255 * sa + da * invA;
1536
+ const a = tA + 1 + (tA >> 8) >> 8;
1537
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1538
+ };
1539
+ var darkenPerfect = (src, dst) => {
1540
+ const sa = src >>> 24 & 255;
1541
+ if (sa === 0) return dst;
1542
+ const sr = src & 255, sg = src >>> 8 & 255, sb = src >>> 16 & 255;
1543
+ const dr = dst & 255, dg = dst >>> 8 & 255, db = dst >>> 16 & 255;
1544
+ const br = sr < dr ? sr : dr;
1545
+ const bg = sg < dg ? sg : dg;
1546
+ const bb = sb < db ? sb : db;
1547
+ if (sa === 255) return (4278190080 | bb << 16 | bg << 8 | br) >>> 0;
1548
+ const invA = 255 - sa;
1549
+ const da = dst >>> 24 & 255;
1550
+ const tR = br * sa + dr * invA;
1551
+ const r = tR + 1 + (tR >> 8) >> 8;
1552
+ const tG = bg * sa + dg * invA;
1553
+ const g = tG + 1 + (tG >> 8) >> 8;
1554
+ const tB = bb * sa + db * invA;
1555
+ const b = tB + 1 + (tB >> 8) >> 8;
1556
+ const tA = 255 * sa + da * invA;
1557
+ const a = tA + 1 + (tA >> 8) >> 8;
1558
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1559
+ };
1560
+ var multiplyPerfect = (src, dst) => {
1561
+ const sa = src >>> 24 & 255;
1562
+ if (sa === 0) return dst;
1563
+ const dr = dst & 255;
1564
+ const dg = dst >>> 8 & 255;
1565
+ const db = dst >>> 16 & 255;
1566
+ const da = dst >>> 24 & 255;
1567
+ const sr = src & 255;
1568
+ const sg = src >>> 8 & 255;
1569
+ const sb = src >>> 16 & 255;
1570
+ const mR = sr * dr;
1571
+ const br = mR + 1 + (mR >> 8) >> 8;
1572
+ const mG = sg * dg;
1573
+ const bg = mG + 1 + (mG >> 8) >> 8;
1574
+ const mB = sb * db;
1575
+ const bb = mB + 1 + (mB >> 8) >> 8;
1576
+ if (sa === 255) return (4278190080 | bb << 16 | bg << 8 | br) >>> 0;
1577
+ const invA = 255 - sa;
1578
+ const tR = br * sa + dr * invA;
1579
+ const r = tR + 1 + (tR >> 8) >> 8;
1580
+ const tG = bg * sa + dg * invA;
1581
+ const g = tG + 1 + (tG >> 8) >> 8;
1582
+ const tB = bb * sa + db * invA;
1583
+ const b = tB + 1 + (tB >> 8) >> 8;
1584
+ const tA = 255 * sa + da * invA;
1585
+ const a = tA + 1 + (tA >> 8) >> 8;
1586
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1587
+ };
1588
+ var colorBurnPerfect = (src, dst) => {
1589
+ const sa = src >>> 24 & 255;
1590
+ if (sa === 0) return dst;
1591
+ const dr = dst & 255;
1592
+ const dg = dst >>> 8 & 255;
1593
+ const db = dst >>> 16 & 255;
1594
+ const sr = src & 255;
1595
+ const sg = src >>> 8 & 255;
1596
+ const sb = src >>> 16 & 255;
1597
+ const resR = dr === 255 ? 255 : sr === 0 ? 0 : 255 - ((255 - dr) * 255 / sr | 0);
1598
+ const br = resR < 0 ? 0 : resR;
1599
+ const resG = dg === 255 ? 255 : sg === 0 ? 0 : 255 - ((255 - dg) * 255 / sg | 0);
1600
+ const bg = resG < 0 ? 0 : resG;
1601
+ const resB = db === 255 ? 255 : sb === 0 ? 0 : 255 - ((255 - db) * 255 / sb | 0);
1602
+ const bb = resB < 0 ? 0 : resB;
1603
+ if (sa === 255) return (4278190080 | bb << 16 | bg << 8 | br) >>> 0;
1604
+ const invA = 255 - sa;
1605
+ const da = dst >>> 24 & 255;
1606
+ const tR = br * sa + dr * invA;
1607
+ const r = tR + 1 + (tR >> 8) >> 8;
1608
+ const tG = bg * sa + dg * invA;
1609
+ const g = tG + 1 + (tG >> 8) >> 8;
1610
+ const tB = bb * sa + db * invA;
1611
+ const b = tB + 1 + (tB >> 8) >> 8;
1612
+ const tA = 255 * sa + da * invA;
1613
+ const a = tA + 1 + (tA >> 8) >> 8;
1614
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
1615
+ };
1616
+ var linearBurnPerfect = (src, dst) => {
1617
+ const sa = src >>> 24 & 255;
1618
+ if (sa === 0) return dst;
1619
+ const dr = dst & 255, dg = dst >>> 8 & 255, db = dst >>> 16 & 255;
1620
+ const sr = src & 255, sg = src >>> 8 & 255, sb = src >>> 16 & 255;
1621
+ const brU = dr + sr - 255;
1622
+ const br = brU < 0 ? 0 : brU;
1623
+ const bgU = dg + sg - 255;
1624
+ const bg = bgU < 0 ? 0 : bgU;
1625
+ const bbU = db + sb - 255;
1626
+ const bb = bbU < 0 ? 0 : bbU;
1627
+ if (sa === 255) return (4278190080 | bb << 16 | bg << 8 | br) >>> 0;
1628
+ const invA = 255 - sa;
1629
+ const da = dst >>> 24 & 255;
1630
+ const tR = br * sa + dr * invA;
1631
+ const r = tR + 1 + (tR >> 8) >> 8;
1254
1632
  const tG = bg * sa + dg * invA;
1255
1633
  const g = tG + 1 + (tG >> 8) >> 8;
1256
1634
  const tB = bb * sa + db * invA;
@@ -1666,6 +2044,14 @@ var dividePerfect = (src, dst) => {
1666
2044
  };
1667
2045
  var BASE_PERFECT_BLEND_MODE_FUNCTIONS = {
1668
2046
  [BaseBlendMode.overwrite]: overwritePerfect,
2047
+ [BaseBlendMode.sourceIn]: sourceInPerfect,
2048
+ [BaseBlendMode.sourceOut]: sourceOutPerfect,
2049
+ [BaseBlendMode.sourceAtop]: sourceAtopPerfect,
2050
+ [BaseBlendMode.destinationOver]: destinationOverPerfect,
2051
+ [BaseBlendMode.destinationIn]: destinationInPerfect,
2052
+ [BaseBlendMode.destinationOut]: destinationOutPerfect,
2053
+ [BaseBlendMode.destinationAtop]: destinationAtopPerfect,
2054
+ [BaseBlendMode.xor]: xorPerfect,
1669
2055
  [BaseBlendMode.sourceOver]: sourceOverPerfect,
1670
2056
  [BaseBlendMode.darken]: darkenPerfect,
1671
2057
  [BaseBlendMode.multiply]: multiplyPerfect,
@@ -1728,9 +2114,23 @@ var getKeyByValue = (obj, value) => {
1728
2114
  }
1729
2115
  };
1730
2116
 
1731
- // support/error-strings.ts
1732
- var OFFSCREEN_CANVAS_CTX_FAILED = "Failed to create OffscreenCanvas context";
1733
- var CANVAS_CTX_FAILED = "Failed to create Canvas context";
2117
+ // src/Canvas/canvas-blend-modes.ts
2118
+ var CANVAS_COMPOSITE_MAP = {
2119
+ [BaseBlendMode.overwrite]: "copy",
2120
+ [BaseBlendMode.sourceOver]: "source-over",
2121
+ [BaseBlendMode.darken]: "darken",
2122
+ [BaseBlendMode.multiply]: "multiply",
2123
+ [BaseBlendMode.colorBurn]: "color-burn",
2124
+ [BaseBlendMode.lighten]: "lighten",
2125
+ [BaseBlendMode.screen]: "screen",
2126
+ [BaseBlendMode.colorDodge]: "color-dodge",
2127
+ [BaseBlendMode.linearDodge]: "lighter",
2128
+ [BaseBlendMode.overlay]: "overlay",
2129
+ [BaseBlendMode.softLight]: "soft-light",
2130
+ [BaseBlendMode.hardLight]: "hard-light",
2131
+ [BaseBlendMode.difference]: "difference",
2132
+ [BaseBlendMode.exclusion]: "exclusion"
2133
+ };
1734
2134
 
1735
2135
  // src/Canvas/ReusableCanvas.ts
1736
2136
  function makeReusableCanvas() {
@@ -1783,37 +2183,38 @@ function makeReusableCanvasMeta(factory) {
1783
2183
  }
1784
2184
 
1785
2185
  // src/Canvas/CanvasFrameRenderer.ts
1786
- var defaults = {
1787
- makeReusableCanvas
1788
- };
1789
- function makeCanvasFrameRenderer(deps = defaults) {
1790
- const {
1791
- makeReusableCanvas: makeReusableCanvas2 = defaults.makeReusableCanvas
1792
- } = deps;
1793
- const bufferCanvas = makeReusableCanvas2();
2186
+ function makeCanvasFrameRenderer(reusableCanvasFactory = makeReusableOffscreenCanvas) {
2187
+ const getBuffer = reusableCanvasFactory();
1794
2188
  return function renderCanvasFrame(pixelCanvas, scale, getImageData, drawPixelLayer, drawScreenLayer) {
1795
- const {
1796
- canvas,
1797
- ctx
1798
- } = pixelCanvas;
1799
- const {
1800
- ctx: pxCtx,
1801
- canvas: pxCanvas
1802
- } = bufferCanvas(canvas.width, canvas.height);
2189
+ const canvas = pixelCanvas.canvas;
2190
+ const ctx = pixelCanvas.ctx;
2191
+ const w = canvas.width;
2192
+ const h = canvas.height;
2193
+ const buffer = getBuffer(w, h);
1803
2194
  const img = getImageData();
1804
2195
  if (img) {
1805
- pxCtx.putImageData(img, 0, 0);
2196
+ buffer.ctx.putImageData(img, 0, 0);
1806
2197
  }
1807
- drawPixelLayer?.(pxCtx);
2198
+ drawPixelLayer?.(buffer.ctx);
1808
2199
  ctx.setTransform(1, 0, 0, 1, 0, 0);
1809
- ctx.clearRect(0, 0, canvas.width, canvas.height);
2200
+ ctx.clearRect(0, 0, w, h);
1810
2201
  ctx.setTransform(scale, 0, 0, scale, 0, 0);
1811
- ctx.drawImage(pxCanvas, 0, 0);
2202
+ ctx.drawImage(buffer.canvas, 0, 0);
1812
2203
  ctx.setTransform(1, 0, 0, 1, 0, 0);
1813
2204
  drawScreenLayer?.(ctx, scale);
1814
2205
  };
1815
2206
  }
1816
2207
 
2208
+ // src/Canvas/CanvasPixelDataRenderer.ts
2209
+ function makeCanvasPixelDataRenderer(reusableCanvasFactory = makeReusableOffscreenCanvas) {
2210
+ const bufferCanvas = reusableCanvasFactory();
2211
+ return function drawPixelData(targetCtx, pixelData, x = 0, y = 0) {
2212
+ const buffer = bufferCanvas(pixelData.w, pixelData.h);
2213
+ buffer.ctx.putImageData(pixelData.imageData, 0, 0);
2214
+ targetCtx.drawImage(buffer.canvas, x, y);
2215
+ };
2216
+ }
2217
+
1817
2218
  // src/Canvas/PixelCanvas.ts
1818
2219
  function makePixelCanvas(canvas) {
1819
2220
  const ctx = canvas.getContext("2d");
@@ -1830,24 +2231,6 @@ function makePixelCanvas(canvas) {
1830
2231
  };
1831
2232
  }
1832
2233
 
1833
- // src/Canvas/canvas-blend-modes.ts
1834
- var CANVAS_COMPOSITE_MAP = {
1835
- [BaseBlendMode.overwrite]: "copy",
1836
- [BaseBlendMode.sourceOver]: "source-over",
1837
- [BaseBlendMode.darken]: "darken",
1838
- [BaseBlendMode.multiply]: "multiply",
1839
- [BaseBlendMode.colorBurn]: "color-burn",
1840
- [BaseBlendMode.lighten]: "lighten",
1841
- [BaseBlendMode.screen]: "screen",
1842
- [BaseBlendMode.colorDodge]: "color-dodge",
1843
- [BaseBlendMode.linearDodge]: "lighter",
1844
- [BaseBlendMode.overlay]: "overlay",
1845
- [BaseBlendMode.softLight]: "soft-light",
1846
- [BaseBlendMode.hardLight]: "hard-light",
1847
- [BaseBlendMode.difference]: "difference",
1848
- [BaseBlendMode.exclusion]: "exclusion"
1849
- };
1850
-
1851
2234
  // src/ImageData/imgBlobToImageData.ts
1852
2235
  async function imgBlobToImageData(blob) {
1853
2236
  let bitmap = null;
@@ -1912,10 +2295,10 @@ function applyPatchTiles(target, tiles, tileSize) {
1912
2295
  for (let i = 0; i < tiles.length; i++) {
1913
2296
  const tile = tiles[i];
1914
2297
  if (!tile) continue;
1915
- const dst = target.data32;
1916
- const src = tile.data32;
1917
- const dstWidth = target.width;
1918
- const dstHeight = target.height;
2298
+ const dst = target.data;
2299
+ const src = tile.data;
2300
+ const dstWidth = target.w;
2301
+ const dstHeight = target.h;
1919
2302
  const startX = tile.tx * tileSize;
1920
2303
  const startY = tile.ty * tileSize;
1921
2304
  const copyWidth = Math.max(0, Math.min(tileSize, dstWidth - startX));
@@ -2010,17 +2393,17 @@ var HistoryManager = class {
2010
2393
 
2011
2394
  // src/History/PixelAccumulator.ts
2012
2395
  var PixelAccumulator = class {
2013
- constructor(config, tilePool) {
2396
+ constructor(config, pixelTilePool) {
2014
2397
  this.config = config;
2015
- this.tilePool = tilePool;
2398
+ this.pixelTilePool = pixelTilePool;
2016
2399
  this.lookup = [];
2017
2400
  this.beforeTiles = [];
2018
2401
  }
2019
2402
  lookup;
2020
2403
  beforeTiles;
2021
2404
  recyclePatch(patch) {
2022
- this.tilePool.releaseTiles(patch.beforeTiles);
2023
- this.tilePool.releaseTiles(patch.afterTiles);
2405
+ this.pixelTilePool.releaseTiles(patch.beforeTiles);
2406
+ this.pixelTilePool.releaseTiles(patch.afterTiles);
2024
2407
  }
2025
2408
  /**
2026
2409
  * @param x pixel x coordinate
@@ -2035,7 +2418,7 @@ var PixelAccumulator = class {
2035
2418
  let tile = this.lookup[id];
2036
2419
  let added = false;
2037
2420
  if (!tile) {
2038
- tile = this.tilePool.getTile(id, tx, ty);
2421
+ tile = this.pixelTilePool.getTile(id, tx, ty);
2039
2422
  this.extractState(tile);
2040
2423
  this.lookup[id] = tile;
2041
2424
  this.beforeTiles.push(tile);
@@ -2045,7 +2428,7 @@ var PixelAccumulator = class {
2045
2428
  if (!didChange && added) {
2046
2429
  this.beforeTiles.pop();
2047
2430
  this.lookup[id] = void 0;
2048
- this.tilePool.releaseTile(tile);
2431
+ this.pixelTilePool.releaseTile(tile);
2049
2432
  }
2050
2433
  return didChange;
2051
2434
  };
@@ -2069,7 +2452,7 @@ var PixelAccumulator = class {
2069
2452
  const id = ty * columns + tx;
2070
2453
  let tile = this.lookup[id];
2071
2454
  if (!tile) {
2072
- tile = this.tilePool.getTile(id, tx, ty);
2455
+ tile = this.pixelTilePool.getTile(id, tx, ty);
2073
2456
  this.extractState(tile);
2074
2457
  this.lookup[id] = tile;
2075
2458
  this.beforeTiles.push(tile);
@@ -2083,7 +2466,7 @@ var PixelAccumulator = class {
2083
2466
  let t = this.beforeTiles[i];
2084
2467
  if (t) {
2085
2468
  this.lookup[t.id] = void 0;
2086
- this.tilePool.releaseTile(t);
2469
+ this.pixelTilePool.releaseTile(t);
2087
2470
  }
2088
2471
  }
2089
2472
  this.beforeTiles.length = startIndex;
@@ -2095,7 +2478,7 @@ var PixelAccumulator = class {
2095
2478
  let tile = this.lookup[id];
2096
2479
  let added = false;
2097
2480
  if (!tile) {
2098
- tile = this.tilePool.getTile(id, tx, ty);
2481
+ tile = this.pixelTilePool.getTile(id, tx, ty);
2099
2482
  this.extractState(tile);
2100
2483
  this.lookup[id] = tile;
2101
2484
  this.beforeTiles.push(tile);
@@ -2105,7 +2488,7 @@ var PixelAccumulator = class {
2105
2488
  if (!didChange && added) {
2106
2489
  this.beforeTiles.pop();
2107
2490
  this.lookup[id] = void 0;
2108
- this.tilePool.releaseTile(tile);
2491
+ this.pixelTilePool.releaseTile(tile);
2109
2492
  }
2110
2493
  return didChange;
2111
2494
  };
@@ -2113,12 +2496,12 @@ var PixelAccumulator = class {
2113
2496
  extractState(tile) {
2114
2497
  const target = this.config.target;
2115
2498
  const TILE_SIZE = this.config.tileSize;
2116
- const dst = tile.data32;
2117
- const src = target.data32;
2499
+ const dst = tile.data;
2500
+ const src = target.data;
2118
2501
  const startX = tile.tx * TILE_SIZE;
2119
2502
  const startY = tile.ty * TILE_SIZE;
2120
- const targetWidth = target.width;
2121
- const targetHeight = target.height;
2503
+ const targetWidth = target.w;
2504
+ const targetHeight = target.h;
2122
2505
  if (startX >= targetWidth || startX + TILE_SIZE <= 0 || startY >= targetHeight || startY + TILE_SIZE <= 0) {
2123
2506
  dst.fill(0);
2124
2507
  return;
@@ -2149,7 +2532,7 @@ var PixelAccumulator = class {
2149
2532
  for (let i = 0; i < length; i++) {
2150
2533
  let beforeTile = this.beforeTiles[i];
2151
2534
  if (beforeTile) {
2152
- let afterTile = this.tilePool.getTile(beforeTile.id, beforeTile.tx, beforeTile.ty);
2535
+ let afterTile = this.pixelTilePool.getTile(beforeTile.id, beforeTile.tx, beforeTile.ty);
2153
2536
  this.extractState(afterTile);
2154
2537
  afterTiles.push(afterTile);
2155
2538
  }
@@ -2171,7 +2554,7 @@ var PixelAccumulator = class {
2171
2554
  let tile = this.beforeTiles[i];
2172
2555
  if (tile) {
2173
2556
  this.lookup[tile.id] = void 0;
2174
- this.tilePool.releaseTile(tile);
2557
+ this.pixelTilePool.releaseTile(tile);
2175
2558
  }
2176
2559
  }
2177
2560
  this.beforeTiles.length = 0;
@@ -2199,8 +2582,8 @@ var PixelEngineConfig = class {
2199
2582
  this.tileMask = tileSize - 1;
2200
2583
  this.tileArea = tileSize * tileSize;
2201
2584
  this.target = target;
2202
- this.targetColumns = target.width + this.tileMask >> this.tileShift;
2203
- this.targetRows = target.height + this.tileMask >> this.tileShift;
2585
+ this.targetColumns = target.w + this.tileMask >> this.tileShift;
2586
+ this.targetRows = target.h + this.tileMask >> this.tileShift;
2204
2587
  }
2205
2588
  };
2206
2589
 
@@ -2208,8 +2591,8 @@ var PixelEngineConfig = class {
2208
2591
  function applyAlphaMaskToPixelData(target, mask, opts) {
2209
2592
  const targetX = opts?.x ?? 0;
2210
2593
  const targetY = opts?.y ?? 0;
2211
- const width = opts?.w ?? target.width;
2212
- const height = opts?.h ?? target.height;
2594
+ const width = opts?.w ?? target.w;
2595
+ const height = opts?.h ?? target.h;
2213
2596
  const globalAlpha = opts?.alpha ?? 255;
2214
2597
  const mx = opts?.mx ?? 0;
2215
2598
  const my = opts?.my ?? 0;
@@ -2227,8 +2610,8 @@ function applyAlphaMaskToPixelData(target, mask, opts) {
2227
2610
  h += y;
2228
2611
  y = 0;
2229
2612
  }
2230
- w = Math.min(w, target.width - x);
2231
- h = Math.min(h, target.height - y);
2613
+ w = Math.min(w, target.w - x);
2614
+ h = Math.min(h, target.h - y);
2232
2615
  if (w <= 0) return false;
2233
2616
  if (h <= 0) return false;
2234
2617
  const mPitch = mask.w;
@@ -2245,49 +2628,82 @@ function applyAlphaMaskToPixelData(target, mask, opts) {
2245
2628
  if (finalH <= 0) return false;
2246
2629
  const xShift = sX0 - startX;
2247
2630
  const yShift = sY0 - startY;
2248
- const dst32 = target.data32;
2249
- const dw = target.width;
2631
+ const dst32 = target.data;
2632
+ const dw = target.w;
2250
2633
  const dStride = dw - finalW;
2251
2634
  const mStride = mPitch - finalW;
2252
2635
  const maskData = mask.data;
2253
2636
  let dIdx = (y + yShift) * dw + (x + xShift);
2254
2637
  let mIdx = sY0 * mPitch + sX0;
2255
2638
  let didChange = false;
2256
- for (let iy = 0; iy < h; iy++) {
2257
- for (let ix = 0; ix < w; ix++) {
2258
- const mVal = maskData[mIdx];
2259
- const effectiveM = invertMask ? 255 - mVal : mVal;
2260
- let weight = 0;
2261
- if (effectiveM === 0) {
2262
- weight = 0;
2263
- } else if (effectiveM === 255) {
2264
- weight = globalAlpha;
2265
- } else if (globalAlpha === 255) {
2266
- weight = effectiveM;
2267
- } else {
2268
- weight = effectiveM * globalAlpha + 128 >> 8;
2639
+ if (invertMask) {
2640
+ for (let iy = 0; iy < finalH; iy++) {
2641
+ for (let ix = 0; ix < finalW; ix++) {
2642
+ const effectiveM = 255 - maskData[mIdx];
2643
+ if (effectiveM === 0) {
2644
+ const current = dst32[dIdx];
2645
+ const next = (current & 16777215) >>> 0;
2646
+ if (current !== next) {
2647
+ dst32[dIdx] = next;
2648
+ didChange = true;
2649
+ }
2650
+ } else {
2651
+ const t1 = effectiveM * globalAlpha + 128;
2652
+ const weight = t1 + (t1 >> 8) >> 8;
2653
+ if (weight < 255) {
2654
+ const current = dst32[dIdx];
2655
+ const da = current >>> 24;
2656
+ if (da !== 0) {
2657
+ const t2 = da * weight + 128;
2658
+ const finalAlpha = t2 + (t2 >> 8) >> 8;
2659
+ const next = (current & 16777215 | finalAlpha << 24) >>> 0;
2660
+ if (current !== next) {
2661
+ dst32[dIdx] = next;
2662
+ didChange = true;
2663
+ }
2664
+ }
2665
+ }
2666
+ }
2667
+ dIdx++;
2668
+ mIdx++;
2269
2669
  }
2270
- if (weight === 0) {
2271
- dst32[dIdx] = (dst32[dIdx] & 16777215) >>> 0;
2272
- didChange = true;
2273
- } else if (weight !== 255) {
2274
- const d = dst32[dIdx];
2275
- const da = d >>> 24;
2276
- if (da !== 0) {
2277
- const finalAlpha = da === 255 ? weight : da * weight + 128 >> 8;
2670
+ dIdx += dStride;
2671
+ mIdx += mStride;
2672
+ }
2673
+ } else {
2674
+ for (let iy = 0; iy < finalH; iy++) {
2675
+ for (let ix = 0; ix < finalW; ix++) {
2676
+ const effectiveM = maskData[mIdx];
2677
+ if (effectiveM === 0) {
2278
2678
  const current = dst32[dIdx];
2279
- const next = (d & 16777215 | finalAlpha << 24) >>> 0;
2679
+ const next = (current & 16777215) >>> 0;
2280
2680
  if (current !== next) {
2281
2681
  dst32[dIdx] = next;
2282
2682
  didChange = true;
2283
2683
  }
2684
+ } else {
2685
+ const t1 = effectiveM * globalAlpha + 128;
2686
+ const weight = t1 + (t1 >> 8) >> 8;
2687
+ if (weight < 255) {
2688
+ const current = dst32[dIdx];
2689
+ const da = current >>> 24;
2690
+ if (da !== 0) {
2691
+ const t2 = da * weight + 128;
2692
+ const finalAlpha = t2 + (t2 >> 8) >> 8;
2693
+ const next = (current & 16777215 | finalAlpha << 24) >>> 0;
2694
+ if (current !== next) {
2695
+ dst32[dIdx] = next;
2696
+ didChange = true;
2697
+ }
2698
+ }
2699
+ }
2284
2700
  }
2701
+ dIdx++;
2702
+ mIdx++;
2285
2703
  }
2286
- dIdx++;
2287
- mIdx++;
2704
+ dIdx += dStride;
2705
+ mIdx += mStride;
2288
2706
  }
2289
- dIdx += dStride;
2290
- mIdx += mStride;
2291
2707
  }
2292
2708
  return didChange;
2293
2709
  }
@@ -2321,338 +2737,77 @@ function resizeImageData(target, newWidth, newHeight, offsetX = 0, offsetY = 0)
2321
2737
  return result;
2322
2738
  }
2323
2739
 
2324
- // src/Rect/trimRectBounds.ts
2325
- function trimRectBounds(x, y, w, h, targetWidth, targetHeight, out) {
2326
- const res = out ?? {
2327
- x: 0,
2328
- y: 0,
2329
- w: 0,
2330
- h: 0
2740
+ // src/PixelData/PixelData.ts
2741
+ function makePixelData(imageData) {
2742
+ return {
2743
+ data: new Uint32Array(
2744
+ imageData.data.buffer,
2745
+ imageData.data.byteOffset,
2746
+ // Shift right by 2 is a fast bitwise division by 4.
2747
+ imageData.data.byteLength >> 2
2748
+ ),
2749
+ imageData,
2750
+ w: imageData.width,
2751
+ h: imageData.height
2331
2752
  };
2332
- const left = Math.max(0, x);
2333
- const top = Math.max(0, y);
2334
- const right = Math.min(targetWidth, x + w);
2335
- const bottom = Math.min(targetHeight, y + h);
2336
- res.x = left;
2337
- res.y = top;
2338
- res.w = Math.max(0, right - left);
2339
- res.h = Math.max(0, bottom - top);
2340
- return res;
2753
+ }
2754
+ function setPixelData(target, imageData) {
2755
+ ;
2756
+ target.data = new Uint32Array(
2757
+ imageData.data.buffer,
2758
+ imageData.data.byteOffset,
2759
+ // Shift right by 2 is a fast bitwise division by 4.
2760
+ imageData.data.byteLength >> 2
2761
+ );
2762
+ target.imageData = imageData;
2763
+ target.w = imageData.width;
2764
+ target.h = imageData.height;
2341
2765
  }
2342
2766
 
2343
- // src/Paint/PaintBuffer.ts
2344
- var PaintBuffer = class {
2345
- constructor(config, tilePool) {
2346
- this.config = config;
2347
- this.tilePool = tilePool;
2348
- this.lookup = [];
2349
- }
2350
- lookup;
2351
- scratchBounds = {
2352
- x: 0,
2353
- y: 0,
2354
- w: 0,
2355
- h: 0
2356
- };
2357
- eachTileInBounds(bounds, callback) {
2358
- const {
2359
- tileShift,
2360
- targetColumns,
2361
- targetRows,
2362
- tileSize
2363
- } = this.config;
2364
- const x1 = Math.max(0, bounds.x >> tileShift);
2365
- const y1 = Math.max(0, bounds.y >> tileShift);
2366
- const x2 = Math.min(targetColumns - 1, bounds.x + bounds.w - 1 >> tileShift);
2367
- const y2 = Math.min(targetRows - 1, bounds.y + bounds.h - 1 >> tileShift);
2368
- if (x1 > x2 || y1 > y2) return;
2369
- const lookup = this.lookup;
2370
- const tilePool = this.tilePool;
2371
- for (let ty = y1; ty <= y2; ty++) {
2372
- const rowOffset = ty * targetColumns;
2373
- const tileTop = ty << tileShift;
2374
- for (let tx = x1; tx <= x2; tx++) {
2375
- const id = rowOffset + tx;
2376
- const tile = lookup[id] ?? (lookup[id] = tilePool.getTile(id, tx, ty));
2377
- const tileLeft = tx << tileShift;
2378
- const startX = bounds.x > tileLeft ? bounds.x : tileLeft;
2379
- const startY = bounds.y > tileTop ? bounds.y : tileTop;
2380
- const maskEndX = bounds.x + bounds.w;
2381
- const tileEndX = tileLeft + tileSize;
2382
- const endX = maskEndX < tileEndX ? maskEndX : tileEndX;
2383
- const maskEndY = bounds.y + bounds.h;
2384
- const tileEndY = tileTop + tileSize;
2385
- const endY = maskEndY < tileEndY ? maskEndY : tileEndY;
2386
- callback(tile, startX, startY, endX - startX, endY - startY);
2387
- }
2388
- }
2389
- }
2390
- writePaintAlphaMaskStroke(color, brush, x0, y0, x1, y1) {
2391
- const cA = color >>> 24;
2392
- if (cA === 0) return false;
2393
- const {
2394
- tileShift,
2395
- tileMask,
2396
- target
2397
- } = this.config;
2398
- const {
2399
- w: bW,
2400
- h: bH,
2401
- data: bD,
2402
- centerOffsetX,
2403
- centerOffsetY
2404
- } = brush;
2405
- const cRGB = color & 16777215;
2406
- const scratch = this.scratchBounds;
2407
- let changed = false;
2408
- forEachLinePoint(x0, y0, x1, y1, (px, py) => {
2409
- const topLeftX = Math.floor(px + centerOffsetX);
2410
- const topLeftY = Math.floor(py + centerOffsetY);
2411
- trimRectBounds(topLeftX, topLeftY, bW, bH, target.width, target.height, scratch);
2412
- if (scratch.w <= 0 || scratch.h <= 0) return;
2413
- this.eachTileInBounds(scratch, (tile, bX, bY, bW_t, bH_t) => {
2414
- const d32 = tile.data32;
2415
- let tileChanged = false;
2416
- for (let i = 0; i < bH_t; i++) {
2417
- const canvasY = bY + i;
2418
- const bOff = (canvasY - topLeftY) * bW;
2419
- const tOff = (canvasY & tileMask) << tileShift;
2420
- const dS = tOff + (bX & tileMask);
2421
- for (let j = 0; j < bW_t; j++) {
2422
- const canvasX = bX + j;
2423
- const brushA = bD[bOff + (canvasX - topLeftX)];
2424
- if (brushA === 0) continue;
2425
- const t = cA * brushA + 128;
2426
- const blendedA = t + (t >> 8) >> 8;
2427
- const idx = dS + j;
2428
- const cur = d32[idx];
2429
- if (brushA > cur >>> 24) {
2430
- const next = (cRGB | blendedA << 24) >>> 0;
2431
- if (cur !== next) {
2432
- d32[idx] = next;
2433
- tileChanged = true;
2434
- }
2435
- }
2436
- }
2437
- }
2438
- if (tileChanged) changed = true;
2439
- });
2440
- });
2441
- return changed;
2442
- }
2443
- writePaintBinaryMaskStroke(color, brush, x0, y0, x1, y1) {
2444
- const alphaIsZero = color >>> 24 === 0;
2445
- if (alphaIsZero) return false;
2446
- const {
2447
- tileShift,
2448
- tileMask,
2449
- target
2450
- } = this.config;
2451
- const {
2452
- w: bW,
2453
- h: bH,
2454
- data: bD,
2455
- centerOffsetX,
2456
- centerOffsetY
2457
- } = brush;
2458
- const scratch = this.scratchBounds;
2459
- let changed = false;
2460
- forEachLinePoint(x0, y0, x1, y1, (px, py) => {
2461
- const topLeftX = Math.floor(px + centerOffsetX);
2462
- const topLeftY = Math.floor(py + centerOffsetY);
2463
- trimRectBounds(topLeftX, topLeftY, bW, bH, target.width, target.height, scratch);
2464
- if (scratch.w <= 0 || scratch.h <= 0) return;
2465
- this.eachTileInBounds(scratch, (tile, bX, bY, bW_t, bH_t) => {
2466
- const d32 = tile.data32;
2467
- let tileChanged = false;
2468
- for (let i = 0; i < bH_t; i++) {
2469
- const canvasY = bY + i;
2470
- const bOff = (canvasY - topLeftY) * bW;
2471
- const tOff = (canvasY & tileMask) << tileShift;
2472
- const dS = tOff + (bX & tileMask);
2473
- for (let j = 0; j < bW_t; j++) {
2474
- const canvasX = bX + j;
2475
- if (bD[bOff + (canvasX - topLeftX)]) {
2476
- const idx = dS + j;
2477
- if (d32[idx] !== color) {
2478
- d32[idx] = color;
2479
- tileChanged = true;
2480
- }
2481
- }
2482
- }
2483
- }
2484
- if (tileChanged) changed = true;
2485
- });
2486
- });
2487
- return changed;
2488
- }
2489
- writeRectStroke(color, brushWidth, brushHeight, x0, y0, x1, y1) {
2490
- const alphaIsZero = color >>> 24 === 0;
2491
- if (alphaIsZero) return false;
2492
- const config = this.config;
2493
- const tileShift = config.tileShift;
2494
- const tileMask = config.tileMask;
2495
- const target = config.target;
2496
- const scratch = this.scratchBounds;
2497
- const centerOffsetX = -(brushWidth - 1 >> 1);
2498
- const centerOffsetY = -(brushHeight - 1 >> 1);
2499
- let changed = false;
2500
- forEachLinePoint(x0, y0, x1, y1, (px, py) => {
2501
- const topLeftX = Math.floor(px + centerOffsetX);
2502
- const topLeftY = Math.floor(py + centerOffsetY);
2503
- trimRectBounds(topLeftX, topLeftY, brushWidth, brushHeight, target.width, target.height, scratch);
2504
- if (scratch.w <= 0 || scratch.h <= 0) return;
2505
- this.eachTileInBounds(scratch, (tile, bX, bY, bW_t, bH_t) => {
2506
- const d32 = tile.data32;
2507
- let tileChanged = false;
2508
- for (let i = 0; i < bH_t; i++) {
2509
- const canvasY = bY + i;
2510
- const tOff = (canvasY & tileMask) << tileShift;
2511
- const dS = tOff + (bX & tileMask);
2512
- for (let j = 0; j < bW_t; j++) {
2513
- const idx = dS + j;
2514
- if (d32[idx] !== color) {
2515
- d32[idx] = color;
2516
- tileChanged = true;
2517
- }
2518
- }
2519
- }
2520
- if (tileChanged) {
2521
- changed = true;
2522
- }
2523
- });
2524
- });
2525
- return changed;
2526
- }
2527
- clear() {
2528
- this.tilePool.releaseTiles(this.lookup);
2529
- }
2530
- };
2767
+ // src/Tile/_tile-types.ts
2768
+ var TileType = /* @__PURE__ */ ((TileType2) => {
2769
+ TileType2[TileType2["PIXEL"] = 0] = "PIXEL";
2770
+ TileType2[TileType2["MASK"] = 1] = "MASK";
2771
+ return TileType2;
2772
+ })(TileType || {});
2531
2773
 
2532
- // src/PixelData/blendPixelData.ts
2533
- function blendPixelData(target, src, opts) {
2534
- const targetX = opts?.x ?? 0;
2535
- const targetY = opts?.y ?? 0;
2536
- const sourceX = opts?.sx ?? 0;
2537
- const sourceY = opts?.sy ?? 0;
2538
- const width = opts?.w ?? src.width;
2539
- const height = opts?.h ?? src.height;
2540
- const globalAlpha = opts?.alpha ?? 255;
2541
- const blendFn = opts?.blendFn ?? sourceOverPerfect;
2542
- if (globalAlpha === 0) return false;
2543
- let x = targetX;
2544
- let y = targetY;
2545
- let sx = sourceX;
2546
- let sy = sourceY;
2547
- let w = width;
2548
- let h = height;
2549
- if (sx < 0) {
2550
- x -= sx;
2551
- w += sx;
2552
- sx = 0;
2553
- }
2554
- if (sy < 0) {
2555
- y -= sy;
2556
- h += sy;
2557
- sy = 0;
2558
- }
2559
- w = Math.min(w, src.width - sx);
2560
- h = Math.min(h, src.height - sy);
2561
- if (x < 0) {
2562
- sx -= x;
2563
- w += x;
2564
- x = 0;
2565
- }
2566
- if (y < 0) {
2567
- sy -= y;
2568
- h += y;
2569
- y = 0;
2570
- }
2571
- const actualW = Math.min(w, target.width - x);
2572
- const actualH = Math.min(h, target.height - y);
2573
- if (actualW <= 0 || actualH <= 0) return false;
2574
- const dst32 = target.data32;
2575
- const src32 = src.data32;
2576
- const dw = target.width;
2577
- const sw = src.width;
2578
- let dIdx = y * dw + x | 0;
2579
- let sIdx = sy * sw + sx | 0;
2580
- const dStride = dw - actualW | 0;
2581
- const sStride = sw - actualW | 0;
2582
- const isOpaque = globalAlpha === 255;
2583
- const isOverwrite = blendFn.isOverwrite;
2584
- let didChange = false;
2585
- for (let iy = 0; iy < actualH; iy++) {
2586
- for (let ix = 0; ix < actualW; ix++) {
2587
- const srcCol = src32[sIdx];
2588
- const srcAlpha = srcCol >>> 24;
2589
- if (srcAlpha === 0 && !isOverwrite) {
2590
- dIdx++;
2591
- sIdx++;
2592
- continue;
2593
- }
2594
- let finalCol = srcCol;
2595
- if (!isOpaque) {
2596
- const a = srcAlpha * globalAlpha + 128 >> 8;
2597
- if (a === 0 && !isOverwrite) {
2598
- dIdx++;
2599
- sIdx++;
2600
- continue;
2601
- }
2602
- finalCol = (srcCol & 16777215 | a << 24) >>> 0;
2603
- }
2604
- const current = dst32[dIdx];
2605
- const next = blendFn(finalCol, dst32[dIdx]);
2606
- if (current !== next) {
2607
- dst32[dIdx] = next;
2608
- didChange = true;
2609
- }
2610
- dIdx++;
2611
- sIdx++;
2612
- }
2613
- dIdx += dStride;
2614
- sIdx += sStride;
2615
- }
2616
- return didChange;
2774
+ // src/Tile/PixelTile.ts
2775
+ function makePixelTile(id, tx, ty, tileSize, tileArea) {
2776
+ const data32 = new Uint32Array(tileArea);
2777
+ const data8 = new Uint8ClampedArray(data32.buffer);
2778
+ return {
2779
+ tileType: 0 /* PIXEL */,
2780
+ id,
2781
+ tx,
2782
+ ty,
2783
+ w: tileSize,
2784
+ h: tileSize,
2785
+ data: data32,
2786
+ imageData: new ImageData(data8, tileSize, tileSize)
2787
+ };
2617
2788
  }
2618
2789
 
2619
- // src/PixelTile/PixelTile.ts
2620
- var PixelTile = class {
2621
- constructor(id, tx, ty, tileSize, tileArea) {
2622
- this.id = id;
2623
- this.tx = tx;
2624
- this.ty = ty;
2625
- this.width = this.height = tileSize;
2626
- this.data32 = new Uint32Array(tileArea);
2627
- const data8 = new Uint8ClampedArray(this.data32.buffer);
2628
- this.imageData = new ImageData(data8, tileSize, tileSize);
2629
- }
2630
- data32;
2631
- width;
2632
- height;
2633
- imageData;
2634
- };
2635
-
2636
- // src/PixelTile/PixelTilePool.ts
2637
- var PixelTilePool = class {
2638
- pool;
2639
- tileSize;
2640
- tileArea;
2641
- constructor(config) {
2790
+ // src/Tile/TilePool.ts
2791
+ var TilePool = class {
2792
+ constructor(config, tileFactory) {
2793
+ this.tileFactory = tileFactory;
2642
2794
  this.pool = [];
2643
2795
  this.tileSize = config.tileSize;
2644
2796
  this.tileArea = config.tileArea;
2645
2797
  }
2798
+ pool;
2799
+ tileSize;
2800
+ tileArea;
2646
2801
  getTile(id, tx, ty) {
2647
2802
  let tile = this.pool.pop();
2648
2803
  if (tile) {
2649
2804
  tile.id = id;
2650
2805
  tile.tx = tx;
2651
2806
  tile.ty = ty;
2652
- tile.data32.fill(0);
2807
+ tile.data.fill(0);
2653
2808
  return tile;
2654
2809
  }
2655
- return new PixelTile(id, tx, ty, this.tileSize, this.tileArea);
2810
+ return this.tileFactory(id, tx, ty, this.tileSize, this.tileArea);
2656
2811
  }
2657
2812
  releaseTile(tile) {
2658
2813
  this.pool.push(tile);
@@ -2676,16 +2831,7 @@ var PixelWriter = class {
2676
2831
  historyActionFactory;
2677
2832
  config;
2678
2833
  pixelTilePool;
2679
- paintBuffer;
2680
2834
  mutator;
2681
- blendPixelDataOpts = {
2682
- alpha: 255,
2683
- blendFn: sourceOverPerfect,
2684
- x: 0,
2685
- y: 0,
2686
- w: 0,
2687
- h: 0
2688
- };
2689
2835
  _inProgress = false;
2690
2836
  constructor(target, mutatorFactory, options) {
2691
2837
  const tileSize = options?.tileSize ?? 256;
@@ -2693,10 +2839,9 @@ var PixelWriter = class {
2693
2839
  this.config = new PixelEngineConfig(tileSize, target);
2694
2840
  this.historyManager = options?.historyManager ?? new HistoryManager(maxHistorySteps);
2695
2841
  this.historyActionFactory = options?.historyActionFactory ?? makeHistoryAction;
2696
- this.pixelTilePool = options?.pixelTilePool ?? new PixelTilePool(this.config);
2842
+ this.pixelTilePool = options?.pixelTilePool ?? new TilePool(this.config, makePixelTile);
2697
2843
  this.accumulator = options?.accumulator ?? new PixelAccumulator(this.config, this.pixelTilePool);
2698
2844
  this.mutator = mutatorFactory(this);
2699
- this.paintBuffer = new PaintBuffer(this.config, this.pixelTilePool);
2700
2845
  }
2701
2846
  /**
2702
2847
  * Executes `transaction` and commits the resulting pixel changes as a single
@@ -2742,59 +2887,37 @@ var PixelWriter = class {
2742
2887
  const target = config.target;
2743
2888
  const beforeImageData = target.imageData;
2744
2889
  const afterImageData = resizeImageDataFn(beforeImageData, newWidth, newHeight, offsetX, offsetY);
2745
- target.set(afterImageData);
2890
+ setPixelData(target, afterImageData);
2746
2891
  this.historyManager.commit({
2747
2892
  undo: () => {
2748
- target.set(beforeImageData);
2893
+ setPixelData(target, beforeImageData);
2749
2894
  afterUndo?.(beforeImageData);
2750
2895
  after?.(beforeImageData);
2751
2896
  },
2752
2897
  redo: () => {
2753
- target.set(afterImageData);
2898
+ setPixelData(target, afterImageData);
2754
2899
  afterRedo?.(afterImageData);
2755
2900
  after?.(afterImageData);
2756
2901
  }
2757
2902
  });
2758
2903
  }
2759
- commitPaintBuffer(alpha = 255, blendFn = sourceOverPerfect, blendPixelDataFn = blendPixelData) {
2760
- const paintBuffer = this.paintBuffer;
2761
- const tileShift = paintBuffer.config.tileShift;
2762
- const lookup = paintBuffer.lookup;
2763
- const opts = this.blendPixelDataOpts;
2764
- opts.alpha = alpha;
2765
- opts.blendFn = blendFn;
2766
- for (let i = 0; i < lookup.length; i++) {
2767
- const tile = lookup[i];
2768
- if (tile) {
2769
- const didChange = this.accumulator.storeTileBeforeState(tile.id, tile.tx, tile.ty);
2770
- const dx = tile.tx << tileShift;
2771
- const dy = tile.ty << tileShift;
2772
- opts.x = dx;
2773
- opts.y = dy;
2774
- opts.w = tile.width;
2775
- opts.h = tile.height;
2776
- didChange(blendPixelDataFn(this.config.target, tile, opts));
2777
- }
2778
- }
2779
- paintBuffer.clear();
2780
- }
2781
2904
  };
2782
2905
 
2783
2906
  // src/History/PixelMutator/mutatorApplyAlphaMask.ts
2784
- var defaults2 = {
2907
+ var defaults = {
2785
2908
  applyAlphaMaskToPixelData
2786
2909
  };
2787
- var mutatorApplyAlphaMask = ((writer, deps = defaults2) => {
2910
+ var mutatorApplyAlphaMask = ((writer, deps = defaults) => {
2788
2911
  const {
2789
- applyAlphaMaskToPixelData: applyAlphaMaskToPixelData2 = defaults2.applyAlphaMaskToPixelData
2912
+ applyAlphaMaskToPixelData: applyAlphaMaskToPixelData2 = defaults.applyAlphaMaskToPixelData
2790
2913
  } = deps;
2791
2914
  return {
2792
2915
  applyAlphaMask(mask, opts) {
2793
2916
  const target = writer.config.target;
2794
2917
  const x = opts?.x ?? 0;
2795
2918
  const y = opts?.y ?? 0;
2796
- const w = opts?.w ?? target.width;
2797
- const h = opts?.h ?? target.height;
2919
+ const w = opts?.w ?? target.w;
2920
+ const h = opts?.h ?? target.h;
2798
2921
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
2799
2922
  return didChange(applyAlphaMaskToPixelData2(target, mask, opts));
2800
2923
  }
@@ -2805,8 +2928,8 @@ var mutatorApplyAlphaMask = ((writer, deps = defaults2) => {
2805
2928
  function applyBinaryMaskToPixelData(target, mask, opts) {
2806
2929
  const targetX = opts?.x ?? 0;
2807
2930
  const targetY = opts?.y ?? 0;
2808
- const width = opts?.w ?? target.width;
2809
- const height = opts?.h ?? target.height;
2931
+ const width = opts?.w ?? target.w;
2932
+ const height = opts?.h ?? target.h;
2810
2933
  const globalAlpha = opts?.alpha ?? 255;
2811
2934
  const mx = opts?.mx ?? 0;
2812
2935
  const my = opts?.my ?? 0;
@@ -2824,8 +2947,8 @@ function applyBinaryMaskToPixelData(target, mask, opts) {
2824
2947
  h += y;
2825
2948
  y = 0;
2826
2949
  }
2827
- w = Math.min(w, target.width - x);
2828
- h = Math.min(h, target.height - y);
2950
+ w = Math.min(w, target.w - x);
2951
+ h = Math.min(h, target.h - y);
2829
2952
  if (w <= 0 || h <= 0) return false;
2830
2953
  const mPitch = mask.w;
2831
2954
  if (mPitch <= 0) return false;
@@ -2842,8 +2965,8 @@ function applyBinaryMaskToPixelData(target, mask, opts) {
2842
2965
  }
2843
2966
  const xShift = sX0 - startX;
2844
2967
  const yShift = sY0 - startY;
2845
- const dst32 = target.data32;
2846
- const dw = target.width;
2968
+ const dst32 = target.data;
2969
+ const dw = target.w;
2847
2970
  const dStride = dw - finalW;
2848
2971
  const mStride = mPitch - finalW;
2849
2972
  const maskData = mask.data;
@@ -2883,20 +3006,20 @@ function applyBinaryMaskToPixelData(target, mask, opts) {
2883
3006
  }
2884
3007
 
2885
3008
  // src/History/PixelMutator/mutatorApplyBinaryMask.ts
2886
- var defaults3 = {
3009
+ var defaults2 = {
2887
3010
  applyBinaryMaskToPixelData
2888
3011
  };
2889
- var mutatorApplyBinaryMask = ((writer, deps = defaults3) => {
3012
+ var mutatorApplyBinaryMask = ((writer, deps = defaults2) => {
2890
3013
  const {
2891
- applyBinaryMaskToPixelData: applyBinaryMaskToPixelData2 = defaults3.applyBinaryMaskToPixelData
3014
+ applyBinaryMaskToPixelData: applyBinaryMaskToPixelData2 = defaults2.applyBinaryMaskToPixelData
2892
3015
  } = deps;
2893
3016
  return {
2894
3017
  applyBinaryMask(mask, opts) {
2895
3018
  const target = writer.config.target;
2896
3019
  const x = opts?.x ?? 0;
2897
3020
  const y = opts?.y ?? 0;
2898
- const w = opts?.w ?? target.width;
2899
- const h = opts?.h ?? target.height;
3021
+ const w = opts?.w ?? target.w;
3022
+ const h = opts?.h ?? target.h;
2900
3023
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
2901
3024
  return didChange(applyBinaryMaskToPixelData2(target, mask, opts));
2902
3025
  }
@@ -2904,22 +3027,22 @@ var mutatorApplyBinaryMask = ((writer, deps = defaults3) => {
2904
3027
  });
2905
3028
 
2906
3029
  // src/History/PixelMutator/mutatorApplyMask.ts
2907
- var defaults4 = {
3030
+ var defaults3 = {
2908
3031
  applyBinaryMaskToPixelData,
2909
3032
  applyAlphaMaskToPixelData
2910
3033
  };
2911
- var mutatorApplyMask = ((writer, deps = defaults4) => {
3034
+ var mutatorApplyMask = ((writer, deps = defaults3) => {
2912
3035
  const {
2913
- applyBinaryMaskToPixelData: applyBinaryMaskToPixelData2 = defaults4.applyBinaryMaskToPixelData,
2914
- applyAlphaMaskToPixelData: applyAlphaMaskToPixelData2 = defaults4.applyAlphaMaskToPixelData
3036
+ applyBinaryMaskToPixelData: applyBinaryMaskToPixelData2 = defaults3.applyBinaryMaskToPixelData,
3037
+ applyAlphaMaskToPixelData: applyAlphaMaskToPixelData2 = defaults3.applyAlphaMaskToPixelData
2915
3038
  } = deps;
2916
3039
  return {
2917
3040
  applyMask(mask, opts) {
2918
3041
  const target = writer.config.target;
2919
3042
  const x = opts?.x ?? 0;
2920
3043
  const y = opts?.y ?? 0;
2921
- const w = opts?.w ?? target.width;
2922
- const h = opts?.h ?? target.height;
3044
+ const w = opts?.w ?? target.w;
3045
+ const h = opts?.h ?? target.h;
2923
3046
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
2924
3047
  if (mask.type === 1 /* BINARY */) {
2925
3048
  return didChange(applyBinaryMaskToPixelData2(target, mask, opts));
@@ -2936,8 +3059,8 @@ function blendPixelDataAlphaMask(target, src, alphaMask, opts) {
2936
3059
  const targetY = opts?.y ?? 0;
2937
3060
  const sourceX = opts?.sx ?? 0;
2938
3061
  const sourceY = opts?.sy ?? 0;
2939
- const width = opts?.w ?? src.width;
2940
- const height = opts?.h ?? src.height;
3062
+ const width = opts?.w ?? src.w;
3063
+ const height = opts?.h ?? src.h;
2941
3064
  const globalAlpha = opts?.alpha ?? 255;
2942
3065
  const blendFn = opts?.blendFn ?? sourceOverPerfect;
2943
3066
  const mx = opts?.mx ?? 0;
@@ -2960,8 +3083,8 @@ function blendPixelDataAlphaMask(target, src, alphaMask, opts) {
2960
3083
  h += sy;
2961
3084
  sy = 0;
2962
3085
  }
2963
- w = Math.min(w, src.width - sx);
2964
- h = Math.min(h, src.height - sy);
3086
+ w = Math.min(w, src.w - sx);
3087
+ h = Math.min(h, src.h - sy);
2965
3088
  if (x < 0) {
2966
3089
  sx -= x;
2967
3090
  w += x;
@@ -2972,17 +3095,17 @@ function blendPixelDataAlphaMask(target, src, alphaMask, opts) {
2972
3095
  h += y;
2973
3096
  y = 0;
2974
3097
  }
2975
- const actualW = Math.min(w, target.width - x);
2976
- const actualH = Math.min(h, target.height - y);
3098
+ const actualW = Math.min(w, target.w - x);
3099
+ const actualH = Math.min(h, target.h - y);
2977
3100
  if (actualW <= 0 || actualH <= 0) return false;
2978
- const dw = target.width;
2979
- const sw = src.width;
3101
+ const dw = target.w;
3102
+ const sw = src.w;
2980
3103
  const mPitch = alphaMask.w;
2981
3104
  const maskData = alphaMask.data;
2982
3105
  const dx = x - targetX | 0;
2983
3106
  const dy = y - targetY | 0;
2984
- const dst32 = target.data32;
2985
- const src32 = src.data32;
3107
+ const dst32 = target.data;
3108
+ const src32 = src.data;
2986
3109
  let dIdx = y * dw + x | 0;
2987
3110
  let sIdx = sy * sw + sx | 0;
2988
3111
  let mIdx = (my + dy) * mPitch + (mx + dx) | 0;
@@ -3051,19 +3174,19 @@ function blendPixelDataAlphaMask(target, src, alphaMask, opts) {
3051
3174
  }
3052
3175
 
3053
3176
  // src/History/PixelMutator/mutatorBlendAlphaMask.ts
3054
- var defaults5 = {
3177
+ var defaults4 = {
3055
3178
  blendPixelDataAlphaMask
3056
3179
  };
3057
- var mutatorBlendAlphaMask = ((writer, deps = defaults5) => {
3180
+ var mutatorBlendAlphaMask = ((writer, deps = defaults4) => {
3058
3181
  const {
3059
- blendPixelDataAlphaMask: blendPixelDataAlphaMask2 = defaults5.blendPixelDataAlphaMask
3182
+ blendPixelDataAlphaMask: blendPixelDataAlphaMask2 = defaults4.blendPixelDataAlphaMask
3060
3183
  } = deps;
3061
3184
  return {
3062
3185
  blendAlphaMask(src, mask, opts) {
3063
3186
  const x = opts?.x ?? 0;
3064
3187
  const y = opts?.y ?? 0;
3065
- const w = opts?.w ?? src.width;
3066
- const h = opts?.h ?? src.height;
3188
+ const w = opts?.w ?? src.w;
3189
+ const h = opts?.h ?? src.h;
3067
3190
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
3068
3191
  return didChange(blendPixelDataAlphaMask2(writer.config.target, src, mask, opts));
3069
3192
  }
@@ -3076,8 +3199,8 @@ function blendPixelDataBinaryMask(target, src, binaryMask, opts) {
3076
3199
  const targetY = opts?.y ?? 0;
3077
3200
  const sourceX = opts?.sx ?? 0;
3078
3201
  const sourceY = opts?.sy ?? 0;
3079
- const width = opts?.w ?? src.width;
3080
- const height = opts?.h ?? src.height;
3202
+ const width = opts?.w ?? src.w;
3203
+ const height = opts?.h ?? src.h;
3081
3204
  const globalAlpha = opts?.alpha ?? 255;
3082
3205
  const blendFn = opts?.blendFn ?? sourceOverPerfect;
3083
3206
  const mx = opts?.mx ?? 0;
@@ -3100,8 +3223,8 @@ function blendPixelDataBinaryMask(target, src, binaryMask, opts) {
3100
3223
  h += sy;
3101
3224
  sy = 0;
3102
3225
  }
3103
- w = Math.min(w, src.width - sx);
3104
- h = Math.min(h, src.height - sy);
3226
+ w = Math.min(w, src.w - sx);
3227
+ h = Math.min(h, src.h - sy);
3105
3228
  if (x < 0) {
3106
3229
  sx -= x;
3107
3230
  w += x;
@@ -3112,15 +3235,15 @@ function blendPixelDataBinaryMask(target, src, binaryMask, opts) {
3112
3235
  h += y;
3113
3236
  y = 0;
3114
3237
  }
3115
- const actualW = Math.min(w, target.width - x);
3116
- const actualH = Math.min(h, target.height - y);
3238
+ const actualW = Math.min(w, target.w - x);
3239
+ const actualH = Math.min(h, target.h - y);
3117
3240
  if (actualW <= 0 || actualH <= 0) return false;
3118
3241
  const dx = x - targetX | 0;
3119
3242
  const dy = y - targetY | 0;
3120
- const dst32 = target.data32;
3121
- const src32 = src.data32;
3122
- const dw = target.width;
3123
- const sw = src.width;
3243
+ const dst32 = target.data;
3244
+ const src32 = src.data;
3245
+ const dw = target.w;
3246
+ const sw = src.w;
3124
3247
  const mPitch = binaryMask.w;
3125
3248
  const maskData = binaryMask.data;
3126
3249
  let dIdx = y * dw + x | 0;
@@ -3178,19 +3301,19 @@ function blendPixelDataBinaryMask(target, src, binaryMask, opts) {
3178
3301
  }
3179
3302
 
3180
3303
  // src/History/PixelMutator/mutatorBlendBinaryMask.ts
3181
- var defaults6 = {
3304
+ var defaults5 = {
3182
3305
  blendPixelDataBinaryMask
3183
3306
  };
3184
- var mutatorBlendBinaryMask = ((writer, deps = defaults6) => {
3307
+ var mutatorBlendBinaryMask = ((writer, deps = defaults5) => {
3185
3308
  const {
3186
- blendPixelDataBinaryMask: blendPixelDataBinaryMask2 = defaults6.blendPixelDataBinaryMask
3309
+ blendPixelDataBinaryMask: blendPixelDataBinaryMask2 = defaults5.blendPixelDataBinaryMask
3187
3310
  } = deps;
3188
3311
  return {
3189
3312
  blendBinaryMask(src, mask, opts) {
3190
3313
  const x = opts?.x ?? 0;
3191
3314
  const y = opts?.y ?? 0;
3192
- const w = opts?.w ?? src.width;
3193
- const h = opts?.h ?? src.height;
3315
+ const w = opts?.w ?? src.w;
3316
+ const h = opts?.h ?? src.h;
3194
3317
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
3195
3318
  return didChange(blendPixelDataBinaryMask2(writer.config.target, src, mask, opts));
3196
3319
  }
@@ -3201,8 +3324,8 @@ var mutatorBlendBinaryMask = ((writer, deps = defaults6) => {
3201
3324
  function blendColorPixelData(target, color, opts) {
3202
3325
  const targetX = opts?.x ?? 0;
3203
3326
  const targetY = opts?.y ?? 0;
3204
- const width = opts?.w ?? target.width;
3205
- const height = opts?.h ?? target.height;
3327
+ const width = opts?.w ?? target.w;
3328
+ const height = opts?.h ?? target.h;
3206
3329
  const globalAlpha = opts?.alpha ?? 255;
3207
3330
  const blendFn = opts?.blendFn ?? sourceOverPerfect;
3208
3331
  if (globalAlpha === 0) return false;
@@ -3221,8 +3344,8 @@ function blendColorPixelData(target, color, opts) {
3221
3344
  h += y;
3222
3345
  y = 0;
3223
3346
  }
3224
- const actualW = Math.min(w, target.width - x);
3225
- const actualH = Math.min(h, target.height - y);
3347
+ const actualW = Math.min(w, target.w - x);
3348
+ const actualH = Math.min(h, target.h - y);
3226
3349
  if (actualW <= 0 || actualH <= 0) return false;
3227
3350
  let finalSrcColor = color;
3228
3351
  if (globalAlpha < 255) {
@@ -3230,8 +3353,8 @@ function blendColorPixelData(target, color, opts) {
3230
3353
  if (a === 0 && !isOverwrite) return false;
3231
3354
  finalSrcColor = (color & 16777215 | a << 24) >>> 0;
3232
3355
  }
3233
- const dst32 = target.data32;
3234
- const dw = target.width;
3356
+ const dst32 = target.data;
3357
+ const dw = target.w;
3235
3358
  let dIdx = y * dw + x | 0;
3236
3359
  const dStride = dw - actualW | 0;
3237
3360
  let didChange = false;
@@ -3251,20 +3374,20 @@ function blendColorPixelData(target, color, opts) {
3251
3374
  }
3252
3375
 
3253
3376
  // src/History/PixelMutator/mutatorBlendColor.ts
3254
- var defaults7 = {
3377
+ var defaults6 = {
3255
3378
  blendColorPixelData
3256
3379
  };
3257
- var mutatorBlendColor = ((writer, deps = defaults7) => {
3380
+ var mutatorBlendColor = ((writer, deps = defaults6) => {
3258
3381
  const {
3259
- blendColorPixelData: blendColorPixelData2 = defaults7.blendColorPixelData
3382
+ blendColorPixelData: blendColorPixelData2 = defaults6.blendColorPixelData
3260
3383
  } = deps;
3261
3384
  return {
3262
3385
  blendColor(color, opts) {
3263
3386
  const target = writer.config.target;
3264
3387
  const x = opts?.x ?? 0;
3265
3388
  const y = opts?.y ?? 0;
3266
- const w = opts?.w ?? target.width;
3267
- const h = opts?.h ?? target.height;
3389
+ const w = opts?.w ?? target.w;
3390
+ const h = opts?.h ?? target.h;
3268
3391
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
3269
3392
  return didChange(blendColorPixelData2(target, color, opts));
3270
3393
  }
@@ -3298,13 +3421,13 @@ function blendColorPixelDataAlphaMask(target, color, mask, opts) {
3298
3421
  actualH += y;
3299
3422
  y = 0;
3300
3423
  }
3301
- actualW = Math.min(actualW, target.width - x);
3302
- actualH = Math.min(actualH, target.height - y);
3424
+ actualW = Math.min(actualW, target.w - x);
3425
+ actualH = Math.min(actualH, target.h - y);
3303
3426
  if (actualW <= 0 || actualH <= 0) return false;
3304
3427
  const dx = x - targetX | 0;
3305
3428
  const dy = y - targetY | 0;
3306
- const dst32 = target.data32;
3307
- const dw = target.width;
3429
+ const dst32 = target.data;
3430
+ const dw = target.w;
3308
3431
  const mPitch = mask.w;
3309
3432
  const maskData = mask.data;
3310
3433
  let dIdx = y * dw + x | 0;
@@ -3360,12 +3483,12 @@ function blendColorPixelDataAlphaMask(target, color, mask, opts) {
3360
3483
  }
3361
3484
 
3362
3485
  // src/History/PixelMutator/mutatorBlendColorPaintAlphaMask.ts
3363
- var defaults8 = {
3486
+ var defaults7 = {
3364
3487
  blendColorPixelDataAlphaMask
3365
3488
  };
3366
- var mutatorBlendColorPaintAlphaMask = ((writer, deps = defaults8) => {
3489
+ var mutatorBlendColorPaintAlphaMask = ((writer, deps = defaults7) => {
3367
3490
  const {
3368
- blendColorPixelDataAlphaMask: blendColorPixelDataAlphaMask2 = defaults8.blendColorPixelDataAlphaMask
3491
+ blendColorPixelDataAlphaMask: blendColorPixelDataAlphaMask2 = defaults7.blendColorPixelDataAlphaMask
3369
3492
  } = deps;
3370
3493
  const OPTS = {
3371
3494
  x: 0,
@@ -3412,8 +3535,8 @@ function blendColorPixelDataBinaryMask(target, color, mask, opts) {
3412
3535
  h += y;
3413
3536
  y = 0;
3414
3537
  }
3415
- const actualW = Math.min(w, target.width - x);
3416
- const actualH = Math.min(h, target.height - y);
3538
+ const actualW = Math.min(w, target.w - x);
3539
+ const actualH = Math.min(h, target.h - y);
3417
3540
  if (actualW <= 0 || actualH <= 0) return false;
3418
3541
  let baseColorWithGlobalAlpha = color;
3419
3542
  if (globalAlpha < 255) {
@@ -3423,8 +3546,8 @@ function blendColorPixelDataBinaryMask(target, color, mask, opts) {
3423
3546
  }
3424
3547
  const dx = x - targetX | 0;
3425
3548
  const dy = y - targetY | 0;
3426
- const dst32 = target.data32;
3427
- const dw = target.width;
3549
+ const dst32 = target.data;
3550
+ const dw = target.w;
3428
3551
  const mPitch = mask.w;
3429
3552
  const maskData = mask.data;
3430
3553
  let dIdx = y * dw + x | 0;
@@ -3456,12 +3579,12 @@ function blendColorPixelDataBinaryMask(target, color, mask, opts) {
3456
3579
  }
3457
3580
 
3458
3581
  // src/History/PixelMutator/mutatorBlendColorPaintBinaryMask.ts
3459
- var defaults9 = {
3582
+ var defaults8 = {
3460
3583
  blendColorPixelDataBinaryMask
3461
3584
  };
3462
- var mutatorBlendColorPaintBinaryMask = ((writer, deps = defaults9) => {
3585
+ var mutatorBlendColorPaintBinaryMask = ((writer, deps = defaults8) => {
3463
3586
  const {
3464
- blendColorPixelDataBinaryMask: blendColorPixelDataBinaryMask2 = defaults9.blendColorPixelDataBinaryMask
3587
+ blendColorPixelDataBinaryMask: blendColorPixelDataBinaryMask2 = defaults8.blendColorPixelDataBinaryMask
3465
3588
  } = deps;
3466
3589
  const OPTS = {
3467
3590
  x: 0,
@@ -3484,14 +3607,14 @@ var mutatorBlendColorPaintBinaryMask = ((writer, deps = defaults9) => {
3484
3607
  });
3485
3608
 
3486
3609
  // src/History/PixelMutator/mutatorBlendColorPaintMask.ts
3487
- var defaults10 = {
3610
+ var defaults9 = {
3488
3611
  blendColorPixelDataAlphaMask,
3489
3612
  blendColorPixelDataBinaryMask
3490
3613
  };
3491
- var mutatorBlendColorPaintMask = ((writer, deps = defaults10) => {
3614
+ var mutatorBlendColorPaintMask = ((writer, deps = defaults9) => {
3492
3615
  const {
3493
- blendColorPixelDataBinaryMask: blendColorPixelDataBinaryMask2 = defaults10.blendColorPixelDataBinaryMask,
3494
- blendColorPixelDataAlphaMask: blendColorPixelDataAlphaMask2 = defaults10.blendColorPixelDataAlphaMask
3616
+ blendColorPixelDataBinaryMask: blendColorPixelDataBinaryMask2 = defaults9.blendColorPixelDataBinaryMask,
3617
+ blendColorPixelDataAlphaMask: blendColorPixelDataAlphaMask2 = defaults9.blendColorPixelDataAlphaMask
3495
3618
  } = deps;
3496
3619
  const OPTS = {
3497
3620
  x: 0,
@@ -3517,39 +3640,13 @@ var mutatorBlendColorPaintMask = ((writer, deps = defaults10) => {
3517
3640
  };
3518
3641
  });
3519
3642
 
3520
- // src/History/PixelMutator/mutatorBlendMask.ts
3521
- var defaults11 = {
3522
- blendPixelDataAlphaMask,
3523
- blendPixelDataBinaryMask
3524
- };
3525
- var mutatorBlendMask = ((writer, deps = defaults11) => {
3526
- const {
3527
- blendPixelDataAlphaMask: blendPixelDataAlphaMask2 = defaults11.blendPixelDataAlphaMask,
3528
- blendPixelDataBinaryMask: blendPixelDataBinaryMask2 = defaults11.blendPixelDataBinaryMask
3529
- } = deps;
3530
- return {
3531
- blendMask(src, mask, opts) {
3532
- const x = opts?.x ?? 0;
3533
- const y = opts?.y ?? 0;
3534
- const w = opts?.w ?? src.width;
3535
- const h = opts?.h ?? src.height;
3536
- const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
3537
- if (mask.type === 1 /* BINARY */) {
3538
- return didChange(blendPixelDataBinaryMask2(writer.config.target, src, mask, opts));
3539
- } else {
3540
- return didChange(blendPixelDataAlphaMask2(writer.config.target, src, mask, opts));
3541
- }
3542
- }
3543
- };
3544
- });
3545
-
3546
3643
  // src/History/PixelMutator/mutatorBlendColorPaintRect.ts
3547
- var defaults12 = {
3644
+ var defaults10 = {
3548
3645
  blendColorPixelData
3549
3646
  };
3550
- var mutatorBlendColorPaintRect = ((writer, deps = defaults12) => {
3647
+ var mutatorBlendColorPaintRect = ((writer, deps = defaults10) => {
3551
3648
  const {
3552
- blendColorPixelData: blendColorPixelData2 = defaults12.blendColorPixelData
3649
+ blendColorPixelData: blendColorPixelData2 = defaults10.blendColorPixelData
3553
3650
  } = deps;
3554
3651
  const OPTS = {
3555
3652
  x: 0,
@@ -3576,16 +3673,42 @@ var mutatorBlendColorPaintRect = ((writer, deps = defaults12) => {
3576
3673
  };
3577
3674
  });
3578
3675
 
3676
+ // src/History/PixelMutator/mutatorBlendMask.ts
3677
+ var defaults11 = {
3678
+ blendPixelDataAlphaMask,
3679
+ blendPixelDataBinaryMask
3680
+ };
3681
+ var mutatorBlendMask = ((writer, deps = defaults11) => {
3682
+ const {
3683
+ blendPixelDataAlphaMask: blendPixelDataAlphaMask2 = defaults11.blendPixelDataAlphaMask,
3684
+ blendPixelDataBinaryMask: blendPixelDataBinaryMask2 = defaults11.blendPixelDataBinaryMask
3685
+ } = deps;
3686
+ return {
3687
+ blendMask(src, mask, opts) {
3688
+ const x = opts?.x ?? 0;
3689
+ const y = opts?.y ?? 0;
3690
+ const w = opts?.w ?? src.w;
3691
+ const h = opts?.h ?? src.h;
3692
+ const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
3693
+ if (mask.type === 1 /* BINARY */) {
3694
+ return didChange(blendPixelDataBinaryMask2(writer.config.target, src, mask, opts));
3695
+ } else {
3696
+ return didChange(blendPixelDataAlphaMask2(writer.config.target, src, mask, opts));
3697
+ }
3698
+ }
3699
+ };
3700
+ });
3701
+
3579
3702
  // src/PixelData/blendPixel.ts
3580
3703
  function blendPixel(target, x, y, color, alpha = 255, blendFn = sourceOverPerfect) {
3581
3704
  if (alpha === 0) return false;
3582
- let width = target.width;
3583
- let height = target.height;
3705
+ let width = target.w;
3706
+ let height = target.h;
3584
3707
  if (x < 0 || x >= width || y < 0 || y >= height) return false;
3585
3708
  let srcAlpha = color >>> 24;
3586
3709
  let isOverwrite = blendFn.isOverwrite;
3587
3710
  if (srcAlpha === 0 && !isOverwrite) return false;
3588
- let dst32 = target.data32;
3711
+ let dst32 = target.data;
3589
3712
  let index = y * width + x;
3590
3713
  let finalColor = color;
3591
3714
  if (alpha !== 255) {
@@ -3603,12 +3726,12 @@ function blendPixel(target, x, y, color, alpha = 255, blendFn = sourceOverPerfec
3603
3726
  }
3604
3727
 
3605
3728
  // src/History/PixelMutator/mutatorBlendPixel.ts
3606
- var defaults13 = {
3729
+ var defaults12 = {
3607
3730
  blendPixel
3608
3731
  };
3609
- var mutatorBlendPixel = ((writer, deps = defaults13) => {
3732
+ var mutatorBlendPixel = ((writer, deps = defaults12) => {
3610
3733
  const {
3611
- blendPixel: blendPixel2 = defaults13.blendPixel
3734
+ blendPixel: blendPixel2 = defaults12.blendPixel
3612
3735
  } = deps;
3613
3736
  return {
3614
3737
  blendPixel(x, y, color, alpha, blendFn) {
@@ -3618,20 +3741,107 @@ var mutatorBlendPixel = ((writer, deps = defaults13) => {
3618
3741
  };
3619
3742
  });
3620
3743
 
3744
+ // src/PixelData/blendPixelData.ts
3745
+ function blendPixelData(target, src, opts) {
3746
+ const targetX = opts?.x ?? 0;
3747
+ const targetY = opts?.y ?? 0;
3748
+ const sourceX = opts?.sx ?? 0;
3749
+ const sourceY = opts?.sy ?? 0;
3750
+ const width = opts?.w ?? src.w;
3751
+ const height = opts?.h ?? src.h;
3752
+ const globalAlpha = opts?.alpha ?? 255;
3753
+ const blendFn = opts?.blendFn ?? sourceOverPerfect;
3754
+ if (globalAlpha === 0) return false;
3755
+ let x = targetX;
3756
+ let y = targetY;
3757
+ let sx = sourceX;
3758
+ let sy = sourceY;
3759
+ let w = width;
3760
+ let h = height;
3761
+ if (sx < 0) {
3762
+ x -= sx;
3763
+ w += sx;
3764
+ sx = 0;
3765
+ }
3766
+ if (sy < 0) {
3767
+ y -= sy;
3768
+ h += sy;
3769
+ sy = 0;
3770
+ }
3771
+ w = Math.min(w, src.w - sx);
3772
+ h = Math.min(h, src.h - sy);
3773
+ if (x < 0) {
3774
+ sx -= x;
3775
+ w += x;
3776
+ x = 0;
3777
+ }
3778
+ if (y < 0) {
3779
+ sy -= y;
3780
+ h += y;
3781
+ y = 0;
3782
+ }
3783
+ const actualW = Math.min(w, target.w - x);
3784
+ const actualH = Math.min(h, target.h - y);
3785
+ if (actualW <= 0 || actualH <= 0) return false;
3786
+ const dst32 = target.data;
3787
+ const src32 = src.data;
3788
+ const dw = target.w;
3789
+ const sw = src.w;
3790
+ let dIdx = y * dw + x | 0;
3791
+ let sIdx = sy * sw + sx | 0;
3792
+ const dStride = dw - actualW | 0;
3793
+ const sStride = sw - actualW | 0;
3794
+ const isOpaque = globalAlpha === 255;
3795
+ const isOverwrite = blendFn.isOverwrite;
3796
+ let didChange = false;
3797
+ for (let iy = 0; iy < actualH; iy++) {
3798
+ for (let ix = 0; ix < actualW; ix++) {
3799
+ const srcCol = src32[sIdx];
3800
+ const srcAlpha = srcCol >>> 24;
3801
+ if (srcAlpha === 0 && !isOverwrite) {
3802
+ dIdx++;
3803
+ sIdx++;
3804
+ continue;
3805
+ }
3806
+ let finalCol = srcCol;
3807
+ if (!isOpaque) {
3808
+ const a = srcAlpha * globalAlpha + 128 >> 8;
3809
+ if (a === 0 && !isOverwrite) {
3810
+ dIdx++;
3811
+ sIdx++;
3812
+ continue;
3813
+ }
3814
+ finalCol = (srcCol & 16777215 | a << 24) >>> 0;
3815
+ }
3816
+ const current = dst32[dIdx];
3817
+ const next = blendFn(finalCol, dst32[dIdx]);
3818
+ if (current !== next) {
3819
+ dst32[dIdx] = next;
3820
+ didChange = true;
3821
+ }
3822
+ dIdx++;
3823
+ sIdx++;
3824
+ }
3825
+ dIdx += dStride;
3826
+ sIdx += sStride;
3827
+ }
3828
+ return didChange;
3829
+ }
3830
+
3621
3831
  // src/History/PixelMutator/mutatorBlendPixelData.ts
3622
- var defaults14 = {
3832
+ var defaults13 = {
3623
3833
  blendPixelData
3624
3834
  };
3625
- var mutatorBlendPixelData = ((writer, deps = defaults14) => {
3835
+ var mutatorBlendPixelData = ((writer, deps = defaults13) => {
3626
3836
  const {
3627
- blendPixelData: blendPixelData2 = defaults14.blendPixelData
3837
+ blendPixelData: blendPixelData2 = defaults13.blendPixelData
3628
3838
  } = deps;
3629
3839
  return {
3630
3840
  blendPixelData(src, opts) {
3631
3841
  const x = opts?.x ?? 0;
3632
3842
  const y = opts?.y ?? 0;
3633
- const w = opts?.w ?? src.width;
3634
- const h = opts?.h ?? src.height;
3843
+ const w = opts?.w ?? src.w;
3844
+ const h = opts?.h ?? src.h;
3635
3845
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
3636
3846
  return didChange(blendPixelData2(writer.config.target, src, opts));
3637
3847
  }
@@ -3648,8 +3858,8 @@ function fillPixelData(dst, color, _x, _y, _w, _h) {
3648
3858
  if (typeof _x === "object") {
3649
3859
  x = _x.x ?? 0;
3650
3860
  y = _x.y ?? 0;
3651
- w = _x.w ?? dst.width;
3652
- h = _x.h ?? dst.height;
3861
+ w = _x.w ?? dst.w;
3862
+ h = _x.h ?? dst.h;
3653
3863
  } else if (typeof _x === "number") {
3654
3864
  x = _x;
3655
3865
  y = _y;
@@ -3658,10 +3868,10 @@ function fillPixelData(dst, color, _x, _y, _w, _h) {
3658
3868
  } else {
3659
3869
  x = 0;
3660
3870
  y = 0;
3661
- w = dst.width;
3662
- h = dst.height;
3871
+ w = dst.w;
3872
+ h = dst.h;
3663
3873
  }
3664
- const clip = resolveRectClipping(x, y, w, h, dst.width, dst.height, SCRATCH_RECT);
3874
+ const clip = resolveRectClipping(x, y, w, h, dst.w, dst.h, SCRATCH_RECT);
3665
3875
  if (!clip.inBounds) return false;
3666
3876
  const {
3667
3877
  x: finalX,
@@ -3669,8 +3879,8 @@ function fillPixelData(dst, color, _x, _y, _w, _h) {
3669
3879
  w: actualW,
3670
3880
  h: actualH
3671
3881
  } = clip;
3672
- const dst32 = dst.data32;
3673
- const dw = dst.width;
3882
+ const dst32 = dst.data;
3883
+ const dw = dst.w;
3674
3884
  let hasChanged = false;
3675
3885
  for (let iy = 0; iy < actualH; iy++) {
3676
3886
  const rowOffset = (finalY + iy) * dw;
@@ -3687,20 +3897,20 @@ function fillPixelData(dst, color, _x, _y, _w, _h) {
3687
3897
  }
3688
3898
 
3689
3899
  // src/History/PixelMutator/mutatorClear.ts
3690
- var defaults15 = {
3900
+ var defaults14 = {
3691
3901
  fillPixelData
3692
3902
  };
3693
- var mutatorClear = ((writer, deps = defaults15) => {
3903
+ var mutatorClear = ((writer, deps = defaults14) => {
3694
3904
  const {
3695
- fillPixelData: fillPixelData2 = defaults15.fillPixelData
3905
+ fillPixelData: fillPixelData2 = defaults14.fillPixelData
3696
3906
  } = deps;
3697
3907
  return {
3698
3908
  clear(rect) {
3699
3909
  const target = writer.config.target;
3700
3910
  const x = rect?.x ?? 0;
3701
3911
  const y = rect?.y ?? 0;
3702
- const w = rect?.w ?? target.width;
3703
- const h = rect?.h ?? target.height;
3912
+ const w = rect?.w ?? target.w;
3913
+ const h = rect?.h ?? target.h;
3704
3914
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
3705
3915
  return didChange(fillPixelData2(target, 0, x, y, w, h));
3706
3916
  }
@@ -3708,24 +3918,24 @@ var mutatorClear = ((writer, deps = defaults15) => {
3708
3918
  });
3709
3919
 
3710
3920
  // src/History/PixelMutator/mutatorFill.ts
3711
- var defaults16 = {
3921
+ var defaults15 = {
3712
3922
  fillPixelData
3713
3923
  };
3714
- var mutatorFill = ((writer, deps = defaults16) => {
3924
+ var mutatorFill = ((writer, deps = defaults15) => {
3715
3925
  const {
3716
- fillPixelData: fillPixelData2 = defaults16.fillPixelData
3926
+ fillPixelData: fillPixelData2 = defaults15.fillPixelData
3717
3927
  } = deps;
3718
3928
  return {
3719
- fill(color, x = 0, y = 0, w = writer.config.target.width, h = writer.config.target.height) {
3929
+ fill(color, x = 0, y = 0, w = writer.config.target.w, h = writer.config.target.h) {
3720
3930
  const target = writer.config.target;
3721
3931
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
3722
3932
  return didChange(fillPixelData2(target, color, x, y, w, h));
3723
3933
  }
3724
3934
  };
3725
3935
  });
3726
- var mutatorFillRect = ((writer, deps = defaults16) => {
3936
+ var mutatorFillRect = ((writer, deps = defaults15) => {
3727
3937
  const {
3728
- fillPixelData: fillPixelData2 = defaults16.fillPixelData
3938
+ fillPixelData: fillPixelData2 = defaults15.fillPixelData
3729
3939
  } = deps;
3730
3940
  return {
3731
3941
  fillRect(color, rect) {
@@ -3741,7 +3951,7 @@ var SCRATCH_RECT2 = makeClippedRect();
3741
3951
  function fillPixelDataBinaryMask(target, color, mask, x = 0, y = 0) {
3742
3952
  const maskW = mask.w;
3743
3953
  const maskH = mask.h;
3744
- const clip = resolveRectClipping(x, y, maskW, maskH, target.width, target.height, SCRATCH_RECT2);
3954
+ const clip = resolveRectClipping(x, y, maskW, maskH, target.w, target.h, SCRATCH_RECT2);
3745
3955
  if (!clip.inBounds) return false;
3746
3956
  const {
3747
3957
  x: finalX,
@@ -3750,8 +3960,8 @@ function fillPixelDataBinaryMask(target, color, mask, x = 0, y = 0) {
3750
3960
  h: actualH
3751
3961
  } = clip;
3752
3962
  const maskData = mask.data;
3753
- const dst32 = target.data32;
3754
- const dw = target.width;
3963
+ const dst32 = target.data;
3964
+ const dw = target.w;
3755
3965
  let hasChanged = false;
3756
3966
  for (let iy = 0; iy < actualH; iy++) {
3757
3967
  const currentY = finalY + iy;
@@ -3775,12 +3985,12 @@ function fillPixelDataBinaryMask(target, color, mask, x = 0, y = 0) {
3775
3985
  }
3776
3986
 
3777
3987
  // src/History/PixelMutator/mutatorFillBinaryMask.ts
3778
- var defaults17 = {
3988
+ var defaults16 = {
3779
3989
  fillPixelDataBinaryMask
3780
3990
  };
3781
- var mutatorFillBinaryMask = ((writer, deps = defaults17) => {
3991
+ var mutatorFillBinaryMask = ((writer, deps = defaults16) => {
3782
3992
  const {
3783
- fillPixelDataBinaryMask: fillPixelDataBinaryMask2 = defaults17.fillPixelDataBinaryMask
3993
+ fillPixelDataBinaryMask: fillPixelDataBinaryMask2 = defaults16.fillPixelDataBinaryMask
3784
3994
  } = deps;
3785
3995
  return {
3786
3996
  fillBinaryMask(color, mask, x = 0, y = 0) {
@@ -3798,10 +4008,10 @@ function invertPixelData(target, opts) {
3798
4008
  const targetY = opts?.y ?? 0;
3799
4009
  const mx = opts?.mx ?? 0;
3800
4010
  const my = opts?.my ?? 0;
3801
- const width = opts?.w ?? target.width;
3802
- const height = opts?.h ?? target.height;
4011
+ const width = opts?.w ?? target.w;
4012
+ const height = opts?.h ?? target.h;
3803
4013
  const invertMask = opts?.invertMask ?? false;
3804
- const clip = resolveRectClipping(targetX, targetY, width, height, target.width, target.height, SCRATCH_RECT3);
4014
+ const clip = resolveRectClipping(targetX, targetY, width, height, target.w, target.h, SCRATCH_RECT3);
3805
4015
  if (!clip.inBounds) return false;
3806
4016
  const {
3807
4017
  x,
@@ -3809,8 +4019,8 @@ function invertPixelData(target, opts) {
3809
4019
  w: actualW,
3810
4020
  h: actualH
3811
4021
  } = clip;
3812
- const dst32 = target.data32;
3813
- const dw = target.width;
4022
+ const dst32 = target.data;
4023
+ const dw = target.w;
3814
4024
  const mPitch = mask?.w ?? width;
3815
4025
  const dx = x - targetX;
3816
4026
  const dy = y - targetY;
@@ -3846,20 +4056,20 @@ function invertPixelData(target, opts) {
3846
4056
  }
3847
4057
 
3848
4058
  // src/History/PixelMutator/mutatorInvert.ts
3849
- var defaults18 = {
4059
+ var defaults17 = {
3850
4060
  invertPixelData
3851
4061
  };
3852
- var mutatorInvert = ((writer, deps = defaults18) => {
4062
+ var mutatorInvert = ((writer, deps = defaults17) => {
3853
4063
  const {
3854
- invertPixelData: invertPixelData2 = defaults18.invertPixelData
4064
+ invertPixelData: invertPixelData2 = defaults17.invertPixelData
3855
4065
  } = deps;
3856
4066
  return {
3857
4067
  invert(opts) {
3858
4068
  const target = writer.config.target;
3859
4069
  const x = opts?.x ?? 0;
3860
4070
  const y = opts?.y ?? 0;
3861
- const w = opts?.w ?? target.width;
3862
- const h = opts?.h ?? target.height;
4071
+ const w = opts?.w ?? target.w;
4072
+ const h = opts?.h ?? target.h;
3863
4073
  const didChange = writer.accumulator.storeRegionBeforeState(x, y, w, h);
3864
4074
  return didChange(invertPixelData2(target, opts));
3865
4075
  }
@@ -3891,30 +4101,6 @@ function makeFullPixelMutator(writer) {
3891
4101
  };
3892
4102
  }
3893
4103
 
3894
- // src/ImageData/ImageDataLike.ts
3895
- function makeImageDataLike(width, height, data) {
3896
- const size = width * height * 4;
3897
- const buffer = data ? new Uint8ClampedArray(data.buffer, data.byteOffset, size) : new Uint8ClampedArray(size);
3898
- return {
3899
- width,
3900
- height,
3901
- data: buffer
3902
- };
3903
- }
3904
-
3905
- // src/ImageData/ReusableImageData.ts
3906
- function makeReusableImageData() {
3907
- let imageData = null;
3908
- return function getReusableImageData(width, height) {
3909
- if (imageData === null || imageData.width !== width || imageData.height !== height) {
3910
- imageData = new ImageData(width, height);
3911
- } else {
3912
- imageData.data.fill(0);
3913
- }
3914
- return imageData;
3915
- };
3916
- }
3917
-
3918
4104
  // src/ImageData/copyImageData.ts
3919
4105
  function copyImageData({
3920
4106
  data,
@@ -3935,6 +4121,17 @@ function copyImageDataLike({
3935
4121
  };
3936
4122
  }
3937
4123
 
4124
+ // src/ImageData/ImageDataLike.ts
4125
+ function makeImageDataLike(width, height, data) {
4126
+ const size = width * height * 4;
4127
+ const buffer = data ? new Uint8ClampedArray(data.buffer, data.byteOffset, size) : new Uint8ClampedArray(size);
4128
+ return {
4129
+ width,
4130
+ height,
4131
+ data: buffer
4132
+ };
4133
+ }
4134
+
3938
4135
  // src/ImageData/imageDataToAlphaMaskBuffer.ts
3939
4136
  function imageDataToAlphaMaskBuffer(imageData) {
3940
4137
  const {
@@ -3964,8 +4161,8 @@ function imageDataToDataUrl(imageData) {
3964
4161
  }
3965
4162
  imageDataToDataUrl.reset = get.reset;
3966
4163
 
3967
- // src/ImageData/imageDataToUInt32Array.ts
3968
- function imageDataToUInt32Array(imageData) {
4164
+ // src/ImageData/imageDataToUint32Array.ts
4165
+ function imageDataToUint32Array(imageData) {
3969
4166
  return new Uint32Array(
3970
4167
  imageData.data.buffer,
3971
4168
  imageData.data.byteOffset,
@@ -3986,43 +4183,29 @@ function invertImageData(imageData) {
3986
4183
  return imageData;
3987
4184
  }
3988
4185
 
3989
- // src/Internal/resample32.ts
3990
- var resample32Scratch = {
3991
- data: null,
3992
- width: 0,
3993
- height: 0
3994
- };
3995
- function resample32(srcData32, srcW, srcH, factor) {
3996
- const dstW = Math.max(1, srcW * factor | 0);
3997
- const dstH = Math.max(1, srcH * factor | 0);
3998
- const dstData = new Int32Array(dstW * dstH);
3999
- const scaleX = srcW / dstW;
4000
- const scaleY = srcH / dstH;
4001
- for (let y = 0; y < dstH; y++) {
4002
- const srcY = Math.min(srcH - 1, y * scaleY | 0);
4003
- const srcRowOffset = srcY * srcW;
4004
- const dstRowOffset = y * dstW;
4005
- for (let x = 0; x < dstW; x++) {
4006
- const srcX = Math.min(srcW - 1, x * scaleX | 0);
4007
- dstData[dstRowOffset + x] = srcData32[srcRowOffset + srcX];
4008
- }
4009
- }
4010
- resample32Scratch.data = dstData;
4011
- resample32Scratch.width = dstW;
4012
- resample32Scratch.height = dstH;
4013
- return resample32Scratch;
4014
- }
4015
-
4016
4186
  // src/ImageData/resampleImageData.ts
4017
4187
  function resampleImageData(source, factor) {
4018
4188
  const src32 = new Uint32Array(source.data.buffer);
4019
4189
  const {
4020
4190
  data,
4021
- width,
4022
- height
4023
- } = resample32(src32, source.width, source.height, factor);
4191
+ w,
4192
+ h
4193
+ } = resampleUint32Array(src32, source.width, source.height, factor);
4024
4194
  const uint8ClampedArray = new Uint8ClampedArray(data.buffer);
4025
- return new ImageData(uint8ClampedArray, width, height);
4195
+ return new ImageData(uint8ClampedArray, w, h);
4196
+ }
4197
+
4198
+ // src/ImageData/ReusableImageData.ts
4199
+ function makeReusableImageData() {
4200
+ let imageData = null;
4201
+ return function getReusableImageData(width, height) {
4202
+ if (imageData === null || imageData.width !== width || imageData.height !== height) {
4203
+ imageData = new ImageData(width, height);
4204
+ } else {
4205
+ imageData.data.fill(0);
4206
+ }
4207
+ return imageData;
4208
+ };
4026
4209
  }
4027
4210
 
4028
4211
  // src/ImageData/serialization.ts
@@ -4179,89 +4362,11 @@ function writeImageDataBuffer(target, data, _x, _y, _w, _h) {
4179
4362
  }
4180
4363
  }
4181
4364
 
4182
- // src/IndexedImage/IndexedImage.ts
4183
- var IndexedImage = class _IndexedImage {
4184
- /** The width of the image in pixels. */
4185
- width;
4186
- /** The height of the image in pixels. */
4187
- height;
4188
- /** Flat array of palette indices. Index = x + (y * width). */
4189
- data;
4190
- /** The palette of unique 32-bit colors (ABGR/RGBA packed) found in the image. */
4191
- palette;
4192
- /** The specific index in the palette reserved for fully transparent pixels. */
4193
- transparentPalletIndex;
4194
- /**
4195
- * @param width - Image width.
4196
- * @param height - Image height.
4197
- * @param data - The indexed pixel data.
4198
- * @param palette - The array of packed colors.
4199
- * @param transparentPalletIndex - The index representing alpha 0.
4200
- */
4201
- constructor(width, height, data, palette, transparentPalletIndex) {
4202
- this.width = width;
4203
- this.height = height;
4204
- this.data = data;
4205
- this.palette = palette;
4206
- this.transparentPalletIndex = transparentPalletIndex;
4207
- }
4208
- /**
4209
- * Creates an IndexedImage from standard browser ImageData.
4210
- * @param imageData - The source ImageData to convert.
4211
- * @returns A new IndexedImage instance.
4212
- */
4213
- static fromImageData(imageData) {
4214
- return _IndexedImage.fromRaw(imageData.data, imageData.width, imageData.height);
4215
- }
4216
- /**
4217
- * Creates an IndexedImage from a raw byte buffer and dimensions.
4218
- * Any pixel with an alpha channel of 0 is normalized to the transparent palette index.
4219
- * @param data - Raw RGBA byte data.
4220
- * @param width - Image width.
4221
- * @param height - Image height.
4222
- * @returns A new IndexedImage instance.
4223
- */
4224
- static fromRaw(data, width, height) {
4225
- const buffer = data.buffer;
4226
- const rawData = new Uint32Array(buffer);
4227
- const indexedData = new Int32Array(rawData.length);
4228
- const colorMap = /* @__PURE__ */ new Map();
4229
- const transparentColor = 0;
4230
- const transparentPalletIndex = 0;
4231
- colorMap.set(transparentColor, transparentPalletIndex);
4232
- for (let i = 0; i < rawData.length; i++) {
4233
- const pixel = rawData[i];
4234
- const alpha = pixel >>> 24 & 255;
4235
- const isTransparent = alpha === 0;
4236
- const colorKey = isTransparent ? transparentColor : pixel >>> 0;
4237
- let id = colorMap.get(colorKey);
4238
- if (id === void 0) {
4239
- id = colorMap.size;
4240
- colorMap.set(colorKey, id);
4241
- }
4242
- indexedData[i] = id;
4243
- }
4244
- const palette = Uint32Array.from(colorMap.keys());
4245
- return new _IndexedImage(width, height, indexedData, palette, transparentPalletIndex);
4246
- }
4247
- /**
4248
- * Retrieves the 32-bit packed color value at the given coordinates.
4249
- * @param x - X coordinate.
4250
- * @param y - Y coordinate.
4251
- * @returns The packed color from the palette.
4252
- */
4253
- getColorAt(x, y) {
4254
- const index = x + y * this.width;
4255
- const paletteIndex = this.data[index];
4256
- return this.palette[paletteIndex];
4257
- }
4258
- };
4259
-
4260
4365
  // src/IndexedImage/getIndexedImageColorCounts.ts
4261
4366
  function getIndexedImageColorCounts(indexedImage) {
4262
4367
  const data = indexedImage.data;
4263
4368
  const palette = indexedImage.palette;
4264
- const frequencies = new Int32Array(palette.length);
4369
+ const frequencies = new Uint32Array(palette.length);
4265
4370
  for (let i = 0; i < data.length; i++) {
4266
4371
  const colorIndex = data[i];
4267
4372
  frequencies[colorIndex]++;
@@ -4269,6 +4374,48 @@ function getIndexedImageColorCounts(indexedImage) {
4269
4374
  return frequencies;
4270
4375
  }
4271
4376
 
4377
+ // src/IndexedImage/IndexedImage.ts
4378
+ function makeIndexedImage(width, height, data, palette, transparentPalletIndex) {
4379
+ return {
4380
+ w: width,
4381
+ h: height,
4382
+ data,
4383
+ palette,
4384
+ transparentPalletIndex
4385
+ };
4386
+ }
4387
+ function makeIndexedImageFromImageDataRaw(data, width, height) {
4388
+ const buffer = data.buffer;
4389
+ const rawData = new Uint32Array(buffer);
4390
+ const indexedData = new Uint32Array(rawData.length);
4391
+ const colorMap = /* @__PURE__ */ new Map();
4392
+ const transparentColor = 0;
4393
+ const transparentPalletIndex = 0;
4394
+ colorMap.set(transparentColor, transparentPalletIndex);
4395
+ for (let i = 0; i < rawData.length; i++) {
4396
+ const pixel = rawData[i];
4397
+ const alpha = pixel >>> 24 & 255;
4398
+ const isTransparent = alpha === 0;
4399
+ const colorKey = isTransparent ? transparentColor : pixel >>> 0;
4400
+ let id = colorMap.get(colorKey);
4401
+ if (id === void 0) {
4402
+ id = colorMap.size;
4403
+ colorMap.set(colorKey, id);
4404
+ }
4405
+ indexedData[i] = id;
4406
+ }
4407
+ const palette = Uint32Array.from(colorMap.keys());
4408
+ return makeIndexedImage(width, height, indexedData, palette, transparentPalletIndex);
4409
+ }
4410
+ function makeIndexedImageFromImageData(imageData) {
4411
+ return makeIndexedImageFromImageDataRaw(imageData.data, imageData.width, imageData.height);
4412
+ }
4413
+ function getIndexedImageColor(target, x, y) {
4414
+ const index = x + y * target.w;
4415
+ const paletteIndex = target.data[index];
4416
+ return target.palette[paletteIndex];
4417
+ }
4418
+
4272
4419
  // src/IndexedImage/indexedImageToAverageColor.ts
4273
4420
  function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
4274
4421
  const {
@@ -4318,29 +4465,27 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
4318
4465
  // src/IndexedImage/indexedImageToImageData.ts
4319
4466
  function indexedImageToImageData(indexedImage) {
4320
4467
  const {
4321
- width,
4322
- height,
4468
+ w,
4469
+ h,
4323
4470
  data,
4324
4471
  palette
4325
4472
  } = indexedImage;
4326
- const result = new ImageData(width, height);
4473
+ const result = new ImageData(w, h);
4327
4474
  const data32 = new Uint32Array(result.data.buffer);
4328
4475
  for (let i = 0; i < data.length; i++) {
4329
4476
  const paletteIndex = data[i];
4330
- const color = palette[paletteIndex];
4331
- data32[i] = color;
4477
+ data32[i] = palette[paletteIndex];
4332
4478
  }
4333
4479
  return result;
4334
4480
  }
4335
4481
 
4336
4482
  // src/IndexedImage/resampleIndexedImage.ts
4337
4483
  function resampleIndexedImage(source, factor) {
4338
- const {
4339
- data,
4340
- width,
4341
- height
4342
- } = resample32(source.data, source.width, source.height, factor);
4343
- return new IndexedImage(width, height, data, source.palette, source.transparentPalletIndex);
4484
+ const output = {
4485
+ palette: source.palette,
4486
+ transparentPalletIndex: source.transparentPalletIndex
4487
+ };
4488
+ return resampleUint32Array(source.data, source.w, source.h, factor, output);
4344
4489
  }
4345
4490
 
4346
4491
  // src/Input/fileInputChangeToImageData.ts
@@ -4416,16 +4561,6 @@ function makeAlphaMask(w, h, data) {
4416
4561
  };
4417
4562
  }
4418
4563
 
4419
- // src/Mask/BinaryMask.ts
4420
- function makeBinaryMask(w, h, data) {
4421
- return {
4422
- type: 1 /* BINARY */,
4423
- data: data ?? new Uint8Array(w * h),
4424
- w,
4425
- h
4426
- };
4427
- }
4428
-
4429
4564
  // src/Mask/applyBinaryMaskToAlphaMask.ts
4430
4565
  function applyBinaryMaskToAlphaMask(alphaMaskDst, binaryMaskSrc, opts) {
4431
4566
  const targetX = opts?.x ?? 0;
@@ -4495,6 +4630,215 @@ function applyBinaryMaskToAlphaMask(alphaMaskDst, binaryMaskSrc, opts) {
4495
4630
  }
4496
4631
  }
4497
4632
 
4633
+ // src/Mask/BinaryMask.ts
4634
+ function makeBinaryMask(w, h, data) {
4635
+ return {
4636
+ type: 1 /* BINARY */,
4637
+ data: data ?? new Uint8Array(w * h),
4638
+ w,
4639
+ h
4640
+ };
4641
+ }
4642
+
4643
+ // src/Mask/BinaryMask/makeBinaryMaskFromAlphaMask.ts
4644
+ function makeBinaryMaskFromAlphaMask(mask, threshold, out) {
4645
+ const w = mask.w;
4646
+ const h = mask.h;
4647
+ const alphaData = mask.data;
4648
+ const area = w * h;
4649
+ const binaryData = new Uint8Array(area);
4650
+ for (let i = 0; i < area; i++) {
4651
+ if (alphaData[i] >= threshold) {
4652
+ binaryData[i] = 1;
4653
+ }
4654
+ }
4655
+ out = out ?? {
4656
+ type: 1 /* BINARY */
4657
+ };
4658
+ out.data = binaryData;
4659
+ out.w = w;
4660
+ out.h = h;
4661
+ return out;
4662
+ }
4663
+
4664
+ // src/Mask/BinaryMask/makeBinaryMaskOutline.ts
4665
+ function makeBinaryMaskOutline(mask, scale = 1) {
4666
+ const w = mask.w;
4667
+ const h = mask.h;
4668
+ const maskData = mask.data;
4669
+ const size = w * scale + 2;
4670
+ const outData = new Uint8Array(size * size);
4671
+ for (let iy = 0; iy < h; iy++) {
4672
+ for (let ix = 0; ix < w; ix++) {
4673
+ const i = iy * w + ix;
4674
+ if (maskData[i] === 0) continue;
4675
+ const lx = ix * scale + 1;
4676
+ const ly = iy * scale + 1;
4677
+ const top = iy === 0 || maskData[i - w] === 0;
4678
+ const bottom = iy === h - 1 || maskData[i + w] === 0;
4679
+ const left = ix === 0 || maskData[i - 1] === 0;
4680
+ const right = ix === w - 1 || maskData[i + 1] === 0;
4681
+ const topLeft = iy === 0 || ix === 0 || maskData[i - w - 1] === 0;
4682
+ const topRight = iy === 0 || ix === w - 1 || maskData[i - w + 1] === 0;
4683
+ const bottomLeft = iy === h - 1 || ix === 0 || maskData[i + w - 1] === 0;
4684
+ const bottomRight = iy === h - 1 || ix === w - 1 || maskData[i + w + 1] === 0;
4685
+ if (top) {
4686
+ for (let sx = 0; sx < scale; sx++) {
4687
+ const outIdx = (ly - 1) * size + (lx + sx);
4688
+ outData[outIdx] = 1;
4689
+ }
4690
+ }
4691
+ if (bottom) {
4692
+ for (let sx = 0; sx < scale; sx++) {
4693
+ const outIdx = (ly + scale) * size + (lx + sx);
4694
+ outData[outIdx] = 1;
4695
+ }
4696
+ }
4697
+ if (left) {
4698
+ for (let sy = 0; sy < scale; sy++) {
4699
+ const outIdx = (ly + sy) * size + (lx - 1);
4700
+ outData[outIdx] = 1;
4701
+ }
4702
+ }
4703
+ if (right) {
4704
+ for (let sy = 0; sy < scale; sy++) {
4705
+ const outIdx = (ly + sy) * size + (lx + scale);
4706
+ outData[outIdx] = 1;
4707
+ }
4708
+ }
4709
+ if (topLeft) {
4710
+ const outIdx = (ly - 1) * size + (lx - 1);
4711
+ outData[outIdx] = 1;
4712
+ }
4713
+ if (topRight) {
4714
+ const outIdx = (ly - 1) * size + (lx + scale);
4715
+ outData[outIdx] = 1;
4716
+ }
4717
+ if (bottomLeft) {
4718
+ const outIdx = (ly + scale) * size + (lx - 1);
4719
+ outData[outIdx] = 1;
4720
+ }
4721
+ if (bottomRight) {
4722
+ const outIdx = (ly + scale) * size + (lx + scale);
4723
+ outData[outIdx] = 1;
4724
+ }
4725
+ }
4726
+ }
4727
+ return {
4728
+ type: 1 /* BINARY */,
4729
+ w: size,
4730
+ h: size,
4731
+ data: outData
4732
+ };
4733
+ }
4734
+
4735
+ // src/Mask/BinaryMask/makeCircleBinaryMaskOutline.ts
4736
+ function makeCircleBinaryMaskOutline(size, scale) {
4737
+ const outSize = size * scale + 2;
4738
+ const outArea = outSize * outSize;
4739
+ const data = new Uint8Array(outArea);
4740
+ const radius = size / 2;
4741
+ const r2 = radius * radius;
4742
+ let prevMinX = -1;
4743
+ let prevMaxX = -1;
4744
+ let currMinX = -1;
4745
+ let currMaxX = -1;
4746
+ const initialDy = 0 - radius + 0.5;
4747
+ const initialDy2 = initialDy * initialDy;
4748
+ if (initialDy2 <= r2) {
4749
+ const dx = Math.sqrt(r2 - initialDy2);
4750
+ currMinX = Math.ceil(radius - 0.5 - dx);
4751
+ currMaxX = Math.floor(radius - 0.5 + dx);
4752
+ }
4753
+ for (let iy = 0; iy < size; iy++) {
4754
+ let nextMinX = -1;
4755
+ let nextMaxX = -1;
4756
+ if (iy + 1 < size) {
4757
+ const ny = iy + 1 - radius + 0.5;
4758
+ const ny2 = ny * ny;
4759
+ if (ny2 <= r2) {
4760
+ const dx = Math.sqrt(r2 - ny2);
4761
+ nextMinX = Math.ceil(radius - 0.5 - dx);
4762
+ nextMaxX = Math.floor(radius - 0.5 + dx);
4763
+ }
4764
+ }
4765
+ if (currMinX !== -1) {
4766
+ for (let ix = currMinX; ix <= currMaxX; ix++) {
4767
+ const sx = ix * scale + 1;
4768
+ const sy = iy * scale + 1;
4769
+ const isTop = prevMinX === -1 || ix < prevMinX || ix > prevMaxX;
4770
+ const isBottom = nextMinX === -1 || ix < nextMinX || ix > nextMaxX;
4771
+ const isLeft = ix === currMinX;
4772
+ const isRight = ix === currMaxX;
4773
+ if (isTop) {
4774
+ const leftOut = prevMinX === -1 || ix - 1 < prevMinX || ix - 1 > prevMaxX;
4775
+ const rightOut = prevMinX === -1 || ix + 1 < prevMinX || ix + 1 > prevMaxX;
4776
+ const startX = leftOut ? sx - 1 : sx;
4777
+ const endX = rightOut ? sx + scale : sx + scale - 1;
4778
+ for (let x = startX; x <= endX; x++) {
4779
+ const index = (sy - 1) * outSize + x;
4780
+ data[index] = 1;
4781
+ }
4782
+ }
4783
+ if (isBottom) {
4784
+ const leftOut = nextMinX === -1 || ix - 1 < nextMinX || ix - 1 > nextMaxX;
4785
+ const rightOut = nextMinX === -1 || ix + 1 < nextMinX || ix + 1 > nextMaxX;
4786
+ const startX = leftOut ? sx - 1 : sx;
4787
+ const endX = rightOut ? sx + scale : sx + scale - 1;
4788
+ for (let x = startX; x <= endX; x++) {
4789
+ const index = (sy + scale) * outSize + x;
4790
+ data[index] = 1;
4791
+ }
4792
+ }
4793
+ if (isLeft) {
4794
+ for (let y = sy; y < sy + scale; y++) {
4795
+ const index = y * outSize + (sx - 1);
4796
+ data[index] = 1;
4797
+ }
4798
+ }
4799
+ if (isRight) {
4800
+ for (let y = sy; y < sy + scale; y++) {
4801
+ const index = y * outSize + (sx + scale);
4802
+ data[index] = 1;
4803
+ }
4804
+ }
4805
+ }
4806
+ }
4807
+ prevMinX = currMinX;
4808
+ prevMaxX = currMaxX;
4809
+ currMinX = nextMinX;
4810
+ currMaxX = nextMaxX;
4811
+ }
4812
+ return {
4813
+ type: 1 /* BINARY */,
4814
+ w: outSize,
4815
+ h: outSize,
4816
+ data
4817
+ };
4818
+ }
4819
+
4820
+ // src/Mask/BinaryMask/makeRectBinaryMaskOutline.ts
4821
+ function makeRectBinaryMaskOutline(w, h, scale = 1) {
4822
+ const rw = w * scale;
4823
+ const rh = h * scale;
4824
+ const outW = rw + 2;
4825
+ const outH = rh + 2;
4826
+ const outData = new Uint8Array(outW * outH);
4827
+ outData.fill(1, 0, outW);
4828
+ outData.fill(1, (outH - 1) * outW, outH * outW);
4829
+ for (let iy = 1; iy < outH - 1; iy++) {
4830
+ const rowStart = iy * outW;
4831
+ outData[rowStart] = 1;
4832
+ outData[rowStart + outW - 1] = 1;
4833
+ }
4834
+ return {
4835
+ type: 1 /* BINARY */,
4836
+ w: outW,
4837
+ h: outH,
4838
+ data: outData
4839
+ };
4840
+ }
4841
+
4498
4842
  // src/Mask/copyMask.ts
4499
4843
  function copyMask(src) {
4500
4844
  return {
@@ -4602,7 +4946,8 @@ function mergeAlphaMasks(dst, src, opts) {
4602
4946
  } else if (globalAlpha === 255) {
4603
4947
  weight = effectiveM;
4604
4948
  } else {
4605
- weight = effectiveM * globalAlpha + 128 >> 8;
4949
+ const t = effectiveM * globalAlpha + 128;
4950
+ weight = t + (t >> 8) >> 8;
4606
4951
  }
4607
4952
  if (weight !== 255) {
4608
4953
  if (weight === 0) {
@@ -4612,7 +4957,8 @@ function mergeAlphaMasks(dst, src, opts) {
4612
4957
  if (da === 255) {
4613
4958
  dstData[dIdx] = weight;
4614
4959
  } else if (da !== 0) {
4615
- dstData[dIdx] = da * weight + 128 >> 8;
4960
+ const t = da * weight + 128;
4961
+ dstData[dIdx] = t + (t >> 8) >> 8;
4616
4962
  }
4617
4963
  }
4618
4964
  }
@@ -4857,189 +5203,1007 @@ function pushPiece(dest, r, x, y, w, h) {
4857
5203
  });
4858
5204
  }
4859
5205
 
4860
- // src/Paint/PaintBufferCanvasRenderer.ts
4861
- function makePaintBufferCanvasRenderer(paintBuffer, offscreenCanvasClass = OffscreenCanvas) {
4862
- const config = paintBuffer.config;
4863
- const tileSize = config.tileSize;
4864
- const tileShift = config.tileShift;
4865
- const lookup = paintBuffer.lookup;
4866
- const canvas = new offscreenCanvasClass(tileSize, tileSize);
4867
- const ctx = canvas.getContext("2d");
4868
- if (!ctx) throw new Error(CANVAS_CTX_FAILED);
4869
- ctx.imageSmoothingEnabled = false;
4870
- return function drawPaintBuffer(targetCtx, alpha = 255, compOperation = "source-over") {
4871
- targetCtx.globalAlpha = alpha / 255;
4872
- targetCtx.globalCompositeOperation = compOperation;
4873
- for (let i = 0; i < lookup.length; i++) {
4874
- const tile = lookup[i];
4875
- if (tile) {
4876
- const dx = tile.tx << tileShift;
4877
- const dy = tile.ty << tileShift;
4878
- ctx.putImageData(tile.imageData, 0, 0);
4879
- targetCtx.drawImage(canvas, dx, dy);
4880
- }
4881
- }
4882
- targetCtx.globalAlpha = 1;
4883
- targetCtx.globalCompositeOperation = "source-over";
4884
- };
4885
- }
5206
+ // src/Paint/_paint-types.ts
5207
+ var PaintMaskOutline = /* @__PURE__ */ ((PaintMaskOutline2) => {
5208
+ PaintMaskOutline2[PaintMaskOutline2["MASKED"] = 0] = "MASKED";
5209
+ PaintMaskOutline2[PaintMaskOutline2["CIRCLE"] = 1] = "CIRCLE";
5210
+ PaintMaskOutline2[PaintMaskOutline2["RECT"] = 2] = "RECT";
5211
+ return PaintMaskOutline2;
5212
+ })(PaintMaskOutline || {});
4886
5213
 
4887
- // src/Paint/makeCirclePaintAlphaMask.ts
4888
- function makeCirclePaintAlphaMask(size, fallOff = (d) => d) {
4889
- const area = size * size;
4890
- const data = new Uint8Array(area);
4891
- const radius = size / 2;
4892
- const invR = 1 / radius;
4893
- const centerOffset = -Math.ceil(radius - 0.5);
4894
- for (let y = 0; y < size; y++) {
4895
- const rowOffset = y * size;
4896
- const dy = y - radius + 0.5;
4897
- const dy2 = dy * dy;
4898
- for (let x = 0; x < size; x++) {
4899
- const dx = x - radius + 0.5;
4900
- const distSqr = dx * dx + dy2;
4901
- if (distSqr <= radius * radius) {
4902
- const dist = Math.sqrt(distSqr) * invR;
4903
- const strength = fallOff(1 - dist);
4904
- if (strength > 0) {
4905
- const intensity = strength * 255 | 0;
4906
- data[rowOffset + x] = Math.max(0, Math.min(255, intensity));
4907
- }
4908
- }
4909
- }
4910
- }
4911
- return {
4912
- type: 0 /* ALPHA */,
4913
- data,
4914
- w: size,
4915
- h: size,
4916
- centerOffsetX: centerOffset,
4917
- centerOffsetY: centerOffset
5214
+ // src/Rect/trimRectBounds.ts
5215
+ function trimRectBounds(x, y, w, h, targetWidth, targetHeight, out) {
5216
+ const res = out ?? {
5217
+ x: 0,
5218
+ y: 0,
5219
+ w: 0,
5220
+ h: 0
4918
5221
  };
5222
+ const left = Math.max(0, x);
5223
+ const top = Math.max(0, y);
5224
+ const right = Math.min(targetWidth, x + w);
5225
+ const bottom = Math.min(targetHeight, y + h);
5226
+ res.x = left;
5227
+ res.y = top;
5228
+ res.w = Math.max(0, right - left);
5229
+ res.h = Math.max(0, bottom - top);
5230
+ return res;
4919
5231
  }
4920
5232
 
4921
- // src/Paint/makeCirclePaintBinaryMask.ts
4922
- function makeCirclePaintBinaryMask(size) {
4923
- const area = size * size;
4924
- const data = new Uint8Array(area);
4925
- const radius = size / 2;
4926
- const centerOffset = -Math.ceil(radius - 0.5);
4927
- for (let y = 0; y < size; y++) {
4928
- for (let x = 0; x < size; x++) {
4929
- const dx = x - radius + 0.5;
4930
- const dy = y - radius + 0.5;
4931
- const distSqr = dx * dx + dy * dy;
4932
- if (distSqr <= radius * radius) {
4933
- data[y * size + x] = 1;
4934
- }
5233
+ // src/Paint/eachTileInBounds.ts
5234
+ function eachTileInBounds(config, lookup, tilePool, bounds, callback) {
5235
+ const {
5236
+ tileShift,
5237
+ targetColumns,
5238
+ targetRows,
5239
+ tileSize
5240
+ } = config;
5241
+ const x1 = Math.max(0, bounds.x >> tileShift);
5242
+ const y1 = Math.max(0, bounds.y >> tileShift);
5243
+ const x2 = Math.min(targetColumns - 1, bounds.x + bounds.w - 1 >> tileShift);
5244
+ const y2 = Math.min(targetRows - 1, bounds.y + bounds.h - 1 >> tileShift);
5245
+ if (x1 > x2 || y1 > y2) return;
5246
+ for (let ty = y1; ty <= y2; ty++) {
5247
+ const rowOffset = ty * targetColumns;
5248
+ const tileTop = ty << tileShift;
5249
+ for (let tx = x1; tx <= x2; tx++) {
5250
+ const id = rowOffset + tx;
5251
+ const tile = lookup[id] ?? (lookup[id] = tilePool.getTile(id, tx, ty));
5252
+ const tileLeft = tx << tileShift;
5253
+ const startX = bounds.x > tileLeft ? bounds.x : tileLeft;
5254
+ const startY = bounds.y > tileTop ? bounds.y : tileTop;
5255
+ const maskEndX = bounds.x + bounds.w;
5256
+ const tileEndX = tileLeft + tileSize;
5257
+ const endX = maskEndX < tileEndX ? maskEndX : tileEndX;
5258
+ const maskEndY = bounds.y + bounds.h;
5259
+ const tileEndY = tileTop + tileSize;
5260
+ const endY = maskEndY < tileEndY ? maskEndY : tileEndY;
5261
+ callback(tile, startX, startY, endX - startX, endY - startY);
4935
5262
  }
4936
5263
  }
4937
- return {
4938
- type: 1 /* BINARY */,
4939
- data,
4940
- w: size,
4941
- h: size,
4942
- centerOffsetX: centerOffset,
4943
- centerOffsetY: centerOffset
4944
- };
4945
- }
4946
-
4947
- // src/Paint/makePaintMask.ts
4948
- function makePaintBinaryMask(mask) {
4949
- return {
4950
- type: 1 /* BINARY */,
4951
- data: mask.data,
4952
- w: mask.w,
4953
- h: mask.h,
4954
- centerOffsetX: -(mask.w >> 1),
4955
- centerOffsetY: -(mask.h >> 1)
4956
- };
4957
- }
4958
- function makePaintAlphaMask(mask) {
4959
- return {
4960
- type: 0 /* ALPHA */,
4961
- data: mask.data,
4962
- w: mask.w,
4963
- h: mask.h,
4964
- centerOffsetX: -(mask.w >> 1),
4965
- centerOffsetY: -(mask.h >> 1)
4966
- };
4967
5264
  }
4968
5265
 
4969
- // src/Paint/makeRectFalloffPaintAlphaMask.ts
4970
- function makeRectFalloffPaintAlphaMask(width, height, fallOff = (d) => d) {
4971
- const fPx = Math.floor(width / 2);
4972
- const fPy = Math.floor(height / 2);
4973
- const invHalfW = 2 / width;
4974
- const invHalfH = 2 / height;
4975
- const offX = width % 2 === 0 ? 0.5 : 0;
4976
- const offY = height % 2 === 0 ? 0.5 : 0;
4977
- const area = width * height;
4978
- const data = new Uint8Array(area);
4979
- for (let y = 0; y < height; y++) {
4980
- const dy = Math.abs(y - fPy + offY) * invHalfH;
4981
- const rowOffset = y * width;
4982
- for (let x = 0; x < width; x++) {
4983
- const dx = Math.abs(x - fPx + offX) * invHalfW;
4984
- const dist = dx > dy ? dx : dy;
4985
- const strength = fallOff(1 - dist);
4986
- if (strength > 0) {
4987
- const intensity = strength * 255 | 0;
4988
- data[rowOffset + x] = Math.max(0, Math.min(255, intensity));
4989
- }
4990
- }
5266
+ // src/Paint/AlphaMaskPaintBuffer.ts
5267
+ var AlphaMaskPaintBuffer = class {
5268
+ constructor(config, tilePool) {
5269
+ this.config = config;
5270
+ this.tilePool = tilePool;
5271
+ this.lookup = [];
4991
5272
  }
4992
- return {
4993
- type: 0 /* ALPHA */,
4994
- data,
4995
- w: width,
4996
- h: height,
4997
- centerOffsetX: -(width >> 1),
4998
- centerOffsetY: -(height >> 1)
5273
+ lookup;
5274
+ scratchBounds = {
5275
+ x: 0,
5276
+ y: 0,
5277
+ w: 0,
5278
+ h: 0
4999
5279
  };
5000
- }
5001
-
5002
- // src/PixelData/PixelData.ts
5003
- var PixelData = class {
5004
- data32;
5005
- imageData;
5006
- width;
5007
- height;
5008
- constructor(imageData) {
5009
- this.data32 = imageDataToUInt32Array(imageData);
5010
- this.imageData = imageData;
5011
- this.width = imageData.width;
5012
- this.height = imageData.height;
5013
- }
5014
- set(imageData) {
5015
- ;
5016
- this.imageData = imageData;
5017
- this.data32 = imageDataToUInt32Array(imageData);
5018
- this.width = imageData.width;
5019
- this.height = imageData.height;
5020
- }
5021
- };
5022
-
5023
- // src/PixelData/applyMaskToPixelData.ts
5024
- function applyMaskToPixelData(dst, mask, opts) {
5025
- if (mask.type === 1 /* BINARY */) {
5026
- return applyBinaryMaskToPixelData(dst, mask, opts);
5027
- } else {
5028
- return applyAlphaMaskToPixelData(dst, mask, opts);
5029
- }
5030
- }
5031
-
5032
- // src/PixelData/blendColorPixelDataMask.ts
5033
- function blendColorPixelDataMask(dst, color, mask, opts) {
5034
- if (mask.type === 1 /* BINARY */) {
5035
- return blendColorPixelDataBinaryMask(dst, color, mask, opts);
5036
- } else {
5280
+ forEachLinePointFn = forEachLinePoint;
5281
+ trimRectBoundsFn = trimRectBounds;
5282
+ eachTileInBoundsFn = eachTileInBounds;
5283
+ paintAlphaMask(brush, x0, y0, x1 = x0, y1 = y0) {
5284
+ const scratch = this.scratchBounds;
5285
+ const lookup = this.lookup;
5286
+ const tilePool = this.tilePool;
5287
+ const config = this.config;
5288
+ const tileShift = config.tileShift;
5289
+ const tileMask = config.tileMask;
5290
+ const target = config.target;
5291
+ const {
5292
+ w: bW,
5293
+ h: bH,
5294
+ data: bD,
5295
+ centerOffsetX,
5296
+ centerOffsetY
5297
+ } = brush;
5298
+ let changed = false;
5299
+ const eachTileInBoundsFn = this.eachTileInBoundsFn;
5300
+ const trimRectBoundsFn = this.trimRectBoundsFn;
5301
+ this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5302
+ const topLeftX = Math.floor(px + centerOffsetX);
5303
+ const topLeftY = Math.floor(py + centerOffsetY);
5304
+ trimRectBoundsFn(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5305
+ if (scratch.w <= 0 || scratch.h <= 0) return;
5306
+ eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5307
+ const data = tile.data;
5308
+ let tileChanged = false;
5309
+ for (let i = 0; i < bH_t; i++) {
5310
+ const canvasY = bY + i;
5311
+ const bOff = (canvasY - topLeftY) * bW;
5312
+ const tOff = (canvasY & tileMask) << tileShift;
5313
+ const dS = tOff + (bX & tileMask);
5314
+ for (let j = 0; j < bW_t; j++) {
5315
+ const canvasX = bX + j;
5316
+ const brushA = bD[bOff + (canvasX - topLeftX)];
5317
+ if (brushA === 0) continue;
5318
+ const idx = dS + j;
5319
+ if (brushA > data[idx]) {
5320
+ data[idx] = brushA;
5321
+ tileChanged = true;
5322
+ }
5323
+ }
5324
+ }
5325
+ if (tileChanged) changed = true;
5326
+ });
5327
+ });
5328
+ return changed;
5329
+ }
5330
+ paintBinaryMask(brush, alpha, x0, y0, x1 = x0, y1 = y0) {
5331
+ if (alpha === 0) return false;
5332
+ const scratch = this.scratchBounds;
5333
+ const lookup = this.lookup;
5334
+ const tilePool = this.tilePool;
5335
+ const config = this.config;
5336
+ const tileShift = config.tileShift;
5337
+ const tileMask = config.tileMask;
5338
+ const target = config.target;
5339
+ const {
5340
+ w: bW,
5341
+ h: bH,
5342
+ data: bD,
5343
+ centerOffsetX,
5344
+ centerOffsetY
5345
+ } = brush;
5346
+ let changed = false;
5347
+ const trimRectBoundsFn = this.trimRectBoundsFn;
5348
+ const eachTileInBoundsFn = this.eachTileInBoundsFn;
5349
+ this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5350
+ const topLeftX = Math.floor(px + centerOffsetX);
5351
+ const topLeftY = Math.floor(py + centerOffsetY);
5352
+ trimRectBoundsFn(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5353
+ if (scratch.w <= 0 || scratch.h <= 0) return;
5354
+ eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5355
+ const data = tile.data;
5356
+ let tileChanged = false;
5357
+ for (let i = 0; i < bH_t; i++) {
5358
+ const canvasY = bY + i;
5359
+ const bOff = (canvasY - topLeftY) * bW;
5360
+ const tOff = (canvasY & tileMask) << tileShift;
5361
+ const dS = tOff + (bX & tileMask);
5362
+ for (let j = 0; j < bW_t; j++) {
5363
+ const canvasX = bX + j;
5364
+ if (bD[bOff + (canvasX - topLeftX)]) {
5365
+ const idx = dS + j;
5366
+ if (data[idx] < alpha) {
5367
+ data[idx] = alpha;
5368
+ tileChanged = true;
5369
+ }
5370
+ }
5371
+ }
5372
+ }
5373
+ if (tileChanged) changed = true;
5374
+ });
5375
+ });
5376
+ return changed;
5377
+ }
5378
+ paintRect(alpha, brushWidth, brushHeight, x0, y0, x1 = x0, y1 = y0) {
5379
+ const scratch = this.scratchBounds;
5380
+ const lookup = this.lookup;
5381
+ const tilePool = this.tilePool;
5382
+ const config = this.config;
5383
+ const tileShift = config.tileShift;
5384
+ const tileMask = config.tileMask;
5385
+ const target = config.target;
5386
+ const centerOffsetX = -(brushWidth - 1 >> 1);
5387
+ const centerOffsetY = -(brushHeight - 1 >> 1);
5388
+ const trimRectBoundsFn = this.trimRectBoundsFn;
5389
+ const eachTileInBoundsFn = this.eachTileInBoundsFn;
5390
+ let changed = false;
5391
+ this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5392
+ const topLeftX = Math.floor(px + centerOffsetX);
5393
+ const topLeftY = Math.floor(py + centerOffsetY);
5394
+ trimRectBoundsFn(topLeftX, topLeftY, brushWidth, brushHeight, target.w, target.h, scratch);
5395
+ if (scratch.w <= 0 || scratch.h <= 0) return;
5396
+ eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5397
+ const data = tile.data;
5398
+ let tileChanged = false;
5399
+ for (let i = 0; i < bH_t; i++) {
5400
+ const canvasY = bY + i;
5401
+ const tOff = (canvasY & tileMask) << tileShift;
5402
+ const dS = tOff + (bX & tileMask);
5403
+ for (let j = 0; j < bW_t; j++) {
5404
+ const idx = dS + j;
5405
+ if (alpha > data[idx]) {
5406
+ data[idx] = alpha;
5407
+ tileChanged = true;
5408
+ }
5409
+ }
5410
+ }
5411
+ if (tileChanged) {
5412
+ changed = true;
5413
+ }
5414
+ });
5415
+ });
5416
+ return changed;
5417
+ }
5418
+ clear() {
5419
+ this.tilePool.releaseTiles(this.lookup);
5420
+ }
5421
+ };
5422
+
5423
+ // src/Paint/BinaryMaskPaintBuffer.ts
5424
+ var BinaryMaskPaintBuffer = class {
5425
+ constructor(config, tilePool) {
5426
+ this.config = config;
5427
+ this.tilePool = tilePool;
5428
+ this.lookup = [];
5429
+ }
5430
+ lookup;
5431
+ scratchBounds = {
5432
+ x: 0,
5433
+ y: 0,
5434
+ w: 0,
5435
+ h: 0
5436
+ };
5437
+ forEachLinePointFn = forEachLinePoint;
5438
+ trimRectBoundsFn = trimRectBounds;
5439
+ eachTileInBoundsFn = eachTileInBounds;
5440
+ paintBinaryMask(brush, x0, y0, x1 = x0, y1 = y0) {
5441
+ const scratch = this.scratchBounds;
5442
+ const lookup = this.lookup;
5443
+ const tilePool = this.tilePool;
5444
+ const config = this.config;
5445
+ const tileShift = config.tileShift;
5446
+ const tileMask = config.tileMask;
5447
+ const target = config.target;
5448
+ const {
5449
+ w: bW,
5450
+ h: bH,
5451
+ data: bD,
5452
+ centerOffsetX,
5453
+ centerOffsetY
5454
+ } = brush;
5455
+ let changed = false;
5456
+ const trimRectBoundsFn = this.trimRectBoundsFn;
5457
+ const eachTileInBoundsFn = this.eachTileInBoundsFn;
5458
+ this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5459
+ const topLeftX = Math.floor(px + centerOffsetX);
5460
+ const topLeftY = Math.floor(py + centerOffsetY);
5461
+ trimRectBoundsFn(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5462
+ if (scratch.w <= 0 || scratch.h <= 0) return;
5463
+ eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5464
+ const data = tile.data;
5465
+ let tileChanged = false;
5466
+ for (let i = 0; i < bH_t; i++) {
5467
+ const canvasY = bY + i;
5468
+ const bOff = (canvasY - topLeftY) * bW;
5469
+ const tOff = (canvasY & tileMask) << tileShift;
5470
+ const dS = tOff + (bX & tileMask);
5471
+ for (let j = 0; j < bW_t; j++) {
5472
+ const canvasX = bX + j;
5473
+ if (bD[bOff + (canvasX - topLeftX)]) {
5474
+ const idx = dS + j;
5475
+ if (data[idx] === 0) {
5476
+ data[idx] = 1;
5477
+ tileChanged = true;
5478
+ }
5479
+ }
5480
+ }
5481
+ }
5482
+ if (tileChanged) changed = true;
5483
+ });
5484
+ });
5485
+ return changed;
5486
+ }
5487
+ paintRect(brushWidth, brushHeight, x0, y0, x1 = x0, y1 = y0) {
5488
+ const scratch = this.scratchBounds;
5489
+ const lookup = this.lookup;
5490
+ const tilePool = this.tilePool;
5491
+ const config = this.config;
5492
+ const tileShift = config.tileShift;
5493
+ const tileMask = config.tileMask;
5494
+ const target = config.target;
5495
+ const centerOffsetX = -(brushWidth - 1 >> 1);
5496
+ const centerOffsetY = -(brushHeight - 1 >> 1);
5497
+ const trimRectBoundsFn = this.trimRectBoundsFn;
5498
+ const eachTileInBoundsFn = this.eachTileInBoundsFn;
5499
+ let changed = false;
5500
+ this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5501
+ const topLeftX = Math.floor(px + centerOffsetX);
5502
+ const topLeftY = Math.floor(py + centerOffsetY);
5503
+ trimRectBoundsFn(topLeftX, topLeftY, brushWidth, brushHeight, target.w, target.h, scratch);
5504
+ if (scratch.w <= 0 || scratch.h <= 0) return;
5505
+ eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5506
+ const data = tile.data;
5507
+ let tileChanged = false;
5508
+ for (let i = 0; i < bH_t; i++) {
5509
+ const canvasY = bY + i;
5510
+ const tOff = (canvasY & tileMask) << tileShift;
5511
+ const dS = tOff + (bX & tileMask);
5512
+ for (let j = 0; j < bW_t; j++) {
5513
+ const idx = dS + j;
5514
+ if (data[idx] === 0) {
5515
+ data[idx] = 1;
5516
+ tileChanged = true;
5517
+ }
5518
+ }
5519
+ }
5520
+ if (tileChanged) {
5521
+ changed = true;
5522
+ }
5523
+ });
5524
+ });
5525
+ return changed;
5526
+ }
5527
+ clear() {
5528
+ this.tilePool.releaseTiles(this.lookup);
5529
+ }
5530
+ };
5531
+
5532
+ // src/Paint/ColorPaintBuffer.ts
5533
+ var ColorPaintBuffer = class {
5534
+ constructor(config, tilePool) {
5535
+ this.config = config;
5536
+ this.tilePool = tilePool;
5537
+ this.lookup = [];
5538
+ }
5539
+ lookup;
5540
+ scratchBounds = {
5541
+ x: 0,
5542
+ y: 0,
5543
+ w: 0,
5544
+ h: 0
5545
+ };
5546
+ paintAlphaMask(color, brush, x0, y0, x1 = x0, y1 = y0) {
5547
+ const cA = color >>> 24;
5548
+ if (cA === 0) return false;
5549
+ const scratch = this.scratchBounds;
5550
+ const lookup = this.lookup;
5551
+ const tilePool = this.tilePool;
5552
+ const config = this.config;
5553
+ const tileShift = config.tileShift;
5554
+ const tileMask = config.tileMask;
5555
+ const target = config.target;
5556
+ const {
5557
+ w: bW,
5558
+ h: bH,
5559
+ data: bD,
5560
+ centerOffsetX,
5561
+ centerOffsetY
5562
+ } = brush;
5563
+ const cRGB = color & 16777215;
5564
+ let changed = false;
5565
+ forEachLinePoint(x0, y0, x1, y1, (px, py) => {
5566
+ const topLeftX = Math.floor(px + centerOffsetX);
5567
+ const topLeftY = Math.floor(py + centerOffsetY);
5568
+ trimRectBounds(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5569
+ if (scratch.w <= 0 || scratch.h <= 0) return;
5570
+ eachTileInBounds(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5571
+ const d32 = tile.data;
5572
+ let tileChanged = false;
5573
+ for (let i = 0; i < bH_t; i++) {
5574
+ const canvasY = bY + i;
5575
+ const bOff = (canvasY - topLeftY) * bW;
5576
+ const tOff = (canvasY & tileMask) << tileShift;
5577
+ const dS = tOff + (bX & tileMask);
5578
+ for (let j = 0; j < bW_t; j++) {
5579
+ const canvasX = bX + j;
5580
+ const brushA = bD[bOff + (canvasX - topLeftX)];
5581
+ if (brushA === 0) continue;
5582
+ const t = cA * brushA + 128;
5583
+ const blendedA = t + (t >> 8) >> 8;
5584
+ const idx = dS + j;
5585
+ const cur = d32[idx];
5586
+ if (brushA > cur >>> 24) {
5587
+ const next = (cRGB | blendedA << 24) >>> 0;
5588
+ if (cur !== next) {
5589
+ d32[idx] = next;
5590
+ tileChanged = true;
5591
+ }
5592
+ }
5593
+ }
5594
+ }
5595
+ if (tileChanged) changed = true;
5596
+ });
5597
+ });
5598
+ return changed;
5599
+ }
5600
+ paintBinaryMask(color, brush, x0, y0, x1 = x0, y1 = y0) {
5601
+ const alphaIsZero = color >>> 24 === 0;
5602
+ if (alphaIsZero) return false;
5603
+ const scratch = this.scratchBounds;
5604
+ const lookup = this.lookup;
5605
+ const tilePool = this.tilePool;
5606
+ const config = this.config;
5607
+ const tileShift = config.tileShift;
5608
+ const tileMask = config.tileMask;
5609
+ const target = config.target;
5610
+ const {
5611
+ w: bW,
5612
+ h: bH,
5613
+ data: bD,
5614
+ centerOffsetX,
5615
+ centerOffsetY
5616
+ } = brush;
5617
+ let changed = false;
5618
+ forEachLinePoint(x0, y0, x1, y1, (px, py) => {
5619
+ const topLeftX = Math.floor(px + centerOffsetX);
5620
+ const topLeftY = Math.floor(py + centerOffsetY);
5621
+ trimRectBounds(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5622
+ if (scratch.w <= 0 || scratch.h <= 0) return;
5623
+ eachTileInBounds(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5624
+ const d32 = tile.data;
5625
+ let tileChanged = false;
5626
+ for (let i = 0; i < bH_t; i++) {
5627
+ const canvasY = bY + i;
5628
+ const bOff = (canvasY - topLeftY) * bW;
5629
+ const tOff = (canvasY & tileMask) << tileShift;
5630
+ const dS = tOff + (bX & tileMask);
5631
+ for (let j = 0; j < bW_t; j++) {
5632
+ const canvasX = bX + j;
5633
+ if (bD[bOff + (canvasX - topLeftX)]) {
5634
+ const idx = dS + j;
5635
+ if (d32[idx] !== color) {
5636
+ d32[idx] = color;
5637
+ tileChanged = true;
5638
+ }
5639
+ }
5640
+ }
5641
+ }
5642
+ if (tileChanged) changed = true;
5643
+ });
5644
+ });
5645
+ return changed;
5646
+ }
5647
+ paintRect(color, brushWidth, brushHeight, x0, y0, x1 = x0, y1 = y0) {
5648
+ const alphaIsZero = color >>> 24 === 0;
5649
+ if (alphaIsZero) return false;
5650
+ const scratch = this.scratchBounds;
5651
+ const lookup = this.lookup;
5652
+ const tilePool = this.tilePool;
5653
+ const config = this.config;
5654
+ const tileShift = config.tileShift;
5655
+ const tileMask = config.tileMask;
5656
+ const target = config.target;
5657
+ const centerOffsetX = -(brushWidth - 1 >> 1);
5658
+ const centerOffsetY = -(brushHeight - 1 >> 1);
5659
+ let changed = false;
5660
+ forEachLinePoint(x0, y0, x1, y1, (px, py) => {
5661
+ const topLeftX = Math.floor(px + centerOffsetX);
5662
+ const topLeftY = Math.floor(py + centerOffsetY);
5663
+ trimRectBounds(topLeftX, topLeftY, brushWidth, brushHeight, target.w, target.h, scratch);
5664
+ if (scratch.w <= 0 || scratch.h <= 0) return;
5665
+ eachTileInBounds(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5666
+ const d32 = tile.data;
5667
+ let tileChanged = false;
5668
+ for (let i = 0; i < bH_t; i++) {
5669
+ const canvasY = bY + i;
5670
+ const tOff = (canvasY & tileMask) << tileShift;
5671
+ const dS = tOff + (bX & tileMask);
5672
+ for (let j = 0; j < bW_t; j++) {
5673
+ const idx = dS + j;
5674
+ if (d32[idx] !== color) {
5675
+ d32[idx] = color;
5676
+ tileChanged = true;
5677
+ }
5678
+ }
5679
+ }
5680
+ if (tileChanged) {
5681
+ changed = true;
5682
+ }
5683
+ });
5684
+ });
5685
+ return changed;
5686
+ }
5687
+ clear() {
5688
+ this.tilePool.releaseTiles(this.lookup);
5689
+ }
5690
+ };
5691
+
5692
+ // src/Paint/Commit/commitMaskPaintBuffer.ts
5693
+ var SCRATCH_OPTS = {
5694
+ alpha: 255,
5695
+ blendFn: sourceOverPerfect,
5696
+ x: 0,
5697
+ y: 0,
5698
+ w: 0,
5699
+ h: 0
5700
+ };
5701
+ function commitMaskPaintBuffer(accumulator, paintBuffer, color, alpha = 255, blendFn = sourceOverPerfect, blendColorPixelDataMaskFn) {
5702
+ const config = accumulator.config;
5703
+ const tileShift = config.tileShift;
5704
+ const lookup = paintBuffer.lookup;
5705
+ SCRATCH_OPTS.alpha = alpha;
5706
+ SCRATCH_OPTS.blendFn = blendFn;
5707
+ for (let i = 0; i < lookup.length; i++) {
5708
+ const tile = lookup[i];
5709
+ if (tile) {
5710
+ const didChange = accumulator.storeTileBeforeState(tile.id, tile.tx, tile.ty);
5711
+ const dx = tile.tx << tileShift;
5712
+ const dy = tile.ty << tileShift;
5713
+ SCRATCH_OPTS.x = dx;
5714
+ SCRATCH_OPTS.y = dy;
5715
+ SCRATCH_OPTS.w = tile.w;
5716
+ SCRATCH_OPTS.h = tile.h;
5717
+ didChange(blendColorPixelDataMaskFn(config.target, color, tile, SCRATCH_OPTS));
5718
+ }
5719
+ }
5720
+ paintBuffer.clear();
5721
+ }
5722
+
5723
+ // src/Paint/Commit/AlphaMaskPaintBufferCommitter.ts
5724
+ function makeAlphaMaskPaintBufferCommitter(accumulator, paintBuffer) {
5725
+ return function commitAlphaMaskPaintBufferToAccumulator(color, alpha = 255, blendFn = sourceOverPerfect) {
5726
+ return commitMaskPaintBuffer(accumulator, paintBuffer, color, alpha, blendFn, blendColorPixelDataAlphaMask);
5727
+ };
5728
+ }
5729
+
5730
+ // src/Internal/_constants.ts
5731
+ var DEFAULT_CANVAS_FACTORY = (w, h) => new OffscreenCanvas(w, h);
5732
+
5733
+ // src/Tile/MaskTile.ts
5734
+ var makeAlphaMaskTile = (id, tx, ty, tileSize, tileArea) => {
5735
+ return {
5736
+ tileType: 1 /* MASK */,
5737
+ type: 0 /* ALPHA */,
5738
+ data: new Uint8Array(tileArea),
5739
+ w: tileSize,
5740
+ h: tileSize,
5741
+ id,
5742
+ tx,
5743
+ ty
5744
+ };
5745
+ };
5746
+ var makeBinaryMaskTile = (id, tx, ty, tileSize, tileArea) => {
5747
+ return {
5748
+ tileType: 1 /* MASK */,
5749
+ type: 1 /* BINARY */,
5750
+ data: new Uint8Array(tileArea),
5751
+ w: tileSize,
5752
+ h: tileSize,
5753
+ id,
5754
+ tx,
5755
+ ty
5756
+ };
5757
+ };
5758
+
5759
+ // src/Paint/Render/AlphaMaskPaintBufferCanvasRenderer.ts
5760
+ function makeAlphaMaskPaintBufferCanvasRenderer(paintBuffer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
5761
+ const config = paintBuffer.config;
5762
+ const tileSize = config.tileSize;
5763
+ const tileShift = config.tileShift;
5764
+ const tileArea = config.tileArea;
5765
+ const lookup = paintBuffer.lookup;
5766
+ const canvas = canvasFactory(tileSize, tileSize);
5767
+ const ctx = canvas.getContext("2d");
5768
+ if (!ctx) throw new Error(CANVAS_CTX_FAILED);
5769
+ ctx.imageSmoothingEnabled = false;
5770
+ const bridge = makePixelData(new ImageData(tileSize, tileSize));
5771
+ const view32 = bridge.data;
5772
+ return function drawPaintBuffer(targetCtx, color, alpha = 255, compOperation = "source-over") {
5773
+ if (alpha === 0) return;
5774
+ const baseSrcAlpha = color >>> 24;
5775
+ const colorRGB = color & 16777215;
5776
+ if (baseSrcAlpha === 0) return;
5777
+ targetCtx.globalAlpha = alpha / 255;
5778
+ targetCtx.globalCompositeOperation = compOperation;
5779
+ for (let i = 0; i < lookup.length; i++) {
5780
+ const tile = lookup[i];
5781
+ if (tile) {
5782
+ const data8 = tile.data;
5783
+ view32.fill(0);
5784
+ for (let p = 0; p < tileArea; p++) {
5785
+ const maskA = data8[p];
5786
+ if (maskA === 0) continue;
5787
+ if (maskA === 255) {
5788
+ view32[p] = color;
5789
+ } else {
5790
+ const t = baseSrcAlpha * maskA + 128;
5791
+ const finalA = t + (t >> 8) >> 8;
5792
+ view32[p] = (colorRGB | finalA << 24) >>> 0;
5793
+ }
5794
+ }
5795
+ const dx = tile.tx << tileShift;
5796
+ const dy = tile.ty << tileShift;
5797
+ ctx.putImageData(bridge.imageData, 0, 0);
5798
+ targetCtx.drawImage(canvas, dx, dy);
5799
+ }
5800
+ }
5801
+ targetCtx.globalAlpha = 1;
5802
+ targetCtx.globalCompositeOperation = "source-over";
5803
+ };
5804
+ }
5805
+
5806
+ // src/Paint/Commit/AlphaMaskPaintBufferManager.ts
5807
+ function makeAlphaMaskPaintBufferManager(writer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
5808
+ const pool = new TilePool(writer.config, makeAlphaMaskTile);
5809
+ const buffer = new AlphaMaskPaintBuffer(writer.config, pool);
5810
+ const draw = makeAlphaMaskPaintBufferCanvasRenderer(buffer, canvasFactory);
5811
+ return {
5812
+ clear: buffer.clear.bind(buffer),
5813
+ paintRect: buffer.paintRect.bind(buffer),
5814
+ paintAlphaMask: buffer.paintAlphaMask.bind(buffer),
5815
+ paintBinaryMask: buffer.paintBinaryMask.bind(buffer),
5816
+ commit: makeAlphaMaskPaintBufferCommitter(writer.accumulator, buffer),
5817
+ draw
5818
+ };
5819
+ }
5820
+
5821
+ // src/Paint/Commit/BinaryMaskPaintBufferCommitter.ts
5822
+ function makeBinaryMaskPaintBufferCommitter(accumulator, paintBuffer) {
5823
+ return function commitBinaryMaskPaintBufferToAccumulator(color, alpha = 255, blendFn = sourceOverPerfect) {
5824
+ return commitMaskPaintBuffer(accumulator, paintBuffer, color, alpha, blendFn, blendColorPixelDataBinaryMask);
5825
+ };
5826
+ }
5827
+
5828
+ // src/Paint/Render/BinaryMaskPaintBufferCanvasRenderer.ts
5829
+ function makeBinaryMaskPaintBufferCanvasRenderer(paintBuffer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
5830
+ const config = paintBuffer.config;
5831
+ const tileSize = config.tileSize;
5832
+ const tileShift = config.tileShift;
5833
+ const tileArea = config.tileArea;
5834
+ const lookup = paintBuffer.lookup;
5835
+ const canvas = canvasFactory(tileSize, tileSize);
5836
+ const ctx = canvas.getContext("2d");
5837
+ if (!ctx) throw new Error(CANVAS_CTX_FAILED);
5838
+ ctx.imageSmoothingEnabled = false;
5839
+ const bridge = makePixelData(new ImageData(tileSize, tileSize));
5840
+ const view32 = bridge.data;
5841
+ return function drawPaintBuffer(targetCtx, color, alpha = 255, compOperation = "source-over") {
5842
+ if (alpha === 0) return;
5843
+ const baseSrcAlpha = color >>> 24;
5844
+ if (baseSrcAlpha === 0) return;
5845
+ targetCtx.globalAlpha = alpha / 255;
5846
+ targetCtx.globalCompositeOperation = compOperation;
5847
+ for (let i = 0; i < lookup.length; i++) {
5848
+ const tile = lookup[i];
5849
+ if (tile) {
5850
+ const data8 = tile.data;
5851
+ view32.fill(0);
5852
+ for (let p = 0; p < tileArea; p++) {
5853
+ if (data8[p] === 1) {
5854
+ view32[p] = color;
5855
+ }
5856
+ }
5857
+ const dx = tile.tx << tileShift;
5858
+ const dy = tile.ty << tileShift;
5859
+ ctx.putImageData(bridge.imageData, 0, 0);
5860
+ targetCtx.drawImage(canvas, dx, dy);
5861
+ }
5862
+ }
5863
+ targetCtx.globalAlpha = 1;
5864
+ targetCtx.globalCompositeOperation = "source-over";
5865
+ };
5866
+ }
5867
+
5868
+ // src/Paint/Commit/BinaryMaskPaintBufferManager.ts
5869
+ function makeBinaryMaskPaintBufferManager(writer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
5870
+ const pool = new TilePool(writer.config, makeBinaryMaskTile);
5871
+ const buffer = new BinaryMaskPaintBuffer(writer.config, pool);
5872
+ const draw = makeBinaryMaskPaintBufferCanvasRenderer(buffer, canvasFactory);
5873
+ return {
5874
+ clear: buffer.clear.bind(buffer),
5875
+ paintRect: buffer.paintRect.bind(buffer),
5876
+ paintBinaryMask: buffer.paintBinaryMask.bind(buffer),
5877
+ commit: makeBinaryMaskPaintBufferCommitter(writer.accumulator, buffer),
5878
+ draw
5879
+ };
5880
+ }
5881
+
5882
+ // src/Paint/Commit/commitColorPaintBuffer.ts
5883
+ var SCRATCH_OPTS2 = {
5884
+ alpha: 255,
5885
+ blendFn: sourceOverPerfect,
5886
+ x: 0,
5887
+ y: 0,
5888
+ w: 0,
5889
+ h: 0
5890
+ };
5891
+ function commitColorPaintBuffer(accumulator, paintBuffer, alpha = 255, blendFn = sourceOverPerfect, blendPixelDataFn = blendPixelData) {
5892
+ const config = accumulator.config;
5893
+ const tileShift = config.tileShift;
5894
+ const lookup = paintBuffer.lookup;
5895
+ SCRATCH_OPTS2.alpha = alpha;
5896
+ SCRATCH_OPTS2.blendFn = blendFn;
5897
+ for (let i = 0; i < lookup.length; i++) {
5898
+ const tile = lookup[i];
5899
+ if (tile) {
5900
+ const didChange = accumulator.storeTileBeforeState(tile.id, tile.tx, tile.ty);
5901
+ const dx = tile.tx << tileShift;
5902
+ const dy = tile.ty << tileShift;
5903
+ SCRATCH_OPTS2.x = dx;
5904
+ SCRATCH_OPTS2.y = dy;
5905
+ SCRATCH_OPTS2.w = tile.w;
5906
+ SCRATCH_OPTS2.h = tile.h;
5907
+ didChange(blendPixelDataFn(config.target, tile, SCRATCH_OPTS2));
5908
+ }
5909
+ }
5910
+ paintBuffer.clear();
5911
+ }
5912
+
5913
+ // src/Paint/Commit/ColorPaintBufferCommitter.ts
5914
+ function makeColorPaintBufferCommitter(accumulator, paintBuffer) {
5915
+ return function commitColorPaintBufferToAccumulator(alpha = 255, blendFn = sourceOverPerfect) {
5916
+ return commitColorPaintBuffer(accumulator, paintBuffer, alpha, blendFn, blendPixelData);
5917
+ };
5918
+ }
5919
+
5920
+ // src/Paint/Render/ColorPaintBufferCanvasRenderer.ts
5921
+ function makeColorPaintBufferCanvasRenderer(paintBuffer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
5922
+ const config = paintBuffer.config;
5923
+ const tileSize = config.tileSize;
5924
+ const tileShift = config.tileShift;
5925
+ const lookup = paintBuffer.lookup;
5926
+ const canvas = canvasFactory(tileSize, tileSize);
5927
+ const ctx = canvas.getContext("2d");
5928
+ if (!ctx) throw new Error(CANVAS_CTX_FAILED);
5929
+ ctx.imageSmoothingEnabled = false;
5930
+ return function drawPaintBuffer(targetCtx, alpha = 255, compOperation = "source-over") {
5931
+ targetCtx.globalAlpha = alpha / 255;
5932
+ targetCtx.globalCompositeOperation = compOperation;
5933
+ for (let i = 0; i < lookup.length; i++) {
5934
+ const tile = lookup[i];
5935
+ if (tile) {
5936
+ const dx = tile.tx << tileShift;
5937
+ const dy = tile.ty << tileShift;
5938
+ ctx.putImageData(tile.imageData, 0, 0);
5939
+ targetCtx.drawImage(canvas, dx, dy);
5940
+ }
5941
+ }
5942
+ targetCtx.globalAlpha = 1;
5943
+ targetCtx.globalCompositeOperation = "source-over";
5944
+ };
5945
+ }
5946
+
5947
+ // src/Paint/Commit/ColorPaintBufferManager.ts
5948
+ function makeColorPaintBufferManager(writer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
5949
+ const pool = new TilePool(writer.config, makePixelTile);
5950
+ const buffer = new ColorPaintBuffer(writer.config, pool);
5951
+ const draw = makeColorPaintBufferCanvasRenderer(buffer, canvasFactory);
5952
+ return {
5953
+ clear: buffer.clear.bind(buffer),
5954
+ paintRect: buffer.paintRect.bind(buffer),
5955
+ paintAlphaMask: buffer.paintAlphaMask.bind(buffer),
5956
+ paintBinaryMask: buffer.paintBinaryMask.bind(buffer),
5957
+ commit: makeColorPaintBufferCommitter(writer.accumulator, buffer),
5958
+ draw
5959
+ };
5960
+ }
5961
+
5962
+ // src/Paint/makeCirclePaintMask.ts
5963
+ function makeCirclePaintAlphaMask(size, fallOff = (d) => d) {
5964
+ const area = size * size;
5965
+ const data = new Uint8Array(area);
5966
+ const radius = size / 2;
5967
+ const invR = 1 / radius;
5968
+ const centerOffset = -Math.ceil(radius - 0.5);
5969
+ for (let y = 0; y < size; y++) {
5970
+ const rowOffset = y * size;
5971
+ const dy = y - radius + 0.5;
5972
+ const dy2 = dy * dy;
5973
+ for (let x = 0; x < size; x++) {
5974
+ const dx = x - radius + 0.5;
5975
+ const distSqr = dx * dx + dy2;
5976
+ if (distSqr <= radius * radius) {
5977
+ const dist = Math.sqrt(distSqr) * invR;
5978
+ const strength = fallOff(1 - dist);
5979
+ if (strength > 0) {
5980
+ const intensity = strength * 255 | 0;
5981
+ data[rowOffset + x] = Math.max(0, Math.min(255, intensity));
5982
+ }
5983
+ }
5984
+ }
5985
+ }
5986
+ return {
5987
+ type: 0 /* ALPHA */,
5988
+ outlineType: 1 /* CIRCLE */,
5989
+ data,
5990
+ w: size,
5991
+ h: size,
5992
+ centerOffsetX: centerOffset,
5993
+ centerOffsetY: centerOffset
5994
+ };
5995
+ }
5996
+ function makeCirclePaintBinaryMask(size) {
5997
+ const area = size * size;
5998
+ const data = new Uint8Array(area);
5999
+ const radius = size / 2;
6000
+ const r2 = radius * radius;
6001
+ for (let y = 0; y < size; y++) {
6002
+ for (let x = 0; x < size; x++) {
6003
+ const dx = x - radius + 0.5;
6004
+ const dy = y - radius + 0.5;
6005
+ const distSqr = dx * dx + dy * dy;
6006
+ if (distSqr <= r2) {
6007
+ data[y * size + x] = 1;
6008
+ }
6009
+ }
6010
+ }
6011
+ const centerOffset = -Math.ceil(radius - 0.5);
6012
+ return {
6013
+ type: 1 /* BINARY */,
6014
+ outlineType: 1 /* CIRCLE */,
6015
+ w: size,
6016
+ h: size,
6017
+ data,
6018
+ centerOffsetX: centerOffset,
6019
+ centerOffsetY: centerOffset
6020
+ };
6021
+ }
6022
+
6023
+ // src/Paint/makePaintMask.ts
6024
+ function makePaintBinaryMask(mask) {
6025
+ return {
6026
+ type: 1 /* BINARY */,
6027
+ outlineType: 0 /* MASKED */,
6028
+ data: mask.data,
6029
+ w: mask.w,
6030
+ h: mask.h,
6031
+ centerOffsetX: -(mask.w >> 1),
6032
+ centerOffsetY: -(mask.h >> 1)
6033
+ };
6034
+ }
6035
+ function makePaintAlphaMask(mask) {
6036
+ return {
6037
+ type: 0 /* ALPHA */,
6038
+ outlineType: 0 /* MASKED */,
6039
+ data: mask.data,
6040
+ w: mask.w,
6041
+ h: mask.h,
6042
+ centerOffsetX: -(mask.w >> 1),
6043
+ centerOffsetY: -(mask.h >> 1)
6044
+ };
6045
+ }
6046
+
6047
+ // src/Paint/makeRectFalloffPaintAlphaMask.ts
6048
+ function makeRectFalloffPaintAlphaMask(width, height, fallOff = (d) => d) {
6049
+ const fPx = Math.floor(width / 2);
6050
+ const fPy = Math.floor(height / 2);
6051
+ const invHalfW = 2 / width;
6052
+ const invHalfH = 2 / height;
6053
+ const offX = width % 2 === 0 ? 0.5 : 0;
6054
+ const offY = height % 2 === 0 ? 0.5 : 0;
6055
+ const area = width * height;
6056
+ const data = new Uint8Array(area);
6057
+ for (let y = 0; y < height; y++) {
6058
+ const dy = Math.abs(y - fPy + offY) * invHalfH;
6059
+ const rowOffset = y * width;
6060
+ for (let x = 0; x < width; x++) {
6061
+ const dx = Math.abs(x - fPx + offX) * invHalfW;
6062
+ const dist = dx > dy ? dx : dy;
6063
+ const strength = fallOff(1 - dist);
6064
+ if (strength > 0) {
6065
+ const intensity = strength * 255 | 0;
6066
+ data[rowOffset + x] = Math.max(0, Math.min(255, intensity));
6067
+ }
6068
+ }
6069
+ }
6070
+ return {
6071
+ type: 0 /* ALPHA */,
6072
+ outlineType: 2 /* RECT */,
6073
+ data,
6074
+ w: width,
6075
+ h: height,
6076
+ centerOffsetX: -(width >> 1),
6077
+ centerOffsetY: -(height >> 1)
6078
+ };
6079
+ }
6080
+
6081
+ // src/PixelData/ReusablePixelData.ts
6082
+ function makeReusablePixelData() {
6083
+ const pixelData = {
6084
+ w: 0,
6085
+ h: 0,
6086
+ data: null,
6087
+ imageData: null
6088
+ };
6089
+ return function getReusablePixelData(width, height) {
6090
+ if (pixelData.w !== width || pixelData.h !== height) {
6091
+ setPixelData(pixelData, new ImageData(width, height));
6092
+ } else {
6093
+ pixelData.data.fill(0);
6094
+ }
6095
+ return pixelData;
6096
+ };
6097
+ }
6098
+
6099
+ // src/Paint/Render/PaintCursorRenderer.ts
6100
+ function makePaintCursorRenderer(reusableCanvasFactory) {
6101
+ const factory = reusableCanvasFactory ?? makeReusableOffscreenCanvas;
6102
+ const updateBuffer = factory();
6103
+ const {
6104
+ canvas,
6105
+ ctx
6106
+ } = updateBuffer(1, 1);
6107
+ const getPixelData = makeReusablePixelData();
6108
+ let _color = packColor(0, 255, 255, 255);
6109
+ let _scale = 1;
6110
+ let currentMask = {
6111
+ type: 1 /* BINARY */,
6112
+ outlineType: 2 /* RECT */,
6113
+ w: 1,
6114
+ h: 1,
6115
+ centerOffsetX: -(10 - 1 >> 1),
6116
+ centerOffsetY: -(10 - 1 >> 1)
6117
+ };
6118
+ let outline;
6119
+ function update(paintMask, scale, color, alphaThreshold = 127) {
6120
+ currentMask = paintMask ?? currentMask;
6121
+ _scale = scale ?? _scale;
6122
+ _color = color ?? _color;
6123
+ updateBuffer(currentMask.w * _scale + 2 * _scale, currentMask.h * _scale + 2 * _scale);
6124
+ if (currentMask.type === 1 /* BINARY */) {
6125
+ if (currentMask.outlineType === 1 /* CIRCLE */) {
6126
+ outline = makeCircleBinaryMaskOutline(currentMask.w, _scale);
6127
+ } else if (currentMask.outlineType === 2 /* RECT */) {
6128
+ outline = makeRectBinaryMaskOutline(currentMask.w, currentMask.h, _scale);
6129
+ } else if (currentMask.outlineType === 0 /* MASKED */) {
6130
+ outline = makeBinaryMaskOutline(currentMask, _scale);
6131
+ }
6132
+ } else if (currentMask.type === 0 /* ALPHA */) {
6133
+ const mask = makeBinaryMaskFromAlphaMask(currentMask, alphaThreshold);
6134
+ outline = makeBinaryMaskOutline(mask, _scale);
6135
+ }
6136
+ const pixelData = getPixelData(outline.w, outline.h);
6137
+ fillPixelDataBinaryMask(pixelData, _color, outline);
6138
+ ctx.putImageData(pixelData.imageData, 0, 0);
6139
+ }
6140
+ const boundsScratch = {
6141
+ x: 0,
6142
+ y: 0,
6143
+ w: 0,
6144
+ h: 0
6145
+ };
6146
+ function getBounds(centerX, centerY) {
6147
+ boundsScratch.x = centerX + currentMask.centerOffsetX;
6148
+ boundsScratch.y = centerY + currentMask.centerOffsetY;
6149
+ boundsScratch.w = currentMask.w;
6150
+ boundsScratch.h = currentMask.h;
6151
+ return boundsScratch;
6152
+ }
6153
+ const boundsScaledScratch = {
6154
+ x: 0,
6155
+ y: 0,
6156
+ w: 0,
6157
+ h: 0
6158
+ };
6159
+ function getOutlineBoundsScaled(centerX, centerY) {
6160
+ boundsScaledScratch.x = centerX * _scale + currentMask.centerOffsetX * _scale - 1;
6161
+ boundsScaledScratch.y = centerY * _scale + currentMask.centerOffsetY * _scale - 1;
6162
+ boundsScaledScratch.w = currentMask.w * _scale;
6163
+ boundsScaledScratch.h = currentMask.h * _scale;
6164
+ return boundsScaledScratch;
6165
+ }
6166
+ function draw(drawCtx, centerX, centerY) {
6167
+ const dx = centerX * _scale + currentMask.centerOffsetX * _scale - 1;
6168
+ const dy = centerY * _scale + currentMask.centerOffsetY * _scale - 1;
6169
+ drawCtx.drawImage(canvas, Math.floor(dx), Math.floor(dy));
6170
+ }
6171
+ function getSettings() {
6172
+ return {
6173
+ color: _color,
6174
+ scale: _scale,
6175
+ currentMask
6176
+ };
6177
+ }
6178
+ return {
6179
+ update,
6180
+ getBounds,
6181
+ getBoundsScaled: getOutlineBoundsScaled,
6182
+ draw,
6183
+ getSettings
6184
+ };
6185
+ }
6186
+
6187
+ // src/PixelData/applyMaskToPixelData.ts
6188
+ function applyMaskToPixelData(dst, mask, opts) {
6189
+ if (mask.type === 1 /* BINARY */) {
6190
+ return applyBinaryMaskToPixelData(dst, mask, opts);
6191
+ } else {
6192
+ return applyAlphaMaskToPixelData(dst, mask, opts);
6193
+ }
6194
+ }
6195
+
6196
+ // src/PixelData/blendColorPixelDataMask.ts
6197
+ function blendColorPixelDataMask(dst, color, mask, opts) {
6198
+ if (mask.type === 1 /* BINARY */) {
6199
+ return blendColorPixelDataBinaryMask(dst, color, mask, opts);
6200
+ } else {
5037
6201
  return blendColorPixelDataAlphaMask(dst, color, mask, opts);
5038
6202
  }
5039
6203
  }
5040
6204
 
5041
6205
  // src/PixelData/blendColorPixelDataPaintAlphaMask.ts
5042
- var SCRATCH_OPTS = {
6206
+ var SCRATCH_OPTS3 = {
5043
6207
  x: 0,
5044
6208
  y: 0,
5045
6209
  alpha: 255,
@@ -5048,15 +6212,15 @@ var SCRATCH_OPTS = {
5048
6212
  function blendColorPixelDataPaintAlphaMask(dst, color, mask, x, y, alpha = 255, blendFn = sourceOverPerfect) {
5049
6213
  const tx = x + mask.centerOffsetX;
5050
6214
  const ty = y + mask.centerOffsetY;
5051
- SCRATCH_OPTS.x = tx;
5052
- SCRATCH_OPTS.y = ty;
5053
- SCRATCH_OPTS.alpha = alpha;
5054
- SCRATCH_OPTS.blendFn = blendFn;
5055
- return blendColorPixelDataAlphaMask(dst, color, mask, SCRATCH_OPTS);
6215
+ SCRATCH_OPTS3.x = tx;
6216
+ SCRATCH_OPTS3.y = ty;
6217
+ SCRATCH_OPTS3.alpha = alpha;
6218
+ SCRATCH_OPTS3.blendFn = blendFn;
6219
+ return blendColorPixelDataAlphaMask(dst, color, mask, SCRATCH_OPTS3);
5056
6220
  }
5057
6221
 
5058
6222
  // src/PixelData/blendColorPixelDataPaintBinaryMask.ts
5059
- var SCRATCH_OPTS2 = {
6223
+ var SCRATCH_OPTS4 = {
5060
6224
  x: 0,
5061
6225
  y: 0,
5062
6226
  alpha: 255,
@@ -5065,15 +6229,15 @@ var SCRATCH_OPTS2 = {
5065
6229
  function blendColorPixelDataPaintBinaryMask(dst, color, mask, x, y, alpha = 255, blendFn = sourceOverPerfect) {
5066
6230
  const tx = x + mask.centerOffsetX;
5067
6231
  const ty = y + mask.centerOffsetY;
5068
- SCRATCH_OPTS2.x = tx;
5069
- SCRATCH_OPTS2.y = ty;
5070
- SCRATCH_OPTS2.alpha = alpha;
5071
- SCRATCH_OPTS2.blendFn = blendFn;
5072
- return blendColorPixelDataBinaryMask(dst, color, mask, SCRATCH_OPTS2);
6232
+ SCRATCH_OPTS4.x = tx;
6233
+ SCRATCH_OPTS4.y = ty;
6234
+ SCRATCH_OPTS4.alpha = alpha;
6235
+ SCRATCH_OPTS4.blendFn = blendFn;
6236
+ return blendColorPixelDataBinaryMask(dst, color, mask, SCRATCH_OPTS4);
5073
6237
  }
5074
6238
 
5075
6239
  // src/PixelData/blendColorPixelDataPaintMask.ts
5076
- var SCRATCH_OPTS3 = {
6240
+ var SCRATCH_OPTS5 = {
5077
6241
  x: 0,
5078
6242
  y: 0,
5079
6243
  alpha: 255,
@@ -5082,14 +6246,14 @@ var SCRATCH_OPTS3 = {
5082
6246
  function blendColorPixelDataPaintMask(dst, color, mask, x, y, alpha = 255, blendFn = sourceOverPerfect) {
5083
6247
  const tx = x + mask.centerOffsetX;
5084
6248
  const ty = y + mask.centerOffsetY;
5085
- SCRATCH_OPTS3.x = tx;
5086
- SCRATCH_OPTS3.y = ty;
5087
- SCRATCH_OPTS3.alpha = alpha;
5088
- SCRATCH_OPTS3.blendFn = blendFn;
6249
+ SCRATCH_OPTS5.x = tx;
6250
+ SCRATCH_OPTS5.y = ty;
6251
+ SCRATCH_OPTS5.alpha = alpha;
6252
+ SCRATCH_OPTS5.blendFn = blendFn;
5089
6253
  if (mask.type === 1 /* BINARY */) {
5090
- return blendColorPixelDataBinaryMask(dst, color, mask, SCRATCH_OPTS3);
6254
+ return blendColorPixelDataBinaryMask(dst, color, mask, SCRATCH_OPTS5);
5091
6255
  } else {
5092
- return blendColorPixelDataAlphaMask(dst, color, mask, SCRATCH_OPTS3);
6256
+ return blendColorPixelDataAlphaMask(dst, color, mask, SCRATCH_OPTS5);
5093
6257
  }
5094
6258
  }
5095
6259
 
@@ -5103,7 +6267,7 @@ function blendPixelDataMask(target, src, mask, opts) {
5103
6267
  }
5104
6268
 
5105
6269
  // src/PixelData/blendPixelDataPaintBuffer.ts
5106
- var SCRATCH_OPTS4 = {
6270
+ var SCRATCH_OPTS6 = {
5107
6271
  x: 0,
5108
6272
  y: 0,
5109
6273
  alpha: 255,
@@ -5117,11 +6281,11 @@ function blendPixelDataPaintBuffer(target, paintBuffer, alpha = 255, blendFn, bl
5117
6281
  if (tile) {
5118
6282
  const x = tile.tx << tileShift;
5119
6283
  const y = tile.ty << tileShift;
5120
- SCRATCH_OPTS4.x = x;
5121
- SCRATCH_OPTS4.y = y;
5122
- SCRATCH_OPTS4.alpha = alpha;
5123
- SCRATCH_OPTS4.blendFn = blendFn;
5124
- blendPixelDataFn(target, tile, SCRATCH_OPTS4);
6284
+ SCRATCH_OPTS6.x = x;
6285
+ SCRATCH_OPTS6.y = y;
6286
+ SCRATCH_OPTS6.alpha = alpha;
6287
+ SCRATCH_OPTS6.blendFn = blendFn;
6288
+ blendPixelDataFn(target, tile, SCRATCH_OPTS6);
5125
6289
  }
5126
6290
  }
5127
6291
  }
@@ -5136,8 +6300,8 @@ function fillPixelDataFast(dst, color, _x, _y, _w, _h) {
5136
6300
  if (typeof _x === "object") {
5137
6301
  x = _x.x ?? 0;
5138
6302
  y = _x.y ?? 0;
5139
- w = _x.w ?? dst.width;
5140
- h = _x.h ?? dst.height;
6303
+ w = _x.w ?? dst.w;
6304
+ h = _x.h ?? dst.h;
5141
6305
  } else if (typeof _x === "number") {
5142
6306
  x = _x;
5143
6307
  y = _y;
@@ -5146,10 +6310,10 @@ function fillPixelDataFast(dst, color, _x, _y, _w, _h) {
5146
6310
  } else {
5147
6311
  x = 0;
5148
6312
  y = 0;
5149
- w = dst.width;
5150
- h = dst.height;
6313
+ w = dst.w;
6314
+ h = dst.h;
5151
6315
  }
5152
- const clip = resolveRectClipping(x, y, w, h, dst.width, dst.height, SCRATCH_RECT4);
6316
+ const clip = resolveRectClipping(x, y, w, h, dst.w, dst.h, SCRATCH_RECT4);
5153
6317
  if (!clip.inBounds) return;
5154
6318
  const {
5155
6319
  x: finalX,
@@ -5157,9 +6321,9 @@ function fillPixelDataFast(dst, color, _x, _y, _w, _h) {
5157
6321
  w: actualW,
5158
6322
  h: actualH
5159
6323
  } = clip;
5160
- const dst32 = dst.data32;
5161
- const dw = dst.width;
5162
- if (actualW === dw && actualH === dst.height && finalX === 0 && finalY === 0) {
6324
+ const dst32 = dst.data;
6325
+ const dw = dst.w;
6326
+ if (actualW === dw && actualH === dst.h && finalX === 0 && finalY === 0) {
5163
6327
  dst32.fill(color);
5164
6328
  return;
5165
6329
  }
@@ -5175,6 +6339,13 @@ function clearPixelDataFast(dst, rect) {
5175
6339
  fillPixelDataFast(dst, 0, rect);
5176
6340
  }
5177
6341
 
6342
+ // src/PixelData/copyPixelData.ts
6343
+ function copyPixelData(target) {
6344
+ const data = target.imageData.data;
6345
+ const buffer = new Uint8ClampedArray(data);
6346
+ return makePixelData(new ImageData(buffer, target.w, target.h));
6347
+ }
6348
+
5178
6349
  // src/PixelData/extractPixelDataBuffer.ts
5179
6350
  var SCRATCH_BLIT4 = makeClippedBlit();
5180
6351
  function extractPixelDataBuffer(source, _x, _y, _w, _h) {
@@ -5189,9 +6360,9 @@ function extractPixelDataBuffer(source, _x, _y, _w, _h) {
5189
6360
  w: _w,
5190
6361
  h: _h
5191
6362
  };
5192
- const srcW = source.width;
5193
- const srcH = source.height;
5194
- const srcData = source.data32;
6363
+ const srcW = source.w;
6364
+ const srcH = source.h;
6365
+ const srcData = source.data;
5195
6366
  if (w <= 0 || h <= 0) {
5196
6367
  return new Uint32Array(0);
5197
6368
  }
@@ -5228,24 +6399,24 @@ function extractPixelData(source, _x, _y, _w, _h) {
5228
6399
  w: _w,
5229
6400
  h: _h
5230
6401
  };
5231
- const result = new PixelData(new ImageData(w, h));
6402
+ const result = makePixelData(new ImageData(w, h));
5232
6403
  const buffer = extractPixelDataBuffer(source, x, y, w, h);
5233
- result.data32.set(buffer);
6404
+ result.data.set(buffer);
5234
6405
  return result;
5235
6406
  }
5236
6407
 
5237
6408
  // src/PixelData/pixelDataToAlphaMask.ts
5238
6409
  function pixelDataToAlphaMask(pixelData) {
5239
6410
  const {
5240
- data32,
5241
- width,
5242
- height
6411
+ data,
6412
+ w,
6413
+ h
5243
6414
  } = pixelData;
5244
- const len = data32.length;
5245
- const mask = makeAlphaMask(width, height);
6415
+ const len = data.length;
6416
+ const mask = makeAlphaMask(w, h);
5246
6417
  const maskData = mask.data;
5247
6418
  for (let i = 0; i < len; i++) {
5248
- const val = data32[i];
6419
+ const val = data[i];
5249
6420
  maskData[i] = val >>> 24 & 255;
5250
6421
  }
5251
6422
  return mask;
@@ -5253,9 +6424,9 @@ function pixelDataToAlphaMask(pixelData) {
5253
6424
 
5254
6425
  // src/PixelData/reflectPixelData.ts
5255
6426
  function reflectPixelDataHorizontal(pixelData) {
5256
- const width = pixelData.width;
5257
- const height = pixelData.height;
5258
- const data = pixelData.data32;
6427
+ const width = pixelData.w;
6428
+ const height = pixelData.h;
6429
+ const data = pixelData.data;
5259
6430
  const halfWidth = Math.floor(width / 2);
5260
6431
  for (let y = 0; y < height; y++) {
5261
6432
  const rowOffset = y * width;
@@ -5269,9 +6440,9 @@ function reflectPixelDataHorizontal(pixelData) {
5269
6440
  }
5270
6441
  }
5271
6442
  function reflectPixelDataVertical(pixelData) {
5272
- const width = pixelData.width;
5273
- const height = pixelData.height;
5274
- const data = pixelData.data32;
6443
+ const width = pixelData.w;
6444
+ const height = pixelData.h;
6445
+ const data = pixelData.data;
5275
6446
  const halfHeight = Math.floor(height / 2);
5276
6447
  for (let y = 0; y < halfHeight; y++) {
5277
6448
  const topRowOffset = y * width;
@@ -5288,19 +6459,21 @@ function reflectPixelDataVertical(pixelData) {
5288
6459
 
5289
6460
  // src/PixelData/resamplePixelData.ts
5290
6461
  function resamplePixelData(pixelData, factor) {
5291
- const {
5292
- data,
5293
- width,
5294
- height
5295
- } = resample32(pixelData.data32, pixelData.width, pixelData.height, factor);
5296
- return new PixelData(new ImageData(new Uint8ClampedArray(data.buffer), width, height));
6462
+ const output = {};
6463
+ const resampled = resampleUint32Array(pixelData.data, pixelData.w, pixelData.h, factor, output);
6464
+ resampled.imageData = uInt32ArrayToImageData(resampled.data, resampled.w, resampled.h);
6465
+ return resampled;
6466
+ }
6467
+ function resamplePixelDataInPlace(pixelData, factor) {
6468
+ const resampled = resampleUint32Array(pixelData.data, pixelData.w, pixelData.h, factor, pixelData);
6469
+ resampled.imageData = uInt32ArrayToImageData(resampled.data, resampled.w, resampled.h);
5297
6470
  }
5298
6471
 
5299
6472
  // src/PixelData/rotatePixelData.ts
5300
6473
  function rotatePixelData(pixelData) {
5301
- const width = pixelData.width;
5302
- const height = pixelData.height;
5303
- const data = pixelData.data32;
6474
+ const width = pixelData.w;
6475
+ const height = pixelData.h;
6476
+ const data = pixelData.data;
5304
6477
  if (width === height) {
5305
6478
  rotateSquareInPlace(pixelData);
5306
6479
  return;
@@ -5318,11 +6491,11 @@ function rotatePixelData(pixelData) {
5318
6491
  }
5319
6492
  }
5320
6493
  const newImageData = new ImageData(new Uint8ClampedArray(newData32.buffer), newWidth, newHeight);
5321
- pixelData.set(newImageData);
6494
+ setPixelData(pixelData, newImageData);
5322
6495
  }
5323
6496
  function rotateSquareInPlace(pixelData) {
5324
- const n = pixelData.width;
5325
- const data = pixelData.data32;
6497
+ const n = pixelData.w;
6498
+ const data = pixelData.data;
5326
6499
  for (let i = 0; i < n / 2; i++) {
5327
6500
  for (let j = i; j < n - i - 1; j++) {
5328
6501
  const top = i * n + j;
@@ -5338,6 +6511,16 @@ function rotateSquareInPlace(pixelData) {
5338
6511
  }
5339
6512
  }
5340
6513
 
6514
+ // src/PixelData/uInt32ArrayToPixelData.ts
6515
+ function uInt32ArrayToPixelData(data, width, height) {
6516
+ const buffer = data.buffer;
6517
+ const byteOffset = data.byteOffset;
6518
+ const byteLength = data.byteLength;
6519
+ const clampedArray = new Uint8ClampedArray(buffer, byteOffset, byteLength);
6520
+ const imageData = new ImageData(clampedArray, width, height);
6521
+ return makePixelData(imageData);
6522
+ }
6523
+
5341
6524
  // src/PixelData/writePixelDataBuffer.ts
5342
6525
  var SCRATCH_BLIT5 = makeClippedBlit();
5343
6526
  function writePixelDataBuffer(target, data, _x, _y, _w, _h) {
@@ -5352,9 +6535,9 @@ function writePixelDataBuffer(target, data, _x, _y, _w, _h) {
5352
6535
  w: _w,
5353
6536
  h: _h
5354
6537
  };
5355
- const dstW = target.width;
5356
- const dstH = target.height;
5357
- const dstData = target.data32;
6538
+ const dstW = target.w;
6539
+ const dstH = target.h;
6540
+ const dstData = target.data;
5358
6541
  const clip = resolveBlitClipping(x, y, 0, 0, w, h, dstW, dstH, w, h, SCRATCH_BLIT5);
5359
6542
  if (!clip.inBounds) return;
5360
6543
  const {
@@ -5381,26 +6564,28 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5381
6564
  if (tile) {
5382
6565
  const dx = tile.tx << tileShift;
5383
6566
  const dy = tile.ty << tileShift;
5384
- writePixelDataBufferFn(target, tile.data32, dx, dy, tile.width, tile.height);
6567
+ writePixelDataBufferFn(target, tile.data, dx, dy, tile.w, tile.h);
5385
6568
  }
5386
6569
  }
5387
6570
  }
5388
6571
  // Annotate the CommonJS export names for ESM import in node:
5389
6572
  0 && (module.exports = {
6573
+ AlphaMaskPaintBuffer,
5390
6574
  BASE_FAST_BLEND_MODE_FUNCTIONS,
5391
6575
  BASE_PERFECT_BLEND_MODE_FUNCTIONS,
5392
6576
  BaseBlendMode,
6577
+ BinaryMaskPaintBuffer,
5393
6578
  CANVAS_COMPOSITE_MAP,
6579
+ ColorPaintBuffer,
6580
+ ERRORS,
5394
6581
  HistoryManager,
5395
- IndexedImage,
5396
6582
  MaskType,
5397
- PaintBuffer,
6583
+ PaintMaskOutline,
5398
6584
  PixelAccumulator,
5399
- PixelData,
5400
6585
  PixelEngineConfig,
5401
- PixelTile,
5402
- PixelTilePool,
5403
6586
  PixelWriter,
6587
+ TilePool,
6588
+ TileType,
5404
6589
  UnsupportedFormatError,
5405
6590
  applyAlphaMaskToPixelData,
5406
6591
  applyBinaryMaskToAlphaMask,
@@ -5430,9 +6615,12 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5430
6615
  colorDistance,
5431
6616
  colorDodgeFast,
5432
6617
  colorDodgePerfect,
6618
+ commitColorPaintBuffer,
6619
+ commitMaskPaintBuffer,
5433
6620
  copyImageData,
5434
6621
  copyImageDataLike,
5435
6622
  copyMask,
6623
+ copyPixelData,
5436
6624
  darkenFast,
5437
6625
  darkenPerfect,
5438
6626
  darkerFast,
@@ -5440,10 +6628,19 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5440
6628
  deserializeImageData,
5441
6629
  deserializeNullableImageData,
5442
6630
  deserializeRawImageData,
6631
+ destinationAtopFast,
6632
+ destinationAtopPerfect,
6633
+ destinationInFast,
6634
+ destinationInPerfect,
6635
+ destinationOutFast,
6636
+ destinationOutPerfect,
6637
+ destinationOverFast,
6638
+ destinationOverPerfect,
5443
6639
  differenceFast,
5444
6640
  differencePerfect,
5445
6641
  divideFast,
5446
6642
  dividePerfect,
6643
+ eachTileInBounds,
5447
6644
  exclusionFast,
5448
6645
  exclusionPerfect,
5449
6646
  extractImageDataBuffer,
@@ -5459,6 +6656,7 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5459
6656
  floodFillSelection,
5460
6657
  forEachLinePoint,
5461
6658
  getImageDataFromClipboard,
6659
+ getIndexedImageColor,
5462
6660
  getIndexedImageColorCounts,
5463
6661
  getRectsBounds,
5464
6662
  getSupportedPixelFormats,
@@ -5469,7 +6667,7 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5469
6667
  imageDataToAlphaMaskBuffer,
5470
6668
  imageDataToDataUrl,
5471
6669
  imageDataToImgBlob,
5472
- imageDataToUInt32Array,
6670
+ imageDataToUint32Array,
5473
6671
  imgBlobToImageData,
5474
6672
  indexedImageToAverageColor,
5475
6673
  indexedImageToImageData,
@@ -5490,26 +6688,48 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5490
6688
  linearLightFast,
5491
6689
  linearLightPerfect,
5492
6690
  makeAlphaMask,
6691
+ makeAlphaMaskPaintBufferCanvasRenderer,
6692
+ makeAlphaMaskPaintBufferCommitter,
6693
+ makeAlphaMaskPaintBufferManager,
6694
+ makeAlphaMaskTile,
5493
6695
  makeBinaryMask,
6696
+ makeBinaryMaskFromAlphaMask,
6697
+ makeBinaryMaskOutline,
6698
+ makeBinaryMaskPaintBufferCanvasRenderer,
6699
+ makeBinaryMaskPaintBufferCommitter,
6700
+ makeBinaryMaskPaintBufferManager,
6701
+ makeBinaryMaskTile,
5494
6702
  makeBlendModeRegistry,
5495
6703
  makeCanvasFrameRenderer,
6704
+ makeCanvasPixelDataRenderer,
6705
+ makeCircleBinaryMaskOutline,
5496
6706
  makeCirclePaintAlphaMask,
5497
6707
  makeCirclePaintBinaryMask,
5498
6708
  makeClippedBlit,
5499
6709
  makeClippedRect,
6710
+ makeColorPaintBufferCanvasRenderer,
6711
+ makeColorPaintBufferCommitter,
6712
+ makeColorPaintBufferManager,
5500
6713
  makeFastBlendModeRegistry,
5501
6714
  makeFullPixelMutator,
5502
6715
  makeHistoryAction,
5503
6716
  makeImageDataLike,
6717
+ makeIndexedImage,
6718
+ makeIndexedImageFromImageData,
6719
+ makeIndexedImageFromImageDataRaw,
5504
6720
  makePaintAlphaMask,
5505
6721
  makePaintBinaryMask,
5506
- makePaintBufferCanvasRenderer,
6722
+ makePaintCursorRenderer,
5507
6723
  makePerfectBlendModeRegistry,
5508
6724
  makePixelCanvas,
6725
+ makePixelData,
6726
+ makePixelTile,
6727
+ makeRectBinaryMaskOutline,
5509
6728
  makeRectFalloffPaintAlphaMask,
5510
6729
  makeReusableCanvas,
5511
6730
  makeReusableImageData,
5512
6731
  makeReusableOffscreenCanvas,
6732
+ makeReusablePixelData,
5513
6733
  merge2BinaryMaskRects,
5514
6734
  mergeAlphaMasks,
5515
6735
  mergeBinaryMaskRects,
@@ -5546,10 +6766,11 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5546
6766
  pixelDataToAlphaMask,
5547
6767
  reflectPixelDataHorizontal,
5548
6768
  reflectPixelDataVertical,
5549
- resample32,
5550
6769
  resampleImageData,
5551
6770
  resampleIndexedImage,
5552
6771
  resamplePixelData,
6772
+ resamplePixelDataInPlace,
6773
+ resampleUint32Array,
5553
6774
  resizeImageData,
5554
6775
  resolveBlitClipping,
5555
6776
  resolveRectClipping,
@@ -5559,8 +6780,15 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5559
6780
  serializeImageData,
5560
6781
  serializeNullableImageData,
5561
6782
  setMaskData,
6783
+ setPixelData,
5562
6784
  softLightFast,
5563
6785
  softLightPerfect,
6786
+ sourceAtopFast,
6787
+ sourceAtopPerfect,
6788
+ sourceInFast,
6789
+ sourceInPerfect,
6790
+ sourceOutFast,
6791
+ sourceOutPerfect,
5564
6792
  sourceOverFast,
5565
6793
  sourceOverPerfect,
5566
6794
  subtractBinaryMaskRects,
@@ -5571,6 +6799,7 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5571
6799
  trimRectBounds,
5572
6800
  uInt32ArrayToImageData,
5573
6801
  uInt32ArrayToImageDataLike,
6802
+ uInt32ArrayToPixelData,
5574
6803
  unpackAlpha,
5575
6804
  unpackBlue,
5576
6805
  unpackColor,
@@ -5584,6 +6813,8 @@ function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn
5584
6813
  writeImageDataToClipboard,
5585
6814
  writeImgBlobToClipboard,
5586
6815
  writePaintBufferToPixelData,
5587
- writePixelDataBuffer
6816
+ writePixelDataBuffer,
6817
+ xorFast,
6818
+ xorPerfect
5588
6819
  });
5589
6820
  //# sourceMappingURL=index.prod.cjs.map