]> git.imager.perl.org - imager.git/blob - t/t101jpeg.t
base for hand-edited test tga images
[imager.git] / t / t101jpeg.t
1 use Imager qw(:all);
2
3 print "1..9\n";
4
5 init_log("testout/t101jpeg.log",1);
6
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);
10
11 $img=Imager::ImgRaw::new(150,150,3);
12 $cmpimg=Imager::ImgRaw::new(150,150,3);
13
14 i_box_filled($img,70,25,130,125,$green);
15 i_box_filled($img,20,25,80,125,$blue);
16 i_arc($img,75,75,30,0,361,$red);
17 i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
18
19 i_has_format("jpeg") && print "# has jpeg\n";
20 if (!i_has_format("jpeg")) {
21   for (1..9) {
22     print "ok $_ # skip no jpeg support\n";
23   }
24 } else {
25   open(FH,">testout/t101.jpg") || die "cannot open testout/t101.jpg for writing\n";
26   binmode(FH);
27   $IO = Imager::io_new_fd(fileno(FH));
28   i_writejpeg_wiol($img,$IO,30);
29   close(FH);
30
31   print "ok 1\n";
32   
33   open(FH, "testout/t101.jpg") || die "cannot open testout/t101.jpg\n";
34   binmode(FH);
35   $IO = Imager::io_new_fd(fileno(FH));
36   ($cmpimg,undef) = i_readjpeg_wiol($IO);
37   close(FH);
38
39   print "$cmpimg\n";
40   my $diff = sqrt(i_img_diff($img,$cmpimg))/150*150;
41   print "# jpeg average mean square pixel difference: ",$diff,"\n";
42   print "ok 2\n";
43
44   $diff < 10000 or print "not ";
45   print "ok 3\n";
46
47         Imager::log_entry("Starting 4\n", 1);
48   my $imoo = Imager->new;
49   $imoo->read(file=>'testout/t101.jpg') or print "not ";
50   print "ok 4\n";
51   $imoo->write(file=>'testout/t101_oo.jpg') or print "not ";
52         Imager::log_entry("Starting 5\n", 1);
53   print "ok 5\n";
54   my $oocmp = Imager->new;
55   $oocmp->read(file=>'testout/t101_oo.jpg') or print "not ";
56   print "ok 6\n";
57
58   $diff = sqrt(i_img_diff($imoo->{IMG},$oocmp->{IMG}))/150*150;
59   print "# OO image difference $diff\n";
60   $diff < 10000 or print "not ";
61   print "ok 7\n";
62
63   # write failure test
64   open FH, "< testout/t101.jpg" or die "Cannot open testout/t101.jpg: $!";
65   binmode FH;
66   ok(8, !$imoo->write(fd=>fileno(FH), type=>'jpeg'), 'failure handling');
67   close FH;
68   print "# ",$imoo->errstr,"\n";
69
70   # check that the i_format tag is set
71   my @fmt = $imoo->tags(name=>'i_format');
72   ok(9, @fmt == 1 && $fmt[0] eq 'jpeg', 'i_format tag');
73 }
74
75 sub ok {
76   my ($num, $test, $msg) = @_;
77
78   if ($test) {
79     print "ok $num\n";
80   }
81   else {
82     print "not ok $num # $msg\n";
83   }
84 }