Debian Codegraph @

TIFF/t/10tiff.t

raw
1 #!perl -w
2 use strict;
3 use Test::More;
4 use Imager qw(:all);
5 use Imager::Test qw(is_image is_image_similar test_image test_image_16 test_image_double test_image_raw);
6
7 BEGIN { use_ok("Imager::File::TIFF"); }
8
9 -d "testout"
10 or mkdir "testout";
11
12 $|=1; # give us some progress in the test harness
13 init_log("testout/t106tiff.log",1);
14
15 my $green=i_color_new(0,255,0,255);
16 my $blue=i_color_new(0,0,255,255);
17 my $red=i_color_new(255,0,0,255);
18
19 my $img=test_image_raw();
20
21 my $ver_string = Imager::File::TIFF->libversion();
22 ok(my ($full, $major, $minor, $point) =
23 $ver_string =~ /Version +((\d+)\.(\d+).(\d+))/,
24 "extract library version")
25 or diag("Could not extract from:\n$ver_string");
26 diag("libtiff binary release $full") if $full;
27 diag("build version date " . Imager::File::TIFF->builddate);
28 # only from 4.5.0 or later
29 my $buildversion = Imager::File::TIFF->buildversion;
30 diag("build version $buildversion") if $buildversion;
31
32 Imager::i_tags_add($img, "i_xres", 0, "300", 0);
33 Imager::i_tags_add($img, "i_yres", 0, undef, 250);
34 # resolutionunit is centimeters
35 Imager::i_tags_add($img, "tiff_resolutionunit", 0, undef, 3);
36 Imager::i_tags_add($img, "tiff_software", 0, "t106tiff.t", 0);
37 open(FH,">testout/t106.tiff") || die "cannot open testout/t106.tiff for writing\n";
38 binmode(FH);
39 my $IO = Imager::io_new_fd(fileno(FH));
40 ok(Imager::File::TIFF::i_writetiff_wiol($img, $IO), "write low level")
41 or print "# ", Imager->_error_as_msg, "\n";
42 close(FH);
43
44 open(FH,"testout/t106.tiff") or die "cannot open testout/t106.tiff\n";
45 binmode(FH);
46 $IO = Imager::io_new_fd(fileno(FH));
47 my $cmpimg = Imager::File::TIFF::i_readtiff_wiol($IO);
48 ok($cmpimg, "read low-level");
49
50 close(FH);
51
52 print "# tiff average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n";
53
54 ok(!i_img_diff($img, $cmpimg), "compare written and read image");
55
56 # check the tags are ok
57 my %tags = map { Imager::i_tags_get($cmpimg, $_) }
58 0 .. Imager::i_tags_count($cmpimg) - 1;
59 ok(abs($tags{i_xres} - 300) < 0.5, "i_xres in range");
60 ok(abs($tags{i_yres} - 250) < 0.5, "i_yres in range");
61 is($tags{tiff_resolutionunit}, 3, "tiff_resolutionunit");
62 is($tags{tiff_software}, 't106tiff.t', "tiff_software");
63 is($tags{tiff_photometric}, 2, "tiff_photometric"); # PHOTOMETRIC_RGB is 2
64 is($tags{tiff_bitspersample}, 8, "tiff_bitspersample");
65
66 $IO = Imager::io_new_bufchain();
67
68 ok(Imager::File::TIFF::i_writetiff_wiol($img, $IO), "write to buffer chain");
69 my $tiffdata = Imager::io_slurp($IO);
70
71 open(FH,"testout/t106.tiff");
72 binmode FH;
73 my $odata;
74 { local $/;
75 $odata = <FH>;
76 }
77
78 is($odata, $tiffdata, "same data in file as in memory");
79
80 # test Micksa's tiff writer
81 # a shortish fax page
82 my $faximg = Imager::ImgRaw::new(1728, 2000, 1);
83 my $black = i_color_new(0,0,0,255);
84 my $white = i_color_new(255,255,255,255);
85 # vaguely test-patterny
86 i_box_filled($faximg, 0, 0, 1728, 2000, $white);
87 i_box_filled($faximg, 100,100,1628, 200, $black);
88 my $width = 1;
89 my $pos = 100;
90 while ($width+$pos < 1628) {
91 i_box_filled($faximg, $pos, 300, $pos+$width-1, 400, $black);
92 $pos += $width + 20;
93 $width += 2;
94 }
95 open FH, "> testout/t106tiff_fax.tiff"
96 or die "Cannot create testout/t106tiff_fax.tiff: $!";
97 binmode FH;
98 $IO = Imager::io_new_fd(fileno(FH));
99 ok(Imager::File::TIFF::i_writetiff_wiol_faxable($faximg, $IO, 1), "write faxable, low level");
100 close FH;
101
102 # test the OO interface
103 my $ooim = Imager->new;
104 ok($ooim->read(file=>'testout/t106.tiff'), "read OO");
105 ok($ooim->write(file=>'testout/t106_oo.tiff'), "write OO");
106
107 # OO with the fax image
108 my $oofim = Imager->new;
109 ok($oofim->read(file=>'testout/t106tiff_fax.tiff'),
110 "read fax OO");
111
112 # this should have tags set for the resolution
113 %tags = map @$_, $oofim->tags;
114 is($tags{i_xres}, 204, "fax i_xres");
115 is($tags{i_yres}, 196, "fax i_yres");
116 ok(!$tags{i_aspect_only}, "i_aspect_only");
117 # resunit_inches
118 is($tags{tiff_resolutionunit}, 2, "tiff_resolutionunit");
119 is($tags{tiff_bitspersample}, 1, "tiff_bitspersample");
120 is($tags{tiff_photometric}, 0, "tiff_photometric");
121
122 ok($oofim->write(file=>'testout/t106_oo_fax.tiff', class=>'fax'),
123 "write OO, faxable");
124
125 # the following should fail since there's no type and no filename
126 my $oodata;
127 ok(!$ooim->write(data=>\$oodata), "write with no type and no filename to guess with");
128
129 # OO to data
130 ok($ooim->write(data=>\$oodata, type=>'tiff'), "write to data")
131 or print "# ",$ooim->errstr, "\n";
132 is($oodata, $tiffdata, "check data matches between memory and file");
133
134 # make sure we can write non-fine mode
135 ok($oofim->write(file=>'testout/t106_oo_faxlo.tiff', class=>'fax', fax_fine=>0), "write OO, fax standard mode");
136
137 # paletted reads
138 my $img4 = Imager->new;
139 ok($img4->read(file=>'testimg/comp4.tif'), "reading 4-bit paletted")
140 or print "# ", $img4->errstr, "\n";
141 is($img4->type, 'paletted', "image isn't paletted");
142 print "# colors: ", $img4->colorcount,"\n";
143 cmp_ok($img4->colorcount, '<=', 16, "more than 16 colors!");
144 #ok($img4->write(file=>'testout/t106_was4.ppm'),
145 # "Cannot write img4");
146 # I know I'm using BMP before it's test, but comp4.tif started life
147 # as comp4.bmp
148 my $bmp4 = Imager->new;
149 ok($bmp4->read(file=>'testimg/comp4.bmp'), "reading 4-bit bmp!");
150 my $diff = i_img_diff($img4->{IMG}, $bmp4->{IMG});
151 print "# diff $diff\n";
152 ok($diff == 0, "image mismatch");
153 my $img4t = Imager->new;
154 ok($img4t->read(file => 'testimg/comp4t.tif'), "read 4-bit paletted, tiled")
155 or print "# ", $img4t->errstr, "\n";
156 is_image($bmp4, $img4t, "check tiled version matches");
157 my $img8 = Imager->new;
158 ok($img8->read(file=>'testimg/comp8.tif'), "reading 8-bit paletted");
159 is($img8->type, 'paletted', "image isn't paletted");
160 print "# colors: ", $img8->colorcount,"\n";
161 #ok($img8->write(file=>'testout/t106_was8.ppm'),
162 # "Cannot write img8");
163 ok($img8->colorcount == 256, "more colors than expected");
164 my $bmp8 = Imager->new;
165 ok($bmp8->read(file=>'testimg/comp8.bmp'), "reading 8-bit bmp!");
166 $diff = i_img_diff($img8->{IMG}, $bmp8->{IMG});
167 print "# diff $diff\n";
168 ok($diff == 0, "image mismatch");
169 my $bad = Imager->new;
170 ok($bad->read(file=>'testimg/comp4bad.tif',
171 allow_incomplete=>1), "bad image not returned");
172 ok(scalar $bad->tags(name=>'i_incomplete'), "incomplete tag not set");
173 ok($img8->write(file=>'testout/t106_pal8.tif'), "writing 8-bit paletted");
174 my $cmp8 = Imager->new;
175 ok($cmp8->read(file=>'testout/t106_pal8.tif'),
176 "reading 8-bit paletted");
177 #print "# ",$cmp8->errstr,"\n";
178 is($cmp8->type, 'paletted', "pal8 isn't paletted");
179 is($cmp8->colorcount, 256, "pal8 bad colorcount");
180 $diff = i_img_diff($img8->{IMG}, $cmp8->{IMG});
181 print "# diff $diff\n";
182 ok($diff == 0, "written image doesn't match read");
183 ok($img4->write(file=>'testout/t106_pal4.tif'), "writing 4-bit paletted");
184 ok(my $cmp4 = Imager->new->read(file=>'testout/t106_pal4.tif'),
185 "reading 4-bit paletted");
186 is($cmp4->type, 'paletted', "pal4 isn't paletted");
187 is($cmp4->colorcount, 16, "pal4 bad colorcount");
188 $diff = i_img_diff($img4->{IMG}, $cmp4->{IMG});
189 print "# diff $diff\n";
190 ok($diff == 0, "written image doesn't match read");
191
192 my $work;
193 my $seekpos;
194 sub io_writer {
195 my ($what) = @_;
196 if ($seekpos > length $work) {
197 $work .= "\0" x ($seekpos - length $work);
198 }
199 substr($work, $seekpos, length $what) = $what;
200 $seekpos += length $what;
201
202 1;
203 }
204 sub io_reader {
205 my ($size, $maxread) = @_;
206 print "# io_reader($size, $maxread) pos $seekpos\n";
207 if ($seekpos + $maxread > length $work) {
208 $maxread = length($work) - $seekpos;
209 }
210 my $out = substr($work, $seekpos, $maxread);
211 $seekpos += length $out;
212 $out;
213 }
214 sub io_reader2 {
215 my ($size, $maxread) = @_;
216 print "# io_reader2($size, $maxread) pos $seekpos\n";
217 my $out = substr($work, $seekpos, $size);
218 $seekpos += length $out;
219 $out;
220 }
221 use IO::Seekable;
222 sub io_seeker {
223 my ($offset, $whence) = @_;
224 print "# io_seeker($offset, $whence)\n";
225 if ($whence == SEEK_SET) {
226 $seekpos = $offset;
227 }
228 elsif ($whence == SEEK_CUR) {
229 $seekpos += $offset;
230 }
231 else { # SEEK_END
232 $seekpos = length($work) + $offset;
233 }
234 #print "-> $seekpos\n";
235 $seekpos;
236 }
237 my $did_close;
238 sub io_closer {
239 ++$did_close;
240 }
241
242 # read via cb
243 $work = $tiffdata;
244 $seekpos = 0;
245 my $IO2 = Imager::io_new_cb(undef, \&io_reader, \&io_seeker, undef);
246 ok($IO2, "new readcb obj");
247 my $img5 = Imager::File::TIFF::i_readtiff_wiol($IO2);
248 ok($img5, "read via cb");
249 ok(i_img_diff($img5, $img) == 0, "read from cb diff");
250
251 # read via cb2
252 $work = $tiffdata;
253 $seekpos = 0;
254 my $IO3 = Imager::io_new_cb(undef, \&io_reader2, \&io_seeker, undef);
255 ok($IO3, "new readcb2 obj");
256 my $img6 = Imager::File::TIFF::i_readtiff_wiol($IO3);
257 ok($img6, "read via cb2");
258 ok(i_img_diff($img6, $img) == 0, "read from cb2 diff");
259
260 # write via cb
261 $work = '';
262 $seekpos = 0;
263 my $IO4 = Imager::io_new_cb(\&io_writer, \&io_reader, \&io_seeker,
264 \&io_closer);
265 ok($IO4, "new writecb obj");
266 ok(Imager::File::TIFF::i_writetiff_wiol($img, $IO4), "write to cb");
267 is($work, $odata, "write cb match");
268 ok($did_close, "write cb did close");
269 open D1, ">testout/d1.tiff" or die;
270 print D1 $work;
271 close D1;
272 open D2, ">testout/d2.tiff" or die;
273 print D2 $tiffdata;
274 close D2;
275
276 # write via cb2
277 $work = '';
278 $seekpos = 0;
279 $did_close = 0;
280 my $IO5 = Imager::io_new_cb(\&io_writer, \&io_reader, \&io_seeker,
281 \&io_closer, 1);
282 ok($IO5, "new writecb obj 2");
283 ok(Imager::File::TIFF::i_writetiff_wiol($img, $IO5), "write to cb2");
284 is($work, $odata, "write cb2 match");
285 ok($did_close, "write cb2 did close");
286
287 open D3, ">testout/d3.tiff" or die;
288 print D3 $work;
289 close D3;
290
291
292 { # check close failures are handled correctly
293 { # single image
294 my $im = test_image();
295 my $fail_close = sub {
296 Imager::i_push_error(0, "synthetic close failure");
297 return 0;
298 };
299 $work = '';
300 $seekpos = 0;
301 ok(!$im->write(type => "tiff",
302 readcb => \&io_reader,
303 writecb => \&io_writer,
304 seekcb => \&io_seeker,
305 closecb => $fail_close),
306 "check failing close fails");
307 like($im->errstr, qr/synthetic close failure/,
308 "check error message");
309 }
310 { # multiple images
311 my $im = test_image();
312 my $fail_close = sub {
313 Imager::i_push_error(0, "synthetic close failure");
314 return 0;
315 };
316 $work = '';
317 $seekpos = 0;
318 ok(!Imager->write_multi({type => "tiff",
319 readcb => \&io_reader,
320 writecb => \&io_writer,
321 seekcb => \&io_seeker,
322 closecb => $fail_close}, $im, $im),
323 "check failing close fails");
324 like(Imager->errstr, qr/synthetic close failure/,
325 "check error message");
326 }
327 }
328
329 # multi-image write/read
330 my @imgs;
331 push(@imgs, map $ooim->copy(), 1..3);
332 for my $i (0..$#imgs) {
333 $imgs[$i]->addtag(name=>"tiff_pagename", value=>"Page ".($i+1));
334 }
335 my $rc = Imager->write_multi({file=>'testout/t106_multi.tif'}, @imgs);
336 ok($rc, "writing multiple images to tiff");
337 my @out = Imager->read_multi(file=>'testout/t106_multi.tif');
338 ok(@out == @imgs, "reading multiple images from tiff");
339 @out == @imgs or print "# ",scalar @out, " ",Imager->errstr,"\n";
340 for my $i (0..$#imgs) {
341 ok(i_img_diff($imgs[$i]{IMG}, $out[$i]{IMG}) == 0,
342 "comparing image $i");
343 my ($tag) = $out[$i]->tags(name=>'tiff_pagename');
344 is($tag, "Page ".($i+1),
345 "tag doesn't match original image");
346 }
347
348 # writing even more images to tiff - we weren't handling more than five
349 # correctly on read
350 @imgs = map $ooim->copy(), 1..40;
351 $rc = Imager->write_multi({file=>'testout/t106_multi2.tif'}, @imgs);
352 ok($rc, "writing 40 images to tiff")
353 or diag("writing 40 images: " . Imager->errstr);
354 @out = Imager->read_multi(file=>'testout/t106_multi2.tif');
355 ok(@imgs == @out, "reading 40 images from tiff")
356 or diag("reading 40 images:" . Imager->errstr);
357 # force some allocation activity - helps crash here if it's the problem
358 @out = @imgs = ();
359
360 # multi-image fax files
361 ok(Imager->write_multi({file=>'testout/t106_faxmulti.tiff', class=>'fax'},
362 $oofim, $oofim), "write multi fax image")
363 or diag("writing 40 fax pages: " . Imager->errstr);
364 @imgs = Imager->read_multi(file=>'testout/t106_faxmulti.tiff');
365 ok(@imgs == 2, "reading multipage fax")
366 or diag("reading 40 fax pages: " . Imager->errstr);
367 ok(Imager::i_img_diff($imgs[0]{IMG}, $oofim->{IMG}) == 0,
368 "compare first fax image");
369 ok(Imager::i_img_diff($imgs[1]{IMG}, $oofim->{IMG}) == 0,
370 "compare second fax image");
371
372 my ($format) = $imgs[0]->tags(name=>'i_format');
373 is($format, 'tiff', "check i_format tag");
374
375 my $unit = $imgs[0]->tags(name=>'tiff_resolutionunit');
376 ok(defined $unit && $unit == 2, "check tiff_resolutionunit tag");
377 my $unitname = $imgs[0]->tags(name=>'tiff_resolutionunit_name');
378 is($unitname, 'inch', "check tiff_resolutionunit_name tag");
379
380 my $warned = Imager->new;
381 ok($warned->read(file=>"testimg/tiffwarn.tif"), "read tiffwarn.tif");
382 my ($warning) = $warned->tags(name=>'i_warning');
383 ok(defined $warning, "check warning is set");
384 like($warning, qr/TIFFReadDirectory: Unknown field with tag 28712/,
385 "check that warning tag correct");
386
387 { # support for reading a given page
388 # first build a simple test image
389 my $im1 = Imager->new(xsize=>50, ysize=>50);
390 $im1->box(filled=>1, color=>$blue);
391 $im1->addtag(name=>'tiff_pagename', value => "Page One");
392 my $im2 = Imager->new(xsize=>60, ysize=>60);
393 $im2->box(filled=>1, color=>$green);
394 $im2->addtag(name=>'tiff_pagename', value=>"Page Two");
395
396 # read second page
397 my $page_file = 'testout/t106_pages.tif';
398 ok(Imager->write_multi({ file=> $page_file}, $im1, $im2),
399 "build simple multiimage for page tests");
400 my $imwork = Imager->new;
401 ok($imwork->read(file=>$page_file, page=>1),
402 "read second page");
403 is($im2->getwidth, $imwork->getwidth, "check width");
404 is($im2->getwidth, $imwork->getheight, "check height");
405 is(i_img_diff($imwork->{IMG}, $im2->{IMG}), 0,
406 "check image content");
407 my ($page_name) = $imwork->tags(name=>'tiff_pagename');
408 is($page_name, 'Page Two', "check tag we set");
409
410 # try an out of range page
411 ok(!$imwork->read(file=>$page_file, page=>2),
412 "check out of range page");
413 is($imwork->errstr, "could not switch to page 2", "check message");
414 }
415
416 { # test writing returns an error message correctly
417 # open a file read only and try to write to it
418 open TIFF, "> testout/t106_empty.tif" or die;
419 close TIFF;
420 open TIFF, "< testout/t106_empty.tif"
421 or skip "Cannot open testout/t106_empty.tif for reading", 8;
422 binmode TIFF;
423 my $im = Imager->new(xsize=>100, ysize=>100);
424 ok(!$im->write(fh => \*TIFF, type=>'tiff', buffered => 0),
425 "fail to write to read only handle");
426 cmp_ok($im->errstr, '=~', 'Could not create TIFF object: Error writing TIFF header: write\(\)',
427 "check error message");
428 ok(!Imager->write_multi({ type => 'tiff', fh => \*TIFF, buffered => 0 }, $im),
429 "fail to write multi to read only handle");
430 cmp_ok(Imager->errstr, '=~', 'Could not create TIFF object: Error writing TIFF header: write\(\)',
431 "check error message");
432 ok(!$im->write(fh => \*TIFF, type=>'tiff', class=>'fax', buffered => 0),
433 "fail to write to read only handle (fax)");
434 cmp_ok($im->errstr, '=~', 'Could not create TIFF object: Error writing TIFF header: write\(\)',
435 "check error message");
436 ok(!Imager->write_multi({ type => 'tiff', fh => \*TIFF, class=>'fax', buffered => 0 }, $im),
437 "fail to write multi to read only handle (fax)");
438 cmp_ok(Imager->errstr, '=~', 'Could not create TIFF object: Error writing TIFF header: write\(\)',
439 "check error message");
440 }
441
442 { # test reading returns an error correctly - use test script as an
443 # invalid TIFF file
444 my $im = Imager->new;
445 ok(!$im->read(file=>'t/10tiff.t', type=>'tiff'),
446 "fail to read script as image");
447 # we get different magic number values depending on the platform
448 # byte ordering
449 cmp_ok($im->errstr, '=~',
450 "Error opening file: Not a TIFF (?:or MDI )?file, bad magic number (8483 \\(0x2123\\)|8993 \\(0x2321\\))",
451 "check error message");
452 my @ims = Imager->read_multi(file =>'t/t106tiff.t', type=>'tiff');
453 ok(!@ims, "fail to read_multi script as image");
454 cmp_ok($im->errstr, '=~',
455 "Error opening file: Not a TIFF (?:or MDI )?file, bad magic number (8483 \\(0x2123\\)|8993 \\(0x2321\\))",
456 "check error message");
457 }
458
459 { # write_multi to data
460 my $data;
461 my $im = Imager->new(xsize => 50, ysize => 50);
462 ok(Imager->write_multi({ data => \$data, type=>'tiff' }, $im, $im),
463 "write multi to in memory");
464 ok(length $data, "make sure something written");
465 my @im = Imager->read_multi(data => $data);
466 is(@im, 2, "make sure we can read it back");
467 is(Imager::i_img_diff($im[0]{IMG}, $im->{IMG}), 0,
468 "check first image");
469 is(Imager::i_img_diff($im[1]{IMG}, $im->{IMG}), 0,
470 "check second image");
471 }
472
473 { # handling of an alpha channel for various images
474 my $photo_rgb = 2;
475 my $photo_cmyk = 5;
476 my $photo_cielab = 8;
477 my @alpha_images =
478 (
479 [ 'srgb.tif', 3, $photo_rgb ],
480 [ 'srgba.tif', 4, $photo_rgb ],
481 [ 'srgbaa.tif', 4, $photo_rgb ],
482 [ 'scmyk.tif', 3, $photo_cmyk ],
483 [ 'scmyka.tif', 4, $photo_cmyk ],
484 [ 'scmykaa.tif', 4, $photo_cmyk ],
485 [ 'slab.tif', 3, $photo_cielab ],
486 );
487
488 for my $test (@alpha_images) {
489 my ($input, $channels, $photo) = @$test;
490
491 SKIP: {
492 my $skipped = $channels == 4 ? 4 : 3;
493 my $im = Imager->new;
494 ok($im->read(file => "testimg/$input"),
495 "read alpha test $input")
496 or print "# ", $im->errstr, "\n";
497 is($im->getchannels, $channels, "channels for $input match");
498 is($im->tags(name=>'tiff_photometric'), $photo,
499 "photometric for $input match");
500 $channels == 4
501 or next;
502 my $c = $im->getpixel(x => 0, 'y' => 7);
503 is(($c->rgba)[3], 0, "bottom row should have 0 alpha");
504 }
505 }
506 }
507
508 {
509 ok(grep($_ eq 'tiff', Imager->read_types), "check tiff in read types");
510 ok(grep($_ eq 'tiff', Imager->write_types), "check tiff in write types");
511 }
512
513 { # reading tile based images
514 my $im = Imager->new;
515 ok($im->read(file => 'testimg/pengtile.tif'), "read tiled image")
516 or print "# ", $im->errstr, "\n";
517 # compare it
518 my $comp = Imager->new;
519 ok($comp->read(file => 'testimg/penguin-base.ppm'), 'read comparison image');
520 is_image($im, $comp, 'compare them');
521 }
522
523 SKIP:
524 { # failing to read tile based images
525 # we grab our tiled image and patch a tile offset to nowhere
526 ok(open(TIFF, '< testimg/pengtile.tif'), 'open pengtile.tif')
527 or skip 'cannot open testimg/pengtile.tif', 4;
528
529 binmode TIFF;
530 my $data = do { local $/; <TIFF>; };
531
532 # patch a tile offset
533 substr($data, 0x1AFA0, 4) = pack("H*", "00000200");
534
535 #open PIPE, "| bytedump -a | less" or die;
536 #print PIPE $data;
537 #close PIPE;
538
539 my $allow = Imager->new;
540 ok($allow->read(data => $data, allow_incomplete => 1),
541 "read incomplete tiled");
542 ok($allow->tags(name => 'i_incomplete'), 'i_incomplete set');
543 is($allow->tags(name => 'i_lines_read'), 173,
544 'check i_lines_read set appropriately');
545
546 my $fail = Imager->new;
547 ok(!$fail->read(data => $data), "read fail tiled");
548 }
549
550 { # read 16-bit/sample
551 my $im16 = Imager->new;
552 ok($im16->read(file => 'testimg/rgb16.tif'), "read 16-bit rgb");
553 is($im16->bits, 16, 'got a 16-bit image');
554 my $im16t = Imager->new;
555 ok($im16t->read(file => 'testimg/rgb16t.tif'), "read 16-bit rgb tiled");
556 is($im16t->bits, 16, 'got a 16-bit image');
557 is_image($im16, $im16t, 'check they match');
558
559 my $grey16 = Imager->new;
560 ok($grey16->read(file => 'testimg/grey16.tif'), "read 16-bit grey")
561 or print "# ", $grey16->errstr, "\n";
562 is($grey16->bits, 16, 'got a 16-bit image');
563 is($grey16->getchannels, 1, 'and its grey');
564 my $comp16 = $im16->convert(matrix => [ [ 0.299, 0.587, 0.114 ] ]);
565 is_image($grey16, $comp16, 'compare grey to converted');
566
567 my $grey32 = Imager->new;
568 ok($grey32->read(file => 'testimg/grey32.tif'), "read 32-bit grey")
569 or print "# ", $grey32->errstr, "\n";
570 is($grey32->bits, 'double', 'got a double image');
571 is($grey32->getchannels, 2, 'and its grey + alpha');
572 is($grey32->tags(name => 'tiff_bitspersample'), 32,
573 "check bits per sample");
574 my $base = test_image_double->convert(preset =>'grey')
575 ->convert(preset => 'addalpha');
576 is_image($grey32, $base, 'compare to original');
577 }
578
579 { # read 16, 32-bit/sample and compare to the original
580 my $rgba = Imager->new;
581 ok($rgba->read(file => 'testimg/srgba.tif'),
582 "read base rgba image");
583 my $rgba16 = Imager->new;
584 ok($rgba16->read(file => 'testimg/srgba16.tif'),
585 "read 16-bit/sample rgba image");
586 is_image($rgba, $rgba16, "check they match");
587 is($rgba16->bits, 16, 'check we got the right type');
588
589 my $rgba32 = Imager->new;
590 ok($rgba32->read(file => 'testimg/srgba32.tif'),
591 "read 32-bit/sample rgba image");
592 is_image($rgba, $rgba32, "check they match");
593 is($rgba32->bits, 'double', 'check we got the right type');
594
595 my $cmyka16 = Imager->new;
596 ok($cmyka16->read(file => 'testimg/scmyka16.tif'),
597 "read cmyk 16-bit")
598 or print "# ", $cmyka16->errstr, "\n";
599 is($cmyka16->bits, 16, "check we got the right type");
600 is_image_similar($rgba, $cmyka16, 10, "check image data");
601
602 # tiled, non-contig, should fallback to RGBA code
603 my $rgbatsep = Imager->new;
604 ok($rgbatsep->read(file => 'testimg/rgbatsep.tif'),
605 "read tiled, separated rgba image")
606 or diag($rgbatsep->errstr);
607 is_image($rgba, $rgbatsep, "check they match");
608 }
609 { # read bi-level
610 my $pbm = Imager->new;
611 ok($pbm->read(file => 'testimg/imager.pbm'), "read original pbm");
612 my $tif = Imager->new;
613 ok($tif->read(file => 'testimg/imager.tif'), "read mono tif");
614 is_image($pbm, $tif, "compare them");
615 is($tif->type, 'paletted', 'check image type');
616 is($tif->colorcount, 2, 'check we got a "mono" image');
617 }
618
619 { # check alpha channels scaled correctly for fallback handler
620 my $im = Imager->new;
621 ok($im->read(file=>'testimg/alpha.tif'), 'read alpha check image');
622 my @colors =
623 (
624 [ 0, 0, 0 ],
625 [ 255, 255, 255 ],
626 [ 127, 0, 127 ],
627 [ 127, 127, 0 ],
628 );
629 my @alphas = ( 255, 191, 127, 63 );
630 my $ok = 1;
631 my $msg = 'alpha check ok';
632 CHECKER:
633 for my $y (0 .. 3) {
634 for my $x (0 .. 3) {
635 my $c = $im->getpixel(x => $x, 'y' => $y);
636 my @c = $c->rgba;
637 my $alpha = pop @c;
638 if ($alpha != $alphas[$y]) {
639 $ok = 0;
640 $msg = "($x,$y) alpha mismatch $alpha vs $alphas[$y]";
641 last CHECKER;
642 }
643 my $expect = $colors[$x];
644 for my $ch (0 .. 2) {
645 if (abs($expect->[$ch]-$c[$ch]) > 3) {
646 $ok = 0;
647 $msg = "($x,$y)[$ch] color mismatch got $c[$ch] vs expected $expect->[$ch]";
648 last CHECKER;
649 }
650 }
651 }
652 }
653 ok($ok, $msg);
654 }
655
656 { # check alpha channels scaled correctly for greyscale
657 my $im = Imager->new;
658 ok($im->read(file=>'testimg/gralpha.tif'), 'read alpha check grey image');
659 my @greys = ( 0, 255, 52, 112 );
660 my @alphas = ( 255, 191, 127, 63 );
661 my $ok = 1;
662 my $msg = 'alpha check ok';
663 CHECKER:
664 for my $y (0 .. 3) {
665 for my $x (0 .. 3) {
666 my $c = $im->getpixel(x => $x, 'y' => $y);
667 my ($grey, $alpha) = $c->rgba;
668 if ($alpha != $alphas[$y]) {
669 $ok = 0;
670 $msg = "($x,$y) alpha mismatch $alpha vs $alphas[$y]";
671 last CHECKER;
672 }
673 if (abs($greys[$x] - $grey) > 3) {
674 $ok = 0;
675 $msg = "($x,$y) grey mismatch $grey vs $greys[$x]";
676 last CHECKER;
677 }
678 }
679 }
680 ok($ok, $msg);
681 }
682
683 { # 16-bit writes
684 my $orig = test_image_16();
685 my $data;
686 ok($orig->write(data => \$data, type => 'tiff',
687 tiff_compression => 'none'), "write 16-bit/sample");
688 my $im = Imager->new;
689 ok($im->read(data => $data), "read it back");
690 is_image($im, $orig, "check read data matches");
691 is($im->tags(name => 'tiff_bitspersample'), 16, "correct bits");
692 is($im->bits, 16, 'check image bits');
693 is($im->tags(name => 'tiff_photometric'), 2, "correct photometric");
694 is($im->tags(name => 'tiff_compression'), 'none', "no compression");
695 is($im->getchannels, 3, 'correct channels');
696 }
697
698 { # 8-bit writes
699 # and check compression
700 my $compress = Imager::File::TIFF::i_tiff_has_compression('lzw') ? 'lzw' : 'packbits';
701 my $orig = test_image()->convert(preset=>'grey')
702 ->convert(preset => 'addalpha');
703 my $data;
704 ok($orig->write(data => \$data, type => 'tiff',
705 tiff_compression=> $compress),
706 "write 8 bit")
707 or print "# ", $orig->errstr, "\n";
708 my $im = Imager->new;
709 ok($im->read(data => $data), "read it back");
710 is_image($im, $orig, "check read data matches");
711 is($im->tags(name => 'tiff_bitspersample'), 8, 'correct bits');
712 is($im->bits, 8, 'check image bits');
713 is($im->tags(name => 'tiff_photometric'), 1, 'correct photometric');
714 is($im->tags(name => 'tiff_compression'), $compress,
715 "$compress compression");
716 is($im->getchannels, 2, 'correct channels');
717 }
718
719 { # double writes
720 my $orig = test_image_double()->convert(preset=>'addalpha');
721 my $data;
722 ok($orig->write(data => \$data, type => 'tiff',
723 tiff_compression => 'none'),
724 "write 32-bit/sample from double")
725 or diag $orig->errstr;
726 my $im = Imager->new;
727 ok($im->read(data => $data), "read it back");
728 is_image($im, $orig, "check read data matches");
729 is($im->tags(name => 'tiff_bitspersample'), 32, "correct bits");
730 is($im->bits, 'double', 'check image bits');
731 is($im->tags(name => 'tiff_photometric'), 2, "correct photometric");
732 is($im->tags(name => 'tiff_compression'), 'none', "no compression");
733 is($im->getchannels, 4, 'correct channels');
734 }
735
736 { # bilevel
737 my $im = test_image()->convert(preset => 'grey')
738 ->to_paletted(make_colors => 'mono',
739 translate => 'errdiff');
740 my $faxdata;
741
742 # fax compression is written as miniswhite
743 ok($im->write(data => \$faxdata, type => 'tiff',
744 tiff_compression => 'fax3'),
745 "write bilevel fax compressed");
746 my $fax = Imager->new;
747 ok($fax->read(data => $faxdata), "read it back");
748 ok($fax->is_bilevel, "got a bi-level image back");
749 is($fax->tags(name => 'tiff_compression'), 'fax3',
750 "check fax compression used");
751 is_image($fax, $im, "compare to original");
752
753 # other compresion written as minisblack
754 my $packdata;
755 ok($im->write(data => \$packdata, type => 'tiff',
756 tiff_compression => 'jpeg'),
757 "write bilevel packbits compressed");
758 my $packim = Imager->new;
759 ok($packim->read(data => $packdata), "read it back");
760 ok($packim->is_bilevel, "got a bi-level image back");
761 is($packim->tags(name => 'tiff_compression'), 'packbits',
762 "check fallback compression used");
763 is_image($packim, $im, "compare to original");
764 }
765
766 { # fallback handling of tiff
767 is(Imager::File::TIFF::i_tiff_has_compression('none'), 1, "can always do uncompresed");
768 is(Imager::File::TIFF::i_tiff_has_compression('xxx'), '', "can't do xxx compression");
769 }
770
771
772 { # check file limits are checked
773 my $limit_file = "testout/t106.tiff";
774 ok(Imager->set_file_limits(reset=>1, width=>149), "set width limit 149");
775 my $im = Imager->new;
776 ok(!$im->read(file=>$limit_file),
777 "should fail read due to size limits");
778 print "# ",$im->errstr,"\n";
779 like($im->errstr, qr/image width/, "check message");
780
781 ok(Imager->set_file_limits(reset=>1, height=>149), "set height limit 149");
782 ok(!$im->read(file=>$limit_file),
783 "should fail read due to size limits");
784 print "# ",$im->errstr,"\n";
785 like($im->errstr, qr/image height/, "check message");
786
787 ok(Imager->set_file_limits(reset=>1, width=>150), "set width limit 150");
788 ok($im->read(file=>$limit_file),
789 "should succeed - just inside width limit");
790 ok(Imager->set_file_limits(reset=>1, height=>150), "set height limit 150");
791 ok($im->read(file=>$limit_file),
792 "should succeed - just inside height limit");
793
794 # 150 x 150 x 3 channel image uses 67500 bytes
795 ok(Imager->set_file_limits(reset=>1, bytes=>67499),
796 "set bytes limit 67499");
797 ok(!$im->read(file=>$limit_file),
798 "should fail - too many bytes");
799 print "# ",$im->errstr,"\n";
800 like($im->errstr, qr/storage size/, "check error message");
801 ok(Imager->set_file_limits(reset=>1, bytes=>67500),
802 "set bytes limit 67500");
803 ok($im->read(file=>$limit_file),
804 "should succeed - just inside bytes limit");
805 Imager->set_file_limits(reset=>1);
806 }
807
808 {
809 # this image has an IFD loop, which sends some TIFF readers into a
810 # loop, including Corel PhotoPaint and the GIMP's tiff reader.
811 my $ifdloop_hex = <<HEX;
812 49 49 2A 00 0A 00 00 00 FE 00 0A 00 00 01 03 00
813 01 00 00 00 01 00 00 00 01 01 03 00 01 00 00 00
814 01 00 00 00 02 01 03 00 03 00 00 00 88 00 00 00
815 03 01 03 00 01 00 00 00 05 80 00 00 06 01 03 00
816 01 00 00 00 02 00 00 00 11 01 04 00 01 00 00 00
817 08 00 00 00 12 01 03 00 01 00 00 00 01 00 00 00
818 15 01 03 00 01 00 00 00 03 00 00 00 17 01 04 00
819 01 00 00 00 02 00 00 00 1C 01 03 00 01 00 00 00
820 01 00 00 00 90 00 00 00 08 00 08 00 08 00 FE 00
821 0A 00 00 01 03 00 01 00 00 00 01 00 00 00 01 01
822 03 00 01 00 00 00 01 00 00 00 02 01 03 00 03 00
823 00 00 0E 01 00 00 03 01 03 00 01 00 00 00 05 80
824 00 00 06 01 03 00 01 00 00 00 02 00 00 00 11 01
825 04 00 01 00 00 00 8E 00 00 00 12 01 03 00 01 00
826 00 00 01 00 00 00 15 01 03 00 01 00 00 00 03 00
827 00 00 17 01 04 00 01 00 00 00 02 00 00 00 1C 01
828 03 00 01 00 00 00 01 00 00 00 0A 00 00 00 08 00
829 08 00 08 00
830 HEX
831 $ifdloop_hex =~ tr/0-9A-F//cd;
832 my $ifdloop = pack("H*", $ifdloop_hex);
833
834 my $im = Imager->new;
835 ok($im->read(data => $ifdloop, type => "tiff", page => 1),
836 "read what should be valid");
837 ok(!$im->read(data => $ifdloop, type => "tiff", page => 2),
838 "third page is after looping back to the start, if this fails, upgrade tifflib")
839 or skip("tifflib is broken", 1);
840 print "# ", $im->errstr, "\n";
841 my @im = Imager->read_multi(type => "tiff", data => $ifdloop);
842 is(@im, 2, "should be only 2 images");
843 }
844
845 SKIP:
846 { # sample format
847 Imager::File::TIFF::i_tiff_has_compression("lzw")
848 or skip "No LZW support", 8;
849 Imager::File::TIFF::i_tiff_ieeefp()
850 or skip "No IEEE FP type", 8;
851
852 SKIP:
853 { # signed
854 my $cmp = Imager->new(file => "testimg/grey16.tif", filetype => "tiff")
855 or skip "Cannot read grey16.tif: ". Imager->errstr, 4;
856 my $im = Imager->new(file => "testimg/grey16sg.tif", filetype => "tiff");
857 ok($im, "read image with SampleFormat = signed int")
858 or skip "Couldn't read the file", 3;
859 is_image($im, $cmp, "check the images match");
860 my %tags = map @$_, $im->tags;
861 is($tags{tiff_sample_format}, 2, "check sample format");
862 is($tags{tiff_sample_format_name}, "int", "check sample format name");
863 }
864
865 SKIP:
866 { # float
867 my $cmp = Imager->new(file => "testimg/srgba32.tif", filetype => "tiff")
868 or skip "Cannot read srgaba32f.tif: ". Imager->errstr, 4;
869 my $im = Imager->new(file => "testimg/srgba32f.tif", filetype => "tiff");
870 ok($im, "read image with SampleFormat = float")
871 or skip "Couldn't read the file", 3;
872 is_image($im, $cmp, "check the images match");
873 my %tags = map @$_, $im->tags;
874 is($tags{tiff_sample_format}, 3, "check sample format");
875 is($tags{tiff_sample_format_name}, "ieeefp", "check sample format name");
876 }
877 }
878
879 {
880 my @codecs = Imager::File::TIFF->codecs;
881
882 my %codecs = map {; $_->{code} => $_ } @codecs;
883
884 is($codecs{1}{description}, "None", "no compression description");
885 is($codecs{1}{name}, "none", "no compression name");
886 is($codecs{5}{description}, "LZW", "LZW description");
887 is($codecs{5}{name}, "lzw", "LZW name");
888 is($codecs{32773}{description}, "PackBits", "PackBits description");
889 is($codecs{32773}{name}, "packbits", "PackBits name");
890 is($codecs{32771}{description}, "CCITT RLE/W", "CCITT RLE/W description");
891 is($codecs{32771}{name}, "ccittrlew", "CCITT RLE/W name");
892 }
893
894 SKIP:
895 {
896 # check compression selection by name works
897 my @codecs = Imager::File::TIFF->codecs;
898 grep { $_->{description} eq "AdobeDeflate" } @codecs
899 or skip "No AdobeDeflate available", 1;
900 my $im = test_image;
901 my $data;
902 ok($im->write(type => "tiff", data => \$data, tiff_compression => "AdobeDeflate"),
903 "write with AdobeDeflate");
904 my $im2 = Imager->new(data => \$data);
905 is_image($im, $im2, "check read image matches");
906 is($im2->tags(name => "tiff_compression"), "deflate",
907 "got expected compression");
908 }
909
910 done_testing();