Debian Codegraph @

PNG/t/10png.t

raw
1 #!perl -w
2 use strict;
3 use Imager qw(:all);
4 use Test::More;
5 use Imager::Test qw(test_image_raw test_image is_image is_imaged
6 test_image_16 test_image_double test_image_gray_16);
7
8 my $debug_writes = 1;
9
10 -d "testout" or mkdir "testout";
11
12 init_log("testout/t102png.log",1);
13
14 # this loads Imager::File::PNG too
15 ok($Imager::formats{"png"}, "must have png format");
16
17 diag("Library version " . Imager::File::PNG::i_png_lib_version());
18
19 my %png_feat = map { $_ => 1 } Imager::File::PNG->features;
20
21 my $green = i_color_new(0, 255, 0, 255);
22 my $blue = i_color_new(0, 0, 255, 255);
23 my $red = i_color_new(255, 0, 0, 255);
24
25 my $img = test_image_raw();
26
27 my $timg = Imager::ImgRaw::new(20, 20, 4);
28 my $trans = i_color_new(255, 0, 0, 127);
29 i_box_filled($timg, 0, 0, 20, 20, $green);
30 i_box_filled($timg, 2, 2, 18, 18, $trans);
31
32 Imager::i_tags_add($img, "i_xres", 0, "300", 0);
33 Imager::i_tags_add($img, "i_yres", 0, undef, 200);
34 # the following confuses the GIMP
35 #Imager::i_tags_add($img, "i_aspect_only", 0, undef, 1);
36 open(FH,">testout/t102.png") || die "cannot open testout/t102.png for writing\n";
37 binmode(FH);
38 my $IO = Imager::io_new_fd(fileno(FH));
39 ok(Imager::File::PNG::i_writepng_wiol($img, $IO), "write")
40 or diag(Imager->_error_as_msg());
41 close(FH);
42
43 open(FH,"testout/t102.png") || die "cannot open testout/t102.png\n";
44 binmode(FH);
45 $IO = Imager::io_new_fd(fileno(FH));
46 my $cmpimg = Imager::File::PNG::i_readpng_wiol($IO);
47 close(FH);
48 ok($cmpimg, "read png");
49
50 print "# png average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n";
51 is(i_img_diff($img, $cmpimg), 0, "compare saved and original images");
52
53 my %tags = map { Imager::i_tags_get($cmpimg, $_) }
54 0..Imager::i_tags_count($cmpimg) - 1;
55 ok(abs($tags{i_xres} - 300) < 1, "i_xres: $tags{i_xres}");
56 ok(abs($tags{i_yres} - 200) < 1, "i_yres: $tags{i_yres}");
57 is($tags{i_format}, "png", "i_format: $tags{i_format}");
58
59 open FH, "> testout/t102_trans.png"
60 or die "Cannot open testout/t102_trans.png: $!";
61 binmode FH;
62 $IO = Imager::io_new_fd(fileno(FH));
63 ok(Imager::File::PNG::i_writepng_wiol($timg, $IO), "write tranparent");
64 close FH;
65
66 open FH,"testout/t102_trans.png"
67 or die "cannot open testout/t102_trans.png\n";
68 binmode(FH);
69 $IO = Imager::io_new_fd(fileno(FH));
70 $cmpimg = Imager::File::PNG::i_readpng_wiol($IO);
71 ok($cmpimg, "read transparent");
72 close(FH);
73
74 print "# png average mean square pixel difference: ",sqrt(i_img_diff($timg,$cmpimg))/150*150,"\n";
75 is(i_img_diff($timg, $cmpimg), 0, "compare saved and original transparent");
76
77 # REGRESSION TEST
78 # png.c 1.1 would produce an incorrect image when loading images with
79 # less than 8 bits/pixel with a transparent palette entry
80 open FH, "< testimg/palette.png"
81 or die "cannot open testimg/palette.png: $!\n";
82 binmode FH;
83 $IO = Imager::io_new_fd(fileno(FH));
84 # 1.1 may segfault here (it does with libefence)
85 my $pimg = Imager::File::PNG::i_readpng_wiol($IO);
86 ok($pimg, "read transparent paletted image");
87 close FH;
88
89 open FH, "< testimg/palette_out.png"
90 or die "cannot open testimg/palette_out.png: $!\n";
91 binmode FH;
92 $IO = Imager::io_new_fd(fileno(FH));
93 my $poimg = Imager::File::PNG::i_readpng_wiol($IO);
94 ok($poimg, "read palette_out image");
95 close FH;
96 if (!is(i_img_diff($pimg, $poimg), 0, "images the same")) {
97 print <<EOS;
98 # this tests a bug in Imager's png.c v1.1
99 # if also tickles a bug in libpng before 1.0.5, so you may need to
100 # upgrade libpng
101 EOS
102 }
103
104 { # check file limits are checked
105 my $limit_file = "testout/t102.png";
106 ok(Imager->set_file_limits(reset=>1, width=>149), "set width limit 149");
107 my $im = Imager->new;
108 ok(!$im->read(file=>$limit_file),
109 "should fail read due to size limits");
110 print "# ",$im->errstr,"\n";
111 like($im->errstr, qr/image width/, "check message");
112
113 ok(Imager->set_file_limits(reset=>1, height=>149), "set height limit 149");
114 ok(!$im->read(file=>$limit_file),
115 "should fail read due to size limits");
116 print "# ",$im->errstr,"\n";
117 like($im->errstr, qr/image height/, "check message");
118
119 ok(Imager->set_file_limits(reset=>1, width=>150), "set width limit 150");
120 ok($im->read(file=>$limit_file),
121 "should succeed - just inside width limit");
122 ok(Imager->set_file_limits(reset=>1, height=>150), "set height limit 150");
123 ok($im->read(file=>$limit_file),
124 "should succeed - just inside height limit");
125
126 # 150 x 150 x 3 channel image uses 67500 bytes
127 ok(Imager->set_file_limits(reset=>1, bytes=>67499),
128 "set bytes limit 67499");
129 ok(!$im->read(file=>$limit_file),
130 "should fail - too many bytes");
131 print "# ",$im->errstr,"\n";
132 like($im->errstr, qr/storage size/, "check error message");
133 ok(Imager->set_file_limits(reset=>1, bytes=>67500),
134 "set bytes limit 67500");
135 ok($im->read(file=>$limit_file),
136 "should succeed - just inside bytes limit");
137 Imager->set_file_limits(reset=>1);
138 }
139
140 { # check if the read_multi fallback works
141 my @imgs = Imager->read_multi(file => 'testout/t102.png');
142 is(@imgs, 1, "check the image was loaded");
143 is(i_img_diff($img, $imgs[0]), 0, "check image matches");
144
145 # check the write_multi fallback
146 ok(Imager->write_multi({ file => 'testout/t102m.png', type => 'png' },
147 @imgs),
148 'test write_multi() callback');
149
150 # check that we fail if we actually write 2
151 ok(!Imager->write_multi({ file => 'testout/t102m.png', type => 'png' },
152 @imgs, @imgs),
153 'test write_multi() callback failure');
154 }
155
156 { # check close failures are handled correctly
157 my $im = test_image();
158 my $fail_close = sub {
159 Imager::i_push_error(0, "synthetic close failure");
160 print "# closecb called\n";
161 return 0;
162 };
163 ok(!$im->write(type => "png", callback => sub { 1 },
164 closecb => $fail_close),
165 "check failing close fails");
166 like($im->errstr, qr/synthetic close failure/,
167 "check error message");
168 }
169
170 {
171 ok(grep($_ eq 'png', Imager->read_types), "check png in read types");
172 ok(grep($_ eq 'png', Imager->write_types), "check png in write types");
173 }
174
175 { # read error reporting
176 my $im = Imager->new;
177 ok(!$im->read(file => "testimg/badcrc.png", type => "png"),
178 "read png with bad CRC chunk should fail");
179 is($im->errstr, "IHDR: CRC error", "check error message");
180 }
181
182 SKIP:
183 { # ignoring "benign" errors
184 $png_feat{"benign-errors"}
185 or skip "libpng not configured for benign error support", 3;
186
187 SKIP:
188 {
189 Imager::File::PNG::i_png_lib_version() < 10610
190 or skip "1.6.10 and later treat CRC errors as non-benign", 1;
191 my $im = Imager->new;
192 ok($im->read(file => "testimg/badcrc.png", type => "png",
193 png_ignore_benign_errors => 1),
194 "read bad crc with png_ignore_benign_errors");
195 }
196
197 SKIP:
198 {
199 Imager::File::PNG::i_png_lib_version() >= 10600
200 or skip "unused palette not benign errored before 1.6.0", 2;
201 my $im = Imager->new;
202 ok($im->read(file => "testimg/bipalette.png", type => "png",
203 png_ignore_benign_errors => 1),
204 "read grey image with palette with png_ignore_benign_errors");
205 ok(!$im->read(file => "testimg/bipalette.png", type => "png",
206 png_ignore_benign_errors => 0),
207 "read grey image with palette without png_ignore_benign_errors should fail");
208 }
209 }
210
211 { # write error reporting
212 my $im = test_image();
213 ok(!$im->write(type => "png", callback => limited_write(1), buffered => 0),
214 "write limited to 1 byte should fail");
215 is($im->errstr, "Write error on an iolayer source.: limit reached",
216 "check error message");
217 }
218
219 SKIP:
220 { # https://sourceforge.net/tracker/?func=detail&aid=3314943&group_id=5624&atid=105624
221 # large images
222 Imager::File::PNG::i_png_lib_version() >= 10503
223 or skip("older libpng limits image sizes", 12);
224
225 {
226 my $im = Imager->new(xsize => 1000001, ysize => 1, channels => 1);
227 ok($im, "make a wide image");
228 my $data;
229 ok($im->write(data => \$data, type => "png"),
230 "write wide image as png")
231 or diag("write wide: " . $im->errstr);
232 my $im2 = Imager->new;
233 ok($im->read(data => $data, type => "png"),
234 "read wide image as png")
235 or diag("read wide: " . $im->errstr);
236 is($im->getwidth, 1000001, "check width");
237 is($im->getheight, 1, "check height");
238 is($im->getchannels, 1, "check channels");
239 }
240
241 {
242 my $im = Imager->new(xsize => 1, ysize => 1000001, channels => 1);
243 ok($im, "make a tall image");
244 my $data;
245 ok($im->write(data => \$data, type => "png"),
246 "write wide image as png")
247 or diag("write tall: " . $im->errstr);
248 my $im2 = Imager->new;
249 ok($im->read(data => $data, type => "png"),
250 "read tall image as png")
251 or diag("read tall: " . $im->errstr);
252 is($im->getwidth, 1, "check width");
253 is($im->getheight, 1000001, "check height");
254 is($im->getchannels, 1, "check channels");
255 }
256 }
257
258 { # test grayscale read as greyscale
259 my $im = Imager->new;
260 ok($im->read(file => "testimg/gray.png", type => "png"),
261 "read grayscale");
262 is($im->getchannels, 1, "check channel count");
263 is($im->type, "direct", "check type");
264 is($im->bits, 8, "check bits");
265 is($im->tags(name => "png_bits"), 8, "check png_bits tag");
266 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
267 }
268
269 { # test grayscale + alpha read as greyscale + alpha
270 my $im = Imager->new;
271 ok($im->read(file => "testimg/graya.png", type => "png"),
272 "read grayscale + alpha");
273 is($im->getchannels, 2, "check channel count");
274 is($im->type, "direct", "check type");
275 is($im->bits, 8, "check bits");
276 is($im->tags(name => "png_bits"), 8, "check png_bits tag");
277 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
278 }
279
280 { # test paletted + alpha read as paletted
281 my $im = Imager->new;
282 ok($im->read(file => "testimg/paltrans.png", type => "png"),
283 "read paletted with alpha");
284 is($im->getchannels, 4, "check channel count");
285 is($im->type, "paletted", "check type");
286 is($im->tags(name => "png_bits"), 8, "check png_bits tag");
287 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
288 }
289
290 { # test paletted read as paletted
291 my $im = Imager->new;
292 ok($im->read(file => "testimg/pal.png", type => "png"),
293 "read paletted");
294 is($im->getchannels, 3, "check channel count");
295 is($im->type, "paletted", "check type");
296 is($im->tags(name => "png_bits"), 8, "check png_bits tag");
297 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
298 }
299
300 { # test 16-bit rgb read as 16 bit
301 my $im = Imager->new;
302 ok($im->read(file => "testimg/rgb16.png", type => "png"),
303 "read 16-bit rgb");
304 is($im->getchannels, 3, "check channel count");
305 is($im->type, "direct", "check type");
306 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
307 is($im->bits, 16, "check bits");
308 is($im->tags(name => "png_bits"), 16, "check png_bits tag");
309 }
310
311 { # test 1-bit grey read as mono
312 my $im = Imager->new;
313 ok($im->read(file => "testimg/bilevel.png", type => "png"),
314 "read bilevel png");
315 is($im->getchannels, 1, "check channel count");
316 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
317 is($im->type, "paletted", "check type");
318 ok($im->is_bilevel, "should be bilevel");
319 is($im->tags(name => "png_bits"), 1, "check png_bits tag");
320 }
321
322 SKIP:
323 { # test interlaced read as interlaced and matches original
324 my $im_i = Imager->new(file => "testimg/rgb8i.png", filetype => "png");
325 ok($im_i, "read interlaced")
326 or skip("Could not read rgb8i.png: " . Imager->errstr, 7);
327 is($im_i->getchannels, 3, "check channel count");
328 is($im_i->type, "direct", "check type");
329 is($im_i->tags(name => "png_bits"), 8, "check png_bits");
330 is($im_i->tags(name => "png_interlace"), 1, "check png_interlace");
331
332 my $im = Imager->new(file => "testimg/rgb8.png", filetype => "png");
333 ok($im, "read non-interlaced")
334 or skip("Could not read testimg/rgb8.png: " . Imager->errstr, 2);
335 is($im->tags(name => "png_interlace"), 0, "check png_interlace");
336 is_image($im_i, $im, "compare interlaced and non-interlaced");
337 }
338
339 {
340 my @match =
341 (
342 [ "cover.png", "coveri.png" ],
343 [ "cover16.png", "cover16i.png" ],
344 [ "coverpal.png", "coverpali.png" ],
345 );
346 for my $match (@match) {
347 my ($normal, $interlace) = @$match;
348
349 my $n_im = Imager->new(file => "testimg/$normal");
350 ok($n_im, "read $normal")
351 or diag "reading $normal: ", Imager->errstr;
352 my $i_im = Imager->new(file => "testimg/$interlace");
353 ok($i_im, "read $interlace")
354 or diag "reading $interlace: ", Imager->errstr;
355 SKIP:
356 {
357 $n_im && $i_im
358 or skip("Couldn't read a file", 1);
359 is_image($i_im, $n_im, "check normal and interlace files read the same");
360 }
361 }
362 }
363
364 {
365 my $interlace = 0;
366 for my $name ("cover.png", "coveri.png") {
367 SKIP: {
368 my $im = Imager->new(file => "testimg/$name");
369 ok($im, "read $name")
370 or diag "Failed to read $name: ", Imager->errstr;
371 $im
372 or skip("Couldn't load $name", 5);
373 is($im->tags(name => "i_format"), "png", "$name: i_format");
374 is($im->tags(name => "png_bits"), 8, "$name: png_bits");
375 is($im->tags(name => "png_interlace"), $interlace,
376 "$name: png_interlace");
377 is($im->getchannels, 4, "$name: four channels");
378 is($im->type, "direct", "$name: direct type");
379
380 is_deeply([ $im->getsamples(y => 0, width => 5) ],
381 [ ( 255, 255, 0, 255 ), ( 255, 255, 0, 191 ),
382 ( 255, 255, 0, 127 ), ( 255, 255, 0, 63 ),
383 ( 0, 0, 0, 0) ],
384 "$name: check expected samples row 0");
385 is_deeply([ $im->getsamples(y => 1, width => 5) ],
386 [ ( 255, 0, 0, 255 ), ( 255, 0, 0, 191 ),
387 ( 255, 0, 0, 127 ), ( 255, 0, 0, 63 ),
388 ( 0, 0, 0, 0) ],
389 "$name: check expected samples row 1");
390 }
391 $interlace = 1;
392 }
393 }
394
395 {
396 my $interlace = 0;
397 for my $name ("coverpal.png", "coverpali.png") {
398 SKIP: {
399 my $im = Imager->new(file => "testimg/$name");
400 ok($im, "read $name")
401 or diag "Failed to read $name: ", Imager->errstr;
402 $im
403 or skip("Couldn't load $name", 5);
404 is($im->tags(name => "i_format"), "png", "$name: i_format");
405 is($im->tags(name => "png_bits"), 4, "$name: png_bits");
406 is($im->tags(name => "png_interlace"), $interlace,
407 "$name: png_interlace");
408 is($im->getchannels, 4, "$name: four channels");
409 is($im->type, "paletted", "$name: paletted type");
410
411 is_deeply([ $im->getsamples(y => 0, width => 5) ],
412 [ ( 255, 255, 0, 255 ), ( 255, 255, 0, 191 ),
413 ( 255, 255, 0, 127 ), ( 255, 255, 0, 63 ),
414 ( 0, 0, 0, 0) ],
415 "$name: check expected samples row 0");
416 is_deeply([ $im->getsamples(y => 1, width => 5) ],
417 [ ( 255, 0, 0, 255 ), ( 255, 0, 0, 191 ),
418 ( 255, 0, 0, 127 ), ( 255, 0, 0, 63 ),
419 ( 0, 0, 0, 0) ],
420 "$name: check expected samples row 1");
421 }
422 $interlace = 1;
423 }
424 }
425
426 {
427 my $interlace = 0;
428 for my $name ("cover16.png", "cover16i.png") {
429 SKIP: {
430 my $im = Imager->new(file => "testimg/$name");
431 ok($im, "read $name")
432 or diag "Failed to read $name: ", Imager->errstr;
433 $im
434 or skip("Couldn't load $name", 5);
435 is($im->tags(name => "i_format"), "png", "$name: i_format");
436 is($im->tags(name => "png_bits"), 16, "$name: png_bits");
437 is($im->tags(name => "png_interlace"), $interlace,
438 "$name: png_interlace");
439 is($im->getchannels, 4, "$name: four channels");
440 is($im->type, "direct", "$name: direct type");
441
442 is_deeply([ $im->getsamples(y => 0, width => 5, type => "16bit") ],
443 [ ( 65535, 65535, 0, 65535 ), ( 65535, 65535, 0, 49087 ),
444 ( 65535, 65535, 0, 32639 ), ( 65535, 65535, 0, 16191 ),
445 ( 65535, 65535, 65535, 0) ],
446 "$name: check expected samples row 0");
447 is_deeply([ $im->getsamples(y => 1, width => 5, type => "16bit") ],
448 [ ( 65535, 0, 0, 65535 ), ( 65535, 0, 0, 49087 ),
449 ( 65535, 0, 0, 32639 ), ( 65535, 0, 0, 16191 ),
450 ( 65535, 65535, 65535, 0) ],
451 "$name: check expected samples row 1");
452 }
453 $interlace = 1;
454 }
455 }
456
457 {
458 my $pim = Imager->new(xsize => 5, ysize => 2, channels => 3, type => "paletted");
459 ok($pim, "make a 3 channel paletted image");
460 ok($pim->addcolors(colors => [ qw(000000 FFFFFF FF0000 00FF00 0000FF) ]),
461 "add some colors");
462 is($pim->setscanline(y => 0, type => "index",
463 pixels => [ 0, 1, 2, 4, 3 ]), 5, "set some pixels");
464 is($pim->setscanline(y => 1, type => "index",
465 pixels => [ 4, 1, 0, 4, 2 ]), 5, "set some more pixels");
466 ok($pim->write(file => "testout/pal3.png"),
467 "write to testout/pal3.png")
468 or diag("Cannot save testout/pal3.png: ".$pim->errstr);
469 my $in = Imager->new(file => "testout/pal3.png");
470 ok($in, "read it back in")
471 or diag("Cann't read pal3.png back: " . Imager->errstr);
472 is_image($pim, $in, "check it matches");
473 is($in->type, "paletted", "make sure the result is paletted");
474 is($in->tags(name => "png_bits"), 4, "4 bit representation");
475 }
476
477 {
478 # make sure the code that pushes maxed alpha to the end doesn't break
479 my $pim = Imager->new(xsize => 8, ysize => 2, channels => 4, type => "paletted");
480 ok($pim, "make a 4 channel paletted image");
481 ok($pim->addcolors
482 (colors => [ NC(255, 255, 0, 128), qw(000000 FFFFFF FF0000 00FF00 0000FF),
483 NC(0, 0, 0, 0), NC(255, 0, 128, 64) ]),
484 "add some colors");
485 is($pim->setscanline(y => 0, type => "index",
486 pixels => [ 5, 0, 1, 7, 2, 4, 6, 3 ]), 8,
487 "set some pixels");
488 is($pim->setscanline(y => 1, type => "index",
489 pixels => [ 7, 4, 6, 1, 0, 4, 2, 5 ]), 8,
490 "set some more pixels");
491 ok($pim->write(file => "testout/pal4.png"),
492 "write to testout/pal4.png")
493 or diag("Cannot save testout/pal4.png: ".$pim->errstr);
494 my $in = Imager->new(file => "testout/pal4.png");
495 ok($in, "read it back in")
496 or diag("Cann't read pal4.png back: " . Imager->errstr);
497 is_image($pim, $in, "check it matches");
498 is($in->type, "paletted", "make sure the result is paletted");
499 is($in->tags(name => "png_bits"), 4, "4 bit representation");
500 }
501
502 {
503 my $pim = Imager->new(xsize => 8, ysize => 2, channels => 1, type => "paletted");
504 ok($pim, "make a 1 channel paletted image");
505 ok($pim->addcolors(colors => [ map NC($_, 0, 0), 0, 7, 127, 255 ]),
506 "add some colors^Wgreys");
507 is($pim->setscanline(y => 0, type => "index",
508 pixels => [ 0, 2, 1, 3, 2, 1, 0, 3 ]), 8,
509 "set some pixels");
510 is($pim->setscanline(y => 1, type => "index",
511 pixels => [ 3, 0, 2, 1, 0, 0, 2, 3 ]), 8,
512 "set some more pixels");
513 ok($pim->write(file => "testout/pal1.png"),
514 "write to testout/pal1.png")
515 or diag("Cannot save testout/pal1.png: ".$pim->errstr);
516 my $in = Imager->new(file => "testout/pal1.png");
517 ok($in, "read it back in")
518 or diag("Cann't read pal1.png back: " . Imager->errstr);
519 # PNG doesn't have a paletted greyscale type, so it's written as
520 # paletted color, convert our source image for the comparison
521 my $cmpim = $pim->convert(preset => "rgb");
522 is_image($in, $cmpim, "check it matches");
523 is($in->type, "paletted", "make sure the result is paletted");
524 is($in->tags(name => "png_bits"), 2, "2 bit representation");
525 }
526
527 {
528 my $pim = Imager->new(xsize => 8, ysize => 2, channels => 2, type => "paletted");
529 ok($pim, "make a 2 channel paletted image");
530 ok($pim->addcolors(colors => [ NC(0, 255, 0), NC(128, 255, 0), NC(255, 255, 0), NC(128, 128, 0) ]),
531 "add some colors^Wgreys")
532 or diag("adding colors: " . $pim->errstr);
533 is($pim->setscanline(y => 0, type => "index",
534 pixels => [ 0, 2, 1, 3, 2, 1, 0, 3 ]), 8,
535 "set some pixels");
536 is($pim->setscanline(y => 1, type => "index",
537 pixels => [ 3, 0, 2, 1, 0, 0, 2, 3 ]), 8,
538 "set some more pixels");
539 ok($pim->write(file => "testout/pal2.png"),
540 "write to testout/pal2.png")
541 or diag("Cannot save testout/pal2.png: ".$pim->errstr);
542 my $in = Imager->new(file => "testout/pal2.png");
543 ok($in, "read it back in")
544 or diag("Can't read pal1.png back: " . Imager->errstr);
545 # PNG doesn't have a paletted greyscale type, so it's written as
546 # paletted color, convert our source image for the comparison
547 my $cmpim = $pim->convert(preset => "rgb");
548 is_image($in, $cmpim, "check it matches");
549 is($in->type, "paletted", "make sure the result is paletted");
550 is($in->tags(name => "png_bits"), 2, "2 bit representation");
551 }
552
553 {
554 my $imbase = test_image();
555 my $mono = $imbase->convert(preset => "gray")
556 ->to_paletted(make_colors => "mono", translate => "errdiff");
557
558 ok($mono->write(file => "testout/bilevel.png"),
559 "write bilevel.png");
560 my $in = Imager->new(file => "testout/bilevel.png");
561 ok($in, "read it back in")
562 or diag("Can't read bilevel.png: " . Imager->errstr);
563 is_image($in, $mono, "check it matches");
564 is($in->type, "paletted", "make sure the result is paletted");
565 is($in->tags(name => "png_bits"), 1, "1 bit representation");
566 }
567
568 SKIP:
569 {
570 my $im = test_image_16();
571 ok($im->write(file => "testout/rgb16.png", type => "png"),
572 "write 16-bit/sample image")
573 or diag("Could not write rgb16.png: ".$im->errstr);
574 my $in = Imager->new(file => "testout/rgb16.png")
575 or diag("Could not read rgb16.png: ".Imager->errstr);
576 ok($in, "read rgb16.png back in")
577 or skip("Could not load image to check", 4);
578 is_imaged($in, $im, 0, "check image matches");
579 is($in->bits, 16, "check we got a 16-bit image");
580 is($in->type, "direct", "check it's direct");
581 is($in->tags(name => "png_bits"), 16, "check png_bits");
582 }
583
584 SKIP:
585 {
586 my $im = test_image_double();
587 my $cmp = $im->to_rgb16;
588 ok($im->write(file => "testout/rgbdbl.png", type => "png"),
589 "write double/sample image - should write as 16-bit/sample")
590 or diag("Could not write rgbdbl.png: ".$im->errstr);
591 my $in = Imager->new(file => "testout/rgbdbl.png")
592 or diag("Could not read rgbdbl.png: ".Imager->errstr);
593 ok($in, "read pngdbl.png back in")
594 or skip("Could not load image to check", 4);
595 is_imaged($in, $cmp, 0, "check image matches");
596 is($in->bits, 16, "check we got a 16-bit image");
597 is($in->type, "direct", "check it's direct");
598 is($in->tags(name => "png_bits"), 16, "check png_bits");
599 }
600
601 SKIP:
602 {
603 my $im = Imager->new(file => "testimg/comment.png");
604 ok($im, "read file with comment")
605 or diag("Cannot read comment.png: ".Imager->errstr);
606 $im
607 or skip("Cannot test tags file I can't read", 5);
608 is($im->tags(name => "i_comment"), "Test comment", "check i_comment");
609 is($im->tags(name => "png_interlace"), "0", "no interlace");
610 is($im->tags(name => "png_interlace_name"), "none", "no interlace (text)");
611 is($im->tags(name => "png_srgb_intent"), "0", "srgb perceptual");
612 is($im->tags(name => "png_time"), "2012-04-16T07:37:36",
613 "modification time");
614 is($im->tags(name => "i_background"), "color(255,255,255,255)",
615 "background color");
616 }
617
618 SKIP:
619 { # test tag writing
620 my $im = Imager->new(xsize => 1, ysize => 1);
621 ok($im->write(file => "testout/tags.png",
622 i_comment => "A Comment",
623 png_author => "An Author",
624 png_author_compressed => 1,
625 png_copyright => "A Copyright",
626 png_creation_time => "16 April 2012 22:56:30+1000",
627 png_description => "A Description",
628 png_disclaimer => "A Disclaimer",
629 png_software => "Some Software",
630 png_source => "A Source",
631 png_title => "A Title",
632 png_warning => "A Warning",
633 png_text0_key => "Custom Key",
634 png_text0_text => "Custom Value",
635 png_text0_compressed => 1,
636 png_text1_key => "Custom Key2",
637 png_text1_text => "Another Custom Value",
638 png_time => "2012-04-20T00:15:10",
639 ),
640 "write with many tags")
641 or diag("Cannot write with many tags: ", $im->errstr);
642
643 my $imr = Imager->new(file => "testout/tags.png");
644 ok($imr, "read it back in")
645 or skip("Couldn't read it back: ". Imager->errstr, 1);
646
647 is_deeply({ map @$_, $imr->tags },
648 {
649 i_format => "png",
650 i_comment => "A Comment",
651 png_author => "An Author",
652 png_author_compressed => 1,
653 png_copyright => "A Copyright",
654 png_creation_time => "16 April 2012 22:56:30+1000",
655 png_description => "A Description",
656 png_disclaimer => "A Disclaimer",
657 png_software => "Some Software",
658 png_source => "A Source",
659 png_title => "A Title",
660 png_warning => "A Warning",
661 png_text0_key => "Custom Key",
662 png_text0_text => "Custom Value",
663 png_text0_compressed => 1,
664 png_text0_type => "text",
665 png_text1_key => "Custom Key2",
666 png_text1_text => "Another Custom Value",
667 png_text1_type => "text",
668 png_time => "2012-04-20T00:15:10",
669 png_interlace => 0,
670 png_interlace_name => "none",
671 png_bits => 8,
672 }, "check tags are what we expected");
673 }
674
675 SKIP:
676 { # cHRM test
677 my $im = Imager->new(xsize => 1, ysize => 1);
678 ok($im->write(file => "testout/tagschrm.png", type => "png",
679 png_chroma_white_x => 0.3,
680 png_chroma_white_y => 0.32,
681 png_chroma_red_x => 0.7,
682 png_chroma_red_y => 0.28,
683 png_chroma_green_x => 0.075,
684 png_chroma_green_y => 0.8,
685 png_chroma_blue_x => 0.175,
686 png_chroma_blue_y => 0.05),
687 "write cHRM chunk");
688 my $imr = Imager->new(file => "testout/tagschrm.png", ftype => "png");
689 ok($imr, "read tagschrm.png")
690 or diag("reading tagschrm.png: ".Imager->errstr);
691 $imr
692 or skip("read of tagschrm.png failed", 1);
693 is_deeply({ map @$_, $imr->tags },
694 {
695 i_format => "png",
696 png_interlace => 0,
697 png_interlace_name => "none",
698 png_bits => 8,
699 png_chroma_white_x => 0.3,
700 png_chroma_white_y => 0.32,
701 png_chroma_red_x => 0.7,
702 png_chroma_red_y => 0.28,
703 png_chroma_green_x => 0.075,
704 png_chroma_green_y => 0.8,
705 png_chroma_blue_x => 0.175,
706 png_chroma_blue_y => 0.05,
707 }, "check chroma tags written");
708 }
709
710 { # gAMA
711 my $im = Imager->new(xsize => 1, ysize => 1);
712 ok($im->write(file => "testout/tagsgama.png", type => "png",
713 png_gamma => 2.22),
714 "write with png_gammma tag");
715 my $imr = Imager->new(file => "testout/tagsgama.png", ftype => "png");
716 ok($imr, "read tagsgama.png")
717 or diag("reading tagsgama.png: ".Imager->errstr);
718 $imr
719 or skip("read of tagsgama.png failed", 1);
720 is_deeply({ map @$_, $imr->tags },
721 {
722 i_format => "png",
723 png_interlace => 0,
724 png_interlace_name => "none",
725 png_bits => 8,
726 png_gamma => "2.22",
727 }, "check gamma tag written");
728 }
729
730 { # various bad tag failures
731 my @tests =
732 (
733 [
734 [ png_chroma_white_x => 0.5 ],
735 "all png_chroma_* tags must be supplied or none"
736 ],
737 [
738 [ png_srgb_intent => 4 ],
739 "tag png_srgb_intent out of range"
740 ],
741 [
742 [ i_comment => "test\0with nul" ],
743 "tag i_comment may not contain NUL characters"
744 ],
745 [
746 [ png_text0_key => "" ],
747 "tag png_text0_key must be between 1 and 79 characters in length"
748 ],
749 [
750 [ png_text0_key => ("x" x 80) ],
751 "tag png_text0_key must be between 1 and 79 characters in length"
752 ],
753 [
754 [ png_text0_key => " x" ],
755 "tag png_text0_key may not contain leading or trailing spaces"
756 ],
757 [
758 [ png_text0_key => "x " ],
759 "tag png_text0_key may not contain leading or trailing spaces"
760 ],
761 [
762 [ png_text0_key => "x y" ],
763 "tag png_text0_key may not contain consecutive spaces"
764 ],
765 [
766 [ png_text0_key => "\x7F" ],
767 "tag png_text0_key may only contain Latin1 characters 32-126, 161-255"
768 ],
769 [
770 [ png_text0_key => "x", png_text0_text => "a\0b" ],
771 "tag png_text0_text may not contain NUL characters"
772 ],
773 [
774 [ png_text0_key => "test" ],
775 "tag png_text0_key found but not png_text0_text"
776 ],
777 [
778 [ png_text0_text => "test" ],
779 "tag png_text0_text found but not png_text0_key"
780 ],
781 [
782 [ png_time => "bad format" ],
783 "png_time must be formatted 'y-m-dTh:m:s'"
784 ],
785 [
786 [ png_time => "2012-13-01T00:00:00" ],
787 "invalid date/time for png_time"
788 ],
789 );
790 my $im = Imager->new(xsize => 1, ysize => 1);
791 for my $test (@tests) {
792 my ($tags, $error) = @$test;
793 my $im2 = $im->copy;
794 my $data;
795 ok(!$im2->write(data => \$data, type => "png", @$tags),
796 "expect $error");
797 is($im2->errstr, $error, "check error message");
798 }
799 }
800
801 {
802 # png_compression_level
803 my $im = test_image();
804 my $out1;
805 ok($im->write(data => \$out1, type => "png"), "write default compression");
806 my $out2;
807 ok($im->write(data => \$out2, type => "png", png_compression_level => 9),
808 "write best compression");
809 cmp_ok(length($out1), '>', length($out2), "best compression smaller than default");
810 note "went from ", length($out1), " to ", length($out2);
811 # error handling
812 ok(!$im->write(data => \$out1, type => "png", png_compression_level => -1),
813 "fail with compression level -1");
814 ok(!$im->write(data => \$out1, type => "png", png_compression_level => 10),
815 "fail with compression level 10");
816 }
817
818 {
819 # 499
820 my @pal = map { Imager::Color->new($_) } "#F00", "#00000000", "#00F";
821 my $im1 = Imager->new(xsize => 10, ysize => 10, channels => 4, type => "paletted");
822 $im1->addcolors(colors => \@pal) or die;
823 $im1->box(filled => 1, color => "#00000000");
824 $im1->box(filled => 1, color => "#F00", box => [ 0, 0, 4, 4 ]) or die;
825 my $data;
826 ok($im1->write(data => \$data, type => "png"), "write paletted")
827 or diag($im1->errstr);
828 my $imr = Imager->new;
829 ok($imr->read(data => $data), "read it back")
830 or diag($imr->errstr);
831 is_image($imr, $im1, "check it matches");
832 }
833
834 {
835 my $im = Imager->new(file => "testimg/rgb8trns.png", type => "png");
836 my $ex = Imager->new(file => "testimg/rgb8trnsa.png", type => "png");
837 is_image($im, $ex, "test trns 8-bit rgb parsed properly");
838 }
839
840 {
841 my $im = Imager->new(file => "testimg/rgb16trns.png", type => "png");
842 my $ex = Imager->new(file => "testimg/rgb16trnsa.png", type => "png");
843 is_image($im, $ex, "test trns 16-bit rgb parsed properly");
844 }
845
846 done_testing();
847
848 sub limited_write {
849 my ($limit) = @_;
850
851 return
852 sub {
853 my ($data) = @_;
854 $limit -= length $data;
855 if ($limit >= 0) {
856 print "# write of ", length $data, " bytes successful ($limit left)\n" if $debug_writes;
857 return 1;
858 }
859 else {
860 print "# write of ", length $data, " bytes failed\n";
861 Imager::i_push_error(0, "limit reached");
862 return;
863 }
864 };
865 }
866