]> git.imager.perl.org - imager.git/blob - t/t101jpeg.t
get it right
[imager.git] / t / t101jpeg.t
1 use Imager qw(:all);
2
3 print "1..8\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..8) {
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   my $imoo = Imager->new;
48   $imoo->read(file=>'testout/t101.jpg') or print "not ";
49   print "ok 4\n";
50   $imoo->write(file=>'testout/t101_oo.jpg') or print "not ";
51   print "ok 5\n";
52   my $oocmp = Imager->new;
53   $oocmp->read(file=>'testout/t101_oo.jpg') or print "not ";
54   print "ok 6\n";
55
56   $diff = sqrt(i_img_diff($imoo->{IMG},$oocmp->{IMG}))/150*150;
57   print "# OO image difference $diff\n";
58   $diff < 10000 or print "not ";
59   print "ok 7\n";
60
61   # write failure test
62   open FH, "< testout/t101.jpg" or die "Cannot open testout/t101.jpg: $!";
63   binmode FH;
64   ok(8, !$imoo->write(fd=>fileno(FH), type=>'jpeg'), 'failure handling');
65   close FH;
66   print "# ",$imoo->errstr,"\n";
67 }
68
69 sub ok {
70   my ($num, $test, $msg) = @_;
71
72   if ($test) {
73     print "ok $num\n";
74   }
75   else {
76     print "not ok $num # $msg\n";
77   }
78 }