pixelkiln 0.148.0 → 0.148.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.cjs CHANGED
@@ -7362,10 +7362,10 @@ function despeckle(png, options) {
7362
7362
  for (const still of options.reference) {
7363
7363
  const { pixels } = decodePng(still);
7364
7364
  for (let i = 0; i < pixels.length; i += 4) {
7365
- if (pixels[i + 3] === 0 || outline(pixels, i)) continue;
7365
+ if (pixels[i + 3] === 0) continue;
7366
7366
  const rgb = pixels[i] << 16 | pixels[i + 1] << 8 | pixels[i + 2];
7367
7367
  palette.set(rgb, (palette.get(rgb) ?? 0) + 1);
7368
- total++;
7368
+ if (!outline(pixels, i)) total++;
7369
7369
  }
7370
7370
  }
7371
7371
  if (!total) throw new Error("the reference stills have no opaque pixels that are not outline");
@@ -7425,7 +7425,7 @@ function despeckle(png, options) {
7425
7425
  const strange = run.filter(([x, y]) => foreign(at(image, x, y))).length / run.length > 0.5;
7426
7426
  let debris;
7427
7427
  const exposed = run.some(([x, y]) => STEPS.some(([dx, dy]) => open(x + dx, y + dy)));
7428
- if (strange && !exposed && run.length > limb) {
7428
+ if (strange && run.length > limb) {
7429
7429
  debris = false;
7430
7430
  limbs.add(index);
7431
7431
  } else if (exposed) {
@@ -7460,6 +7460,7 @@ function despeckle(png, options) {
7460
7460
  if (src[at(image, x, y) + 3] > 0 && out[at(image, x, y) + 3] === 0) frontier.push([x, y]);
7461
7461
  }
7462
7462
  }
7463
+ const seeds = frontier.length;
7463
7464
  for (let n = 0; n < frontier.length; n++) {
7464
7465
  const [fx, fy] = frontier[n];
7465
7466
  for (let dy = -1; dy <= 1; dy++) {
@@ -7483,6 +7484,31 @@ function despeckle(png, options) {
7483
7484
  }
7484
7485
  }
7485
7486
  }
7487
+ const chained = new Set(frontier.slice(seeds).map(([x, y]) => local(x, y)));
7488
+ const visited = /* @__PURE__ */ new Set();
7489
+ for (const [sx, sy] of frontier.slice(seeds)) {
7490
+ if (visited.has(local(sx, sy))) continue;
7491
+ const group = [[sx, sy]];
7492
+ visited.add(local(sx, sy));
7493
+ for (let n = 0; n < group.length; n++) {
7494
+ const [gx, gy] = group[n];
7495
+ for (let dy = -1; dy <= 1; dy++) {
7496
+ for (let dx = -1; dx <= 1; dx++) {
7497
+ const x = gx + dx;
7498
+ const y = gy + dy;
7499
+ if (!inside(cell, x, y) || visited.has(local(x, y)) || !chained.has(local(x, y))) continue;
7500
+ visited.add(local(x, y));
7501
+ group.push([x, y]);
7502
+ }
7503
+ }
7504
+ }
7505
+ if (group.length <= limb) continue;
7506
+ for (const [x, y] of group) {
7507
+ const i = at(image, x, y);
7508
+ src.copy(out, i, i, i + 4);
7509
+ removed--;
7510
+ }
7511
+ }
7486
7512
  }
7487
7513
  return { png: encodeRgbaPng(image.width, image.height, out), removed };
7488
7514
  }
@@ -8286,10 +8312,13 @@ function applyPostprocess(bytes, mediaType, record, reference = [], role) {
8286
8312
  if (mediaType !== MediaType.PNG || !record?.palette && !record?.despeckle && !record?.offset) return { bytes, changed: false };
8287
8313
  let out = bytes;
8288
8314
  let removed = 0;
8315
+ let opaque2 = 0;
8289
8316
  if (record.despeckle) {
8290
8317
  if (!reference.length) throw new Error(`cleaning needs the rotations of ${record.despeckle.of}, and none were given`);
8291
8318
  const cleaned = despeckle(out, { reference });
8292
8319
  if (cleaned.removed) {
8320
+ const { pixels } = decodePng(out);
8321
+ for (let i = 3; i < pixels.length; i += 4) if (pixels[i] > 0) opaque2++;
8293
8322
  out = cleaned.png;
8294
8323
  removed = cleaned.removed;
8295
8324
  }
@@ -8313,8 +8342,9 @@ function applyPostprocess(bytes, mediaType, record, reference = [], role) {
8313
8342
  }
8314
8343
  }
8315
8344
  if (!removed && !snapped && !moved) return { bytes, changed: false };
8316
- return { bytes: out, changed: true, ...removed ? { removed } : {}, ...snapped ? { snapped } : {}, ...moved ? { moved } : {} };
8345
+ return { bytes: out, changed: true, ...removed ? { removed, opaque: opaque2 } : {}, ...snapped ? { snapped } : {}, ...moved ? { moved } : {} };
8317
8346
  }
8347
+ var CLEAN_DAMAGE_SHARE = 0.03;
8318
8348
 
8319
8349
  // src/pipeline/mirror.ts
8320
8350
  function mirrorSourceHash(source) {
@@ -17579,10 +17609,15 @@ async function buildPlan(specs, lock, opts = {}) {
17579
17609
  } else if (spec.mirror && entry.status === "downloaded" && entry.outputs.some((_output, index) => !(0, import_node_fs16.existsSync)(currentEntryOutputPath(entry, spec, index)))) {
17580
17610
  state = "stale";
17581
17611
  reason = "mirror output missing on disk; flip it again (no generation cost)";
17582
- } else if (spec.character?.kind === "outfit" && entry.outfit?.sourceSha256 !== mirrorSourceHash(lock.entries[lockKey(spec.styleId, spec.character.parentAssetId)] ?? { outputs: [] })) {
17612
+ } else if (
17613
+ // A mirror carries its source's character shape, but it is a flip of
17614
+ // the source's files: whether it is current is the check above, never
17615
+ // the outfit or layer rule its source answers to (#418).
17616
+ !spec.mirror && spec.character?.kind === "outfit" && entry.outfit?.sourceSha256 !== mirrorSourceHash(lock.entries[lockKey(spec.styleId, spec.character.parentAssetId)] ?? { outputs: [] })
17617
+ ) {
17583
17618
  state = "stale";
17584
17619
  reason = `outfit source ${spec.character.parentAssetId} changed; re-clothe the loop again`;
17585
- } else if (spec.character?.kind === "layer" && changedLayerSource(spec, entry, lock)) {
17620
+ } else if (!spec.mirror && spec.character?.kind === "layer" && changedLayerSource(spec, entry, lock)) {
17586
17621
  state = "stale";
17587
17622
  reason = isHeldItem(spec) ? `layer source ${changedLayerSource(spec, entry, lock)} changed; place the item again (no generation cost)` : `layer source ${changedLayerSource(spec, entry, lock)} changed; dress the frames again`;
17588
17623
  } else if (isHeldItem(spec) && entry.status === "downloaded" && entry.outputs.some((_output, index) => !(0, import_node_fs16.existsSync)(currentEntryOutputPath(entry, spec, index)))) {
@@ -17650,6 +17685,7 @@ async function buildPlan(specs, lock, opts = {}) {
17650
17685
  }
17651
17686
  const isActionable = (i) => i.state === "missing" || i.state === "stale" || i.state === "failed";
17652
17687
  const stateOf = new Map(items.map((i) => [i.key, i.state]));
17688
+ const waiting = /* @__PURE__ */ new Set();
17653
17689
  for (const item of items) {
17654
17690
  if (!isActionable(item)) continue;
17655
17691
  for (const parent of dependencies(item.spec)) {
@@ -17658,11 +17694,35 @@ async function buildPlan(specs, lock, opts = {}) {
17658
17694
  if (parentState && isActionable({ state: parentState })) {
17659
17695
  item.state = "blocked";
17660
17696
  item.reason = `parent ${parentKey} is being generated in this run; this follows in the next wave`;
17697
+ waiting.add(item.key);
17661
17698
  break;
17662
17699
  }
17663
17700
  if ((item.spec.mirror || item.spec.derive || isHeldItem(item.spec)) && (parentState === "recoverable" || parentState === "in-flight")) {
17664
17701
  item.state = "blocked";
17665
17702
  item.reason = `mirror source ${parentKey} is ${parentState}; this follows in the next wave`;
17703
+ waiting.add(item.key);
17704
+ break;
17705
+ }
17706
+ }
17707
+ }
17708
+ const byKey = new Map(items.map((i) => [i.key, i]));
17709
+ for (let changed = true; changed; ) {
17710
+ changed = false;
17711
+ for (const item of items) {
17712
+ if (item.state !== "blocked" || waiting.has(item.key)) continue;
17713
+ for (const parent of dependencies(item.spec)) {
17714
+ const parentKey = lockKey(parent.styleId, parent.assetId);
17715
+ const parentItem = byKey.get(parentKey);
17716
+ if (!parentItem || !item.reason.includes(`${parentKey} is not ready`)) continue;
17717
+ if (isActionable(parentItem)) {
17718
+ item.reason = `waits for ${parentKey}, which this run generates first; follows in a later wave (${item.reason})`;
17719
+ } else if (waiting.has(parentKey)) {
17720
+ item.reason = `waits for ${parentKey}, which itself follows in a later wave of this run (${item.reason})`;
17721
+ } else {
17722
+ continue;
17723
+ }
17724
+ waiting.add(item.key);
17725
+ changed = true;
17666
17726
  break;
17667
17727
  }
17668
17728
  }
@@ -20379,6 +20439,9 @@ async function fetchAssets(provider, specs, lock, lockPath, opts = {}) {
20379
20439
  if (processed.changed) rawHash = sha256(raw);
20380
20440
  if (processed.moved?.clipped) log(` moved ${import_node_path30.default.relative(process.cwd(), target)}: ${processed.moved.clipped} pixels fell off the canvas; raise layer.padding to keep them`);
20381
20441
  if (processed.removed) log(` cleaned ${import_node_path30.default.relative(process.cwd(), target)} (${processed.removed} ghost pixels)`);
20442
+ if (processed.removed && processed.opaque && processed.removed > processed.opaque * CLEAN_DAMAGE_SHARE) {
20443
+ log(` warning: cleaning took ${processed.removed} of ${processed.opaque} opaque pixels (${(100 * processed.removed / processed.opaque).toFixed(1)}%) from ${import_node_path30.default.relative(process.cwd(), target)}; check the frame for holes, and set clean: false then fetch to restore the provider's bytes`);
20444
+ }
20382
20445
  if (processed.snapped) log(` snapped ${import_node_path30.default.relative(process.cwd(), target)} to ${wanted.palette.colors.length} colours`);
20383
20446
  }
20384
20447
  if (cacheDir) await cacheMedia(cacheDir, buf, mediaType);