mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 16:57:26 +00:00
* Refactoring codecs * Plugging in new processor * Fixing decorator * MozJPEG free issue * Better worker aborting, and terminate workers that aren't used for 10 seconds * Better comment * Ooops, half-typed comment * Uncommenting problematic line * Surma fixed it! * Abstracting WASM initialisation * Better comment * Don't need this. * Adding ticket * noInitalRun * Reverting MozJPEG issue demo * Making a const for worker timeout * Inline docs * Bail early rather than nesting * Addressing nits
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import { nativeResize, NativeResizeMethod, drawableToImageData } from '../../lib/util';
|
|
import { BitmapResizeOptions, VectorResizeOptions } from './processor-meta';
|
|
|
|
function getCoverOffsets(sw: number, sh: number, dw: number, dh: number) {
|
|
const currentAspect = sw / sh;
|
|
const endAspect = dw / dh;
|
|
|
|
if (endAspect > currentAspect) {
|
|
const newSh = sw / endAspect;
|
|
const newSy = (sh - newSh) / 2;
|
|
return { sw, sh: newSh, sx: 0, sy: newSy };
|
|
}
|
|
|
|
const newSw = sh * endAspect;
|
|
const newSx = (sw - newSw) / 2;
|
|
return { sh, sw: newSw, sx: newSx, sy: 0 };
|
|
}
|
|
|
|
export function resize(data: ImageData, opts: BitmapResizeOptions): ImageData {
|
|
let sx = 0;
|
|
let sy = 0;
|
|
let sw = data.width;
|
|
let sh = data.height;
|
|
|
|
if (opts.fitMethod === 'cover') {
|
|
({ sx, sy, sw, sh } = getCoverOffsets(sw, sh, opts.width, opts.height));
|
|
}
|
|
|
|
return nativeResize(
|
|
data, sx, sy, sw, sh, opts.width, opts.height,
|
|
opts.method.slice('browser-'.length) as NativeResizeMethod,
|
|
);
|
|
}
|
|
|
|
export function vectorResize(data: HTMLImageElement, opts: VectorResizeOptions): ImageData {
|
|
let sx = 0;
|
|
let sy = 0;
|
|
let sw = data.width;
|
|
let sh = data.height;
|
|
|
|
if (opts.fitMethod === 'cover') {
|
|
({ sx, sy, sw, sh } = getCoverOffsets(sw, sh, opts.width, opts.height));
|
|
}
|
|
|
|
return drawableToImageData(data, {
|
|
sx, sy, sw, sh,
|
|
width: opts.width, height: opts.height,
|
|
});
|
|
}
|