-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from SalOne22/feature/tiff
TIFF Support
- Loading branch information
Showing
8 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::io::{Read, Seek}; | ||
|
||
use zune_core::colorspace::ColorSpace; | ||
use zune_image::{errors::ImageErrors, image::Image, traits::DecoderTrait}; | ||
|
||
/// A Tiff decoder | ||
pub struct TiffDecoder<R: Read + Seek> { | ||
inner: tiff::decoder::Decoder<R>, | ||
dimensions: Option<(usize, usize)>, | ||
colorspace: ColorSpace, | ||
} | ||
|
||
impl<R: Read + Seek> TiffDecoder<R> { | ||
/// Create a new tiff decoder that reads data from `source` | ||
pub fn try_new(source: R) -> Result<Self, ImageErrors> { | ||
let inner = tiff::decoder::Decoder::new(source).map_err(|e| { | ||
ImageErrors::ImageDecodeErrors(format!("Unable to create TIFF decoder: {}", e)) | ||
})?; | ||
|
||
Ok(Self { | ||
inner, | ||
dimensions: None, | ||
colorspace: ColorSpace::Unknown, | ||
}) | ||
} | ||
} | ||
|
||
impl<R> DecoderTrait for TiffDecoder<R> | ||
where | ||
R: Read + Seek, | ||
{ | ||
fn decode(&mut self) -> Result<Image, ImageErrors> { | ||
let (width, height) = self.inner.dimensions().map_err(|e| { | ||
ImageErrors::ImageDecodeErrors(format!("Unable to read dimensions - {}", e)) | ||
})?; | ||
|
||
let (width, height) = (width as usize, height as usize); | ||
self.dimensions = Some((width, height)); | ||
|
||
let colorspace = self | ||
.inner | ||
.colortype() | ||
.map(|colortype| match colortype { | ||
tiff::ColorType::RGB(_) => ColorSpace::RGB, | ||
tiff::ColorType::RGBA(_) => ColorSpace::RGBA, | ||
tiff::ColorType::CMYK(_) => ColorSpace::CMYK, | ||
tiff::ColorType::Gray(_) => ColorSpace::Luma, | ||
tiff::ColorType::GrayA(_) => ColorSpace::LumaA, | ||
tiff::ColorType::YCbCr(_) => ColorSpace::YCbCr, | ||
_ => ColorSpace::Unknown, | ||
}) | ||
.map_err(|e| { | ||
ImageErrors::ImageDecodeErrors(format!("Unable to read colorspace - {}", e)) | ||
})?; | ||
|
||
self.colorspace = colorspace; | ||
|
||
let result = self.inner.read_image().map_err(|e| { | ||
ImageErrors::ImageDecodeErrors(format!("Unable to decode TIFF file - {}", e)) | ||
})?; | ||
|
||
match result { | ||
tiff::decoder::DecodingResult::U8(data) => { | ||
Ok(Image::from_u8(&data, width, height, colorspace)) | ||
} | ||
tiff::decoder::DecodingResult::U16(data) => { | ||
Ok(Image::from_u16(&data, width, height, colorspace)) | ||
} | ||
tiff::decoder::DecodingResult::F32(data) => { | ||
Ok(Image::from_f32(&data, width, height, colorspace)) | ||
} | ||
_ => Err(ImageErrors::ImageDecodeErrors( | ||
"Tiff Data format not supported".to_string(), | ||
)), | ||
} | ||
} | ||
|
||
fn dimensions(&self) -> Option<(usize, usize)> { | ||
self.dimensions | ||
} | ||
|
||
fn out_colorspace(&self) -> ColorSpace { | ||
self.colorspace | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"tiff-decoder" | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use std::fs::File; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn decode() { | ||
let file_content = File::open("tests/files/tiff/f1t.tif").unwrap(); | ||
|
||
let decoder = TiffDecoder::try_new(file_content).unwrap(); | ||
|
||
let img = Image::from_decoder(decoder).unwrap(); | ||
|
||
assert_eq!(img.dimensions(), (48, 80)); | ||
assert_eq!(img.colorspace(), ColorSpace::RGB); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod decoder; | ||
|
||
pub use decoder::*; |
Binary file not shown.