mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-15 18:19:47 +00:00
Typescriptify auto optimizer in libSquoosh
This commit is contained in:
@@ -2,6 +2,39 @@ import { instantiateEmscriptenWasm } from './emscripten-utils.js';
|
|||||||
|
|
||||||
import visdif from '../../codecs/visdif/visdif.js';
|
import visdif from '../../codecs/visdif/visdif.js';
|
||||||
import visdifWasm from 'asset-url:../../codecs/visdif/visdif.wasm';
|
import visdifWasm from 'asset-url:../../codecs/visdif/visdif.wasm';
|
||||||
|
import type ImageData from './image_data';
|
||||||
|
|
||||||
|
interface VisDiff {
|
||||||
|
distance: (data: Uint8ClampedArray) => number;
|
||||||
|
delete: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface VisdiffConstructor {
|
||||||
|
new (data: Uint8ClampedArray, width: number, height: number): VisDiff;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface VisDiffModule extends EmscriptenWasm.Module {
|
||||||
|
VisDiff: VisdiffConstructor;
|
||||||
|
}
|
||||||
|
|
||||||
|
type VisDiffModuleFactory = EmscriptenWasm.ModuleFactory<VisDiffModule>;
|
||||||
|
|
||||||
|
interface BinarySearchParams {
|
||||||
|
min?: number;
|
||||||
|
max?: number;
|
||||||
|
epsilon?: number;
|
||||||
|
maxRounds?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AutoOptimizeParams extends BinarySearchParams {
|
||||||
|
butteraugliDistanceGoal?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AutoOptimizeResult {
|
||||||
|
bitmap: ImageData;
|
||||||
|
binary: Uint8Array;
|
||||||
|
quality: number;
|
||||||
|
}
|
||||||
|
|
||||||
// `measure` is a (async) function that takes exactly one numeric parameter and
|
// `measure` is a (async) function that takes exactly one numeric parameter and
|
||||||
// returns a value. The function is assumed to be monotonic (an increase in `parameter`
|
// returns a value. The function is assumed to be monotonic (an increase in `parameter`
|
||||||
@@ -9,9 +42,9 @@ import visdifWasm from 'asset-url:../../codecs/visdif/visdif.wasm';
|
|||||||
// to find `parameter` such that `measure` returns `measureGoal`, within an error
|
// to find `parameter` such that `measure` returns `measureGoal`, within an error
|
||||||
// of `epsilon`. It will use at most `maxRounds` attempts.
|
// of `epsilon`. It will use at most `maxRounds` attempts.
|
||||||
export async function binarySearch(
|
export async function binarySearch(
|
||||||
measureGoal,
|
measureGoal: number,
|
||||||
measure,
|
measure: (val: number) => Promise<number>,
|
||||||
{ min = 0, max = 100, epsilon = 0.1, maxRounds = 6 } = {},
|
{ min = 0, max = 100, epsilon = 0.1, maxRounds = 6 }: BinarySearchParams = {},
|
||||||
) {
|
) {
|
||||||
let parameter = (max - min) / 2 + min;
|
let parameter = (max - min) / 2 + min;
|
||||||
let delta = (max - min) / 4;
|
let delta = (max - min) / 4;
|
||||||
@@ -33,12 +66,21 @@ export async function binarySearch(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function autoOptimize(
|
export async function autoOptimize(
|
||||||
bitmapIn,
|
bitmapIn: ImageData,
|
||||||
encode,
|
encode: (
|
||||||
decode,
|
bitmap: ImageData,
|
||||||
{ butteraugliDistanceGoal = 1.4, ...otherOpts } = {},
|
quality: number,
|
||||||
) {
|
) => Promise<Uint8Array> | Uint8Array,
|
||||||
const { VisDiff } = await instantiateEmscriptenWasm(visdif, visdifWasm);
|
decode: (binary: Uint8Array) => Promise<ImageData> | ImageData,
|
||||||
|
{
|
||||||
|
butteraugliDistanceGoal = 1.4,
|
||||||
|
...binarySearchParams
|
||||||
|
}: AutoOptimizeParams = {},
|
||||||
|
): Promise<AutoOptimizeResult> {
|
||||||
|
const { VisDiff } = await instantiateEmscriptenWasm(
|
||||||
|
visdif as VisDiffModuleFactory,
|
||||||
|
visdifWasm,
|
||||||
|
);
|
||||||
|
|
||||||
const comparator = new VisDiff(
|
const comparator = new VisDiff(
|
||||||
bitmapIn.data,
|
bitmapIn.data,
|
||||||
@@ -46,8 +88,11 @@ export async function autoOptimize(
|
|||||||
bitmapIn.height,
|
bitmapIn.height,
|
||||||
);
|
);
|
||||||
|
|
||||||
let bitmapOut;
|
// We're able to do non null assertion because
|
||||||
let binaryOut;
|
// we know that binarySearch will set these values
|
||||||
|
let bitmapOut!: ImageData;
|
||||||
|
let binaryOut!: Uint8Array;
|
||||||
|
|
||||||
// Increasing quality means _decrease_ in Butteraugli distance.
|
// Increasing quality means _decrease_ in Butteraugli distance.
|
||||||
// `binarySearch` assumes that increasing `parameter` will
|
// `binarySearch` assumes that increasing `parameter` will
|
||||||
// increase the metric value. So multipliy Butteraugli values by -1.
|
// increase the metric value. So multipliy Butteraugli values by -1.
|
||||||
@@ -58,7 +103,7 @@ export async function autoOptimize(
|
|||||||
bitmapOut = await decode(binaryOut);
|
bitmapOut = await decode(binaryOut);
|
||||||
return -1 * comparator.distance(bitmapOut.data);
|
return -1 * comparator.distance(bitmapOut.data);
|
||||||
},
|
},
|
||||||
otherOpts,
|
binarySearchParams,
|
||||||
);
|
);
|
||||||
comparator.delete();
|
comparator.delete();
|
||||||
|
|
||||||
Reference in New Issue
Block a user