pixel-data-js 0.8.0 → 0.9.1
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.cjs +93 -11
- package/dist/index.dev.cjs.map +1 -1
- package/dist/index.dev.js +90 -11
- package/dist/index.dev.js.map +1 -1
- package/dist/index.prod.cjs +93 -11
- package/dist/index.prod.cjs.map +1 -1
- package/dist/index.prod.d.ts +32 -19
- package/dist/index.prod.js +90 -11
- package/dist/index.prod.js.map +1 -1
- package/package.json +6 -2
- package/src/BlendModes/blend-mode-getters.ts +14 -0
- package/src/BlendModes/blend-modes-fast.ts +9 -13
- package/src/BlendModes/blend-modes-perfect.ts +7 -7
- package/src/BlendModes/blend-modes.ts +3 -0
- package/src/IndexedImage/IndexedImage.ts +24 -22
- package/src/IndexedImage/getIndexedImageColorCounts.ts +20 -0
- package/src/IndexedImage/indexedImageToAverageColor.ts +7 -6
- package/src/_types.ts +6 -0
- package/src/index.ts +11 -2
package/dist/index.dev.cjs
CHANGED
|
@@ -111,7 +111,10 @@ __export(src_exports, {
|
|
|
111
111
|
pinLightFast: () => pinLightFast,
|
|
112
112
|
pinLightPerfect: () => pinLightPerfect,
|
|
113
113
|
pixelDataToAlphaMask: () => pixelDataToAlphaMask,
|
|
114
|
+
reflectPixelDataHorizontal: () => reflectPixelDataHorizontal,
|
|
115
|
+
reflectPixelDataVertical: () => reflectPixelDataVertical,
|
|
114
116
|
resizeImageData: () => resizeImageData,
|
|
117
|
+
rotatePixelData: () => rotatePixelData,
|
|
115
118
|
screenFast: () => screenFast,
|
|
116
119
|
screenPerfect: () => screenPerfect,
|
|
117
120
|
serializeImageData: () => serializeImageData,
|
|
@@ -1734,24 +1737,22 @@ function makeIndexedImage(imageData) {
|
|
|
1734
1737
|
const rawData = new Uint32Array(imageData.data.buffer);
|
|
1735
1738
|
const indexedData = new Int32Array(rawData.length);
|
|
1736
1739
|
const colorMap = /* @__PURE__ */ new Map();
|
|
1737
|
-
const tempPalette = [];
|
|
1738
1740
|
const transparentColor = 0;
|
|
1739
1741
|
const transparentPalletIndex = 0;
|
|
1740
1742
|
colorMap.set(transparentColor, transparentPalletIndex);
|
|
1741
|
-
tempPalette.push(transparentColor);
|
|
1742
1743
|
for (let i = 0; i < rawData.length; i++) {
|
|
1743
1744
|
const pixel = rawData[i];
|
|
1744
|
-
const
|
|
1745
|
+
const alpha = pixel >>> 24 & 255;
|
|
1746
|
+
const isTransparent = alpha === 0;
|
|
1745
1747
|
const colorKey = isTransparent ? transparentColor : pixel;
|
|
1746
1748
|
let id = colorMap.get(colorKey);
|
|
1747
1749
|
if (id === void 0) {
|
|
1748
1750
|
id = colorMap.size;
|
|
1749
|
-
tempPalette.push(colorKey);
|
|
1750
1751
|
colorMap.set(colorKey, id);
|
|
1751
1752
|
}
|
|
1752
1753
|
indexedData[i] = id;
|
|
1753
1754
|
}
|
|
1754
|
-
const palette = new Int32Array(
|
|
1755
|
+
const palette = new Int32Array(colorMap.keys());
|
|
1755
1756
|
return {
|
|
1756
1757
|
width,
|
|
1757
1758
|
height,
|
|
@@ -1764,7 +1765,7 @@ function makeIndexedImage(imageData) {
|
|
|
1764
1765
|
// src/IndexedImage/indexedImageToAverageColor.ts
|
|
1765
1766
|
function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
|
|
1766
1767
|
const { data, palette, transparentPalletIndex } = indexedImage;
|
|
1767
|
-
const counts = new Uint32Array(palette.length
|
|
1768
|
+
const counts = new Uint32Array(palette.length);
|
|
1768
1769
|
for (let i = 0; i < data.length; i++) {
|
|
1769
1770
|
const id = data[i];
|
|
1770
1771
|
counts[id]++;
|
|
@@ -1782,11 +1783,11 @@ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
|
|
|
1782
1783
|
if (!includeTransparent && id === transparentPalletIndex) {
|
|
1783
1784
|
continue;
|
|
1784
1785
|
}
|
|
1785
|
-
const
|
|
1786
|
-
const r2 =
|
|
1787
|
-
const g2 =
|
|
1788
|
-
const b2 =
|
|
1789
|
-
const a2 =
|
|
1786
|
+
const color = palette[id] >>> 0;
|
|
1787
|
+
const r2 = color & 255;
|
|
1788
|
+
const g2 = color >> 8 & 255;
|
|
1789
|
+
const b2 = color >> 16 & 255;
|
|
1790
|
+
const a2 = color >> 24 & 255;
|
|
1790
1791
|
rSum += r2 * weight;
|
|
1791
1792
|
gSum += g2 * weight;
|
|
1792
1793
|
bSum += b2 * weight;
|
|
@@ -2391,6 +2392,84 @@ function invertPixelData(pixelData) {
|
|
|
2391
2392
|
}
|
|
2392
2393
|
return pixelData;
|
|
2393
2394
|
}
|
|
2395
|
+
|
|
2396
|
+
// src/PixelData/reflectPixelData.ts
|
|
2397
|
+
function reflectPixelDataHorizontal(pixelData) {
|
|
2398
|
+
const width = pixelData.width;
|
|
2399
|
+
const height = pixelData.height;
|
|
2400
|
+
const data = pixelData.data32;
|
|
2401
|
+
const halfWidth = Math.floor(width / 2);
|
|
2402
|
+
for (let y = 0; y < height; y++) {
|
|
2403
|
+
const rowOffset = y * width;
|
|
2404
|
+
for (let x = 0; x < halfWidth; x++) {
|
|
2405
|
+
const leftIdx = rowOffset + x;
|
|
2406
|
+
const rightIdx = rowOffset + (width - 1 - x);
|
|
2407
|
+
const temp = data[leftIdx];
|
|
2408
|
+
data[leftIdx] = data[rightIdx];
|
|
2409
|
+
data[rightIdx] = temp;
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
}
|
|
2413
|
+
function reflectPixelDataVertical(pixelData) {
|
|
2414
|
+
const width = pixelData.width;
|
|
2415
|
+
const height = pixelData.height;
|
|
2416
|
+
const data = pixelData.data32;
|
|
2417
|
+
const halfHeight = Math.floor(height / 2);
|
|
2418
|
+
for (let y = 0; y < halfHeight; y++) {
|
|
2419
|
+
const topRowOffset = y * width;
|
|
2420
|
+
const bottomRowOffset = (height - 1 - y) * width;
|
|
2421
|
+
for (let x = 0; x < width; x++) {
|
|
2422
|
+
const topIdx = topRowOffset + x;
|
|
2423
|
+
const bottomIdx = bottomRowOffset + x;
|
|
2424
|
+
const temp = data[topIdx];
|
|
2425
|
+
data[topIdx] = data[bottomIdx];
|
|
2426
|
+
data[bottomIdx] = temp;
|
|
2427
|
+
}
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
// src/PixelData/rotatePixelData.ts
|
|
2432
|
+
function rotatePixelData(pixelData) {
|
|
2433
|
+
const width = pixelData.width;
|
|
2434
|
+
const height = pixelData.height;
|
|
2435
|
+
const data = pixelData.data32;
|
|
2436
|
+
if (width === height) {
|
|
2437
|
+
rotateSquareInPlace(pixelData);
|
|
2438
|
+
return;
|
|
2439
|
+
}
|
|
2440
|
+
const newWidth = height;
|
|
2441
|
+
const newHeight = width;
|
|
2442
|
+
const newData = new Uint32Array(data.length);
|
|
2443
|
+
for (let y = 0; y < height; y++) {
|
|
2444
|
+
for (let x = 0; x < width; x++) {
|
|
2445
|
+
const oldIdx = y * width + x;
|
|
2446
|
+
const newX = height - 1 - y;
|
|
2447
|
+
const newY = x;
|
|
2448
|
+
const newIdx = newY * newWidth + newX;
|
|
2449
|
+
newData[newIdx] = data[oldIdx];
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
pixelData.width = newWidth;
|
|
2453
|
+
pixelData.height = newHeight;
|
|
2454
|
+
pixelData.data32 = newData;
|
|
2455
|
+
}
|
|
2456
|
+
function rotateSquareInPlace(pixelData) {
|
|
2457
|
+
const n = pixelData.width;
|
|
2458
|
+
const data = pixelData.data32;
|
|
2459
|
+
for (let i = 0; i < n / 2; i++) {
|
|
2460
|
+
for (let j = i; j < n - i - 1; j++) {
|
|
2461
|
+
const temp = data[i * n + j];
|
|
2462
|
+
const top = i * n + j;
|
|
2463
|
+
const right = j * n + (n - 1 - i);
|
|
2464
|
+
const bottom = (n - 1 - i) * n + (n - 1 - j);
|
|
2465
|
+
const left = (n - 1 - j) * n + i;
|
|
2466
|
+
data[top] = data[left];
|
|
2467
|
+
data[left] = data[bottom];
|
|
2468
|
+
data[bottom] = data[right];
|
|
2469
|
+
data[right] = temp;
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
}
|
|
2394
2473
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2395
2474
|
0 && (module.exports = {
|
|
2396
2475
|
BlendMode,
|
|
@@ -2484,7 +2563,10 @@ function invertPixelData(pixelData) {
|
|
|
2484
2563
|
pinLightFast,
|
|
2485
2564
|
pinLightPerfect,
|
|
2486
2565
|
pixelDataToAlphaMask,
|
|
2566
|
+
reflectPixelDataHorizontal,
|
|
2567
|
+
reflectPixelDataVertical,
|
|
2487
2568
|
resizeImageData,
|
|
2569
|
+
rotatePixelData,
|
|
2488
2570
|
screenFast,
|
|
2489
2571
|
screenPerfect,
|
|
2490
2572
|
serializeImageData,
|