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

fix image example buffer size calculation #359

Merged
merged 6 commits into from
Aug 25, 2024
Merged

Conversation

JanNeuenfeld
Copy link
Contributor

The calculation of the buffer size for a loaded image in the example code currently allocates a Vec<u32> that is exactly 4 times longer than needed.
This is due to png::decoder::Reader::output_buffer_size() returning the size of the buffer in bytes.
These bytes are later packed to u32s, which reduces the needed length of the u32 buffer to png::decoder::Reader::output_buffer_size() / 4

@JanNeuenfeld
Copy link
Contributor Author

Also i went through the clippy suggestions. Please pay extra attention to the changed slice size calculations as I'm extremely tired at the moment and not sure if i overlooked reasons to not use std::mem::size_of_val in certain places.

@emoon
Copy link
Owner

emoon commented Aug 25, 2024

Thanks for the PR! LGTM 👍

@emoon
Copy link
Owner

emoon commented Aug 25, 2024

I will do some testing before merging this

@emoon emoon merged commit 578327d into emoon:master Aug 25, 2024
4 checks passed
@emoon
Copy link
Owner

emoon commented Aug 25, 2024

This change breakes the image test

cargo run --example image

Gives

thread 'main' panicked at examples/image.rs:23:39:
called `Result::unwrap()` on an `Err` value: Parameter(ParameterError { inner: ImageBufferSize { expected: 24, actual: 409600 } })
stack backtrace:
   0: rust_begin_unwind
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/std/src/panicking.rs:652:5
   1: core::panicking::panic_fmt
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/result.rs:1679:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/result.rs:1102:23
   4: image::main
             at ./examples/image.rs:23:5
   5: core::ops::function::FnOnce::call_once
             at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

emoon added a commit that referenced this pull request Aug 25, 2024
emoon added a commit that referenced this pull request Aug 25, 2024
@JanNeuenfeld
Copy link
Contributor Author

Okay that's embarrassing, sorry about that.
As far as I can tell everything still works on commit 3a4b729, so I'm pretty sure I messed up on dbe8eb1 when replacing manual slice size calculations.

@emoon
Copy link
Owner

emoon commented Aug 26, 2024

Sorry, I kinda screwed up when merging this and then reverting it. I wonder if you can open up a new PR with the correct fixes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants