summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2023-09-05 01:31:24 -0400
committerpommicket <pommicket@gmail.com>2023-09-05 01:31:24 -0400
commit08db1dc9428ea5bc9604b229d686af1414d461af (patch)
treee6b38e0e8a0f7c0beeb53a893c908e22f2d4de09
parentebf696d4757b6cb040fbd7799149413cf2cfef9b (diff)
little size compeitino
-rw-r--r--.gitignore4
-rw-r--r--README.md22
-rwxr-xr-xcheck-size.sh6
-rw-r--r--examples/small_rgba.pngbin0 -> 94 bytes
-rw-r--r--examples/wasm-png/.cargo/config.toml2
-rw-r--r--examples/wasm-png/Cargo.toml14
-rw-r--r--examples/wasm-png/src/lib.rs17
-rw-r--r--examples/wasm-tiny-png/.cargo/config.toml2
-rw-r--r--examples/wasm-tiny-png/Cargo.toml14
-rw-r--r--examples/wasm-tiny-png/src/lib.rs15
-rw-r--r--src/lib.rs4
-rw-r--r--test/small_plt.pngbin0 -> 95 bytes
-rw-r--r--test/small_rgba.pngbin104 -> 0 bytes
13 files changed, 94 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore
index f40f6b7..45a013f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
-/target
-/Cargo.lock
+target
+Cargo.lock
flamegraph.svg
perf.data*
diff --git a/README.md b/README.md
index 746d3b8..15c3360 100644
--- a/README.md
+++ b/README.md
@@ -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
new file mode 100644
index 0000000..82e7fe9
--- /dev/null
+++ b/examples/small_rgba.png
Binary files differ
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
+}
diff --git a/src/lib.rs b/src/lib.rs
index 52da421..c626a0c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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
new file mode 100644
index 0000000..5c762cd
--- /dev/null
+++ b/test/small_plt.png
Binary files differ
diff --git a/test/small_rgba.png b/test/small_rgba.png
deleted file mode 100644
index 33d4c13..0000000
--- a/test/small_rgba.png
+++ /dev/null
Binary files differ