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