Skip to content

Commit

Permalink
Added decode tests
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannicalo committed Aug 1, 2021
1 parent ae77332 commit c055c5d
Show file tree
Hide file tree
Showing 17 changed files with 1,027 additions and 32 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
* text=auto
*.rgba binary
*.yuv binary
7 changes: 7 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ jobs:
run: npm install --no-save
- name: Lint
run: npm run lint
- name: Test
run: npm test
- if: ${{matrix.os == 'ubuntu-20.04'}}
name: Test coverage
uses: coverallsapp/github-action@1.1.3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
strategy:
matrix:
os:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
build
coverage
libraries
node_modules
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
.editorconfig
.eslintignore
.eslintrc.js
.gitattributes
.github
.gitignore
.npmignore
.vscode
**/*.spec.js
build
coverage
jest.config.js
jsconfig.json
libraries
test
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.21.0)
project("node-webp" VERSION 0.0.0)

set(CMAKE_CXX_STANDARD 23)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if(MSVC)
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
collectCoverage: true,
collectCoverageFrom: ["source/**/*.js"]
};
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
},
"description": "WebP",
"devDependencies": {
"eslint": "^7.31.0",
"eslint-config-giovanni": "giovannicalo/eslint-config"
"eslint": "^7.32.0",
"eslint-config-giovanni": "giovannicalo/eslint-config",
"jest": "^27.0.6"
},
"engines": {
"node": "^16.5.0"
Expand All @@ -34,7 +35,9 @@
"build": "cmake-js compile",
"build-debug": "cmake-js compile --debug",
"install": "node scripts/install && npm run build",
"lint": "eslint -f pretty ."
"lint": "eslint -f pretty .",
"start": "jest --watch",
"test": "jest"
},
"version": "0.0.0"
}
59 changes: 30 additions & 29 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Node WebP

[![Build Status](https://github.com/giovannicalo/node-webp/actions/workflows/build.yml/badge.svg)](https://github.com/giovannicalo/node-webp/actions/workflows/build.yml)
[![Coverage Status](https://coveralls.io/repos/github/giovannicalo/node-webp/badge.svg)](https://coveralls.io/github/giovannicalo/node-webp)

## Prerequisites

Expand All @@ -24,25 +25,25 @@ const { promises: { readFile, writeFile } } = require("fs");
const { Format, decode } = require("webp");

(async () => {
const webp = await readFile("foo.webp");
const rgba = await decode(webp, Format.rgba);
console.log(rgba);
// {
// data: Uint8ClampedArray(8294400) [...],
// format: 0,
// height: 1080,
// width: 1920
// }
await writeFile("foo.rgba", rgba.data);
const yuv = await decode(webp, Format.yuv);
console.log(yuv);
// {
// data: Uint8ClampedArray(3110400) [...],
// format: 1,
// height: 1080,
// width: 1920
// }
await writeFile("foo.yuv", yuv.data);
const webp = await readFile("foo.webp");
const rgba = await decode(webp, Format.rgba);
console.log(rgba);
// {
// data: Uint8ClampedArray(8294400) [...],
// format: 0,
// height: 1080,
// width: 1920
// }
await writeFile("foo.rgba", rgba.data);
const yuv = await decode(webp, Format.yuv);
console.log(yuv);
// {
// data: Uint8ClampedArray(3110400) [...],
// format: 1,
// height: 1080,
// width: 1920
// }
await writeFile("foo.yuv", yuv.data);
})();
```

Expand All @@ -54,16 +55,16 @@ const { promises: { readFile, writeFile } } = require("fs");
const { Format, encode } = require("webp");

(async () => {
const rgba = {
data: await readFile("foo.rgba"),
format: Format.rgba,
height: 1080,
width: 1920
};
const webp = await encode(rgba, 90);
console.log(webp);
// <Buffer ...>
await writeFile("foo.webp", webp);
const rgba = {
data: await readFile("foo.rgba"),
format: Format.rgba,
height: 1080,
width: 1920
};
const webp = await encode(rgba, 90);
console.log(webp);
// <Buffer ...>
await writeFile("foo.webp", webp);
})();
```

Expand Down
76 changes: 76 additions & 0 deletions source/decode.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const { readFileSync } = require("fs");
const { join } = require("path");

const { Format, decode } = require(".");

const path = join(__dirname, "..", "test");

const rgba511 = readFileSync(join(path, "511x511.rgba"));

const rgba512 = readFileSync(join(path, "512x512.rgba"));

const webp511 = readFileSync(join(path, "511x511.webp"));

const webp512 = readFileSync(join(path, "512x512.webp"));

const yuv511 = readFileSync(join(path, "511x511.yuv"));

const yuv512 = readFileSync(join(path, "512x512.yuv"));

it("should decode an even-sized WebP as RGBA", async () => {
const decoded = await decode(webp512, Format.rgba);
expect(Buffer.from(decoded.data).equals(rgba512)).toBeTruthy();
expect(decoded.format).toBe(Format.rgba);
expect(decoded.height).toBe(512);
expect(decoded.width).toBe(512);
});

it("should decode an odd-sized WebP as RGBA", async () => {
const decoded = await decode(webp511, Format.rgba);
expect(Buffer.from(decoded.data).equals(rgba511)).toBeTruthy();
expect(decoded.format).toBe(Format.rgba);
expect(decoded.height).toBe(511);
expect(decoded.width).toBe(511);
});

it("should decode an even-sized WebP as YUV", async () => {
const decoded = await decode(webp512, Format.yuv);
expect(Buffer.from(decoded.data).equals(yuv512)).toBeTruthy();
expect(decoded.format).toBe(Format.yuv);
expect(decoded.height).toBe(512);
expect(decoded.width).toBe(512);
});

it("should decode an odd-sized WebP as YUV", async () => {
const decoded = await decode(webp511, Format.yuv);
expect(Buffer.from(decoded.data).equals(yuv511)).toBeTruthy();
expect(decoded.format).toBe(Format.yuv);
expect(decoded.height).toBe(512);
expect(decoded.width).toBe(512);
});

it("should throw when given no arguments", () => {
expect(() => {
decode();
}).toThrow();
});

it("should throw when the first argument is not a buffer", () => {
expect(() => {
decode(42);
}).toThrow();
});

it("should throw when given a buffer that isn't a WebP", () => {
return expect(decode(Buffer.from([1, 2, 3]))).rejects.toThrow();
});

it("should throw when given a format that isn't RGBA or YUV", () => {
return expect(decode(webp512, 42)).rejects.toThrow();
});

it("shouldn't throw when given more than two arguments", () => {
expect(() => {
decode(webp512, Format.rgba, 42);
}).not.toThrow();
});
1 change: 1 addition & 0 deletions source/native/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace nodeWebp {
uint64_t uvSize = uvStride * std::floor((height + 1) / 2);
uint64_t size = ySize + uvSize * 2;
image = new Image(Format::yuv, width, height, size);
std::memset(image->data, 0, size);
if (!WebPDecodeYUVInto(
source.Data(),
source.ByteLength(),
Expand Down
434 changes: 434 additions & 0 deletions test/511x511.rgba

Large diffs are not rendered by default.

Binary file added test/511x511.webp
Binary file not shown.
Binary file added test/511x511.yuv
Binary file not shown.
458 changes: 458 additions & 0 deletions test/512x512.rgba

Large diffs are not rendered by default.

Binary file added test/512x512.webp
Binary file not shown.
1 change: 1 addition & 0 deletions test/512x512.yuv

Large diffs are not rendered by default.

0 comments on commit c055c5d

Please sign in to comment.