mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-11 16:26:20 +00:00
Stick to the term 'preprocessor'
This commit is contained in:
@@ -32,23 +32,23 @@ const image = imagePool.ingestImage(imagePath);
|
||||
|
||||
These `ingestImage` function can take anything the node [`readFile`][readFile] function can take, uncluding a buffer and `FileHandle`.
|
||||
|
||||
The returned `image` object is a representation of the original image, that you can now manipulate, encode, and extract information about.
|
||||
The returned `image` object is a representation of the original image, that you can now preprocess, encode, and extract information about.
|
||||
|
||||
## Manipulating and encoding images
|
||||
## Preprocessing and encoding images
|
||||
|
||||
When an image has been ingested, you can start manipulating it and encoding it to other formats. This example will resize the image and then encode it to a `.jpg` and `.jxl` image:
|
||||
When an image has been ingested, you can start preprocessing it and encoding it to other formats. This example will resize the image and then encode it to a `.jpg` and `.jxl` image:
|
||||
|
||||
```js
|
||||
await image.decoded; //Wait until the image is decoded before running manipulations
|
||||
await image.decoded; //Wait until the image is decoded before running preprocessors
|
||||
|
||||
const manipulateOptions: {
|
||||
const preprocessOptions: {
|
||||
resize: {
|
||||
enabled: true,
|
||||
width: 100,
|
||||
height: 50,
|
||||
}
|
||||
}
|
||||
await image.manipulate(manipulateOptions);
|
||||
await image.preprocess(preprocessOptions);
|
||||
|
||||
const encodeOptions: {
|
||||
mozjpeg: {}, //an empty object means 'use default settings'
|
||||
|
||||
@@ -2,11 +2,11 @@ import { isMainThread } from 'worker_threads';
|
||||
import { cpus } from 'os';
|
||||
import { promises as fsp } from 'fs';
|
||||
|
||||
import { codecs as encoders, preprocessors as manipulations } from './codecs.js';
|
||||
import { codecs as encoders, preprocessors} from './codecs.js';
|
||||
import WorkerPool from './worker_pool.js';
|
||||
import { autoOptimize } from './auto-optimizer.js';
|
||||
|
||||
export { ImagePool, encoders, manipulations };
|
||||
export { ImagePool, encoders, preprocessors};
|
||||
|
||||
|
||||
async function decodeFile({ file }) {
|
||||
@@ -30,9 +30,9 @@ async function decodeFile({ file }) {
|
||||
};
|
||||
}
|
||||
|
||||
async function manipulateImage({ manipulationType, options, image }) {
|
||||
const manipulator = await manipulations[manipulationType].instantiate();
|
||||
image.bitmap = await manipulator(
|
||||
async function preprocessImage({ preprocessorName, options, image }) {
|
||||
const preprocessor = await preprocessors[preprocessorName].instantiate();
|
||||
image.bitmap = await preprocessor(
|
||||
image.bitmap.data,
|
||||
image.bitmap.width,
|
||||
image.bitmap.height,
|
||||
@@ -103,8 +103,8 @@ function handleJob(params) {
|
||||
return encodeImage(params);
|
||||
case 'decode':
|
||||
return decodeFile(params);
|
||||
case 'manipulate':
|
||||
return manipulateImage(params);
|
||||
case 'preprocess':
|
||||
return preprocessImage(params);
|
||||
default:
|
||||
throw Error(`Invalid job "${operation}"`);
|
||||
}
|
||||
@@ -121,25 +121,25 @@ class Image {
|
||||
}
|
||||
|
||||
/**
|
||||
* Define one or several manipulations to apply to the image.
|
||||
* @param {object} manipulateOptions - An object with manipulations to apply, and their settings.
|
||||
* @returns {Promise<undefined>} - A promise that resolves when all manipulations have completed.
|
||||
* Define one or several preprocessors to use on the image.
|
||||
* @param {object} preprocessOptions - An object with preprocessors to use, and their settings.
|
||||
* @returns {Promise<undefined>} - A promise that resolves when all preprocessors have completed their work.
|
||||
*/
|
||||
async manipulate (manipulateOptions = {}) {
|
||||
for (const [type, options] of Object.entries(manipulateOptions)) {
|
||||
if (!Object.keys(manipulations).includes(type)) {
|
||||
throw Error(`Invalid manipulation type "${type}"`);
|
||||
async preprocess (preprocessOptions = {}) {
|
||||
for (const [name, options] of Object.entries(preprocessOptions)) {
|
||||
if (!Object.keys(preprocessors).includes(name)) {
|
||||
throw Error(`Invalid preprocessor "${name}"`);
|
||||
}
|
||||
const manipulatorOptions = Object.assign(
|
||||
const preprocessorOptions = Object.assign(
|
||||
{},
|
||||
manipulations[type].defaultOptions,
|
||||
preprocessors[name].defaultOptions,
|
||||
options,
|
||||
);
|
||||
this.decoded = this.workerPool.dispatchJob({
|
||||
operation: 'manipulate',
|
||||
manipulationType: type,
|
||||
operation: 'preprocess',
|
||||
preprocessorName: name,
|
||||
image: await this.decoded,
|
||||
options: manipulatorOptions,
|
||||
options: preprocessorOptions,
|
||||
});
|
||||
await this.decoded;
|
||||
}
|
||||
|
||||
14
cli/src/index.js
Normal file → Executable file
14
cli/src/index.js
Normal file → Executable file
@@ -8,7 +8,7 @@ import ora from 'ora';
|
||||
import kleur from 'kleur';
|
||||
|
||||
//Replace package name with '../../api/build/index.js' to test unpublished changes in the API
|
||||
import { ImagePool, manipulations, encoders } from '@squoosh/api';
|
||||
import { ImagePool, preprocessors, encoders } from '@squoosh/api';
|
||||
|
||||
function clamp(v, min, max) {
|
||||
if (v < min) return min;
|
||||
@@ -129,17 +129,17 @@ async function processFiles(files) {
|
||||
}),
|
||||
);
|
||||
|
||||
const manipulationOptions = {};
|
||||
const preprocessOptions = {};
|
||||
|
||||
for (const manipulatorType of Object.keys(manipulations)) {
|
||||
if (!program[manipulatorType]) {
|
||||
for (const preprocessorName of Object.keys(preprocessors)) {
|
||||
if (!program[preprocessorName]) {
|
||||
continue;
|
||||
}
|
||||
manipulationOptions[manipulatorType] = JSON5.parse(program[manipulatorType]);
|
||||
preprocessOptions[preprocessorName] = JSON5.parse(program[preprocessorName]);
|
||||
}
|
||||
|
||||
for(const image of decodedFiles){
|
||||
image.manipulate(manipulationOptions);
|
||||
image.preprocess(preprocessOptions);
|
||||
}
|
||||
|
||||
await Promise.all(decodedFiles.map( (image) => image.decoded ));
|
||||
@@ -214,7 +214,7 @@ program
|
||||
.action(processFiles);
|
||||
|
||||
// Create a CLI option for each supported preprocessor
|
||||
for (const [key, value] of Object.entries(manipulations)) {
|
||||
for (const [key, value] of Object.entries(preprocessors)) {
|
||||
program.option(`--${key} [config]`, value.description);
|
||||
}
|
||||
// Create a CLI option for each supported encoder
|
||||
|
||||
Reference in New Issue
Block a user