@blazediff/cli 5.0.5 → 6.0.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.
@@ -9136,6 +9136,51 @@ module.exports = __toCommonJS(core_wasm_exports);
9136
9136
  var import_promises = require("fs/promises");
9137
9137
 
9138
9138
  // src/commands/_core-shared.ts
9139
+ var SHARED_FLAGS = ` -o, --output <path> Output path for the diff image
9140
+ -t, --threshold <num> Matching threshold (0 to 1, default: 0.1)
9141
+ --no-antialiasing Count anti-aliased pixels as differences
9142
+ --metric <name> Color metric: yiq (default) or oklab (pixelmatch 8)
9143
+ --mask <path> Image whose non-black pixels are skipped
9144
+ --window <n> Also report the n\xD7n window with the most differences
9145
+ -a, --alpha <num> Opacity of the original image in the diff (default: 0.1)
9146
+ --diff-color <r,g,b> Color for different pixels (default: 255,0,0)
9147
+ --diff-color-alt <r,g,b> Color for pixels that get darker (default: diff color)
9148
+ --aa-color <r,g,b> Color for anti-aliased pixels (default: 255,255,0)
9149
+ --background <name> faded (default) or transparent
9150
+ --json Print the result as JSON`;
9151
+ function parseMetric(value) {
9152
+ if (value === "yiq" || value === "oklab") return value;
9153
+ throw new Error(`Invalid metric: ${value}. Expected yiq or oklab`);
9154
+ }
9155
+ function parseWindow(value) {
9156
+ const window = Number(value);
9157
+ if (!Number.isInteger(window) || window < 1) {
9158
+ throw new Error(`Invalid window: ${value}. Must be a positive integer`);
9159
+ }
9160
+ return window;
9161
+ }
9162
+ function parseBackground(value) {
9163
+ if (value === "faded" || value === "transparent") return value;
9164
+ throw new Error(
9165
+ `Invalid background: ${value}. Expected faded or transparent`
9166
+ );
9167
+ }
9168
+ function parseUnit(name, value) {
9169
+ const number = Number.parseFloat(value);
9170
+ if (Number.isNaN(number) || number < 0 || number > 1) {
9171
+ throw new Error(`Invalid ${name}: ${value}. Must be between 0 and 1`);
9172
+ }
9173
+ return number;
9174
+ }
9175
+ function parseIntIn(name, value, min, max) {
9176
+ const number = Number.parseInt(value, 10);
9177
+ if (Number.isNaN(number) || number < min || number > max) {
9178
+ throw new Error(
9179
+ `Invalid ${name}: ${value}. Must be between ${min} and ${max}`
9180
+ );
9181
+ }
9182
+ return number;
9183
+ }
9139
9184
  function parseRGB(colorStr) {
9140
9185
  const parts = colorStr.split(",").map((s2) => parseInt(s2.trim(), 10));
9141
9186
  if (parts.length !== 3 || parts.some((p2) => Number.isNaN(p2) || p2 < 0 || p2 > 255)) {
@@ -9160,154 +9205,147 @@ var getCodec = async (codec) => {
9160
9205
  }
9161
9206
  throw new Error(`Unknown codec: ${codec}`);
9162
9207
  };
9163
- function parseCoreArgs(args, printUsage2) {
9208
+ async function loadMask(path, codecName) {
9209
+ const codec = await getCodec(codecName);
9210
+ const { data, width, height } = await codec.read(path);
9211
+ const mask = new Uint8Array(width * height);
9212
+ for (let i3 = 0; i3 < mask.length; i3++) {
9213
+ mask[i3] = data[i3 * 4] | data[i3 * 4 + 1] | data[i3 * 4 + 2] ? 1 : 0;
9214
+ }
9215
+ return mask;
9216
+ }
9217
+ function parseCoreArgs(args, printUsage2, { native = false } = {}) {
9164
9218
  if (args.length < 2) {
9165
9219
  console.error("Error: Two image paths are required");
9166
9220
  printUsage2();
9167
- process.exit(1);
9221
+ process.exit(2);
9168
9222
  }
9169
9223
  const parsed = {
9170
9224
  image1: args[0],
9171
- image2: args[1]
9225
+ image2: args[1],
9226
+ json: false,
9227
+ options: {}
9172
9228
  };
9229
+ const render = {};
9173
9230
  for (let i3 = 2; i3 < args.length; i3++) {
9174
9231
  const arg = args[i3];
9175
- const nextArg = args[i3 + 1];
9232
+ const takeValue = () => {
9233
+ const value = args[++i3];
9234
+ if (value === void 0) throw new Error(`${arg} needs a value`);
9235
+ return value;
9236
+ };
9176
9237
  switch (arg) {
9177
9238
  case "-o":
9178
9239
  case "--output":
9179
- if (nextArg) {
9180
- parsed.outputPath = nextArg;
9181
- i3++;
9182
- }
9240
+ parsed.outputPath = takeValue();
9183
9241
  break;
9184
9242
  case "-t":
9185
9243
  case "--threshold":
9186
- if (nextArg) {
9187
- const threshold = parseFloat(nextArg);
9188
- if (Number.isNaN(threshold) || threshold < 0 || threshold > 1) {
9189
- throw new Error(
9190
- `Invalid threshold: ${nextArg}. Must be between 0 and 1`
9191
- );
9192
- }
9193
- parsed.threshold = threshold;
9194
- i3++;
9195
- }
9244
+ parsed.options.threshold = parseUnit("threshold", takeValue());
9245
+ break;
9246
+ case "--antialiasing":
9247
+ parsed.options.antialiasing = true;
9248
+ break;
9249
+ case "--no-antialiasing":
9250
+ parsed.options.antialiasing = false;
9251
+ break;
9252
+ case "--metric":
9253
+ parsed.options.metric = parseMetric(takeValue());
9254
+ break;
9255
+ case "--mask":
9256
+ parsed.maskPath = takeValue();
9257
+ break;
9258
+ case "--window":
9259
+ parsed.options.window = parseWindow(takeValue());
9196
9260
  break;
9197
9261
  case "-a":
9198
9262
  case "--alpha":
9199
- if (nextArg) {
9200
- const alpha = parseFloat(nextArg);
9201
- if (Number.isNaN(alpha) || alpha < 0 || alpha > 1) {
9202
- throw new Error(
9203
- `Invalid alpha: ${nextArg}. Must be between 0 and 1`
9204
- );
9205
- }
9206
- parsed.alpha = alpha;
9207
- i3++;
9208
- }
9209
- break;
9210
- case "--aa-color":
9211
- if (nextArg) {
9212
- parsed.aaColor = parseRGB(nextArg);
9213
- i3++;
9214
- }
9263
+ render.alpha = parseUnit("alpha", takeValue());
9215
9264
  break;
9216
9265
  case "--diff-color":
9217
- if (nextArg) {
9218
- parsed.diffColor = parseRGB(nextArg);
9219
- i3++;
9220
- }
9266
+ render.diffColor = parseRGB(takeValue());
9221
9267
  break;
9222
9268
  case "--diff-color-alt":
9223
- if (nextArg) {
9224
- parsed.diffColorAlt = parseRGB(nextArg);
9225
- i3++;
9226
- }
9269
+ render.diffColorAlt = parseRGB(takeValue());
9270
+ break;
9271
+ case "--aa-color":
9272
+ render.aaColor = parseRGB(takeValue());
9227
9273
  break;
9228
- case "--include-aa":
9229
- parsed.includeAA = true;
9274
+ case "--background":
9275
+ render.background = parseBackground(takeValue());
9230
9276
  break;
9231
- case "--diff-mask":
9232
- parsed.diffMask = true;
9277
+ case "--json":
9278
+ parsed.json = true;
9233
9279
  break;
9234
9280
  case "--codec":
9235
- if (nextArg) {
9236
- parsed.codec = nextArg;
9237
- i3++;
9238
- }
9281
+ if (native) throw new Error("--codec is not supported by core-native");
9282
+ parsed.codec = takeValue();
9283
+ break;
9284
+ case "--compression":
9285
+ if (!native) throw new Error(`${arg} is only supported by core-native`);
9286
+ parsed.compression = parseIntIn("compression", takeValue(), 0, 9);
9287
+ break;
9288
+ case "--quality":
9289
+ if (!native) throw new Error(`${arg} is only supported by core-native`);
9290
+ parsed.quality = parseIntIn("quality", takeValue(), 1, 100);
9239
9291
  break;
9240
9292
  default:
9241
9293
  console.error(`Unknown option: ${arg}`);
9242
9294
  printUsage2();
9243
- process.exit(1);
9295
+ process.exit(2);
9244
9296
  }
9245
9297
  }
9298
+ if (Object.keys(render).length > 0) parsed.options.render = render;
9246
9299
  return parsed;
9247
9300
  }
9301
+ function reportResult(result, {
9302
+ json,
9303
+ outputPath,
9304
+ duration
9305
+ }) {
9306
+ if (json) {
9307
+ console.log(JSON.stringify(result));
9308
+ } else if (result.kind === "dimension-mismatch") {
9309
+ const { a: a2, b } = result;
9310
+ console.log(
9311
+ `Image dimensions do not match: ${a2.width}x${a2.height} vs ${b.width}x${b.height}`
9312
+ );
9313
+ } else {
9314
+ console.log(`completed in: ${duration.toFixed(2)}ms`);
9315
+ console.log(`different pixels: ${result.count}`);
9316
+ console.log(`error: ${result.percentage.toFixed(2)}%`);
9317
+ if (result.window) {
9318
+ const { x, y, count } = result.window;
9319
+ console.log(`busiest window: ${count} pixels at ${x},${y}`);
9320
+ }
9321
+ if (!result.identical && outputPath)
9322
+ console.log(`diff image: ${outputPath}`);
9323
+ if (result.identical) console.log("Images are identical!");
9324
+ }
9325
+ process.exit(result.kind === "compared" && result.identical ? 0 : 1);
9326
+ }
9248
9327
  async function runCoreDiff(parsed, diffFn) {
9249
9328
  const codec = await getCodec(parsed.codec);
9250
- const [img1, img2] = await Promise.all([
9329
+ const [a2, b, mask] = await Promise.all([
9251
9330
  codec.read(parsed.image1),
9252
- codec.read(parsed.image2)
9331
+ codec.read(parsed.image2),
9332
+ parsed.maskPath ? loadMask(parsed.maskPath, parsed.codec) : void 0
9253
9333
  ]);
9254
- if (img1.width !== img2.width || img1.height !== img2.height) {
9255
- throw new Error(
9256
- `Image dimensions do not match: ${img1.width}x${img1.height} vs ${img2.width}x${img2.height}`
9257
- );
9258
- }
9259
- let outputData;
9260
- if (parsed.outputPath) {
9261
- outputData = new Uint8Array(img1.data.length);
9262
- }
9263
- const coreOptions = {
9264
- threshold: parsed.threshold,
9265
- alpha: parsed.alpha,
9266
- aaColor: parsed.aaColor,
9267
- diffColor: parsed.diffColor,
9268
- diffColorAlt: parsed.diffColorAlt,
9269
- includeAA: parsed.includeAA,
9270
- diffMask: parsed.diffMask
9271
- };
9334
+ const output = parsed.outputPath ? { data: new Uint8Array(a2.data.length), width: a2.width, height: a2.height } : void 0;
9272
9335
  const startTime = performance.now();
9273
- const diffCount = await diffFn(
9274
- img1.data,
9275
- img2.data,
9276
- img1.width,
9277
- img1.height,
9278
- outputData,
9279
- coreOptions
9280
- );
9336
+ const result = await diffFn(a2, b, { ...parsed.options, mask, output });
9281
9337
  const duration = performance.now() - startTime;
9282
- if (diffCount > 0 && parsed.outputPath && outputData) {
9283
- await codec.write(
9284
- {
9285
- data: outputData,
9286
- width: img1.width,
9287
- height: img1.height
9288
- },
9289
- parsed.outputPath
9290
- );
9291
- }
9292
- console.log(`completed in: ${duration.toFixed(2)}ms`);
9293
- console.log(`dimensions: ${img1.width}x${img1.height}`);
9294
- console.log(`different pixels: ${diffCount}`);
9295
- console.log(
9296
- `error: ${(diffCount / (img1.width * img1.height) * 100).toFixed(2)}%`
9297
- );
9298
- if (diffCount > 0 && outputData && parsed.outputPath) {
9299
- console.log(`diff image: ${parsed.outputPath}`);
9300
- }
9301
- if (diffCount > 0) {
9302
- process.exit(1);
9303
- } else {
9304
- console.log(`Images are identical!`);
9305
- process.exit(0);
9338
+ if (result.kind === "compared" && !result.identical && output && parsed.outputPath) {
9339
+ await codec.write(output, parsed.outputPath);
9306
9340
  }
9341
+ return reportResult(result, {
9342
+ json: parsed.json,
9343
+ outputPath: parsed.outputPath,
9344
+ duration
9345
+ });
9307
9346
  }
9308
9347
 
9309
9348
  // src/commands/core-wasm.ts
9310
- var toUint8Array = (data) => data instanceof Uint8Array ? data : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
9311
9349
  function printUsage() {
9312
9350
  console.log(`
9313
9351
  Usage: blazediff-cli core-wasm <image1> <image2> [options]
@@ -9317,17 +9355,14 @@ Arguments:
9317
9355
  image2 Path to the second image
9318
9356
 
9319
9357
  Options:
9320
- -o, --output <path> Output path for the diff image
9321
- -t, --threshold <num> Matching threshold (0 to 1, default: 0.1)
9322
- --include-aa Include anti-aliasing detection
9323
- --diff-mask Draw diff over transparent background
9324
- --codec <name> Specify codec to use (pngjs, sharp, jsquash-png)
9358
+ ${SHARED_FLAGS}
9359
+ --codec <name> Codec to use (pngjs, sharp, jsquash-png)
9325
9360
  -h, --help Show this help message
9326
9361
 
9327
9362
  Examples:
9328
9363
  blazediff-cli core-wasm image1.png image2.png
9329
9364
  blazediff-cli core-wasm image1.png image2.png -o diff.png -t 0.05
9330
- blazediff-cli core-wasm image1.png image2.png --include-aa --diff-mask
9365
+ blazediff-cli core-wasm image1.png image2.png --no-antialiasing --background transparent
9331
9366
  `);
9332
9367
  }
9333
9368
  var loadWasm = () => Function('return import("@blazediff/core-wasm")')();
@@ -9342,27 +9377,13 @@ async function main() {
9342
9377
  const wasmPath = require.resolve("@blazediff/core-wasm/wasm/blazediff_bg.wasm");
9343
9378
  await initBlazediff(await (0, import_promises.readFile)(wasmPath));
9344
9379
  const parsed = parseCoreArgs(args, printUsage);
9345
- await runCoreDiff(
9346
- parsed,
9347
- (a2, b, width, height, output, options) => wasmDiff(
9348
- toUint8Array(a2),
9349
- toUint8Array(b),
9350
- width,
9351
- height,
9352
- output ? toUint8Array(output) : void 0,
9353
- {
9354
- threshold: options.threshold,
9355
- includeAA: options.includeAA,
9356
- diffMask: options.diffMask
9357
- }
9358
- )
9359
- );
9380
+ await runCoreDiff(parsed, wasmDiff);
9360
9381
  } catch (error) {
9361
9382
  console.error(
9362
9383
  "Error:",
9363
9384
  error instanceof Error ? error.message : String(error)
9364
9385
  );
9365
- process.exit(1);
9386
+ process.exit(2);
9366
9387
  }
9367
9388
  }
9368
9389
  if (typeof require !== "undefined" && require.main === module) {