5 use Imager::Test qw(test_image_raw test_image is_image);
9 -d "testout" or mkdir "testout";
11 init_log("testout/t102png.log",1);
13 $Imager::formats{"png"}
14 or plan skip_all => "No png support";
18 diag("Library version " . Imager::File::PNG::i_png_lib_version());
20 my $green = i_color_new(0, 255, 0, 255);
21 my $blue = i_color_new(0, 0, 255, 255);
22 my $red = i_color_new(255, 0, 0, 255);
24 my $img = test_image_raw();
26 my $timg = Imager::ImgRaw::new(20, 20, 4);
27 my $trans = i_color_new(255, 0, 0, 127);
28 i_box_filled($timg, 0, 0, 20, 20, $green);
29 i_box_filled($timg, 2, 2, 18, 18, $trans);
31 Imager::i_tags_add($img, "i_xres", 0, "300", 0);
32 Imager::i_tags_add($img, "i_yres", 0, undef, 200);
33 # the following confuses the GIMP
34 #Imager::i_tags_add($img, "i_aspect_only", 0, undef, 1);
35 open(FH,">testout/t102.png") || die "cannot open testout/t102.png for writing\n";
37 my $IO = Imager::io_new_fd(fileno(FH));
38 ok(Imager::File::PNG::i_writepng_wiol($img, $IO), "write");
41 open(FH,"testout/t102.png") || die "cannot open testout/t102.png\n";
43 $IO = Imager::io_new_fd(fileno(FH));
44 my $cmpimg = Imager::File::PNG::i_readpng_wiol($IO);
46 ok($cmpimg, "read png");
48 print "# png average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n";
49 is(i_img_diff($img, $cmpimg), 0, "compare saved and original images");
51 my %tags = map { Imager::i_tags_get($cmpimg, $_) }
52 0..Imager::i_tags_count($cmpimg) - 1;
53 ok(abs($tags{i_xres} - 300) < 1, "i_xres: $tags{i_xres}");
54 ok(abs($tags{i_yres} - 200) < 1, "i_yres: $tags{i_yres}");
55 is($tags{i_format}, "png", "i_format: $tags{i_format}");
57 open FH, "> testout/t102_trans.png"
58 or die "Cannot open testout/t102_trans.png: $!";
60 $IO = Imager::io_new_fd(fileno(FH));
61 ok(Imager::File::PNG::i_writepng_wiol($timg, $IO), "write tranparent");
64 open FH,"testout/t102_trans.png"
65 or die "cannot open testout/t102_trans.png\n";
67 $IO = Imager::io_new_fd(fileno(FH));
68 $cmpimg = Imager::File::PNG::i_readpng_wiol($IO);
69 ok($cmpimg, "read transparent");
72 print "# png average mean square pixel difference: ",sqrt(i_img_diff($timg,$cmpimg))/150*150,"\n";
73 is(i_img_diff($timg, $cmpimg), 0, "compare saved and original transparent");
76 # png.c 1.1 would produce an incorrect image when loading images with
77 # less than 8 bits/pixel with a transparent palette entry
78 open FH, "< testimg/palette.png"
79 or die "cannot open testimg/palette.png: $!\n";
81 $IO = Imager::io_new_fd(fileno(FH));
82 # 1.1 may segfault here (it does with libefence)
83 my $pimg = Imager::File::PNG::i_readpng_wiol($IO);
84 ok($pimg, "read transparent paletted image");
87 open FH, "< testimg/palette_out.png"
88 or die "cannot open testimg/palette_out.png: $!\n";
90 $IO = Imager::io_new_fd(fileno(FH));
91 my $poimg = Imager::File::PNG::i_readpng_wiol($IO);
92 ok($poimg, "read palette_out image");
94 if (!is(i_img_diff($pimg, $poimg), 0, "images the same")) {
96 # this tests a bug in Imager's png.c v1.1
97 # if also tickles a bug in libpng before 1.0.5, so you may need to
102 { # check file limits are checked
103 my $limit_file = "testout/t102.png";
104 ok(Imager->set_file_limits(reset=>1, width=>149), "set width limit 149");
105 my $im = Imager->new;
106 ok(!$im->read(file=>$limit_file),
107 "should fail read due to size limits");
108 print "# ",$im->errstr,"\n";
109 like($im->errstr, qr/image width/, "check message");
111 ok(Imager->set_file_limits(reset=>1, height=>149), "set height limit 149");
112 ok(!$im->read(file=>$limit_file),
113 "should fail read due to size limits");
114 print "# ",$im->errstr,"\n";
115 like($im->errstr, qr/image height/, "check message");
117 ok(Imager->set_file_limits(reset=>1, width=>150), "set width limit 150");
118 ok($im->read(file=>$limit_file),
119 "should succeed - just inside width limit");
120 ok(Imager->set_file_limits(reset=>1, height=>150), "set height limit 150");
121 ok($im->read(file=>$limit_file),
122 "should succeed - just inside height limit");
124 # 150 x 150 x 3 channel image uses 67500 bytes
125 ok(Imager->set_file_limits(reset=>1, bytes=>67499),
126 "set bytes limit 67499");
127 ok(!$im->read(file=>$limit_file),
128 "should fail - too many bytes");
129 print "# ",$im->errstr,"\n";
130 like($im->errstr, qr/storage size/, "check error message");
131 ok(Imager->set_file_limits(reset=>1, bytes=>67500),
132 "set bytes limit 67500");
133 ok($im->read(file=>$limit_file),
134 "should succeed - just inside bytes limit");
135 Imager->set_file_limits(reset=>1);
138 { # check if the read_multi fallback works
139 my @imgs = Imager->read_multi(file => 'testout/t102.png');
140 is(@imgs, 1, "check the image was loaded");
141 is(i_img_diff($img, $imgs[0]), 0, "check image matches");
143 # check the write_multi fallback
144 ok(Imager->write_multi({ file => 'testout/t102m.png', type => 'png' },
146 'test write_multi() callback');
148 # check that we fail if we actually write 2
149 ok(!Imager->write_multi({ file => 'testout/t102m.png', type => 'png' },
151 'test write_multi() callback failure');
154 { # check close failures are handled correctly
155 my $im = test_image();
156 my $fail_close = sub {
157 Imager::i_push_error(0, "synthetic close failure");
158 print "# closecb called\n";
161 ok(!$im->write(type => "png", callback => sub { 1 },
162 closecb => $fail_close),
163 "check failing close fails");
164 like($im->errstr, qr/synthetic close failure/,
165 "check error message");
169 ok(grep($_ eq 'png', Imager->read_types), "check png in read types");
170 ok(grep($_ eq 'png', Imager->write_types), "check png in write types");
173 { # read error reporting
174 my $im = Imager->new;
175 ok(!$im->read(file => "testimg/badcrc.png", type => "png"),
176 "read png with bad CRC chunk should fail");
177 is($im->errstr, "IHDR: CRC error", "check error message");
180 { # write error reporting
181 my $im = test_image();
182 ok(!$im->write(type => "png", callback => limited_write(1), buffered => 0),
183 "write limited to 1 byte should fail");
184 is($im->errstr, "Write error on an iolayer source.: limit reached",
185 "check error message");
189 { # https://sourceforge.net/tracker/?func=detail&aid=3314943&group_id=5624&atid=105624
191 Imager::File::PNG::i_png_lib_version() >= 10503
192 or skip("older libpng limits image sizes", 12);
195 my $im = Imager->new(xsize => 1000001, ysize => 1, channels => 1);
196 ok($im, "make a wide image");
198 ok($im->write(data => \$data, type => "png"),
199 "write wide image as png")
200 or diag("write wide: " . $im->errstr);
201 my $im2 = Imager->new;
202 ok($im->read(data => $data, type => "png"),
203 "read wide image as png")
204 or diag("read wide: " . $im->errstr);
205 is($im->getwidth, 1000001, "check width");
206 is($im->getheight, 1, "check height");
207 is($im->getchannels, 1, "check channels");
211 my $im = Imager->new(xsize => 1, ysize => 1000001, channels => 1);
212 ok($im, "make a tall image");
214 ok($im->write(data => \$data, type => "png"),
215 "write wide image as png")
216 or diag("write tall: " . $im->errstr);
217 my $im2 = Imager->new;
218 ok($im->read(data => $data, type => "png"),
219 "read tall image as png")
220 or diag("read tall: " . $im->errstr);
221 is($im->getwidth, 1, "check width");
222 is($im->getheight, 1000001, "check height");
223 is($im->getchannels, 1, "check channels");
227 { # test grayscale read as greyscale
228 my $im = Imager->new;
229 ok($im->read(file => "testimg/gray.png", type => "png"),
231 is($im->getchannels, 1, "check channel count");
232 is($im->type, "direct", "check type");
233 is($im->bits, 8, "check bits");
234 is($im->tags(name => "png_bits"), 8, "check png_bits tag");
235 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
238 { # test grayscale + alpha read as greyscale + alpha
239 my $im = Imager->new;
240 ok($im->read(file => "testimg/graya.png", type => "png"),
241 "read grayscale + alpha");
242 is($im->getchannels, 2, "check channel count");
243 is($im->type, "direct", "check type");
244 is($im->bits, 8, "check bits");
245 is($im->tags(name => "png_bits"), 8, "check png_bits tag");
246 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
249 { # test paletted + alpha read as paletted
250 my $im = Imager->new;
251 ok($im->read(file => "testimg/paltrans.png", type => "png"),
252 "read paletted with alpha");
253 is($im->getchannels, 4, "check channel count");
254 is($im->type, "paletted", "check type");
255 is($im->tags(name => "png_bits"), 8, "check png_bits tag");
256 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
259 { # test paletted read as paletted
260 my $im = Imager->new;
261 ok($im->read(file => "testimg/pal.png", type => "png"),
263 is($im->getchannels, 3, "check channel count");
264 is($im->type, "paletted", "check type");
265 is($im->tags(name => "png_bits"), 8, "check png_bits tag");
266 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
269 { # test 16-bit rgb read as 16 bit
270 my $im = Imager->new;
271 ok($im->read(file => "testimg/rgb16.png", type => "png"),
273 is($im->getchannels, 3, "check channel count");
274 is($im->type, "direct", "check type");
275 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
276 is($im->bits, 16, "check bits");
277 is($im->tags(name => "png_bits"), 16, "check png_bits tag");
280 { # test 1-bit grey read as mono
281 my $im = Imager->new;
282 ok($im->read(file => "testimg/bilevel.png", type => "png"),
284 is($im->getchannels, 1, "check channel count");
285 is($im->tags(name => "png_interlace"), 0, "check png_interlace tag");
286 is($im->type, "paletted", "check type");
287 ok($im->is_bilevel, "should be bilevel");
288 is($im->tags(name => "png_bits"), 1, "check png_bits tag");
292 { # test interlaced read as interlaced and matches original
293 my $im_i = Imager->new(file => "testimg/rgb8i.png", filetype => "png");
294 ok($im_i, "read interlaced")
295 or skip("Could not read rgb8i.png: " . Imager->errstr, 7);
296 is($im_i->getchannels, 3, "check channel count");
297 is($im_i->type, "direct", "check type");
298 is($im_i->tags(name => "png_bits"), 8, "check png_bits");
299 is($im_i->tags(name => "png_interlace"), "adam7", "check png_interlace");
301 my $im = Imager->new(file => "testimg/rgb8.png", filetype => "png");
302 ok($im, "read non-interlaced")
303 or skip("Could not read testimg/rgb8.png: " . Imager->errstr, 2);
304 is($im->tags(name => "png_interlace"), "0", "check png_interlace");
305 is_image($im_i, $im, "compare interlaced and non-interlaced");
314 $limit -= length $data;
316 print "# write of ", length $data, " bytes successful ($limit left)\n" if $debug_writes;
320 print "# write of ", length $data, " bytes failed\n";
321 Imager::i_push_error(0, "limit reached");