Skip to content

Commit

Permalink
Add loadFile parameter to ImagePool
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Aug 16, 2021
1 parent eeaa195 commit 1c5b44f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
15 changes: 7 additions & 8 deletions libsquoosh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You can start using the libSquoosh by adding these lines to the top of your JS p

```js
import { ImagePool } from '@squoosh/lib';
const imagePool = new ImagePool();
const imagePool = new ImagePool((url) => fs.readFile(url));
```

This will create an image pool with an underlying processing pipeline that you can use to ingest and encode images. The ImagePool constructor takes one argument that defines how many parallel operations it is allowed to run at any given time. By default, this number is set to the amount of CPU cores available in the system it is running on.
Expand All @@ -26,11 +26,11 @@ This will create an image pool with an underlying processing pipeline that you c
You can ingest a new image like so:

```js
const imagePath = 'path/to/image.png';
const imagePath = 'file://path/to/image.png';
const image = imagePool.ingestImage(imagePath);
```

The `ingestImage` function can take anything the node [`readFile`][readfile] function can take, including a buffer and `FileHandle`.
The `ingestImage` function will accept a URL path and call the `loadFile()` function defined in the `ImagePool`.

The returned `image` object is a representation of the original image, that you can now preprocess, encode, and extract information about.

Expand All @@ -39,33 +39,32 @@ The returned `image` object is a representation of the original image, that you
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 preprocessors.
await image.decoded; //Wait until the image is decoded before running preprocessors.

const preprocessOptions = {
//When both width and height are specified, the image resized to specified size.
resize: {
enabled: true,
width: 100,
height: 50,
}
},
/*
//When either width or height is specified, the image resized to specified size keeping aspect ratio.
resize: {
enabled: true,
width: 100,
}
*/
}
};
await image.preprocess(preprocessOptions);

const encodeOptions = {
mozjpeg: {}, //an empty object means 'use default settings'
jxl: {
quality: 90,
},
}
};
await image.encode(encodeOptions);

```

The default values for each option can be found in the [`codecs.ts`][codecs.ts] file under `defaultEncoderOptions`. Every unspecified value will use the default value specified there. _Better documentation is needed here._
Expand Down
21 changes: 12 additions & 9 deletions libsquoosh/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isMainThread } from 'worker_threads';
import { cpus } from 'os';
import { promises as fsp } from 'fs';

import { codecs as encoders, preprocessors } from './codecs.js';
Expand Down Expand Up @@ -169,12 +168,12 @@ function handleJob(params: JobMessage) {
* Represents an ingested image.
*/
class Image {
public file: FileLike;
public file: ArrayBuffer;
public workerPool: WorkerPool<JobMessage, any>;
public decoded: Promise<{ bitmap: ImageData }>;
public encodedWith: { [key: string]: any };

constructor(workerPool: WorkerPool<JobMessage, any>, file: FileLike) {
constructor(workerPool: WorkerPool<JobMessage, any>, file: ArrayBuffer) {
this.file = file;
this.workerPool = workerPool;
this.decoded = workerPool.dispatchJob({ operation: 'decode', file });
Expand Down Expand Up @@ -251,22 +250,26 @@ class Image {
*/
class ImagePool {
public workerPool: WorkerPool<JobMessage, any>;
public loadFile: (path: URL) => Promise<ArrayBuffer>;

/**
* Create a new pool.
* @param {number} [threads] - Number of concurrent image processes to run in the pool. Defaults to the number of CPU cores in the system.
* @param {(path: URL) => Promise<ArrayBuffer>} [loadFile] - A function that loads a file from a URL.
* @param {number} [threads] - Number of concurrent image processes to run in the pool.
*/
constructor(threads: number) {
this.workerPool = new WorkerPool(threads || cpus().length, __filename);
constructor(loadFile: (path: URL) => Promise<ArrayBuffer>, threads: number) {
this.loadFile = loadFile;
this.workerPool = new WorkerPool(threads, __filename);
}

/**
* Ingest an image into the image pool.
* @param {FileLike} image - The image or path to the image that should be ingested and decoded.
* @param {URL} url - The URL path to the image that should be ingested and decoded.
* @returns {Image} - A custom class reference to the decoded image.
*/
ingestImage(image: FileLike): Image {
return new Image(this.workerPool, image);
ingestImage(url: URL): Image {
const file = await this.loadFile(url);
return new Image(this.workerPool, file);
}

/**
Expand Down

0 comments on commit 1c5b44f

Please sign in to comment.