diff --git a/example.html b/example.html
deleted file mode 100644
index 23efe80..0000000
--- a/example.html
+++ /dev/null
@@ -1,22 +0,0 @@
-
\ No newline at end of file
diff --git a/quality.js b/image.js
similarity index 57%
rename from quality.js
rename to image.js
index 53a7b54..5f6dcae 100644
--- a/quality.js
+++ b/image.js
@@ -1,7 +1,43 @@
const { PNG } = require('pngjs');
const fs = require('fs');
-// Fixed from https://www.npmjs.com/package/png-quality
+async function segmentationMask(input1, input2, output, threshold = 0.02) {
+ let img1 = await loadPngFile(input1);
+ let img2 = await loadPngFile(input2);
+
+ if (img1.width !== img2.width || img1.height !== img2.height) {
+ throw new Error("Cannot compute mask on images with different sizes");
+ }
+
+ for (let i = 0; i < img1.data.length; i += 4) {
+ let r1 = img1.data[i + 0] / 255;
+ let g1 = img1.data[i + 1] / 255;
+ let b1 = img1.data[i + 2] / 255;
+
+ let r2 = img2.data[i + 0] / 255;
+ let g2 = img2.data[i + 1] / 255;
+ let b2 = img2.data[i + 2] / 255;
+
+ // Test difference
+ let difference = Math.sqrt((r1 - r2) * (r1 - r2) + (g1 - g2) * (g1 - g2) + (b1 - b2) * (b1 - b2));
+ let pixelsAreDifferent = difference > threshold;
+
+ if (pixelsAreDifferent) {
+ img1.data[i + 0] = 255;
+ img1.data[i + 1] = 255;
+ img1.data[i + 2] = 255;
+ } else {
+ img1.data[i + 0] = 0;
+ img1.data[i + 1] = 0;
+ img1.data[i + 2] = 0;
+ }
+ }
+
+ let outputStream = fs.createWriteStream(output);
+ await img1.pack().pipe(outputStream);
+}
+
+// The following is fixed from https://www.npmjs.com/package/png-quality
async function loadPngFile(pathOrBuffer) {
// Load buffer of path
@@ -62,4 +98,5 @@ module.exports = {
loadPngFile,
mse,
psnr,
+ segmentationMask,
};
diff --git a/index.js b/index.js
index 2650ea9..ae288d5 100644
--- a/index.js
+++ b/index.js
@@ -3,7 +3,7 @@
const fs = require('fs').promises;
const process = require('process');
const puppeteer = require('puppeteer');
-const quality = require('./quality.js');
+const image = require('./image.js');
const uuid = require('uuid').v4;
// Size of the rendering of the web page
@@ -34,36 +34,181 @@ async function eprint(data) {
}
async function println(data) {
- await write(process.stdout, data + '\n');
+ await write(process.stdout, (data ? data : "") + '\n');
}
async function eprintln(data) {
- await write(process.stderr, data + '\n');
+ await write(process.stderr, (data ? data : "") + '\n');
+}
+
+async function info(data) {
+ await eprintln("\x1b[34;1minfo\x1b[0m\x1b[1m:\x1b[0m " + data);
+}
+
+async function warning(data) {
+ await eprintln("\x1b[33;1mwarning\x1b[0m\x1b[1m:\x1b[0m " + data);
+}
+
+async function error(data) {
+ await eprintln("\x1b[31;1merror\x1b[0m\x1b[1m:\x1b[0m " + data);
+}
+
+function help() {
+ const name = "\x1b[32mlocator\x1b[0m";
+ const version = "0.1.0";
+ const description = "Helper tool to analyse HTML content produced from marp slides";
+ const command = "locator";
+
+ const usage = "\x1b[33mUSAGE:\x1b[0m";
+ const args = "\x1b[33mARGUMENTS:\x1b[0m";
+
+ const helpShort = "\x1b[32m-h\x1b[0m";
+ const helpLong = "\x1b[32m--help\x1b[0m";
+
+ const inputShort = "\x1b[32m-i\x1b[0m";
+ const inputLong = "\x1b[32m--input\x1b[0m";
+
+ const outputShort = "\x1b[32m-o\x1b[0m";
+ const outputLong = "\x1b[32m--output\x1b[0m";
+
+ const forceShort = "\x1b[32m-f\x1b[0m";
+ const forceLong = "\x1b[32m--force\x1b[0m";
+
+ const shrinkShort = "\x1b[32m-s\x1b[0m";
+ const shrinkLong = "\x1b[32m--shrink\x1b[0m";
+
+ const flattenLong = "\x1b[32m--flatten\x1b[0m";
+
+ println(`${name} ${version}
+${description}
+
+${usage}
+ ${command} -i
+
+${args}
+ ${helpShort}, ${helpLong} Displays this help and quit
+ ${inputShort}, ${inputLong} Path to the HTML input file
+ ${outputShort}, ${outputLong}