diff --git a/Cargo.toml b/Cargo.toml index bd30db578b..4a31e9b814 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ gif = { version = "0.10.0", optional = true } jpeg = { package = "jpeg-decoder", version = "0.1.17", default-features = false, optional = true } png = { version = "0.16.0", optional = true } scoped_threadpool = { version = "0.1", optional = true } -tiff = { version = "0.4.0", optional = true } +tiff = { version = "0.5.0", optional = true } [dev-dependencies] crc32fast = "1.2.0" diff --git a/Cargo.toml.public-private-dependencies b/Cargo.toml.public-private-dependencies index 281b0281ef..6f8ce64e3b 100644 --- a/Cargo.toml.public-private-dependencies +++ b/Cargo.toml.public-private-dependencies @@ -34,7 +34,7 @@ gif = { version = "0.10.0", optional = true } jpeg = { package = "jpeg-decoder", version = "0.1.17", default-features = false, optional = true } png = { version = "0.16.0", optional = true } scoped_threadpool = { version = "0.1", optional = true } -tiff = { version = "0.4.0", optional = true } +tiff = { version = "0.5.0", optional = true } [dev-dependencies] crc32fast = "1.2.0" diff --git a/src/tiff.rs b/src/tiff.rs index 91425babf3..1a28211c9a 100644 --- a/src/tiff.rs +++ b/src/tiff.rs @@ -13,15 +13,13 @@ use std::io::{self, Cursor, Read, Write, Seek}; use std::marker::PhantomData; use std::mem; -use byteorder::{NativeEndian, ByteOrder}; - use crate::color::{ColorType, ExtendedColorType}; use crate::error::{ DecodingError, EncodingError, ImageError, ImageResult, LimitError, LimitErrorKind, ParameterError, ParameterErrorKind, UnsupportedError, UnsupportedErrorKind, }; use crate::image::{ImageDecoder, ImageEncoder, ImageFormat}; -use crate::utils::vec_u16_into_u8; +use crate::utils; /// Decoder for TIFF images. pub struct TiffDecoder @@ -78,7 +76,7 @@ impl ImageError { fn from_tiff_decode(err: tiff::TiffError) -> ImageError { match err { tiff::TiffError::IoError(err) => ImageError::IoError(err), - err @ tiff::TiffError::FormatError(_) => { + err @ tiff::TiffError::FormatError(_) | err @ tiff::TiffError::IntSizeError => { ImageError::Decoding(DecodingError::new(ImageFormat::Tiff.into(), err)) } tiff::TiffError::UnsupportedError(desc) => { @@ -96,7 +94,7 @@ impl ImageError { fn from_tiff_encode(err: tiff::TiffError) -> ImageError { match err { tiff::TiffError::IoError(err) => ImageError::IoError(err), - err @ tiff::TiffError::FormatError(_) => { + err @ tiff::TiffError::FormatError(_) | err @ tiff::TiffError::IntSizeError => { ImageError::Encoding(EncodingError::new(ImageFormat::Tiff.into(), err)) } tiff::TiffError::UnsupportedError(desc) => { @@ -146,7 +144,9 @@ impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TiffDecoder { .map_err(ImageError::from_tiff_decode)? { tiff::decoder::DecodingResult::U8(v) => v, - tiff::decoder::DecodingResult::U16(v) => vec_u16_into_u8(v), + tiff::decoder::DecodingResult::U16(v) => utils::vec_u16_into_u8(v), + tiff::decoder::DecodingResult::U32(v) => utils::vec_u32_into_u8(v), + tiff::decoder::DecodingResult::U64(v) => utils::vec_u64_into_u8(v), }; Ok(TiffReader(Cursor::new(buf), PhantomData)) @@ -163,7 +163,13 @@ impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TiffDecoder { buf.copy_from_slice(&v); } tiff::decoder::DecodingResult::U16(v) => { - NativeEndian::write_u16_into(&v, buf); + buf.copy_from_slice(bytemuck::cast_slice(&v)); + } + tiff::decoder::DecodingResult::U32(v) => { + buf.copy_from_slice(bytemuck::cast_slice(&v)); + } + tiff::decoder::DecodingResult::U64(v) => { + buf.copy_from_slice(bytemuck::cast_slice(&v)); } } Ok(()) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 4b4fda670b..561115da35 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,8 +1,6 @@ //! Utilities -use byteorder::{NativeEndian, ByteOrder}; use num_iter::range_step; -use std::mem; use std::iter::repeat; #[inline(always)] @@ -69,15 +67,30 @@ pub(crate) fn expand_bits(bit_depth: u8, row_size: u32, buf: &[u8]) -> Vec { // When no image formats that use it are enabled pub(crate) fn vec_u16_into_u8(vec: Vec) -> Vec { // Do this way until we find a way to not alloc/dealloc but get llvm to realloc instead. - vec_u16_copy_u8(&vec) + vec_copy_to_u8(&vec) } #[allow(dead_code)] // When no image formats that use it are enabled -pub(crate) fn vec_u16_copy_u8(vec: &[u16]) -> Vec { - let mut new = vec![0; vec.len() * mem::size_of::()]; - NativeEndian::write_u16_into(&vec[..], &mut new[..]); - new +pub(crate) fn vec_u32_into_u8(vec: Vec) -> Vec { + // Do this way until we find a way to not alloc/dealloc but get llvm to realloc instead. + vec_copy_to_u8(&vec) +} + +#[allow(dead_code)] +// When no image formats that use it are enabled +pub(crate) fn vec_u64_into_u8(vec: Vec) -> Vec { + // Do this way until we find a way to not alloc/dealloc but get llvm to realloc instead. + vec_copy_to_u8(&vec) +} + +#[allow(dead_code)] +// When no image formats that use it are enabled +pub(crate) fn vec_copy_to_u8(vec: &[T]) -> Vec +where + T: bytemuck::Pod, +{ + bytemuck::cast_slice(vec).to_owned() }