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
Changes from 1 commit
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
18 changes: 14 additions & 4 deletions wasm-thumbnail/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ pub extern "C" fn resize_and_pad(
let slice: &[u8] = unsafe { std::slice::from_raw_parts(pointer, length) };

let mut out: Vec<u8> = 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 mut mquality = nquality;

while mquality >= 15 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is definitely an improvement, but I think we could theoretically still not manage to create a small enough image for the size. (maybe for a massive resolution noisy image, with a small nsize).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but if we've made ~17 attempts to resize the image and failed, I think it's fair to fall back to previous behavior and have the external program notify the user that a different set of parameters is required, (smaller rez, different image, etc)

// 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, mquality) {
out.splice(..4, thumbnail_len.to_be_bytes().iter().cloned());
break;
}

mquality -= 5;
}

out.resize(nsize, 0);
Expand Down
Loading