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