mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-11 16:26:20 +00:00
Parameterize auto optimizer
This commit is contained in:
@@ -16,9 +16,12 @@ export async function binarySearch(
|
||||
let parameter = (max - min) / 2 + min;
|
||||
let delta = (max - min) / 4;
|
||||
let value;
|
||||
let round = 0;
|
||||
do {
|
||||
let round = 1;
|
||||
while (true) {
|
||||
value = await measure(parameter);
|
||||
if (Math.abs(value - measureGoal) < epsilon || round >= maxRounds) {
|
||||
return { parameter, round, value };
|
||||
}
|
||||
if (value > measureGoal) {
|
||||
parameter -= delta;
|
||||
} else if (value < measureGoal) {
|
||||
@@ -26,8 +29,7 @@ export async function binarySearch(
|
||||
}
|
||||
delta /= 2;
|
||||
round++;
|
||||
} while (Math.abs(value - measureGoal) > epsilon && round < maxRounds);
|
||||
return { parameter, round, value };
|
||||
}
|
||||
}
|
||||
export async function autoOptimize(
|
||||
bitmapIn,
|
||||
|
||||
@@ -8,4 +8,3 @@ export function instantiateEmscriptenWasm(factory, path) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,9 @@ async function encodeFile({
|
||||
bitmap: bitmapIn,
|
||||
outputFile,
|
||||
encName,
|
||||
encConfig
|
||||
encConfig,
|
||||
optimizerButteraugliTarget,
|
||||
maxOptimizerRounds
|
||||
}) {
|
||||
let out;
|
||||
const encoder = await supportedFormats[encName].enc();
|
||||
@@ -74,14 +76,16 @@ async function encodeFile({
|
||||
decode,
|
||||
{
|
||||
min: supportedFormats[encName].autoOptimize.min,
|
||||
max: supportedFormats[encName].autoOptimize.max
|
||||
max: supportedFormats[encName].autoOptimize.max,
|
||||
butteraugliDistanceTarget: optimizerButteraugliTarget,
|
||||
maxRounds: maxOptimizerRounds
|
||||
}
|
||||
);
|
||||
out = binary;
|
||||
console.log(
|
||||
`Used ${JSON.stringify({
|
||||
`Used \`--${encName} '${JSON.stringify({
|
||||
[optionToOptimize]: quality
|
||||
})} for ${outputFile}`
|
||||
})}'\` for ${outputFile}`
|
||||
);
|
||||
} else {
|
||||
out = encoder.encode(
|
||||
@@ -101,9 +105,9 @@ async function encodeFile({
|
||||
}
|
||||
|
||||
async function processFiles(files) {
|
||||
const workerPool = new WorkerPool(cpus().length, __filename);
|
||||
// Create output directory
|
||||
await fsp.mkdir(program.outputDir, { recursive: true });
|
||||
const workerPool = new WorkerPool(cpus().length, __filename);
|
||||
|
||||
const decodedFiles = await Promise.all(files.map(file => decodeFile(file)));
|
||||
|
||||
@@ -134,7 +138,9 @@ async function processFiles(files) {
|
||||
bitmap,
|
||||
outputFile,
|
||||
encName,
|
||||
encConfig
|
||||
encConfig,
|
||||
optimizerButteraugliTarget: program.optimizerButteraugliTarget,
|
||||
maxOptimizerRounds: program.maxOptimizerRounds
|
||||
})
|
||||
.then(({ outputFile, inputSize, outputSize }) => {
|
||||
jobsFinished++;
|
||||
@@ -164,6 +170,16 @@ if (isMainThread) {
|
||||
.version(version)
|
||||
.arguments("<files...>")
|
||||
.option("-d, --output-dir <dir>", "Output directory", ".")
|
||||
.option(
|
||||
"--max-optimizer-rounds <rounds>",
|
||||
"Maximum number of compressions to use for auto optimizations",
|
||||
6
|
||||
)
|
||||
.option(
|
||||
"--optimizer-butteraugli-target <butteraugli distance>",
|
||||
"Target Butteraugli distance for auto optimizer",
|
||||
1.4
|
||||
)
|
||||
.action(processFiles);
|
||||
|
||||
// Create a CLI option for each supported encoder
|
||||
|
||||
Reference in New Issue
Block a user