]> git.imager.perl.org - imager.git/blobdiff - t/t106tiff.t
added pixel type 'index' to getscanline() and setscanline() for
[imager.git] / t / t106tiff.t
index 984f3f82ad4ac0f5721887940e60f9c9696dd830..0c964eca46abe53ae94870603bd50bfc3cee4647 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use lib 't';
-use Test::More tests => 81;
+use Test::More tests => 127;
 use Imager qw(:all);
 $^W=1; # warnings during command-line tests
 $|=1;  # give us some progress in the test harness
@@ -32,7 +32,7 @@ SKIP:
     $im = Imager->new(xsize=>2, ysize=>2);
     ok(!$im->write(file=>"testout/notiff.tif"), "should fail to write tiff");
     is($im->errstr, 'format not supported', "check no tiff message");
-    skip("no tiff support", 77);
+    skip("no tiff support", 123);
   }
 
   Imager::i_tags_add($img, "i_xres", 0, "300", 0);
@@ -122,7 +122,7 @@ SKIP:
   # resunit_inches
   is($tags{tiff_resolutionunit}, 2, "tiff_resolutionunit");
   is($tags{tiff_bitspersample}, 1, "tiff_bitspersample");
-  is($tags{tiff_photometric}, 1, "tiff_photometric");
+  is($tags{tiff_photometric}, 0, "tiff_photometric");
 
   ok($oofim->write(file=>'testout/t106_oo_fax.tiff', class=>'fax'),
      "write OO, faxable");
@@ -337,4 +337,116 @@ SKIP:
   my ($warning) = $warned->tags(name=>'i_warning');
   ok(defined $warning && $warning =~ /unknown field with tag 28712/,
      "check that warning tag set and 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'),
+       "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 }, $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'),
+       "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' }, $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/t106tiff.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 ],
+       [ 'srgba.tif',   4, $photo_rgb  ],
+       [ 'srgbaa.tif',  4, $photo_rgb  ],
+       [ 'scmyk.tif',   3, $photo_cmyk ],
+       [ 'scmyka.tif',  4, $photo_cmyk ],
+       [ 'scmykaa.tif', 4, $photo_cmyk ],
+       [ 'slab.tif',    3, $photo_cielab ],
+      );
+    for my $test (@alpha_images) {
+      my $im = Imager->new;
+      ok($im->read(file => "testimg/$test->[0]"),
+        "read alpha test $test->[0]")
+         or print "# ", $im->errstr, "\n";
+      is($im->getchannels, $test->[1], "channels for $test->[0] match");
+      is($im->tags(name=>'tiff_photometric'), $test->[2],
+        "photometric for $test->[0] match");
+    }
+  }
 }
+