handle a slightly different warning from libtiff 4.x
[imager.git] / TIFF / t / t10tiff.t
CommitLineData
faa9b3e7 1#!perl -w
66614d6e 2use strict;
afee75f6 3use Test::More tests => 217;
b89b3153 4use Imager qw(:all);
e83b349b
TC
5use Imager::Test qw(is_image is_image_similar test_image test_image_16 test_image_double test_image_raw);
6
e5ee047b 7BEGIN { use_ok("Imager::File::TIFF"); }
e83b349b 8
e5ee047b
TC
9-d "testout"
10 or mkdir "testout";
e83b349b 11
4c2d6970 12$|=1; # give us some progress in the test harness
e2cb7e23 13init_log("testout/t106tiff.log",1);
b89b3153 14
66614d6e
TC
15my $green=i_color_new(0,255,0,255);
16my $blue=i_color_new(0,0,255,255);
17my $red=i_color_new(255,0,0,255);
b89b3153 18
e83b349b
TC
19my $img=test_image_raw();
20
e5ee047b 21my $ver_string = Imager::File::TIFF::i_tiff_libversion();
e83b349b
TC
22ok(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");
26diag("libtiff release $full") if $full;
27# make something we can compare
28my $cmp_ver = sprintf("%03d%03d%03d", $major, $minor, $point);
29if ($cmp_ver lt '003007000') {
30 diag("You have an old version of libtiff - $full, some tests will be skipped");
31}
bd8052a6 32
e83b349b
TC
33Imager::i_tags_add($img, "i_xres", 0, "300", 0);
34Imager::i_tags_add($img, "i_yres", 0, undef, 250);
35# resolutionunit is centimeters
36Imager::i_tags_add($img, "tiff_resolutionunit", 0, undef, 3);
37Imager::i_tags_add($img, "tiff_software", 0, "t106tiff.t", 0);
38open(FH,">testout/t106.tiff") || die "cannot open testout/t106.tiff for writing\n";
39binmode(FH);
40my $IO = Imager::io_new_fd(fileno(FH));
e5ee047b 41ok(Imager::File::TIFF::i_writetiff_wiol($img, $IO), "write low level")
e83b349b
TC
42 or print "# ", Imager->_error_as_msg, "\n";
43close(FH);
44
45open(FH,"testout/t106.tiff") or die "cannot open testout/t106.tiff\n";
46binmode(FH);
47$IO = Imager::io_new_fd(fileno(FH));
e5ee047b 48my $cmpimg = Imager::File::TIFF::i_readtiff_wiol($IO);
e83b349b
TC
49ok($cmpimg, "read low-level");
50
51close(FH);
52
53print "# tiff average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n";
54
55ok(!i_img_diff($img, $cmpimg), "compare written and read image");
56
57# check the tags are ok
58my %tags = map { Imager::i_tags_get($cmpimg, $_) }
59 0 .. Imager::i_tags_count($cmpimg) - 1;
60ok(abs($tags{i_xres} - 300) < 0.5, "i_xres in range");
61ok(abs($tags{i_yres} - 250) < 0.5, "i_yres in range");
62is($tags{tiff_resolutionunit}, 3, "tiff_resolutionunit");
63is($tags{tiff_software}, 't106tiff.t', "tiff_software");
64is($tags{tiff_photometric}, 2, "tiff_photometric"); # PHOTOMETRIC_RGB is 2
65is($tags{tiff_bitspersample}, 8, "tiff_bitspersample");
66
67$IO = Imager::io_new_bufchain();
68
e5ee047b 69ok(Imager::File::TIFF::i_writetiff_wiol($img, $IO), "write to buffer chain");
e83b349b
TC
70my $tiffdata = Imager::io_slurp($IO);
71
72open(FH,"testout/t106.tiff");
73binmode FH;
74my $odata;
75{ local $/;
76 $odata = <FH>;
77}
66614d6e 78
e83b349b
TC
79is($odata, $tiffdata, "same data in file as in memory");
80
81# test Micksa's tiff writer
82# a shortish fax page
83my $faximg = Imager::ImgRaw::new(1728, 2000, 1);
84my $black = i_color_new(0,0,0,255);
85my $white = i_color_new(255,255,255,255);
86# vaguely test-patterny
87i_box_filled($faximg, 0, 0, 1728, 2000, $white);
88i_box_filled($faximg, 100,100,1628, 200, $black);
89my $width = 1;
90my $pos = 100;
91while ($width+$pos < 1628) {
92 i_box_filled($faximg, $pos, 300, $pos+$width-1, 400, $black);
93 $pos += $width + 20;
94 $width += 2;
95}
96open FH, "> testout/t106tiff_fax.tiff"
97 or die "Cannot create testout/t106tiff_fax.tiff: $!";
98binmode FH;
99$IO = Imager::io_new_fd(fileno(FH));
e5ee047b 100ok(Imager::File::TIFF::i_writetiff_wiol_faxable($faximg, $IO, 1), "write faxable, low level");
e83b349b
TC
101close FH;
102
103# test the OO interface
104my $ooim = Imager->new;
105ok($ooim->read(file=>'testout/t106.tiff'), "read OO");
106ok($ooim->write(file=>'testout/t106_oo.tiff'), "write OO");
107
108# OO with the fax image
109my $oofim = Imager->new;
110ok($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;
115is($tags{i_xres}, 204, "fax i_xres");
116is($tags{i_yres}, 196, "fax i_yres");
117ok(!$tags{i_aspect_only}, "i_aspect_only");
118# resunit_inches
119is($tags{tiff_resolutionunit}, 2, "tiff_resolutionunit");
120is($tags{tiff_bitspersample}, 1, "tiff_bitspersample");
121is($tags{tiff_photometric}, 0, "tiff_photometric");
122
123ok($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
127my $oodata;
128ok(!$ooim->write(data=>\$oodata), "write with no type and no filename to guess with");
129
130# OO to data
131ok($ooim->write(data=>\$oodata, type=>'tiff'), "write to data")
132 or print "# ",$ooim->errstr, "\n";
133is($oodata, $tiffdata, "check data matches between memory and file");
134
135# make sure we can write non-fine mode
136ok($oofim->write(file=>'testout/t106_oo_faxlo.tiff', class=>'fax', fax_fine=>0), "write OO, fax standard mode");
137
138# paletted reads
139my $img4 = Imager->new;
140ok($img4->read(file=>'testimg/comp4.tif'), "reading 4-bit paletted")
141 or print "# ", $img4->errstr, "\n";
142is($img4->type, 'paletted', "image isn't paletted");
143print "# 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
149my $bmp4 = Imager->new;
150ok($bmp4->read(file=>'testimg/comp4.bmp'), "reading 4-bit bmp!");
151my $diff = i_img_diff($img4->{IMG}, $bmp4->{IMG});
152print "# diff $diff\n";
153ok($diff == 0, "image mismatch");
154my $img4t = Imager->new;
155ok($img4t->read(file => 'testimg/comp4t.tif'), "read 4-bit paletted, tiled")
156 or print "# ", $img4t->errstr, "\n";
157is_image($bmp4, $img4t, "check tiled version matches");
158my $img8 = Imager->new;
159ok($img8->read(file=>'testimg/comp8.tif'), "reading 8-bit paletted");
160is($img8->type, 'paletted', "image isn't paletted");
161print "# colors: ", $img8->colorcount,"\n";
162#ok($img8->write(file=>'testout/t106_was8.ppm'),
163# "Cannot write img8");
164ok($img8->colorcount == 256, "more colors than expected");
165my $bmp8 = Imager->new;
166ok($bmp8->read(file=>'testimg/comp8.bmp'), "reading 8-bit bmp!");
167$diff = i_img_diff($img8->{IMG}, $bmp8->{IMG});
168print "# diff $diff\n";
169ok($diff == 0, "image mismatch");
170my $bad = Imager->new;
171ok($bad->read(file=>'testimg/comp4bad.tif',
172 allow_incomplete=>1), "bad image not returned");
173ok(scalar $bad->tags(name=>'i_incomplete'), "incomplete tag not set");
174ok($img8->write(file=>'testout/t106_pal8.tif'), "writing 8-bit paletted");
175my $cmp8 = Imager->new;
176ok($cmp8->read(file=>'testout/t106_pal8.tif'),
177 "reading 8-bit paletted");
178#print "# ",$cmp8->errstr,"\n";
179is($cmp8->type, 'paletted', "pal8 isn't paletted");
180is($cmp8->colorcount, 256, "pal8 bad colorcount");
181$diff = i_img_diff($img8->{IMG}, $cmp8->{IMG});
182print "# diff $diff\n";
183ok($diff == 0, "written image doesn't match read");
184ok($img4->write(file=>'testout/t106_pal4.tif'), "writing 4-bit paletted");
185ok(my $cmp4 = Imager->new->read(file=>'testout/t106_pal4.tif'),
186 "reading 4-bit paletted");
187is($cmp4->type, 'paletted', "pal4 isn't paletted");
188is($cmp4->colorcount, 16, "pal4 bad colorcount");
189$diff = i_img_diff($img4->{IMG}, $cmp4->{IMG});
190print "# diff $diff\n";
191ok($diff == 0, "written image doesn't match read");
192
193my $work;
194my $seekpos;
195sub io_writer {
196 my ($what) = @_;
197 if ($seekpos > length $work) {
198 $work .= "\0" x ($seekpos - length $work);
b89b3153 199 }
e83b349b
TC
200 substr($work, $seekpos, length $what) = $what;
201 $seekpos += length $what;
b89b3153 202
e83b349b
TC
203 1;
204}
205sub 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}
212sub 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}
219use IO::Seekable;
220sub io_seeker {
221 my ($offset, $whence) = @_;
222 #print "io_seeker($offset, $whence)\n";
223 if ($whence == SEEK_SET) {
224 $seekpos = $offset;
10461f9a 225 }
e83b349b
TC
226 elsif ($whence == SEEK_CUR) {
227 $seekpos += $offset;
10461f9a 228 }
e83b349b
TC
229 else { # SEEK_END
230 $seekpos = length($work) + $offset;
10461f9a 231 }
e83b349b
TC
232 #print "-> $seekpos\n";
233 $seekpos;
234}
235my $did_close;
236sub io_closer {
237 ++$did_close;
238}
10461f9a 239
e83b349b
TC
240# read via cb
241$work = $tiffdata;
242$seekpos = 0;
243my $IO2 = Imager::io_new_cb(undef, \&io_reader, \&io_seeker, undef);
244ok($IO2, "new readcb obj");
e5ee047b 245my $img5 = Imager::File::TIFF::i_readtiff_wiol($IO2);
e83b349b
TC
246ok($img5, "read via cb");
247ok(i_img_diff($img5, $img) == 0, "read from cb diff");
248
249# read via cb2
250$work = $tiffdata;
251$seekpos = 0;
252my $IO3 = Imager::io_new_cb(undef, \&io_reader2, \&io_seeker, undef);
253ok($IO3, "new readcb2 obj");
e5ee047b 254my $img6 = Imager::File::TIFF::i_readtiff_wiol($IO3);
e83b349b
TC
255ok($img6, "read via cb2");
256ok(i_img_diff($img6, $img) == 0, "read from cb2 diff");
257
258# write via cb
259$work = '';
260$seekpos = 0;
261my $IO4 = Imager::io_new_cb(\&io_writer, \&io_reader, \&io_seeker,
262 \&io_closer);
263ok($IO4, "new writecb obj");
e5ee047b 264ok(Imager::File::TIFF::i_writetiff_wiol($img, $IO4), "write to cb");
e83b349b
TC
265is($work, $odata, "write cb match");
266ok($did_close, "write cb did close");
267open D1, ">testout/d1.tiff" or die;
268print D1 $work;
269close D1;
270open D2, ">testout/d2.tiff" or die;
271print D2 $tiffdata;
272close D2;
273
274# write via cb2
275$work = '';
276$seekpos = 0;
277$did_close = 0;
278my $IO5 = Imager::io_new_cb(\&io_writer, \&io_reader, \&io_seeker,
279 \&io_closer, 1);
280ok($IO5, "new writecb obj 2");
e5ee047b 281ok(Imager::File::TIFF::i_writetiff_wiol($img, $IO5), "write to cb2");
e83b349b
TC
282is($work, $odata, "write cb2 match");
283ok($did_close, "write cb2 did close");
284
285open D3, ">testout/d3.tiff" or die;
286print D3 $work;
287close D3;
288
289# multi-image write/read
290my @imgs;
291push(@imgs, map $ooim->copy(), 1..3);
292for my $i (0..$#imgs) {
293 $imgs[$i]->addtag(name=>"tiff_pagename", value=>"Page ".($i+1));
294}
295my $rc = Imager->write_multi({file=>'testout/t106_multi.tif'}, @imgs);
296ok($rc, "writing multiple images to tiff");
297my @out = Imager->read_multi(file=>'testout/t106_multi.tif');
298ok(@out == @imgs, "reading multiple images from tiff");
299@out == @imgs or print "# ",scalar @out, " ",Imager->errstr,"\n";
300for 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}
10461f9a 307
e83b349b
TC
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);
312ok($rc, "writing 40 images to tiff");
313@out = Imager->read_multi(file=>'testout/t106_multi2.tif');
314ok(@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
319ok(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');
322ok(@imgs == 2, "reading multipage fax");
323ok(Imager::i_img_diff($imgs[0]{IMG}, $oofim->{IMG}) == 0,
324 "compare first fax image");
325ok(Imager::i_img_diff($imgs[1]{IMG}, $oofim->{IMG}) == 0,
326 "compare second fax image");
327
328my ($format) = $imgs[0]->tags(name=>'i_format');
329is($format, 'tiff', "check i_format tag");
330
331my $unit = $imgs[0]->tags(name=>'tiff_resolutionunit');
332ok(defined $unit && $unit == 2, "check tiff_resolutionunit tag");
333my $unitname = $imgs[0]->tags(name=>'tiff_resolutionunit_name');
334is($unitname, 'inch', "check tiff_resolutionunit_name tag");
335
336my $warned = Imager->new;
337ok($warned->read(file=>"testimg/tiffwarn.tif"), "read tiffwarn.tif");
338my ($warning) = $warned->tags(name=>'i_warning');
afee75f6
TC
339ok(defined $warning, "check warning is set");
340like($warning, qr/[Uu]nknown field with tag 28712/,
341 "check that warning tag correct");
e83b349b
TC
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}
2691d220 371
e83b349b
TC
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}
2691d220 397
e83b349b
TC
398{ # test reading returns an error correctly - use test script as an
399 # invalid TIFF file
400 my $im = Imager->new;
e5ee047b 401 ok(!$im->read(file=>'t/t10tiff.t', type=>'tiff'),
e83b349b
TC
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}
2b405c9e 414
e83b349b
TC
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}
a50608d2 428
e83b349b
TC
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");
a50608d2
TC
462 }
463 }
e83b349b 464}
f245645a 465
e83b349b
TC
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}
bd8052a6 470
e83b349b
TC
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}
bd8052a6 480
e83b349b
TC
481SKIP:
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}
bd8052a6 509
e83b349b
TC
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;
ff97aa8e 515 ok($im16t->read(file => 'testimg/rgb16t.tif'), "read 16-bit rgb tiled");
e83b349b
TC
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}
bd8052a6 538
e83b349b
TC
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");
ff97aa8e
TC
561
562 # tiled, non-contig, should fallback to RGBA code
563 my $rgbatsep = Imager->new;
564 ok($rgbatsep->read(file => 'testimg/rgbatsep.tif'),
5b9f7f08
TC
565 "read tiled, separated rgba image")
566 or diag($rgbatsep->errstr);
ff97aa8e 567 is_image($rgba, $rgbatsep, "check they match");
e83b349b
TC
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}
bd8052a6 578
e83b349b
TC
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) {
bd8052a6 606 $ok = 0;
e83b349b 607 $msg = "($x,$y)[$ch] color mismatch got $c[$ch] vs expected $expect->[$ch]";
bd8052a6
TC
608 last CHECKER;
609 }
bd8052a6
TC
610 }
611 }
bd8052a6 612 }
e83b349b
TC
613 ok($ok, $msg);
614}
bd8052a6 615
e83b349b
TC
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;
bd8052a6
TC
637 }
638 }
bd8052a6 639 }
e83b349b
TC
640 ok($ok, $msg);
641}
bd8052a6 642
e83b349b
TC
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");
bd8052a6 654 is($im->tags(name => 'tiff_compression'), 'none', "no compression");
e83b349b
TC
655 is($im->getchannels, 3, 'correct channels');
656}
bd8052a6 657
e83b349b
TC
658{ # 8-bit writes
659 # and check compression
e5ee047b 660 my $compress = Imager::File::TIFF::i_tiff_has_compression('lzw') ? 'lzw' : 'packbits';
e83b349b
TC
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}
bd8052a6 678
e83b349b
TC
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}
bd8052a6 695
e83b349b
TC
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}
bd8052a6 725
e83b349b 726{ # fallback handling of tiff
e5ee047b
TC
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");
b89b3153 729}
2b405c9e 730
e83b349b 731