5 use Test::More tests => 49;
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", 45);
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");
59 ok($imoo->write(file=>'testout/t101_oo.jpg'), "write jpeg OO");
60 Imager::log_entry("Starting 5\n", 1);
61 my $oocmp = Imager->new;
62 ok($oocmp->read(file=>'testout/t101_oo.jpg'), "read jpeg OO for comparison");
64 $diff = sqrt(i_img_diff($imoo->{IMG},$oocmp->{IMG}))/150*150;
65 print "# OO image difference $diff\n";
66 ok($diff < 10000, "difference between original and jpeg within bounds");
69 open FH, "< testout/t101.jpg" or die "Cannot open testout/t101.jpg: $!";
71 ok(!$imoo->write(fd=>fileno(FH), type=>'jpeg'), 'failure handling');
73 print "# ",$imoo->errstr,"\n";
75 # check that the i_format tag is set
76 my @fmt = $imoo->tags(name=>'i_format');
77 is($fmt[0], 'jpeg', 'i_format tag');
79 { # check file limits are checked
80 my $limit_file = "testout/t101.jpg";
81 ok(Imager->set_file_limits(reset=>1, width=>149), "set width limit 149");
83 ok(!$im->read(file=>$limit_file),
84 "should fail read due to size limits");
85 print "# ",$im->errstr,"\n";
86 like($im->errstr, qr/image width/, "check message");
88 ok(Imager->set_file_limits(reset=>1, height=>149), "set height limit 149");
89 ok(!$im->read(file=>$limit_file),
90 "should fail read due to size limits");
91 print "# ",$im->errstr,"\n";
92 like($im->errstr, qr/image height/, "check message");
94 ok(Imager->set_file_limits(reset=>1, width=>150), "set width limit 150");
95 ok($im->read(file=>$limit_file),
96 "should succeed - just inside width limit");
97 ok(Imager->set_file_limits(reset=>1, height=>150), "set height limit 150");
98 ok($im->read(file=>$limit_file),
99 "should succeed - just inside height limit");
101 # 150 x 150 x 3 channel image uses 67500 bytes
102 ok(Imager->set_file_limits(reset=>1, bytes=>67499),
103 "set bytes limit 67499");
104 ok(!$im->read(file=>$limit_file),
105 "should fail - too many bytes");
106 print "# ",$im->errstr,"\n";
107 like($im->errstr, qr/storage size/, "check error message");
108 ok(Imager->set_file_limits(reset=>1, bytes=>67500),
109 "set bytes limit 67500");
110 ok($im->read(file=>$limit_file),
111 "should succeed - just inside bytes limit");
112 Imager->set_file_limits(reset=>1);
117 # we don't test them all
120 exif_date_time_original => "2005:11:25 00:00:00",
122 exif_image_description => "Imager Development Notes",
123 exif_make => "Canon",
124 exif_model => "CanoScan LiDE 35",
125 exif_resolution_unit => 2,
126 exif_resolution_unit_name => "inches",
127 exif_user_comment => " Part of notes from reworking i_arc() and friends.",
128 exif_white_balance => 0,
129 exif_white_balance_name => "Auto white balance",
133 Imager::i_exif_enabled()
134 or skip("no exif support", scalar keys %expected_tags);
136 my $im = Imager->new;
137 $im->read(file=>"testimg/exiftest.jpg")
138 or skip("Could not read test image:".$im->errstr, scalar keys %expected_tags);
140 for my $key (keys %expected_tags) {
141 is($expected_tags{$key}, $im->tags(name => $key),
142 "test value of exif tag $key");
147 # tests that the density values are set and read correctly
148 # tests jpeg_comment too
153 jpeg_density_unit => 2,
158 jpeg_density_unit => 2,
161 i_aspect_only => undef,
172 jpeg_density_unit => 1,
173 i_aspect_only => undef,
184 jpeg_density_unit => 1,
185 i_aspect_only => undef,
199 jpeg_density_unit => 0,
205 jpeg_comment => 'test comment'
210 print "# test density tags\n";
211 # I don't care about the content
212 my $base_im = Imager->new(xsize => 10, ysize => 10);
213 for my $test (@density_tests) {
214 my ($filename, $out_tags, $expect_tags) = @$test;
215 $expect_tags ||= $out_tags;
217 my $work = $base_im->copy;
218 for my $key (keys %$out_tags) {
219 $work->addtag(name => $key, value => $out_tags->{$key});
222 ok($work->write(file=>"testout/$filename", type=>'jpeg'),
225 my $check = Imager->new;
226 ok($check->read(file=> "testout/$filename"),
230 for my $key (keys %$expect_tags) {
231 $tags{$key} = $check->tags(name=>$key);
233 is_deeply($expect_tags, \%tags, "check tags for $filename");