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