]> git.imager.perl.org - imager.git/blob - t/t50basicoo.t
Changes to t50basicoo.t test... not working yet.
[imager.git] / t / t50basicoo.t
1 #!perl -w
2 ######################### We start with some black magic to print on failure.
3
4 # this used to do the check for the load of Imager, but I want to be able 
5 # to count tests, which means I need to load Imager first
6 # since many of the early tests already do this, we don't really need to
7
8 use strict;
9 use Imager;
10 use IO::Seekable;
11
12 my $buggy_giflib_file = "buggy_giflib.txt";
13
14 Imager::init("log"=>"testout/t50basicoo.log");
15
16 # single image/file types
17 my @types = qw( jpeg png raw ppm gif tiff bmp tga );
18
19 # multiple image/file formats
20 my @mtypes = qw(tiff gif);
21
22 my %hsh=%Imager::formats;
23
24 my $test_num = 0;
25 my $count;
26 for my $type (@types) {
27   $count += 31 if $hsh{$type};
28 }
29 for my $type (@mtypes) {
30   $count += 7 if $hsh{$type};
31 }
32
33 print "1..$count\n";
34
35 print "# avaliable formats:\n";
36 for(keys %hsh) { print "# $_\n"; }
37
38 #print Dumper(\%hsh);
39
40 my $img = Imager->new();
41
42 my %files;
43 @files{@types} = ({ file => "testout/t101.jpg"  },
44                   { file => "testout/t102.png"  },
45                   { file => "testout/t103.raw", xsize=>150, ysize=>150, type=>'raw'},
46                   { file => "testout/t104.ppm"  },
47                   { file => "testout/t105.gif"  },
48                   { file => "testout/t106.tiff" },
49                   { file => "testout/t107_24bit.bmp" },
50                   { file => "testout/t108_24bit.tga" }, );
51 my %writeopts =
52   (
53    gif=> { make_colors=>'webmap', translate=>'closest', gifquant=>'gen',
54          gif_delay=>20 },
55   );
56
57 for my $type (@types) {
58   next unless $hsh{$type};
59   print "# type $type\n";
60   my %opts = %{$files{$type}};
61   my @a = map { "$_=>${opts{$_}}" } keys %opts;
62   print "#opening Format: $type, options: @a\n";
63   ok($img->read( %opts ), "reading from file", $img);
64   #or die "failed: ",$img->errstr,"\n";
65
66   my %mopts = %opts;
67   delete $mopts{file};
68
69   # read from a file handle
70   my $fh = IO::File->new($opts{file}, "r");
71   if (ok($fh, "opening $opts{file}")) {
72     binmode $fh;
73     my $fhimg = Imager->new;
74     if (ok($fhimg->read(fh=>$fh, %mopts), "read from fh")) {
75       ok($fh->seek(0, SEEK_SET), "seek after read");
76       if (ok($fhimg->read(fh=>$fh, %mopts, type=>$type), "read from fh")) {
77         ok(Imager::i_img_diff($img->{IMG}, $fhimg->{IMG}) == 0,
78            "image comparison after fh read");
79       }
80       else {
81         skip("no image to compare");
82       }
83       ok($fh->seek(0, SEEK_SET), "seek after read");
84     }
85
86     # read from a fd
87     my $fdimg = Imager->new;
88     if (ok($fdimg->read(fd=>fileno($fh), %mopts, type=>$type), "read from fd")) {
89       ok(Imager::i_img_diff($img->{IMG}, $fdimg->{IMG}) == 0,
90          "image comparistion after fd read");
91     }
92     else {
93       skip("no image to compare");
94     }
95     ok($fh->seek(0, SEEK_SET), "seek after fd read");
96     ok($fh->close, "close fh after reads");
97   }
98   else {
99     skip("couldn't open the damn file: $!", 7);
100   }
101
102   if ($type ne 'gif' || Imager::i_giflib_version() >= 4) {
103     # read from a memory buffer
104     open DATA, "< $opts{file}"
105       or die "Cannot open $opts{file}: $!";
106     binmode DATA;
107     my $data = do { local $/; <DATA> };
108     close DATA;
109     my $bimg = Imager->new;
110     
111     if (ok($bimg->read(data=>$data, %mopts, type=>$type), "read from buffer", 
112            $img)) {
113       ok(Imager::i_img_diff($img->{IMG}, $bimg->{IMG}) == 0,
114          "comparing buffer read image");
115     }
116     else {
117       skip("nothing to compare");
118     }
119     
120     # read from callbacks, both with minimum and maximum reads
121     my $buf = $data;
122     my $seekpos = 0;
123     my $reader_min = 
124       sub { 
125         my ($size, $maxread) = @_;
126         my $out = substr($buf, $seekpos, $size);
127         $seekpos += length $out;
128         $out;
129       };
130     my $reader_max = 
131       sub { 
132         my ($size, $maxread) = @_;
133         my $out = substr($buf, $seekpos, $maxread);
134         $seekpos += length $out;
135         $out;
136       };
137     my $seeker =
138       sub {
139         my ($offset, $whence) = @_;
140         #print "io_seeker($offset, $whence)\n";
141         if ($whence == SEEK_SET) {
142           $seekpos = $offset;
143         }
144         elsif ($whence == SEEK_CUR) {
145           $seekpos += $offset;
146         }
147         else { # SEEK_END
148           $seekpos = length($buf) + $offset;
149         }
150         #print "-> $seekpos\n";
151         $seekpos;
152       };
153     my $cbimg = Imager->new;
154     ok($cbimg->read(callback=>$reader_min, seekcb=>$seeker, type=>$type, %mopts),
155        "read from callback min", $cbimg);
156     ok(Imager::i_img_diff($cbimg->{IMG}, $img->{IMG}) == 0,
157        "comparing mincb image");
158     $seekpos = 0;
159     ok($cbimg->read(callback=>$reader_max, seekcb=>$seeker, type=>$type, %mopts),
160        "read from callback max", $cbimg);
161     ok(Imager::i_img_diff($cbimg->{IMG}, $img->{IMG}) == 0,
162        "comparing maxcb image");
163   }
164   else {
165     skip("giflib < 4 doesn't support callbacks", 6);
166   }
167 }
168
169 for my $type (@types) {
170   next unless $hsh{$type};
171
172   print "# write tests for $type\n";
173   # test writes
174   next unless $hsh{$type};
175   my $file = "testout/t50out.$type";
176   my $wimg = Imager->new;
177   # if this doesn't work, we're so screwed up anyway
178   
179   ok($wimg->read(file=>"testout/t104.ppm"),
180      "cannot read base file", $wimg);
181
182   # first to a file
183   print "# writing $type to a file\n";
184   my %extraopts;
185   %extraopts = %{$writeopts{$type}} if $writeopts{$type};
186   ok($wimg->write(file=>$file, %extraopts),
187      "writing $type to a file $file", $wimg);
188
189   print "# writing $type to a FH\n";
190   # to a FH
191   my $fh = IO::File->new($file, "w+")
192     or die "Could not create $file: $!";
193   binmode $fh;
194   ok($wimg->write(fh=>$fh, %extraopts, type=>$type),
195      "writing $type to a FH", $wimg);
196   ok($fh->seek(0, SEEK_END) > 0,
197      "seek after writing $type to a FH");
198   ok(print($fh "SUFFIX\n"),
199      "write to FH after writing $type");
200   ok($fh->close, "closing FH after writing $type");
201
202   if ($type ne 'gif' || 
203       (Imager::i_giflib_version() >= 4 && !-e $buggy_giflib_file)) {
204     if (ok(open(DATA, "< $file"), "opening data source")) {
205       binmode DATA;
206       my $data = do { local $/; <DATA> };
207       close DATA;
208
209       # writing to a buffer
210       print "# writing $type to a buffer\n";
211       my $buf = '';
212       ok($wimg->write(data=>\$buf, %extraopts, type=>$type),
213          "writing $type to a buffer", $wimg);
214       $buf .= "SUFFIX\n";
215       open DATA, "> testout/t50_buf.$type"
216         or die "Cannot create $type buffer file: $!";
217       binmode DATA;
218       print DATA $buf;
219       close DATA;
220       ok($data eq $buf, "comparing file data to buffer");
221
222       $buf = '';
223       my $seekpos = 0;
224       my $did_close;
225       my $writer = 
226         sub {
227           my ($what) = @_;
228           if ($seekpos > length $buf) {
229             $buf .= "\0" x ($seekpos - length $buf);
230           }
231           substr($buf, $seekpos, length $what) = $what;
232           $seekpos += length $what;
233           $did_close = 0; # the close must be last
234           1;
235         };
236       my $reader_min = 
237         sub { 
238           my ($size, $maxread) = @_;
239           my $out = substr($buf, $seekpos, $size);
240           $seekpos += length $out;
241           $out;
242         };
243       my $reader_max = 
244         sub { 
245           my ($size, $maxread) = @_;
246           my $out = substr($buf, $seekpos, $maxread);
247           $seekpos += length $out;
248           $out;
249         };
250       use IO::Seekable;
251       my $seeker =
252         sub {
253           my ($offset, $whence) = @_;
254           #print "io_seeker($offset, $whence)\n";
255           if ($whence == SEEK_SET) {
256             $seekpos = $offset;
257           }
258           elsif ($whence == SEEK_CUR) {
259             $seekpos += $offset;
260           }
261           else { # SEEK_END
262             $seekpos = length($buf) + $offset;
263           }
264           #print "-> $seekpos\n";
265           $seekpos;
266         };
267       
268       my $closer = sub { ++$did_close; };
269       
270       print "# writing $type via callbacks (mb=1)\n";
271       ok($wimg->write(writecb=>$writer, seekcb=>$seeker, closecb=>$closer,
272                    readcb=>$reader_min,
273                    %extraopts, type=>$type, maxbuffer=>1),
274          "writing $type to callback (mb=1)", $wimg);
275
276       ok($did_close, "checking closecb called");
277       $buf .= "SUFFIX\n";
278       ok($data eq $buf, "comparing callback output to file data");
279       print "# writing $type via callbacks (no mb)\n";
280       $buf = '';
281       $did_close = 0;
282       $seekpos = 0;
283       # we don't use the closecb here - used to make sure we don't get 
284       # a warning/error on an attempt to call an undef close sub
285       ok($wimg->write(writecb=>$writer, seekcb=>$seeker, readcb=>$reader_min,
286                    %extraopts, type=>$type),
287          "writing $type to callback (no mb)", $wimg);
288       $buf .= "SUFFIX\n";
289       ok($data eq $buf, "comparing callback output to file data");
290     }
291     else {
292       skip("couldn't open data source", 7);
293     }
294   }
295   else {
296     if (-e $buggy_giflib_file) {
297       skip("see $buggy_giflib_file", 8);
298     }
299     else {
300       skip("giflib < 4 doesn't support callbacks", 8);
301     }
302   }
303 }
304
305 my $img2 =  $img->crop(width=>50, height=>50);
306 $img2 -> write(file=> 'testout/t50.ppm', type=>'pnm');
307
308 undef($img);
309
310 # multi image/file tests
311 print "# multi-image write tests\n";
312 for my $type (@mtypes) {
313   next unless $hsh{$type};
314   print "# $type\n";
315
316   my $file = "testout/t50out.$type";
317   my $wimg = Imager->new;
318
319   # if this doesn't work, we're so screwed up anyway
320   ok($wimg->read(file=>"testout/t50out.$type"),
321      "reading base file", $wimg);
322
323   ok(my $wimg2 = $wimg->copy, "copying base image", $wimg);
324   ok($wimg2->flip(dir=>'h'), "flipping base image", $wimg2);
325
326   my @out = ($wimg, $wimg2);
327   my %extraopts;
328   %extraopts = %{$writeopts{$type}} if $writeopts{$type};
329   ok(Imager->write_multi({ file=>"testout/t50_multi.$type", %extraopts },
330                          @out),
331      "writing multiple to a file", "Imager");
332
333   # make sure we get the same back
334   my @images = Imager->read_multi(file=>"testout/t50_multi.$type");
335   if (ok(@images == @out, "checking read image count")) {
336     for my $i (0 .. $#out) {
337       my $diff = Imager::i_img_diff($out[$i]{IMG}, $images[$i]{IMG});
338       print "# diff $diff\n";
339       ok($diff == 0, "comparing image $i");
340     }
341   }
342   else {
343     skip("wrong number of images read", 2);
344   }
345 }
346
347
348 Imager::malloc_state();
349
350 #print "ok 2\n";
351
352 sub ok {
353   my ($ok, $msg, $img, $why, $skipcount) = @_;
354
355   ++$test_num;
356   if ($ok) {
357     print "ok $test_num # $msg\n";
358     Imager::log_entry("ok $test_num # $msg\n", 0);
359   }
360   else {
361     my $err;
362     $err = $img->errstr if $img;
363     # VMS (if we ever support it) wants the whole line in one print
364     my $line = "not ok $test_num # line ".(caller)[2].": $msg";
365     $line .= ": $err" if $err;
366     print $line, "\n";
367     Imager::log_entry($line."\n", 0);
368   }
369   skip($why, $skipcount) if defined $why;
370   $ok;
371 }
372
373 sub skip {
374   my ($why, $skipcount) = @_;
375
376   $skipcount ||= 1;
377   for (1.. $skipcount) {
378     ++$test_num;
379     print "ok $test_num # skipped $why\n";
380   }
381 }