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

Update to the latest image crate #16

Merged
merged 6 commits into from
Jan 16, 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
105 changes: 55 additions & 50 deletions wasm-thumbnail/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wasm-thumbnail/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ crate-type = ["rlib", "cdylib"]
wasm-bindgen = []

[dependencies]
image = { version = "0.23.10", default_features = false, features = ["gif", "jpeg", "png", "webp"] }
image = { default_features = false, features = ["gif", "jpeg", "png", "webp", "bmp", "ico"] } # leaving "avif-decoder" out of features until it can compile to wasm
2 changes: 1 addition & 1 deletion wasm-thumbnail/src/hook.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Function imported into WASM for reporting panic details
// Function imported into WASM for reporting panic details
extern "C" {
fn register_panic(
msg_ptr: *const u8,
Expand Down
31 changes: 19 additions & 12 deletions wasm-thumbnail/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::error::Error;
use std::io::Cursor;
use std::mem;
use std::os::raw::c_void;

use image;
use image::imageops::FilterType;
use image::DynamicImage;
use image::GenericImage;
use image::GenericImageView;
use image::ImageOutputFormat;

#[cfg(not(feature = "wasm-bindgen"))]
Expand All @@ -27,28 +27,35 @@ pub extern "C" fn resize_and_pad(
nsize: usize,
nquality: u8,
) -> *const u8 {
use std::io::Write;

register_panic_hook();

let slice: &[u8] = unsafe { std::slice::from_raw_parts(pointer, length) };

let mut out: Vec<u8> = Vec::with_capacity(nsize);
let mut out: Cursor<Vec<u8>> = Cursor::new(Vec::with_capacity(nsize));
// Reserve space at the start for length header
out.extend_from_slice(&[0, 0, 0, 0]);

if let Ok(thumbnail_len) = _resize_and_pad(slice, &mut out, nwidth, nheight, nsize, nquality) {
out.splice(..4, thumbnail_len.to_be_bytes().iter().cloned());
let _ = out.write_all(&[0,0,0,0]);

match _resize_and_pad(slice, &mut out, nwidth, nheight, nsize, nquality) {
Ok(thumbnail_len) => {
out.get_mut().splice(..4, thumbnail_len.to_be_bytes().iter().cloned());
}
_ => {
panic!("Image too large")
}
}

out.resize(nsize, 0);
out.get_mut().resize(nsize, 0);

let pointer = out.as_mut_ptr();
let pointer = out.get_mut().as_mut_ptr();
mem::forget(out);
pointer
}

pub fn _resize_and_pad(
slice: &[u8],
out: &mut Vec<u8>,
out: &mut Cursor<Vec<u8>>,
nwidth: u32,
nheight: u32,
nsize: usize,
Expand All @@ -65,11 +72,11 @@ pub fn _resize_and_pad(

result.write_to(out, ImageOutputFormat::Jpeg(nquality))?;

if out.len() > nsize {
if out.get_ref().len() > nsize {
return Err("size is too large".into());
}

Ok(out.len() as u32 - 4)
Ok(out.get_ref().len() as u32 - 4)
}

/// Allocate a new buffer in the wasm memory space
Expand All @@ -88,4 +95,4 @@ pub extern "C" fn deallocate(pointer: *mut c_void, capacity: usize) {
unsafe {
let _ = Vec::from_raw_parts(pointer, 0, capacity);
}
}
}
Loading