pixel-data-js 0.36.0 → 0.37.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 (72) hide show
  1. package/dist/index.prod.cjs +372 -303
  2. package/dist/index.prod.cjs.map +1 -1
  3. package/dist/index.prod.d.ts +109 -67
  4. package/dist/index.prod.js +368 -302
  5. package/dist/index.prod.js.map +1 -1
  6. package/package.json +2 -2
  7. package/src/Algorithm/floodFillSelection.ts +3 -2
  8. package/src/BlendModes/blend-modes-fast.ts +2 -1
  9. package/src/BlendModes/blend-modes-perfect.ts +2 -1
  10. package/src/Canvas/ReusableCanvas.ts +0 -5
  11. package/src/Color/_color-types.ts +8 -0
  12. package/src/Color/colorDistance.ts +9 -0
  13. package/src/Color/convert-color.ts +43 -0
  14. package/src/Color/lerpColor32.ts +44 -0
  15. package/src/Color/pack-color.ts +38 -0
  16. package/src/History/HistoryAction.ts +2 -2
  17. package/src/History/PixelAccumulator.ts +13 -15
  18. package/src/History/PixelMutator/mutatorBlendColor.ts +2 -1
  19. package/src/History/PixelMutator/mutatorBlendColorPaintAlphaMask.ts +1 -1
  20. package/src/History/PixelMutator/mutatorBlendColorPaintBinaryMask.ts +1 -1
  21. package/src/History/PixelMutator/mutatorBlendColorPaintMask.ts +23 -8
  22. package/src/History/PixelMutator/mutatorBlendColorPaintRect.ts +2 -1
  23. package/src/History/PixelMutator/mutatorBlendPixel.ts +2 -1
  24. package/src/History/PixelMutator/mutatorClear.ts +1 -1
  25. package/src/History/PixelMutator/mutatorFill.ts +1 -1
  26. package/src/History/PixelMutator/mutatorFillBinaryMask.ts +1 -1
  27. package/src/History/PixelWriter.ts +5 -5
  28. package/src/IndexedImage/IndexedImage.ts +1 -1
  29. package/src/IndexedImage/indexedImageToAverageColor.ts +3 -2
  30. package/src/Mask/_mask-types.ts +9 -0
  31. package/src/Paint/AlphaMaskPaintBuffer.ts +26 -26
  32. package/src/Paint/BinaryMaskPaintBuffer.ts +19 -19
  33. package/src/Paint/ColorPaintBuffer.ts +40 -42
  34. package/src/Paint/Commit/AlphaMaskPaintBufferCommitter.ts +1 -1
  35. package/src/Paint/Commit/AlphaMaskPaintBufferManager.ts +6 -7
  36. package/src/Paint/Commit/BinaryMaskPaintBufferCommitter.ts +1 -1
  37. package/src/Paint/Commit/BinaryMaskPaintBufferManager.ts +6 -7
  38. package/src/Paint/Commit/ColorPaintBufferManager.ts +6 -7
  39. package/src/Paint/Commit/commitColorPaintBuffer.ts +2 -6
  40. package/src/Paint/Commit/commitMaskPaintBuffer.ts +3 -7
  41. package/src/Paint/Render/AlphaMaskPaintBufferCanvasRenderer.ts +42 -25
  42. package/src/Paint/Render/BinaryMaskPaintBufferCanvasRenderer.ts +40 -24
  43. package/src/Paint/Render/ColorPaintBufferCanvasRenderer.ts +21 -21
  44. package/src/Paint/Render/PaintCursorRenderer.ts +3 -2
  45. package/src/Paint/eachTileInBounds.ts +9 -10
  46. package/src/PixelData/blendColorPixelData.ts +2 -1
  47. package/src/PixelData/blendColorPixelDataAlphaMask.ts +2 -1
  48. package/src/PixelData/blendColorPixelDataBinaryMask.ts +2 -1
  49. package/src/PixelData/blendColorPixelDataMask.ts +2 -1
  50. package/src/PixelData/blendColorPixelDataPaintAlphaMask.ts +1 -1
  51. package/src/PixelData/blendColorPixelDataPaintBinaryMask.ts +1 -1
  52. package/src/PixelData/blendColorPixelDataPaintMask.ts +19 -8
  53. package/src/PixelData/blendPixel.ts +2 -1
  54. package/src/PixelData/blendPixelData.ts +2 -1
  55. package/src/PixelData/blendPixelDataAlphaMask.ts +2 -1
  56. package/src/PixelData/blendPixelDataBinaryMask.ts +2 -1
  57. package/src/PixelData/blendPixelDataPaintBuffer.ts +2 -3
  58. package/src/PixelData/clearPixelDataFast.ts +1 -1
  59. package/src/PixelData/fillPixelData.ts +1 -1
  60. package/src/PixelData/fillPixelDataBinaryMask.ts +1 -1
  61. package/src/PixelData/fillPixelDataFast.ts +1 -1
  62. package/src/PixelData/writePaintBufferToPixelData.ts +1 -5
  63. package/src/Tile/MaskTile.ts +4 -0
  64. package/src/Tile/PixelTile.ts +2 -0
  65. package/src/Tile/TilePool.ts +9 -8
  66. package/src/Tile/TileTargetConfig.ts +27 -0
  67. package/src/Tile/_tile-types.ts +16 -0
  68. package/src/_types.ts +1 -6
  69. package/src/index.ts +7 -3
  70. package/src/History/PixelEngineConfig.ts +0 -28
  71. package/src/Internal/_constants.ts +0 -3
  72. package/src/color.ts +0 -112
@@ -32,7 +32,6 @@ __export(src_exports, {
32
32
  MaskType: () => MaskType,
33
33
  PaintMaskOutline: () => PaintMaskOutline,
34
34
  PixelAccumulator: () => PixelAccumulator,
35
- PixelEngineConfig: () => PixelEngineConfig,
36
35
  PixelWriter: () => PixelWriter,
37
36
  TilePool: () => TilePool,
38
37
  TileType: () => TileType,
@@ -59,6 +58,7 @@ __export(src_exports, {
59
58
  blendPixelDataPaintBuffer: () => blendPixelDataPaintBuffer,
60
59
  clearPixelDataFast: () => clearPixelDataFast,
61
60
  color32ToCssRGBA: () => color32ToCssRGBA,
61
+ color32ToCssRGBAString: () => color32ToCssRGBAString,
62
62
  color32ToHex: () => color32ToHex,
63
63
  colorBurnFast: () => colorBurnFast,
64
64
  colorBurnPerfect: () => colorBurnPerfect,
@@ -72,6 +72,7 @@ __export(src_exports, {
72
72
  copyMask: () => copyMask,
73
73
  copyPixelData: () => copyPixelData,
74
74
  cropPixelData: () => cropPixelData,
75
+ cssRGBAToColor32: () => cssRGBAToColor32,
75
76
  darkenFast: () => darkenFast,
76
77
  darkenPerfect: () => darkenPerfect,
77
78
  darkerFast: () => darkerFast,
@@ -184,6 +185,8 @@ __export(src_exports, {
184
185
  makeReusableImageData: () => makeReusableImageData,
185
186
  makeReusableOffscreenCanvas: () => makeReusableOffscreenCanvas,
186
187
  makeReusablePixelData: () => makeReusablePixelData,
188
+ makeTileTargetConfig: () => makeTileTargetConfig,
189
+ makeTileTargetMeta: () => makeTileTargetMeta,
187
190
  merge2BinaryMaskRects: () => merge2BinaryMaskRects,
188
191
  mergeAlphaMasks: () => mergeAlphaMasks,
189
192
  mergeBinaryMaskRects: () => mergeBinaryMaskRects,
@@ -283,43 +286,7 @@ __export(errors_exports, {
283
286
  var OFFSCREEN_CANVAS_CTX_FAILED = "Failed to create OffscreenCanvas context";
284
287
  var CANVAS_CTX_FAILED = "Failed to create Canvas context";
285
288
 
286
- // src/color.ts
287
- function packColor(r, g, b, a) {
288
- return (a << 24 | b << 16 | g << 8 | r) >>> 0;
289
- }
290
- function packRGBA({
291
- r,
292
- g,
293
- b,
294
- a
295
- }) {
296
- return (a << 24 | b << 16 | g << 8 | r) >>> 0;
297
- }
298
- var unpackRed = (packed) => packed >>> 0 & 255;
299
- var unpackGreen = (packed) => packed >>> 8 & 255;
300
- var unpackBlue = (packed) => packed >>> 16 & 255;
301
- var unpackAlpha = (packed) => packed >>> 24 & 255;
302
- function unpackColor(packed) {
303
- return {
304
- r: packed >>> 0 & 255,
305
- g: packed >>> 8 & 255,
306
- b: packed >>> 16 & 255,
307
- a: packed >>> 24 & 255
308
- };
309
- }
310
- var SCRATCH_RGBA = {
311
- r: 0,
312
- g: 0,
313
- b: 0,
314
- a: 0
315
- };
316
- function unpackColorTo(packed, scratch = SCRATCH_RGBA) {
317
- scratch.r = packed >>> 0 & 255;
318
- scratch.g = packed >>> 8 & 255;
319
- scratch.b = packed >>> 16 & 255;
320
- scratch.a = packed >>> 24 & 255;
321
- return scratch;
322
- }
289
+ // src/Color/colorDistance.ts
323
290
  function colorDistance(a, b) {
324
291
  const dr = (a & 255) - (b & 255);
325
292
  const dg = (a >>> 8 & 255) - (b >>> 8 & 255);
@@ -327,34 +294,6 @@ function colorDistance(a, b) {
327
294
  const da = (a >>> 24 & 255) - (b >>> 24 & 255);
328
295
  return dr * dr + dg * dg + db * db + da * da;
329
296
  }
330
- function lerpColor32(a, b, t) {
331
- const r = (a & 255) + t * ((b & 255) - (a & 255));
332
- const g = (a >>> 8 & 255) + t * ((b >>> 8 & 255) - (a >>> 8 & 255));
333
- const b_ = (a >>> 16 & 255) + t * ((b >>> 16 & 255) - (a >>> 16 & 255));
334
- const a_ = (a >>> 24 & 255) + t * ((b >>> 24 & 255) - (a >>> 24 & 255));
335
- return (a_ << 24 | b_ << 16 | g << 8 | r) >>> 0;
336
- }
337
- function lerpColor32Fast(src, dst, w) {
338
- const invA = 255 - w;
339
- const rb = (src & 16711935) * w + (dst & 16711935) * invA >>> 8 & 16711935;
340
- const ga = (src >>> 8 & 16711935) * w + (dst >>> 8 & 16711935) * invA >>> 8 & 16711935;
341
- return (rb | ga << 8) >>> 0;
342
- }
343
- function color32ToHex(color) {
344
- const r = (color & 255).toString(16).padStart(2, "0");
345
- const g = (color >>> 8 & 255).toString(16).padStart(2, "0");
346
- const b = (color >>> 16 & 255).toString(16).padStart(2, "0");
347
- const a = (color >>> 24 & 255).toString(16).padStart(2, "0");
348
- return `#${r}${g}${b}${a}`;
349
- }
350
- function color32ToCssRGBA(color) {
351
- const r = color & 255;
352
- const g = color >>> 8 & 255;
353
- const b = color >>> 16 & 255;
354
- const a = color >>> 24 & 255;
355
- const alpha = Number((a / 255).toFixed(3));
356
- return `rgba(${r},${g},${b},${alpha})`;
357
- }
358
297
 
359
298
  // src/ImageData/extractImageDataBuffer.ts
360
299
  function extractImageDataBuffer(imageData, _x, _y, _w, _h) {
@@ -2115,9 +2054,6 @@ function makeReusableCanvasMeta(factory) {
2115
2054
  canvas.width = width;
2116
2055
  canvas.height = height;
2117
2056
  ctx.imageSmoothingEnabled = false;
2118
- } else {
2119
- ctx.setTransform(1, 0, 0, 1, 0, 0);
2120
- ctx.clearRect(0, 0, width, height);
2121
2057
  }
2122
2058
  return result;
2123
2059
  }
@@ -2238,6 +2174,96 @@ async function writeImageDataToClipboard(imageData) {
2238
2174
  return writeImgBlobToClipboard(blob);
2239
2175
  }
2240
2176
 
2177
+ // src/Color/lerpColor32.ts
2178
+ function lerpColor32(a, b, t) {
2179
+ const r = (a & 255) + t * ((b & 255) - (a & 255));
2180
+ const g = (a >>> 8 & 255) + t * ((b >>> 8 & 255) - (a >>> 8 & 255));
2181
+ const b_ = (a >>> 16 & 255) + t * ((b >>> 16 & 255) - (a >>> 16 & 255));
2182
+ const a_ = (a >>> 24 & 255) + t * ((b >>> 24 & 255) - (a >>> 24 & 255));
2183
+ return (a_ << 24 | b_ << 16 | g << 8 | r) >>> 0;
2184
+ }
2185
+ function lerpColor32Fast(src, dst, w) {
2186
+ const invA = 255 - w;
2187
+ const rb = (src & 16711935) * w + (dst & 16711935) * invA >>> 8 & 16711935;
2188
+ const ga = (src >>> 8 & 16711935) * w + (dst >>> 8 & 16711935) * invA >>> 8 & 16711935;
2189
+ return (rb | ga << 8) >>> 0;
2190
+ }
2191
+
2192
+ // src/Color/convert-color.ts
2193
+ function color32ToHex(color) {
2194
+ const r = (color & 255).toString(16).padStart(2, "0");
2195
+ const g = (color >>> 8 & 255).toString(16).padStart(2, "0");
2196
+ const b = (color >>> 16 & 255).toString(16).padStart(2, "0");
2197
+ const a = (color >>> 24 & 255).toString(16).padStart(2, "0");
2198
+ return `#${r}${g}${b}${a}`;
2199
+ }
2200
+ function color32ToCssRGBAString(color) {
2201
+ const r = color & 255;
2202
+ const g = color >>> 8 & 255;
2203
+ const b = color >>> 16 & 255;
2204
+ const a = color >>> 24 & 255;
2205
+ const alpha = Number((a / 255).toFixed(3));
2206
+ return `rgba(${r},${g},${b},${alpha})`;
2207
+ }
2208
+ function color32ToCssRGBA(color) {
2209
+ const r = color & 255;
2210
+ const g = color >>> 8 & 255;
2211
+ const b = color >>> 16 & 255;
2212
+ const a = color >>> 24 & 255;
2213
+ return {
2214
+ r,
2215
+ g,
2216
+ b,
2217
+ a: a / 255
2218
+ };
2219
+ }
2220
+ function cssRGBAToColor32({
2221
+ r,
2222
+ g,
2223
+ b,
2224
+ a
2225
+ }) {
2226
+ return (a * 255 << 24 | b << 16 | g << 8 | r) >>> 0;
2227
+ }
2228
+
2229
+ // src/Color/pack-color.ts
2230
+ function packColor(r, g, b, a) {
2231
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
2232
+ }
2233
+ function packRGBA({
2234
+ r,
2235
+ g,
2236
+ b,
2237
+ a
2238
+ }) {
2239
+ return (a << 24 | b << 16 | g << 8 | r) >>> 0;
2240
+ }
2241
+ var unpackRed = (packed) => packed >>> 0 & 255;
2242
+ var unpackGreen = (packed) => packed >>> 8 & 255;
2243
+ var unpackBlue = (packed) => packed >>> 16 & 255;
2244
+ var unpackAlpha = (packed) => packed >>> 24 & 255;
2245
+ function unpackColor(packed) {
2246
+ return {
2247
+ r: packed >>> 0 & 255,
2248
+ g: packed >>> 8 & 255,
2249
+ b: packed >>> 16 & 255,
2250
+ a: packed >>> 24 & 255
2251
+ };
2252
+ }
2253
+ var SCRATCH_RGBA = {
2254
+ r: 0,
2255
+ g: 0,
2256
+ b: 0,
2257
+ a: 0
2258
+ };
2259
+ function unpackColorTo(packed, scratch = SCRATCH_RGBA) {
2260
+ scratch.r = packed >>> 0 & 255;
2261
+ scratch.g = packed >>> 8 & 255;
2262
+ scratch.b = packed >>> 16 & 255;
2263
+ scratch.a = packed >>> 24 & 255;
2264
+ return scratch;
2265
+ }
2266
+
2241
2267
  // src/Control/BatchedQueue.ts
2242
2268
  function makeBatchedQueue(processor, queue) {
2243
2269
  let activeSet = /* @__PURE__ */ new Set();
@@ -2417,15 +2443,14 @@ var PixelAccumulator = class {
2417
2443
  * @param y pixel y coordinate
2418
2444
  */
2419
2445
  storePixelBeforeState(x, y) {
2420
- const shift = this.config.tileShift;
2421
2446
  const columns = this.config.targetColumns;
2422
- const targetWidth = this.config.target.w;
2423
- const targetHeight = this.config.target.h;
2447
+ const targetWidth = this.config.targetWidth;
2448
+ const targetHeight = this.config.targetHeight;
2424
2449
  if (x < 0 || x >= targetWidth || y < 0 || y >= targetHeight) {
2425
2450
  return null;
2426
2451
  }
2427
- const tx = x >> shift;
2428
- const ty = y >> shift;
2452
+ const tx = x * this.config.invTileSize | 0;
2453
+ const ty = y * this.config.invTileSize | 0;
2429
2454
  const id = ty * columns + tx;
2430
2455
  let tile = this.lookup[id];
2431
2456
  let added = false;
@@ -2452,10 +2477,10 @@ var PixelAccumulator = class {
2452
2477
  * @param h pixel height
2453
2478
  */
2454
2479
  storeRegionBeforeState(x, y, w, h) {
2455
- const shift = this.config.tileShift;
2456
2480
  const columns = this.config.targetColumns;
2457
- const targetWidth = this.config.target.w;
2458
- const targetHeight = this.config.target.h;
2481
+ const targetWidth = this.config.targetWidth;
2482
+ const targetHeight = this.config.targetHeight;
2483
+ const invTileSize = this.config.invTileSize;
2459
2484
  const clipX1 = Math.max(0, x);
2460
2485
  const clipY1 = Math.max(0, y);
2461
2486
  const clipX2 = Math.min(targetWidth - 1, x + w - 1);
@@ -2463,10 +2488,10 @@ var PixelAccumulator = class {
2463
2488
  if (clipX2 < clipX1 || clipY2 < clipY1) {
2464
2489
  return null;
2465
2490
  }
2466
- const startX = clipX1 >> shift;
2467
- const startY = clipY1 >> shift;
2468
- const endX = clipX2 >> shift;
2469
- const endY = clipY2 >> shift;
2491
+ const startX = clipX1 * invTileSize | 0;
2492
+ const startY = clipY1 * invTileSize | 0;
2493
+ const endX = clipX2 * invTileSize | 0;
2494
+ const endY = clipY2 * invTileSize | 0;
2470
2495
  const startIndex = this.beforeTiles.length;
2471
2496
  for (let ty = startY; ty <= endY; ty++) {
2472
2497
  for (let tx = startX; tx <= endX; tx++) {
@@ -2583,31 +2608,6 @@ var PixelAccumulator = class {
2583
2608
  }
2584
2609
  };
2585
2610
 
2586
- // src/History/PixelEngineConfig.ts
2587
- var PixelEngineConfig = class {
2588
- tileSize;
2589
- // pixelX = tileX << tileShift
2590
- // pixelY = tileY << tileShift
2591
- tileShift;
2592
- tileMask;
2593
- tileArea;
2594
- target;
2595
- targetColumns = 0;
2596
- targetRows = 0;
2597
- constructor(tileSize, target) {
2598
- if ((tileSize & tileSize - 1) !== 0) {
2599
- throw new Error("tileSize must be a power of 2");
2600
- }
2601
- this.tileSize = tileSize;
2602
- this.tileShift = 31 - Math.clz32(tileSize);
2603
- this.tileMask = tileSize - 1;
2604
- this.tileArea = tileSize * tileSize;
2605
- this.target = target;
2606
- this.targetColumns = target.w + this.tileMask >> this.tileShift;
2607
- this.targetRows = target.h + this.tileMask >> this.tileShift;
2608
- }
2609
- };
2610
-
2611
2611
  // src/PixelData/applyAlphaMaskToPixelData.ts
2612
2612
  function applyAlphaMaskToPixelData(target, mask, opts) {
2613
2613
  const targetX = opts?.x ?? 0;
@@ -2801,6 +2801,8 @@ function makePixelTile(id, tx, ty, tileSize, tileArea) {
2801
2801
  id,
2802
2802
  tx,
2803
2803
  ty,
2804
+ x: tx * tileSize,
2805
+ y: ty * tileSize,
2804
2806
  w: tileSize,
2805
2807
  h: tileSize,
2806
2808
  data: data32,
@@ -2810,25 +2812,28 @@ function makePixelTile(id, tx, ty, tileSize, tileArea) {
2810
2812
 
2811
2813
  // src/Tile/TilePool.ts
2812
2814
  var TilePool = class {
2813
- constructor(config, tileFactory) {
2815
+ constructor(tileSize, tileFactory) {
2816
+ this.tileSize = tileSize;
2814
2817
  this.tileFactory = tileFactory;
2815
2818
  this.pool = [];
2816
- this.tileSize = config.tileSize;
2817
- this.tileArea = config.tileArea;
2819
+ this.tileSize = tileSize;
2820
+ this.tileArea = tileSize * tileSize;
2818
2821
  }
2819
2822
  pool;
2820
- tileSize;
2821
2823
  tileArea;
2822
2824
  getTile(id, tx, ty) {
2823
2825
  let tile = this.pool.pop();
2826
+ const tileSize = this.tileSize;
2824
2827
  if (tile) {
2825
2828
  tile.id = id;
2826
2829
  tile.tx = tx;
2827
2830
  tile.ty = ty;
2831
+ tile.x = tx * tileSize;
2832
+ tile.y = ty * tileSize;
2828
2833
  tile.data.fill(0);
2829
2834
  return tile;
2830
2835
  }
2831
- return this.tileFactory(id, tx, ty, this.tileSize, this.tileArea);
2836
+ return this.tileFactory(id, tx, ty, tileSize, this.tileArea);
2832
2837
  }
2833
2838
  releaseTile(tile) {
2834
2839
  this.pool.push(tile);
@@ -2845,6 +2850,25 @@ var TilePool = class {
2845
2850
  }
2846
2851
  };
2847
2852
 
2853
+ // src/Tile/TileTargetConfig.ts
2854
+ function makeTileTargetConfig(tileSize, target) {
2855
+ return {
2856
+ target,
2857
+ ...makeTileTargetMeta(tileSize, target)
2858
+ };
2859
+ }
2860
+ function makeTileTargetMeta(tileSize, target) {
2861
+ return {
2862
+ targetWidth: target.w,
2863
+ targetHeight: target.h,
2864
+ tileSize,
2865
+ invTileSize: 1 / tileSize,
2866
+ tileArea: tileSize * tileSize,
2867
+ targetColumns: Math.ceil(target.w / tileSize),
2868
+ targetRows: Math.ceil(target.h / tileSize)
2869
+ };
2870
+ }
2871
+
2848
2872
  // src/History/PixelWriter.ts
2849
2873
  var PixelWriter = class {
2850
2874
  historyManager;
@@ -2857,10 +2881,10 @@ var PixelWriter = class {
2857
2881
  constructor(target, mutatorFactory, options) {
2858
2882
  const tileSize = options?.tileSize ?? 256;
2859
2883
  const maxHistorySteps = options?.maxHistorySteps ?? 50;
2860
- this.config = new PixelEngineConfig(tileSize, target);
2884
+ this.config = makeTileTargetConfig(tileSize, target);
2861
2885
  this.historyManager = options?.historyManager ?? new HistoryManager(maxHistorySteps);
2862
2886
  this.historyActionFactory = options?.historyActionFactory ?? makeHistoryAction;
2863
- this.pixelTilePool = options?.pixelTilePool ?? new TilePool(this.config, makePixelTile);
2887
+ this.pixelTilePool = options?.pixelTilePool ?? new TilePool(this.config.tileSize, makePixelTile);
2864
2888
  this.accumulator = options?.accumulator ?? new PixelAccumulator(this.config, this.pixelTilePool);
2865
2889
  this.mutator = mutatorFactory(this);
2866
2890
  }
@@ -3639,18 +3663,22 @@ var mutatorBlendColorPaintBinaryMask = ((writer, deps = defaults8) => {
3639
3663
  // src/History/PixelMutator/mutatorBlendColorPaintMask.ts
3640
3664
  var defaults9 = {
3641
3665
  blendColorPixelDataAlphaMask,
3642
- blendColorPixelDataBinaryMask
3666
+ blendColorPixelDataBinaryMask,
3667
+ blendColorPixelData
3643
3668
  };
3644
3669
  var mutatorBlendColorPaintMask = ((writer, deps = defaults9) => {
3645
3670
  const {
3646
3671
  blendColorPixelDataBinaryMask: blendColorPixelDataBinaryMask2 = defaults9.blendColorPixelDataBinaryMask,
3647
- blendColorPixelDataAlphaMask: blendColorPixelDataAlphaMask2 = defaults9.blendColorPixelDataAlphaMask
3672
+ blendColorPixelDataAlphaMask: blendColorPixelDataAlphaMask2 = defaults9.blendColorPixelDataAlphaMask,
3673
+ blendColorPixelData: blendColorPixelData2 = defaults9.blendColorPixelData
3648
3674
  } = deps;
3649
3675
  const OPTS = {
3650
3676
  x: 0,
3651
3677
  y: 0,
3652
3678
  blendFn: sourceOverPerfect,
3653
- alpha: 255
3679
+ alpha: 255,
3680
+ w: void 0,
3681
+ h: void 0
3654
3682
  };
3655
3683
  return {
3656
3684
  blendColorPaintMask(color, mask, x, y, alpha = 255, blendFn = sourceOverPerfect) {
@@ -3662,11 +3690,17 @@ var mutatorBlendColorPaintMask = ((writer, deps = defaults9) => {
3662
3690
  OPTS.y = ty;
3663
3691
  OPTS.alpha = alpha;
3664
3692
  OPTS.blendFn = blendFn;
3665
- if (mask.type === 1 /* BINARY */) {
3666
- return didChange(blendColorPixelDataBinaryMask2(writer.config.target, color, mask, OPTS));
3667
- } else {
3693
+ OPTS.w = void 0;
3694
+ OPTS.h = void 0;
3695
+ if (mask.data) {
3696
+ if (mask.type === 1 /* BINARY */) {
3697
+ return didChange(blendColorPixelDataBinaryMask2(writer.config.target, color, mask, OPTS));
3698
+ }
3668
3699
  return didChange(blendColorPixelDataAlphaMask2(writer.config.target, color, mask, OPTS));
3669
3700
  }
3701
+ OPTS.w = mask.w;
3702
+ OPTS.h = mask.h;
3703
+ return didChange(blendColorPixelData2(writer.config.target, color, OPTS));
3670
3704
  }
3671
3705
  };
3672
3706
  });
@@ -5355,26 +5389,55 @@ function trimRectBounds(x, y, w, h, targetWidth, targetHeight, out) {
5355
5389
  return res;
5356
5390
  }
5357
5391
 
5392
+ // src/Tile/MaskTile.ts
5393
+ var makeAlphaMaskTile = (id, tx, ty, tileSize, tileArea) => {
5394
+ return {
5395
+ tileType: 1 /* MASK */,
5396
+ type: 0 /* ALPHA */,
5397
+ data: new Uint8Array(tileArea),
5398
+ w: tileSize,
5399
+ h: tileSize,
5400
+ x: tx * tileSize,
5401
+ y: ty * tileSize,
5402
+ id,
5403
+ tx,
5404
+ ty
5405
+ };
5406
+ };
5407
+ var makeBinaryMaskTile = (id, tx, ty, tileSize, tileArea) => {
5408
+ return {
5409
+ tileType: 1 /* MASK */,
5410
+ type: 1 /* BINARY */,
5411
+ data: new Uint8Array(tileArea),
5412
+ w: tileSize,
5413
+ h: tileSize,
5414
+ x: tx * tileSize,
5415
+ y: ty * tileSize,
5416
+ id,
5417
+ tx,
5418
+ ty
5419
+ };
5420
+ };
5421
+
5358
5422
  // src/Paint/eachTileInBounds.ts
5359
5423
  function eachTileInBounds(config, lookup, tilePool, bounds, callback) {
5360
5424
  const {
5361
- tileShift,
5362
- targetColumns,
5363
5425
  targetRows,
5426
+ targetColumns,
5364
5427
  tileSize
5365
5428
  } = config;
5366
- const x1 = Math.max(0, bounds.x >> tileShift);
5367
- const y1 = Math.max(0, bounds.y >> tileShift);
5368
- const x2 = Math.min(targetColumns - 1, bounds.x + bounds.w - 1 >> tileShift);
5369
- const y2 = Math.min(targetRows - 1, bounds.y + bounds.h - 1 >> tileShift);
5429
+ const x1 = Math.max(0, Math.floor(bounds.x / tileSize));
5430
+ const y1 = Math.max(0, Math.floor(bounds.y / tileSize));
5431
+ const x2 = Math.min(targetColumns - 1, Math.floor((bounds.x + bounds.w - 1) / tileSize));
5432
+ const y2 = Math.min(targetRows - 1, Math.floor((bounds.y + bounds.h - 1) / tileSize));
5370
5433
  if (x1 > x2 || y1 > y2) return;
5371
5434
  for (let ty = y1; ty <= y2; ty++) {
5372
5435
  const rowOffset = ty * targetColumns;
5373
- const tileTop = ty << tileShift;
5436
+ const tileTop = ty * tileSize;
5374
5437
  for (let tx = x1; tx <= x2; tx++) {
5375
5438
  const id = rowOffset + tx;
5376
5439
  const tile = lookup[id] ?? (lookup[id] = tilePool.getTile(id, tx, ty));
5377
- const tileLeft = tx << tileShift;
5440
+ const tileLeft = tx * tileSize;
5378
5441
  const startX = bounds.x > tileLeft ? bounds.x : tileLeft;
5379
5442
  const startY = bounds.y > tileTop ? bounds.y : tileTop;
5380
5443
  const maskEndX = bounds.x + bounds.w;
@@ -5390,7 +5453,7 @@ function eachTileInBounds(config, lookup, tilePool, bounds, callback) {
5390
5453
 
5391
5454
  // src/Paint/AlphaMaskPaintBuffer.ts
5392
5455
  var AlphaMaskPaintBuffer = class {
5393
- constructor(config, tilePool) {
5456
+ constructor(config, tilePool = new TilePool(config.tileSize, makeAlphaMaskTile)) {
5394
5457
  this.config = config;
5395
5458
  this.tilePool = tilePool;
5396
5459
  this.lookup = [];
@@ -5410,9 +5473,9 @@ var AlphaMaskPaintBuffer = class {
5410
5473
  const lookup = this.lookup;
5411
5474
  const tilePool = this.tilePool;
5412
5475
  const config = this.config;
5413
- const tileShift = config.tileShift;
5414
- const tileMask = config.tileMask;
5415
- const target = config.target;
5476
+ const targetW = config.targetWidth;
5477
+ const targetH = config.targetHeight;
5478
+ const tileSize = config.tileSize;
5416
5479
  const {
5417
5480
  w: bW,
5418
5481
  h: bH,
@@ -5426,7 +5489,7 @@ var AlphaMaskPaintBuffer = class {
5426
5489
  this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5427
5490
  const topLeftX = Math.floor(px + centerOffsetX);
5428
5491
  const topLeftY = Math.floor(py + centerOffsetY);
5429
- trimRectBoundsFn(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5492
+ trimRectBoundsFn(topLeftX, topLeftY, bW, bH, targetW, targetH, scratch);
5430
5493
  if (scratch.w <= 0 || scratch.h <= 0) return;
5431
5494
  eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5432
5495
  const data = tile.data;
@@ -5434,8 +5497,8 @@ var AlphaMaskPaintBuffer = class {
5434
5497
  for (let i = 0; i < bH_t; i++) {
5435
5498
  const canvasY = bY + i;
5436
5499
  const bOff = (canvasY - topLeftY) * bW;
5437
- const tOff = (canvasY & tileMask) << tileShift;
5438
- const dS = tOff + (bX & tileMask);
5500
+ const tOff = (canvasY - tile.y) * tileSize;
5501
+ const dS = tOff + (bX - tile.x);
5439
5502
  for (let j = 0; j < bW_t; j++) {
5440
5503
  const canvasX = bX + j;
5441
5504
  const brushA = bD[bOff + (canvasX - topLeftX)];
@@ -5458,9 +5521,9 @@ var AlphaMaskPaintBuffer = class {
5458
5521
  const lookup = this.lookup;
5459
5522
  const tilePool = this.tilePool;
5460
5523
  const config = this.config;
5461
- const tileShift = config.tileShift;
5462
- const tileMask = config.tileMask;
5463
- const target = config.target;
5524
+ const targetW = config.targetWidth;
5525
+ const targetH = config.targetHeight;
5526
+ const tileSize = config.tileSize;
5464
5527
  const {
5465
5528
  w: bW,
5466
5529
  h: bH,
@@ -5474,7 +5537,7 @@ var AlphaMaskPaintBuffer = class {
5474
5537
  this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5475
5538
  const topLeftX = Math.floor(px + centerOffsetX);
5476
5539
  const topLeftY = Math.floor(py + centerOffsetY);
5477
- trimRectBoundsFn(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5540
+ trimRectBoundsFn(topLeftX, topLeftY, bW, bH, targetW, targetH, scratch);
5478
5541
  if (scratch.w <= 0 || scratch.h <= 0) return;
5479
5542
  eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5480
5543
  const data = tile.data;
@@ -5482,8 +5545,8 @@ var AlphaMaskPaintBuffer = class {
5482
5545
  for (let i = 0; i < bH_t; i++) {
5483
5546
  const canvasY = bY + i;
5484
5547
  const bOff = (canvasY - topLeftY) * bW;
5485
- const tOff = (canvasY & tileMask) << tileShift;
5486
- const dS = tOff + (bX & tileMask);
5548
+ const tOff = (canvasY - tile.y) * tileSize;
5549
+ const dS = tOff + (bX - tile.x);
5487
5550
  for (let j = 0; j < bW_t; j++) {
5488
5551
  const canvasX = bX + j;
5489
5552
  if (bD[bOff + (canvasX - topLeftX)]) {
@@ -5505,9 +5568,9 @@ var AlphaMaskPaintBuffer = class {
5505
5568
  const lookup = this.lookup;
5506
5569
  const tilePool = this.tilePool;
5507
5570
  const config = this.config;
5508
- const tileShift = config.tileShift;
5509
- const tileMask = config.tileMask;
5510
- const target = config.target;
5571
+ const targetW = config.targetWidth;
5572
+ const targetH = config.targetHeight;
5573
+ const tileSize = config.tileSize;
5511
5574
  const brushWidth = brush.w;
5512
5575
  const brushHeight = brush.h;
5513
5576
  const centerOffsetX = brush.centerOffsetX;
@@ -5518,15 +5581,15 @@ var AlphaMaskPaintBuffer = class {
5518
5581
  this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5519
5582
  const topLeftX = Math.floor(px + centerOffsetX);
5520
5583
  const topLeftY = Math.floor(py + centerOffsetY);
5521
- trimRectBoundsFn(topLeftX, topLeftY, brushWidth, brushHeight, target.w, target.h, scratch);
5584
+ trimRectBoundsFn(topLeftX, topLeftY, brushWidth, brushHeight, targetW, targetH, scratch);
5522
5585
  if (scratch.w <= 0 || scratch.h <= 0) return;
5523
5586
  eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5524
5587
  const data = tile.data;
5525
5588
  let tileChanged = false;
5526
5589
  for (let i = 0; i < bH_t; i++) {
5527
5590
  const canvasY = bY + i;
5528
- const tOff = (canvasY & tileMask) << tileShift;
5529
- const dS = tOff + (bX & tileMask);
5591
+ const tOff = (canvasY - tile.y) * tileSize;
5592
+ const dS = tOff + (bX - tile.x);
5530
5593
  for (let j = 0; j < bW_t; j++) {
5531
5594
  const idx = dS + j;
5532
5595
  if (alpha > data[idx]) {
@@ -5549,7 +5612,7 @@ var AlphaMaskPaintBuffer = class {
5549
5612
 
5550
5613
  // src/Paint/BinaryMaskPaintBuffer.ts
5551
5614
  var BinaryMaskPaintBuffer = class {
5552
- constructor(config, tilePool) {
5615
+ constructor(config, tilePool = new TilePool(config.tileSize, makeBinaryMaskTile)) {
5553
5616
  this.config = config;
5554
5617
  this.tilePool = tilePool;
5555
5618
  this.lookup = [];
@@ -5569,9 +5632,9 @@ var BinaryMaskPaintBuffer = class {
5569
5632
  const lookup = this.lookup;
5570
5633
  const tilePool = this.tilePool;
5571
5634
  const config = this.config;
5572
- const tileShift = config.tileShift;
5573
- const tileMask = config.tileMask;
5574
- const target = config.target;
5635
+ const targetW = config.targetWidth;
5636
+ const targetH = config.targetHeight;
5637
+ const tileSize = config.tileSize;
5575
5638
  const {
5576
5639
  w: bW,
5577
5640
  h: bH,
@@ -5585,7 +5648,7 @@ var BinaryMaskPaintBuffer = class {
5585
5648
  this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5586
5649
  const topLeftX = Math.floor(px + centerOffsetX);
5587
5650
  const topLeftY = Math.floor(py + centerOffsetY);
5588
- trimRectBoundsFn(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5651
+ trimRectBoundsFn(topLeftX, topLeftY, bW, bH, targetW, targetH, scratch);
5589
5652
  if (scratch.w <= 0 || scratch.h <= 0) return;
5590
5653
  eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5591
5654
  const data = tile.data;
@@ -5593,8 +5656,8 @@ var BinaryMaskPaintBuffer = class {
5593
5656
  for (let i = 0; i < bH_t; i++) {
5594
5657
  const canvasY = bY + i;
5595
5658
  const bOff = (canvasY - topLeftY) * bW;
5596
- const tOff = (canvasY & tileMask) << tileShift;
5597
- const dS = tOff + (bX & tileMask);
5659
+ const tOff = (canvasY - tile.y) * tileSize;
5660
+ const dS = tOff + (bX - tile.x);
5598
5661
  for (let j = 0; j < bW_t; j++) {
5599
5662
  const canvasX = bX + j;
5600
5663
  if (bD[bOff + (canvasX - topLeftX)]) {
@@ -5616,9 +5679,9 @@ var BinaryMaskPaintBuffer = class {
5616
5679
  const lookup = this.lookup;
5617
5680
  const tilePool = this.tilePool;
5618
5681
  const config = this.config;
5619
- const tileShift = config.tileShift;
5620
- const tileMask = config.tileMask;
5621
- const target = config.target;
5682
+ const targetW = config.targetWidth;
5683
+ const targetH = config.targetHeight;
5684
+ const tileSize = config.tileSize;
5622
5685
  const brushWidth = brush.w;
5623
5686
  const brushHeight = brush.h;
5624
5687
  const centerOffsetX = brush.centerOffsetX;
@@ -5629,15 +5692,15 @@ var BinaryMaskPaintBuffer = class {
5629
5692
  this.forEachLinePointFn(x0, y0, x1, y1, (px, py) => {
5630
5693
  const topLeftX = Math.floor(px + centerOffsetX);
5631
5694
  const topLeftY = Math.floor(py + centerOffsetY);
5632
- trimRectBoundsFn(topLeftX, topLeftY, brushWidth, brushHeight, target.w, target.h, scratch);
5695
+ trimRectBoundsFn(topLeftX, topLeftY, brushWidth, brushHeight, targetW, targetH, scratch);
5633
5696
  if (scratch.w <= 0 || scratch.h <= 0) return;
5634
5697
  eachTileInBoundsFn(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5635
5698
  const data = tile.data;
5636
5699
  let tileChanged = false;
5637
5700
  for (let i = 0; i < bH_t; i++) {
5638
5701
  const canvasY = bY + i;
5639
- const tOff = (canvasY & tileMask) << tileShift;
5640
- const dS = tOff + (bX & tileMask);
5702
+ const tOff = (canvasY - tile.y) * tileSize;
5703
+ const dS = tOff + (bX - tile.x);
5641
5704
  for (let j = 0; j < bW_t; j++) {
5642
5705
  const idx = dS + j;
5643
5706
  if (data[idx] === 0) {
@@ -5679,9 +5742,9 @@ var ColorPaintBuffer = class {
5679
5742
  const lookup = this.lookup;
5680
5743
  const tilePool = this.tilePool;
5681
5744
  const config = this.config;
5682
- const tileShift = config.tileShift;
5683
- const tileMask = config.tileMask;
5684
- const target = config.target;
5745
+ const tileSize = config.tileSize;
5746
+ const targetW = config.targetWidth;
5747
+ const targetH = config.targetHeight;
5685
5748
  const {
5686
5749
  w: bW,
5687
5750
  h: bH,
@@ -5694,7 +5757,7 @@ var ColorPaintBuffer = class {
5694
5757
  forEachLinePoint(x0, y0, x1, y1, (px, py) => {
5695
5758
  const topLeftX = Math.floor(px + centerOffsetX);
5696
5759
  const topLeftY = Math.floor(py + centerOffsetY);
5697
- trimRectBounds(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5760
+ trimRectBounds(topLeftX, topLeftY, bW, bH, targetW, targetH, scratch);
5698
5761
  if (scratch.w <= 0 || scratch.h <= 0) return;
5699
5762
  eachTileInBounds(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5700
5763
  const d32 = tile.data;
@@ -5702,8 +5765,8 @@ var ColorPaintBuffer = class {
5702
5765
  for (let i = 0; i < bH_t; i++) {
5703
5766
  const canvasY = bY + i;
5704
5767
  const bOff = (canvasY - topLeftY) * bW;
5705
- const tOff = (canvasY & tileMask) << tileShift;
5706
- const dS = tOff + (bX & tileMask);
5768
+ const tOff = (canvasY - tile.y) * tileSize;
5769
+ const dS = tOff + (bX - tile.x);
5707
5770
  for (let j = 0; j < bW_t; j++) {
5708
5771
  const canvasX = bX + j;
5709
5772
  const brushA = bD[bOff + (canvasX - topLeftX)];
@@ -5733,9 +5796,9 @@ var ColorPaintBuffer = class {
5733
5796
  const lookup = this.lookup;
5734
5797
  const tilePool = this.tilePool;
5735
5798
  const config = this.config;
5736
- const tileShift = config.tileShift;
5737
- const tileMask = config.tileMask;
5738
- const target = config.target;
5799
+ const tileSize = config.tileSize;
5800
+ const targetW = config.targetWidth;
5801
+ const targetH = config.targetHeight;
5739
5802
  const {
5740
5803
  w: bW,
5741
5804
  h: bH,
@@ -5747,7 +5810,7 @@ var ColorPaintBuffer = class {
5747
5810
  forEachLinePoint(x0, y0, x1, y1, (px, py) => {
5748
5811
  const topLeftX = Math.floor(px + centerOffsetX);
5749
5812
  const topLeftY = Math.floor(py + centerOffsetY);
5750
- trimRectBounds(topLeftX, topLeftY, bW, bH, target.w, target.h, scratch);
5813
+ trimRectBounds(topLeftX, topLeftY, bW, bH, targetW, targetH, scratch);
5751
5814
  if (scratch.w <= 0 || scratch.h <= 0) return;
5752
5815
  eachTileInBounds(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5753
5816
  const d32 = tile.data;
@@ -5755,8 +5818,8 @@ var ColorPaintBuffer = class {
5755
5818
  for (let i = 0; i < bH_t; i++) {
5756
5819
  const canvasY = bY + i;
5757
5820
  const bOff = (canvasY - topLeftY) * bW;
5758
- const tOff = (canvasY & tileMask) << tileShift;
5759
- const dS = tOff + (bX & tileMask);
5821
+ const tOff = (canvasY - tile.y) * tileSize;
5822
+ const dS = tOff + (bX - tile.x);
5760
5823
  for (let j = 0; j < bW_t; j++) {
5761
5824
  const canvasX = bX + j;
5762
5825
  if (bD[bOff + (canvasX - topLeftX)]) {
@@ -5780,9 +5843,9 @@ var ColorPaintBuffer = class {
5780
5843
  const lookup = this.lookup;
5781
5844
  const tilePool = this.tilePool;
5782
5845
  const config = this.config;
5783
- const tileShift = config.tileShift;
5784
- const tileMask = config.tileMask;
5785
- const target = config.target;
5846
+ const targetW = config.targetWidth;
5847
+ const targetH = config.targetHeight;
5848
+ const tileSize = config.tileSize;
5786
5849
  const brushWidth = brush.w;
5787
5850
  const brushHeight = brush.h;
5788
5851
  const centerOffsetX = brush.centerOffsetX;
@@ -5791,15 +5854,15 @@ var ColorPaintBuffer = class {
5791
5854
  forEachLinePoint(x0, y0, x1, y1, (px, py) => {
5792
5855
  const topLeftX = Math.floor(px + centerOffsetX);
5793
5856
  const topLeftY = Math.floor(py + centerOffsetY);
5794
- trimRectBounds(topLeftX, topLeftY, brushWidth, brushHeight, target.w, target.h, scratch);
5857
+ trimRectBounds(topLeftX, topLeftY, brushWidth, brushHeight, targetW, targetH, scratch);
5795
5858
  if (scratch.w <= 0 || scratch.h <= 0) return;
5796
5859
  eachTileInBounds(config, lookup, tilePool, scratch, (tile, bX, bY, bW_t, bH_t) => {
5797
5860
  const d32 = tile.data;
5798
5861
  let tileChanged = false;
5799
5862
  for (let i = 0; i < bH_t; i++) {
5800
5863
  const canvasY = bY + i;
5801
- const tOff = (canvasY & tileMask) << tileShift;
5802
- const dS = tOff + (bX & tileMask);
5864
+ const tOff = (canvasY - tile.y) * tileSize;
5865
+ const dS = tOff + (bX - tile.x);
5803
5866
  for (let j = 0; j < bW_t; j++) {
5804
5867
  const idx = dS + j;
5805
5868
  if (d32[idx] !== color) {
@@ -5831,7 +5894,6 @@ var SCRATCH_OPTS = {
5831
5894
  };
5832
5895
  function commitMaskPaintBuffer(accumulator, paintBuffer, color, alpha = 255, blendFn = sourceOverPerfect, blendColorPixelDataMaskFn) {
5833
5896
  const config = accumulator.config;
5834
- const tileShift = config.tileShift;
5835
5897
  const lookup = paintBuffer.lookup;
5836
5898
  SCRATCH_OPTS.alpha = alpha;
5837
5899
  SCRATCH_OPTS.blendFn = blendFn;
@@ -5839,10 +5901,8 @@ function commitMaskPaintBuffer(accumulator, paintBuffer, color, alpha = 255, ble
5839
5901
  const tile = lookup[i];
5840
5902
  if (tile) {
5841
5903
  const didChange = accumulator.storeTileBeforeState(tile.id, tile.tx, tile.ty);
5842
- const dx = tile.tx << tileShift;
5843
- const dy = tile.ty << tileShift;
5844
- SCRATCH_OPTS.x = dx;
5845
- SCRATCH_OPTS.y = dy;
5904
+ SCRATCH_OPTS.x = tile.x;
5905
+ SCRATCH_OPTS.y = tile.y;
5846
5906
  SCRATCH_OPTS.w = tile.w;
5847
5907
  SCRATCH_OPTS.h = tile.h;
5848
5908
  didChange(blendColorPixelDataMaskFn(config.target, color, tile, SCRATCH_OPTS));
@@ -5858,49 +5918,51 @@ function makeAlphaMaskPaintBufferCommitter(accumulator, paintBuffer) {
5858
5918
  };
5859
5919
  }
5860
5920
 
5861
- // src/Internal/_constants.ts
5862
- var DEFAULT_CANVAS_FACTORY = (w, h) => new OffscreenCanvas(w, h);
5863
-
5864
- // src/Tile/MaskTile.ts
5865
- var makeAlphaMaskTile = (id, tx, ty, tileSize, tileArea) => {
5866
- return {
5867
- tileType: 1 /* MASK */,
5868
- type: 0 /* ALPHA */,
5869
- data: new Uint8Array(tileArea),
5870
- w: tileSize,
5871
- h: tileSize,
5872
- id,
5873
- tx,
5874
- ty
5921
+ // src/PixelData/ReusablePixelData.ts
5922
+ function makeReusablePixelData() {
5923
+ const pixelData = {
5924
+ w: 0,
5925
+ h: 0,
5926
+ data: null,
5927
+ imageData: null
5875
5928
  };
5876
- };
5877
- var makeBinaryMaskTile = (id, tx, ty, tileSize, tileArea) => {
5878
- return {
5879
- tileType: 1 /* MASK */,
5880
- type: 1 /* BINARY */,
5881
- data: new Uint8Array(tileArea),
5882
- w: tileSize,
5883
- h: tileSize,
5884
- id,
5885
- tx,
5886
- ty
5929
+ return function getReusablePixelData(width, height) {
5930
+ if (pixelData.w !== width || pixelData.h !== height) {
5931
+ setPixelData(pixelData, new ImageData(width, height));
5932
+ } else {
5933
+ pixelData.data.fill(0);
5934
+ }
5935
+ return pixelData;
5887
5936
  };
5888
- };
5937
+ }
5889
5938
 
5890
5939
  // src/Paint/Render/AlphaMaskPaintBufferCanvasRenderer.ts
5891
- function makeAlphaMaskPaintBufferCanvasRenderer(paintBuffer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
5892
- const config = paintBuffer.config;
5893
- const tileSize = config.tileSize;
5894
- const tileShift = config.tileShift;
5895
- const tileArea = config.tileArea;
5896
- const lookup = paintBuffer.lookup;
5897
- const canvas = canvasFactory(tileSize, tileSize);
5898
- const ctx = canvas.getContext("2d");
5899
- if (!ctx) throw new Error(CANVAS_CTX_FAILED);
5900
- ctx.imageSmoothingEnabled = false;
5901
- const bridge = makePixelData(new ImageData(tileSize, tileSize));
5902
- const view32 = bridge.data;
5903
- return function drawPaintBuffer(targetCtx, color, alpha = 255, compOperation = "source-over") {
5940
+ function makeAlphaMaskPaintBufferCanvasRenderer(paintBuffer, reusableCanvasFactory) {
5941
+ const factory = reusableCanvasFactory ?? makeReusableOffscreenCanvas;
5942
+ const getBuffer = factory();
5943
+ const getBridge = makeReusablePixelData();
5944
+ let config;
5945
+ let tileSize;
5946
+ let tileArea;
5947
+ let lookup;
5948
+ let view32;
5949
+ let bridge;
5950
+ let canvas;
5951
+ let ctx;
5952
+ setBuffer(paintBuffer);
5953
+ function setBuffer(value) {
5954
+ paintBuffer = value;
5955
+ config = paintBuffer.config;
5956
+ tileSize = config.tileSize;
5957
+ tileArea = config.tileArea;
5958
+ lookup = paintBuffer.lookup;
5959
+ bridge = getBridge(tileSize, tileSize);
5960
+ view32 = bridge.data;
5961
+ const buff = getBuffer(tileSize, tileSize);
5962
+ canvas = buff.canvas;
5963
+ ctx = buff.ctx;
5964
+ }
5965
+ function draw(targetCtx, color, alpha = 255, compOperation = "source-over") {
5904
5966
  if (alpha === 0) return;
5905
5967
  const baseSrcAlpha = color >>> 24;
5906
5968
  const colorRGB = color & 16777215;
@@ -5923,29 +5985,31 @@ function makeAlphaMaskPaintBufferCanvasRenderer(paintBuffer, canvasFactory = DEF
5923
5985
  view32[p] = (colorRGB | finalA << 24) >>> 0;
5924
5986
  }
5925
5987
  }
5926
- const dx = tile.tx << tileShift;
5927
- const dy = tile.ty << tileShift;
5928
5988
  ctx.putImageData(bridge.imageData, 0, 0);
5929
- targetCtx.drawImage(canvas, dx, dy);
5989
+ targetCtx.drawImage(canvas, tile.x, tile.y);
5930
5990
  }
5931
5991
  }
5932
5992
  targetCtx.globalAlpha = 1;
5933
5993
  targetCtx.globalCompositeOperation = "source-over";
5994
+ }
5995
+ return {
5996
+ draw,
5997
+ setBuffer
5934
5998
  };
5935
5999
  }
5936
6000
 
5937
6001
  // src/Paint/Commit/AlphaMaskPaintBufferManager.ts
5938
- function makeAlphaMaskPaintBufferManager(writer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
5939
- const pool = new TilePool(writer.config, makeAlphaMaskTile);
6002
+ function makeAlphaMaskPaintBufferManager(writer, reusableCanvasFactory) {
6003
+ const pool = new TilePool(writer.config.tileSize, makeAlphaMaskTile);
5940
6004
  const buffer = new AlphaMaskPaintBuffer(writer.config, pool);
5941
- const draw = makeAlphaMaskPaintBufferCanvasRenderer(buffer, canvasFactory);
6005
+ const renderer = makeAlphaMaskPaintBufferCanvasRenderer(buffer, reusableCanvasFactory);
5942
6006
  return {
5943
6007
  clear: buffer.clear.bind(buffer),
5944
6008
  paintRect: buffer.paintRect.bind(buffer),
5945
6009
  paintAlphaMask: buffer.paintAlphaMask.bind(buffer),
5946
6010
  paintBinaryMask: buffer.paintBinaryMask.bind(buffer),
5947
6011
  commit: makeAlphaMaskPaintBufferCommitter(writer.accumulator, buffer),
5948
- draw
6012
+ renderer
5949
6013
  };
5950
6014
  }
5951
6015
 
@@ -5957,19 +6021,32 @@ function makeBinaryMaskPaintBufferCommitter(accumulator, paintBuffer) {
5957
6021
  }
5958
6022
 
5959
6023
  // src/Paint/Render/BinaryMaskPaintBufferCanvasRenderer.ts
5960
- function makeBinaryMaskPaintBufferCanvasRenderer(paintBuffer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
5961
- const config = paintBuffer.config;
5962
- const tileSize = config.tileSize;
5963
- const tileShift = config.tileShift;
5964
- const tileArea = config.tileArea;
5965
- const lookup = paintBuffer.lookup;
5966
- const canvas = canvasFactory(tileSize, tileSize);
5967
- const ctx = canvas.getContext("2d");
5968
- if (!ctx) throw new Error(CANVAS_CTX_FAILED);
5969
- ctx.imageSmoothingEnabled = false;
5970
- const bridge = makePixelData(new ImageData(tileSize, tileSize));
5971
- const view32 = bridge.data;
5972
- return function drawPaintBuffer(targetCtx, color, alpha = 255, compOperation = "source-over") {
6024
+ function makeBinaryMaskPaintBufferCanvasRenderer(paintBuffer, reusableCanvasFactory) {
6025
+ const factory = reusableCanvasFactory ?? makeReusableOffscreenCanvas;
6026
+ const getBuffer = factory();
6027
+ const getBridge = makeReusablePixelData();
6028
+ let config;
6029
+ let tileSize;
6030
+ let tileArea;
6031
+ let lookup;
6032
+ let view32;
6033
+ let bridge;
6034
+ let canvas;
6035
+ let ctx;
6036
+ setBuffer(paintBuffer);
6037
+ function setBuffer(value) {
6038
+ paintBuffer = value;
6039
+ config = paintBuffer.config;
6040
+ tileSize = config.tileSize;
6041
+ tileArea = config.tileArea;
6042
+ lookup = paintBuffer.lookup;
6043
+ bridge = getBridge(tileSize, tileSize);
6044
+ view32 = bridge.data;
6045
+ const buff = getBuffer(tileSize, tileSize);
6046
+ canvas = buff.canvas;
6047
+ ctx = buff.ctx;
6048
+ }
6049
+ function draw(targetCtx, color, alpha = 255, compOperation = "source-over") {
5973
6050
  if (alpha === 0) return;
5974
6051
  const baseSrcAlpha = color >>> 24;
5975
6052
  if (baseSrcAlpha === 0) return;
@@ -5985,28 +6062,30 @@ function makeBinaryMaskPaintBufferCanvasRenderer(paintBuffer, canvasFactory = DE
5985
6062
  view32[p] = color;
5986
6063
  }
5987
6064
  }
5988
- const dx = tile.tx << tileShift;
5989
- const dy = tile.ty << tileShift;
5990
6065
  ctx.putImageData(bridge.imageData, 0, 0);
5991
- targetCtx.drawImage(canvas, dx, dy);
6066
+ targetCtx.drawImage(canvas, tile.x, tile.y);
5992
6067
  }
5993
6068
  }
5994
6069
  targetCtx.globalAlpha = 1;
5995
6070
  targetCtx.globalCompositeOperation = "source-over";
6071
+ }
6072
+ return {
6073
+ draw,
6074
+ setBuffer
5996
6075
  };
5997
6076
  }
5998
6077
 
5999
6078
  // src/Paint/Commit/BinaryMaskPaintBufferManager.ts
6000
- function makeBinaryMaskPaintBufferManager(writer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
6001
- const pool = new TilePool(writer.config, makeBinaryMaskTile);
6079
+ function makeBinaryMaskPaintBufferManager(writer, reusableCanvasFactory) {
6080
+ const pool = new TilePool(writer.config.tileSize, makeBinaryMaskTile);
6002
6081
  const buffer = new BinaryMaskPaintBuffer(writer.config, pool);
6003
- const draw = makeBinaryMaskPaintBufferCanvasRenderer(buffer, canvasFactory);
6082
+ const renderer = makeBinaryMaskPaintBufferCanvasRenderer(buffer, reusableCanvasFactory);
6004
6083
  return {
6005
6084
  clear: buffer.clear.bind(buffer),
6006
6085
  paintRect: buffer.paintRect.bind(buffer),
6007
6086
  paintBinaryMask: buffer.paintBinaryMask.bind(buffer),
6008
6087
  commit: makeBinaryMaskPaintBufferCommitter(writer.accumulator, buffer),
6009
- draw
6088
+ renderer
6010
6089
  };
6011
6090
  }
6012
6091
 
@@ -6021,7 +6100,6 @@ var SCRATCH_OPTS2 = {
6021
6100
  };
6022
6101
  function commitColorPaintBuffer(accumulator, paintBuffer, alpha = 255, blendFn = sourceOverPerfect, blendPixelDataFn = blendPixelData) {
6023
6102
  const config = accumulator.config;
6024
- const tileShift = config.tileShift;
6025
6103
  const lookup = paintBuffer.lookup;
6026
6104
  SCRATCH_OPTS2.alpha = alpha;
6027
6105
  SCRATCH_OPTS2.blendFn = blendFn;
@@ -6029,10 +6107,8 @@ function commitColorPaintBuffer(accumulator, paintBuffer, alpha = 255, blendFn =
6029
6107
  const tile = lookup[i];
6030
6108
  if (tile) {
6031
6109
  const didChange = accumulator.storeTileBeforeState(tile.id, tile.tx, tile.ty);
6032
- const dx = tile.tx << tileShift;
6033
- const dy = tile.ty << tileShift;
6034
- SCRATCH_OPTS2.x = dx;
6035
- SCRATCH_OPTS2.y = dy;
6110
+ SCRATCH_OPTS2.x = tile.x;
6111
+ SCRATCH_OPTS2.y = tile.y;
6036
6112
  SCRATCH_OPTS2.w = tile.w;
6037
6113
  SCRATCH_OPTS2.h = tile.h;
6038
6114
  didChange(blendPixelDataFn(config.target, tile, SCRATCH_OPTS2));
@@ -6049,44 +6125,47 @@ function makeColorPaintBufferCommitter(accumulator, paintBuffer) {
6049
6125
  }
6050
6126
 
6051
6127
  // src/Paint/Render/ColorPaintBufferCanvasRenderer.ts
6052
- function makeColorPaintBufferCanvasRenderer(paintBuffer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
6053
- const config = paintBuffer.config;
6054
- const tileSize = config.tileSize;
6055
- const tileShift = config.tileShift;
6056
- const lookup = paintBuffer.lookup;
6057
- const canvas = canvasFactory(tileSize, tileSize);
6058
- const ctx = canvas.getContext("2d");
6059
- if (!ctx) throw new Error(CANVAS_CTX_FAILED);
6060
- ctx.imageSmoothingEnabled = false;
6061
- return function drawPaintBuffer(targetCtx, alpha = 255, compOperation = "source-over") {
6128
+ function makeColorPaintBufferCanvasRenderer(paintBuffer, reusableCanvasFactory) {
6129
+ const factory = reusableCanvasFactory ?? makeReusableOffscreenCanvas;
6130
+ const getBuffer = factory();
6131
+ function draw(targetCtx, alpha = 255, compOperation = "source-over") {
6132
+ const buff = getBuffer(paintBuffer.config.tileSize, paintBuffer.config.tileSize);
6133
+ const lookup = paintBuffer.lookup;
6134
+ const length = lookup.length;
6135
+ const ctx = buff.ctx;
6136
+ const canvas = buff.canvas;
6062
6137
  targetCtx.globalAlpha = alpha / 255;
6063
6138
  targetCtx.globalCompositeOperation = compOperation;
6064
- for (let i = 0; i < lookup.length; i++) {
6139
+ for (let i = 0; i < length; i++) {
6065
6140
  const tile = lookup[i];
6066
6141
  if (tile) {
6067
- const dx = tile.tx << tileShift;
6068
- const dy = tile.ty << tileShift;
6069
6142
  ctx.putImageData(tile.imageData, 0, 0);
6070
- targetCtx.drawImage(canvas, dx, dy);
6143
+ targetCtx.drawImage(canvas, tile.x, tile.y);
6071
6144
  }
6072
6145
  }
6073
6146
  targetCtx.globalAlpha = 1;
6074
6147
  targetCtx.globalCompositeOperation = "source-over";
6148
+ }
6149
+ return {
6150
+ draw,
6151
+ setBuffer(value) {
6152
+ paintBuffer = value;
6153
+ }
6075
6154
  };
6076
6155
  }
6077
6156
 
6078
6157
  // src/Paint/Commit/ColorPaintBufferManager.ts
6079
- function makeColorPaintBufferManager(writer, canvasFactory = DEFAULT_CANVAS_FACTORY) {
6080
- const pool = new TilePool(writer.config, makePixelTile);
6158
+ function makeColorPaintBufferManager(writer, reusableCanvasFactory) {
6159
+ const pool = new TilePool(writer.config.tileSize, makePixelTile);
6081
6160
  const buffer = new ColorPaintBuffer(writer.config, pool);
6082
- const draw = makeColorPaintBufferCanvasRenderer(buffer, canvasFactory);
6161
+ const renderer = makeColorPaintBufferCanvasRenderer(buffer, reusableCanvasFactory);
6083
6162
  return {
6084
6163
  clear: buffer.clear.bind(buffer),
6085
6164
  paintRect: buffer.paintRect.bind(buffer),
6086
6165
  paintAlphaMask: buffer.paintAlphaMask.bind(buffer),
6087
6166
  paintBinaryMask: buffer.paintBinaryMask.bind(buffer),
6088
6167
  commit: makeColorPaintBufferCommitter(writer.accumulator, buffer),
6089
- draw
6168
+ renderer
6090
6169
  };
6091
6170
  }
6092
6171
 
@@ -6222,24 +6301,6 @@ function makePaintRect(w, h) {
6222
6301
  };
6223
6302
  }
6224
6303
 
6225
- // src/PixelData/ReusablePixelData.ts
6226
- function makeReusablePixelData() {
6227
- const pixelData = {
6228
- w: 0,
6229
- h: 0,
6230
- data: null,
6231
- imageData: null
6232
- };
6233
- return function getReusablePixelData(width, height) {
6234
- if (pixelData.w !== width || pixelData.h !== height) {
6235
- setPixelData(pixelData, new ImageData(width, height));
6236
- } else {
6237
- pixelData.data.fill(0);
6238
- }
6239
- return pixelData;
6240
- };
6241
- }
6242
-
6243
6304
  // src/Paint/Render/PaintCursorRenderer.ts
6244
6305
  function makePaintCursorRenderer(reusableCanvasFactory) {
6245
6306
  const factory = reusableCanvasFactory ?? makeReusableOffscreenCanvas;
@@ -6392,20 +6453,29 @@ var SCRATCH_OPTS5 = {
6392
6453
  x: 0,
6393
6454
  y: 0,
6394
6455
  alpha: 255,
6395
- blendFn: sourceOverPerfect
6456
+ blendFn: sourceOverPerfect,
6457
+ w: void 0,
6458
+ h: void 0
6396
6459
  };
6397
- function blendColorPixelDataPaintMask(dst, color, mask, x, y, alpha = 255, blendFn = sourceOverPerfect) {
6460
+ function blendColorPixelDataPaintMask(target, color, mask, x, y, alpha = 255, blendFn = sourceOverPerfect) {
6398
6461
  const tx = x + mask.centerOffsetX;
6399
6462
  const ty = y + mask.centerOffsetY;
6400
6463
  SCRATCH_OPTS5.x = tx;
6401
6464
  SCRATCH_OPTS5.y = ty;
6402
6465
  SCRATCH_OPTS5.alpha = alpha;
6403
6466
  SCRATCH_OPTS5.blendFn = blendFn;
6404
- if (mask.type === 1 /* BINARY */) {
6405
- return blendColorPixelDataBinaryMask(dst, color, mask, SCRATCH_OPTS5);
6406
- } else {
6407
- return blendColorPixelDataAlphaMask(dst, color, mask, SCRATCH_OPTS5);
6467
+ SCRATCH_OPTS5.w = void 0;
6468
+ SCRATCH_OPTS5.h = void 0;
6469
+ if (mask.data) {
6470
+ if (mask.type === 1 /* BINARY */) {
6471
+ return blendColorPixelDataBinaryMask(target, color, mask, SCRATCH_OPTS5);
6472
+ } else {
6473
+ return blendColorPixelDataAlphaMask(target, color, mask, SCRATCH_OPTS5);
6474
+ }
6408
6475
  }
6476
+ SCRATCH_OPTS5.w = mask.w;
6477
+ SCRATCH_OPTS5.h = mask.h;
6478
+ return blendColorPixelData(target, color, SCRATCH_OPTS5);
6409
6479
  }
6410
6480
 
6411
6481
  // src/PixelData/blendPixelDataMask.ts
@@ -6425,13 +6495,12 @@ var SCRATCH_OPTS6 = {
6425
6495
  blendFn: void 0
6426
6496
  };
6427
6497
  function blendPixelDataPaintBuffer(target, paintBuffer, alpha = 255, blendFn, blendPixelDataFn = blendPixelData) {
6428
- const tileShift = paintBuffer.config.tileShift;
6429
6498
  const lookup = paintBuffer.lookup;
6430
6499
  for (let i = 0; i < lookup.length; i++) {
6431
6500
  const tile = lookup[i];
6432
6501
  if (tile) {
6433
- const x = tile.tx << tileShift;
6434
- const y = tile.ty << tileShift;
6502
+ const x = tile.x;
6503
+ const y = tile.y;
6435
6504
  SCRATCH_OPTS6.x = x;
6436
6505
  SCRATCH_OPTS6.y = y;
6437
6506
  SCRATCH_OPTS6.alpha = alpha;
@@ -6850,14 +6919,11 @@ function writePixelDataBuffer(target, data, _x, _y, _w, _h) {
6850
6919
 
6851
6920
  // src/PixelData/writePaintBufferToPixelData.ts
6852
6921
  function writePaintBufferToPixelData(target, paintBuffer, writePixelDataBufferFn = writePixelDataBuffer) {
6853
- const tileShift = paintBuffer.config.tileShift;
6854
6922
  const lookup = paintBuffer.lookup;
6855
6923
  for (let i = 0; i < lookup.length; i++) {
6856
6924
  const tile = lookup[i];
6857
6925
  if (tile) {
6858
- const dx = tile.tx << tileShift;
6859
- const dy = tile.ty << tileShift;
6860
- writePixelDataBufferFn(target, tile.data, dx, dy, tile.w, tile.h);
6926
+ writePixelDataBufferFn(target, tile.data, tile.x, tile.y, tile.w, tile.h);
6861
6927
  }
6862
6928
  }
6863
6929
  }
@@ -6910,7 +6976,6 @@ function writePixelData(target, source, x = 0, y = 0) {
6910
6976
  MaskType,
6911
6977
  PaintMaskOutline,
6912
6978
  PixelAccumulator,
6913
- PixelEngineConfig,
6914
6979
  PixelWriter,
6915
6980
  TilePool,
6916
6981
  TileType,
@@ -6937,6 +7002,7 @@ function writePixelData(target, source, x = 0, y = 0) {
6937
7002
  blendPixelDataPaintBuffer,
6938
7003
  clearPixelDataFast,
6939
7004
  color32ToCssRGBA,
7005
+ color32ToCssRGBAString,
6940
7006
  color32ToHex,
6941
7007
  colorBurnFast,
6942
7008
  colorBurnPerfect,
@@ -6950,6 +7016,7 @@ function writePixelData(target, source, x = 0, y = 0) {
6950
7016
  copyMask,
6951
7017
  copyPixelData,
6952
7018
  cropPixelData,
7019
+ cssRGBAToColor32,
6953
7020
  darkenFast,
6954
7021
  darkenPerfect,
6955
7022
  darkerFast,
@@ -7062,6 +7129,8 @@ function writePixelData(target, source, x = 0, y = 0) {
7062
7129
  makeReusableImageData,
7063
7130
  makeReusableOffscreenCanvas,
7064
7131
  makeReusablePixelData,
7132
+ makeTileTargetConfig,
7133
+ makeTileTargetMeta,
7065
7134
  merge2BinaryMaskRects,
7066
7135
  mergeAlphaMasks,
7067
7136
  mergeBinaryMaskRects,