]> git.imager.perl.org - imager.git/blob - t/t101jpeg.t
- Imager::Font::BBox advance_width() method was falling back to
[imager.git] / t / t101jpeg.t
1 #!perl -w
2 use strict;
3 use lib 't';
4 use Imager qw(:all);
5 use Test::More tests => 9;
6
7 init_log("testout/t101jpeg.log",1);
8
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);
12
13 my $img=Imager::ImgRaw::new(150,150,3);
14 my $cmpimg=Imager::ImgRaw::new(150,150,3);
15
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]);
20
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
25  SKIP:
26   {
27     my $im = Imager->new;
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);
34   }
35 } else {
36   open(FH,">testout/t101.jpg") || die "cannot open testout/t101.jpg for writing\n";
37   binmode(FH);
38   my $IO = Imager::io_new_fd(fileno(FH));
39   ok(i_writejpeg_wiol($img,$IO,30), "write jpeg low level");
40   close(FH);
41
42   open(FH, "testout/t101.jpg") || die "cannot open testout/t101.jpg\n";
43   binmode(FH);
44   $IO = Imager::io_new_fd(fileno(FH));
45   ($cmpimg,undef) = i_readjpeg_wiol($IO);
46   close(FH);
47
48   print "$cmpimg\n";
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");
52
53   ok($diff < 10000, "difference between original and jpeg within bounds");
54
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");
62
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");
66
67   # write failure test
68   open FH, "< testout/t101.jpg" or die "Cannot open testout/t101.jpg: $!";
69   binmode FH;
70   ok(!$imoo->write(fd=>fileno(FH), type=>'jpeg'), 'failure handling');
71   close FH;
72   print "# ",$imoo->errstr,"\n";
73
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');
77 }
78