1 extern crate tiff;
2 
3 use tiff::decoder::{Decoder, DecodingResult};
4 use tiff::encoder::{colortype, TiffEncoder, SRational};
5 use tiff::ColorType;
6 use tiff::tags::Tag;
7 
8 use std::fs::File;
9 use std::io::{Cursor, Seek, SeekFrom};
10 use std::path::PathBuf;
11 
12 #[test]
encode_decode()13 fn encode_decode() {
14     let mut image_data = Vec::new();
15     for x in 0..100 {
16         for y in 0..100u8 {
17             let val = x + y;
18             image_data.push(val);
19             image_data.push(val);
20             image_data.push(val);
21         }
22     }
23     let mut file = Cursor::new(Vec::new());
24     {
25         let mut tiff = TiffEncoder::new(&mut file).unwrap();
26 
27         tiff.write_image::<colortype::RGB8>(100, 100, &image_data)
28             .unwrap();
29     }
30     {
31         file.seek(SeekFrom::Start(0)).unwrap();
32         let mut decoder = Decoder::new(&mut file).unwrap();
33         assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
34         assert_eq!(decoder.dimensions().unwrap(), (100, 100));
35         if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() {
36             assert_eq!(image_data, img_res);
37         } else {
38             panic!("Wrong data type");
39         }
40     }
41 }
42 
43 #[test]
44 /// Test that attempting to encode when the input buffer is undersized returns
45 /// an error rather than panicking.
46 /// See: https://github.com/PistonDevelopers/image-tiff/issues/35
test_encode_undersized_buffer()47 fn test_encode_undersized_buffer() {
48     let input_data = vec![1, 2, 3];
49     let output = Vec::new();
50     let mut output_stream = Cursor::new(output);
51     if let Ok(mut tiff) = TiffEncoder::new(&mut output_stream) {
52         let res = tiff.write_image::<colortype::RGB8>(50, 50, &input_data);
53         assert!(res.is_err());
54     }
55 }
56 
57 const TEST_IMAGE_DIR: &str = "./tests/images/";
58 
59 macro_rules! test_roundtrip {
60     ($name:ident, $buffer:ident, $buffer_ty:ty) => {
61         fn $name<C: colortype::ColorType<Inner=$buffer_ty>>(file: &str, expected_type: ColorType) {
62             let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
63             let img_file = File::open(path).expect("Cannot find test image!");
64             let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
65             assert_eq!(decoder.colortype().unwrap(), expected_type);
66 
67             let image_data = match decoder.read_image().unwrap() {
68                 DecodingResult::$buffer(res) => res,
69                 _ => panic!("Wrong data type"),
70             };
71 
72             let mut file = Cursor::new(Vec::new());
73             {
74                 let mut tiff = TiffEncoder::new(&mut file).unwrap();
75 
76                 let (width, height) = decoder.dimensions().unwrap();
77                 tiff.write_image::<C>(width, height, &image_data)
78                     .unwrap();
79             }
80             file.seek(SeekFrom::Start(0)).unwrap();
81             {
82                 let mut decoder = Decoder::new(&mut file).unwrap();
83                 if let DecodingResult::$buffer(img_res) = decoder.read_image().unwrap() {
84                     assert_eq!(image_data, img_res);
85                 } else {
86                     panic!("Wrong data type");
87                 }
88             }
89         }
90     };
91 }
92 
93 test_roundtrip!(test_u8_roundtrip, U8, u8);
94 test_roundtrip!(test_u16_roundtrip, U16, u16);
95 test_roundtrip!(test_u32_roundtrip, U32, u32);
96 test_roundtrip!(test_u64_roundtrip, U64, u64);
97 
98 #[test]
test_gray_u8_roundtrip()99 fn test_gray_u8_roundtrip() {
100     test_u8_roundtrip::<colortype::Gray8>("minisblack-1c-8b.tiff", ColorType::Gray(8));
101 }
102 
103 #[test]
test_rgb_u8_roundtrip()104 fn test_rgb_u8_roundtrip() {
105     test_u8_roundtrip::<colortype::RGB8>("rgb-3c-8b.tiff", ColorType::RGB(8));
106 }
107 
108 #[test]
test_cmyk_u8_roundtrip()109 fn test_cmyk_u8_roundtrip() {
110     test_u8_roundtrip::<colortype::CMYK8>("cmyk-3c-8b.tiff", ColorType::CMYK(8));
111 }
112 
113 #[test]
test_gray_u16_roundtrip()114 fn test_gray_u16_roundtrip() {
115     test_u16_roundtrip::<colortype::Gray16>("minisblack-1c-16b.tiff", ColorType::Gray(16));
116 }
117 
118 #[test]
test_rgb_u16_roundtrip()119 fn test_rgb_u16_roundtrip() {
120     test_u16_roundtrip::<colortype::RGB16>("rgb-3c-16b.tiff", ColorType::RGB(16));
121 }
122 
123 #[test]
test_cmyk_u16_roundtrip()124 fn test_cmyk_u16_roundtrip() {
125     test_u16_roundtrip::<colortype::CMYK16>("cmyk-3c-16b.tiff", ColorType::CMYK(16));
126 }
127 
128 #[test]
test_gray_u32_roundtrip()129 fn test_gray_u32_roundtrip() {
130     test_u32_roundtrip::<colortype::Gray32>("gradient-1c-32b.tiff", ColorType::Gray(32));
131 }
132 
133 #[test]
test_rgb_u32_roundtrip()134 fn test_rgb_u32_roundtrip() {
135     test_u32_roundtrip::<colortype::RGB32>("gradient-3c-32b.tiff", ColorType::RGB(32));
136 }
137 
138 #[test]
test_gray_u64_roundtrip()139 fn test_gray_u64_roundtrip() {
140     test_u64_roundtrip::<colortype::Gray64>("gradient-1c-64b.tiff", ColorType::Gray(64));
141 }
142 
143 #[test]
test_rgb_u64_roundtrip()144 fn test_rgb_u64_roundtrip() {
145     test_u64_roundtrip::<colortype::RGB64>("gradient-3c-64b.tiff", ColorType::RGB(64));
146 }
147 
148 #[test]
test_multiple_byte()149 fn test_multiple_byte() {
150     let mut data = Cursor::new(Vec::new());
151 
152     {
153         let mut tiff = TiffEncoder::new(&mut data).unwrap();
154         let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap();
155         let encoder = image_encoder.encoder();
156 
157         encoder.write_tag(Tag::Unknown(65000), &[1_u8][..]).unwrap();
158         encoder.write_tag(Tag::Unknown(65001), &[1_u8, 2][..]).unwrap();
159         encoder.write_tag(Tag::Unknown(65002), &[1_u8, 2, 3][..]).unwrap();
160         encoder.write_tag(Tag::Unknown(65003), &[1_u8, 2, 3, 4][..]).unwrap();
161         encoder.write_tag(Tag::Unknown(65004), &[1_u8, 2, 3, 4, 5][..]).unwrap();
162     }
163 
164     data.set_position(0);
165     {
166         let mut decoder = Decoder::new(&mut data).unwrap();
167 
168         assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_u32_vec().unwrap(), [1]);
169         assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_u32_vec().unwrap(), [1, 2]);
170         assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_u32_vec().unwrap(), [1, 2, 3]);
171         assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_u32_vec().unwrap(), [1, 2, 3, 4]);
172         assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_u32_vec().unwrap(), [1, 2, 3, 4, 5]);
173     }
174 }
175 
176 #[test]
177 /// Test writing signed tags from TIFF 6.0
test_signed()178 fn test_signed() {
179     let mut data = Cursor::new(Vec::new());
180 
181     {
182         let mut tiff = TiffEncoder::new(&mut data).unwrap();
183         let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap();
184         let encoder = image_encoder.encoder();
185 
186         //Use the "reusable" tags section as per the TIFF6 spec
187         encoder.write_tag(Tag::Unknown(65000), -1_i8).unwrap();
188         encoder.write_tag(Tag::Unknown(65001), &[-1_i8][..]).unwrap();
189         encoder.write_tag(Tag::Unknown(65002), &[-1_i8, 2][..]).unwrap();
190         encoder.write_tag(Tag::Unknown(65003), &[-1_i8, 2, -3][..]).unwrap();
191         encoder.write_tag(Tag::Unknown(65004), &[-1_i8, 2, -3, 4][..]).unwrap();
192         encoder.write_tag(Tag::Unknown(65005), &[-1_i8, 2, -3, 4, -5][..]).unwrap();
193 
194         encoder.write_tag(Tag::Unknown(65010), -1_i16).unwrap();
195         encoder.write_tag(Tag::Unknown(65011), -1_i16).unwrap();
196         encoder.write_tag(Tag::Unknown(65012), &[-1_i16, 2][..]).unwrap();
197         encoder.write_tag(Tag::Unknown(65013), &[-1_i16, 2, -3][..]).unwrap();
198 
199         encoder.write_tag(Tag::Unknown(65020), -1_i32).unwrap();
200         encoder.write_tag(Tag::Unknown(65021), &[-1_i32][..]).unwrap();
201         encoder.write_tag(Tag::Unknown(65022), &[-1_i32, 2][..]).unwrap();
202 
203         encoder.write_tag(Tag::Unknown(65030), SRational { n: -1, d: 100 }).unwrap();
204         encoder.write_tag(Tag::Unknown(65031), &[SRational { n: -1, d: 100 }, SRational { n: 2, d: 100 }][..]).unwrap();
205     }
206 
207     //Rewind the cursor for reading
208     data.set_position(0);
209     {
210         let mut decoder = Decoder::new(&mut data).unwrap();
211 
212         assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_i32().unwrap(), -1, );
213         assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_i32_vec().unwrap(), [-1]);
214         assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_i32_vec().unwrap(), [-1, 2]);
215         assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3]);
216         assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3, 4]);
217         assert_eq!(decoder.get_tag(Tag::Unknown(65005)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3, 4, -5], );
218 
219         assert_eq!(decoder.get_tag(Tag::Unknown(65010)).unwrap().into_i32().unwrap(), -1);
220         assert_eq!(decoder.get_tag(Tag::Unknown(65011)).unwrap().into_i32_vec().unwrap(), [-1]);
221         assert_eq!(decoder.get_tag(Tag::Unknown(65012)).unwrap().into_i32_vec().unwrap(), [-1, 2]);
222         assert_eq!(decoder.get_tag(Tag::Unknown(65013)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3]);
223 
224         assert_eq!(decoder.get_tag(Tag::Unknown(65020)).unwrap().into_i32().unwrap(), -1);
225         assert_eq!(decoder.get_tag(Tag::Unknown(65021)).unwrap().into_i32_vec().unwrap(), [-1]);
226         assert_eq!(decoder.get_tag(Tag::Unknown(65022)).unwrap().into_i32_vec().unwrap(), [-1, 2]);
227 
228         assert_eq!(decoder.get_tag(Tag::Unknown(65030)).unwrap().into_i32_vec().unwrap(), [-1, 100]);
229         assert_eq!(decoder.get_tag(Tag::Unknown(65031)).unwrap().into_i32_vec().unwrap(), [-1_i32, 100, 2, 100]);
230     }
231 }
232 
233 #[test]
234 /// check multipage image handling
test_multipage_image()235 fn test_multipage_image() {
236     let mut img_file = Cursor::new(Vec::new());
237 
238     {
239         // first create a multipage image with 2 images
240         let mut img_encoder = TiffEncoder::new(&mut img_file).unwrap();
241 
242         // write first grayscale image (2x2 16-bit)
243         let img1: Vec<u16> = [1, 2, 3, 4].to_vec();
244         img_encoder.write_image::<colortype::Gray16>(2, 2, &img1[..]).unwrap();
245         // write second grayscale image (3x3 8-bit)
246         let img2: Vec<u8> = [9, 8, 7, 6, 5, 4, 3, 2, 1].to_vec();
247         img_encoder.write_image::<colortype::Gray8>(3, 3, &img2[..]).unwrap();
248     }
249 
250     // seek to the beginning of the file, so that it can be decoded
251     img_file.seek(SeekFrom::Start(0)).unwrap();
252 
253     {
254         let mut img_decoder = Decoder::new(&mut img_file).unwrap();
255 
256         // check the dimensions of the image in the first page
257         assert_eq!(img_decoder.dimensions().unwrap(), (2, 2));
258         img_decoder.next_image().unwrap();
259         // check the dimensions of the image in the second page
260         assert_eq!(img_decoder.dimensions().unwrap(), (3, 3));
261     }
262 }
263