mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-13 09:17:20 +00:00
Improve preprocess parameter type
This commit is contained in:
@@ -29,14 +29,23 @@ interface ResizeWithAspectParams {
|
|||||||
target_height: number;
|
target_height: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ResizeInstantiateOptions {
|
export interface ResizeOptions {
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
method: string;
|
method: 'triange' | 'catrom' | 'mitchell' | 'lanczos3';
|
||||||
premultiply: boolean;
|
premultiply: boolean;
|
||||||
linearRGB: boolean;
|
linearRGB: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface QuantOptions {
|
||||||
|
numColors: number;
|
||||||
|
dither: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RotateOptions {
|
||||||
|
numRotations: number;
|
||||||
|
}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
// Needed for being able to use ImageData as type in codec types
|
// Needed for being able to use ImageData as type in codec types
|
||||||
type ImageData = import('./image_data.js').default;
|
type ImageData = import('./image_data.js').default;
|
||||||
@@ -171,13 +180,7 @@ export const preprocessors = {
|
|||||||
buffer: Uint8Array,
|
buffer: Uint8Array,
|
||||||
input_width: number,
|
input_width: number,
|
||||||
input_height: number,
|
input_height: number,
|
||||||
{
|
{ width, height, method, premultiply, linearRGB }: ResizeOptions,
|
||||||
width,
|
|
||||||
height,
|
|
||||||
method,
|
|
||||||
premultiply,
|
|
||||||
linearRGB,
|
|
||||||
}: ResizeInstantiateOptions,
|
|
||||||
) => {
|
) => {
|
||||||
({ width, height } = resizeWithAspect({
|
({ width, height } = resizeWithAspect({
|
||||||
input_width,
|
input_width,
|
||||||
@@ -218,7 +221,7 @@ export const preprocessors = {
|
|||||||
buffer: Uint8Array,
|
buffer: Uint8Array,
|
||||||
width: number,
|
width: number,
|
||||||
height: number,
|
height: number,
|
||||||
{ numColors, dither }: { numColors: number; dither: number },
|
{ numColors, dither }: QuantOptions,
|
||||||
) =>
|
) =>
|
||||||
new ImageData(
|
new ImageData(
|
||||||
imageQuant.quantize(buffer, width, height, numColors, dither),
|
imageQuant.quantize(buffer, width, height, numColors, dither),
|
||||||
@@ -239,7 +242,7 @@ export const preprocessors = {
|
|||||||
buffer: Uint8Array,
|
buffer: Uint8Array,
|
||||||
width: number,
|
width: number,
|
||||||
height: number,
|
height: number,
|
||||||
{ numRotations }: { numRotations: number },
|
{ numRotations }: RotateOptions,
|
||||||
) => {
|
) => {
|
||||||
const degrees = (numRotations * 90) % 360;
|
const degrees = (numRotations * 90) % 360;
|
||||||
const sameDimensions = degrees == 0 || degrees == 180;
|
const sameDimensions = degrees == 0 || degrees == 180;
|
||||||
|
|||||||
@@ -2,7 +2,13 @@ import { isMainThread } from 'worker_threads';
|
|||||||
import { cpus } from 'os';
|
import { cpus } from 'os';
|
||||||
import { promises as fsp } from 'fs';
|
import { promises as fsp } from 'fs';
|
||||||
|
|
||||||
import { codecs as encoders, preprocessors } from './codecs.js';
|
import {
|
||||||
|
codecs as encoders,
|
||||||
|
preprocessors,
|
||||||
|
QuantOptions,
|
||||||
|
ResizeOptions,
|
||||||
|
RotateOptions,
|
||||||
|
} from './codecs.js';
|
||||||
import WorkerPool from './worker_pool.js';
|
import WorkerPool from './worker_pool.js';
|
||||||
import { autoOptimize } from './auto-optimizer.js';
|
import { autoOptimize } from './auto-optimizer.js';
|
||||||
import type ImageData from './image_data';
|
import type ImageData from './image_data';
|
||||||
@@ -12,6 +18,12 @@ type EncoderKey = keyof typeof encoders;
|
|||||||
type PreprocessorKey = keyof typeof preprocessors;
|
type PreprocessorKey = keyof typeof preprocessors;
|
||||||
type FileLike = Buffer | ArrayBuffer | string | ArrayBufferView;
|
type FileLike = Buffer | ArrayBuffer | string | ArrayBufferView;
|
||||||
|
|
||||||
|
type PreprocessOptions = {
|
||||||
|
resize?: ResizeOptions;
|
||||||
|
quant?: QuantOptions;
|
||||||
|
rotate?: RotateOptions;
|
||||||
|
};
|
||||||
|
|
||||||
async function decodeFile({
|
async function decodeFile({
|
||||||
file,
|
file,
|
||||||
}: {
|
}: {
|
||||||
@@ -183,10 +195,10 @@ class Image {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Define one or several preprocessors to use on the image.
|
* Define one or several preprocessors to use on the image.
|
||||||
* @param {object} preprocessOptions - An object with preprocessors to use, and their settings.
|
* @param {PreprocessOptions} preprocessOptions - An object with preprocessors to use, and their settings.
|
||||||
* @returns {Promise<undefined>} - A promise that resolves when all preprocessors have completed their work.
|
* @returns {Promise<undefined>} - A promise that resolves when all preprocessors have completed their work.
|
||||||
*/
|
*/
|
||||||
async preprocess(preprocessOptions = {}) {
|
async preprocess(preprocessOptions: PreprocessOptions = {}) {
|
||||||
for (const [name, options] of Object.entries(preprocessOptions)) {
|
for (const [name, options] of Object.entries(preprocessOptions)) {
|
||||||
if (!Object.keys(preprocessors).includes(name)) {
|
if (!Object.keys(preprocessors).includes(name)) {
|
||||||
throw Error(`Invalid preprocessor "${name}"`);
|
throw Error(`Invalid preprocessor "${name}"`);
|
||||||
|
|||||||
Reference in New Issue
Block a user