Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update examples and site #85

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ jobs:
set -e &&
apt-get update &&
apt-get install meson -y &&
rm /usr/lib/llvm-16/lib/libc++.so &&
export LDFLAGS="-L/usr/lib/llvm-16/lib -L/usr/x86_64-unknown-linux-gnu/lib/gcc/x86_64-unknown-linux-gnu/4.8.5" &&
rm /usr/lib/llvm-18/lib/libc++.so &&
export LDFLAGS="-L/usr/lib/llvm-18/lib -L/usr/x86_64-unknown-linux-gnu/lib/gcc/x86_64-unknown-linux-gnu/4.8.5" &&
export CFLAGS="-fuse-ld=lld -B/usr/aarch64-unknown-linux-gnu/lib/gcc/aarch64-unknown-linux-gnu/4.8.5 --sysroot=/usr/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/sysroot" &&
export CXXFLAGS="-stdlib=libc++ -fuse-ld=lld -B/usr/aarch64-unknown-linux-gnu/lib/gcc/aarch64-unknown-linux-gnu/4.8.5 --sysroot=/usr/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/sysroot -L/usr/lib/llvm-15/lib" &&
yarn workspace @napi-rs/image build --features with_simd --target x86_64-unknown-linux-gnu &&
Expand Down Expand Up @@ -97,11 +97,11 @@ jobs:
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian-aarch64
build: >-
set -e &&
rm /usr/aarch64-unknown-linux-gnu/lib/llvm-16/lib/libunwind.so &&
rm /usr/aarch64-unknown-linux-gnu/lib/llvm-16/lib/libc++.so &&
rm /usr/aarch64-unknown-linux-gnu/lib/llvm-18/lib/libunwind.so &&
rm /usr/aarch64-unknown-linux-gnu/lib/llvm-18/lib/libc++.so &&
export LDFLAGS="-L/usr/aarch64-unknown-linux-gnu/lib/gcc/aarch64-unknown-linux-gnu/4.8.5" &&
export CFLAGS="-fuse-ld=lld -mcrc -B/usr/aarch64-unknown-linux-gnu/lib/gcc/aarch64-unknown-linux-gnu/4.8.5 --sysroot=/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot" &&
export CXXFLAGS="-fuse-ld=lld -mcrc -B/usr/aarch64-unknown-linux-gnu/lib/gcc/aarch64-unknown-linux-gnu/4.8.5 --sysroot=/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot -L/usr/aarch64-unknown-linux-gnu/lib/llvm-16/lib -stdlib=libc++" &&
export CXXFLAGS="-fuse-ld=lld -mcrc -B/usr/aarch64-unknown-linux-gnu/lib/gcc/aarch64-unknown-linux-gnu/4.8.5 --sysroot=/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot -L/usr/aarch64-unknown-linux-gnu/lib/llvm-18/lib -stdlib=libc++" &&
yarn workspace @napi-rs/image build --features with_simd --target aarch64-unknown-linux-gnu &&
chmod -R 777 target
- host: ubuntu-latest
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ You can clone this repo and run the following command to taste the example below
- `yarn install`
- `node example.mjs`

