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`.
|
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
|
```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: {
|
resize: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
width: 100,
|
width: 100,
|
||||||
height: 50,
|
height: 50,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await image.manipulate(manipulateOptions);
|
await image.preprocess(preprocessOptions);
|
||||||
|
|
||||||
const encodeOptions: {
|
const encodeOptions: {
|
||||||
mozjpeg: {}, //an empty object means 'use default settings'
|
mozjpeg: {}, //an empty object means 'use default settings'
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ 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 as manipulations } from './codecs.js';
|
import { codecs as encoders, preprocessors} 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';
|
||||||
|
|
||||||
export { ImagePool, encoders, manipulations };
|
export { ImagePool, encoders, preprocessors};
|
||||||
|
|
||||||
|
|
||||||
async function decodeFile({ file }) {
|
async function decodeFile({ file }) {
|
||||||
@@ -30,9 +30,9 @@ async function decodeFile({ file }) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function manipulateImage({ manipulationType, options, image }) {
|
async function preprocessImage({ preprocessorName, options, image }) {
|
||||||
const manipulator = await manipulations[manipulationType].instantiate();
|
const preprocessor = await preprocessors[preprocessorName].instantiate();
|
||||||
image.bitmap = await manipulator(
|
image.bitmap = await preprocessor(
|
||||||
image.bitmap.data,
|
image.bitmap.data,
|
||||||
image.bitmap.width,
|
image.bitmap.width,
|
||||||
image.bitmap.height,
|
image.bitmap.height,
|
||||||
@@ -103,8 +103,8 @@ function handleJob(params) {
|
|||||||
return encodeImage(params);
|
return encodeImage(params);
|
||||||
case 'decode':
|
case 'decode':
|
||||||
return decodeFile(params);
|
return decodeFile(params);
|
||||||
case 'manipulate':
|
case 'preprocess':
|
||||||
return manipulateImage(params);
|
return preprocessImage(params);
|
||||||
default:
|
default:
|
||||||
throw Error(`Invalid job "${operation}"`);
|
throw Error(`Invalid job "${operation}"`);
|
||||||
}
|
}
|
||||||
@@ -121,25 +121,25 @@ class Image {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define one or several manipulations to apply to the image.
|
* Define one or several preprocessors to use on the image.
|
||||||
* @param {object} manipulateOptions - An object with manipulations to apply, and their settings.
|
* @param {object} preprocessOptions - An object with preprocessors to use, and their settings.
|
||||||
* @returns {Promise<undefined>} - A promise that resolves when all manipulations have completed.
|
* @returns {Promise<undefined>} - A promise that resolves when all preprocessors have completed their work.
|
||||||
*/
|
*/
|
||||||
async manipulate (manipulateOptions = {}) {
|
async preprocess (preprocessOptions = {}) {
|
||||||
for (const [type, options] of Object.entries(manipulateOptions)) {
|
for (const [name, options] of Object.entries(preprocessOptions)) {
|
||||||
if (!Object.keys(manipulations).includes(type)) {
|
if (!Object.keys(preprocessors).includes(name)) {
|
||||||
throw Error(`Invalid manipulation type "${type}"`);
|
throw Error(`Invalid preprocessor "${name}"`);
|
||||||
}
|
}
|
||||||
const manipulatorOptions = Object.assign(
|
const preprocessorOptions = Object.assign(
|
||||||
{},
|
{},
|
||||||
manipulations[type].defaultOptions,
|
preprocessors[name].defaultOptions,
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
this.decoded = this.workerPool.dispatchJob({
|
this.decoded = this.workerPool.dispatchJob({
|
||||||
operation: 'manipulate',
|
operation: 'preprocess',
|
||||||
manipulationType: type,
|
preprocessorName: name,
|
||||||
image: await this.decoded,
|
image: await this.decoded,
|
||||||
options: manipulatorOptions,
|
options: preprocessorOptions,
|
||||||
});
|
});
|
||||||
await this.decoded;
|
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';
|
import kleur from 'kleur';
|
||||||
|
|
||||||
//Replace package name with '../../api/build/index.js' to test unpublished changes in the API
|
//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) {
|
function clamp(v, min, max) {
|
||||||
if (v < min) return min;
|
if (v < min) return min;
|
||||||
@@ -129,17 +129,17 @@ async function processFiles(files) {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const manipulationOptions = {};
|
const preprocessOptions = {};
|
||||||
|
|
||||||
for (const manipulatorType of Object.keys(manipulations)) {
|
for (const preprocessorName of Object.keys(preprocessors)) {
|
||||||
if (!program[manipulatorType]) {
|
if (!program[preprocessorName]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
manipulationOptions[manipulatorType] = JSON5.parse(program[manipulatorType]);
|
preprocessOptions[preprocessorName] = JSON5.parse(program[preprocessorName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(const image of decodedFiles){
|
for(const image of decodedFiles){
|
||||||
image.manipulate(manipulationOptions);
|
image.preprocess(preprocessOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(decodedFiles.map( (image) => image.decoded ));
|
await Promise.all(decodedFiles.map( (image) => image.decoded ));
|
||||||
@@ -214,7 +214,7 @@ program
|
|||||||
.action(processFiles);
|
.action(processFiles);
|
||||||
|
|
||||||
// Create a CLI option for each supported preprocessor
|
// 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);
|
program.option(`--${key} [config]`, value.description);
|
||||||
}
|
}
|
||||||
// Create a CLI option for each supported encoder
|
// Create a CLI option for each supported encoder
|
||||||
|
|||||||
Reference in New Issue
Block a user