Skip to content

Commit 81fc73a

Browse files
committed
feat: support avif
- Add avif encode feature - Add documentation for `@napi-rs/image` package - Add tests for `webp` and `avif` encode function
1 parent 5511848 commit 81fc73a

33 files changed

Lines changed: 933 additions & 459 deletions

File tree

.github/workflows/CI.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ jobs:
5252
set -e &&
5353
unset RUSTFLAGS &&
5454
unset CC &&
55+
apk add --update --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing --no-cache nasm &&
5556
yarn build -- --features with_simd &&
5657
strip packages/*/*.node
5758
- host: macos-latest
@@ -197,7 +198,7 @@ jobs:
197198
usesh: true
198199
mem: 3000
199200
prepare: |
200-
pkg install -y curl node14 python2
201+
pkg install -y curl nasm node14 python2
201202
curl -qL https://www.npmjs.com/install.sh | sh
202203
npm install -g yarn
203204
curl https://sh.rustup.rs -sSf --output rustup.sh

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Cargo.lock
1313
!.yarn/versions
1414
.turbo
1515
*.tsbuildinfo
16-
optimized-lossless.*
17-
optimized.svg
18-
quantized.png
16+
optimized*
1917
lib
2018
dist

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
target
22
node_modules
3-
lib
3+
lib
4+
.yarn

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
[workspace]
2-
members = [
3-
"./packages/binding",
4-
]
2+
members = ["./packages/binding"]
53

64
[profile.release]
75
codegen-units = 1

README.md

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
Image processing library.
44

55
![CI](https://github.com/Brooooooklyn/image/workflows/CI/badge.svg)
6-
[![install size](https://packagephobia.com/badge?p=@napi-rs/image)](https://packagephobia.com/result?p=@napi-rs/image)
7-
[![Downloads](https://img.shields.io/npm/dm/@napi-rs/image.svg?sanitize=true)](https://npmcharts.com/compare/@napi-rs/image?minimal=true)
86

97
## Support matrix
108

@@ -23,69 +21,6 @@ Image processing library.
2321
| Android armv7 ||||||
2422
| FreeBSD x64 ||||||
2523

26-
## Lossless compression
24+
## `@napi-rs/image`
2725

28-
### `PNG`
29-
30-
```ts
31-
export interface PNGLosslessOptions {
32-
/**
33-
* Attempt to fix errors when decoding the input file rather than returning an Err.
34-
* Default: `false`
35-
*/
36-
fixErrors?: boolean | undefined | null
37-
/**
38-
* Write to output even if there was no improvement in compression.
39-
* Default: `false`
40-
*/
41-
force?: boolean | undefined | null
42-
/** Which filters to try on the file (0-5) */
43-
filter?: Array<number> | undefined | null
44-
/**
45-
* Whether to attempt bit depth reduction
46-
* Default: `true`
47-
*/
48-
bitDepthReduction?: boolean | undefined | null
49-
/**
50-
* Whether to attempt color type reduction
51-
* Default: `true`
52-
*/
53-
colorTypeReduction?: boolean | undefined | null
54-
/**
55-
* Whether to attempt palette reduction
56-
* Default: `true`
57-
*/
58-
paletteReduction?: boolean | undefined | null
59-
/**
60-
* Whether to attempt grayscale reduction
61-
* Default: `true`
62-
*/
63-
grayscaleReduction?: boolean | undefined | null
64-
/**
65-
* Whether to perform IDAT recoding
66-
* If any type of reduction is performed, IDAT recoding will be performed regardless of this setting
67-
* Default: `true`
68-
*/
69-
idatRecoding?: boolean | undefined | null
70-
/** Whether to remove ***All non-critical headers*** on PNG */
71-
strip?: boolean | undefined | null
72-
/** Whether to use heuristics to pick the best filter and compression */
73-
useHeuristics?: boolean | undefined | null
74-
}
75-
export function losslessCompressPng(input: Buffer, options?: PNGLosslessOptions | undefined | null): Buffer
76-
```
77-
78-
### `JPEG`
79-
80-
```ts
81-
export interface JpegCompressOptions {
82-
/** Output quality, default is 100 (lossless) */
83-
quality?: number | undefined | null
84-
/**
85-
* If true, it will use MozJPEG’s scan optimization. Makes progressive image files smaller.
86-
* Default is `true`
87-
*/
88-
optimizeScans?: boolean | undefined | null
89-
}
90-
export function compressJpeg(input: Buffer, options?: JpegCompressOptions | undefined | null): Buffer
91-
```
26+
See [Documentation for `@napi-rs/image`](./packages/binding)

example.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { readFileSync, writeFileSync } from 'fs'
2+
3+
import {
4+
losslessCompressPng,
5+
compressJpeg,
6+
pngQuantize,
7+
losslessEncodeWebp,
8+
encodeAvif,
9+
encodeWebp,
10+
} from '@napi-rs/image'
11+
12+
const PNG = readFileSync('./un-optimized.png')
13+
const JPEG = readFileSync('./un-optimized.jpg')
14+
15+
writeFileSync('optimized-lossless.png', losslessCompressPng(PNG))
16+
17+
writeFileSync('optimized-lossy.png', pngQuantize(PNG))
18+
19+
writeFileSync('optimized-lossless.jpg', compressJpeg(readFileSync('./un-optimized.jpg')))
20+
21+
writeFileSync('optimized-lossless.webp', losslessEncodeWebp(PNG))
22+
23+
writeFileSync('optimized-lossy-jpeg.webp', encodeWebp(JPEG, 90))
24+
25+
writeFileSync('optimized-lossy.webp', encodeWebp(PNG, 90))
26+
27+
writeFileSync('optimized.avif', encodeAvif(PNG))

optimize-test.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

package.json

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
],
1313
"devDependencies": {
1414
"@napi-rs/cli": "^2.5.0",
15+
"@taplo/cli": "^0.3.2",
1516
"@types/node": "^17.0.23",
1617
"ava": "^4.1.0",
1718
"lerna": "^4.0.0",
@@ -23,6 +24,10 @@
2324
"artifacts": "lerna run artifacts",
2425
"build": "lerna run build --stream --concurrency 1 --no-prefix",
2526
"build:ts": "tsc -b tsconfig.project.json",
27+
"format": "run-p format:prettier format:rs format:toml",
28+
"format:prettier": "prettier . -w",
29+
"format:rs": "cargo fmt",
30+
"format:toml": "taplo format",
2631
"test": "ava"
2732
},
2833
"prettier": {
@@ -36,11 +41,27 @@
3641
"extensions": [
3742
"mjs"
3843
],
39-
"timeout": "3m",
44+
"timeout": "10m",
4045
"environmentVariables": {
4146
"NODE_ENV": "ava"
4247
}
4348
},
49+
"lint-staged": {
50+
"*.@(js||ts|json|md|yml|yaml)": [
51+
"prettier --write"
52+
],
53+
"*.toml": [
54+
"taplo format"
55+
],
56+
"*.rs": [
57+
"cargo fmt --"
58+
]
59+
},
60+
"husky": {
61+
"hooks": {
62+
"pre-commit": "lint-staged && cargo fmt --all"
63+
}
64+
},
4465
"packageManager": "yarn@3.2.0",
4566
"repository": "git@github.com:Brooooooklyn/Image.git"
4667
}

packages/binding/Cargo.toml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ with_simd = ["mozjpeg-sys/nasm_simd_parallel_build"]
1313

1414
[dependencies]
1515
imagequant = "4.0.0"
16+
infer = "0.7"
17+
jpeg-decoder = "0.2"
1618
libc = "0.2"
17-
libwebp-sys = {version = "0.5", features = ["avx2", "sse41", "neon"]}
19+
libwebp-sys = { version = "0.5", features = ["avx2", "sse41", "neon"] }
1820
lodepng = "3"
19-
napi = {version = "2", default-features = false, features = ["napi3"]}
20-
napi-derive = {version = "2", default-features = false, features = ["type-def"]}
21+
napi = { version = "2", default-features = false, features = ["napi3"] }
22+
napi-derive = { version = "2", default-features = false, features = [
23+
"type-def",
24+
] }
25+
png = "0.17"
26+
ravif = "0.8"
27+
rgb = "0.8"
2128

2229
[dependencies.oxipng]
2330
default-features = false

0 commit comments

Comments
 (0)