mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-16 10:39:53 +00:00
Switch to Butteraugli and simpel CLI
This commit is contained in:
7
cli/image_data.js
Normal file
7
cli/image_data.js
Normal file
@@ -0,0 +1,7 @@
|
||||
module.exports = class ImageData {
|
||||
constructor(data, width, height) {
|
||||
this.data = data;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
};
|
||||
100
cli/index.js
Normal file
100
cli/index.js
Normal file
@@ -0,0 +1,100 @@
|
||||
const mozjpeg_dec = require("../codecs/mozjpeg/dec/mozjpeg_dec.js");
|
||||
const mozjpeg_enc = require("../codecs/mozjpeg/enc/mozjpeg_enc.js");
|
||||
const webp_enc = require("../codecs/webp/enc/webp_enc.js");
|
||||
const visdifModule = require("../codecs/visdif/visdif.js");
|
||||
|
||||
// Our decoders currently rely on this.
|
||||
globalThis.ImageData = require("./image_data.js");
|
||||
|
||||
const fsp = require("fs").promises;
|
||||
|
||||
async function decodeFile(file) {
|
||||
const buffer = await fsp.readFile(file);
|
||||
const rgba = (await mozjpeg_dec()).decode(new Uint8Array(buffer));
|
||||
return rgba;
|
||||
}
|
||||
|
||||
const butteraugliGoal = 1.4;
|
||||
const maxRounds = 8;
|
||||
async function optimize(bitmapIn, encode, decode) {
|
||||
let quality = 50;
|
||||
let inc = 25;
|
||||
let butteraugliDistance = 2;
|
||||
let attempts = 0;
|
||||
let bitmapOut;
|
||||
let binaryOut;
|
||||
|
||||
const { visdif } = await visdifModule();
|
||||
do {
|
||||
binaryOut = await encode(bitmapIn, quality);
|
||||
bitmapOut = await decode(binaryOut);
|
||||
console.log({
|
||||
butteraugliDistance,
|
||||
quality,
|
||||
attempts,
|
||||
binaryOut,
|
||||
bitmapOut
|
||||
});
|
||||
butteraugliDistance = visdif(
|
||||
bitmapIn.data,
|
||||
bitmapOut.data,
|
||||
bitmapIn.width,
|
||||
bitmapIn.height
|
||||
);
|
||||
if (butteraugliDistance > butteraugliGoal) {
|
||||
quality += inc;
|
||||
} else {
|
||||
quality -= inc;
|
||||
}
|
||||
inc /= 2;
|
||||
attempts++;
|
||||
} while (
|
||||
Math.abs(butteraugliDistance - butteraugliGoal) > 0.1 &&
|
||||
attempts < maxRounds
|
||||
);
|
||||
|
||||
return {
|
||||
bitmap: bitmapOut,
|
||||
binary: binaryOut,
|
||||
quality,
|
||||
attempts
|
||||
};
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const [, , inFile, outFile] = process.argv;
|
||||
const bitmapIn = await decodeFile(inFile);
|
||||
|
||||
const jpegEncModule = await mozjpeg_enc();
|
||||
const jpegEnc = async (bitmap, quality) =>
|
||||
jpegEncModule.encode(bitmap.data.buffer, bitmap.width, bitmap.height, {
|
||||
quality,
|
||||
baseline: false,
|
||||
arithmetic: false,
|
||||
progressive: true,
|
||||
optimize_coding: true,
|
||||
smoothing: 0,
|
||||
color_space: 3 /*YCbCr*/,
|
||||
quant_table: 3,
|
||||
trellis_multipass: false,
|
||||
trellis_opt_zero: false,
|
||||
trellis_opt_table: false,
|
||||
trellis_loops: 1,
|
||||
auto_subsample: true,
|
||||
chroma_subsample: 2,
|
||||
separate_chroma_quality: false,
|
||||
chroma_quality: 75
|
||||
});
|
||||
const jpegDecModule = await mozjpeg_dec();
|
||||
const jpegDec = async binary => jpegDecModule.decode(binary);
|
||||
const jpegOut = await optimize(bitmapIn, jpegEnc, jpegDec);
|
||||
await fsp.writeFile("out.jpg", jpegOut.binary);
|
||||
//await webp_enc();
|
||||
//console.log(bitmapIn);
|
||||
}
|
||||
|
||||
main().catch(err => {
|
||||
console.error("Something went wrong");
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
13
cli/package-lock.json
generated
Normal file
13
cli/package-lock.json
generated
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "cli",
|
||||
"version": "0.0.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"commander": {
|
||||
"version": "6.0.0",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-6.0.0.tgz",
|
||||
"integrity": "sha512-s7EA+hDtTYNhuXkTlhqew4txMZVdszBmKWSPEMxGr8ru8JXR7bLUFIAtPhcSuFdJQ0ILMxnJi8GkQL0yvDy/YA=="
|
||||
}
|
||||
}
|
||||
}
|
||||
15
cli/package.json
Normal file
15
cli/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "cli",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Surma <surma@surma.dev>",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"commander": "^6.0.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user