| 1 |
#!perl -w |
| 2 |
use strict; |
| 3 |
use Imager qw(:all); |
| 4 |
use Test::More; |
| 5 |
use Imager::Test qw(is_color_close3 test_image_raw test_image is_image); |
| 6 |
|
| 7 |
-d "testout" or mkdir "testout"; |
| 8 |
|
| 9 |
init_log("testout/t101jpeg.log",1); |
| 10 |
|
| 11 |
$Imager::formats{"jpeg"} |
| 12 |
or plan skip_all => "no jpeg support"; |
| 13 |
|
| 14 |
diag "libjpeg version: ", Imager::File::JPEG->libjpeg_version(), "\n"; |
| 15 |
|
| 16 |
my $green=i_color_new(0,255,0,255); |
| 17 |
my $blue=i_color_new(0,0,255,255); |
| 18 |
my $red=i_color_new(255,0,0,255); |
| 19 |
|
| 20 |
my $img=test_image_raw(); |
| 21 |
my $cmpimg=Imager::ImgRaw::new(150,150,3); |
| 22 |
|
| 23 |
open(FH,">testout/t101.jpg") |
| 24 |
|| die "cannot open testout/t101.jpg for writing\n"; |
| 25 |
binmode(FH); |
| 26 |
my $IO = Imager::io_new_fd(fileno(FH)); |
| 27 |
ok(Imager::File::JPEG::i_writejpeg_wiol($img,$IO,30), "write jpeg low level"); |
| 28 |
close(FH); |
| 29 |
|
| 30 |
open(FH, "testout/t101.jpg") || die "cannot open testout/t101.jpg\n"; |
| 31 |
binmode(FH); |
| 32 |
$IO = Imager::io_new_fd(fileno(FH)); |
| 33 |
($cmpimg,undef) = Imager::File::JPEG::i_readjpeg_wiol($IO); |
| 34 |
close(FH); |
| 35 |
|
| 36 |
my $diff = sqrt(i_img_diff($img,$cmpimg))/150*150; |
| 37 |
print "# jpeg average mean square pixel difference: ",$diff,"\n"; |
| 38 |
ok($cmpimg, "read jpeg low level"); |
| 39 |
|
| 40 |
ok($diff < 10000, "difference between original and jpeg within bounds"); |
| 41 |
|
| 42 |
Imager::i_log_entry("Starting 4\n", 1); |
| 43 |
my $imoo = Imager->new; |
| 44 |
ok($imoo->read(file=>'testout/t101.jpg'), "read jpeg OO"); |
| 45 |
|
| 46 |
ok($imoo->write(file=>'testout/t101_oo.jpg'), "write jpeg OO"); |
| 47 |
Imager::i_log_entry("Starting 5\n", 1); |
| 48 |
my $oocmp = Imager->new; |
| 49 |
ok($oocmp->read(file=>'testout/t101_oo.jpg'), "read jpeg OO for comparison"); |
| 50 |
|
| 51 |
$diff = sqrt(i_img_diff($imoo->{IMG},$oocmp->{IMG}))/150*150; |
| 52 |
print "# OO image difference $diff\n"; |
| 53 |
ok($diff < 10000, "difference between original and jpeg within bounds"); |
| 54 |
|
| 55 |
# write failure test |
| 56 |
open FH, "< testout/t101.jpg" or die "Cannot open testout/t101.jpg: $!"; |
| 57 |
binmode FH; |
| 58 |
my $io = Imager::io_new_fd(fileno(FH)); |
| 59 |
$io->set_buffered(0); |
| 60 |
ok(!$imoo->write(io => $io, type=>'jpeg'), 'failure handling'); |
| 61 |
close FH; |
| 62 |
print "# ",$imoo->errstr,"\n"; |
| 63 |
|
| 64 |
# check that the i_format tag is set |
| 65 |
my @fmt = $imoo->tags(name=>'i_format'); |
| 66 |
is($fmt[0], 'jpeg', 'i_format tag'); |
| 67 |
|
| 68 |
{ # check file limits are checked |
| 69 |
my $limit_file = "testout/t101.jpg"; |
| 70 |
ok(Imager->set_file_limits(reset=>1, width=>149), "set width limit 149"); |
| 71 |
my $im = Imager->new; |
| 72 |
ok(!$im->read(file=>$limit_file), |
| 73 |
"should fail read due to size limits"); |
| 74 |
print "# ",$im->errstr,"\n"; |
| 75 |
like($im->errstr, qr/image width/, "check message"); |
| 76 |
|
| 77 |
ok(Imager->set_file_limits(reset=>1, height=>149), "set height limit 149"); |
| 78 |
ok(!$im->read(file=>$limit_file), |
| 79 |
"should fail read due to size limits"); |
| 80 |
print "# ",$im->errstr,"\n"; |
| 81 |
like($im->errstr, qr/image height/, "check message"); |
| 82 |
|
| 83 |
ok(Imager->set_file_limits(reset=>1, width=>150), "set width limit 150"); |
| 84 |
ok($im->read(file=>$limit_file), |
| 85 |
"should succeed - just inside width limit"); |
| 86 |
ok(Imager->set_file_limits(reset=>1, height=>150), "set height limit 150"); |
| 87 |
ok($im->read(file=>$limit_file), |
| 88 |
"should succeed - just inside height limit"); |
| 89 |
|
| 90 |
# 150 x 150 x 3 channel image uses 67500 bytes |
| 91 |
ok(Imager->set_file_limits(reset=>1, bytes=>67499), |
| 92 |
"set bytes limit 67499"); |
| 93 |
ok(!$im->read(file=>$limit_file), |
| 94 |
"should fail - too many bytes"); |
| 95 |
print "# ",$im->errstr,"\n"; |
| 96 |
like($im->errstr, qr/storage size/, "check error message"); |
| 97 |
ok(Imager->set_file_limits(reset=>1, bytes=>67500), |
| 98 |
"set bytes limit 67500"); |
| 99 |
ok($im->read(file=>$limit_file), |
| 100 |
"should succeed - just inside bytes limit"); |
| 101 |
Imager->set_file_limits(reset=>1); |
| 102 |
} |
| 103 |
|
| 104 |
SKIP: |
| 105 |
{ |
| 106 |
# we don't test them all |
| 107 |
my %expected_tags = |
| 108 |
( |
| 109 |
exif_date_time_original => "2005:11:25 00:00:00", |
| 110 |
exif_flash => 0, |
| 111 |
exif_image_description => "Imager Development Notes", |
| 112 |
exif_make => "Canon", |
| 113 |
exif_model => "CanoScan LiDE 35", |
| 114 |
exif_resolution_unit => 2, |
| 115 |
exif_resolution_unit_name => "inches", |
| 116 |
exif_user_comment => " Part of notes from reworking i_arc() and friends.", |
| 117 |
exif_white_balance => 0, |
| 118 |
exif_white_balance_name => "Auto white balance", |
| 119 |
); |
| 120 |
|
| 121 |
my $im = Imager->new; |
| 122 |
$im->read(file=>"testimg/exiftest.jpg") |
| 123 |
or skip("Could not read test image:".$im->errstr, scalar keys %expected_tags); |
| 124 |
|
| 125 |
for my $key (keys %expected_tags) { |
| 126 |
is($expected_tags{$key}, $im->tags(name => $key), |
| 127 |
"test value of exif tag $key"); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
{ |
| 132 |
# tests that the density values are set and read correctly |
| 133 |
# tests jpeg_comment too |
| 134 |
my @density_tests = |
| 135 |
( |
| 136 |
[ 't101cm100.jpg', |
| 137 |
{ |
| 138 |
jpeg_density_unit => 2, |
| 139 |
i_xres => 254, |
| 140 |
i_yres => 254 |
| 141 |
}, |
| 142 |
{ |
| 143 |
jpeg_density_unit => 2, |
| 144 |
i_xres => 254, |
| 145 |
i_yres => 254, |
| 146 |
i_aspect_only => undef, |
| 147 |
}, |
| 148 |
], |
| 149 |
[ |
| 150 |
't101xonly.jpg', |
| 151 |
{ |
| 152 |
i_xres => 100, |
| 153 |
}, |
| 154 |
{ |
| 155 |
i_xres => 100, |
| 156 |
i_yres => 100, |
| 157 |
jpeg_density_unit => 1, |
| 158 |
i_aspect_only => undef, |
| 159 |
}, |
| 160 |
], |
| 161 |
[ |
| 162 |
't101yonly.jpg', |
| 163 |
{ |
| 164 |
i_yres => 100, |
| 165 |
}, |
| 166 |
{ |
| 167 |
i_xres => 100, |
| 168 |
i_yres => 100, |
| 169 |
jpeg_density_unit => 1, |
| 170 |
i_aspect_only => undef, |
| 171 |
}, |
| 172 |
], |
| 173 |
[ |
| 174 |
't101asponly.jpg', |
| 175 |
{ |
| 176 |
i_xres => 50, |
| 177 |
i_yres => 100, |
| 178 |
i_aspect_only => 1, |
| 179 |
}, |
| 180 |
{ |
| 181 |
i_xres => 50, |
| 182 |
i_yres => 100, |
| 183 |
i_aspect_only => 1, |
| 184 |
jpeg_density_unit => 0, |
| 185 |
}, |
| 186 |
], |
| 187 |
[ |
| 188 |
't101com.jpg', |
| 189 |
{ |
| 190 |
jpeg_comment => 'test comment' |
| 191 |
}, |
| 192 |
], |
| 193 |
); |
| 194 |
|
| 195 |
print "# test density tags\n"; |
| 196 |
# I don't care about the content |
| 197 |
my $base_im = Imager->new(xsize => 10, ysize => 10); |
| 198 |
for my $test (@density_tests) { |
| 199 |
my ($filename, $out_tags, $expect_tags) = @$test; |
| 200 |
$expect_tags ||= $out_tags; |
| 201 |
|
| 202 |
my $work = $base_im->copy; |
| 203 |
for my $key (keys %$out_tags) { |
| 204 |
$work->addtag(name => $key, value => $out_tags->{$key}); |
| 205 |
} |
| 206 |
|
| 207 |
ok($work->write(file=>"testout/$filename", type=>'jpeg'), |
| 208 |
"save $filename"); |
| 209 |
|
| 210 |
my $check = Imager->new; |
| 211 |
ok($check->read(file=> "testout/$filename"), |
| 212 |
"read $filename"); |
| 213 |
|
| 214 |
my %tags; |
| 215 |
for my $key (keys %$expect_tags) { |
| 216 |
$tags{$key} = $check->tags(name=>$key); |
| 217 |
} |
| 218 |
is_deeply($expect_tags, \%tags, "check tags for $filename"); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
{ # Issue # 17981 |
| 223 |
# the test image has a zero-length user_comment field |
| 224 |
# the code would originally attempt to convert '\0' to ' ' |
| 225 |
# for the first 8 bytes, even if the string was less than |
| 226 |
# 8 bytes long |
| 227 |
my $im = Imager->new; |
| 228 |
ok($im->read(file => 'testimg/209_yonge.jpg', type=>'jpeg'), |
| 229 |
"test read of image with invalid exif_user_comment"); |
| 230 |
is($im->tags(name=>'exif_user_comment'), '', |
| 231 |
"check exif_user_comment set correctly"); |
| 232 |
} |
| 233 |
|
| 234 |
{ # test parseiptc handling no IPTC data correctly |
| 235 |
my $saw_warn; |
| 236 |
local $SIG{__WARN__} = |
| 237 |
sub { |
| 238 |
++$saw_warn; |
| 239 |
print "# @_\n"; |
| 240 |
}; |
| 241 |
my $im = Imager->new; |
| 242 |
ok($im->read(file => 'testout/t101.jpg', type=>'jpeg'), |
| 243 |
"read jpeg with no IPTC data"); |
| 244 |
ok(!defined $im->{IPTCRAW}, "no iptc data"); |
| 245 |
my %iptc = $im->parseiptc; |
| 246 |
ok(!$saw_warn, "should be no warnings"); |
| 247 |
} |
| 248 |
|
| 249 |
{ # Issue # 18397 |
| 250 |
# attempting to write a 4 channel image to a bufchain would |
| 251 |
# cause a seg fault. |
| 252 |
# it should fail still |
| 253 |
# overridden by # 29876 |
| 254 |
# give 4/2 channel images a background color when saving to JPEG |
| 255 |
my $im = Imager->new(xsize => 16, ysize => 16, channels => 4); |
| 256 |
$im->box(filled => 1, xmin => 8, color => '#FFE0C0'); |
| 257 |
my $data; |
| 258 |
ok($im->write(data => \$data, type => 'jpeg'), |
| 259 |
"should write with a black background"); |
| 260 |
my $imread = Imager->new; |
| 261 |
ok($imread->read(data => $data, type => 'jpeg'), 'read it back'); |
| 262 |
is_color_close3($imread->getpixel('x' => 0, 'y' => 0), 0, 0, 0, 4, |
| 263 |
"check it's black"); |
| 264 |
is_color_close3($imread->getpixel('x' => 15, 'y' => 9), 255, 224, 192, 4, |
| 265 |
"check filled area filled"); |
| 266 |
|
| 267 |
# write with a red background |
| 268 |
$data = ''; |
| 269 |
ok($im->write(data => \$data, type => 'jpeg', i_background => '#FF0000'), |
| 270 |
"write with red background"); |
| 271 |
ok($imread->read(data => $data, type => 'jpeg'), "read it back"); |
| 272 |
is_color_close3($imread->getpixel('x' => 0, 'y' => 0), 255, 0, 0, 4, |
| 273 |
"check it's red"); |
| 274 |
is_color_close3($imread->getpixel('x' => 15, 'y' => 9), 255, 224, 192, 4, |
| 275 |
"check filled area filled"); |
| 276 |
} |
| 277 |
SKIP: |
| 278 |
{ # Issue # 18496 |
| 279 |
# If a jpeg with EXIF data containing an (invalid) IFD entry with a |
| 280 |
# type of zero is read then Imager crashes with a Floating point |
| 281 |
# exception |
| 282 |
# testimg/zerojpeg.jpg was manually modified from exiftest.jpg to |
| 283 |
# reproduce the problem. |
| 284 |
my $im = Imager->new; |
| 285 |
ok($im->read(file=>'testimg/zerotype.jpg'), "shouldn't crash"); |
| 286 |
} |
| 287 |
|
| 288 |
SKIP: |
| 289 |
{ # code coverage - make sure wiol_skip_input_data is called |
| 290 |
open BASEDATA, "< testimg/exiftest.jpg" |
| 291 |
or skip "can't open base data", 1; |
| 292 |
binmode BASEDATA; |
| 293 |
my $data = do { local $/; <BASEDATA> }; |
| 294 |
close BASEDATA; |
| 295 |
|
| 296 |
substr($data, 3, 1) eq "\xE1" |
| 297 |
or skip "base data isn't as expected", 1; |
| 298 |
# inserting a lot of marker data here means we take the branch in |
| 299 |
# wiol_skip_input_data that refills the buffer |
| 300 |
my $marker = "\xFF\xE9"; # APP9 marker |
| 301 |
$marker .= pack("n", 8192) . "x" x 8190; |
| 302 |
$marker x= 10; # make it take up a lot of space |
| 303 |
substr($data, 2, 0) = $marker; |
| 304 |
my $im = Imager->new; |
| 305 |
ok($im->read(data => $data), "read with a skip of data"); |
| 306 |
} |
| 307 |
|
| 308 |
SKIP: |
| 309 |
{ # code coverage - take the branch that provides a fake EOI |
| 310 |
open BASEDATA, "< testimg/exiftest.jpg" |
| 311 |
or skip "can't open base data", 1; |
| 312 |
binmode BASEDATA; |
| 313 |
my $data = do { local $/; <BASEDATA> }; |
| 314 |
close BASEDATA; |
| 315 |
substr($data, -1000) = ''; |
| 316 |
|
| 317 |
my $im = Imager->new; |
| 318 |
ok($im->read(data => $data), "read with image data truncated"); |
| 319 |
} |
| 320 |
|
| 321 |
{ # code coverage - make sure wiol_empty_output_buffer is called |
| 322 |
my $im = Imager->new(xsize => 1000, ysize => 1000); |
| 323 |
for my $x (0 .. 999) { |
| 324 |
$im->line(x1 => $x, y1 => 0, x2 => $x, y2 => 999, |
| 325 |
color => Imager::Color->new(rand 256, rand 256, rand 256)); |
| 326 |
} |
| 327 |
my $data; |
| 328 |
ok($im->write(data => \$data, type=>'jpeg', jpegquality => 100), |
| 329 |
"write big file to ensure wiol_empty_output_buffer is called") |
| 330 |
or print "# ", $im->errstr, "\n"; |
| 331 |
|
| 332 |
# code coverage - write failure path in wiol_empty_output_buffer |
| 333 |
ok(!$im->write(callback => sub { return }, |
| 334 |
type => 'jpeg', jpegquality => 100), |
| 335 |
"fail to write") |
| 336 |
and print "# ", $im->errstr, "\n"; |
| 337 |
} |
| 338 |
|
| 339 |
{ # code coverage - virtual image branch in i_writejpeg_wiol() |
| 340 |
my $im = $imoo->copy; |
| 341 |
my $immask = $im->masked; |
| 342 |
ok($immask, "made a virtual image (via masked)"); |
| 343 |
ok($immask->virtual, "check it's virtual"); |
| 344 |
my $mask_data; |
| 345 |
ok($immask->write(data => \$mask_data, type => 'jpeg'), |
| 346 |
"write masked version"); |
| 347 |
my $base_data; |
| 348 |
ok($im->write(data => \$base_data, type=>'jpeg'), |
| 349 |
"write normal version"); |
| 350 |
is($base_data, $mask_data, "check the data written matches"); |
| 351 |
} |
| 352 |
|
| 353 |
SKIP: |
| 354 |
{ # code coverage - IPTC data |
| 355 |
# this is dummy data |
| 356 |
my $iptc = "\x04\x04" . |
| 357 |
"\034\002x My Caption" |
| 358 |
. "\034\002P Tony Cook" |
| 359 |
. "\034\002i Dummy Headline!" |
| 360 |
. "\034\002n No Credit Given"; |
| 361 |
|
| 362 |
my $app13 = "\xFF\xED" . pack("n", 2 + length $iptc) . $iptc; |
| 363 |
|
| 364 |
open BASEDATA, "< testimg/exiftest.jpg" |
| 365 |
or skip "can't open base data", 1; |
| 366 |
binmode BASEDATA; |
| 367 |
my $data = do { local $/; <BASEDATA> }; |
| 368 |
close BASEDATA; |
| 369 |
substr($data, 2, 0) = $app13; |
| 370 |
|
| 371 |
my $im = Imager->new; |
| 372 |
ok($im->read(data => $data), "read with app13 data"); |
| 373 |
my %iptc = $im->parseiptc; |
| 374 |
is($iptc{caption}, 'My Caption', 'check iptc caption'); |
| 375 |
is($iptc{photogr}, 'Tony Cook', 'check iptc photogr'); |
| 376 |
is($iptc{headln}, 'Dummy Headline!', 'check iptc headln'); |
| 377 |
is($iptc{credit}, 'No Credit Given', 'check iptc credit'); |
| 378 |
} |
| 379 |
|
| 380 |
{ |
| 381 |
# leak with duplicate IPTC blocks |
| 382 |
# only detectable with valgrind, etc |
| 383 |
my $im = Imager->new; |
| 384 |
ok($im->read(file => "testimg/iptcdup.jpg"), |
| 385 |
"read jpeg with multiple iptc blocks") |
| 386 |
or diag "read iptcdup.jpg: ", Imager->errstr; |
| 387 |
} |
| 388 |
|
| 389 |
{ # handling of CMYK jpeg |
| 390 |
# http://rt.cpan.org/Ticket/Display.html?id=20416 |
| 391 |
my $im = Imager->new; |
| 392 |
ok($im->read(file => 'testimg/scmyk.jpg'), 'read a CMYK jpeg'); |
| 393 |
is($im->getchannels, 3, "check channel count"); |
| 394 |
my $col = $im->getpixel(x => 0, 'y' => 0); |
| 395 |
ok($col, "got the 'black' pixel"); |
| 396 |
# this is jpeg, so we can't compare colors exactly |
| 397 |
# older versions returned this pixel at a light color, but |
| 398 |
# it's black in the image |
| 399 |
my ($r, $g, $b) = $col->rgba; |
| 400 |
cmp_ok($r, '<', 10, 'black - red low'); |
| 401 |
cmp_ok($g, '<', 10, 'black - green low'); |
| 402 |
cmp_ok($b, '<', 10, 'black - blue low'); |
| 403 |
$col = $im->getpixel(x => 15, 'y' => 0); |
| 404 |
ok($col, "got the dark blue"); |
| 405 |
($r, $g, $b) = $col->rgba; |
| 406 |
cmp_ok($r, '<', 10, 'dark blue - red low'); |
| 407 |
cmp_ok($g, '<', 10, 'dark blue - green low'); |
| 408 |
cmp_ok($b, '>', 110, 'dark blue - blue middle (bottom)'); |
| 409 |
cmp_ok($b, '<', 130, 'dark blue - blue middle (top)'); |
| 410 |
$col = $im->getpixel(x => 0, 'y' => 15); |
| 411 |
ok($col, "got the red"); |
| 412 |
($r, $g, $b) = $col->rgba; |
| 413 |
cmp_ok($r, '>', 245, 'red - red high'); |
| 414 |
cmp_ok($g, '<', 10, 'red - green low'); |
| 415 |
cmp_ok($b, '<', 10, 'red - blue low'); |
| 416 |
} |
| 417 |
|
| 418 |
{ |
| 419 |
ok(grep($_ eq 'jpeg', Imager->read_types), "check jpeg in read types"); |
| 420 |
ok(grep($_ eq 'jpeg', Imager->write_types), "check jpeg in write types"); |
| 421 |
} |
| 422 |
|
| 423 |
{ # progressive JPEG |
| 424 |
# https://rt.cpan.org/Ticket/Display.html?id=68691 |
| 425 |
my $im = test_image(); |
| 426 |
my $progim = $im->copy; |
| 427 |
|
| 428 |
ok($progim->write(file => "testout/t10prog.jpg", type => "jpeg", |
| 429 |
jpeg_progressive => 1), |
| 430 |
"write progressive jpeg"); |
| 431 |
|
| 432 |
my $rdprog = Imager->new(file => "testout/t10prog.jpg"); |
| 433 |
ok($rdprog, "read progressive jpeg"); |
| 434 |
my @prog = $rdprog->tags(name => "jpeg_progressive"); |
| 435 |
is($prog[0], 1, "check progressive flag set on read"); |
| 436 |
|
| 437 |
my $data; |
| 438 |
ok($im->write(data => \$data, type => "jpeg"), |
| 439 |
"save as non-progressive to compare"); |
| 440 |
my $norm = Imager->new(data => $data); |
| 441 |
ok($norm, "read non-progressive file"); |
| 442 |
my @nonprog = $norm->tags(name => "jpeg_progressive"); |
| 443 |
is($nonprog[0], 0, "check progressive flag 0 for non prog file"); |
| 444 |
|
| 445 |
is_image($rdprog, $norm, "prog vs norm should be the same image"); |
| 446 |
} |
| 447 |
|
| 448 |
SKIP: |
| 449 |
{ # optimize coding |
| 450 |
my $im = test_image(); |
| 451 |
my $base; |
| 452 |
ok($im->write(data => \$base, type => "jpeg"), "save without optimize"); |
| 453 |
my $opt; |
| 454 |
ok($im->write(data => \$opt, type => "jpeg", jpeg_optimize => 1), |
| 455 |
"save with optimize"); |
| 456 |
cmp_ok(length $opt, '<', length $base, "check optimized is smaller"); |
| 457 |
my $im_base = Imager->new(data => $base, filetype => "jpeg"); |
| 458 |
ok($im_base, "read unoptimized back"); |
| 459 |
my $im_opt = Imager->new(data => $opt, filetype => "jpeg"); |
| 460 |
ok($im_opt, "read optimized back"); |
| 461 |
$im_base && $im_opt |
| 462 |
or skip "couldn't read one back", 1; |
| 463 |
is_image($im_opt, $im_base, |
| 464 |
"optimization should only change huffman compression, not quality"); |
| 465 |
} |
| 466 |
|
| 467 |
SKIP: |
| 468 |
{ |
| 469 |
my $im = test_image(); |
| 470 |
# unknown jpeg_compress_profile always fails, doesn't matter whether we have mozjpeg |
| 471 |
my $data; |
| 472 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_compress_profile => "invalid"), |
| 473 |
"fail to write with unknown compression profile"); |
| 474 |
like($im->errstr, qr/jpeg_compress_profile=invalid unknown/, |
| 475 |
"check the error message"); |
| 476 |
|
| 477 |
ok($im->write(data => \$data, type => "jpeg", jpeg_compress_profile => "fastest"), |
| 478 |
"jpeg_compress_profile=fastest is always available"); |
| 479 |
Imager::File::JPEG->is_mozjpeg |
| 480 |
and skip "this is mozjpeg", 1; |
| 481 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_compress_profile => "max"), |
| 482 |
"fail to write with max compression profile without mozjpeg"); |
| 483 |
like($im->errstr, qr/jpeg_compress_profile=max requires mozjpeg/, |
| 484 |
"check the error message"); |
| 485 |
} |
| 486 |
|
| 487 |
SKIP: |
| 488 |
{ |
| 489 |
Imager::File::JPEG->is_mozjpeg |
| 490 |
or skip "this isn't mozjpeg", 1; |
| 491 |
my $im = test_image(); |
| 492 |
my $base_data; |
| 493 |
ok($im->write(data => \$base_data, type => "jpeg", jpeg_compress_profile => "fastest"), |
| 494 |
"write using defaults"); |
| 495 |
my $opt_data; |
| 496 |
ok($im->write(data => \$opt_data, type => "jpeg", jpeg_compress_profile => "max"), |
| 497 |
"write using max compression profile"); |
| 498 |
cmp_ok(length($opt_data), '<', length($base_data), |
| 499 |
"max compression should be smaller"); |
| 500 |
} |
| 501 |
|
| 502 |
SKIP: |
| 503 |
{ |
| 504 |
Imager::File::JPEG->has_arith_coding |
| 505 |
or skip "arithmetic coding not available", 1; |
| 506 |
my $im = test_image; |
| 507 |
my $data; |
| 508 |
ok($im->write(data => \$data, type => "jpeg", jpeg_arithmetic => 1), |
| 509 |
"write with arithmetic coding"); |
| 510 |
my $im2 = Imager->new(data => $data); |
| 511 |
ok($im2, "read back arithmetic coded"); |
| 512 |
ok($im2->tags(name => "jpeg_read_arithmetic"), |
| 513 |
"and read tag set"); |
| 514 |
} |
| 515 |
|
| 516 |
{ |
| 517 |
# default RGB (YCbCr) image has JFIF header |
| 518 |
my $im = test_image; |
| 519 |
my $data; |
| 520 |
ok($im->write(data => \$data, type => "jpeg"), |
| 521 |
"write default RGB"); |
| 522 |
my $im2 = Imager->new; |
| 523 |
ok($im2->read(data => $data), "read it back"); |
| 524 |
is($im2->tags(name => "jpeg_read_jfif"), 1, "JFIF detected"); |
| 525 |
ok($im->write(data => \$data, type => "jpeg", jpeg_jfif => 0), |
| 526 |
"disable the JFIF header"); |
| 527 |
ok($im2->read(data => $data), "read back file without JFIF"); |
| 528 |
is($im2->tags(name => "jpeg_read_jfif"), 0, "no JFIF detected"); |
| 529 |
} |
| 530 |
|
| 531 |
{ |
| 532 |
my $im = Imager->new(xsize => 150, ysize => 150); |
| 533 |
$im->box(filled => 1, color => "#F00", box => [ 15, 15, 84, 84 ]); |
| 534 |
$im->box(filled => 1, color => "#0F0", box => [ 30, 30, 69, 69 ]); |
| 535 |
# guesstimate |
| 536 |
my $smoothed = $im->copy->filter(type => 'gaussian', stddev => 1); |
| 537 |
my $data_base; |
| 538 |
ok($im->write(data => \$data_base, type => "jpeg"), "write base image (control)"); |
| 539 |
my $data_smoothed; |
| 540 |
ok($im->write(data => \$data_smoothed, type => "jpeg", jpeg_smooth => 50), |
| 541 |
"write smoothed image"); |
| 542 |
my $smim = Imager->new; |
| 543 |
ok($smim->read(data => $data_smoothed), "read smoothed back"); |
| 544 |
my $diffWvsS = Imager::i_img_diff($smim->{IMG}, $smoothed->{IMG}); |
| 545 |
my $diffWvsB = Imager::i_img_diff($smim->{IMG}, $im->{IMG}); |
| 546 |
cmp_ok($diffWvsS, '<', $diffWvsB, |
| 547 |
"written image should be closer to blurred image than base image"); |
| 548 |
|
| 549 |
my $data; |
| 550 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_smooth => 101), |
| 551 |
"invalid smoothing value"); |
| 552 |
like($im->errstr, qr/jpeg_smooth must be an integer from 0 to 100/, "check error message"); |
| 553 |
} |
| 554 |
|
| 555 |
{ |
| 556 |
# jpeg_restart |
| 557 |
my $im = test_image; |
| 558 |
my $data; |
| 559 |
ok($im->write(data => \$data, type => "jpeg"), |
| 560 |
"write with default restarts"); |
| 561 |
my $data_8_rows; |
| 562 |
ok($im->write(data => \$data_8_rows, type => "jpeg", jpeg_restart => 8), |
| 563 |
"write with restart every 8 rows"); |
| 564 |
my $data_8_mcus; |
| 565 |
ok($im->write(data => \$data_8_mcus, type => "jpeg", jpeg_restart => "8b"), |
| 566 |
"write with restart every 8 mcus"); |
| 567 |
cmp_ok(length($data), '<', length($data_8_rows), |
| 568 |
"no restarts smaller than restart every 8 rows"); |
| 569 |
cmp_ok(length($data_8_rows), '<', length($data_8_mcus), |
| 570 |
"restarts every 8 rows smaller than restart every 8 mcus"); |
| 571 |
|
| 572 |
# error handling |
| 573 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_restart => "8c"), |
| 574 |
"bad units value"); |
| 575 |
like($im->errstr, qr/jpeg_restart must be an integer from 0 to 65535 followed by an optional b/, |
| 576 |
"check error message"); |
| 577 |
$im->_set_error(""); |
| 578 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_restart => "65536"), |
| 579 |
"out of range value"); |
| 580 |
like($im->errstr, qr/jpeg_restart must be an integer from 0 to 65535 followed by an optional b/, |
| 581 |
"check error message"); |
| 582 |
} |
| 583 |
|
| 584 |
SKIP: |
| 585 |
{ |
| 586 |
# jpeg_sample |
| 587 |
my $im = test_image; |
| 588 |
my $data_base; |
| 589 |
ok($im->write(data => \$data_base, type => "jpeg"), |
| 590 |
"write default"); |
| 591 |
my $data_1x1; |
| 592 |
ok($im->write(data => \$data_1x1, type => "jpeg", jpeg_sample => "1x1"), |
| 593 |
"write with 1x1") |
| 594 |
or diag $im->errstr; |
| 595 |
cmp_ok(length($data_base), '<', length($data_1x1), |
| 596 |
"1x1 sampled file is larger"); |
| 597 |
my $baseim = Imager->new(data => $data_base) |
| 598 |
or skip "couldn't read back base image", 1; |
| 599 |
my $im1x1 = Imager->new(data => $data_1x1) |
| 600 |
or skip "couldn't read back 1x1 image", 1; |
| 601 |
my $diff_base = Imager::i_img_diff($im->{IMG}, $baseim->{IMG}); |
| 602 |
my $diff_1x1 = Imager::i_img_diff($im->{IMG}, $im1x1->{IMG}); |
| 603 |
cmp_ok($diff_1x1, '<', $diff_base, |
| 604 |
"1x1 sampled image should be closer to source image"); |
| 605 |
|
| 606 |
# varieties |
| 607 |
my $data_deftag; |
| 608 |
ok($im->write(data => \$data_deftag, type => "jpeg", jpeg_sample => "2x2,1x1,1x1"), |
| 609 |
"specify default by tag"); |
| 610 |
is(length($data_base), length($data_deftag), |
| 611 |
"default set by tag matches default"); |
| 612 |
|
| 613 |
my $data; |
| 614 |
my $errq = quotemeta "jpeg_sample: must match /^[1-4]x[1-4](,[1-4]x[1-4]){0,9}\$/aai"; |
| 615 |
my $errqr = qr/$errq/; |
| 616 |
# failures |
| 617 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_sample => "5x1"), |
| 618 |
"H out of range"); |
| 619 |
like($im->errstr, $errqr, "check error message"); |
| 620 |
$im->_set_error(""); |
| 621 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_sample => "1x5"), |
| 622 |
"V out of range"); |
| 623 |
like($im->errstr, $errqr, "check error message"); |
| 624 |
$im->_set_error(""); |
| 625 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_sample => "1x5."), |
| 626 |
"non-comma/eof after V"); |
| 627 |
like($im->errstr, $errqr, "check error message"); |
| 628 |
$im->_set_error(""); |
| 629 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_sample => "1y1"), |
| 630 |
"non X between H and V"); |
| 631 |
like($im->errstr, $errqr, "check error message"); |
| 632 |
$im->_set_error(""); |
| 633 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_sample => "1x1,"), |
| 634 |
"orphan comma at end"); |
| 635 |
like($im->errstr, $errqr, "check error message"); |
| 636 |
$im->_set_error(""); |
| 637 |
} |
| 638 |
|
| 639 |
my @bool_tags = |
| 640 |
qw(jpeg_optimize_scans jpeg_trellis_quant jpeg_trellis_quant_dc |
| 641 |
jpeg_tresllis_eob_opt jpeg_use_lambda_weight_tbl jpeg_use_scans_in_trellis |
| 642 |
jpeg_overshoot_deringing); |
| 643 |
|
| 644 |
my @float_tags = |
| 645 |
qw(jpeg_lambda_log_scale1 jpeg_lambda_log_scale2 jpeg_trellis_delta_dc_weight); |
| 646 |
|
| 647 |
my @int_tags = |
| 648 |
qw(jpeg_trellis_freq_split jpeg_trellis_num_loops jpeg_base_quant_tbl_idx |
| 649 |
jpeg_dc_scan_opt_mode); |
| 650 |
SKIP: |
| 651 |
{ |
| 652 |
Imager::File::JPEG->is_mozjpeg |
| 653 |
or skip "These tags are mozjpeg only", 1; |
| 654 |
|
| 655 |
# boolean tags |
| 656 |
for my $tag (@bool_tags) { |
| 657 |
my $im = test_image(); |
| 658 |
my $data; |
| 659 |
ok($im->write(data => \$data, type => "jpeg", $tag => 0), |
| 660 |
"write with boolean tag $tag"); |
| 661 |
} |
| 662 |
# float tags |
| 663 |
for my $tag (@float_tags) { |
| 664 |
my $im = test_image(); |
| 665 |
my $data; |
| 666 |
ok($im->write(data => \$data, type => "jpeg", $tag => 8.1), |
| 667 |
"write with float tag $tag"); |
| 668 |
} |
| 669 |
# int tags |
| 670 |
for my $tag (@int_tags) { |
| 671 |
my $im = test_image(); |
| 672 |
my $data; |
| 673 |
ok($im->write(data => \$data, type => "jpeg", $tag => 2), |
| 674 |
"write with int tag $tag"); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
SKIP: |
| 679 |
{ |
| 680 |
Imager::File::JPEG->is_mozjpeg |
| 681 |
and skip "Testing absence of mozjpeg", 1; |
| 682 |
|
| 683 |
# none of these tags should be settable |
| 684 |
# boolean tags |
| 685 |
for my $tag (@bool_tags) { |
| 686 |
my $im = test_image(); |
| 687 |
my $data; |
| 688 |
ok(!$im->write(data => \$data, type => "jpeg", $tag => 0), |
| 689 |
"fail write with boolean tag $tag"); |
| 690 |
} |
| 691 |
# float tags |
| 692 |
for my $tag (@float_tags) { |
| 693 |
my $im = test_image(); |
| 694 |
my $data; |
| 695 |
ok(!$im->write(data => \$data, type => "jpeg", $tag => 8.1), |
| 696 |
"fail write with float tag $tag"); |
| 697 |
} |
| 698 |
# int tags |
| 699 |
for my $tag (@int_tags) { |
| 700 |
my $im = test_image(); |
| 701 |
my $data; |
| 702 |
ok(!$im->write(data => \$data, type => "jpeg", $tag => 2), |
| 703 |
"fail write with int tag $tag"); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
SKIP: |
| 708 |
{ |
| 709 |
Imager::File::JPEG->is_mozjpeg |
| 710 |
or skip "jpeg_tune needs mozjpeg", 1; |
| 711 |
for my $tune (qw(psnr ssim ms-ssim hvs-psnr)) { |
| 712 |
my $data; |
| 713 |
my $im = test_image; |
| 714 |
ok($im->write(data => \$data, type => "jpeg", jpeg_tune => $tune), |
| 715 |
"jpeg_tune=$tune"); |
| 716 |
note "size ".length $data; |
| 717 |
} |
| 718 |
{ |
| 719 |
my $data; |
| 720 |
my $im = test_image; |
| 721 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_tune => "rubbish"), |
| 722 |
"fail jpeg_tune=rubbish"); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
SKIP: |
| 727 |
{ |
| 728 |
Imager::File::JPEG->is_mozjpeg |
| 729 |
and skip "testing absence of mozjpeg", 1; |
| 730 |
for my $tune (qw(psnr ssim ms-ssim hvs-psnr)) { |
| 731 |
my $data; |
| 732 |
my $im = test_image; |
| 733 |
ok(!$im->write(data => \$data, type => "jpeg", jpeg_tune => $tune), |
| 734 |
"fail jpeg_tune=$tune"); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
{ # check close failures are handled correctly |
| 739 |
my $im = test_image(); |
| 740 |
my $fail_close = sub { |
| 741 |
Imager::i_push_error(0, "synthetic close failure"); |
| 742 |
return 0; |
| 743 |
}; |
| 744 |
ok(!$im->write(type => "jpeg", callback => sub { 1 }, |
| 745 |
closecb => $fail_close), |
| 746 |
"check failing close fails"); |
| 747 |
like($im->errstr, qr/synthetic close failure/, |
| 748 |
"check error message"); |
| 749 |
} |
| 750 |
|
| 751 |
done_testing(); |