2 ######################### We start with some black magic to print on failure.
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
12 my $buggy_giflib_file = "buggy_giflib.txt";
14 -d "testout" or mkdir "testout";
16 Imager::init("log"=>"testout/t50basicoo.log");
18 # single image/file types
19 my @types = qw( jpeg png raw pnm gif tiff bmp tga );
21 # multiple image/file formats
22 my @mtypes = qw(tiff gif);
24 my %hsh=%Imager::formats;
28 for my $type (@types) {
29 $count += 31 if $hsh{$type};
31 for my $type (@mtypes) {
32 $count += 7 if $hsh{$type};
37 print "# avaliable formats:\n";
38 for(keys %hsh) { print "# $_\n"; }
42 my $img = Imager->new();
45 @files{@types} = ({ file => "JPEG/testimg/209_yonge.jpg" },
46 { file => "testimg/test.png" },
47 { file => "testimg/test.raw", xsize=>150, ysize=>150, type=>'raw', interleave => 0},
48 { file => "testimg/penguin-base.ppm" },
49 { file => "GIF/testimg/expected.gif" },
50 { file => "TIFF/testimg/comp8.tif" },
51 { file => "testimg/winrgb24.bmp" },
52 { file => "testimg/test.tga" }, );
55 gif=> { make_colors=>'webmap', translate=>'closest', gifquant=>'gen',
59 for my $type (@types) {
60 next unless $hsh{$type};
61 print "# type $type\n";
62 my %opts = %{$files{$type}};
63 my @a = map { "$_=>${opts{$_}}" } keys %opts;
64 print "#opening Format: $type, options: @a\n";
65 ok($img->read( %opts ), "reading from file", $img);
66 #or die "failed: ",$img->errstr,"\n";
71 # read from a file handle
72 my $fh = IO::File->new($opts{file}, "r");
73 if (ok($fh, "opening $opts{file}")) {
75 my $fhimg = Imager->new;
76 if (ok($fhimg->read(fh=>$fh, %mopts), "read from fh")) {
77 ok($fh->seek(0, SEEK_SET), "seek after read");
78 if (ok($fhimg->read(fh=>$fh, %mopts, type=>$type), "read from fh")) {
79 ok(Imager::i_img_diff($img->{IMG}, $fhimg->{IMG}) == 0,
80 "image comparison after fh read");
83 skip("no image to compare");
85 ok($fh->seek(0, SEEK_SET), "seek after read");
89 my $fdimg = Imager->new;
90 if (ok($fdimg->read(fd=>fileno($fh), %mopts, type=>$type), "read from fd")) {
91 ok(Imager::i_img_diff($img->{IMG}, $fdimg->{IMG}) == 0,
92 "image comparistion after fd read");
95 skip("no image to compare");
97 ok($fh->seek(0, SEEK_SET), "seek after fd read");
98 ok($fh->close, "close fh after reads");
101 skip("couldn't open the damn file: $!", 7);
104 # read from a memory buffer
105 open DATA, "< $opts{file}"
106 or die "Cannot open $opts{file}: $!";
108 my $data = do { local $/; <DATA> };
110 my $bimg = Imager->new;
112 if (ok($bimg->read(data=>$data, %mopts, type=>$type), "read from buffer",
114 ok(Imager::i_img_diff($img->{IMG}, $bimg->{IMG}) == 0,
115 "comparing buffer read image");
118 skip("nothing to compare");
121 # read from callbacks, both with minimum and maximum reads
126 my ($size, $maxread) = @_;
127 my $out = substr($buf, $seekpos, $size);
128 $seekpos += length $out;
133 my ($size, $maxread) = @_;
134 my $out = substr($buf, $seekpos, $maxread);
135 $seekpos += length $out;
140 my ($offset, $whence) = @_;
141 #print "io_seeker($offset, $whence)\n";
142 if ($whence == SEEK_SET) {
145 elsif ($whence == SEEK_CUR) {
149 $seekpos = length($buf) + $offset;
151 #print "-> $seekpos\n";
154 my $cbimg = Imager->new;
155 ok($cbimg->read(callback=>$reader_min, seekcb=>$seeker, type=>$type, %mopts),
156 "read from callback min", $cbimg);
157 ok(Imager::i_img_diff($cbimg->{IMG}, $img->{IMG}) == 0,
158 "comparing mincb image");
160 ok($cbimg->read(callback=>$reader_max, seekcb=>$seeker, type=>$type, %mopts),
161 "read from callback max", $cbimg);
162 ok(Imager::i_img_diff($cbimg->{IMG}, $img->{IMG}) == 0,
163 "comparing maxcb image");
166 for my $type (@types) {
167 next unless $hsh{$type};
169 print "# write tests for $type\n";
171 next unless $hsh{$type};
172 my $file = "testout/t50out.$type";
173 my $wimg = Imager->new;
174 # if this doesn't work, we're so screwed up anyway
176 ok($wimg->read(file=>"testimg/penguin-base.ppm"),
177 "cannot read base file", $wimg);
180 print "# writing $type to a file\n";
182 %extraopts = %{$writeopts{$type}} if $writeopts{$type};
183 ok($wimg->write(file=>$file, %extraopts),
184 "writing $type to a file $file", $wimg);
186 print "# writing $type to a FH\n";
188 my $fh = IO::File->new($file, "w+")
189 or die "Could not create $file: $!";
191 ok($wimg->write(fh=>$fh, %extraopts, type=>$type),
192 "writing $type to a FH", $wimg);
193 ok($fh->seek(0, SEEK_END) > 0,
194 "seek after writing $type to a FH");
195 ok(print($fh "SUFFIX\n"),
196 "write to FH after writing $type");
197 ok($fh->close, "closing FH after writing $type");
199 if (ok(open(DATA, "< $file"), "opening data source")) {
201 my $data = do { local $/; <DATA> };
204 # writing to a buffer
205 print "# writing $type to a buffer\n";
207 ok($wimg->write(data=>\$buf, %extraopts, type=>$type),
208 "writing $type to a buffer", $wimg);
210 open DATA, "> testout/t50_buf.$type"
211 or die "Cannot create $type buffer file: $!";
215 ok($data eq $buf, "comparing file data to buffer");
223 if ($seekpos > length $buf) {
224 $buf .= "\0" x ($seekpos - length $buf);
226 substr($buf, $seekpos, length $what) = $what;
227 $seekpos += length $what;
228 $did_close = 0; # the close must be last
233 my ($size, $maxread) = @_;
234 my $out = substr($buf, $seekpos, $size);
235 $seekpos += length $out;
240 my ($size, $maxread) = @_;
241 my $out = substr($buf, $seekpos, $maxread);
242 $seekpos += length $out;
248 my ($offset, $whence) = @_;
249 #print "io_seeker($offset, $whence)\n";
250 if ($whence == SEEK_SET) {
253 elsif ($whence == SEEK_CUR) {
257 $seekpos = length($buf) + $offset;
259 #print "-> $seekpos\n";
263 my $closer = sub { ++$did_close; };
265 print "# writing $type via callbacks (mb=1)\n";
266 ok($wimg->write(writecb=>$writer, seekcb=>$seeker, closecb=>$closer,
268 %extraopts, type=>$type, maxbuffer=>1),
269 "writing $type to callback (mb=1)", $wimg);
271 ok($did_close, "checking closecb called");
273 ok($data eq $buf, "comparing callback output to file data");
274 print "# writing $type via callbacks (no mb)\n";
278 # we don't use the closecb here - used to make sure we don't get
279 # a warning/error on an attempt to call an undef close sub
280 ok($wimg->write(writecb=>$writer, seekcb=>$seeker, readcb=>$reader_min,
281 %extraopts, type=>$type),
282 "writing $type to callback (no mb)", $wimg);
284 ok($data eq $buf, "comparing callback output to file data");
287 skip("couldn't open data source", 7);
291 my $img2 = $img->crop(width=>50, height=>50);
292 $img2 -> write(file=> 'testout/t50.ppm', type=>'pnm');
296 # multi image/file tests
297 print "# multi-image write tests\n";
298 for my $type (@mtypes) {
299 next unless $hsh{$type};
302 my $file = "testout/t50out.$type";
303 my $wimg = Imager->new;
305 # if this doesn't work, we're so screwed up anyway
306 ok($wimg->read(file=>"testout/t50out.$type"),
307 "reading base file", $wimg);
309 ok(my $wimg2 = $wimg->copy, "copying base image", $wimg);
310 ok($wimg2->flip(dir=>'h'), "flipping base image", $wimg2);
312 my @out = ($wimg, $wimg2);
314 %extraopts = %{$writeopts{$type}} if $writeopts{$type};
315 ok(Imager->write_multi({ file=>"testout/t50_multi.$type", %extraopts },
317 "writing multiple to a file", "Imager");
319 # make sure we get the same back
320 my @images = Imager->read_multi(file=>"testout/t50_multi.$type");
321 if (ok(@images == @out, "checking read image count")) {
322 for my $i (0 .. $#out) {
323 my $diff = Imager::i_img_diff($out[$i]{IMG}, $images[$i]{IMG});
324 print "# diff $diff\n";
325 ok($diff == 0, "comparing image $i");
329 skip("wrong number of images read", 2);
334 Imager::malloc_state();
339 my ($ok, $msg, $img, $why, $skipcount) = @_;
343 print "ok $test_num # $msg\n";
344 Imager::i_log_entry("ok $test_num # $msg\n", 0);
348 $err = $img->errstr if $img;
349 # VMS (if we ever support it) wants the whole line in one print
350 my $line = "not ok $test_num # line ".(caller)[2].": $msg";
351 $line .= ": $err" if $err;
353 Imager::i_log_entry($line."\n", 0);
355 skip($why, $skipcount) if defined $why;
360 my ($why, $skipcount) = @_;
363 for (1.. $skipcount) {
365 print "ok $test_num # skipped $why\n";