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