| 1 |
extern crate tiff; |
| 2 |
|
| 3 |
use tiff::decoder::{ifd, Decoder, DecodingResult}; |
| 4 |
use tiff::encoder::{colortype, Ifd, Ifd8, SRational, TiffEncoder}; |
| 5 |
use tiff::tags::Tag; |
| 6 |
use tiff::ColorType; |
| 7 |
|
| 8 |
use std::fs::File; |
| 9 |
use std::io::{Cursor, Seek, SeekFrom}; |
| 10 |
use std::path::PathBuf; |
| 11 |
|
| 12 |
#[test] |
| 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 |
let mut image = tiff.new_image::<colortype::RGB8>(100, 100).unwrap(); |
| 28 |
image |
| 29 |
.encoder() |
| 30 |
.write_tag(Tag::Artist, "Image-tiff") |
| 31 |
.unwrap(); |
| 32 |
image.write_data(&image_data).unwrap(); |
| 33 |
} |
| 34 |
{ |
| 35 |
file.seek(SeekFrom::Start(0)).unwrap(); |
| 36 |
let mut decoder = Decoder::new(&mut file).unwrap(); |
| 37 |
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8)); |
| 38 |
assert_eq!(decoder.dimensions().unwrap(), (100, 100)); |
| 39 |
|
| 40 |
let mut all_tags = decoder |
| 41 |
.tag_iter() |
| 42 |
.filter_map(Result::ok) |
| 43 |
.collect::<Vec<_>>(); |
| 44 |
all_tags.sort_by_key(|(t, _)| t.to_u16()); |
| 45 |
assert_eq!( |
| 46 |
all_tags, |
| 47 |
vec![ |
| 48 |
(Tag::ImageWidth, ifd::Value::Unsigned(100)), |
| 49 |
(Tag::ImageLength, ifd::Value::Unsigned(100)), |
| 50 |
( |
| 51 |
Tag::BitsPerSample, |
| 52 |
ifd::Value::List(vec![ |
| 53 |
ifd::Value::Short(8), |
| 54 |
ifd::Value::Short(8), |
| 55 |
ifd::Value::Short(8) |
| 56 |
]) |
| 57 |
), |
| 58 |
(Tag::Compression, ifd::Value::Short(1)), |
| 59 |
(Tag::PhotometricInterpretation, ifd::Value::Short(2)), |
| 60 |
(Tag::StripOffsets, ifd::Value::Unsigned(47)), |
| 61 |
(Tag::SamplesPerPixel, ifd::Value::Short(3)), |
| 62 |
(Tag::RowsPerStrip, ifd::Value::Unsigned(3334)), |
| 63 |
(Tag::StripByteCounts, ifd::Value::Unsigned(30000)), |
| 64 |
(Tag::XResolution, ifd::Value::Rational(1, 1)), |
| 65 |
(Tag::YResolution, ifd::Value::Rational(1, 1)), |
| 66 |
(Tag::ResolutionUnit, ifd::Value::Short(1)), |
| 67 |
(Tag::Artist, ifd::Value::Ascii("Image-tiff".into())), |
| 68 |
(Tag::Predictor, ifd::Value::Short(1)), |
| 69 |
( |
| 70 |
Tag::SampleFormat, |
| 71 |
ifd::Value::List(vec![ |
| 72 |
ifd::Value::Short(1), |
| 73 |
ifd::Value::Short(1), |
| 74 |
ifd::Value::Short(1) |
| 75 |
]) |
| 76 |
), |
| 77 |
] |
| 78 |
); |
| 79 |
|
| 80 |
assert_eq!( |
| 81 |
decoder.get_tag(Tag::Artist).unwrap(), |
| 82 |
ifd::Value::Ascii("Image-tiff".into()) |
| 83 |
); |
| 84 |
if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() { |
| 85 |
assert_eq!(image_data, img_res); |
| 86 |
} else { |
| 87 |
panic!("Wrong data type"); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn encode_decode_big() { |
| 94 |
let mut image_data = Vec::new(); |
| 95 |
for x in 0..100 { |
| 96 |
for y in 0..100u8 { |
| 97 |
let val = x + y; |
| 98 |
image_data.push(val); |
| 99 |
image_data.push(val); |
| 100 |
image_data.push(val); |
| 101 |
} |
| 102 |
} |
| 103 |
let mut file = Cursor::new(Vec::new()); |
| 104 |
{ |
| 105 |
let mut tiff = TiffEncoder::new_big(&mut file).unwrap(); |
| 106 |
|
| 107 |
let mut image = tiff.new_image::<colortype::RGB8>(100, 100).unwrap(); |
| 108 |
image |
| 109 |
.encoder() |
| 110 |
.write_tag(Tag::Artist, "Image-tiff") |
| 111 |
.unwrap(); |
| 112 |
image.write_data(&image_data).unwrap(); |
| 113 |
} |
| 114 |
{ |
| 115 |
file.seek(SeekFrom::Start(0)).unwrap(); |
| 116 |
let mut decoder = Decoder::new(&mut file).unwrap(); |
| 117 |
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8)); |
| 118 |
assert_eq!(decoder.dimensions().unwrap(), (100, 100)); |
| 119 |
|
| 120 |
let mut all_tags = decoder |
| 121 |
.tag_iter() |
| 122 |
.filter_map(Result::ok) |
| 123 |
.collect::<Vec<_>>(); |
| 124 |
all_tags.sort_by_key(|(t, _)| t.to_u16()); |
| 125 |
assert_eq!( |
| 126 |
all_tags, |
| 127 |
vec![ |
| 128 |
(Tag::ImageWidth, ifd::Value::Unsigned(100)), |
| 129 |
(Tag::ImageLength, ifd::Value::Unsigned(100)), |
| 130 |
( |
| 131 |
Tag::BitsPerSample, |
| 132 |
ifd::Value::List(vec![ |
| 133 |
ifd::Value::Short(8), |
| 134 |
ifd::Value::Short(8), |
| 135 |
ifd::Value::Short(8) |
| 136 |
]) |
| 137 |
), |
| 138 |
(Tag::Compression, ifd::Value::Short(1)), |
| 139 |
(Tag::PhotometricInterpretation, ifd::Value::Short(2)), |
| 140 |
(Tag::StripOffsets, ifd::Value::UnsignedBig(27)), |
| 141 |
(Tag::SamplesPerPixel, ifd::Value::Short(3)), |
| 142 |
(Tag::RowsPerStrip, ifd::Value::Unsigned(3334)), |
| 143 |
(Tag::StripByteCounts, ifd::Value::UnsignedBig(30000)), |
| 144 |
(Tag::XResolution, ifd::Value::Rational(1, 1)), |
| 145 |
(Tag::YResolution, ifd::Value::Rational(1, 1)), |
| 146 |
(Tag::ResolutionUnit, ifd::Value::Short(1)), |
| 147 |
(Tag::Artist, ifd::Value::Ascii("Image-tiff".into())), |
| 148 |
(Tag::Predictor, ifd::Value::Short(1)), |
| 149 |
( |
| 150 |
Tag::SampleFormat, |
| 151 |
ifd::Value::List(vec![ |
| 152 |
ifd::Value::Short(1), |
| 153 |
ifd::Value::Short(1), |
| 154 |
ifd::Value::Short(1) |
| 155 |
]) |
| 156 |
), |
| 157 |
] |
| 158 |
); |
| 159 |
|
| 160 |
assert_eq!( |
| 161 |
decoder.get_tag(Tag::Artist).unwrap(), |
| 162 |
ifd::Value::Ascii("Image-tiff".into()) |
| 163 |
); |
| 164 |
if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() { |
| 165 |
assert_eq!(image_data, img_res); |
| 166 |
} else { |
| 167 |
panic!("Wrong data type"); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn test_encode_ifd() { |
| 174 |
let mut data = Cursor::new(Vec::new()); |
| 175 |
|
| 176 |
{ |
| 177 |
let mut tiff = TiffEncoder::new(&mut data).unwrap(); |
| 178 |
let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap(); |
| 179 |
image_encoder.write_strip(&[1]).unwrap(); |
| 180 |
let encoder = image_encoder.encoder(); |
| 181 |
|
| 182 |
// Use the "reusable" tags section as per the TIFF6 spec |
| 183 |
encoder.write_tag(Tag::Unknown(65000), Ifd(42u32)).unwrap(); |
| 184 |
encoder |
| 185 |
.write_tag(Tag::Unknown(65001), &[Ifd(100u32)][..]) |
| 186 |
.unwrap(); |
| 187 |
encoder |
| 188 |
.write_tag(Tag::Unknown(65002), &[Ifd(1u32), Ifd(2u32), Ifd(3u32)][..]) |
| 189 |
.unwrap(); |
| 190 |
|
| 191 |
encoder.write_tag(Tag::Unknown(65010), Ifd8(43u64)).unwrap(); |
| 192 |
encoder |
| 193 |
.write_tag(Tag::Unknown(65011), &[Ifd8(100u64)][..]) |
| 194 |
.unwrap(); |
| 195 |
encoder |
| 196 |
.write_tag( |
| 197 |
Tag::Unknown(65012), |
| 198 |
&[Ifd8(1u64), Ifd8(2u64), Ifd8(3u64)][..], |
| 199 |
) |
| 200 |
.unwrap(); |
| 201 |
} |
| 202 |
|
| 203 |
// Rewind the cursor for reading |
| 204 |
data.set_position(0); |
| 205 |
{ |
| 206 |
let mut decoder = Decoder::new(&mut data).unwrap(); |
| 207 |
|
| 208 |
assert_eq!(decoder.assert_tag_u32(65000), 42); |
| 209 |
assert_eq!(decoder.assert_tag_u32_vec(65000), [42]); |
| 210 |
assert_eq!(decoder.assert_tag_u32_vec(65001), [100]); |
| 211 |
assert_eq!(decoder.assert_tag_u32_vec(65002), [1, 2, 3]); |
| 212 |
|
| 213 |
assert_eq!(decoder.assert_tag_u64(65010), 43); |
| 214 |
assert_eq!(decoder.assert_tag_u64_vec(65010), [43]); |
| 215 |
assert_eq!(decoder.assert_tag_u64_vec(65011), [100]); |
| 216 |
assert_eq!(decoder.assert_tag_u64_vec(65012), [1, 2, 3]); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
/// Test that attempting to encode when the input buffer is undersized returns |
| 222 |
/// an error rather than panicking. |
| 223 |
/// See: https://github.com/PistonDevelopers/image-tiff/issues/35 |
| 224 |
fn test_encode_undersized_buffer() { |
| 225 |
let input_data = vec![1, 2, 3]; |
| 226 |
let output = Vec::new(); |
| 227 |
let mut output_stream = Cursor::new(output); |
| 228 |
if let Ok(mut tiff) = TiffEncoder::new(&mut output_stream) { |
| 229 |
let res = tiff.write_image::<colortype::RGB8>(50, 50, &input_data); |
| 230 |
assert!(res.is_err()); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
const TEST_IMAGE_DIR: &str = "./tests/images/"; |
| 235 |
|
| 236 |
macro_rules! test_roundtrip { |
| 237 |
($name:ident, $buffer:ident, $buffer_ty:ty) => { |
| 238 |
fn $name<C: colortype::ColorType<Inner = $buffer_ty>>( |
| 239 |
file: &str, |
| 240 |
expected_type: ColorType, |
| 241 |
) { |
| 242 |
let path = PathBuf::from(TEST_IMAGE_DIR).join(file); |
| 243 |
let img_file = File::open(path).expect("Cannot find test image!"); |
| 244 |
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 245 |
assert_eq!(decoder.colortype().unwrap(), expected_type); |
| 246 |
|
| 247 |
let image_data = match decoder.read_image().unwrap() { |
| 248 |
DecodingResult::$buffer(res) => res, |
| 249 |
_ => panic!("Wrong data type"), |
| 250 |
}; |
| 251 |
|
| 252 |
let mut file = Cursor::new(Vec::new()); |
| 253 |
{ |
| 254 |
let mut tiff = TiffEncoder::new(&mut file).unwrap(); |
| 255 |
|
| 256 |
let (width, height) = decoder.dimensions().unwrap(); |
| 257 |
tiff.write_image::<C>(width, height, &image_data).unwrap(); |
| 258 |
} |
| 259 |
file.seek(SeekFrom::Start(0)).unwrap(); |
| 260 |
{ |
| 261 |
let mut decoder = Decoder::new(&mut file).unwrap(); |
| 262 |
if let DecodingResult::$buffer(img_res) = decoder.read_image().unwrap() { |
| 263 |
assert_eq!(image_data, img_res); |
| 264 |
} else { |
| 265 |
panic!("Wrong data type"); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
}; |
| 270 |
} |
| 271 |
|
| 272 |
test_roundtrip!(test_u8_roundtrip, U8, u8); |
| 273 |
test_roundtrip!(test_i8_roundtrip, I8, i8); |
| 274 |
test_roundtrip!(test_u16_roundtrip, U16, u16); |
| 275 |
test_roundtrip!(test_i16_roundtrip, I16, i16); |
| 276 |
test_roundtrip!(test_u32_roundtrip, U32, u32); |
| 277 |
test_roundtrip!(test_u64_roundtrip, U64, u64); |
| 278 |
test_roundtrip!(test_f32_roundtrip, F32, f32); |
| 279 |
test_roundtrip!(test_f64_roundtrip, F64, f64); |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn test_gray_u8_roundtrip() { |
| 283 |
test_u8_roundtrip::<colortype::Gray8>("minisblack-1c-8b.tiff", ColorType::Gray(8)); |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn test_gray_i8_roundtrip() { |
| 288 |
test_i8_roundtrip::<colortype::GrayI8>("minisblack-1c-i8b.tiff", ColorType::Gray(8)); |
| 289 |
} |
| 290 |
|
| 291 |
#[test] |
| 292 |
fn test_rgb_u8_roundtrip() { |
| 293 |
test_u8_roundtrip::<colortype::RGB8>("rgb-3c-8b.tiff", ColorType::RGB(8)); |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn test_cmyk_u8_roundtrip() { |
| 298 |
test_u8_roundtrip::<colortype::CMYK8>("cmyk-3c-8b.tiff", ColorType::CMYK(8)); |
| 299 |
} |
| 300 |
|
| 301 |
#[test] |
| 302 |
fn test_cmyka_u8_roundtrip() { |
| 303 |
test_u8_roundtrip::<colortype::CMYKA8>("cmyk-4c-8b.tiff", ColorType::CMYKA(8)); |
| 304 |
} |
| 305 |
|
| 306 |
#[test] |
| 307 |
fn test_gray_u16_roundtrip() { |
| 308 |
test_u16_roundtrip::<colortype::Gray16>("minisblack-1c-16b.tiff", ColorType::Gray(16)); |
| 309 |
} |
| 310 |
|
| 311 |
#[test] |
| 312 |
fn test_gray_i16_roundtrip() { |
| 313 |
test_i16_roundtrip::<colortype::GrayI16>("minisblack-1c-i16b.tiff", ColorType::Gray(16)); |
| 314 |
} |
| 315 |
|
| 316 |
#[test] |
| 317 |
fn test_rgb_u16_roundtrip() { |
| 318 |
test_u16_roundtrip::<colortype::RGB16>("rgb-3c-16b.tiff", ColorType::RGB(16)); |
| 319 |
} |
| 320 |
|
| 321 |
#[test] |
| 322 |
fn test_cmyk_u16_roundtrip() { |
| 323 |
test_u16_roundtrip::<colortype::CMYK16>("cmyk-3c-16b.tiff", ColorType::CMYK(16)); |
| 324 |
} |
| 325 |
|
| 326 |
#[test] |
| 327 |
fn test_gray_u32_roundtrip() { |
| 328 |
test_u32_roundtrip::<colortype::Gray32>("gradient-1c-32b.tiff", ColorType::Gray(32)); |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn test_rgb_u32_roundtrip() { |
| 333 |
test_u32_roundtrip::<colortype::RGB32>("gradient-3c-32b.tiff", ColorType::RGB(32)); |
| 334 |
} |
| 335 |
|
| 336 |
#[test] |
| 337 |
fn test_gray_u64_roundtrip() { |
| 338 |
test_u64_roundtrip::<colortype::Gray64>("gradient-1c-64b.tiff", ColorType::Gray(64)); |
| 339 |
} |
| 340 |
|
| 341 |
#[test] |
| 342 |
fn test_rgb_u64_roundtrip() { |
| 343 |
test_u64_roundtrip::<colortype::RGB64>("gradient-3c-64b.tiff", ColorType::RGB(64)); |
| 344 |
} |
| 345 |
|
| 346 |
#[test] |
| 347 |
fn test_gray_f32_roundtrip() { |
| 348 |
test_f32_roundtrip::<colortype::Gray32Float>("gradient-1c-32b-float.tiff", ColorType::Gray(32)); |
| 349 |
} |
| 350 |
|
| 351 |
#[test] |
| 352 |
fn test_rgb_f32_roundtrip() { |
| 353 |
test_f32_roundtrip::<colortype::RGB32Float>("gradient-3c-32b-float.tiff", ColorType::RGB(32)); |
| 354 |
} |
| 355 |
|
| 356 |
#[test] |
| 357 |
fn test_cmyk_f32_roundtrip() { |
| 358 |
test_f32_roundtrip::<colortype::CMYK32Float>("cmyk-3c-32b-float.tiff", ColorType::CMYK(32)); |
| 359 |
} |
| 360 |
|
| 361 |
#[test] |
| 362 |
fn test_gray_f64_roundtrip() { |
| 363 |
test_f64_roundtrip::<colortype::Gray64Float>("gradient-1c-64b-float.tiff", ColorType::Gray(64)); |
| 364 |
} |
| 365 |
|
| 366 |
#[test] |
| 367 |
fn test_ycbcr_u8_roundtrip() { |
| 368 |
test_u8_roundtrip::<colortype::YCbCr8>("tiled-jpeg-ycbcr.tif", ColorType::YCbCr(8)); |
| 369 |
} |
| 370 |
|
| 371 |
trait AssertDecode { |
| 372 |
fn assert_tag_u32(&mut self, tag: u16) -> u32; |
| 373 |
fn assert_tag_u32_vec(&mut self, tag: u16) -> Vec<u32>; |
| 374 |
fn assert_tag_i32(&mut self, tag: u16) -> i32; |
| 375 |
fn assert_tag_i32_vec(&mut self, tag: u16) -> Vec<i32>; |
| 376 |
fn assert_tag_u64(&mut self, tag: u16) -> u64; |
| 377 |
fn assert_tag_u64_vec(&mut self, tag: u16) -> Vec<u64>; |
| 378 |
fn assert_tag_i64(&mut self, tag: u16) -> i64; |
| 379 |
fn assert_tag_i64_vec(&mut self, tag: u16) -> Vec<i64>; |
| 380 |
} |
| 381 |
|
| 382 |
impl<R: std::io::Read + std::io::Seek> AssertDecode for Decoder<R> { |
| 383 |
fn assert_tag_u32(&mut self, tag: u16) -> u32 { |
| 384 |
self.get_tag(Tag::Unknown(tag)).unwrap().into_u32().unwrap() |
| 385 |
} |
| 386 |
fn assert_tag_u32_vec(&mut self, tag: u16) -> Vec<u32> { |
| 387 |
self.get_tag(Tag::Unknown(tag)) |
| 388 |
.unwrap() |
| 389 |
.into_u32_vec() |
| 390 |
.unwrap() |
| 391 |
} |
| 392 |
fn assert_tag_i32(&mut self, tag: u16) -> i32 { |
| 393 |
self.get_tag(Tag::Unknown(tag)).unwrap().into_i32().unwrap() |
| 394 |
} |
| 395 |
fn assert_tag_i32_vec(&mut self, tag: u16) -> Vec<i32> { |
| 396 |
self.get_tag(Tag::Unknown(tag)) |
| 397 |
.unwrap() |
| 398 |
.into_i32_vec() |
| 399 |
.unwrap() |
| 400 |
} |
| 401 |
fn assert_tag_u64(&mut self, tag: u16) -> u64 { |
| 402 |
self.get_tag(Tag::Unknown(tag)).unwrap().into_u64().unwrap() |
| 403 |
} |
| 404 |
fn assert_tag_u64_vec(&mut self, tag: u16) -> Vec<u64> { |
| 405 |
self.get_tag(Tag::Unknown(tag)) |
| 406 |
.unwrap() |
| 407 |
.into_u64_vec() |
| 408 |
.unwrap() |
| 409 |
} |
| 410 |
fn assert_tag_i64(&mut self, tag: u16) -> i64 { |
| 411 |
self.get_tag(Tag::Unknown(tag)).unwrap().into_i64().unwrap() |
| 412 |
} |
| 413 |
fn assert_tag_i64_vec(&mut self, tag: u16) -> Vec<i64> { |
| 414 |
self.get_tag(Tag::Unknown(tag)) |
| 415 |
.unwrap() |
| 416 |
.into_i64_vec() |
| 417 |
.unwrap() |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
#[test] |
| 422 |
fn test_multiple_byte() { |
| 423 |
let mut data = Cursor::new(Vec::new()); |
| 424 |
|
| 425 |
{ |
| 426 |
let mut tiff = TiffEncoder::new(&mut data).unwrap(); |
| 427 |
let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap(); |
| 428 |
image_encoder.write_strip(&[1]).unwrap(); |
| 429 |
let encoder = image_encoder.encoder(); |
| 430 |
|
| 431 |
encoder.write_tag(Tag::Unknown(65000), &[1_u8][..]).unwrap(); |
| 432 |
encoder |
| 433 |
.write_tag(Tag::Unknown(65001), &[1_u8, 2][..]) |
| 434 |
.unwrap(); |
| 435 |
encoder |
| 436 |
.write_tag(Tag::Unknown(65002), &[1_u8, 2, 3][..]) |
| 437 |
.unwrap(); |
| 438 |
encoder |
| 439 |
.write_tag(Tag::Unknown(65003), &[1_u8, 2, 3, 4][..]) |
| 440 |
.unwrap(); |
| 441 |
encoder |
| 442 |
.write_tag(Tag::Unknown(65004), &[1_u8, 2, 3, 4, 5][..]) |
| 443 |
.unwrap(); |
| 444 |
} |
| 445 |
|
| 446 |
data.set_position(0); |
| 447 |
{ |
| 448 |
let mut decoder = Decoder::new(&mut data).unwrap(); |
| 449 |
|
| 450 |
assert_eq!(decoder.assert_tag_u32_vec(65000), [1]); |
| 451 |
assert_eq!(decoder.assert_tag_u32_vec(65001), [1, 2]); |
| 452 |
assert_eq!(decoder.assert_tag_u32_vec(65002), [1, 2, 3]); |
| 453 |
assert_eq!(decoder.assert_tag_u32_vec(65003), [1, 2, 3, 4]); |
| 454 |
assert_eq!(decoder.assert_tag_u32_vec(65004), [1, 2, 3, 4, 5]); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
#[test] |
| 459 |
/// Test writing signed tags from TIFF 6.0 |
| 460 |
fn test_signed() { |
| 461 |
let mut data = Cursor::new(Vec::new()); |
| 462 |
fn make_srational(i: i32) -> SRational { |
| 463 |
SRational { n: i, d: 100 } |
| 464 |
} |
| 465 |
|
| 466 |
{ |
| 467 |
let mut tiff = TiffEncoder::new(&mut data).unwrap(); |
| 468 |
let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap(); |
| 469 |
image_encoder.write_strip(&[1]).unwrap(); |
| 470 |
let encoder = image_encoder.encoder(); |
| 471 |
|
| 472 |
//Use the "reusable" tags section as per the TIFF6 spec |
| 473 |
encoder.write_tag(Tag::Unknown(65000), -1_i8).unwrap(); |
| 474 |
encoder |
| 475 |
.write_tag(Tag::Unknown(65001), &[-1_i8][..]) |
| 476 |
.unwrap(); |
| 477 |
encoder |
| 478 |
.write_tag(Tag::Unknown(65002), &[-1_i8, 2][..]) |
| 479 |
.unwrap(); |
| 480 |
encoder |
| 481 |
.write_tag(Tag::Unknown(65003), &[-1_i8, 2, -3][..]) |
| 482 |
.unwrap(); |
| 483 |
encoder |
| 484 |
.write_tag(Tag::Unknown(65004), &[-1_i8, 2, -3, 4][..]) |
| 485 |
.unwrap(); |
| 486 |
encoder |
| 487 |
.write_tag(Tag::Unknown(65005), &[-1_i8, 2, -3, 4, -5][..]) |
| 488 |
.unwrap(); |
| 489 |
|
| 490 |
encoder.write_tag(Tag::Unknown(65010), -1_i16).unwrap(); |
| 491 |
encoder.write_tag(Tag::Unknown(65011), -1_i16).unwrap(); |
| 492 |
encoder |
| 493 |
.write_tag(Tag::Unknown(65012), &[-1_i16, 2][..]) |
| 494 |
.unwrap(); |
| 495 |
encoder |
| 496 |
.write_tag(Tag::Unknown(65013), &[-1_i16, 2, -3][..]) |
| 497 |
.unwrap(); |
| 498 |
|
| 499 |
encoder.write_tag(Tag::Unknown(65020), -1_i32).unwrap(); |
| 500 |
encoder |
| 501 |
.write_tag(Tag::Unknown(65021), &[-1_i32][..]) |
| 502 |
.unwrap(); |
| 503 |
encoder |
| 504 |
.write_tag(Tag::Unknown(65022), &[-1_i32, 2][..]) |
| 505 |
.unwrap(); |
| 506 |
|
| 507 |
encoder.write_tag(Tag::Unknown(65030), -1_i64).unwrap(); |
| 508 |
encoder |
| 509 |
.write_tag(Tag::Unknown(65031), &[-1_i64][..]) |
| 510 |
.unwrap(); |
| 511 |
encoder |
| 512 |
.write_tag(Tag::Unknown(65032), &[-1_i64, 2][..]) |
| 513 |
.unwrap(); |
| 514 |
|
| 515 |
encoder |
| 516 |
.write_tag(Tag::Unknown(65040), make_srational(-1)) |
| 517 |
.unwrap(); |
| 518 |
encoder |
| 519 |
.write_tag( |
| 520 |
Tag::Unknown(65041), |
| 521 |
&[make_srational(-1), make_srational(2)][..], |
| 522 |
) |
| 523 |
.unwrap(); |
| 524 |
} |
| 525 |
|
| 526 |
//Rewind the cursor for reading |
| 527 |
data.set_position(0); |
| 528 |
{ |
| 529 |
let mut decoder = Decoder::new(&mut data).unwrap(); |
| 530 |
|
| 531 |
assert_eq!(decoder.assert_tag_i32(65000), -1); |
| 532 |
assert_eq!(decoder.assert_tag_i32_vec(65001), [-1]); |
| 533 |
assert_eq!(decoder.assert_tag_i32_vec(65002), [-1, 2]); |
| 534 |
assert_eq!(decoder.assert_tag_i32_vec(65003), [-1, 2, -3]); |
| 535 |
assert_eq!(decoder.assert_tag_i32_vec(65004), [-1, 2, -3, 4]); |
| 536 |
assert_eq!(decoder.assert_tag_i32_vec(65005), [-1, 2, -3, 4, -5],); |
| 537 |
|
| 538 |
assert_eq!(decoder.assert_tag_i32(65010), -1); |
| 539 |
assert_eq!(decoder.assert_tag_i32_vec(65011), [-1]); |
| 540 |
assert_eq!(decoder.assert_tag_i32_vec(65012), [-1, 2]); |
| 541 |
assert_eq!(decoder.assert_tag_i32_vec(65013), [-1, 2, -3]); |
| 542 |
|
| 543 |
assert_eq!(decoder.assert_tag_i32(65020), -1); |
| 544 |
assert_eq!(decoder.assert_tag_i32_vec(65021), [-1]); |
| 545 |
assert_eq!(decoder.assert_tag_i32_vec(65022), [-1, 2]); |
| 546 |
|
| 547 |
assert_eq!(decoder.assert_tag_i64(65030), -1); |
| 548 |
assert_eq!(decoder.assert_tag_i64_vec(65031), [-1]); |
| 549 |
assert_eq!(decoder.assert_tag_i64_vec(65032), [-1, 2]); |
| 550 |
|
| 551 |
assert_eq!(decoder.assert_tag_i32_vec(65040), [-1, 100]); |
| 552 |
assert_eq!(decoder.assert_tag_i32_vec(65041), [-1_i32, 100, 2, 100]); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
#[test] |
| 557 |
/// check multipage image handling |
| 558 |
fn test_multipage_image() { |
| 559 |
let mut img_file = Cursor::new(Vec::new()); |
| 560 |
|
| 561 |
{ |
| 562 |
// first create a multipage image with 2 images |
| 563 |
let mut img_encoder = TiffEncoder::new(&mut img_file).unwrap(); |
| 564 |
|
| 565 |
// write first grayscale image (2x2 16-bit) |
| 566 |
let img1: Vec<u16> = [1, 2, 3, 4].to_vec(); |
| 567 |
img_encoder |
| 568 |
.write_image::<colortype::Gray16>(2, 2, &img1[..]) |
| 569 |
.unwrap(); |
| 570 |
// write second grayscale image (3x3 8-bit) |
| 571 |
let img2: Vec<u8> = [9, 8, 7, 6, 5, 4, 3, 2, 1].to_vec(); |
| 572 |
img_encoder |
| 573 |
.write_image::<colortype::Gray8>(3, 3, &img2[..]) |
| 574 |
.unwrap(); |
| 575 |
} |
| 576 |
|
| 577 |
// seek to the beginning of the file, so that it can be decoded |
| 578 |
img_file.seek(SeekFrom::Start(0)).unwrap(); |
| 579 |
|
| 580 |
{ |
| 581 |
let mut img_decoder = Decoder::new(&mut img_file).unwrap(); |
| 582 |
|
| 583 |
// check the dimensions of the image in the first page |
| 584 |
assert_eq!(img_decoder.dimensions().unwrap(), (2, 2)); |
| 585 |
img_decoder.next_image().unwrap(); |
| 586 |
// check the dimensions of the image in the second page |
| 587 |
assert_eq!(img_decoder.dimensions().unwrap(), (3, 3)); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
#[test] |
| 592 |
/// verify rows per strip setting |
| 593 |
fn test_rows_per_strip() { |
| 594 |
let mut file = Cursor::new(Vec::new()); |
| 595 |
{ |
| 596 |
let mut img_encoder = TiffEncoder::new(&mut file).unwrap(); |
| 597 |
|
| 598 |
let mut image = img_encoder.new_image::<colortype::Gray8>(100, 100).unwrap(); |
| 599 |
assert_eq!(image.next_strip_sample_count(), 100 * 100); |
| 600 |
image.rows_per_strip(2).unwrap(); |
| 601 |
assert_eq!(image.next_strip_sample_count(), 2 * 100); |
| 602 |
|
| 603 |
let img2: Vec<u8> = vec![0; 2 * 100]; |
| 604 |
image.write_strip(&img2[..]).unwrap(); |
| 605 |
assert!(image.rows_per_strip(5).is_err()); |
| 606 |
for i in 1..50 { |
| 607 |
let img2: Vec<u8> = vec![i; 2 * 100]; |
| 608 |
image.write_strip(&img2[..]).unwrap(); |
| 609 |
} |
| 610 |
assert!(image.write_strip(&img2[..]).is_err()); |
| 611 |
image.finish().unwrap(); |
| 612 |
} |
| 613 |
|
| 614 |
file.seek(SeekFrom::Start(0)).unwrap(); |
| 615 |
{ |
| 616 |
let mut decoder = Decoder::new(&mut file).unwrap(); |
| 617 |
assert_eq!(decoder.get_tag_u64(Tag::RowsPerStrip).unwrap(), 2); |
| 618 |
assert_eq!(decoder.strip_count().unwrap(), 50); |
| 619 |
|
| 620 |
for i in 0..50 { |
| 621 |
let img2 = [i; 2 * 100]; |
| 622 |
match decoder.read_chunk(i as u32).unwrap() { |
| 623 |
DecodingResult::U8(data) => assert_eq!(&img2[..], &data[..]), |
| 624 |
other => panic!("Incorrect strip type {other:?}"), |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
#[test] |
| 631 |
fn test_auxiliary_directory() { |
| 632 |
const ARTIST: &str = "Auxiliary-tiff"; |
| 633 |
|
| 634 |
let mut data = Cursor::new(Vec::new()); |
| 635 |
let mut tiff = TiffEncoder::new(&mut data).unwrap(); |
| 636 |
|
| 637 |
{ |
| 638 |
let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap(); |
| 639 |
image_encoder.write_strip(&[1]).unwrap(); |
| 640 |
} |
| 641 |
|
| 642 |
let exif = { |
| 643 |
let mut extra = tiff.extra_directory().unwrap(); |
| 644 |
extra.write_tag(Tag::Artist, ARTIST).unwrap(); |
| 645 |
extra.finish_with_offsets().unwrap() |
| 646 |
}; |
| 647 |
|
| 648 |
{ |
| 649 |
let mut image_encoder = tiff.new_image::<colortype::Gray8>(2, 1).unwrap(); |
| 650 |
image_encoder.write_strip(&[1, 2]).unwrap(); |
| 651 |
let encoder = image_encoder.encoder(); |
| 652 |
encoder.write_tag(Tag::ExifDirectory, exif.offset).unwrap(); |
| 653 |
} |
| 654 |
|
| 655 |
data.set_position(0); |
| 656 |
let mut decoder = Decoder::new(&mut data).unwrap(); |
| 657 |
|
| 658 |
{ |
| 659 |
assert_eq!(decoder.dimensions().unwrap(), (1, 1)); |
| 660 |
let _ = decoder.read_image().unwrap(); |
| 661 |
} |
| 662 |
|
| 663 |
{ |
| 664 |
decoder.next_image().unwrap(); |
| 665 |
assert_eq!(decoder.dimensions().unwrap(), (2, 1)); |
| 666 |
let _ = decoder.read_image().unwrap(); |
| 667 |
|
| 668 |
let exif_location = decoder |
| 669 |
.get_tag(Tag::ExifDirectory) |
| 670 |
.expect("second directory missing Exif") |
| 671 |
.into_ifd_pointer() |
| 672 |
.expect("exif directory is not an IFD pointer"); |
| 673 |
|
| 674 |
let exif = decoder.read_directory(exif_location).unwrap(); |
| 675 |
|
| 676 |
let artist = decoder |
| 677 |
.read_directory_tags(&exif) |
| 678 |
.get_tag_ascii_string(Tag::Artist) |
| 679 |
.expect("EXIF directory missing artist tag"); |
| 680 |
assert_eq!(artist, ARTIST); |
| 681 |
} |
| 682 |
} |