| Optimization | Raw | Raw Size | Optimized Size |
| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | -------------- |
| `losslessCompressPng()` <br/>**Lossless** | <img src="./un-optimized.png" width="400" />[CC-BY-SA 3.0](https://creativecommons.org/licenses/by/3.0) by [Niabot](https://commons.wikimedia.org/wiki/User:Niabot) | `888K` | `736K` |
| `pngQuantize({ maxQuality: 75 })` <br/>**Lossy** | <img src="./un-optimized.png" width="400" />[CC-BY-SA 3.0](https://creativecommons.org/licenses/by/3.0) by [Niabot](https://commons.wikimedia.org/wiki/User:Niabot) | `888K` | `248K` |
| `compressJpeg()` <br/>**Lossless** | <img src="./un-optimized.jpg" width="400" /> | `192K` | `184K` |
| `compressJpeg(75)` <br/>**Lossy** | <img src="./un-optimized.jpg" width="400" /> | `192K` | `104K` |
| `new Transformer(PNG).webpLossless()`<br/>**Lossless** | <img src="./un-optimized.png" width="400" />[CC-BY-SA 3.0](https://creativecommons.org/licenses/by/3.0) by [Niabot](https://commons.wikimedia.org/wiki/User:Niabot) | `888K` | `588K` |
| `new Transformer(PNG).webp(75)`<br/>**Lossy** | <img src="./un-optimized.png" width="400" />[CC-BY-SA 3.0](https://creativecommons.org/licenses/by/3.0) by [Niabot](https://commons.wikimedia.org/wiki/User:Niabot) | `888K` | `64K` |
| `Transformer(PNG).avif({ quality: 100 })`<br/>**Lossless** | <img src="./un-optimized.png" width="400" />[CC-BY-SA 3.0](https://creativecommons.org/licenses/by/3.0) by [Niabot](https://commons.wikimedia.org/wiki/User:Niabot) | `888K` | `536K` |
| `new Transformer(PNG).avif({ quality: 75, chromaSubsampling: ChromaSubsampling.Yuv420 })`<br/>**Lossy** | <img src="./un-optimized.png" width="400" />[CC-BY-SA 3.0](https://creativecommons.org/licenses/by/3.0) by [Niabot](https://commons.wikimedia.org/wiki/User:Niabot) | `888K` | `64K` |
| Optimization | Raw | Raw Size | Optimized Size |
| ------------------------------------------------------------------------------------------------------- | -------------------------------------------- | -------- | -------------- |
| `losslessCompressPng()` <br/>**Lossless** | <img src="./un-optimized.png" width="400" /> | `1.2M` | `876K` |
| `pngQuantize({ maxQuality: 75 })` <br/>**Lossy** | <img src="./un-optimized.png" width="400" /> | `1.2M` | `228K` |
| `compressJpeg()` <br/>**Lossless** | <img src="./un-optimized.jpg" width="400" /> | `192K` | `184K` |
| `compressJpeg(75)` <br/>**Lossy** | <img src="./un-optimized.jpg" width="400" /> | `192K` | `104K` |
| `new Transformer(PNG).webpLossless()`<br/>**Lossless** | <img src="./un-optimized.png" width="400" /> | `1.2M` | `676K` |
| `new Transformer(PNG).webp(75)`<br/>**Lossy** | <img src="./un-optimized.png" width="400" /> | `1.2M` | `84K` |
| `Transformer(PNG).avif({ quality: 100 })`<br/>**Lossless** | <img src="./un-optimized.png" width="400" /> | `1.2M` | `584K` |
| `new Transformer(PNG).avif({ quality: 75, chromaSubsampling: ChromaSubsampling.Yuv420 })`<br/>**Lossy** | <img src="./un-optimized.png" width="400" /> | `1.2M` | `112K` |

```js
import { readFileSync, writeFileSync } from 'fs'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"website"
],
"devDependencies": {
"@napi-rs/cli": "^3.0.0-alpha.38",
"@napi-rs/cli": "^3.0.0-alpha.40",
"@taplo/cli": "^0.7.0",
"@types/node": "^20.11.16",
"@types/sharp": "^0.31.1",
Expand Down
13 changes: 3 additions & 10 deletions packages/binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ image = { version = "0.24", default-features = false, features = [
"openexr",
] }
jpeg-decoder = "0.3"
libavif = { version = "0.13", default-features = false, features = [
"codec-aom",
] }
libc = "0.2"
lodepng = "3"
napi = { version = "2", default-features = false, features = ["napi3"] }
Expand Down Expand Up @@ -64,15 +67,5 @@ libwebp-sys = { version = "0.9", default-features = false, features = ["std", "p
[target.'cfg(all(target_os = "macos", target_arch = "x86_64"))'.dependencies]
libwebp-sys = { version = "0.9", default-features = false, features = ["std", "parallel"] }

[target.'cfg(target_family = "wasm")'.dependencies]
libavif = { git="https://github.com/njaard/libavif-rs.git", rev = "678bd68", version = "0.12", default-features = false, features = [
"codec-rav1e",
] }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
libavif = { git="https://github.com/njaard/libavif-rs.git", rev = "678bd68", version = "0.12", default-features = false, features = [
"codec-aom",
] }

[build-dependencies]
napi-build = "2"
18 changes: 14 additions & 4 deletions packages/binding/__test__/optimize.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { losslessCompressPng, pngQuantize, compressJpeg, Transformer } from '../

const ROOT_DIR = join(fileURLToPath(import.meta.url), '..', '..', '..', '..')

const PNG = await fs.readFile(join(ROOT_DIR, 'nasa-4928x3279.png'))
const PNG = await fs.readFile(join(ROOT_DIR, 'un-optimized.png'))
const JPEG = await fs.readFile(join(ROOT_DIR, 'un-optimized.jpg'))

test('should be able to lossless optimize png image', async (t) => {
Expand All @@ -21,7 +21,11 @@ test('should be able to lossless optimize png image', async (t) => {
})

test('should be able to lossy optimize png image', async (t) => {
const dest = await pngQuantize(PNG, { speed: 10 })
if (process.env.NAPI_RS_FORCE_WASI) {
t.pass()
return
}
const dest = await pngQuantize(PNG, { speed: 5, maxQuality: 1, minQuality: 1 })
t.true(dest.length < PNG.length)
})

Expand Down Expand Up @@ -49,9 +53,15 @@ test('should be able to lossless encode webp from jpeg', (t) => {
})

test('should be able to encode avif from png', (t) => {
t.true(new Transformer(PNG).avifSync().length < PNG.length)
t.true(new Transformer(PNG).avifSync({
speed: 10,
threads: 4,
}).length < PNG.length)
})

test('should be able to encode avif from jpeg', (t) => {
t.true(new Transformer(JPEG).avifSync().length < JPEG.length)
t.true(new Transformer(JPEG).avifSync({
speed: 10,
threads: 4,
}).length < JPEG.length)
})
4 changes: 2 additions & 2 deletions packages/binding/__test__/transformer.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const SVG = await fs.readFile(join(ROOT_DIR, 'input-debian.svg'))
test('should be able to get metadata from png', async (t) => {
const decoder = new Transformer(PNG)
const metadata = await decoder.metadata()
t.is(metadata.width, 1052)
t.is(metadata.height, 744)
t.is(metadata.width, 1024)
t.is(metadata.height, 681)
})

test('should be able to get metadata from jpg', async (t) => {
Expand Down
2 changes: 0 additions & 2 deletions packages/binding/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate napi_build;

fn main() {
napi_build::setup();
}
2 changes: 1 addition & 1 deletion packages/binding/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
"repository": "git@github.com:Brooooooklyn/Image.git",
"devDependencies": {
"@napi-rs/cli": "^3.0.0-alpha.38",
"@napi-rs/cli": "^3.0.0-alpha.40",
"@napi-rs/wasm-runtime": "^0.1.1"
}
}
4 changes: 2 additions & 2 deletions packages/binding/src/avif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ pub(crate) fn encode_avif_inner(
) -> Result<AvifData<'static>> {
let mut encoder = libavif::Encoder::new();
let config: Config = config.unwrap_or_default().into();
encoder.set_quality((63.0 * (1.0 - config.quality as f32 / 100.0)) as u8);
encoder.set_alpha_quality((63.0 * (1.0 - config.alpha_quality as f32 / 100.0)) as u8);
encoder.set_quality(config.quality);
encoder.set_alpha_quality(config.alpha_quality);
encoder.set_speed(config.speed);
encoder.set_max_threads(config.threads);
let (width, height) = input_image.dimensions();
Expand Down
9 changes: 9 additions & 0 deletions packages/binding/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,15 @@ impl Task for EncodeTask {
color_type,
)
}?;
if output_buf.is_null() {
return Err(Error::new(
Status::GenericFailure,
format!(
"Encode lossless webp failed, {}",
dynamic_image.as_bytes().len()
),
));
}
return Ok(EncodeOutput::Raw(output_buf, size));
}
EncodeOptions::Avif(ref options) => {
Expand Down
Binary file modified un-optimized.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading