-
Notifications
You must be signed in to change notification settings - Fork 619
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
CI seems to be failing because this exposes the tiff crate's |
I somehow missed this, just pushed changes that hide tiff crate's |
@fintelia can you please take a look at this? |
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 |
@fintelia I hope you feel better soon! |
There was a problem hiding this 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.
impl From<TiffDeflateLevel> for DeflateLevel { | ||
fn from(val: TiffDeflateLevel) -> Self { | ||
match val { | ||
TiffDeflateLevel::Fast => DeflateLevel::Fast, | ||
TiffDeflateLevel::Balanced => DeflateLevel::Balanced, | ||
TiffDeflateLevel::Best => DeflateLevel::Best, | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.
enum DtypeContainer<'a, T> { | ||
Slice(&'a [T]), | ||
Vec(Vec<T>), | ||
} |
There was a problem hiding this comment.
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?
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)), | ||
))) | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.
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.