mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 16:57:26 +00:00
Merge pull request #1141 from ergunsh/improve-exposed-types
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: 'triangle' | '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;
|
||||||
@@ -52,6 +61,7 @@ import mozEnc from '../../codecs/mozjpeg/enc/mozjpeg_node_enc.js';
|
|||||||
import mozEncWasm from 'asset-url:../../codecs/mozjpeg/enc/mozjpeg_node_enc.wasm';
|
import mozEncWasm from 'asset-url:../../codecs/mozjpeg/enc/mozjpeg_node_enc.wasm';
|
||||||
import mozDec from '../../codecs/mozjpeg/dec/mozjpeg_node_dec.js';
|
import mozDec from '../../codecs/mozjpeg/dec/mozjpeg_node_dec.js';
|
||||||
import mozDecWasm from 'asset-url:../../codecs/mozjpeg/dec/mozjpeg_node_dec.wasm';
|
import mozDecWasm from 'asset-url:../../codecs/mozjpeg/dec/mozjpeg_node_dec.wasm';
|
||||||
|
import type { EncodeOptions as MozJPEGEncodeOptions } from '../../codecs/mozjpeg/enc/mozjpeg_enc';
|
||||||
|
|
||||||
// WebP
|
// WebP
|
||||||
import type { WebPModule as WebPEncodeModule } from '../../codecs/webp/enc/webp_enc';
|
import type { WebPModule as WebPEncodeModule } from '../../codecs/webp/enc/webp_enc';
|
||||||
@@ -59,6 +69,7 @@ import webpEnc from '../../codecs/webp/enc/webp_node_enc.js';
|
|||||||
import webpEncWasm from 'asset-url:../../codecs/webp/enc/webp_node_enc.wasm';
|
import webpEncWasm from 'asset-url:../../codecs/webp/enc/webp_node_enc.wasm';
|
||||||
import webpDec from '../../codecs/webp/dec/webp_node_dec.js';
|
import webpDec from '../../codecs/webp/dec/webp_node_dec.js';
|
||||||
import webpDecWasm from 'asset-url:../../codecs/webp/dec/webp_node_dec.wasm';
|
import webpDecWasm from 'asset-url:../../codecs/webp/dec/webp_node_dec.wasm';
|
||||||
|
import type { EncodeOptions as WebPEncodeOptions } from '../../codecs/webp/enc/webp_enc.js';
|
||||||
|
|
||||||
// AVIF
|
// AVIF
|
||||||
import type { AVIFModule as AVIFEncodeModule } from '../../codecs/avif/enc/avif_enc';
|
import type { AVIFModule as AVIFEncodeModule } from '../../codecs/avif/enc/avif_enc';
|
||||||
@@ -69,6 +80,7 @@ import avifEncMtWorker from 'chunk-url:../../codecs/avif/enc/avif_node_enc_mt.wo
|
|||||||
import avifEncMtWasm from 'asset-url:../../codecs/avif/enc/avif_node_enc_mt.wasm';
|
import avifEncMtWasm from 'asset-url:../../codecs/avif/enc/avif_node_enc_mt.wasm';
|
||||||
import avifDec from '../../codecs/avif/dec/avif_node_dec.js';
|
import avifDec from '../../codecs/avif/dec/avif_node_dec.js';
|
||||||
import avifDecWasm from 'asset-url:../../codecs/avif/dec/avif_node_dec.wasm';
|
import avifDecWasm from 'asset-url:../../codecs/avif/dec/avif_node_dec.wasm';
|
||||||
|
import type { EncodeOptions as AvifEncodeOptions } from '../../codecs/avif/enc/avif_enc.js';
|
||||||
|
|
||||||
// JXL
|
// JXL
|
||||||
import type { JXLModule as JXLEncodeModule } from '../../codecs/jxl/enc/jxl_enc';
|
import type { JXLModule as JXLEncodeModule } from '../../codecs/jxl/enc/jxl_enc';
|
||||||
@@ -76,6 +88,7 @@ import jxlEnc from '../../codecs/jxl/enc/jxl_node_enc.js';
|
|||||||
import jxlEncWasm from 'asset-url:../../codecs/jxl/enc/jxl_node_enc.wasm';
|
import jxlEncWasm from 'asset-url:../../codecs/jxl/enc/jxl_node_enc.wasm';
|
||||||
import jxlDec from '../../codecs/jxl/dec/jxl_node_dec.js';
|
import jxlDec from '../../codecs/jxl/dec/jxl_node_dec.js';
|
||||||
import jxlDecWasm from 'asset-url:../../codecs/jxl/dec/jxl_node_dec.wasm';
|
import jxlDecWasm from 'asset-url:../../codecs/jxl/dec/jxl_node_dec.wasm';
|
||||||
|
import type { EncodeOptions as JxlEncodeOptions } from '../../codecs/jxl/enc/jxl_enc.js';
|
||||||
|
|
||||||
// WP2
|
// WP2
|
||||||
import type { WP2Module as WP2EncodeModule } from '../../codecs/wp2/enc/wp2_enc';
|
import type { WP2Module as WP2EncodeModule } from '../../codecs/wp2/enc/wp2_enc';
|
||||||
@@ -83,6 +96,7 @@ import wp2Enc from '../../codecs/wp2/enc/wp2_node_enc.js';
|
|||||||
import wp2EncWasm from 'asset-url:../../codecs/wp2/enc/wp2_node_enc.wasm';
|
import wp2EncWasm from 'asset-url:../../codecs/wp2/enc/wp2_node_enc.wasm';
|
||||||
import wp2Dec from '../../codecs/wp2/dec/wp2_node_dec.js';
|
import wp2Dec from '../../codecs/wp2/dec/wp2_node_dec.js';
|
||||||
import wp2DecWasm from 'asset-url:../../codecs/wp2/dec/wp2_node_dec.wasm';
|
import wp2DecWasm from 'asset-url:../../codecs/wp2/dec/wp2_node_dec.wasm';
|
||||||
|
import type { EncodeOptions as WP2EncodeOptions } from '../../codecs/wp2/enc/wp2_enc.js';
|
||||||
|
|
||||||
// PNG
|
// PNG
|
||||||
import * as pngEncDec from '../../codecs/png/pkg/squoosh_png.js';
|
import * as pngEncDec from '../../codecs/png/pkg/squoosh_png.js';
|
||||||
@@ -95,6 +109,9 @@ const pngEncDecPromise = pngEncDec.default(
|
|||||||
import * as oxipng from '../../codecs/oxipng/pkg/squoosh_oxipng.js';
|
import * as oxipng from '../../codecs/oxipng/pkg/squoosh_oxipng.js';
|
||||||
import oxipngWasm from 'asset-url:../../codecs/oxipng/pkg/squoosh_oxipng_bg.wasm';
|
import oxipngWasm from 'asset-url:../../codecs/oxipng/pkg/squoosh_oxipng_bg.wasm';
|
||||||
const oxipngPromise = oxipng.default(fsp.readFile(pathify(oxipngWasm)));
|
const oxipngPromise = oxipng.default(fsp.readFile(pathify(oxipngWasm)));
|
||||||
|
interface OxiPngEncodeOptions {
|
||||||
|
level: number;
|
||||||
|
}
|
||||||
|
|
||||||
// Resize
|
// Resize
|
||||||
import * as resize from '../../codecs/resize/pkg/squoosh_resize.js';
|
import * as resize from '../../codecs/resize/pkg/squoosh_resize.js';
|
||||||
@@ -171,13 +188,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 +229,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 +250,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;
|
||||||
@@ -401,13 +412,13 @@ export const codecs = {
|
|||||||
jxlEncWasm,
|
jxlEncWasm,
|
||||||
),
|
),
|
||||||
defaultEncoderOptions: {
|
defaultEncoderOptions: {
|
||||||
speed: 4,
|
effort: 1,
|
||||||
quality: 75,
|
quality: 75,
|
||||||
progressive: false,
|
progressive: false,
|
||||||
epf: -1,
|
epf: -1,
|
||||||
nearLossless: 0,
|
|
||||||
lossyPalette: false,
|
lossyPalette: false,
|
||||||
decodingSpeedTier: 0,
|
decodingSpeedTier: 0,
|
||||||
|
photonNoiseIso: 0,
|
||||||
},
|
},
|
||||||
autoOptimize: {
|
autoOptimize: {
|
||||||
option: 'quality',
|
option: 'quality',
|
||||||
@@ -480,3 +491,12 @@ export const codecs = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
|
export {
|
||||||
|
MozJPEGEncodeOptions,
|
||||||
|
WebPEncodeOptions,
|
||||||
|
AvifEncodeOptions,
|
||||||
|
JxlEncodeOptions,
|
||||||
|
WP2EncodeOptions,
|
||||||
|
OxiPngEncodeOptions,
|
||||||
|
};
|
||||||
|
|||||||
@@ -2,7 +2,19 @@ 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 {
|
||||||
|
AvifEncodeOptions,
|
||||||
|
codecs as encoders,
|
||||||
|
JxlEncodeOptions,
|
||||||
|
MozJPEGEncodeOptions,
|
||||||
|
OxiPngEncodeOptions,
|
||||||
|
preprocessors,
|
||||||
|
QuantOptions,
|
||||||
|
ResizeOptions,
|
||||||
|
RotateOptions,
|
||||||
|
WebPEncodeOptions,
|
||||||
|
WP2EncodeOptions,
|
||||||
|
} 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 +24,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 +201,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}"`);
|
||||||
@@ -217,7 +235,12 @@ class Image {
|
|||||||
optimizerButteraugliTarget?: number;
|
optimizerButteraugliTarget?: number;
|
||||||
maxOptimizerRounds?: number;
|
maxOptimizerRounds?: number;
|
||||||
} & {
|
} & {
|
||||||
[key in EncoderKey]?: any; // any is okay for now
|
mozjpeg?: Partial<MozJPEGEncodeOptions>;
|
||||||
|
webp?: Partial<WebPEncodeOptions>;
|
||||||
|
avif?: Partial<AvifEncodeOptions>;
|
||||||
|
jxl?: Partial<JxlEncodeOptions>;
|
||||||
|
wp2?: Partial<WP2EncodeOptions>;
|
||||||
|
oxipng?: Partial<OxiPngEncodeOptions>;
|
||||||
} = {},
|
} = {},
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { bitmap } = await this.decoded;
|
const { bitmap } = await this.decoded;
|
||||||
|
|||||||
Reference in New Issue
Block a user