ndarray-pixels
Advanced tools
Comparing version 4.0.0 to 4.1.0
import type { NdArray } from 'ndarray'; | ||
export interface EncoderOptions { | ||
quality?: number; | ||
} | ||
export declare function savePixelsInternal(pixels: NdArray<Uint8Array | Uint8ClampedArray>, mimeType: string): Promise<Uint8Array>; | ||
export declare function savePixelsInternal(pixels: NdArray<Uint8Array | Uint8ClampedArray>, mimeType: string, options?: EncoderOptions): Promise<Uint8Array>; | ||
import type { ImageEncodeOptions } from './common'; | ||
export declare function savePixelsInternal(pixels: NdArray<Uint8Array | Uint8ClampedArray>, options: ImageEncodeOptions): Promise<Uint8Array>; |
import { NdArray } from 'ndarray'; | ||
export interface ImageEncodeOptions { | ||
type?: string; | ||
quality?: number; | ||
} | ||
export declare function putPixelData(array: NdArray<Uint8Array | Uint8ClampedArray>, data: Uint8Array | Uint8ClampedArray, frame?: number): Uint8Array | Uint8ClampedArray; |
@@ -26,6 +26,11 @@ import type { NdArray } from 'ndarray'; | ||
* @param pixels ndarray of shape W x H x 4. | ||
* @param mimeType `image/jpeg`, `image/png`, etc. | ||
* @param typeOrOptions object with encoding options or just the type | ||
* @param typeOrOptions.type target format (`image/jpeg`, `image/png`, `image/webp`, etc.) | ||
* @param typeOrOptions.quality quality as a number from 0 to 1, inclusive | ||
* @returns | ||
*/ | ||
declare function savePixels(pixels: NdArray<Uint8Array | Uint8ClampedArray>, mimeType: string): Promise<Uint8Array>; | ||
declare function savePixels(pixels: NdArray<Uint8Array | Uint8ClampedArray>, typeOrOptions: string | { | ||
type?: string; | ||
quality?: number; | ||
}): Promise<Uint8Array>; | ||
export { getPixels, savePixels }; |
@@ -57,3 +57,3 @@ import ndarray from 'ndarray'; | ||
async function savePixelsInternal(pixels, mimeType, options = {}) { | ||
async function savePixelsInternal(pixels, options) { | ||
// Create OffscreenCanvas and write pixel data. | ||
@@ -65,17 +65,7 @@ const canvas = new OffscreenCanvas(pixels.shape[0], pixels.shape[1]); | ||
context.putImageData(imageData, 0, 0); | ||
const quality = options.quality ? options.quality / 100 : undefined; | ||
// Encode to target format. | ||
switch (mimeType) { | ||
case 'image/jpeg': | ||
return streamCanvas(canvas, 'image/jpeg', quality); | ||
default: | ||
return streamCanvas(canvas, mimeType); | ||
} | ||
return streamCanvas(canvas, options); | ||
} | ||
/** Creates readable stream from given OffscreenCanvas and options. */ | ||
async function streamCanvas(canvas, mimeType, quality) { | ||
const blob = await canvas.convertToBlob({ | ||
type: mimeType, | ||
quality | ||
}); | ||
async function streamCanvas(canvas, options) { | ||
const blob = await canvas.convertToBlob(options); | ||
const ab = await blob.arrayBuffer(); | ||
@@ -111,7 +101,21 @@ return new Uint8Array(ab); | ||
* @param pixels ndarray of shape W x H x 4. | ||
* @param mimeType `image/jpeg`, `image/png`, etc. | ||
* @param typeOrOptions object with encoding options or just the type | ||
* @param typeOrOptions.type target format (`image/jpeg`, `image/png`, `image/webp`, etc.) | ||
* @param typeOrOptions.quality quality as a number from 0 to 1, inclusive | ||
* @returns | ||
*/ | ||
async function savePixels(pixels, mimeType) { | ||
return savePixelsInternal(pixels, mimeType); | ||
async function savePixels(pixels, typeOrOptions) { | ||
let options; | ||
if (typeof typeOrOptions === 'string') { | ||
options = { | ||
type: typeOrOptions, | ||
quality: undefined | ||
}; | ||
} else { | ||
options = { | ||
type: typeOrOptions.type, | ||
quality: typeOrOptions.quality | ||
}; | ||
} | ||
return savePixelsInternal(pixels, options); | ||
} | ||
@@ -118,0 +122,0 @@ |
@@ -43,6 +43,20 @@ import ndarray from 'ndarray'; | ||
async function savePixelsInternal(pixels, mimeType) { | ||
async function savePixelsInternal(pixels, options) { | ||
const [width, height, channels] = pixels.shape; | ||
const data = putPixelData(pixels, new Uint8Array(width * height * channels)); | ||
const format = mimeType.replace('image/', ''); | ||
const { | ||
type, | ||
quality | ||
} = options; | ||
const format = (type != null ? type : 'image/png').replace('image/', ''); | ||
const sharpOptions = { | ||
// Applicable to most formats. | ||
// Where used, an integer between 1 and 100 | ||
quality: typeof quality === 'number' ? Math.round(1 + quality * 99) : undefined, | ||
// applicable to some formats, notably webp, avif | ||
lossless: quality === 1, | ||
// if this flag is true or unset, sharp interprets the `quality` flag to mean | ||
// that we want lossy color quantization. | ||
palette: false | ||
}; | ||
return sharp(data, { | ||
@@ -54,3 +68,3 @@ raw: { | ||
} | ||
}).toFormat(format).toBuffer(); | ||
}).toFormat(format, sharpOptions).toBuffer(); | ||
} | ||
@@ -84,7 +98,21 @@ | ||
* @param pixels ndarray of shape W x H x 4. | ||
* @param mimeType `image/jpeg`, `image/png`, etc. | ||
* @param typeOrOptions object with encoding options or just the type | ||
* @param typeOrOptions.type target format (`image/jpeg`, `image/png`, `image/webp`, etc.) | ||
* @param typeOrOptions.quality quality as a number from 0 to 1, inclusive | ||
* @returns | ||
*/ | ||
async function savePixels(pixels, mimeType) { | ||
return savePixelsInternal(pixels, mimeType); | ||
async function savePixels(pixels, typeOrOptions) { | ||
let options; | ||
if (typeof typeOrOptions === 'string') { | ||
options = { | ||
type: typeOrOptions, | ||
quality: undefined | ||
}; | ||
} else { | ||
options = { | ||
type: typeOrOptions.type, | ||
quality: typeOrOptions.quality | ||
}; | ||
} | ||
return savePixelsInternal(pixels, options); | ||
} | ||
@@ -91,0 +119,0 @@ |
import { NdArray } from 'ndarray'; | ||
export declare function savePixelsInternal(pixels: NdArray<Uint8Array | Uint8ClampedArray>, mimeType: string): Promise<Uint8Array>; | ||
import type { ImageEncodeOptions } from './common'; | ||
export declare function savePixelsInternal(pixels: NdArray<Uint8Array | Uint8ClampedArray>, options: ImageEncodeOptions): Promise<Uint8Array>; |
{ | ||
"name": "ndarray-pixels", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "ndarray-pixels", | ||
@@ -33,3 +33,3 @@ "type": "module", | ||
"lint": "eslint \"{src,test}/**/*.ts\"", | ||
"docs": "typedoc src/index.ts --plugin typedoc-plugin-markdown --out ./docs --hideBreadcrumbs && cat docs/modules.md | tail -n +12 | replace-between --target README.md --token API && rm -rf docs", | ||
"docs": "typedoc src/index.ts --plugin typedoc-plugin-markdown --out ./docs --hideBreadcrumbs --hidePageHeader && cat docs/functions/*.md | sed \"s/# /### /g\" | replace-between --target README.md --token API && rm -rf docs", | ||
"preversion": "yarn dist && yarn test", | ||
@@ -40,6 +40,6 @@ "version": "yarn dist && yarn docs && git add -u", | ||
"dependencies": { | ||
"@types/ndarray": "^1.0.11", | ||
"@types/ndarray": "^1.0.14", | ||
"ndarray": "^1.0.19", | ||
"ndarray-ops": "^1.2.2", | ||
"sharp": "^0.32.6" | ||
"sharp": "^0.33.4" | ||
}, | ||
@@ -49,3 +49,3 @@ "devDependencies": { | ||
"@types/ndarray-ops": "1.2.7", | ||
"@types/node": "20.12.7", | ||
"@types/node": "20.12.12", | ||
"@types/tape": "5.6.4", | ||
@@ -62,5 +62,5 @@ "browserify": "17.0.0", | ||
"typedoc": "0.25.13", | ||
"typedoc-plugin-markdown": "3.17.1", | ||
"typedoc-plugin-markdown": "4.0.2", | ||
"typescript": "5.4.5", | ||
"typescript-eslint": "^7.8.0" | ||
"typescript-eslint": "^7.9.0" | ||
}, | ||
@@ -73,3 +73,4 @@ "files": [ | ||
"package.json" | ||
] | ||
], | ||
"packageManager": "yarn@4.2.2" | ||
} |
@@ -88,6 +88,7 @@ # ndarray-pixels | ||
### getPixels | ||
▸ **getPixels**(`data`, `mimeType`): `Promise`\<`NdArray`\<`Uint8Array`\>\> | ||
### Function: getPixels() | ||
> **getPixels**(`data`, `mimeType`): `Promise`\<`NdArray`\<`Uint8Array`\>\> | ||
Decodes image data to an `ndarray`. | ||
@@ -102,7 +103,8 @@ | ||
| Name | Type | Description | | ||
| :------ | :------ | :------ | | ||
| `data` | `Uint8Array` | | | ||
| `mimeType` | `string` | `image/jpeg`, `image/png`, etc. | | ||
• **data**: `Uint8Array` | ||
• **mimeType**: `string` | ||
`image/jpeg`, `image/png`, etc. | ||
#### Returns | ||
@@ -112,12 +114,9 @@ | ||
#### Defined in | ||
#### Source | ||
[index.ts:17](https://github.com/donmccurdy/ndarray-pixels/blob/cff6320c9bbc3295943525c25cecd4acdcc9e63b/src/index.ts#L17) | ||
[index.ts:17](https://github.com/donmccurdy/ndarray-pixels/blob/c5bf2c6211099ea4610a51aa2cd7a7ed7c7c2df5/src/index.ts#L17) | ||
### Function: savePixels() | ||
___ | ||
> **savePixels**(`pixels`, `typeOrOptions`): `Promise`\<`Uint8Array`\> | ||
### savePixels | ||
▸ **savePixels**(`pixels`, `mimeType`): `Promise`\<`Uint8Array`\> | ||
Encodes an `ndarray` as image data in the given format. | ||
@@ -134,7 +133,10 @@ | ||
| Name | Type | Description | | ||
| :------ | :------ | :------ | | ||
| `pixels` | `NdArray`\<`Uint8Array` \| `Uint8ClampedArray`\> | ndarray of shape W x H x 4. | | ||
| `mimeType` | `string` | `image/jpeg`, `image/png`, etc. | | ||
• **pixels**: `NdArray`\<`Uint8Array` \| `Uint8ClampedArray`\> | ||
ndarray of shape W x H x 4. | ||
• **typeOrOptions**: `string` \| `object` | ||
object with encoding options or just the type | ||
#### Returns | ||
@@ -144,5 +146,5 @@ | ||
#### Defined in | ||
#### Source | ||
[index.ts:35](https://github.com/donmccurdy/ndarray-pixels/blob/cff6320c9bbc3295943525c25cecd4acdcc9e63b/src/index.ts#L35) | ||
[index.ts:37](https://github.com/donmccurdy/ndarray-pixels/blob/c5bf2c6211099ea4610a51aa2cd7a7ed7c7c2df5/src/index.ts#L37) | ||
<!--- API END ---> |
@@ -23,3 +23,3 @@ import ndarray from 'ndarray'; | ||
const canvas = new OffscreenCanvas(img.width, img.height) | ||
const canvas = new OffscreenCanvas(img.width, img.height); | ||
const context = canvas.getContext('2d')!; | ||
@@ -26,0 +26,0 @@ context.drawImage(img, 0, 0); |
import type { NdArray } from 'ndarray'; | ||
import { putPixelData } from './common'; | ||
import type { ImageEncodeOptions } from './common'; | ||
export interface EncoderOptions { | ||
quality?: number; | ||
} | ||
export async function savePixelsInternal( | ||
pixels: NdArray<Uint8Array | Uint8ClampedArray>, | ||
mimeType: string | ||
): Promise<Uint8Array>; | ||
export async function savePixelsInternal( | ||
pixels: NdArray<Uint8Array | Uint8ClampedArray>, | ||
mimeType: string, | ||
options?: EncoderOptions | ||
): Promise<Uint8Array>; | ||
export async function savePixelsInternal( | ||
pixels: NdArray<Uint8Array | Uint8ClampedArray>, | ||
mimeType: string, | ||
options: EncoderOptions = {} | ||
options: ImageEncodeOptions | ||
): Promise<Uint8Array> { | ||
// Create OffscreenCanvas and write pixel data. | ||
const canvas = new OffscreenCanvas(pixels.shape[0], pixels.shape[1]) | ||
const canvas = new OffscreenCanvas(pixels.shape[0], pixels.shape[1]); | ||
@@ -31,11 +18,3 @@ const context = canvas.getContext('2d')!; | ||
const quality = options.quality ? options.quality / 100 : undefined; | ||
// Encode to target format. | ||
switch (mimeType) { | ||
case 'image/jpeg': | ||
return streamCanvas(canvas, 'image/jpeg', quality); | ||
default: | ||
return streamCanvas(canvas, mimeType); | ||
} | ||
return streamCanvas(canvas, options); | ||
} | ||
@@ -46,11 +25,7 @@ | ||
canvas: OffscreenCanvas, | ||
mimeType: string, | ||
quality?: number | ||
options: ImageEncodeOptions | ||
): Promise<Uint8Array> { | ||
const blob = await canvas.convertToBlob({ | ||
type: mimeType, | ||
quality | ||
}) | ||
const ab = await blob.arrayBuffer() | ||
return new Uint8Array(ab) | ||
const blob = await canvas.convertToBlob(options); | ||
const ab = await blob.arrayBuffer(); | ||
return new Uint8Array(ab); | ||
} |
import ndarray, { NdArray } from 'ndarray'; | ||
import ops from 'ndarray-ops'; | ||
export interface ImageEncodeOptions { | ||
type?: string; | ||
quality?: number; | ||
} | ||
export function putPixelData( | ||
@@ -5,0 +10,0 @@ array: NdArray<Uint8Array | Uint8ClampedArray>, |
@@ -32,3 +32,5 @@ import type { NdArray } from 'ndarray'; | ||
* @param pixels ndarray of shape W x H x 4. | ||
* @param mimeType `image/jpeg`, `image/png`, etc. | ||
* @param typeOrOptions object with encoding options or just the type | ||
* @param typeOrOptions.type target format (`image/jpeg`, `image/png`, `image/webp`, etc.) | ||
* @param typeOrOptions.quality quality as a number from 0 to 1, inclusive | ||
* @returns | ||
@@ -38,7 +40,19 @@ */ | ||
pixels: NdArray<Uint8Array | Uint8ClampedArray>, | ||
mimeType: string | ||
typeOrOptions: string | { type?: string; quality?: number } | ||
): Promise<Uint8Array> { | ||
return savePixelsInternal(pixels, mimeType); | ||
let options; | ||
if (typeof typeOrOptions === 'string') { | ||
options = { | ||
type: typeOrOptions, | ||
quality: undefined, | ||
}; | ||
} else { | ||
options = { | ||
type: typeOrOptions.type, | ||
quality: typeOrOptions.quality, | ||
}; | ||
} | ||
return savePixelsInternal(pixels, options); | ||
} | ||
export { getPixels, savePixels }; |
import { NdArray } from 'ndarray'; | ||
import sharp, { AvailableFormatInfo } from 'sharp'; | ||
import sharp, { FormatEnum } from 'sharp'; | ||
import { putPixelData } from './common'; | ||
import type { ImageEncodeOptions } from './common'; | ||
export async function savePixelsInternal( | ||
pixels: NdArray<Uint8Array | Uint8ClampedArray>, | ||
mimeType: string | ||
options: ImageEncodeOptions | ||
): Promise<Uint8Array> { | ||
const [width, height, channels] = pixels.shape as [number, number, 4]; | ||
const data = putPixelData(pixels, new Uint8Array(width * height * channels)); | ||
const format = mimeType.replace('image/', '') as unknown as AvailableFormatInfo; | ||
return sharp(data, { raw: { width, height, channels } }).toFormat(format).toBuffer(); | ||
const { type, quality } = options; | ||
const format = (type ?? 'image/png').replace('image/', '') as keyof FormatEnum; | ||
const sharpOptions = { | ||
// Applicable to most formats. | ||
// Where used, an integer between 1 and 100 | ||
quality: typeof quality === 'number' ? Math.round(1 + quality * 99) : undefined, | ||
// applicable to some formats, notably webp, avif | ||
lossless: quality === 1, | ||
// if this flag is true or unset, sharp interprets the `quality` flag to mean | ||
// that we want lossy color quantization. | ||
palette: false, | ||
}; | ||
return sharp(data, { raw: { width, height, channels } }) | ||
.toFormat(format, sharpOptions) | ||
.toBuffer(); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85332
756
146
+ Added@emnapi/runtime@1.3.1(transitive)
+ Added@img/sharp-darwin-arm64@0.33.5(transitive)
+ Added@img/sharp-darwin-x64@0.33.5(transitive)
+ Added@img/sharp-libvips-darwin-arm64@1.0.4(transitive)
+ Added@img/sharp-libvips-darwin-x64@1.0.4(transitive)
+ Added@img/sharp-libvips-linux-arm@1.0.5(transitive)
+ Added@img/sharp-libvips-linux-arm64@1.0.4(transitive)
+ Added@img/sharp-libvips-linux-s390x@1.0.4(transitive)
+ Added@img/sharp-libvips-linux-x64@1.0.4(transitive)
+ Added@img/sharp-libvips-linuxmusl-arm64@1.0.4(transitive)
+ Added@img/sharp-libvips-linuxmusl-x64@1.0.4(transitive)
+ Added@img/sharp-linux-arm@0.33.5(transitive)
+ Added@img/sharp-linux-arm64@0.33.5(transitive)
+ Added@img/sharp-linux-s390x@0.33.5(transitive)
+ Added@img/sharp-linux-x64@0.33.5(transitive)
+ Added@img/sharp-linuxmusl-arm64@0.33.5(transitive)
+ Added@img/sharp-linuxmusl-x64@0.33.5(transitive)
+ Added@img/sharp-wasm32@0.33.5(transitive)
+ Added@img/sharp-win32-ia32@0.33.5(transitive)
+ Added@img/sharp-win32-x64@0.33.5(transitive)
+ Addedsharp@0.33.5(transitive)
+ Addedtslib@2.8.1(transitive)
- Removedb4a@1.6.7(transitive)
- Removedbare-events@2.5.0(transitive)
- Removedbare-fs@2.3.5(transitive)
- Removedbare-os@2.4.4(transitive)
- Removedbare-path@2.1.3(transitive)
- Removedbare-stream@2.4.2(transitive)
- Removedbase64-js@1.5.1(transitive)
- Removedbl@4.1.0(transitive)
- Removedbuffer@5.7.1(transitive)
- Removedchownr@1.1.4(transitive)
- Removeddecompress-response@6.0.0(transitive)
- Removeddeep-extend@0.6.0(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedexpand-template@2.0.3(transitive)
- Removedfast-fifo@1.3.2(transitive)
- Removedfs-constants@1.0.0(transitive)
- Removedgithub-from-package@0.0.0(transitive)
- Removedieee754@1.2.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedini@1.3.8(transitive)
- Removedmimic-response@3.1.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp-classic@0.5.3(transitive)
- Removednapi-build-utils@1.0.2(transitive)
- Removednode-abi@3.71.0(transitive)
- Removednode-addon-api@6.1.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedprebuild-install@7.1.2(transitive)
- Removedpump@3.0.2(transitive)
- Removedqueue-tick@1.0.1(transitive)
- Removedrc@1.2.8(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsharp@0.32.6(transitive)
- Removedsimple-concat@1.0.1(transitive)
- Removedsimple-get@4.0.1(transitive)
- Removedstreamx@2.20.2(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedstrip-json-comments@2.0.1(transitive)
- Removedtar-fs@2.1.13.0.6(transitive)
- Removedtar-stream@2.2.03.1.7(transitive)
- Removedtext-decoder@1.2.1(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwrappy@1.0.2(transitive)
Updated@types/ndarray@^1.0.14
Updatedsharp@^0.33.4