summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-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
7 files changed, 64 insertions, 0 deletions
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
+}