Debian Codegraph @

t/jpeg.t

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use strict;
2
3 use File::Path ();
4 use File::Spec::Functions;
5 use FindBin ();
6 use Test::More;
7 require Test::NoWarnings;
8
9 use Image::Scale;
10
11 my $jpeg_version = Image::Scale->jpeg_version();
12
13 if ($jpeg_version) {
•14 plan tests => 112;
15 }
16 else {
17 plan skip_all => 'Image::Scale not built with libjpeg support';
18 }
19
20 my $tmpdir = catdir( $FindBin::Bin, 'tmp' );
21 if ( -d $tmpdir ) {
22 File::Path::rmtree($tmpdir);
23 }
24 mkdir $tmpdir;
25
26 my @types = qw(
27 rgb
28 rgb_progressive
29 gray
30 gray_progressive
31 cmyk
32 );
33
34 my @resizes = qw(
35 resize_gd
36 resize_gd_fixed_point
37 resize_gm
38 resize_gm_fixed_point
39 );
40
41 # width/height
42 for my $type ( @types ) {
43 my $im = Image::Scale->new( _f("${type}.jpg") );
44
45 is( $im->width, 313, "JPEG $type width ok" );
46 is( $im->height, 234, "JPEG $type height ok" );
47 }
48
49 # Test resized height/width
50 {
51 my $im = Image::Scale->new( _f("rgb.jpg") );
52 is( $im->resized_width, 0, "JPEG resized_width is 0 before resize" );
53 is( $im->resized_height, 0, "JPEG resized_height is 0 before resize" );
54
55 $im->resize_gd( { width => 50, height => 50 } );
56
57 is( $im->resized_width, 50, "JPEG resized_width ok" );
58 is( $im->resized_height, 50, "JPEG resized_height ok" );
59 }
60
61 SKIP:
62 {
63 skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 44
64 if $jpeg_version != 62;
65
66 # These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
67 skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 44 unless $ENV{TEST_COMPARE};
68
69 # Normal width resize
70 for my $resize ( @resizes ) {
71 for my $type ( @types ) {
72 my $outfile = _tmp("${type}_${resize}_w100.jpg");
73
74 my $im = Image::Scale->new( _f("${type}.jpg") );
75 $im->$resize( { width => 100 } );
76 $im->save_jpeg($outfile);
77 my $data = $im->as_jpeg();
78
79 # Only perform file comparisons for fixed-point methods, as floating-point
80 # is expected to differ on different platforms
81 next if $resize !~ /fixed/;
82
83 is( _compare( _load($outfile), "${type}_${resize}_w100.jpg" ), 1, "JPEG $type $resize 100 file ok" );
84 is( _compare( \$data, "${type}_${resize}_w100.jpg" ), 1, "JPEG $type $resize 100 scalar ok" );
85 }
86 }
87
88 # square, without keep_aspect
89 for my $resize ( @resizes ) {
90 for my $type ( @types ) {
91 my $outfile = _tmp("${type}_${resize}_50x50.jpg");
92
93 my $im = Image::Scale->new( _f("${type}.jpg") );
94 $im->$resize( { width => 50, height => 50 } );
95 $im->save_jpeg($outfile);
96 my $data = $im->as_jpeg();
97
98 # Only perform file comparisons for fixed-point methods, as floating-point
99 # is expected to differ on different platforms
100 next if $resize !~ /fixed/;
101
102 is( _compare( _load($outfile), "${type}_${resize}_50x50.jpg" ), 1, "JPEG $type $resize 50x50 square file ok" );
103 is( _compare( \$data, "${type}_${resize}_50x50.jpg" ), 1, "JPEG $type $resize 50x50 square scalar ok" );
104 }
105 }
106
107 # keep_aspect with height padding
108 for my $resize ( @resizes ) {
109 my $outfile = _tmp("${resize}_50x50_keep_aspect_height.jpg");
110
111 my $im = Image::Scale->new( _f("rgb.jpg") );
112 $im->$resize( { width => 50, height => 50, keep_aspect => 1 } );
113 $im->save_jpeg($outfile);
114
115 # Only perform file comparisons for fixed-point methods, as floating-point
116 # is expected to differ on different platforms
117 next if $resize !~ /fixed/;
118
119 is( _compare( _load($outfile), "${resize}_50x50_keep_aspect_height.jpg" ), 1, "JPEG $resize 50x50 keep_aspect file ok" );
120 }
121
122 # keep_aspect with width padding
123 for my $resize ( @resizes ) {
124 my $outfile = _tmp("${resize}_50x50_keep_aspect_width.jpg");
125
126 my $im = Image::Scale->new( _f("exif_90_ccw.jpg") );
127 $im->$resize( { width => 50, height => 50, ignore_exif => 1, keep_aspect => 1 } );
128 $im->save_jpeg($outfile);
129
130 # Only perform file comparisons for fixed-point methods, as floating-point
131 # is expected to differ on different platforms
132 next if $resize !~ /fixed/;
133
134 is( _compare( _load($outfile), "${resize}_50x50_keep_aspect_width.jpg" ), 1, "JPEG $resize 50x50 keep_aspect file ok" );
135 }
136 }
137
138 # memory_limit
139 {
140 my $im = Image::Scale->new( _f("rgb.jpg") );
141 eval { $im->resize( { width => 50, memory_limit => 1000 } ) };
142 like( $@, qr/memory_limit exceeded/, 'JPEG memory_limit ok' );
143 }
144
145 # corrupt truncated file but will still resize with a gray area
146 SKIP:
147 {
148 Test::NoWarnings::clear_warnings();
149
150 my $outfile = _tmp("truncated_50.jpg");
151 my $im = Image::Scale->new( _f("truncated.jpg") );
152 $im->resize_gd_fixed_point( { width => 50 } );
153 $im->save_jpeg($outfile);
154
155 # Test that the correct warning was output
156 like( (Test::NoWarnings::warnings())[0]->getMessage, qr/premature end of/i, 'JPEG corrupt truncated warning output ok' );
157
158 skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 1
159 if $jpeg_version != 62;
160
161 # These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
162 skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 1 unless $ENV{TEST_COMPARE};
163
164 is( _compare( _load($outfile), "truncated_50.jpg" ), 1, 'JPEG corrupt truncated resize_gd ok' );
165 }
166
167 # corrupt file resulting in a fatal error
168 {
169 Test::NoWarnings::clear_warnings();
170
171 my $im = Image::Scale->new( _f("corrupt.jpg") );
172
173 is( $im, undef, 'JPEG corrupt failed new() ok' );
174
175 # Test that the correct warning was output
176 like( (Test::NoWarnings::warnings())[0]->getMessage, qr/Image::Scale libjpeg error: Corrupt JPEG data/i, 'JPEG corrupt warning output ok' );
177 }
178
179 # keep_aspect bgcolor
180 SKIP:
181 {
182 skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 2
183 if $jpeg_version != 62;
184
185 # These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
186 skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 2 unless $ENV{TEST_COMPARE};
187
188 for my $resize ( @resizes ) {
189 my $outfile = _tmp("bgcolor_${resize}.jpg");
190
191 my $im = Image::Scale->new( _f("rgb.jpg") );
192 $im->$resize( { width => 50, height => 50, keep_aspect => 1, bgcolor => 0x123456 } );
193 $im->save_jpeg($outfile);
194
195 # Only perform file comparisons for fixed-point methods, as floating-point
196 # is expected to differ on different platforms
197 next if $resize !~ /fixed/;
198
199 is( _compare( _load($outfile), "bgcolor_${resize}.jpg" ), 1, "JPEG bgcolor $resize ok" );
200 }
201 }
202
203 # Exif rotation
204 # Exif with both width/height specified
205 # Exif with keep_aspect
206 SKIP:
207 {
208 skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 42
209 if $jpeg_version != 62;
210
211 # These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
212 skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 42 unless $ENV{TEST_COMPARE};
213
214 my @rotations = qw(
215 mirror_horiz
216 180
217 mirror_vert
218 mirror_horiz_270_ccw
219 90_ccw
220 mirror_horiz_90_ccw
221 270_ccw
222 );
223
224 for my $resize ( @resizes ) {
225 for my $r ( @rotations ) {
226 my $outfile = _tmp("exif_${r}_${resize}_50.jpg");
227 my $im = Image::Scale->new( _f("exif_${r}.jpg") );
228 $im->$resize( { width => 50 } );
229 $im->save_jpeg($outfile);
230
231 # Only perform file comparisons for fixed-point methods, as floating-point
232 # is expected to differ on different platforms
233 next if $resize !~ /fixed/;
234
235 is( _compare( _load($outfile), "exif_${r}_${resize}_50.jpg" ), 1, "JPEG EXIF auto-rotation $r $resize width 50 ok" );
236 }
237
238 for my $r ( @rotations ) {
239 my $outfile = _tmp("exif_${r}_${resize}_50x50.jpg");
240 my $im = Image::Scale->new( _f("exif_${r}.jpg") );
241 $im->$resize( { width => 50, height => 50 } );
242 $im->save_jpeg($outfile);
243
244 # Only perform file comparisons for fixed-point methods, as floating-point
245 # is expected to differ on different platforms
246 next if $resize !~ /fixed/;
247
248 is( _compare( _load($outfile), "exif_${r}_${resize}_50x50.jpg" ), 1, "JPEG EXIF auto-rotation $r $resize 50x50 ok" );
249 }
250
251 for my $r ( @rotations ) {
252 my $outfile = _tmp("exif_${r}_${resize}_50x50_keep_aspect.jpg");
253 my $im = Image::Scale->new( _f("exif_${r}.jpg") );
254 $im->$resize( { width => 50, height => 50, keep_aspect => 1 } );
255 $im->save_jpeg($outfile);
256
257 # Only perform file comparisons for fixed-point methods, as floating-point
258 # is expected to differ on different platforms
259 next if $resize !~ /fixed/;
260
261 is( _compare( _load($outfile), "exif_${r}_${resize}_50x50_keep_aspect.jpg" ), 1, "JPEG EXIF auto-rotation $r $resize 50x50 keep_aspect ok" );
262 }
263 }
264 }
265
266 # Exif with ignore_exif
267 SKIP:
268 {
269 skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 1
270 if $jpeg_version != 62;
271
272 # These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
273 skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 1 unless $ENV{TEST_COMPARE};
274
275 my $outfile = _tmp("exif_ignore_50.jpg");
276 my $im = Image::Scale->new( _f("exif_90_ccw.jpg") );
277 $im->resize_gd_fixed_point( { width => 50, ignore_exif => 1 } );
278 $im->save_jpeg($outfile);
279
280 is( _compare( _load($outfile), "exif_ignore_50.jpg" ), 1, "JPEG EXIF ignore_exif ok" );
281 }
282
283 # multiple resize calls on same $im object, should throw away previous resize data
284 SKIP:
285 {
286 skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 2
287 if $jpeg_version != 62;
288
289 # These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
290 skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 2 unless $ENV{TEST_COMPARE};
291
292 for my $resize ( @resizes ) {
293 my $outfile = _tmp("rgb_multiple_${resize}.jpg");
294 my $im = Image::Scale->new( _f("rgb.jpg") );
295 $im->$resize( { width => 50 } );
296 $im->$resize( { width => 75 } );
297 $im->save_jpeg($outfile);
298
299 # Only perform file comparisons for fixed-point methods, as floating-point
300 # is expected to differ on different platforms
301 next if $resize !~ /fixed/;
302
303 is( _compare( _load($outfile), "rgb_multiple_${resize}.jpg" ), 1, "JPEG multiple resize $resize ok" );
304 }
305 }
306
307 # resize from JPEG in scalar
308 SKIP:
309 {
310 skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 1
311 if $jpeg_version != 62;
312
313 # These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
314 skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 1 unless $ENV{TEST_COMPARE};
315
316 my $dataref = _load( _f("rgb.jpg") );
317
318 my $outfile = _tmp("rgb_resize_gd_fixed_point_w100.jpg");
319 my $im = Image::Scale->new($dataref);
320 $im->resize_gd_fixed_point( { width => 100 } );
321 $im->save_jpeg($outfile);
322
323 is( _compare( _load($outfile), "rgb_resize_gd_fixed_point_w100.jpg" ), 1, "JPEG resize_gd_fixed_point from scalar ok" );
324 }
325
326 # resize multiple from JPEG scalar
327 SKIP:
328 {
329 skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 1
330 if $jpeg_version != 62;
331
332 # These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
333 skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 1 unless $ENV{TEST_COMPARE};
334
335 my $dataref = _load( _f("rgb.jpg") );
336
337 my $outfile = _tmp("rgb_resize_gd_fixed_point_w100.jpg");
338 my $im = Image::Scale->new($dataref);
339 $im->resize_gd_fixed_point( { width => 150 } );
340 $im->resize_gd_fixed_point( { width => 100 } );
341 $im->save_jpeg($outfile);
342
343 is( _compare( _load($outfile), "rgb_resize_gd_fixed_point_w100.jpg" ), 1, "JPEG resize_gd_fixed_point multiple from scalar ok" );
344 }
345
346 # XXX fatal errors during compression, will this ever actually happen?
347
348 # XXX progressive JPEG with/without memory_limit
349
350 diag("libjpeg version: $jpeg_version");
351
352 END {
353 File::Path::rmtree($tmpdir);
354 }
355
356 sub _f {
357 return catfile( $FindBin::Bin, 'images', 'jpg', shift );
358 }
359
360 sub _tmp {
361 return catfile( $tmpdir, shift );
362 }
363
364 sub _load {
365 my $path = shift;
366
367 open my $fh, '<', $path or die "Cannot open $path";
368 binmode $fh;
369 my $data = do { local $/; <$fh> };
370 close $fh;
371
372 return \$data;
373 }
374
375 sub _compare {
376 my ( $test, $path ) = @_;
377
378 my $ref = _load( catfile( $FindBin::Bin, 'ref', 'jpg', $path ) );
379
380 return $$ref eq $$test;
381 }