Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ndarray-pixels

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ndarray-pixels - npm Package Compare versions

Comparing version 4.0.0 to 4.1.0

7

dist/browser-save-pixels.d.ts
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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc