Skip to content

Commit

Permalink
Merge pull request #1256 from HeroicKatora/update-tiff-0.5
Browse files Browse the repository at this point in the history
Update tiff to v0.5
  • Loading branch information
HeroicKatora committed Jun 10, 2020
2 parents 1eac5ba + 2cb2a22 commit 78d65d1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
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

0 comments on commit 78d65d1

Please sign in to comment.