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

Update tiff to v0.5 #1256

Merged
merged 1 commit into from Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml.public-private-dependencies
Expand Up @@ -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"
Expand Down
20 changes: 13 additions & 7 deletions src/tiff.rs
Expand Up @@ -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<R>
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand Down Expand Up @@ -146,7 +144,9 @@ impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TiffDecoder<R> {
.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))
Expand All @@ -163,7 +163,13 @@ impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TiffDecoder<R> {
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(())
Expand Down
27 changes: 20 additions & 7 deletions 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)]
Expand Down Expand Up @@ -69,15 +67,30 @@ pub(crate) fn expand_bits(bit_depth: u8, row_size: u32, buf: &[u8]) -> Vec<u8> {
// When no image formats that use it are enabled
pub(crate) fn vec_u16_into_u8(vec: Vec<u16>) -> Vec<u8> {
// 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<u8> {
let mut new = vec![0; vec.len() * mem::size_of::<u16>()];
NativeEndian::write_u16_into(&vec[..], &mut new[..]);
new
pub(crate) fn vec_u32_into_u8(vec: Vec<u32>) -> Vec<u8> {
// 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<u64>) -> Vec<u8> {
// 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<T>(vec: &[T]) -> Vec<u8>
where
T: bytemuck::Pod,
{
bytemuck::cast_slice(vec).to_owned()
}


Expand Down