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 Imager::init("log"=>"testout/t50basicoo.log");
16 # single image/file types
17 my @types = qw( jpeg png raw ppm gif tiff bmp tga );
19 # multiple image/file formats
20 my @mtypes = qw(tiff gif);
22 my %hsh=%Imager::formats;
26 for my $type (@types) {
27 $count += 31 if $hsh{$type};
29 for my $type (@mtypes) {
30 $count += 7 if $hsh{$type};
35 print "# avaliable formats:\n";
36 for(keys %hsh) { print "# $_\n"; }
40 my $img = Imager->new();
43 @files{@types} = ({ file => "testout/t101.jpg" },
44 { file => "testout/t102.png" },
45 { file => "testout/t103.raw", xsize=>150, ysize=>150, type=>'raw', interleave => 0},
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" }, );
53 gif=> { make_colors=>'webmap', translate=>'closest', gifquant=>'gen',
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";
69 # read from a file handle
70 my $fh = IO::File->new($opts{file}, "r");
71 if (ok($fh, "opening $opts{file}")) {
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");
81 skip("no image to compare");
83 ok($fh->seek(0, SEEK_SET), "seek after read");
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");
93 skip("no image to compare");
95 ok($fh->seek(0, SEEK_SET), "seek after fd read");
96 ok($fh->close, "close fh after reads");
99 skip("couldn't open the damn file: $!", 7);
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}: $!";
107 my $data = do { local $/; <DATA> };
109 my $bimg = Imager->new;
111 if (ok($bimg->read(data=>$data, %mopts, type=>$type), "read from buffer",
113 ok(Imager::i_img_diff($img->{IMG}, $bimg->{IMG}) == 0,
114 "comparing buffer read image");
117 skip("nothing to compare");
120 # read from callbacks, both with minimum and maximum reads
125 my ($size, $maxread) = @_;
126 my $out = substr($buf, $seekpos, $size);
127 $seekpos += length $out;
132 my ($size, $maxread) = @_;
133 my $out = substr($buf, $seekpos, $maxread);
134 $seekpos += length $out;
139 my ($offset, $whence) = @_;
140 #print "io_seeker($offset, $whence)\n";
141 if ($whence == SEEK_SET) {
144 elsif ($whence == SEEK_CUR) {
148 $seekpos = length($buf) + $offset;
150 #print "-> $seekpos\n";
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");
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");
165 skip("giflib < 4 doesn't support callbacks", 6);
169 for my $type (@types) {
170 next unless $hsh{$type};
172 print "# write tests for $type\n";
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
179 ok($wimg->read(file=>"testout/t104.ppm"),
180 "cannot read base file", $wimg);
183 print "# writing $type to a file\n";
185 %extraopts = %{$writeopts{$type}} if $writeopts{$type};
186 ok($wimg->write(file=>$file, %extraopts),
187 "writing $type to a file $file", $wimg);
189 print "# writing $type to a FH\n";
191 my $fh = IO::File->new($file, "w+")
192 or die "Could not create $file: $!";
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");
202 if ($type ne 'gif' ||
203 (Imager::i_giflib_version() >= 4 && !-e $buggy_giflib_file)) {
204 if (ok(open(DATA, "< $file"), "opening data source")) {
206 my $data = do { local $/; <DATA> };
209 # writing to a buffer
210 print "# writing $type to a buffer\n";
212 ok($wimg->write(data=>\$buf, %extraopts, type=>$type),
213 "writing $type to a buffer", $wimg);
215 open DATA, "> testout/t50_buf.$type"
216 or die "Cannot create $type buffer file: $!";
220 ok($data eq $buf, "comparing file data to buffer");
228 if ($seekpos > length $buf) {
229 $buf .= "\0" x ($seekpos - length $buf);
231 substr($buf, $seekpos, length $what) = $what;
232 $seekpos += length $what;
233 $did_close = 0; # the close must be last
238 my ($size, $maxread) = @_;
239 my $out = substr($buf, $seekpos, $size);
240 $seekpos += length $out;
245 my ($size, $maxread) = @_;
246 my $out = substr($buf, $seekpos, $maxread);
247 $seekpos += length $out;
253 my ($offset, $whence) = @_;
254 #print "io_seeker($offset, $whence)\n";
255 if ($whence == SEEK_SET) {
258 elsif ($whence == SEEK_CUR) {
262 $seekpos = length($buf) + $offset;
264 #print "-> $seekpos\n";
268 my $closer = sub { ++$did_close; };
270 print "# writing $type via callbacks (mb=1)\n";
271 ok($wimg->write(writecb=>$writer, seekcb=>$seeker, closecb=>$closer,
273 %extraopts, type=>$type, maxbuffer=>1),
274 "writing $type to callback (mb=1)", $wimg);
276 ok($did_close, "checking closecb called");
278 ok($data eq $buf, "comparing callback output to file data");
279 print "# writing $type via callbacks (no mb)\n";
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);
289 ok($data eq $buf, "comparing callback output to file data");
292 skip("couldn't open data source", 7);
296 if (-e $buggy_giflib_file) {
297 skip("see $buggy_giflib_file", 8);
300 skip("giflib < 4 doesn't support callbacks", 8);
305 my $img2 = $img->crop(width=>50, height=>50);
306 $img2 -> write(file=> 'testout/t50.ppm', type=>'pnm');
310 # multi image/file tests
311 print "# multi-image write tests\n";
312 for my $type (@mtypes) {
313 next unless $hsh{$type};
316 my $file = "testout/t50out.$type";
317 my $wimg = Imager->new;
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);
323 ok(my $wimg2 = $wimg->copy, "copying base image", $wimg);
324 ok($wimg2->flip(dir=>'h'), "flipping base image", $wimg2);
326 my @out = ($wimg, $wimg2);
328 %extraopts = %{$writeopts{$type}} if $writeopts{$type};
329 ok(Imager->write_multi({ file=>"testout/t50_multi.$type", %extraopts },
331 "writing multiple to a file", "Imager");
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");
343 skip("wrong number of images read", 2);
348 Imager::malloc_state();
353 my ($ok, $msg, $img, $why, $skipcount) = @_;
357 print "ok $test_num # $msg\n";
358 Imager::i_log_entry("ok $test_num # $msg\n", 0);
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;
367 Imager::i_log_entry($line."\n", 0);
369 skip($why, $skipcount) if defined $why;
374 my ($why, $skipcount) = @_;
377 for (1.. $skipcount) {
379 print "ok $test_num # skipped $why\n";