diff options
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | README.md | 22 | ||||
-rwxr-xr-x | check-size.sh | 6 | ||||
-rw-r--r-- | examples/small_rgba.png | bin | 0 -> 94 bytes | |||
-rw-r--r-- | examples/wasm-png/.cargo/config.toml | 2 | ||||
-rw-r--r-- | examples/wasm-png/Cargo.toml | 14 | ||||
-rw-r--r-- | examples/wasm-png/src/lib.rs | 17 | ||||
-rw-r--r-- | examples/wasm-tiny-png/.cargo/config.toml | 2 | ||||
-rw-r--r-- | examples/wasm-tiny-png/Cargo.toml | 14 | ||||
-rw-r--r-- | examples/wasm-tiny-png/src/lib.rs | 15 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | test/small_plt.png | bin | 0 -> 95 bytes | |||
-rw-r--r-- | test/small_rgba.png | bin | 104 -> 0 bytes |
13 files changed, 94 insertions, 6 deletions
@@ -1,4 +1,4 @@ -/target -/Cargo.lock +target +Cargo.lock flamegraph.svg perf.data* @@ -4,6 +4,8 @@ Tiny Rust PNG decoder. This decoder can be used without `std` or `alloc` by disabling the `std` feature (enabled by default). +Also it has tiny code size (e.g. 5x smaller `.wasm.gz` size compared to the `png` crate — see `check-size.sh`). + ## Goals - Correctly decode all valid non-interlaced PNG files which can fit in memory. @@ -63,3 +65,23 @@ A `pre-commit` git hook is provided to run `cargo fmt` and `cargo clippy`. You c ```sh ln -s ../../pre-commit .git/hooks/ ``` + +## License + +```text +Zero-Clause BSD +============= + +Permission to use, copy, modify, and/or distribute this software for +any purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL +WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE +FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY +DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN +AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +``` + +Note: all the test PNG images are either in the U.S. public domain or are CC0-licensed. diff --git a/check-size.sh b/check-size.sh new file mode 100755 index 0000000..6f12db0 --- /dev/null +++ b/check-size.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +cd examples/wasm-tiny-png && cargo b --release && cd ../.. && wasm-opt -Oz examples/wasm-tiny-png/target/wasm32-unknown-unknown/release/wasm_tiny_png.wasm -o target/tiny_png.wasm && gzip -f target/tiny_png.wasm || exit 1 +cd examples/wasm-png && cargo b --release && cd ../.. && wasm-opt -Oz examples/wasm-png/target/wasm32-unknown-unknown/release/wasm_png.wasm -o target/png.wasm && gzip -f target/png.wasm || exit 1 + +wc -c target/png.wasm.gz target/tiny_png.wasm.gz | head -n2 diff --git a/examples/small_rgba.png b/examples/small_rgba.png Binary files differnew file mode 100644 index 0000000..82e7fe9 --- /dev/null +++ b/examples/small_rgba.png diff --git a/examples/wasm-png/.cargo/config.toml b/examples/wasm-png/.cargo/config.toml new file mode 100644 index 0000000..f4e8c00 --- /dev/null +++ b/examples/wasm-png/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "wasm32-unknown-unknown" diff --git a/examples/wasm-png/Cargo.toml b/examples/wasm-png/Cargo.toml new file mode 100644 index 0000000..edd3a9d --- /dev/null +++ b/examples/wasm-png/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "wasm-png" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +png = "0.17.10" + +[profile.release] +opt-level = "z" +lto = true diff --git a/examples/wasm-png/src/lib.rs b/examples/wasm-png/src/lib.rs new file mode 100644 index 0000000..1c3c150 --- /dev/null +++ b/examples/wasm-png/src/lib.rs @@ -0,0 +1,17 @@ +#[no_mangle] +pub unsafe fn decode_png(input: *const u8, input_len: usize, output: *mut u8, output_len: usize) -> usize { + let input = core::slice::from_raw_parts(input, input_len); + let output = core::slice::from_raw_parts_mut(output, output_len); + + let decoder = png::Decoder::new(input); + let Ok(mut reader) = decoder.read_info() else { + return 0; + }; + let required_bytes = reader.output_buffer_size(); + if output_len >= required_bytes { + if reader.next_frame(output).is_err() { + return 0; + } + } + required_bytes +} diff --git a/examples/wasm-tiny-png/.cargo/config.toml b/examples/wasm-tiny-png/.cargo/config.toml new file mode 100644 index 0000000..f4e8c00 --- /dev/null +++ b/examples/wasm-tiny-png/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "wasm32-unknown-unknown" diff --git a/examples/wasm-tiny-png/Cargo.toml b/examples/wasm-tiny-png/Cargo.toml new file mode 100644 index 0000000..cbbfd03 --- /dev/null +++ b/examples/wasm-tiny-png/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "wasm-tiny-png" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +tiny-png = { path = "../..", default-features = false } + +[profile.release] +opt-level = "z" +lto = true diff --git a/examples/wasm-tiny-png/src/lib.rs b/examples/wasm-tiny-png/src/lib.rs new file mode 100644 index 0000000..ff553f1 --- /dev/null +++ b/examples/wasm-tiny-png/src/lib.rs @@ -0,0 +1,15 @@ +#[no_mangle] +pub unsafe fn decode_png(input: *const u8, input_len: usize, output: *mut u8, output_len: usize) -> usize { + let mut input = core::slice::from_raw_parts(input, input_len); + let output = core::slice::from_raw_parts_mut(output, output_len); + let Ok(header) = tiny_png::read_png_header(&mut input) else { + return 0; + }; + let required_bytes = header.required_bytes(); + if output_len >= required_bytes { + if tiny_png::read_png(&mut input, Some(&header), output).is_err() { + return 0; + } + } + required_bytes +} @@ -1205,10 +1205,6 @@ mod tests { test_both!("test/small_rgb.png"); } #[test] - fn test_small_rgba() { - test_both!("test/small_rgba.png"); - } - #[test] fn test_gray_alpha() { test_both!("test/gray_alpha.png"); } diff --git a/test/small_plt.png b/test/small_plt.png Binary files differnew file mode 100644 index 0000000..5c762cd --- /dev/null +++ b/test/small_plt.png diff --git a/test/small_rgba.png b/test/small_rgba.png Binary files differdeleted file mode 100644 index 33d4c13..0000000 --- a/test/small_rgba.png +++ /dev/null |