5 use Test::More tests => 9;
7 init_log("testout/t101jpeg.log",1);
9 my $green=i_color_new(0,255,0,255);
10 my $blue=i_color_new(0,0,255,255);
11 my $red=i_color_new(255,0,0,255);
13 my $img=Imager::ImgRaw::new(150,150,3);
14 my $cmpimg=Imager::ImgRaw::new(150,150,3);
16 i_box_filled($img,70,25,130,125,$green);
17 i_box_filled($img,20,25,80,125,$blue);
18 i_arc($img,75,75,30,0,361,$red);
19 i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
21 i_has_format("jpeg") && print "# has jpeg\n";
22 if (!i_has_format("jpeg")) {
23 # previously we'd crash if we tried to save/read an image via the OO
24 # interface when there was no jpeg support
28 ok(!$im->read(file=>"testimg/base.jpg"), "should fail to read jpeg");
29 cmp_ok($im->errstr, '=~', qr/format 'jpeg' not supported/, "check no jpeg message");
30 $im = Imager->new(xsize=>2, ysize=>2);
31 ok(!$im->write(file=>"testout/nojpeg.jpg"), "should fail to write jpeg");
32 cmp_ok($im->errstr, '=~', qr/format not supported/, "check no jpeg message");
33 skip("no jpeg support", 5);
36 open(FH,">testout/t101.jpg") || die "cannot open testout/t101.jpg for writing\n";
38 my $IO = Imager::io_new_fd(fileno(FH));
39 ok(i_writejpeg_wiol($img,$IO,30), "write jpeg low level");
42 open(FH, "testout/t101.jpg") || die "cannot open testout/t101.jpg\n";
44 $IO = Imager::io_new_fd(fileno(FH));
45 ($cmpimg,undef) = i_readjpeg_wiol($IO);
49 my $diff = sqrt(i_img_diff($img,$cmpimg))/150*150;
50 print "# jpeg average mean square pixel difference: ",$diff,"\n";
51 ok($cmpimg, "read jpeg low level");
53 ok($diff < 10000, "difference between original and jpeg within bounds");
55 Imager::log_entry("Starting 4\n", 1);
56 my $imoo = Imager->new;
57 ok($imoo->read(file=>'testout/t101.jpg'), "read jpeg OO");
58 ok($imoo->write(file=>'testout/t101_oo.jpg'), "write jpeg OO");
59 Imager::log_entry("Starting 5\n", 1);
60 my $oocmp = Imager->new;
61 ok($oocmp->read(file=>'testout/t101_oo.jpg'), "read jpeg OO for comparison");
63 $diff = sqrt(i_img_diff($imoo->{IMG},$oocmp->{IMG}))/150*150;
64 print "# OO image difference $diff\n";
65 ok($diff < 10000, "difference between original and jpeg within bounds");
68 open FH, "< testout/t101.jpg" or die "Cannot open testout/t101.jpg: $!";
70 ok(!$imoo->write(fd=>fileno(FH), type=>'jpeg'), 'failure handling');
72 print "# ",$imoo->errstr,"\n";
74 # check that the i_format tag is set
75 my @fmt = $imoo->tags(name=>'i_format');
76 is($fmt[0], 'jpeg', 'i_format tag');