5 init_log("testout/t105gif.log",1);
7 $green=i_color_new(0,255,0,255);
8 $blue=i_color_new(0,0,255,255);
9 $red=i_color_new(255,0,0,255);
11 $img=Imager::ImgRaw::new(150,150,3);
13 i_box_filled($img,70,25,130,125,$green);
14 i_box_filled($img,20,25,80,125,$blue);
15 i_arc($img,75,75,30,0,361,$red);
16 i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
18 my $timg = Imager::ImgRaw::new(20, 20, 4);
19 my $trans = i_color_new(255, 0, 0, 127);
20 i_box_filled($timg, 0, 0, 20, 20, $green);
21 i_box_filled($timg, 2, 2, 18, 18, $trans);
23 if (!i_has_format("gif")) {
24 for (1..23) { print "ok $_ # skip no gif support\n"; }
26 open(FH,">testout/t105.gif") || die "Cannot open testout/t105.gif\n";
28 i_writegifmc($img,fileno(FH),7) || die "Cannot write testout/t105.gif\n";
33 open(FH,"testout/t105.gif") || die "Cannot open testout/t105.gif\n";
35 $img=i_readgif(fileno(FH)) || die "Cannot read testout/t105.gif\n";
40 open(FH,"testout/t105.gif") || die "Cannot open testout/t105.gif\n";
42 ($img, $palette)=i_readgif(fileno(FH));
43 $img || die "Cannot read testout/t105.gif\n";
46 $palette=''; # just to skip a warning.
50 # check that reading interlaced/non-interlaced versions of
51 # the same GIF produce the same image
52 # I could replace this with code that used Imager's built-in
53 # image comparison code, but I know this code revealed the error
54 open(FH, "<testimg/scalei.gif") || die "Cannot open testimg/scalei.gif";
56 ($imgi) = i_readgif(fileno(FH));
57 $imgi || die "Cannot read testimg/scalei.gif";
60 open FH, "<testimg/scale.gif" or die "Cannot open testimg/scale.gif";
62 ($imgni) = i_readgif(fileno(FH));
63 $imgni or die "Cannot read testimg/scale.gif";
67 open FH, ">testout/t105i.ppm" or die "Cannot create testout/t105i.ppm";
69 i_writeppm($imgi, fileno(FH)) or die "Cannot write testout/t105i.ppm";
72 open FH, ">testout/t105ni.ppm" or die "Cannot create testout/t105ni.ppm";
74 i_writeppm($imgni, fileno(FH)) or die "Cannot write testout/t105ni.ppm";
78 open FH, "<testout/t105i.ppm" or die "Cannot open testout/t105i.ppm";
79 $datai = do { local $/; <FH> };
81 open FH, "<testout/t105ni.ppm" or die "Cannot open testout/t105ni.ppm";
82 $datani = do { local $/; <FH> };
84 if ($datai eq $datani) {
91 my $gifver = Imager::i_giflib_version();
93 # reading with a callback
94 # various sizes to make sure the buffering works
96 open FH, "<testimg/scale.gif" or die "Cannot open testimg/scale.gif";
98 # no callback version in giflib3, so don't overwrite a good image
99 my $img2 = i_readgif_callback(sub { my $tmp; read(FH, $tmp, $_[0]) and $tmp });
101 print $img ? "ok 7\n" : "not ok 7\n";
103 print test_readgif_cb(1) ? "ok 8\n" : "not ok 8\n";
104 print test_readgif_cb(512) ? "ok 9\n" : "not ok 9\n";
105 print test_readgif_cb(1024) ? "ok 10\n" : "not ok 10\n";
108 print "ok $_ # skip giflib3 doesn't support callbacks\n" for (7..10);
110 open FH, ">testout/t105_mc.gif" or die "Cannot open testout/t105_mc.gif";
112 i_writegifmc($img, fileno(FH), 7) or print "not ";
117 # test webmap, custom errdiff map
118 # (looks fairly awful)
119 open FH, ">testout/t105_gen.gif" or die $!;
121 i_writegif_gen(fileno(FH), { make_colors=>'webmap',
122 translate=>'errdiff',
126 errdiff_map=>[0, 1, 1, 0]}, $img)
131 print "# the following tests are fairly slow\n";
133 # test animation, mc_addi, error diffusion, ordered transparency
135 my $sortagreen = i_color_new(0, 255, 0, 63);
137 my $im = Imager::ImgRaw::new(200, 200, 4);
138 for my $j (0..$i-1) {
139 my $fill = i_color_new(0, 128, 0, 255 * ($i-$j)/$i);
140 i_box_filled($im, 0, $j*40, 199, $j*40+40, $fill);
142 i_box_filled($im, 0, $i*40, 199, 199, $blue);
145 my @gif_delays = (50) x 5;
146 my @gif_disposal = (2) x 5;
147 open FH, ">testout/t105_anim.gif" or die $!;
149 i_writegif_gen(fileno(FH), { make_colors=>'addi',
150 translate=>'closest',
151 gif_delays=>\@gif_delays,
152 gif_disposal=>\@gif_disposal,
154 tr_orddith=>'dot8'}, @imgs)
155 or die "Cannot write anim gif";
159 if ($gifver >= 4.0) {
161 # this can SIGSEGV with some versions of giflib
162 open FH, ">testout/t105_anim_cb.gif" or die $!;
163 i_writegif_callback(sub {
167 { make_colors=>'webmap',
168 translate=>'closest',
169 gif_delays=>\@gif_delays,
170 gif_disposal=>\@gif_disposal,
172 tr_orddith=>'dot8'}, @imgs)
173 or die "Cannot write anim gif";
178 if (wait > 0 && $?) {
179 print "not ok 14 # you probably need to patch giflib\n";
181 #--- egif_lib.c 2000/12/11 07:33:12 1.1
182 #+++ egif_lib.c 2000/12/11 07:33:48
183 #@@ -167,6 +167,12 @@
184 # _GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
187 #+ if ((Private->HashTable = _InitHashTable()) == NULL) {
190 #+ _GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
194 # GifFile->Private = (VoidPtr) Private;
195 # Private->FileHandle = 0;
200 print "ok 14 # skip giflib3 doesn't support callbacks\n";
204 my $im = Imager::ImgRaw::new(200, 200, 3);
205 for my $x (0 .. 39) {
206 for my $y (0 .. 39) {
207 my $c = i_color_new($x * 6, $y * 6, 32*$g+$x+$y, 255);
208 i_box_filled($im, $x*5, $y*5, $x*5+4, $y*5+4, $c);
213 # test giflib with multiple palettes
214 # (it was meant to test the NS loop extension too, but that's broken)
215 # this looks better with make_colors=>'addi', translate=>'errdiff'
216 # this test aims to overload the palette for each image, so the
217 # output looks moderately horrible
218 open FH, ">testout/t105_mult_pall.gif" or die "Cannot create file: $!";
220 i_writegif_gen(fileno(FH), { make_colors=>'webmap',
222 gif_delays=>[ 50, 50, 50, 50 ],
223 #gif_loop_count => 50,
224 gif_each_palette => 1,
225 }, @imgs) or print "not ";
229 # regression test: giflib doesn't like 1 colour images
230 my $img1 = Imager::ImgRaw::new(100, 100, 3);
231 i_box_filled($img1, 0, 0, 100, 100, $red);
232 open FH, ">testout/t105_onecol.gif" or die $!;
234 if (i_writegif_gen(fileno(FH), { translate=>'giflib'}, $img1)) {
235 print "ok 16 # single colour write regression\n";
237 print "not ok 16 # single colour write regression\n";
242 # previously it was harder do write transparent images
243 # tests the improvements
244 my $timg = Imager::ImgRaw::new(20, 20, 4);
245 my $trans = i_color_new(255, 0, 0, 127);
246 i_box_filled($timg, 0, 0, 20, 20, $green);
247 i_box_filled($timg, 2, 2, 18, 18, $trans);
248 open FH, ">testout/t105_trans.gif" or die $!;
250 i_writegif_gen(fileno(FH), { make_colors=>'addi',
251 translate=>'closest',
253 }, $timg) or print "not ";
257 # some error handling tests
258 # open a file handle for read and try to save to it
259 # is this idea portable?
260 # whether or not it is, giflib segfaults on this <sigh>
261 #open FH, "<testout/t105_trans.gif" or die $!;
262 #binmode FH; # habit, I suppose
263 #if (i_writegif_gen(fileno(FH), {}, $timg)) {
264 # # this is meant to _fail_
265 # print "not ok 18 # writing to read-only should fail";
268 # print "ok 18 # ",Imager::_error_as_msg(),"\n";
272 # try to read a file of the wrong format - the script will do
273 open FH, "<t/t105gif.t"
274 or die "Cannot open this script!: $!";
276 my $im2 = i_readgif(fileno(FH));
278 # this should have failed
279 print "not ok 18 # giflib think script if a GIF file\n";
282 print "ok 18 # ",Imager::_error_as_msg(),"\n";
286 # try to save no images :)
287 open FH, ">testout/t105_none.gif"
288 or die "Cannot open testout/t105_none.gif: $!";
290 if (i_writegif_gen(fileno(FH), {}, "hello")) {
291 print "not ok 19 # shouldn't be able to save strings\n";
294 print "ok 19 # ",Imager::_error_as_msg(),"\n";
297 # try to read a truncated gif (no image descriptors)
298 read_failure('testimg/trimgdesc.gif', 20);
299 # file truncated just after the image descriptor tag
300 read_failure('testimg/trmiddesc.gif', 21);
301 # image has no colour map
302 read_failure('testimg/nocmap.gif', 22);
304 # image has a local colour map
305 open FH, "< testimg/loccmap.gif"
306 or die "Cannot open testimg/loccmap.gif: $!";
308 if (i_readgif(fileno(FH))) {
312 print "not ok 23 # failed to read image with only a local colour map";
317 sub test_readgif_cb {
320 open FH, "<testimg/scale.gif" or die "Cannot open testimg/scale.gif";
322 my $img = i_readgif_callback(sub { my $tmp; read(FH, $tmp, $size) and $tmp });
327 # tests for reading bad gif files
329 my ($filename, $testnum) = @_;
331 open FH, "< $filename"
332 or die "Cannot open $filename: $!";
334 my ($result, $map) = i_readgif(fileno(FH));
336 print "not ok $testnum # this is an invalid file, we succeeded\n";
339 print "ok $testnum # ",Imager::_error_as_msg(),"\n";