Skip to content
Draft
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
22 changes: 20 additions & 2 deletions src/codecs/tiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ where
(tiff::ColorType::Gray(1), Uint) => ColorType::L8,
(tiff::ColorType::Gray(8), Uint) => ColorType::L8,
(tiff::ColorType::Gray(16), Uint) => ColorType::L16,
(tiff::ColorType::Gray(32), IEEEFP) => ColorType::L32F,
(tiff::ColorType::GrayA(8), Uint) => ColorType::La8,
(tiff::ColorType::GrayA(16), Uint) => ColorType::La16,
(tiff::ColorType::RGB(8), Uint) => ColorType::Rgb8,
Expand All @@ -123,9 +122,13 @@ where
(tiff::ColorType::RGBA(16), Uint) => ColorType::Rgba16,
(tiff::ColorType::CMYK(8), Uint) => ColorType::Rgb8,
(tiff::ColorType::CMYK(16), Uint) => ColorType::Rgb16,
(tiff::ColorType::YCbCr(8), Uint) => ColorType::Rgb8,
(tiff::ColorType::Gray(16), IEEEFP) => ColorType::L32F,
(tiff::ColorType::Gray(32), IEEEFP) => ColorType::L32F,
(tiff::ColorType::RGB(16), IEEEFP) => ColorType::Rgb32F,
(tiff::ColorType::RGB(32), IEEEFP) => ColorType::Rgb32F,
(tiff::ColorType::RGBA(16), IEEEFP) => ColorType::Rgba32F,
(tiff::ColorType::RGBA(32), IEEEFP) => ColorType::Rgba32F,
(tiff::ColorType::YCbCr(8), Uint) => ColorType::Rgb8,
_ => {
return Err(ImageError::Unsupported(
UnsupportedError::from_format_and_kind(
Expand All @@ -143,6 +146,9 @@ where
(tiff::ColorType::CMYK(8), Uint) => ExtendedColorType::Cmyk8,
(tiff::ColorType::CMYK(16), Uint) => ExtendedColorType::Cmyk16,
(tiff::ColorType::YCbCr(8), Uint) => ExtendedColorType::YCbCr8,
(tiff::ColorType::Gray(16), IEEEFP) => ExtendedColorType::L16F,
(tiff::ColorType::RGB(16), IEEEFP) => ExtendedColorType::Rgb16F,
(tiff::ColorType::RGBA(16), IEEEFP) => ExtendedColorType::Rgba16F,
_ => color_type.into(),
};

Expand Down Expand Up @@ -518,6 +524,18 @@ impl<R: BufRead + Seek> ImageDecoder for TiffDecoder<R> {

ycbcr_to_rgb8(ycbcr, lr, lg, lb, out);
}
DecodingResult::F16(v)
if matches!(
info.original_color_type,
ExtendedColorType::L16F
| ExtendedColorType::Rgb16F
| ExtendedColorType::Rgba16F
) =>
{
for (half, out) in v.iter().zip(buf.as_chunks_mut::<4>().0.iter_mut()) {
*out = half.to_f32().to_ne_bytes();
}
Comment on lines +535 to +537

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe a bit more intuitive:

Suggested change
for (half, out) in v.iter().zip(buf.as_chunks_mut::<4>().0.iter_mut()) {
*out = half.to_f32().to_ne_bytes();
}
for (out, f) in bytemuck::cast_slice_mut::<u8, f32>(buf).iter_mut().zip(v) {
*out = f.to_f32();
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is wrong, unfortunately. A slice of f32 must have an alignment requirement of 4, but the slice of u8 we have doesn't guarantee that. So bytemuck::cast_slice_mut will panic if the slice doesn't happen to be aligned to a multiple of 4. Users are allowed to give us slices of any alignment, so we have to be careful.

}
DecodingResult::U8(v) => {
buf.copy_from_slice(v);
}
Expand Down
19 changes: 18 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,16 @@ pub enum ExtendedColorType {
/// Pixel is 8-bit BGR with an alpha channel
Bgra8,

// TODO f16 types?
// TODO: Should the new f16 types come after Ycbcr8 for serde backwards compatibility?
/// Pixel is 16-bit float luminance
L16F,
/// Pixel is 16-bit float luminance with an alpha channel
La16F,
/// Pixel is 16-bit float RGB
Rgb16F,
/// Pixel is 16-bit float RGBA
Rgba16F,

/// Pixel is 32-bit float luminance
L32F,
/// Pixel is 32-bit float luminance with an alpha channel
Expand Down Expand Up @@ -191,20 +200,23 @@ impl ExtendedColorType {
| ExtendedColorType::L4
| ExtendedColorType::L8
| ExtendedColorType::L16
| ExtendedColorType::L16F
| ExtendedColorType::L32F
| ExtendedColorType::Unknown(_) => 1,
ExtendedColorType::La1
| ExtendedColorType::La2
| ExtendedColorType::La4
| ExtendedColorType::La8
| ExtendedColorType::La16
| ExtendedColorType::La16F
| ExtendedColorType::La32F => 2,
ExtendedColorType::Rgb1
| ExtendedColorType::Rgb2
| ExtendedColorType::Rgb4
| ExtendedColorType::Rgb5x1
| ExtendedColorType::Rgb8
| ExtendedColorType::Rgb16
| ExtendedColorType::Rgb16F
| ExtendedColorType::Rgb32F
| ExtendedColorType::YCbCr8
| ExtendedColorType::Bgr8 => 3,
Expand All @@ -213,6 +225,7 @@ impl ExtendedColorType {
| ExtendedColorType::Rgba4
| ExtendedColorType::Rgba8
| ExtendedColorType::Rgba16
| ExtendedColorType::Rgba16F
| ExtendedColorType::Rgba32F
| ExtendedColorType::Bgra8
| ExtendedColorType::Cmyk8
Expand Down Expand Up @@ -246,6 +259,10 @@ impl ExtendedColorType {
ExtendedColorType::La16 => 32,
ExtendedColorType::Rgb16 => 48,
ExtendedColorType::Rgba16 => 64,
ExtendedColorType::L16F => 16,
ExtendedColorType::La16F => 32,
ExtendedColorType::Rgb16F => 48,
ExtendedColorType::Rgba16F => 64,
ExtendedColorType::L32F => 32,
ExtendedColorType::La32F => 64,
ExtendedColorType::Rgb32F => 96,
Expand Down
Binary file added tests/images/tiff/testsuite/random-fp16.tiff
Binary file not shown.
Binary file added tests/reference/tiff/testsuite/random-fp16.tiff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading