Debian Codegraph @

tests/predict.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 extern crate tiff;
2
3 use tiff::decoder::{Decoder, DecodingResult};
4 use tiff::encoder::{colortype, Predictor, TiffEncoder};
5 use tiff::ColorType;
6
7 use std::fs::File;
8 use std::io::{Cursor, Seek, SeekFrom};
9 use std::path::PathBuf;
10
11 const TEST_IMAGE_DIR: &str = "./tests/images/";
12
•13 /*
14 macro_rules! test_predict {
15 ($name:ident, $buffer:ident, $buffer_ty:ty) => {
16 fn $name<C: colortype::ColorType<Inner = $buffer_ty>>(
17 file: &str,
18 expected_type: ColorType,
19 ) {
20 let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
21 let file = File::open(path).expect("Cannot find test image!");
22 let mut decoder = Decoder::new(file).expect("Cannot create decoder!");
23
24 assert_eq!(decoder.colortype().unwrap(), expected_type);
25 let image_data = match decoder.read_image().unwrap() {
26 DecodingResult::$buffer(res) => res,
27 _ => panic!("Wrong data type"),
28 };
29
30 let mut predicted = Vec::with_capacity(image_data.len());
31 C::horizontal_predict(&image_data, &mut predicted);
32
33 let sample_size = C::SAMPLE_FORMAT.len();
34
35 (0..sample_size).for_each(|i| {
36 assert_eq!(predicted[i], image_data[i]);
37 });
38
39 (sample_size..image_data.len()).for_each(|i| {
40 predicted[i] = predicted[i].wrapping_add(predicted[i - sample_size]);
41 assert_eq!(predicted[i], image_data[i]);
42 });
43 }
44 };
45 }
46
47 test_predict!(test_u8_predict, U8, u8);
48 test_predict!(test_i8_predict, I8, i8);
49 test_predict!(test_u16_predict, U16, u16);
50 test_predict!(test_i16_predict, I16, i16);
51 test_predict!(test_u32_predict, U32, u32);
52 test_predict!(test_u64_predict, U64, u64);
53
54 #[test]
55 fn test_gray_u8_predict() {
56 test_u8_predict::<colortype::Gray8>("minisblack-1c-8b.tiff", ColorType::Gray(8));
57 }
58
59 #[test]
60 fn test_gray_i8_predict() {
61 test_i8_predict::<colortype::GrayI8>("minisblack-1c-i8b.tiff", ColorType::Gray(8));
62 }
63
64 #[test]
65 fn test_rgb_u8_predict() {
66 test_u8_predict::<colortype::RGB8>("rgb-3c-8b.tiff", ColorType::RGB(8));
67 }
68
69 #[test]
70 fn test_cmyk_u8_predict() {
71 test_u8_predict::<colortype::CMYK8>("cmyk-3c-8b.tiff", ColorType::CMYK(8));
72 }
73
74 #[test]
75 fn test_gray_u16_predict() {
76 test_u16_predict::<colortype::Gray16>("minisblack-1c-16b.tiff", ColorType::Gray(16));
77 }
78
79 #[test]
80 fn test_gray_i16_predict() {
81 test_i16_predict::<colortype::GrayI16>("minisblack-1c-i16b.tiff", ColorType::Gray(16));
82 }
83
84 #[test]
85 fn test_rgb_u16_predict() {
86 test_u16_predict::<colortype::RGB16>("rgb-3c-16b.tiff", ColorType::RGB(16));
87 }
88
89 #[test]
90 fn test_cmyk_u16_predict() {
91 test_u16_predict::<colortype::CMYK16>("cmyk-3c-16b.tiff", ColorType::CMYK(16));
92 }
93
94 #[test]
95 fn test_gray_u32_predict() {
96 test_u32_predict::<colortype::Gray32>("gradient-1c-32b.tiff", ColorType::Gray(32));
97 }
98
99 #[test]
100 fn test_rgb_u32_predict() {
101 test_u32_predict::<colortype::RGB32>("gradient-3c-32b.tiff", ColorType::RGB(32));
102 }
103
104 #[test]
105 fn test_gray_u64_predict() {
106 test_u64_predict::<colortype::Gray64>("gradient-1c-64b.tiff", ColorType::Gray(64));
107 }
108
109 #[test]
110 fn test_rgb_u64_predict() {
111 test_u64_predict::<colortype::RGB64>("gradient-3c-64b.tiff", ColorType::RGB(64));
112 }
113
114 #[test]
115 fn test_ycbcr_u8_predict() {
116 test_u8_predict::<colortype::YCbCr8>("tiled-jpeg-ycbcr.tif", ColorType::YCbCr(8));
117 }
118
119 macro_rules! test_predict_roundtrip {
120 ($name:ident, $buffer:ident, $buffer_ty:ty) => {
121 fn $name<C: colortype::ColorType<Inner = $buffer_ty>>(
122 file: &str,
123 expected_type: ColorType,
124 ) {
125 let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
126 let img_file = File::open(path).expect("Cannot find test image!");
127 let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
128 assert_eq!(decoder.colortype().unwrap(), expected_type);
129
130 let image_data = match decoder.read_image().unwrap() {
131 DecodingResult::$buffer(res) => res,
132 _ => panic!("Wrong data type"),
133 };
134
135 let mut file = Cursor::new(Vec::new());
136 {
137 let mut tiff = TiffEncoder::new(&mut file)
138 .unwrap()
139 .with_predictor(Predictor::Horizontal);
140
141 let (width, height) = decoder.dimensions().unwrap();
142 tiff.write_image::<C>(width, height, &image_data).unwrap();
143 }
144 file.seek(SeekFrom::Start(0)).unwrap();
145 {
146 let mut decoder = Decoder::new(&mut file).unwrap();
147 if let DecodingResult::$buffer(img_res) =
148 decoder.read_image().expect("Decoding image failed")
149 {
150 assert_eq!(image_data, img_res);
151 } else {
152 panic!("Wrong data type");
153 }
154 }
155 }
156 };
157 }
158
159 test_predict_roundtrip!(test_u8_predict_roundtrip, U8, u8);
160 test_predict_roundtrip!(test_i8_predict_roundtrip, I8, i8);
161 test_predict_roundtrip!(test_u16_predict_roundtrip, U16, u16);
162 test_predict_roundtrip!(test_i16_predict_roundtrip, I16, i16);
163 test_predict_roundtrip!(test_u32_predict_roundtrip, U32, u32);
164 test_predict_roundtrip!(test_u64_predict_roundtrip, U64, u64);
165
166 #[test]
167 fn test_gray_u8_predict_roundtrip() {
168 test_u8_predict_roundtrip::<colortype::Gray8>("minisblack-1c-8b.tiff", ColorType::Gray(8));
169 }
170
171 #[test]
172 fn test_gray_i8_predict_roundtrip() {
173 test_i8_predict_roundtrip::<colortype::GrayI8>("minisblack-1c-i8b.tiff", ColorType::Gray(8));
174 }
175
176 #[test]
177 fn test_rgb_u8_predict_roundtrip() {
178 test_u8_predict_roundtrip::<colortype::RGB8>("rgb-3c-8b.tiff", ColorType::RGB(8));
179 }
180
181 #[test]
182 fn test_cmyk_u8_predict_roundtrip() {
183 test_u8_predict_roundtrip::<colortype::CMYK8>("cmyk-3c-8b.tiff", ColorType::CMYK(8));
184 }
185
186 #[test]
187 fn test_gray_u16_predict_roundtrip() {
188 test_u16_predict_roundtrip::<colortype::Gray16>("minisblack-1c-16b.tiff", ColorType::Gray(16));
189 }
190
191 #[test]
192 fn test_gray_i16_predict_roundtrip() {
193 test_i16_predict_roundtrip::<colortype::GrayI16>(
194 "minisblack-1c-i16b.tiff",
195 ColorType::Gray(16),
196 );
197 }
198
199 #[test]
200 fn test_rgb_u16_predict_roundtrip() {
201 test_u16_predict_roundtrip::<colortype::RGB16>("rgb-3c-16b.tiff", ColorType::RGB(16));
202 }
203
204 #[test]
205 fn test_cmyk_u16_predict_roundtrip() {
206 test_u16_predict_roundtrip::<colortype::CMYK16>("cmyk-3c-16b.tiff", ColorType::CMYK(16));
207 }
208
209 #[test]
210 fn test_gray_u32_predict_roundtrip() {
211 test_u32_predict_roundtrip::<colortype::Gray32>("gradient-1c-32b.tiff", ColorType::Gray(32));
212 }
213
214 #[test]
215 fn test_rgb_u32_predict_roundtrip() {
216 test_u32_predict_roundtrip::<colortype::RGB32>("gradient-3c-32b.tiff", ColorType::RGB(32));
217 }
218
219 #[test]
220 fn test_gray_u64_predict_roundtrip() {
221 test_u64_predict_roundtrip::<colortype::Gray64>("gradient-1c-64b.tiff", ColorType::Gray(64));
222 }
223
224 #[test]
225 fn test_rgb_u64_predict_roundtrip() {
226 test_u64_predict_roundtrip::<colortype::RGB64>("gradient-3c-64b.tiff", ColorType::RGB(64));
227 }
228
229 #[test]
230 fn test_ycbcr_u8_predict_roundtrip() {
231 test_u8_predict_roundtrip::<colortype::YCbCr8>("tiled-jpeg-ycbcr.tif", ColorType::YCbCr(8));
232 }
•233 */