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

Added compression and f32 support for TIFF encoding/decoding. #2251

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sunipkm
Copy link

@sunipkm sunipkm commented Jun 2, 2024

I license past and future contributions under the dual MIT/Apache-2.0 license,
allowing licensees to choose either at their option.

f32 encoding/decoding support is added without additional byte alignment requirements. Removed 2-byte alignment requirement for u16 encoding/decoding. Added TIFF compression support.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a pretty big image to check into the repository. Could you crop it / make a smaller one?

Copy link
Author

Choose a reason for hiding this comment

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

Do you want me to break it into F32 stuff and compression?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, just make it like a 10x10 image rather than a 400x400 one

@fintelia
Copy link
Contributor

fintelia commented Jun 2, 2024

CI seems to be failing because this exposes the tiff crate's Compressor type

@sunipkm
Copy link
Author

sunipkm commented Aug 10, 2024

I somehow missed this, just pushed changes that hide tiff crate's Compressor type.

@sunipkm
Copy link
Author

sunipkm commented Sep 10, 2024

@fintelia can you please take a look at this?

@fintelia
Copy link
Contributor

fintelia commented Sep 12, 2024

I've been taking a hiatus from most PR review and issue triage because I got burned out following the 0.25 release. Another reviewer might have a chance to review this at some point

@sunipkm
Copy link
Author

sunipkm commented Sep 13, 2024

@fintelia I hope you feel better soon!

Copy link
Member

@HeroicKatora HeroicKatora left a comment

Choose a reason for hiding this comment

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

The bindings themselves look quite alright, I think the lower-level code could be cleaned up quite a lot with help of bytemuck and the standard library. Quite boilerplatey.

Comment on lines +351 to +359
impl From<TiffDeflateLevel> for DeflateLevel {
fn from(val: TiffDeflateLevel) -> Self {
match val {
TiffDeflateLevel::Fast => DeflateLevel::Fast,
TiffDeflateLevel::Balanced => DeflateLevel::Balanced,
TiffDeflateLevel::Best => DeflateLevel::Best,
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This introduces a public dependency on the tiff major version. A standard method would be sufficient and does not have that effect.

Comment on lines +384 to +387
enum DtypeContainer<'a, T> {
Slice(&'a [T]),
Vec(Vec<T>),
}
Copy link
Member

Choose a reason for hiding this comment

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

This looks much like std::borrow::Cow. What's the difference for not using that standard type?

Comment on lines +398 to +422
fn u8_slice_as_f32(buf: &[u8]) -> ImageResult<DtypeContainer<f32>> {
let res = bytemuck::try_cast_slice(buf);
match res {
Ok(slc) => Ok(DtypeContainer::<f32>::Slice(slc)),
Err(err) => {
match err {
bytemuck::PodCastError::TargetAlignmentGreaterAndInputNotAligned => {
// If the buffer is not aligned for a f32 slice, copy the buffer into a new Vec<f32>
let mut vec = vec![0.0; buf.len() / 4];
for (i, chunk) in buf.chunks_exact(4).enumerate() {
let f32_val = f32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
vec[i] = f32_val;
}
Ok(DtypeContainer::Vec(vec))
}
_ => {
// If the buffer is not the correct length for a f32 slice, err.
Err(ImageError::Parameter(ParameterError::from_kind(
ParameterErrorKind::Generic(format!("{:?}", err)),
)))
}
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised this is not a generic method bounded by Pod. In particular some of the verbosity around from_ne_bytes is simulating an unaligned read, a method bytemuck provides for the relevant sample types.

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.

3 participants