Skip to content

Commit

Permalink
update input type to support Uint8Array and Uint8ClampedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Aug 18, 2022
1 parent 134f0f8 commit 9b85408
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
36 changes: 35 additions & 1 deletion test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import writeImage from "./write-image";
test("jpg to png", async ({ eq }) => {
const jpg = findAndRead("flower.jpg");
const { height, width, pixels } = await readim({ data: jpg });
const result = writeImage({ data: pixels, debug: false, format: "png", height, width });
const result = writeImage({ data: pixels as Uint8Array, debug: false, format: "png", height, width });
writeFileSync("jpg-to-png.ts.png", result.data as Buffer);
eq(result.height, height);
eq(result.width, width);
Expand Down Expand Up @@ -46,6 +46,40 @@ test("jpg to jpg", async ({ eq }) => {
eq(Buffer.isBuffer(result.data), true);
});

test("Uint8Array[]", async ({ eq }) => {
const jpg = findAndRead("flower.jpg");
const { height, width, pixels } = await readim({ data: jpg });
const { data } = transform({
data: pixels,
from: "[r,c,b]",
to: "[b][r,c]",
sizes: { b: 4, r: height, c: width }
});
const typedData: Uint8Array[] = data.map(b => Uint8Array.from(b));
const result = writeImage({ data: typedData, debug: false, format: "png", height, width });
writeFileSync("jpg-to-png.u8[].png", result.data as Buffer);
eq(result.height, height);
eq(result.width, width);
eq(Buffer.isBuffer(result.data), true);
});

test("Uint8Array[][]", async ({ eq }) => {
const jpg = findAndRead("flower.jpg");
const { height, width, pixels } = await readim({ data: jpg });
const { data } = transform({
data: pixels,
from: "[r,c,b]",
to: "[b][r][c]",
sizes: { b: 4, r: height, c: width }
});
const typedData: Uint8Array[][] = data.map(b => b.map(r => Uint8Array.from(r)));
const result = writeImage({ data: typedData, debug: false, format: "png", height, width });
writeFileSync("jpg-to-png.u8[][].png", result.data as Buffer);
eq(result.height, height);
eq(result.width, width);
eq(Buffer.isBuffer(result.data), true);
});

test("JPEG", async ({ eq }) => {
const result = writeImage({
data: [
Expand Down
11 changes: 10 additions & 1 deletion write-image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ export default function writeImage({
width,
quality
}: {
data: number[] | number[][] | number[][][];
data:
| number[]
| number[][]
| number[][][]
| Uint8Array
| Uint8Array[]
| Uint8Array[][]
| Uint8ClampedArray
| Uint8ClampedArray[]
| Uint8ClampedArray[][];
debug?: boolean;
format: "jpeg" | "JPEG" | "jpg" | "JPG" | "png" | "PNG";
height: number;
Expand Down
1 change: 0 additions & 1 deletion write-image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { write } = require("fs");
const jpeg = require("jpeg-js");
const { PNG } = require("pngjs");
const toImageData = require("to-image-data");
Expand Down

0 comments on commit 9b85408

Please sign in to comment.