pixel-data-js 0.25.0 → 0.25.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.dev.js CHANGED
@@ -1533,6 +1533,24 @@ var getKeyByValue = (obj, value) => {
1533
1533
  var OFFSCREEN_CANVAS_CTX_FAILED = "Failed to create OffscreenCanvas context";
1534
1534
  var CANVAS_CTX_FAILED = "Failed to create Canvas context";
1535
1535
 
1536
+ // src/Canvas/canvas-blend-modes.ts
1537
+ var CANVAS_COMPOSITE_MAP = {
1538
+ [BaseBlendMode.overwrite]: "copy",
1539
+ [BaseBlendMode.sourceOver]: "source-over",
1540
+ [BaseBlendMode.darken]: "darken",
1541
+ [BaseBlendMode.multiply]: "multiply",
1542
+ [BaseBlendMode.colorBurn]: "color-burn",
1543
+ [BaseBlendMode.lighten]: "lighten",
1544
+ [BaseBlendMode.screen]: "screen",
1545
+ [BaseBlendMode.colorDodge]: "color-dodge",
1546
+ [BaseBlendMode.linearDodge]: "lighter",
1547
+ [BaseBlendMode.overlay]: "overlay",
1548
+ [BaseBlendMode.softLight]: "soft-light",
1549
+ [BaseBlendMode.hardLight]: "hard-light",
1550
+ [BaseBlendMode.difference]: "difference",
1551
+ [BaseBlendMode.exclusion]: "exclusion"
1552
+ };
1553
+
1536
1554
  // src/Canvas/ReusableCanvas.ts
1537
1555
  function makeReusableCanvas() {
1538
1556
  return makeReusableCanvasMeta((w, h) => {
@@ -2821,6 +2839,9 @@ function resizeImageData(target, newWidth, newHeight, offsetX = 0, offsetY = 0)
2821
2839
  return result;
2822
2840
  }
2823
2841
 
2842
+ // src/Internal/helpers.ts
2843
+ var macro_halfAndFloor = (value) => value >> 1;
2844
+
2824
2845
  // src/Rect/trimRectBounds.ts
2825
2846
  function trimRectBounds(x, y, w, h, targetWidth, targetHeight, out) {
2826
2847
  const res = out ?? {
@@ -2994,8 +3015,8 @@ var PaintBuffer = class {
2994
3015
  const tileMask = config.tileMask;
2995
3016
  const target = config.target;
2996
3017
  const scratch = this.scratchBounds;
2997
- const centerOffsetX = brushWidth - 1 >> 1;
2998
- const centerOffsetY = brushHeight - 1 >> 1;
3018
+ const centerOffsetX = macro_halfAndFloor(brushWidth - 1);
3019
+ const centerOffsetY = macro_halfAndFloor(brushHeight - 1);
2999
3020
  let changed = false;
3000
3021
  forEachLinePoint(x0, y0, x1, y1, (px, py) => {
3001
3022
  const topLeftX = Math.floor(px + centerOffsetX);
@@ -3417,6 +3438,196 @@ var mutatorApplyBinaryMask = ((writer, deps = defaults12) => {
3417
3438
  };
3418
3439
  });
3419
3440
 
3441
+ // src/PixelData/blendColorPixelDataAlphaMask.ts
3442
+ function blendColorPixelDataAlphaMask(dst, color, mask, opts = {}) {
3443
+ const targetX = opts.x ?? 0;
3444
+ const targetY = opts.y ?? 0;
3445
+ const w = opts.w ?? mask.w;
3446
+ const h = opts.h ?? mask.h;
3447
+ const globalAlpha = opts.alpha ?? 255;
3448
+ const blendFn = opts.blendFn ?? sourceOverPerfect;
3449
+ const mx = opts.mx ?? 0;
3450
+ const my = opts.my ?? 0;
3451
+ const invertMask = opts.invertMask ?? false;
3452
+ if (globalAlpha === 0) return false;
3453
+ const baseSrcAlpha = color >>> 24;
3454
+ const isOverwrite = blendFn.isOverwrite || false;
3455
+ if (baseSrcAlpha === 0 && !isOverwrite) return false;
3456
+ let x = targetX;
3457
+ let y = targetY;
3458
+ let actualW = w;
3459
+ let actualH = h;
3460
+ if (x < 0) {
3461
+ actualW += x;
3462
+ x = 0;
3463
+ }
3464
+ if (y < 0) {
3465
+ actualH += y;
3466
+ y = 0;
3467
+ }
3468
+ actualW = Math.min(actualW, dst.width - x);
3469
+ actualH = Math.min(actualH, dst.height - y);
3470
+ if (actualW <= 0 || actualH <= 0) return false;
3471
+ const dx = x - targetX | 0;
3472
+ const dy = y - targetY | 0;
3473
+ const dst32 = dst.data32;
3474
+ const dw = dst.width;
3475
+ const mPitch = mask.w;
3476
+ const maskData = mask.data;
3477
+ let dIdx = y * dw + x | 0;
3478
+ let mIdx = (my + dy) * mPitch + (mx + dx) | 0;
3479
+ const dStride = dw - actualW | 0;
3480
+ const mStride = mPitch - actualW | 0;
3481
+ const isOpaque = globalAlpha === 255;
3482
+ const colorRGB = color & 16777215;
3483
+ let didChange = false;
3484
+ for (let iy = 0; iy < actualH; iy++) {
3485
+ for (let ix = 0; ix < actualW; ix++) {
3486
+ const mVal = maskData[mIdx];
3487
+ const effM = invertMask ? 255 - mVal : mVal;
3488
+ if (effM === 0) {
3489
+ dIdx++;
3490
+ mIdx++;
3491
+ continue;
3492
+ }
3493
+ let weight = globalAlpha;
3494
+ if (isOpaque) {
3495
+ weight = effM;
3496
+ } else if (effM !== 255) {
3497
+ weight = effM * globalAlpha + 128 >> 8;
3498
+ }
3499
+ if (weight === 0) {
3500
+ dIdx++;
3501
+ mIdx++;
3502
+ continue;
3503
+ }
3504
+ let finalCol = color;
3505
+ if (weight < 255) {
3506
+ const a = baseSrcAlpha * weight + 128 >> 8;
3507
+ if (a === 0 && !isOverwrite) {
3508
+ dIdx++;
3509
+ mIdx++;
3510
+ continue;
3511
+ }
3512
+ finalCol = (colorRGB | a << 24) >>> 0;
3513
+ }
3514
+ const current = dst32[dIdx];
3515
+ const next = blendFn(finalCol, current);
3516
+ if (current !== next) {
3517
+ dst32[dIdx] = next;
3518
+ didChange = true;
3519
+ }
3520
+ dIdx++;
3521
+ mIdx++;
3522
+ }
3523
+ dIdx += dStride;
3524
+ mIdx += mStride;
3525
+ }
3526
+ return didChange;
3527
+ }
3528
+
3529
+ // src/PixelData/blendColorPixelDataBinaryMask.ts
3530
+ function blendColorPixelDataBinaryMask(dst, color, mask, opts = {}) {
3531
+ const targetX = opts.x ?? 0;
3532
+ const targetY = opts.y ?? 0;
3533
+ let w = opts.w ?? mask.w;
3534
+ let h = opts.h ?? mask.h;
3535
+ const globalAlpha = opts.alpha ?? 255;
3536
+ const blendFn = opts.blendFn ?? sourceOverPerfect;
3537
+ const mx = opts.mx ?? 0;
3538
+ const my = opts.my ?? 0;
3539
+ const invertMask = opts.invertMask ?? false;
3540
+ if (globalAlpha === 0) return false;
3541
+ const baseSrcAlpha = color >>> 24;
3542
+ const isOverwrite = blendFn.isOverwrite || false;
3543
+ if (baseSrcAlpha === 0 && !isOverwrite) return false;
3544
+ let x = targetX;
3545
+ let y = targetY;
3546
+ if (x < 0) {
3547
+ w += x;
3548
+ x = 0;
3549
+ }
3550
+ if (y < 0) {
3551
+ h += y;
3552
+ y = 0;
3553
+ }
3554
+ const actualW = Math.min(w, dst.width - x);
3555
+ const actualH = Math.min(h, dst.height - y);
3556
+ if (actualW <= 0 || actualH <= 0) return false;
3557
+ let baseColorWithGlobalAlpha = color;
3558
+ if (globalAlpha < 255) {
3559
+ const a = baseSrcAlpha * globalAlpha + 128 >> 8;
3560
+ if (a === 0 && !isOverwrite) return false;
3561
+ baseColorWithGlobalAlpha = (color & 16777215 | a << 24) >>> 0;
3562
+ }
3563
+ const dx = x - targetX | 0;
3564
+ const dy = y - targetY | 0;
3565
+ const dst32 = dst.data32;
3566
+ const dw = dst.width;
3567
+ const mPitch = mask.w;
3568
+ const maskData = mask.data;
3569
+ let dIdx = y * dw + x | 0;
3570
+ let mIdx = (my + dy) * mPitch + (mx + dx) | 0;
3571
+ const dStride = dw - actualW | 0;
3572
+ const mStride = mPitch - actualW | 0;
3573
+ const skipVal = invertMask ? 1 : 0;
3574
+ let didChange = false;
3575
+ for (let iy = 0; iy < actualH; iy++) {
3576
+ for (let ix = 0; ix < actualW; ix++) {
3577
+ if (maskData[mIdx] === skipVal) {
3578
+ dIdx++;
3579
+ mIdx++;
3580
+ continue;
3581
+ }
3582
+ const current = dst32[dIdx];
3583
+ const next = blendFn(baseColorWithGlobalAlpha, current);
3584
+ if (current !== next) {
3585
+ dst32[dIdx] = next;
3586
+ didChange = true;
3587
+ }
3588
+ dIdx++;
3589
+ mIdx++;
3590
+ }
3591
+ dIdx += dStride;
3592
+ mIdx += mStride;
3593
+ }
3594
+ return didChange;
3595
+ }
3596
+
3597
+ // src/History/PixelMutator/mutatorBlendPaintMask.ts
3598
+ var defaults13 = {
3599
+ blendColorPixelDataAlphaMask,
3600
+ blendColorPixelDataBinaryMask
3601
+ };
3602
+ var mutatorBlendPaintMask = ((writer, deps = defaults13) => {
3603
+ const {
3604
+ blendColorPixelDataBinaryMask: blendColorPixelDataBinaryMask2 = defaults13.blendColorPixelDataBinaryMask,
3605
+ blendColorPixelDataAlphaMask: blendColorPixelDataAlphaMask2 = defaults13.blendColorPixelDataAlphaMask
3606
+ } = deps;
3607
+ const OPTS = {
3608
+ x: 0,
3609
+ y: 0,
3610
+ blendFn: sourceOverPerfect,
3611
+ alpha: 255
3612
+ };
3613
+ return {
3614
+ blendColorPaintMask(color, mask, x, y, alpha = 255, blendFn = sourceOverPerfect) {
3615
+ const tx = x + mask.centerOffsetX;
3616
+ const ty = y + mask.centerOffsetY;
3617
+ const didChange = writer.accumulator.storeRegionBeforeState(tx, ty, mask.w, mask.h);
3618
+ OPTS.x = tx;
3619
+ OPTS.y = ty;
3620
+ OPTS.alpha = alpha;
3621
+ OPTS.blendFn = blendFn;
3622
+ if (mask.type === 1 /* BINARY */) {
3623
+ return didChange(blendColorPixelDataBinaryMask2(writer.config.target, color, mask, OPTS));
3624
+ } else {
3625
+ return didChange(blendColorPixelDataAlphaMask2(writer.config.target, color, mask, OPTS));
3626
+ }
3627
+ }
3628
+ };
3629
+ });
3630
+
3420
3631
  // src/ImageData/copyImageData.ts
3421
3632
  function copyImageData({
3422
3633
  data,
@@ -4406,162 +4617,6 @@ var PixelData = class {
4406
4617
  }
4407
4618
  };
4408
4619
 
4409
- // src/PixelData/blendColorPixelDataAlphaMask.ts
4410
- function blendColorPixelDataAlphaMask(dst, color, mask, opts = {}) {
4411
- const targetX = opts.x ?? 0;
4412
- const targetY = opts.y ?? 0;
4413
- const w = opts.w ?? mask.w;
4414
- const h = opts.h ?? mask.h;
4415
- const globalAlpha = opts.alpha ?? 255;
4416
- const blendFn = opts.blendFn ?? sourceOverPerfect;
4417
- const mx = opts.mx ?? 0;
4418
- const my = opts.my ?? 0;
4419
- const invertMask = opts.invertMask ?? false;
4420
- if (globalAlpha === 0) return false;
4421
- const baseSrcAlpha = color >>> 24;
4422
- const isOverwrite = blendFn.isOverwrite || false;
4423
- if (baseSrcAlpha === 0 && !isOverwrite) return false;
4424
- let x = targetX;
4425
- let y = targetY;
4426
- let actualW = w;
4427
- let actualH = h;
4428
- if (x < 0) {
4429
- actualW += x;
4430
- x = 0;
4431
- }
4432
- if (y < 0) {
4433
- actualH += y;
4434
- y = 0;
4435
- }
4436
- actualW = Math.min(actualW, dst.width - x);
4437
- actualH = Math.min(actualH, dst.height - y);
4438
- if (actualW <= 0 || actualH <= 0) return false;
4439
- const dx = x - targetX | 0;
4440
- const dy = y - targetY | 0;
4441
- const dst32 = dst.data32;
4442
- const dw = dst.width;
4443
- const mPitch = mask.w;
4444
- const maskData = mask.data;
4445
- let dIdx = y * dw + x | 0;
4446
- let mIdx = (my + dy) * mPitch + (mx + dx) | 0;
4447
- const dStride = dw - actualW | 0;
4448
- const mStride = mPitch - actualW | 0;
4449
- const isOpaque = globalAlpha === 255;
4450
- const colorRGB = color & 16777215;
4451
- let didChange = false;
4452
- for (let iy = 0; iy < actualH; iy++) {
4453
- for (let ix = 0; ix < actualW; ix++) {
4454
- const mVal = maskData[mIdx];
4455
- const effM = invertMask ? 255 - mVal : mVal;
4456
- if (effM === 0) {
4457
- dIdx++;
4458
- mIdx++;
4459
- continue;
4460
- }
4461
- let weight = globalAlpha;
4462
- if (isOpaque) {
4463
- weight = effM;
4464
- } else if (effM !== 255) {
4465
- weight = effM * globalAlpha + 128 >> 8;
4466
- }
4467
- if (weight === 0) {
4468
- dIdx++;
4469
- mIdx++;
4470
- continue;
4471
- }
4472
- let finalCol = color;
4473
- if (weight < 255) {
4474
- const a = baseSrcAlpha * weight + 128 >> 8;
4475
- if (a === 0 && !isOverwrite) {
4476
- dIdx++;
4477
- mIdx++;
4478
- continue;
4479
- }
4480
- finalCol = (colorRGB | a << 24) >>> 0;
4481
- }
4482
- const current = dst32[dIdx];
4483
- const next = blendFn(finalCol, current);
4484
- if (current !== next) {
4485
- dst32[dIdx] = next;
4486
- didChange = true;
4487
- }
4488
- dIdx++;
4489
- mIdx++;
4490
- }
4491
- dIdx += dStride;
4492
- mIdx += mStride;
4493
- }
4494
- return didChange;
4495
- }
4496
-
4497
- // src/PixelData/blendColorPixelDataBinaryMask.ts
4498
- function blendColorPixelDataBinaryMask(dst, color, mask, opts = {}) {
4499
- const targetX = opts.x ?? 0;
4500
- const targetY = opts.y ?? 0;
4501
- let w = opts.w ?? mask.w;
4502
- let h = opts.h ?? mask.h;
4503
- const globalAlpha = opts.alpha ?? 255;
4504
- const blendFn = opts.blendFn ?? sourceOverPerfect;
4505
- const mx = opts.mx ?? 0;
4506
- const my = opts.my ?? 0;
4507
- const invertMask = opts.invertMask ?? false;
4508
- if (globalAlpha === 0) return false;
4509
- const baseSrcAlpha = color >>> 24;
4510
- const isOverwrite = blendFn.isOverwrite || false;
4511
- if (baseSrcAlpha === 0 && !isOverwrite) return false;
4512
- let x = targetX;
4513
- let y = targetY;
4514
- if (x < 0) {
4515
- w += x;
4516
- x = 0;
4517
- }
4518
- if (y < 0) {
4519
- h += y;
4520
- y = 0;
4521
- }
4522
- const actualW = Math.min(w, dst.width - x);
4523
- const actualH = Math.min(h, dst.height - y);
4524
- if (actualW <= 0 || actualH <= 0) return false;
4525
- let baseColorWithGlobalAlpha = color;
4526
- if (globalAlpha < 255) {
4527
- const a = baseSrcAlpha * globalAlpha + 128 >> 8;
4528
- if (a === 0 && !isOverwrite) return false;
4529
- baseColorWithGlobalAlpha = (color & 16777215 | a << 24) >>> 0;
4530
- }
4531
- const dx = x - targetX | 0;
4532
- const dy = y - targetY | 0;
4533
- const dst32 = dst.data32;
4534
- const dw = dst.width;
4535
- const mPitch = mask.w;
4536
- const maskData = mask.data;
4537
- let dIdx = y * dw + x | 0;
4538
- let mIdx = (my + dy) * mPitch + (mx + dx) | 0;
4539
- const dStride = dw - actualW | 0;
4540
- const mStride = mPitch - actualW | 0;
4541
- const skipVal = invertMask ? 1 : 0;
4542
- let didChange = false;
4543
- for (let iy = 0; iy < actualH; iy++) {
4544
- for (let ix = 0; ix < actualW; ix++) {
4545
- if (maskData[mIdx] === skipVal) {
4546
- dIdx++;
4547
- mIdx++;
4548
- continue;
4549
- }
4550
- const current = dst32[dIdx];
4551
- const next = blendFn(baseColorWithGlobalAlpha, current);
4552
- if (current !== next) {
4553
- dst32[dIdx] = next;
4554
- didChange = true;
4555
- }
4556
- dIdx++;
4557
- mIdx++;
4558
- }
4559
- dIdx += dStride;
4560
- mIdx += mStride;
4561
- }
4562
- return didChange;
4563
- }
4564
-
4565
4620
  // src/PixelData/blendPixelDataPaintBuffer.ts
4566
4621
  var SCRATCH_OPTS = {
4567
4622
  x: 0,
@@ -4904,9 +4959,6 @@ function makePaintAlphaMask(mask) {
4904
4959
  };
4905
4960
  }
4906
4961
 
4907
- // src/Internal/helpers.ts
4908
- var macro_halfAndFloor = (value) => value >> 1;
4909
-
4910
4962
  // src/Paint/makeRectFalloffPaintAlphaMask.ts
4911
4963
  function makeRectFalloffPaintAlphaMask(width, height, fallOff = (d) => d) {
4912
4964
  const fPx = Math.floor(width / 2);
@@ -4970,6 +5022,7 @@ export {
4970
5022
  BASE_FAST_BLEND_MODE_FUNCTIONS,
4971
5023
  BASE_PERFECT_BLEND_MODE_FUNCTIONS,
4972
5024
  BaseBlendMode,
5025
+ CANVAS_COMPOSITE_MAP,
4973
5026
  CANVAS_CTX_FAILED,
4974
5027
  HistoryManager,
4975
5028
  IndexedImage,
@@ -5093,6 +5146,7 @@ export {
5093
5146
  mutatorApplyAlphaMask,
5094
5147
  mutatorApplyBinaryMask,
5095
5148
  mutatorBlendColor,
5149
+ mutatorBlendPaintMask,
5096
5150
  mutatorBlendPixel,
5097
5151
  mutatorBlendPixelData,
5098
5152
  mutatorBlendPixelDataAlphaMask,