pixel-data-js 0.5.1 → 0.5.3

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.
@@ -63,6 +63,7 @@ __export(src_exports, {
63
63
  imageDataToDataUrl: () => imageDataToDataUrl,
64
64
  imageDataToImgBlob: () => imageDataToImgBlob,
65
65
  imgBlobToImageData: () => imgBlobToImageData,
66
+ indexedImageToAverageColor: () => indexedImageToAverageColor,
66
67
  invertAlphaMask: () => invertAlphaMask,
67
68
  invertBinaryMask: () => invertBinaryMask,
68
69
  invertImageData: () => invertImageData,
@@ -74,6 +75,7 @@ __export(src_exports, {
74
75
  linearBurnColor32: () => linearBurnColor32,
75
76
  linearDodgeColor32: () => linearDodgeColor32,
76
77
  linearLightColor32: () => linearLightColor32,
78
+ makeIndexedImage: () => makeIndexedImage,
77
79
  makePixelCanvas: () => makePixelCanvas,
78
80
  makeReusableCanvas: () => makeReusableCanvas,
79
81
  mergeMasks: () => mergeMasks,
@@ -1182,6 +1184,95 @@ function writeImageDataPixels(imageData, data, _x, _y, _w, _h) {
1182
1184
  }
1183
1185
  }
1184
1186
 
1187
+ // src/IndexedImage/IndexedImage.ts
1188
+ function makeIndexedImage(imageData) {
1189
+ const width = imageData.width;
1190
+ const height = imageData.height;
1191
+ const rawData = imageData.data;
1192
+ const indexedData = new Int32Array(rawData.length / 4);
1193
+ const colorMap = /* @__PURE__ */ new Map();
1194
+ const tempPalette = [];
1195
+ const transparentKey = "0,0,0,0";
1196
+ const transparentPalletIndex = 0;
1197
+ colorMap.set(transparentKey, transparentPalletIndex);
1198
+ tempPalette.push(0);
1199
+ tempPalette.push(0);
1200
+ tempPalette.push(0);
1201
+ tempPalette.push(0);
1202
+ for (let i = 0; i < indexedData.length; i++) {
1203
+ const r = rawData[i * 4];
1204
+ const g = rawData[i * 4 + 1];
1205
+ const b = rawData[i * 4 + 2];
1206
+ const a = rawData[i * 4 + 3];
1207
+ let key;
1208
+ if (a === 0) {
1209
+ key = transparentKey;
1210
+ } else {
1211
+ key = `${r},${g},${b},${a}`;
1212
+ }
1213
+ let id = colorMap.get(key);
1214
+ if (id === void 0) {
1215
+ id = colorMap.size;
1216
+ tempPalette.push(r);
1217
+ tempPalette.push(g);
1218
+ tempPalette.push(b);
1219
+ tempPalette.push(a);
1220
+ colorMap.set(key, id);
1221
+ }
1222
+ indexedData[i] = id;
1223
+ }
1224
+ const palette = new Uint8Array(tempPalette);
1225
+ return {
1226
+ width,
1227
+ height,
1228
+ data: indexedData,
1229
+ transparentPalletIndex,
1230
+ palette
1231
+ };
1232
+ }
1233
+
1234
+ // src/IndexedImage/indexedImageToAverageColor.ts
1235
+ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1236
+ const { data, palette, transparentPalletIndex } = indexedImage;
1237
+ const counts = new Uint32Array(palette.length / 4);
1238
+ for (let i = 0; i < data.length; i++) {
1239
+ const id = data[i];
1240
+ counts[id]++;
1241
+ }
1242
+ let rSum = 0;
1243
+ let gSum = 0;
1244
+ let bSum = 0;
1245
+ let aSum = 0;
1246
+ let totalWeight = 0;
1247
+ for (let id = 0; id < counts.length; id++) {
1248
+ const weight = counts[id];
1249
+ if (weight === 0) {
1250
+ continue;
1251
+ }
1252
+ if (!includeTransparent && id === transparentPalletIndex) {
1253
+ continue;
1254
+ }
1255
+ const pIdx = id * 4;
1256
+ const r2 = palette[pIdx];
1257
+ const g2 = palette[pIdx + 1];
1258
+ const b2 = palette[pIdx + 2];
1259
+ const a2 = palette[pIdx + 3];
1260
+ rSum += r2 * weight;
1261
+ gSum += g2 * weight;
1262
+ bSum += b2 * weight;
1263
+ aSum += a2 * weight;
1264
+ totalWeight += weight;
1265
+ }
1266
+ if (totalWeight === 0) {
1267
+ return packColor(0, 0, 0, 0);
1268
+ }
1269
+ const r = rSum / totalWeight | 0;
1270
+ const g = gSum / totalWeight | 0;
1271
+ const b = bSum / totalWeight | 0;
1272
+ const a = aSum / totalWeight | 0;
1273
+ return packColor(r, g, b, a);
1274
+ }
1275
+
1185
1276
  // src/Input/fileInputChangeToImageData.ts
1186
1277
  async function fileInputChangeToImageData(event) {
1187
1278
  const target = event.target;
@@ -1351,7 +1442,7 @@ function mergeMasks(dst, dstWidth, src, opts) {
1351
1442
  }
1352
1443
  }
1353
1444
 
1354
- // src/PixelData.ts
1445
+ // src/PixelData/PixelData.ts
1355
1446
  var PixelData = class {
1356
1447
  constructor(imageData) {
1357
1448
  this.imageData = imageData;
@@ -1805,6 +1896,7 @@ function invertPixelData(pixelData) {
1805
1896
  imageDataToDataUrl,
1806
1897
  imageDataToImgBlob,
1807
1898
  imgBlobToImageData,
1899
+ indexedImageToAverageColor,
1808
1900
  invertAlphaMask,
1809
1901
  invertBinaryMask,
1810
1902
  invertImageData,
@@ -1816,6 +1908,7 @@ function invertPixelData(pixelData) {
1816
1908
  linearBurnColor32,
1817
1909
  linearDodgeColor32,
1818
1910
  linearLightColor32,
1911
+ makeIndexedImage,
1819
1912
  makePixelCanvas,
1820
1913
  makeReusableCanvas,
1821
1914
  mergeMasks,