Debian Codegraph @

GIF/t/t10gif.t

raw
1 #!perl -w
2
3 use strict;
4 $|=1;
5 use Test::More;
6 use Imager qw(:all);
7 use Imager::Test qw(is_color3 test_image test_image_raw test_image_mono);
8 use Imager::File::GIF;
9
10 use Carp 'confess';
11 $SIG{__DIE__} = sub { confess @_ };
12
13 -d "testout" or mkdir "testout";
14
15 init_log("testout/t105gif.log",1);
16
17 plan tests => 147;
18
19 my $green=i_color_new(0,255,0,255);
20 my $blue=i_color_new(0,0,255,255);
21 my $red=i_color_new(255,0,0,255);
22
23 my $img=test_image_raw;
24
25 my $gifver = Imager::File::GIF::i_giflib_version();
26 diag("giflib version (from header) $gifver");
27
28 {
29 open(FH,">testout/t105.gif") || die "Cannot open testout/t105.gif\n";
30 binmode(FH);
31 my $io = Imager::io_new_fd(fileno(FH));
32 ok(Imager::File::GIF::i_writegif_wiol($io, {}, $img), "write low") or
33 die "Cannot write testout/t105.gif\n";
34 $io->close;
35 close(FH);
36 }
37
38 {
39 open(FH,"testout/t105.gif") || die "Cannot open testout/t105.gif\n";
40 binmode(FH);
41 my $io = Imager::io_new_fd(fileno(FH));
42 ok($img=Imager::File::GIF::i_readgif_wiol($io), "read low")
43 or die "Cannot read testout/t105.gif\n";
44 close(FH);
45 }
46
47 {
48 open(FH,"testout/t105.gif") || die "Cannot open testout/t105.gif\n";
49 binmode(FH);
50 my $io = Imager::io_new_fd(fileno(FH));
51 ($img, my $palette)=Imager::File::GIF::i_readgif_wiol($io);
52 ok($img, "read palette") or die "Cannot read testout/t105.gif\n";
53 close(FH);
54 $palette=''; # just to skip a warning.
55 }
56
57
58 # check that reading interlaced/non-interlaced versions of
59 # the same GIF produce the same image
60 # I could replace this with code that used Imager's built-in
61 # image comparison code, but I know this code revealed the error
62 {
63 open(FH, "<testimg/scalei.gif") || die "Cannot open testimg/scalei.gif";
64 binmode FH;
65 my $io = Imager::io_new_fd(fileno(FH));
66 my ($imgi) = Imager::File::GIF::i_readgif_wiol($io);
67 ok($imgi, "read interlaced") or die "Cannot read testimg/scalei.gif";
68 close FH;
69
70 open FH, "<testimg/scale.gif" or die "Cannot open testimg/scale.gif";
71 binmode FH;
72 $io = Imager::io_new_fd(fileno(FH));
73 my ($imgni) = Imager::File::GIF::i_readgif_wiol($io);
74 ok($imgni, "read normal") or die "Cannot read testimg/scale.gif";
75 close FH;
76
77 open FH, ">testout/t105i.ppm" or die "Cannot create testout/t105i.ppm";
78 binmode FH;
79 my $IO = Imager::io_new_fd( fileno(FH) );
80 i_writeppm_wiol($imgi, $IO)
81 or die "Cannot write testout/t105i.ppm";
82 close FH;
83
84 open FH, ">testout/t105ni.ppm" or die "Cannot create testout/t105ni.ppm";
85 binmode FH;
86 $IO = Imager::io_new_fd( fileno(FH) );
87 i_writeppm_wiol($imgni, $IO)
88 or die "Cannot write testout/t105ni.ppm";
89 close FH;
90
91 # compare them
92 open FH, "<testout/t105i.ppm" or die "Cannot open testout/t105i.ppm";
93 my $datai = do { local $/; <FH> };
94 close FH;
95
96 open FH, "<testout/t105ni.ppm" or die "Cannot open testout/t105ni.ppm";
97 my $datani = do { local $/; <FH> };
98 close FH;
99 is($datai, $datani, "images match");
100 }
101
102 {
103 # reading with a callback
104 # various sizes to make sure the buffering works
105 # requested size
106 open FH, "<testimg/scale.gif" or die "Cannot open testimg/scale.gif";
107 binmode FH;
108 my $io = Imager::io_new_cb
109 (
110 undef,
111 sub { my $tmp; read(FH, $tmp, $_[0]) and $tmp },
112 undef,
113 undef
114 );
115 # no callback version in giflib3, so don't overwrite a good image
116 my $img2 = Imager::File::GIF::i_readgif_wiol($io);
117 close FH;
118 ok($img, "reading with a callback");
119
120 ok(test_readgif_cb(1), "read callback 1 char buffer");
121 ok(test_readgif_cb(512), "read callback 512 char buffer");
122 ok(test_readgif_cb(1024), "read callback 1024 char buffer");
123 }
124 # new writegif_gen
125 # test webmap, custom errdiff map
126 # (looks fairly awful)
127 {
128 open FH, ">testout/t105_gen.gif" or die $!;
129 binmode FH;
130 my $io = Imager::io_new_fd(fileno(FH));
131 ok(Imager::File::GIF::i_writegif_wiol
132 ($io, { make_colors=>'webmap',
133 translate=>'errdiff',
134 errdiff=>'custom',
135 errdiff_width=>2,
136 errdiff_height=>2,
137 errdiff_map=>[0, 1, 1, 0]}, $img),
138 "webmap, custom errdif map");
139 close FH;
140 }
141
142 print "# the following tests are fairly slow\n";
143
144 # test animation, mc_addi, error diffusion, ordered transparency
145 my @imgs;
146 my $sortagreen = i_color_new(0, 255, 0, 63);
147 for my $i (0..4) {
148 my $im = Imager::ImgRaw::new(200, 200, 4);
149 _add_tags($im, gif_delay=>50, gif_disposal=>2);
150 for my $j (0..$i-1) {
151 my $fill = i_color_new(0, 128, 0, 255 * ($i-$j)/$i);
152 i_box_filled($im, 0, $j*40, 199, $j*40+40, $fill);
153 }
154 i_box_filled($im, 0, $i*40, 199, 199, $blue);
155 push(@imgs, $im);
156 }
157 {
158 my @gif_delays = (50) x 5;
159 my @gif_disposal = (2) x 5;
160 open FH, ">testout/t105_anim.gif" or die $!;
161 binmode FH;
162 my $io = Imager::io_new_fd(fileno(FH));
163 ok(Imager::File::GIF::i_writegif_wiol
164 ($io, { make_colors=>'addi',
165 translate=>'closest',
166 gif_delays=>\@gif_delays,
167 gif_disposal=>\@gif_disposal,
168 gif_positions=> [ map [ $_*10, $_*10 ], 0..4 ],
169 gif_user_input=>[ 1, 0, 1, 0, 1 ],
170 transp=>'ordered',
171 'tr_orddith'=>'dot8'}, @imgs),
172 "write anim gif");
173 close FH;
174 }
175
176 @imgs = ();
177 my $c = i_color_new(0,0,0,0);
178 for my $g (0..3) {
179 my $im = Imager::ImgRaw::new(200, 200, 3);
180 _add_tags($im, gif_local_map=>1, gif_delay=>150, gif_loop=>10);
181 for my $x (0 .. 39) {
182 for my $y (0 .. 39) {
183 $c->set($x * 6, $y * 6, 32*$g+$x+$y, 255);
184 i_box_filled($im, $x*5, $y*5, $x*5+4, $y*5+4, $c);
185 }
186 }
187 push(@imgs, $im);
188 }
189 # test giflib with multiple palettes
190 # (it was meant to test the NS loop extension too, but that's broken)
191 # this looks better with make_colors=>'addi', translate=>'errdiff'
192 # this test aims to overload the palette for each image, so the
193 # output looks moderately horrible
194 {
195 open FH, ">testout/t105_mult_pall.gif" or die "Cannot create file: $!";
196 binmode FH;
197 my $io = Imager::io_new_fd(fileno(FH));
198 ok(Imager::File::GIF::i_writegif_wiol
199 ($io, { #make_colors=>'webmap',
200 translate=>'giflib',
201 }, @imgs), "write multiple palettes")
202 or print "# ", join(":", map $_->[1], Imager::i_errors()),"\n";
203 close FH;
204 }
205
206 {
207 # regression test: giflib doesn't like 1 colour images
208 my $img1 = Imager::ImgRaw::new(100, 100, 3);
209 i_box_filled($img1, 0, 0, 100, 100, $red);
210 open FH, ">testout/t105_onecol.gif" or die $!;
211 binmode FH;
212 my $io = Imager::io_new_fd(fileno(FH));
213 ok(Imager::File::GIF::i_writegif_wiol($io, { translate=>'giflib'}, $img1),
214 "single colour write regression");
215 close FH;
216 }
217
218 {
219 # transparency test
220 # previously it was harder do write transparent images
221 # tests the improvements
222 my $timg = Imager::ImgRaw::new(20, 20, 4);
223 my $trans = i_color_new(255, 0, 0, 127);
224 i_box_filled($timg, 0, 0, 20, 20, $green);
225 i_box_filled($timg, 2, 2, 18, 18, $trans);
226 open FH, ">testout/t105_trans.gif" or die $!;
227 binmode FH;
228 my $io = Imager::io_new_fd(fileno(FH));
229 ok(Imager::File::GIF::i_writegif_wiol
230 ($io, { make_colors=>'addi',
231 translate=>'closest',
232 transp=>'ordered',
233 }, $timg), "write transparent");
234 close FH;
235 }
236
237 # some error handling tests
238 # open a file handle for read and try to save to it
239 # is this idea portable?
240 # whether or not it is, giflib segfaults on this <sigh>
241 #open FH, "<testout/t105_trans.gif" or die $!;
242 #binmode FH; # habit, I suppose
243 #if (i_writegif_gen(fileno(FH), {}, $timg)) {
244 # # this is meant to _fail_
245 # print "not ok 18 # writing to read-only should fail";
246 #}
247 #else {
248 # print "ok 18 # ",Imager::_error_as_msg(),"\n";
249 #}
250 #close FH;
251
252 {
253 # try to read a file of the wrong format - the script will do
254 open FH, "<t/t10gif.t"
255 or die "Cannot open this script!: $!";
256 binmode FH;
257 my $io = Imager::io_new_fd(fileno(FH));
258 ok(!Imager::File::GIF::i_readgif_wiol($io),
259 "read test script as gif should fail ". Imager::_error_as_msg());
260 close FH;
261 }
262
263 {
264 # try to save no images :)
265 open FH, ">testout/t105_none.gif"
266 or die "Cannot open testout/t105_none.gif: $!";
267 binmode FH;
268 my $io = Imager::io_new_fd(fileno(FH));
269 if (ok(!Imager::File::GIF::i_writegif_wiol($io, {}, "hello"), "shouldn't be able to write a string as a gif")) {
270 print "# ",Imager::_error_as_msg(),"\n";
271 }
272 }
273
274 # try to read a truncated gif (no image descriptors)
275 read_failure('testimg/trimgdesc.gif');
276 # file truncated just after the image descriptor tag
277 read_failure('testimg/trmiddesc.gif');
278 # image has no colour map
279 read_failure('testimg/nocmap.gif');
280
281 {
282 # image has a local colour map
283 open FH, "< testimg/loccmap.gif"
284 or die "Cannot open testimg/loccmap.gif: $!";
285 binmode FH;
286 my $io = Imager::io_new_fd(fileno(FH));
287 ok(Imager::File::GIF::i_readgif_wiol($io),
288 "read an image with only a local colour map");
289 close FH;
290 }
291
292 {
293 # image has global and local colour maps
294 open FH, "< testimg/screen2.gif"
295 or die "Cannot open testimg/screen2.gif: $!";
296 binmode FH;
297 my $io = Imager::io_new_fd(fileno(FH));
298 my $ims = Imager::File::GIF::i_readgif_wiol($io);
299 unless (ok($ims, "read an image with global and local colour map")) {
300 print "# ",Imager::_error_as_msg(),"\n";
301 }
302 close FH;
303
304 open FH, "< testimg/expected.gif"
305 or die "Cannot open testimg/expected.gif: $!";
306 binmode FH;
307 $io = Imager::io_new_fd(fileno(FH));
308 my $ime = Imager::File::GIF::i_readgif_wiol($io);
309 close FH;
310 ok($ime, "reading testimg/expected.gif");
311 SKIP:
312 {
313 skip("could not read one or both of expected.gif or loccamp.gif", 1)
314 unless $ims and $ime;
315 unless (is(i_img_diff($ime, $ims), 0,
316 "compare loccmap and expected")) {
317 # save the bad one
318 open FH, "> testout/t105_screen2.gif"
319 or die "Cannot create testout/t105_screen.gif: $!";
320 binmode FH;
321 my $io = Imager::io_new_fd(fileno(FH));
322 Imager::File::GIF::i_writegif_wiol($io, {}, $ims)
323 or print "# could not save t105_screen.gif\n";
324 close FH;
325 }
326 }
327 }
328
329 {
330 # test reading a multi-image file into multiple images
331 open FH, "< testimg/screen2.gif"
332 or die "Cannot open testimg/screen2.gif: $!";
333 binmode FH;
334 my $io = Imager::io_new_fd(fileno(FH));
335 @imgs = Imager::File::GIF::i_readgif_multi_wiol($io);
336 ok(@imgs, "read multi-image file into multiple images");
337 close FH;
338 is(@imgs, 2, "should be 2 images");
339 my $paletted = 1;
340 for my $img (@imgs) {
341 unless (Imager::i_img_type($img) == 1) {
342 $paletted = 0;
343 last;
344 }
345 }
346 ok($paletted, "both images should be paletted");
347 is(Imager::i_colorcount($imgs[0]), 4, "4 colours in first image");
348 is(Imager::i_colorcount($imgs[1]), 2, "2 colours in second image");
349 ok(Imager::i_tags_find($imgs[0], "gif_left", 0),
350 "gif_left tag should be there");
351 my @tags = map {[ Imager::i_tags_get($imgs[1], $_) ]} 0..Imager::i_tags_count($imgs[1])-1;
352 my ($left) = grep $_->[0] eq 'gif_left', @tags;
353 ok($left && $left->[1] == 3, "check gif_left value");
354 }
355
356 {
357 # screen3.gif was saved with
358 open FH, "< testimg/screen3.gif"
359 or die "Cannot open testimg/screen3.gif: $!";
360 binmode FH;
361 my $io = Imager::io_new_fd(fileno(FH));
362 @imgs = Imager::File::GIF::i_readgif_multi_wiol($io);
363 ok(@imgs, "read screen3.gif");
364 close FH;
365 eval {
366 require 'Data/Dumper.pm';
367 Data::Dumper->import();
368 };
369
370 unless ($@) {
371 # build a big map of all tags for all images
372 my @tags =
373 map {
374 my $im = $_;
375 [
376 map { join ",", map { defined() ? $_ : "undef" } Imager::i_tags_get($im, $_) }
377 0..Imager::i_tags_count($_)-1
378 ]
379 } @imgs;
380 my $dump = Dumper(\@tags);
381 $dump =~ s/^/# /mg;
382 print "# tags from gif\n", $dump;
383 }
384
385 # at this point @imgs should contain only paletted images
386 ok(Imager::i_img_type($imgs[0]) == 1, "imgs[0] paletted");
387 ok(Imager::i_img_type($imgs[1]) == 1, "imgs[1] paletted");
388
389 # see how we go saving it
390 open FH, ">testout/t105_pal.gif" or die $!;
391 binmode FH;
392 $io = Imager::io_new_fd(fileno(FH));
393 ok(Imager::File::GIF::i_writegif_wiol
394 ($io, { make_colors=>'addi',
395 translate=>'closest',
396 transp=>'ordered',
397 }, @imgs), "write from paletted")
398 or diag(Imager->_error_as_msg());
399 close FH;
400
401 # make sure nothing bad happened
402 open FH, "< testout/t105_pal.gif" or die $!;
403 binmode FH;
404 $io = Imager::io_new_fd(fileno(FH));
405 ok((my @imgs2 = Imager::File::GIF::i_readgif_multi_wiol($io)) == 2,
406 "re-reading saved paletted images");
407 ok(i_img_diff($imgs[0], $imgs2[0]) == 0, "imgs[0] mismatch");
408 ok(i_img_diff($imgs[1], $imgs2[1]) == 0, "imgs[1] mismatch");
409 }
410
411 # test that the OO interface warns when we supply old options
412 {
413 my @warns;
414 local $SIG{__WARN__} = sub { push(@warns, "@_") };
415
416 my $ooim = Imager->new;
417 ok($ooim->read(file=>"testout/t105.gif"), "read into object");
418 ok($ooim->write(file=>"testout/t105_warn.gif", interlace=>1),
419 "save from object")
420 or print "# ", $ooim->errstr, "\n";
421 ok(grep(/Obsolete .* interlace .* gif_interlace/, @warns),
422 "check for warning");
423 init(warn_obsolete=>0);
424 @warns = ();
425 ok($ooim->write(file=>"testout/t105_warn.gif", interlace=>1),
426 "save from object");
427 ok(!grep(/Obsolete .* interlace .* gif_interlace/, @warns),
428 "check for warning");
429 }
430
431 # test that we get greyscale from 1 channel images
432 # we check for each makemap, and for each translate
433 print "# test writes of grayscale images - ticket #365\n";
434 my $ooim = Imager->new(xsize=>50, ysize=>50, channels=>1);
435 for (my $y = 0; $y < 50; $y += 10) {
436 $ooim->box(box=>[ 0, $y, 49, $y+9], color=>NC($y*5,0,0), filled=>1);
437 }
438 my $ooim3 = $ooim->convert(preset=>'rgb');
439 #$ooim3->write(file=>'testout/t105gray.ppm');
440 my %maxerror = ( mediancut => 51000,
441 addi => 0,
442 closest => 0,
443 perturb => 0,
444 errdiff => 0 );
445 for my $makemap (qw(mediancut addi)) {
446 print "# make_colors => $makemap\n";
447 ok( $ooim->write(file=>"testout/t105gray-$makemap.gif",
448 make_colors=>$makemap,
449 gifquant=>'gen'),
450 "writing gif with makemap $makemap");
451 my $im2 = Imager->new;
452 if (ok($im2->read(file=>"testout/t105gray-$makemap.gif"),
453 "reading written grayscale gif")) {
454 my $diff = i_img_diff($ooim3->{IMG}, $im2->{IMG});
455 ok($diff <= $maxerror{$makemap}, "comparing images $diff");
456 #$im2->write(file=>"testout/t105gray-$makemap.ppm");
457 }
458 else {
459 SKIP: { skip("could not get test image", 1); }
460 }
461 }
462 for my $translate (qw(closest perturb errdiff)) {
463 print "# translate => $translate\n";
464 my @colors = map NC($_*50, $_*50, $_*50), 0..4;
465 ok($ooim->write(file=>"testout/t105gray-$translate.gif",
466 translate=>$translate,
467 make_colors=>'none',
468 colors=>\@colors,
469 gifquant=>'gen'),
470 "writing gif with translate $translate");
471 my $im2 = Imager->new;
472 if (ok($im2->read(file=>"testout/t105gray-$translate.gif"),
473 "reading written grayscale gif")) {
474 my $diff = i_img_diff($ooim3->{IMG}, $im2->{IMG});
475 ok($diff <= $maxerror{$translate}, "comparing images $diff");
476 #$im2->write(file=>"testout/t105gray-$translate.ppm");
477 }
478 else {
479 SKIP: { skip("could not load test image", 1) }
480 }
481 }
482
483 # try to write an image with no colors - should error
484 ok(!$ooim->write(file=>"testout/t105nocolors.gif",
485 make_colors=>'none',
486 colors=>[], gifquant=>'gen'),
487 "write with no colors");
488
489 # try to write multiple with no colors, with separate maps
490 # I don't see a way to test this, since we don't have a mechanism
491 # to give the second image different quant options, we can't trigger
492 # a failure just for the second image
493
494 # check that the i_format tag is set for both multiple and single
495 # image reads
496 {
497 my @anim = Imager->read_multi(file=>"testout/t105_anim.gif");
498 ok(@anim == 5, "check we got all the images");
499 for my $frame (@anim) {
500 my ($type) = $frame->tags(name=>'i_format');
501 is($type, 'gif', "check i_format for animation frame");
502 }
503
504 my $im = Imager->new;
505 ok($im->read(file=>"testout/t105.gif"), "read some gif");
506 my ($type) = $im->tags(name=>'i_format');
507 is($type, 'gif', 'check i_format for single image read');
508 }
509
510 { # check file limits are checked
511 my $limit_file = "testout/t105.gif";
512 ok(Imager->set_file_limits(reset=>1, width=>149), "set width limit 149");
513 my $im = Imager->new;
514 ok(!$im->read(file=>$limit_file),
515 "should fail read due to size limits");
516 print "# ",$im->errstr,"\n";
517 like($im->errstr, qr/image width/, "check message");
518
519 ok(Imager->set_file_limits(reset=>1, height=>149), "set height limit 149");
520 ok(!$im->read(file=>$limit_file),
521 "should fail read due to size limits");
522 print "# ",$im->errstr,"\n";
523 like($im->errstr, qr/image height/, "check message");
524
525 ok(Imager->set_file_limits(reset=>1, width=>150), "set width limit 150");
526 ok($im->read(file=>$limit_file),
527 "should succeed - just inside width limit");
528 ok(Imager->set_file_limits(reset=>1, height=>150), "set height limit 150");
529 ok($im->read(file=>$limit_file),
530 "should succeed - just inside height limit");
531
532 # 150 x 150 x 3 channel image uses 67500 bytes
533 ok(Imager->set_file_limits(reset=>1, bytes=>67499),
534 "set bytes limit 67499");
535 ok(!$im->read(file=>$limit_file),
536 "should fail - too many bytes");
537 print "# ",$im->errstr,"\n";
538 like($im->errstr, qr/storage size/, "check error message");
539 ok(Imager->set_file_limits(reset=>1, bytes=>67500),
540 "set bytes limit 67500");
541 ok($im->read(file=>$limit_file),
542 "should succeed - just inside bytes limit");
543 Imager->set_file_limits(reset=>1);
544 }
545
546 {
547 print "# test OO interface reading of consolidated images\n";
548 my $im = Imager->new;
549 ok($im->read(file=>'testimg/screen2.gif', gif_consolidate=>1),
550 "read image to consolidate");
551 my $expected = Imager->new;
552 ok($expected->read(file=>'testimg/expected.gif'),
553 "read expected via OO");
554 is(i_img_diff($im->{IMG}, $expected->{IMG}), 0,
555 "compare them");
556
557 # check the default read doesn't match
558 ok($im->read(file=>'testimg/screen2.gif'),
559 "read same image without consolidate");
560 isnt(i_img_diff($im->{IMG}, $expected->{IMG}), 0,
561 "compare them - shouldn't include the overlayed second image");
562 }
563 {
564 print "# test the reading of single pages\n";
565 # build a test file
566 my $test_file = 'testout/t105_multi_sing.gif';
567 my $im1 = Imager->new(xsize=>100, ysize=>100);
568 $im1->box(filled=>1, color=>$blue);
569 $im1->addtag(name=>'gif_left', value=>10);
570 $im1->addtag(name=>'gif_top', value=>15);
571 $im1->addtag(name=>'gif_comment', value=>'First page');
572 my $im2 = Imager->new(xsize=>50, ysize=>50);
573 $im2->box(filled=>1, color=>$red);
574 $im2->addtag(name=>'gif_left', value=>30);
575 $im2->addtag(name=>'gif_top', value=>25);
576 $im2->addtag(name=>'gif_comment', value=>'Second page');
577 my $im3 = Imager->new(xsize=>25, ysize=>25);
578 $im3->box(filled=>1, color=>$green);
579 $im3->addtag(name=>'gif_left', value=>35);
580 $im3->addtag(name=>'gif_top', value=>45);
581 # don't set comment for $im3
582 ok(Imager->write_multi({ file=> $test_file}, $im1, $im2, $im3),
583 "write test file for single page reads");
584
585 my $res = Imager->new;
586 # check we get the first image
587 ok($res->read(file=>$test_file), "read default (first) page");
588 is(i_img_diff($im1->{IMG}, $res->{IMG}), 0, "compare against first");
589 # check tags
590 is($res->tags(name=>'gif_left'), 10, "gif_left");
591 is($res->tags(name=>'gif_top'), 15, "gif_top");
592 is($res->tags(name=>'gif_comment'), 'First page', "gif_comment");
593
594 # get the second image
595 ok($res->read(file=>$test_file, page=>1), "read second page")
596 or print "# ",$res->errstr, "\n";
597 is(i_img_diff($im2->{IMG}, $res->{IMG}), 0, "compare against second");
598 # check tags
599 is($res->tags(name=>'gif_left'), 30, "gif_left");
600 is($res->tags(name=>'gif_top'), 25, "gif_top");
601 is($res->tags(name=>'gif_comment'), 'Second page', "gif_comment");
602
603 # get the third image
604 ok($res->read(file=>$test_file, page=>2), "read third page")
605 or print "# ",$res->errstr, "\n";
606 is(i_img_diff($im3->{IMG}, $res->{IMG}), 0, "compare against third");
607 is($res->tags(name=>'gif_left'), 35, "gif_left");
608 is($res->tags(name=>'gif_top'), 45, "gif_top");
609 is($res->tags(name=>'gif_comment'), undef, 'gif_comment undef');
610
611 # try to read a fourth page
612 ok(!$res->read(file=>$test_file, page=>3), "fail reading fourth page");
613 cmp_ok($res->errstr, "=~", 'page 3 not found',
614 "check error message");
615 }
616 SKIP:
617 {
618 skip("gif_loop not supported on giflib before 4.1", 6)
619 unless $gifver >= 4.1;
620 # testing writing the loop extension
621 my $im1 = Imager->new(xsize => 100, ysize => 100);
622 $im1->box(filled => 1, color => '#FF0000');
623 my $im2 = Imager->new(xsize => 100, ysize => 100);
624 $im2->box(filled => 1, color => '#00FF00');
625 ok(Imager->write_multi({
626 gif_loop => 5,
627 gif_delay => 50,
628 file => 'testout/t105loop.gif'
629 }, $im1, $im2),
630 "write with loop extension");
631
632 my @im = Imager->read_multi(file => 'testout/t105loop.gif');
633 is(@im, 2, "read loop images back");
634 is($im[0]->tags(name => 'gif_loop'), 5, "first loop read back");
635 is($im[1]->tags(name => 'gif_loop'), 5, "second loop read back");
636 is($im[0]->tags(name => 'gif_delay'), 50, "first delay read back");
637 is($im[1]->tags(name => 'gif_delay'), 50, "second delay read back");
638 }
639 SKIP:
640 { # check graphic control extension and ns loop tags are read correctly
641 print "# check GCE and netscape loop extension tag values\n";
642 my @im = Imager->read_multi(file => 'testimg/screen3.gif');
643 is(@im, 2, "read 2 images from screen3.gif")
644 or skip("Could not load testimg/screen3.gif:".Imager->errstr, 11);
645 is($im[0]->tags(name => 'gif_delay'), 50, "0 - gif_delay");
646 is($im[0]->tags(name => 'gif_disposal'), 2, "0 - gif_disposal");
647 is($im[0]->tags(name => 'gif_trans_index'), undef, "0 - gif_trans_index");
648 is($im[0]->tags(name => 'gif_user_input'), 0, "0 - gif_user_input");
649 is($im[0]->tags(name => 'gif_loop'), 0, "0 - gif_loop");
650 is($im[1]->tags(name => 'gif_delay'), 50, "1 - gif_delay");
651 is($im[1]->tags(name => 'gif_disposal'), 2, "1 - gif_disposal");
652 is($im[1]->tags(name => 'gif_trans_index'), 7, "1 - gif_trans_index");
653 is($im[1]->tags(name => 'gif_trans_color'), 'color(255,255,255,0)',
654 "1 - gif_trans_index");
655 is($im[1]->tags(name => 'gif_user_input'), 0, "1 - gif_user_input");
656 is($im[1]->tags(name => 'gif_loop'), 0, "1 - gif_loop");
657 }
658
659 {
660 # manually modified from a small gif, this had the palette
661 # size changed to half the size, leaving an index out of range
662 my $im = Imager->new;
663 ok($im->read(file => 'testimg/badindex.gif', type => 'gif'),
664 "read bad index gif")
665 or print "# ", $im->errstr, "\n";
666 my @indexes = $im->getscanline('y' => 0, type => 'index');
667 is_deeply(\@indexes, [ 0..4 ], "check for correct indexes");
668 is($im->colorcount, 5, "check the palette was adjusted");
669 is_color3($im->getpixel('y' => 0, x => 4), 0, 0, 0,
670 "check it was black added");
671 is($im->tags(name => 'gif_colormap_size'), 4, 'color map size tag');
672 }
673
674 {
675 ok(grep($_ eq 'gif', Imager->read_types), "check gif in read types");
676 ok(grep($_ eq 'gif', Imager->write_types), "check gif in write types");
677 }
678
679 {
680 # check screen tags handled correctly note the screen size
681 # supplied is larger than the box covered by the images
682 my $im1 = Imager->new(xsize => 10, ysize => 8);
683 $im1->settag(name => 'gif_top', value => 4);
684 $im1->settag(name => 'gif_screen_width', value => 18);
685 $im1->settag(name => 'gif_screen_height', value => 16);
686 my $im2 = Imager->new(xsize => 7, ysize => 10);
687 $im2->settag(name => 'gif_left', value => 3);
688 my @im = ( $im1, $im2 );
689
690 my $data;
691 ok(Imager->write_multi({ data => \$data, type => 'gif' }, @im),
692 "write with screen settings")
693 or print "# ", Imager->errstr, "\n";
694 my @result = Imager->read_multi(data => $data);
695 is(@result, 2, "got 2 images back");
696 is($result[0]->tags(name => 'gif_screen_width'), 18,
697 "check result screen width");
698 is($result[0]->tags(name => 'gif_screen_height'), 16,
699 "check result screen height");
700 is($result[0]->tags(name => 'gif_left'), 0,
701 "check first gif_left");
702 is($result[0]->tags(name => 'gif_top'), 4,
703 "check first gif_top");
704 is($result[1]->tags(name => 'gif_left'), 3,
705 "check second gif_left");
706 is($result[1]->tags(name => 'gif_top'), 0,
707 "check second gif_top");
708 }
709
710 { # test colors array returns colors
711 my $data;
712 my $im = test_image();
713 my @colors;
714 ok($im->write(data => \$data,
715 colors => \@colors,
716 make_colors => 'webmap',
717 translate => 'closest',
718 gifquant => 'gen',
719 type => 'gif'),
720 "write using webmap to check color table");
721 is(@colors, 216, "should be 216 colors in the webmap");
722 is_color3($colors[0], 0, 0, 0, "first should be 000000");
723 is_color3($colors[1], 0, 0, 0x33, "second should be 000033");
724 is_color3($colors[8], 0, 0x33, 0x66, "9th should be 003366");
725 }
726
727 { # a zero length extension could make read_/read_multi crash
728 my ($im) = Imager->read_multi(file => "testimg/zerocomm.gif");
729 ok($im, "read image with zero-length extension");
730 }
731
732
733 { # check close failures are handled correctly
734 my $im = test_image_mono();
735 my $fail_close = sub {
736 Imager::i_push_error(0, "synthetic close failure");
737 return 0;
738 };
739 ok(!$im->write(type => "gif", callback => sub { 1 },
740 closecb => $fail_close),
741 "check failing close fails");
742 like($im->errstr, qr/synthetic close failure/,
743 "check error message");
744 }
745
746 ext_test(1, <<'CODE', 1, "CVE-2026-8454");
747 use Imager;
748 my $im = Imager->new(file => "testimg/cve-2026-8454.gif", page => 1);
749 print "ok #cve-2026-8454\n";
750 CODE
751
752 Imager->close_log;
753
754 sub test_readgif_cb {
755 my ($size) = @_;
756
757 open FH, "<testimg/scale.gif" or die "Cannot open testimg/scale.gif";
758 binmode FH;
759 my $io = Imager::io_new_cb
760 (
761 undef,
762 sub { my $tmp; read(FH, $tmp, $size) and $tmp },
763 undef,
764 undef
765 );
766 my $img = Imager::File::GIF::i_readgif_wiol($io);
767 close FH;
768 return $img;
769 }
770
771 # tests for reading bad gif files
772 sub read_failure {
773 my ($filename) = @_;
774
775 open FH, "< $filename"
776 or die "Cannot open $filename: $!";
777 binmode FH;
778 my $io = Imager::io_new_fd(fileno(FH));
779 my ($result, $map) = Imager::File::GIF::i_readgif_wiol($io);
780 ok(!$result, "attempt to read invalid image $filename ".Imager::_error_as_msg());
781 close FH;
782 }
783
784 sub _clear_tags {
785 my (@imgs) = @_;
786
787 for my $img (@imgs) {
788 $img->deltag(code=>0);
789 }
790 }
791
792 sub _add_tags {
793 my ($img, %tags) = @_;
794
795 for my $key (keys %tags) {
796 Imager::i_tags_add($img, $key, 0, $tags{$key}, 0);
797 }
798 }
799
800 # usage: ext_test($testnum, $code, $count, $name)
801 # $testnum - used to set a default $name
802 # $code - script code to run
803 # $count - number of TAP test results to expect and generate
804 # $name - script name
805 # The name for ok($ok, $name) is extracted from the script output,
806 # expecting /^ok \d* - (.*)/
807
808 sub ext_test {
809 my ($testnum, $code, $count, $name) = @_;
810
811 $count ||= 1;
812 $name ||= "gif$testnum";
813
814 # build our code
815 my $script = "testout/$name.pl";
816 if (open my $script_fh, "> $script") {
817 print $script_fh <<'PROLOG';
818 #!perl -w
819 if (lc $^O eq 'mswin32') {
820 # avoid the dialog box that window's pops up on a GPF
821 # if you want to debug this stuff, I suggest you comment out the
822 # following
823 eval {
824 require Win32API::File;
825 Win32API::File::SetErrorMode( Win32API::File::SEM_NOGPFAULTERRORBOX());
826 };
827 }
828 PROLOG
829
830 print $script_fh $code;
831 close $script_fh;
832
833 my $perl = $^X;
834 $perl = qq/"$perl"/ if $perl =~ / /;
835
836 note "script: $script\n";
837 my $cmd = "$perl -Mblib $script";
838 if ($ENV{IMAGER_VALGRIND}) {
839 my $valgrind = $ENV{IMAGER_VALGRIND};
840 $valgrind =~ /valgrind/ or $valgrind = "valgrind";
841 $cmd = "$valgrind $cmd";
842 }
843 note "command: $cmd\n";
844
845 my $ok = 1;
846 my @out = `$cmd`; # should work on DOS and Win32
847 my $found = 0;
848 for (@out) {
849 if (/^not ok\s+(?:\d+\s*)?#(.*)/ || /^not ok/) {
850 my $msg = $1 || '';
851 ok(0, $msg);
852 $ok = 0;
853 ++$found;
854 }
855 elsif (/^ok\s+(?:\d+\s*)?#(.*)/ || /^ok/) {
856 my $msg = $1 || '';
857 ok(1, $msg);
858 ++$found;
859 }
860 }
861 unless ($count == $found) {
862 diag "didn't see enough ok/not ok\n";
863 $ok = 0;
864 }
865 return $ok;
866 }
867 else {
868 return skip("could not create test script $script: $!");
869 return 0;
870 }
871 }