Skip to content

Make images smaller using best-in-class codecs, right in the browser.

JavaScript

Add better output formatting

Surma1e700cd7c381
diff --git a/cli/src/index.js b/cli/src/index.js
index 6d1303ef..bdc148e1 100644
--- a/cli/src/index.js
+++ b/cli/src/index.js
@@ -13,6 +13,19 @@ import WorkerPool from "./worker_pool.js";
import ImageData from "./image_data.js";
globalThis.ImageData = ImageData;
+function clamp(v, min, max) {
+ if (v < min) return min;
+ if (v > max) return max;
+ return v;
+}
+
+const suffix = ["B", "KB", "MB"];
+function prettyPrintSize(size) {
+ const base = Math.floor(Math.log2(size) / 10);
+ const index = clamp(base, 0, 2);
+ return (size / 2 ** (10 * index)).toFixed(2) + suffix[index];
+}
+
async function decodeFile(file) {
const buffer = await fsp.readFile(file);
const firstChunk = buffer.slice(0, 16);
@@ -28,69 +41,49 @@ async function decodeFile(file) {
const rgba = (await supportedFormats[key].dec()).decode(
new Uint8Array(buffer)
);
- return rgba;
+ return {
+ file,
+ bitmap: rgba,
+ size: buffer.length
+ };
}
-/*
-const butteraugliGoal = 1.4;
-const maxRounds = 8;
-async function optimize(bitmapIn, encode, decode) {
-const visdifModule = require("../codecs/visdif/visdif.js");
- let quality = 50;
- let inc = 25;
- let butteraugliDistance = 2;
- let attempts = 0;
- let bitmapOut;
- let binaryOut;
-
- const { VisDiff } = await visdifModule();
- const comparator = new VisDiff(
- bitmapIn.data,
- bitmapIn.width,
- bitmapIn.height
- );
- do {
- binaryOut = await encode(bitmapIn, quality);
- bitmapOut = await decode(binaryOut);
- butteraugliDistance = comparator.distance(bitmapOut.data);
- console.log({
- butteraugliDistance,
- quality,
- attempts,
- binaryOut,
- bitmapOut
- });
- if (butteraugliDistance > butteraugliGoal) {
- quality += inc;
- } else {
- quality -= inc;
- }
- inc /= 2;
- attempts++;
- } while (
- Math.abs(butteraugliDistance - butteraugliGoal) > 0.1 &&
- attempts < maxRounds
+async function encodeFile({
+ file,
+ size,
+ bitmap,
+ outputFile,
+ encName,
+ encConfig
+}) {
+ const encoder = await supportedFormats[encName].enc();
+ const out = encoder.encode(
+ bitmap.data.buffer,
+ bitmap.width,
+ bitmap.height,
+ encConfig
);
-
- comparator.delete();
-
+ await fsp.writeFile(outputFile, out);
return {
- bitmap: bitmapOut,
- binary: binaryOut,
- quality,
- attempts
+ inputSize: size,
+ inputFile: file,
+ outputFile,
+ outputSize: out.length
};
-}*/
+}
async function processFiles(files) {
// Create output directory
await fsp.mkdir(program.outputDir, { recursive: true });
const workerPool = new WorkerPool(cpus().length, __filename);
- for (const file of files) {
+ const decodedFiles = await Promise.all(files.map(file => decodeFile(file)));
+
+ let jobsStarted = 0;
+ let jobsFinished = 0;
+ for (const { file, bitmap, size } of decodedFiles) {
const ext = extname(file);
const base = basename(file, ext);
- const bitmap = await decodeFile(file);
for (const [encName, value] of Object.entries(supportedFormats)) {
if (!program[encName]) {
@@ -102,15 +95,31 @@ async function processFiles(files) {
JSON5.parse(program[encName])
);
const outputFile = join(program.outputDir, `${base}.${value.extension}`);
+ jobsStarted++;
workerPool
.dispatchJob({
+ file,
+ size,
bitmap,
outputFile,
encName,
encConfig
})
- .then(({ outputSize }) => {
- console.log(`Written ${file}. Size: ${outputSize}`);
+ .then(({ outputFile, inputSize, outputSize }) => {
+ jobsFinished++;
+ const numDigits = jobsStarted.toString().length;
+ console.log(
+ `${jobsFinished
+ .toString()
+ .padStart(
+ numDigits
+ )}/${jobsStarted}: ${outputFile} ${prettyPrintSize(
+ inputSize
+ )} -> ${prettyPrintSize(outputSize)} (${(
+ (outputSize / inputSize) *
+ 100
+ ).toFixed(1)}%)`
+ );
});
}
}
@@ -136,18 +145,56 @@ if (isMainThread) {
program.parse(process.argv);
} else {
- WorkerPool.useThisThreadAsWorker(
- async ({ id, bitmap, outputFile, encName, encConfig }) => {
- console.log("received", { outputFile, encName });
- const encoder = await supportedFormats[encName].enc();
- const out = encoder.encode(
- bitmap.data.buffer,
- bitmap.width,
- bitmap.height,
- encConfig
- );
- await fsp.writeFile(outputFile, out);
- return { outputSize: out.length };
+ WorkerPool.useThisThreadAsWorker(encodeFile);
+}
+
+/*
+const butteraugliGoal = 1.4;
+const maxRounds = 8;
+async function optimize(bitmapIn, encode, decode) {
+const visdifModule = require("../codecs/visdif/visdif.js");
+ let quality = 50;
+ let inc = 25;
+ let butteraugliDistance = 2;
+ let attempts = 0;
+ let bitmapOut;
+ let binaryOut;
+
+ const { VisDiff } = await visdifModule();
+ const comparator = new VisDiff(
+ bitmapIn.data,
+ bitmapIn.width,
+ bitmapIn.height
+ );
+ do {
+ binaryOut = await encode(bitmapIn, quality);
+ bitmapOut = await decode(binaryOut);
+ butteraugliDistance = comparator.distance(bitmapOut.data);
+ console.log({
+ butteraugliDistance,
+ quality,
+ attempts,
+ binaryOut,
+ bitmapOut
+ });
+ if (butteraugliDistance > butteraugliGoal) {
+ quality += inc;
+ } else {
+ quality -= inc;
}
+ inc /= 2;
+ attempts++;
+ } while (
+ Math.abs(butteraugliDistance - butteraugliGoal) > 0.1 &&
+ attempts < maxRounds
);
-}
+
+ comparator.delete();
+
+ return {
+ bitmap: bitmapOut,
+ binary: binaryOut,
+ quality,
+ attempts
+ };
+}*/