lib/Image/Info/GIF.pm

filter: Statementstmt 78.4% 80 / 102 Branchbran 58.3% 35 / 60 Conditioncond 22.2% 2 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 83.3% 5 / 6 Podpod 20.0% 1 / 5 totaltotal 66.3% 124 / 187 CC 37 SCAR 38.1
CC
37
Cov
68%
process_file
59.2
color_table
12.0
my_read
3.1
CRAP 45.0
« blib/lib/Image/Info/SVG/XMLSimple.pm ↑ Summary lib/Image/TIFF.pm »
1 package Image::Info::GIF;
2 3 $VERSION = '1.02';
3  
4 # Copyright 1999-2000, Gisle Aas.
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the same terms as Perl itself.
8  
9 =begin register
10  
11 MAGIC: /^GIF8[79]a/
12  
13 Both GIF87a and GIF89a are supported and the version number is found
14 as C<GIF_Version> for the first image. GIF files can contain multiple
15 images, and information for all images will be returned if
16 image_info() is called in list context. The Netscape-2.0 extension to
17 loop animation sequences is represented by the C<GIF_Loop> key for the
18 first image. The value is either "forever" or a number indicating
19 loop count.
20  
21 =end register
22  
23 =cut
24  
25 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub my_read
28 425 ▶{
Subroutine my_read: called CC=3 SCAR=11.2
Pod undocumented
29 425 my($source, $len) = @_;
30 425 my $buf;
31 425 my $n = read($source, $buf, $len);
32 425 ▶ die "read failed: $!" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 425 425
33 425 ▶ die "short read ($len/$n) at pos " . tell($source) unless $n == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $len 0 425 425
34 425 $buf;
35 }
36  
37 sub read_data_blocks
38 7 ▶{
Subroutine read_data_blocks: called CC=2 SCAR=6.9
Pod undocumented
39 7 my $source = shift;
40 7 my @data;
41 7 ▶ while (my $len = ord(my_read($source, 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = ord my_read($source, 1)) 7 7 14
42 7 push(@data, my_read($source, $len));
43 }
44 7 join("", @data);
45 }
46  
47 sub seek_data_blocks
48 7 ▶{
Subroutine seek_data_blocks: called CC=2 SCAR=6.9
Pod undocumented
49 7 my $source = shift;
50 7 ▶ while (my $len = ord(my_read($source, 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = ord my_read($source, 1)) 343 7 350
51 343 seek($source, $len, 1);
52 }
53 }
54  
55 sub process_file
56 7 ▶{
Subroutine process_file: called CC=31 SCAR=40.8
Pod documented
57 7 my($info, $fh) = @_;
58  
59 7 my $header = my_read($fh, 13);
60 7 ▶ die "Bad GIF signature"
Branch: 1 branch
BranchTrueFalseTotal
unless $header =~ s/^GIF(8[79]a)// 0 7 7
61 unless $header =~ s/^GIF(8[79]a)//;
62 7 my $version = $1;
63 7 $info->push_info(0, "GIF_Version" => $version);
64  
65 # process logical screen descriptor
66 7 my($sw, $sh, $packed, $bg, $aspect) = unpack("vvCCC", $header);
67 7 $info->push_info(0, "ScreenWidth" => $sw);
68 7 $info->push_info(0, "ScreenHeight" => $sh);
69  
70 7 my $color_table_size = 1 << (($packed & 0x07) + 1);
71 7 $info->push_info(0, "ColorTableSize" => $color_table_size);
72  
73 7 ▶ $info->push_info(0, "SortedColors" => ($packed & 0x08) ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
if $version eq "89a" 7 0 7
$packed & 8 ? : 0 7 7
74 if $version eq "89a";
75  
76 7 $info->push_info(0, "ColorResolution", (($packed & 0x70) >> 4) + 1);
77  
78 7 my $global_color_table = $packed & 0x80;
79 7 ▶ $info->push_info(0, "GlobalColorTableFlag" => $global_color_table ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$global_color_table ? : 7 0 7
80 7 ▶ if ($global_color_table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global_color_table) 7 0 7
81 7 $info->push_info(0, "BackgroundColor", $bg);
82 }
83  
84 7 ▶ if ($aspect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aspect) { } 0 7 7
85 0 $aspect = ($aspect + 15) / 64;
86 0 $info->push_info(0, "PixelAspectRatio" => $aspect);
87  
88 # XXX is this correct????
89 0 $info->push_info(0, "resolution", "1/$aspect");
90 }
91 else {
92 7 $info->push_info(0, "resolution", "1/1");
93 }
94  
95 7 $info->push_info(0, "file_media_type" => "image/gif");
96 7 $info->push_info(0, "file_ext" => "gif");
97  
98 # more??
99 7 ▶ if ($global_color_table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global_color_table) 7 0 7
100 7 my $color_table = my_read($fh, $color_table_size * 3);
101 #$info->push_info(0, "GlobalColorTable", color_table($color_table));
102 }
103  
104 7 my $img_no = 0;
105 7 my @comments;
106 my @warnings;
107  
108 7 while (1) {
109 21 ▶ last if eof($fh); # EOF
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 2 19 21
110 19 my $intro = ord(my_read($fh, 1));
111 19 ▶ if ($intro == 0x3B) { # trailer (end of image)
Branch: 3 branches
BranchTrueFalseTotal
if ($intro == 59) { } 3 16 19
elsif ($intro == 44) { } 7 9 16
elsif ($intro == 33) { } 7 2 9
112 3 last;
113 }
114 elsif ($intro == 0x2C) { # new image
115  
116  
117 7 ▶ if (@comments) {
Branch: 1 branch
BranchTrueFalseTotal
if (@comments) 3 4 7
118 3 for (@comments) {
119 3 $info->push_info(0, "Comment", $_);
120 }
121 3 @comments = ();
122 }
123  
124 7 $info->push_info($img_no, "color_type" => "Indexed-RGB");
125  
126 7 my($x_pos, $y_pos, $w, $h, $packed) =
127 unpack("vvvvC", my_read($fh, 9));
128 7 $info->push_info($img_no, "XPosition", $x_pos);
129 7 $info->push_info($img_no, "YPosition", $y_pos);
130 7 $info->push_info($img_no, "width", $w);
131 7 $info->push_info($img_no, "height", $h);
132  
133 7 ▶ if ($packed & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packed & 128) 0 7 7
134 # yes, we have a local color table
135 0 my $ct_size = 1 << (($packed & 0x07) + 1);
136 0 $info->push_info($img_no, "LColorTableSize" => $ct_size);
137 0 my $color_table = my_read($fh, $ct_size * 3);
138 }
139  
140 7 ▶ $info->push_info($img_no, "Interlace" => "GIF")
Branch: 1 branch
BranchTrueFalseTotal
if $packed & 64 3 4 7
141 if $packed & 0x40;
142  
143 7 my $lzw_code_size = ord(my_read($fh, 1));
144 #$info->push_info($img_no, "LZW_MininmCodeSize", $lzw_code_size);
145 7 seek_data_blocks($fh); # skip image data
146 7 $img_no++;
147 }
148 elsif ($intro == 0x21) { # GIF89a extension
149 7 ▶ push(@warnings, "GIF 89a extensions in 87a")
Branch: 1 branch
BranchTrueFalseTotal
if $version eq "87a" 0 7 7
150 if $version eq "87a";
151  
152 7 my $label = ord(my_read($fh, 1));
153 7 my $data = read_data_blocks($fh);
154 7 ▶ if ($label == 0xF9 && length($data) == 4) { # Graphic Control
Branch: 3 branches
BranchTrueFalseTotal
if ($label == 249 and length $data == 4) { } 4 3 7
elsif ($label == 254) { } 3 0 3
elsif ($label == 255) { } 0 0 0
Condition66%2/3
$label == 249 and length $data == 4
!ll&&!rl&&r
304
MC/DC50%1/2
$label == 249 and length $data == 4
$label == 249 length $data == 4
Truth table66%2/3
A and B
A= $label == 249B= length $data == 4
ABresult
0X0
100
111
155 4 my($packed, $delay, $trans_color) = unpack("CvC", $data);
156 4 my $disposal_method = ($packed >> 2) & 0x07;
157 4 ▶ $info->push_info($img_no, "DisposalMethod", $disposal_method)
Branch: 1 branch
BranchTrueFalseTotal
if $disposal_method 0 4 4
158 if $disposal_method;
159 4 ▶ $info->push_info($img_no, "UserInput", 1)
Branch: 1 branch
BranchTrueFalseTotal
if $packed & 2 0 4 4
160 if $packed & 0x02;
161 4 ▶ $info->push_info($img_no, "Delay" => $delay/100) if $delay;
Branch: 1 branch
BranchTrueFalseTotal
if $delay 0 4 4
162 4 ▶ $info->push_info($img_no, "TransparencyIndex" => $trans_color)
Branch: 1 branch
BranchTrueFalseTotal
if $packed & 1 2 2 4
163 if $packed & 0x01;
164 }
165 elsif ($label == 0xFE) { # Comment
166 3 $data =~ s/\0+$//; # is often NUL-terminated
167 3 push(@comments, $data);
168 }
169 elsif ($label == 0xFF) { # Application
170 0 my $app = substr($data, 0, 11, "");
171 0 my $auth = substr($app, -3, 3, "");
172 0 ▶ if ($app eq "NETSCAPE" && $auth eq "2.0"
Branch: 1 branch
BranchTrueFalseTotal
if ($app eq 'NETSCAPE' and $auth eq '2.0' and $data =~ /^\01/) { } 0 0 0
Condition0%0/6
$app eq "NETSCAPE" and $auth eq "2.0" and $data =~ /^\01/
!ll&&!rl&&r
000
$app eq "NETSCAPE" and $auth eq "2.0"
!ll&&!rl&&r
000
MC/DC0%0/3
$app eq "NETSCAPE" and $auth eq "2.0" and $data =~ /^\01/
$app eq "NETSCAPE" $auth eq "2.0" $data =~ /^\01/
Truth table0%0/4
A and B and C
A= $app eq "NETSCAPE"B= $auth eq "2.0"C= $data =~ /^\01/
ABCresult
0XX0
10X0
1100
1111
173 && $data =~ /^\01/) {
174 0 my $loop = unpack("xv", $data);
175 0 ▶ $loop = "forever" unless $loop;
Branch: 1 branch
BranchTrueFalseTotal
unless $loop 0 0 0
176 0 $info->push_info(0, "GIF_Loop" => $loop);
177 } else {
178 0 $info->push_info(0, "APP-$app-$auth" => $data);
179 }
180 }
181 else {
182 0 $info->push_info($img_no, "GIF_Extension-$label" => $data);
183 }
184 }
185 else {
186 2 push @warnings, "Unknown introduced code $intro, ignoring following chunks";
187 2 last;
188 }
189 }
190  
191 7 for (@comments) {
192 0 $info->push_info(0, "Comment", $_);
193 }
194  
195 7 for (@warnings) {
196 2 $info->push_info(0, "Warn", $_);
197 }
198 }
199  
200 sub color_table
201 0 ▶{
Subroutine color_table: not called CC=3 SCAR=24.8
Pod undocumented
202 0 my @n = unpack("C*", shift);
203 0 ▶ die "Color table not a multiple of 3" if @n % 3;
Branch: 1 branch
BranchTrueFalseTotal
if @n % 3 0 0 0
204 0 my @table;
205 0 ▶ while (@n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@n) 0 0 0
206 0 my @triple = splice(@n, -3);
207 0 push(@table, sprintf("#%02x%02x%02x", @triple));
208 }
209 0 [reverse @table];
210 }
211  
212 3 1;
213  
214 __END__
« blib/lib/Image/Info/SVG/XMLSimple.pm ↑ Summary lib/Image/TIFF.pm »