1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
extern crate tiff;
use color::ColorType;
use image::{DecodingResult, ImageDecoder, ImageResult, ImageError};
use std::io::{Read, Seek};
pub struct TIFFDecoder<R>
where R: Read + Seek
{
inner: tiff::decoder::Decoder<R>
}
impl<R> TIFFDecoder<R>
where R: Read + Seek
{
pub fn new(r: R) -> Result<TIFFDecoder<R>, ImageError> {
Ok(TIFFDecoder { inner: tiff::decoder::Decoder::new(r)? })
}
}
impl From<tiff::TiffError> for ImageError {
fn from(err: tiff::TiffError) -> ImageError {
match err {
tiff::TiffError::IoError(err) => ImageError::IoError(err),
tiff::TiffError::FormatError(desc) => ImageError::FormatError(desc.to_string()),
tiff::TiffError::UnsupportedError(desc) => ImageError::UnsupportedError(desc.to_string()),
}
}
}
impl From<tiff::ColorType> for ColorType {
fn from(ct: tiff::ColorType) -> ColorType {
match ct {
tiff::ColorType::Gray(depth) => ColorType::Gray(depth),
tiff::ColorType::RGB(depth) => ColorType::RGB(depth),
tiff::ColorType::Palette(depth) => ColorType::Palette(depth),
tiff::ColorType::GrayA(depth) => ColorType::GrayA(depth),
tiff::ColorType::RGBA(depth) => ColorType::RGBA(depth),
tiff::ColorType::CMYK(_) => unimplemented!()
}
}
}
impl From<tiff::decoder::DecodingResult> for DecodingResult {
fn from(res: tiff::decoder::DecodingResult) -> DecodingResult {
match res {
tiff::decoder::DecodingResult::U8(data) => DecodingResult::U8(data),
tiff::decoder::DecodingResult::U16(data) => DecodingResult::U16(data),
}
}
}
impl<R: Read + Seek> ImageDecoder for TIFFDecoder<R> {
fn dimensions(&mut self) -> ImageResult<(u32, u32)> {
self.inner.dimensions().map_err(|e| e.into())
}
fn colortype(&mut self) -> ImageResult<ColorType> {
Ok(self.inner.colortype()?.into())
}
fn row_len(&mut self) -> ImageResult<usize> {
unimplemented!()
}
fn read_scanline(&mut self, _: &mut [u8]) -> ImageResult<u32> {
unimplemented!()
}
fn read_image(&mut self) -> ImageResult<DecodingResult> {
self.inner.read_image().map_err(|e| e.into()).map(|res| res.into())
}
}