Debian Codegraph @

tests/encode_images.rs

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