]> git.imager.perl.org - imager.git/blame - t/t101jpeg.t
base for hand edited JPEG file
[imager.git] / t / t101jpeg.t
CommitLineData
20adc63d
TC
1use Imager qw(:all);
2
2c2c832a 3print "1..9\n";
20adc63d
TC
4
5init_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
14i_box_filled($img,70,25,130,125,$green);
15i_box_filled($img,20,25,80,125,$blue);
16i_arc($img,75,75,30,0,361,$red);
17i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
18
19i_has_format("jpeg") && print "# has jpeg\n";
20if (!i_has_format("jpeg")) {
2c2c832a 21 for (1..9) {
cf692b64
TC
22 print "ok $_ # skip no jpeg support\n";
23 }
20adc63d 24} else {
e2cb7e23 25 open(FH,">testout/t101.jpg") || die "cannot open testout/t101.jpg for writing\n";
20adc63d 26 binmode(FH);
dd55acc8
AMH
27 $IO = Imager::io_new_fd(fileno(FH));
28 i_writejpeg_wiol($img,$IO,30);
20adc63d
TC
29 close(FH);
30
31 print "ok 1\n";
32
dd55acc8 33 open(FH, "testout/t101.jpg") || die "cannot open testout/t101.jpg\n";
20adc63d 34 binmode(FH);
dd55acc8
AMH
35 $IO = Imager::io_new_fd(fileno(FH));
36 ($cmpimg,undef) = i_readjpeg_wiol($IO);
20adc63d
TC
37 close(FH);
38
dd55acc8 39 print "$cmpimg\n";
cf692b64
TC
40 my $diff = sqrt(i_img_diff($img,$cmpimg))/150*150;
41 print "# jpeg average mean square pixel difference: ",$diff,"\n";
20adc63d 42 print "ok 2\n";
cf692b64
TC
43
44 $diff < 10000 or print "not ";
45 print "ok 3\n";
46
84e51293 47 Imager::log_entry("Starting 4\n", 1);
cf692b64
TC
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 ";
84e51293 52 Imager::log_entry("Starting 5\n", 1);
cf692b64
TC
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";
f873cb01
TC
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";
2c2c832a
TC
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');
f873cb01
TC
73}
74
75sub 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 }
20adc63d 84}