img-auto-parallax 0.1.1 → 0.1.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/cli.mjs CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import process from "node:process";
3
3
  import { parseArgs } from "node:util";
4
- import { r as precompute } from "./node-Cgcvci4U.mjs";
4
+ import { r as precompute } from "./node-DOo42478.mjs";
5
5
  // src/cli.ts
6
6
  /**
7
- * img-auto-parallax precompute <patterns...> [--model name] [--resolution n|full] [--suffix s] [--force]
7
+ * img-auto-parallax precompute <patterns...> [--model name] [--resolution n|full] [--map-size model|full] [--levels n] [--suffix s] [--force]
8
8
  */
9
9
  const HELP = `Usage: img-auto-parallax precompute <patterns...> [options]
10
10
 
@@ -12,7 +12,10 @@ Generate depth maps (photo.jpg -> photo.depth.png) for <img-auto-parallax src-de
12
12
 
13
13
  Options:
14
14
  --model <name> depth-anything (default) | midas | fastdepth
15
- --resolution <px> model input long side (default 518), or "full" (slow; output is 1:1 regardless)
15
+ --resolution <px> model input long side (default 518), or "full" (slow, usually softer)
16
+ --map-size <size> model (default): the model's output size, small; the browser
17
+ upsamples it along the photo's edges. full: 1:1 with the photo
18
+ --levels <n> gray levels in the map, 2-256 (default 128; fewer is smaller)
16
19
  --suffix <suffix> output suffix (default .depth.png)
17
20
  --force regenerate even when up to date
18
21
  -h, --help show this help
@@ -21,11 +24,13 @@ async function main() {
21
24
  const { values, positionals } = parseArgs({
22
25
  allowPositionals: true,
23
26
  options: {
24
- model: { type: "string" },
25
- resolution: { type: "string" },
26
- suffix: { type: "string" },
27
- force: { type: "boolean" },
28
- help: {
27
+ "model": { type: "string" },
28
+ "resolution": { type: "string" },
29
+ "map-size": { type: "string" },
30
+ "levels": { type: "string" },
31
+ "suffix": { type: "string" },
32
+ "force": { type: "boolean" },
33
+ "help": {
29
34
  type: "boolean",
30
35
  short: "h"
31
36
  }
@@ -41,6 +46,8 @@ async function main() {
41
46
  const result = await precompute(patterns, {
42
47
  model: values.model,
43
48
  resolution: values.resolution && values.resolution !== "full" ? Number(values.resolution) : void 0,
49
+ mapSize: values["map-size"] === "full" ? "full" : void 0,
50
+ levels: values.levels ? Number(values.levels) : void 0,
44
51
  suffix: values.suffix,
45
52
  force: values.force,
46
53
  onProgress: ({ output, status, index, total }) => {
@@ -45,11 +45,24 @@ interface DepthMapOptions {
45
45
  model?: DepthModelOption;
46
46
  /**
47
47
  * Long side of the model input in px (default 518, what Depth Anything is
48
- * trained at), or `'full'` for the image's own size up to 2048 px. The output
49
- * map is always 1:1 with the photo either way; `'full'` is ~60× slower, needs
50
- * gigabytes of memory, and usually comes out softer. Ignored by fixed-size models.
48
+ * trained at), or `'full'` for the image's own size up to 2048 px. `'full'` is
49
+ * ~60× slower, needs gigabytes of memory, and usually comes out softer. Ignored
50
+ * by fixed-size models. See `mapSize` for the size of the written map.
51
51
  */
52
52
  resolution?: number | 'full';
53
+ /**
54
+ * Size of the written map. `'model'` (default) keeps the model's own output size: the
55
+ * browser upsamples it along the photo's edges just as it does live, so it looks the
56
+ * same at a fraction of the bytes. `'full'` writes it already upsampled, 1:1 with the
57
+ * photo (up to 2048 px, several times larger), e.g. to inspect or retouch it by hand.
58
+ */
59
+ mapSize?: 'model' | 'full';
60
+ /**
61
+ * Gray levels in the written map (2–256, default 128). Fewer compress better; the
62
+ * browser's edge-aware upsampling smooths the steps, and 128 keeps them below what
63
+ * the parallax shows. 256 keeps full 8-bit precision.
64
+ */
65
+ levels?: number;
53
66
  }
54
67
  interface DepthMap {
55
68
  width: number;
@@ -73,11 +73,16 @@ function resizeRgba(src, sw, sh, dw, dh) {
73
73
  }
74
74
  return dst;
75
75
  }
76
- /** Nearness (0..1) to grayscale RGBA bytes: the precompiled depth map format. */
77
- function nearnessToRgba(nearness) {
76
+ /**
77
+ * Nearness (0..1) to grayscale RGBA bytes: the precompiled depth map format.
78
+ * `levels` (2–256, default 256) rounds to that many evenly spaced grays, which
79
+ * PNG compresses far better.
80
+ */
81
+ function nearnessToRgba(nearness, levels = 256) {
82
+ const steps = Math.min(255, Math.max(1, Math.round(levels) - 1));
78
83
  const rgba = new Uint8ClampedArray(nearness.length * 4);
79
84
  for (let i = 0; i < nearness.length; i++) {
80
- const v = Math.round(Math.min(1, Math.max(0, nearness[i])) * 255);
85
+ const v = Math.round(Math.round(Math.min(1, Math.max(0, nearness[i])) * steps) * 255 / steps);
81
86
  rgba[i * 4] = v;
82
87
  rgba[i * 4 + 1] = v;
83
88
  rgba[i * 4 + 2] = v;
@@ -288,7 +293,8 @@ function encodePng(rgba, width, height) {
288
293
  png.data = Buffer.from(rgba.buffer, rgba.byteOffset, rgba.byteLength);
289
294
  return new Uint8Array(PNG.sync.write(png, {
290
295
  colorType: 0,
291
- inputColorType: 6
296
+ inputColorType: 6,
297
+ deflateLevel: 9
292
298
  }));
293
299
  }
294
300
  async function loadModelBytes(model) {
@@ -334,11 +340,17 @@ async function generateDepthMap(image, options = {}) {
334
340
  const outW = dims.at(-1);
335
341
  const outH = dims.at(-2);
336
342
  const nearness = normalizeDepth(output.data, model.output);
343
+ const levels = options.levels ?? 128;
344
+ if (options.mapSize !== "full") return {
345
+ width: outW,
346
+ height: outH,
347
+ png: encodePng(nearnessToRgba(nearness, levels), outW, outH)
348
+ };
337
349
  const [width1, height1] = fitWithin(decoded.width, decoded.height, MAX_COLOUR_SIDE);
338
350
  return {
339
351
  width: width1,
340
352
  height: height1,
341
- png: encodePng(nearnessToRgba(jointBilateralUpsample(nearness, outW, outH, width1 === decoded.width && height1 === decoded.height ? decoded.data : resizeRgba(decoded.data, decoded.width, decoded.height, width1, height1), width1, height1)), width1, height1)
353
+ png: encodePng(nearnessToRgba(jointBilateralUpsample(nearness, outW, outH, width1 === decoded.width && height1 === decoded.height ? decoded.data : resizeRgba(decoded.data, decoded.width, decoded.height, width1, height1), width1, height1), levels), width1, height1)
342
354
  };
343
355
  }
344
356
  /**
@@ -359,7 +371,7 @@ async function precompute(patterns, options = {}) {
359
371
  try {
360
372
  cache = JSON.parse(await readFile(cacheFile, "utf8"));
361
373
  } catch {}
362
- const settingsKey = `v2:${String(resolveModel(options.model ?? "depth-anything", true).url).split("/").pop()}:${options.resolution ?? 518}`;
374
+ const settingsKey = `v2:${String(resolveModel(options.model ?? "depth-anything", true).url).split("/").pop()}:${options.resolution ?? 518}:${options.mapSize ?? "model"}:${options.levels ?? 128}`;
363
375
  const result = {
364
376
  generated: [],
365
377
  cached: []
package/dist/node.d.mts CHANGED
@@ -2,5 +2,5 @@
2
2
  * Node.js API: generate depth maps ahead of time, so pages can use
3
3
  * `<img-auto-parallax src-depth>` and never load a model in the browser.
4
4
  */
5
- import { a as decodeImage, c as depthMapPath, i as PrecomputeResult, n as DepthMapOptions, o as generateDepthMap, r as PrecomputeOptions, s as precompute, t as DepthMap } from "./node-C8vJqZAm.mjs";
5
+ import { a as decodeImage, c as depthMapPath, i as PrecomputeResult, n as DepthMapOptions, o as generateDepthMap, r as PrecomputeOptions, s as precompute, t as DepthMap } from "./node-BCog5g3V.mjs";
6
6
  export { DepthMap, DepthMapOptions, PrecomputeOptions, PrecomputeResult, decodeImage, depthMapPath, generateDepthMap, precompute };
package/dist/node.mjs CHANGED
@@ -1 +1 @@
1
- export { t as decodeImage, i as depthMapPath, n as generateDepthMap, r as precompute } from "./node-Cgcvci4U.mjs";
1
+ export { t as decodeImage, i as depthMapPath, n as generateDepthMap, r as precompute } from "./node-DOo42478.mjs";
package/dist/vite.d.mts CHANGED
@@ -3,7 +3,7 @@
3
3
  * `<img-auto-parallax src-depth="photo.depth.png">` with no model in the browser.
4
4
  */
5
5
  import { Plugin } from "vite";
6
- import { r as PrecomputeOptions } from "./node-C8vJqZAm.mjs";
6
+ import { r as PrecomputeOptions } from "./node-BCog5g3V.mjs";
7
7
  export interface DepthMapsPluginOptions extends Omit<PrecomputeOptions, 'cwd' | 'onProgress'> {
8
8
  /** Glob(s) relative to the Vite root, e.g. `public/images/*.jpg`. */
9
9
  include: string | string[];
package/dist/vite.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import process from "node:process";
2
- import { r as precompute } from "./node-Cgcvci4U.mjs";
2
+ import { r as precompute } from "./node-DOo42478.mjs";
3
3
  // src/vite.ts
4
4
  function depthMaps(options) {
5
5
  let root = process.cwd();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "img-auto-parallax",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "description": "Drop-in <img> replacement that estimates depth on the GPU and adds a pointer/gyro parallax effect",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",