pixelkiln 0.143.0 → 0.144.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.
package/dist/cli.js CHANGED
@@ -3551,6 +3551,7 @@ var ParallaxTintSchema = z.object({
3551
3551
  /** 0 leaves the picture as it is; 1 makes every pixel the colour, snapped. */
3552
3552
  amount: z.number().min(0).max(1)
3553
3553
  }).strict();
3554
+ var PARALLAX_LOOPS = ["cut", "crossfade", "mirror"];
3554
3555
  var ParallaxLayerSchema = z.object({
3555
3556
  /** Names the layer in exports; defaults to the asset id, the file name, or "sky". */
3556
3557
  name: z.string().regex(/^[A-Za-z][A-Za-z0-9_-]*$/, "letters, digits, - and _, starting with a letter").optional(),
@@ -3603,7 +3604,14 @@ var ParallaxLayerSchema = z.object({
3603
3604
  */
3604
3605
  foot: z.number().gt(0).max(1).optional(),
3605
3606
  /** Pull the picture (each sprite of a run) toward a colour and snap it to the set's palette. Applied after `foot`. */
3606
- tint: ParallaxTintSchema.optional()
3607
+ tint: ParallaxTintSchema.optional(),
3608
+ /**
3609
+ * Make a picture that repeats sideways join smoothly, before `foot` and
3610
+ * `tint`: `cut` crops it to the two columns that best continue into each
3611
+ * other, `crossfade` blends its last eighth into its first and snaps the
3612
+ * blend to the picture's colours, `mirror` adds a flipped copy beside it.
3613
+ */
3614
+ loop: z.enum(PARALLAX_LOOPS).optional()
3607
3615
  }).strict().superRefine((layer, ctx) => {
3608
3616
  const sources = [layer.asset, layer.file, layer.sky, layer.run].filter((value) => value !== void 0).length;
3609
3617
  if (sources !== 1) {
@@ -3622,6 +3630,10 @@ var ParallaxLayerSchema = z.object({
3622
3630
  if (layer[key3] !== void 0) ctx.addIssue({ code: z.ZodIssueCode.custom, path: [key3], message: `${key3} does not apply to a sky layer` });
3623
3631
  }
3624
3632
  }
3633
+ if (layer.loop !== void 0) {
3634
+ const why = layer.sky !== void 0 ? "a sky layer is drawn to the view's width" : layer.run !== void 0 ? "a run is laid as a loop already" : layer.repeat === false ? "the layer does not repeat" : void 0;
3635
+ if (why) ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["loop"], message: `loop does not apply: ${why}` });
3636
+ }
3625
3637
  if (layer.foot !== void 0 && layer.extend !== void 0) {
3626
3638
  ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["extend"], message: "extend repeats the bottom row, which foot thins out; use one or the other" });
3627
3639
  }
@@ -35844,19 +35856,26 @@ async function resolveParallaxSet(manifest, manifestDir, lock, setId) {
35844
35856
  const finish = (picture, keepWidth) => finishPicture(picture, { foot: layer.foot, tint: layer.tint, palette, keepWidth }, `parallax ${setId}: ${name}`);
35845
35857
  let png;
35846
35858
  let source;
35859
+ let loop;
35847
35860
  if (layer.sky) {
35848
35861
  png = skyBands(viewW, viewH + Math.ceil(climb * speedY), layer.sky);
35849
35862
  source = { kind: "sky", colors: layer.sky };
35850
35863
  } else if (layer.run) {
35851
35864
  const sprites = await Promise.all(layer.run.map((sprite) => readPicture(manifest, manifestDir, lock, setId, name, sprite, warnings)));
35852
35865
  const seed = layer.seed ?? seedFromName(name);
35853
- const loop = layer.repeat ?? true;
35854
- const width = layer.width ?? Math.max(1, viewW + Math.ceil(travel * layer.speed) - (loop ? 0 : layer.x ?? 0));
35855
- png = layRun(sprites.map((sprite) => finish(sprite.png, false)), width, { seed, step: layer.step, gap: layer.gap, jitter: layer.jitter, loop });
35866
+ const loop2 = layer.repeat ?? true;
35867
+ const width = layer.width ?? Math.max(1, viewW + Math.ceil(travel * layer.speed) - (loop2 ? 0 : layer.x ?? 0));
35868
+ png = layRun(sprites.map((sprite) => finish(sprite.png, false)), width, { seed, step: layer.step, gap: layer.gap, jitter: layer.jitter, loop: loop2 });
35856
35869
  source = { kind: "run", seed, sprites: sprites.map((sprite) => sprite.source) };
35857
35870
  } else {
35858
35871
  const picture = await readPicture(manifest, manifestDir, lock, setId, name, layer, warnings);
35859
- png = finish(picture.png, repeat);
35872
+ png = picture.png;
35873
+ if (layer.loop) {
35874
+ const looped = loopPicture(png, layer.loop, `parallax ${setId}: ${name}`);
35875
+ png = looped.png;
35876
+ loop = looped.loop;
35877
+ }
35878
+ png = finish(png, repeat);
35860
35879
  source = picture.source;
35861
35880
  }
35862
35881
  let bottom = (layer.bottom ?? viewH) + (layer.run ? layer.jitter ?? 0 : 0);
@@ -35877,11 +35896,12 @@ async function resolveParallaxSet(manifest, manifestDir, lock, setId) {
35877
35896
  source
35878
35897
  };
35879
35898
  if (layer.foot !== void 0 || layer.tint) image.finish = { foot: layer.foot, tint: layer.tint };
35899
+ if (loop) image.loop = loop;
35880
35900
  if (repeat && source.kind !== "sky" && source.kind !== "run") {
35881
35901
  image.seamRatio = seamRatio(png);
35882
35902
  if (travel * layer.speed > 0 && image.seamRatio > SEAM_WARN_RATIO) {
35883
35903
  warnings.push(
35884
- `${name}: its sideways join is ${image.seamRatio.toFixed(1)} times rougher than the picture's own steps; loop it first (research/scripts/parallax.py loop) or set repeat: false`
35904
+ `${name}: its sideways join is ${image.seamRatio.toFixed(1)} times rougher than the picture's own steps; ` + (layer.loop ? `the ${layer.loop} loop did not smooth it; try another, or set repeat: false` : 'set "loop": "cut" to smooth it, or set repeat: false')
35885
35905
  );
35886
35906
  }
35887
35907
  }
@@ -36004,6 +36024,88 @@ function roundHalfEven(value) {
36004
36024
  if (rest < 0.5) return floor;
36005
36025
  return floor % 2 === 0 ? floor : floor + 1;
36006
36026
  }
36027
+ function loopPicture(png, method, where = "picture") {
36028
+ const { width: w, height: h } = png;
36029
+ if (w < 8) throw new Error(`${where}: ${w} px wide is too narrow to loop; loop needs at least 8 columns`);
36030
+ const columns = (left, right) => {
36031
+ const out = Buffer.alloc((right - left) * h * 4);
36032
+ for (let y = 0; y < h; y++) png.pixels.copy(out, y * (right - left) * 4, (y * w + left) * 4, (y * w + right) * 4);
36033
+ return { width: right - left, height: h, pixels: out };
36034
+ };
36035
+ if (method === "cut") {
36036
+ const pre = new Int32Array(w * h * 4);
36037
+ for (let x = 0; x < w; x++) {
36038
+ for (let y = 0; y < h; y++) {
36039
+ const p = (y * w + x) * 4;
36040
+ const q = (x * h + y) * 4;
36041
+ const alpha = png.pixels[p + 3];
36042
+ for (let c = 0; c < 3; c++) pre[q + c] = Math.floor(png.pixels[p + c] * alpha / 255);
36043
+ pre[q + 3] = alpha;
36044
+ }
36045
+ }
36046
+ const span = h * 4;
36047
+ const diff = (a, b) => {
36048
+ let sum = 0;
36049
+ for (let k = 0; k < span; k++) sum += Math.abs(pre[a * span + k] - pre[b * span + k]);
36050
+ return sum;
36051
+ };
36052
+ let best = Infinity;
36053
+ let cut = [0, w];
36054
+ const apart = Math.floor(w * 0.6);
36055
+ for (let i = 0; i < w; i++) {
36056
+ for (let j = i + apart; j <= w; j++) {
36057
+ const d = diff(j - 1, i);
36058
+ if (d < best) {
36059
+ best = d;
36060
+ cut = [i, j];
36061
+ }
36062
+ }
36063
+ }
36064
+ return { png: columns(cut[0], cut[1]), loop: { method, from: w, columns: cut } };
36065
+ }
36066
+ if (method === "crossfade") {
36067
+ const k = Math.floor(w / 8);
36068
+ const out = columns(0, w - k);
36069
+ const seen = /* @__PURE__ */ new Map();
36070
+ for (let p = 0; p < png.pixels.length; p += 4) {
36071
+ seen.set(png.pixels[p] << 16 | png.pixels[p + 1] << 8 | png.pixels[p + 2], [png.pixels[p], png.pixels[p + 1], png.pixels[p + 2]]);
36072
+ }
36073
+ const colours = [...seen.values()].sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]);
36074
+ const nearest = /* @__PURE__ */ new Map();
36075
+ for (let x = 0; x < k; x++) {
36076
+ const t = (x + 0.5) / k;
36077
+ for (let y = 0; y < h; y++) {
36078
+ const head = (y * w + x) * 4;
36079
+ const tail = (y * w + w - k + x) * 4;
36080
+ const mixed = [0, 1, 2].map((c) => roundHalfEven(png.pixels[tail + c] * (1 - t) + png.pixels[head + c] * t));
36081
+ const key3 = mixed[0] << 16 | mixed[1] << 8 | mixed[2];
36082
+ let snapped = nearest.get(key3);
36083
+ if (!snapped) {
36084
+ let bestDistance = Infinity;
36085
+ for (const colour of colours) {
36086
+ const d = (mixed[0] - colour[0]) ** 2 + (mixed[1] - colour[1]) ** 2 + (mixed[2] - colour[2]) ** 2;
36087
+ if (d < bestDistance) {
36088
+ bestDistance = d;
36089
+ snapped = colour;
36090
+ }
36091
+ }
36092
+ nearest.set(key3, snapped);
36093
+ }
36094
+ const o = (y * out.width + x) * 4;
36095
+ out.pixels[o] = snapped[0];
36096
+ out.pixels[o + 1] = snapped[1];
36097
+ out.pixels[o + 2] = snapped[2];
36098
+ }
36099
+ }
36100
+ return { png: out, loop: { method, from: w } };
36101
+ }
36102
+ const pixels = Buffer.alloc(w * 2 * h * 4);
36103
+ for (let y = 0; y < h; y++) {
36104
+ png.pixels.copy(pixels, y * w * 2 * 4, y * w * 4, (y + 1) * w * 4);
36105
+ for (let x = 0; x < w; x++) png.pixels.copy(pixels, (y * w * 2 + w + x) * 4, (y * w + w - 1 - x) * 4, (y * w + w - x) * 4);
36106
+ }
36107
+ return { png: { width: w * 2, height: h, pixels }, loop: { method, from: w } };
36108
+ }
36007
36109
  async function readPicture(manifest, manifestDir, lock, setId, name, ref, warnings) {
36008
36110
  if (ref.file) {
36009
36111
  const file2 = path73.resolve(manifestDir, ref.file);
@@ -36630,6 +36732,7 @@ function setOptions(set) {
36630
36732
  repeatY: layer.repeatY,
36631
36733
  ...layer.source.kind === "sky" ? { sky: layer.source.colors } : {},
36632
36734
  ...layer.source.kind === "run" ? { run: { seed: layer.source.seed, sprites: layer.source.sprites.length, width: layer.png.width } } : {},
36735
+ ...layer.loop ? { loop: layer.loop } : {},
36633
36736
  ...layer.finish ?? {}
36634
36737
  })),
36635
36738
  ...set.palette ? { palette: set.palette } : {}