blib/lib/Image/Info/GIF.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 183 CC 28 SCAR 62.0
CC
28
Cov
0%
process_file
650.0
my_read
12.0
color_table
6.0
CRAP 494.1
1 package Image::Info::GIF;
2 $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 use strict;
26  
27 sub my_read
28 {
29 my($source, $len) = @_;
30 my $buf;
31 my $n = read($source, $buf, $len);
32 die "read failed: $!" unless defined $n;
33 die "short read ($len/$n) at pos " . tell($source) unless $n == $len;
34 $buf;
35 }
36  
37 sub read_data_blocks
38 {
39 my $source = shift;
40 my @data;
41 while (my $len = ord(my_read($source, 1))) {
42 push(@data, my_read($source, $len));
43 }
44 join("", @data);
45 }
46  
47 sub seek_data_blocks
48 {
49 my $source = shift;
50 while (my $len = ord(my_read($source, 1))) {
51 seek($source, $len, 1);
52 }
53 }
54  
55 sub process_file
56 {
57 my($info, $fh) = @_;
58  
59 my $header = my_read($fh, 13);
60 die "Bad GIF signature"
61 unless $header =~ s/^GIF(8[79]a)//;
62 my $version = $1;
63 $info->push_info(0, "GIF_Version" => $version);
64  
65 # process logical screen descriptor
66 my($sw, $sh, $packed, $bg, $aspect) = unpack("vvCCC", $header);
67 $info->push_info(0, "ScreenWidth" => $sw);
68 $info->push_info(0, "ScreenHeight" => $sh);
69  
70 my $color_table_size = 1 << (($packed & 0x07) + 1);
71 $info->push_info(0, "ColorTableSize" => $color_table_size);
72  
73 $info->push_info(0, "SortedColors" => ($packed & 0x08) ? 1 : 0)
74 if $version eq "89a";
75  
76 $info->push_info(0, "ColorResolution", (($packed & 0x70) >> 4) + 1);
77  
78 my $global_color_table = $packed & 0x80;
79 $info->push_info(0, "GlobalColorTableFlag" => $global_color_table ? 1 : 0);
80 if ($global_color_table) {
81 $info->push_info(0, "BackgroundColor", $bg);
82 }
83  
84 if ($aspect) {
85 $aspect = ($aspect + 15) / 64;
86 $info->push_info(0, "PixelAspectRatio" => $aspect);
87  
88 # XXX is this correct????
89 $info->push_info(0, "resolution", "1/$aspect");
90 }
91 else {
92 $info->push_info(0, "resolution", "1/1");
93 }
94  
95 $info->push_info(0, "file_media_type" => "image/gif");
96 $info->push_info(0, "file_ext" => "gif");
97  
98 # more??
99 if ($global_color_table) {
100 my $color_table = my_read($fh, $color_table_size * 3);
101 #$info->push_info(0, "GlobalColorTable", color_table($color_table));
102 }
103  
104 my $img_no = 0;
105 my @comments;
106 my @warnings;
107  
108 while (1) {
109 last if eof($fh); # EOF
110 my $intro = ord(my_read($fh, 1));
111 if ($intro == 0x3B) { # trailer (end of image)
112 last;
113 }
114 elsif ($intro == 0x2C) { # new image
115  
116  
117 if (@comments) {
118 for (@comments) {
119 $info->push_info(0, "Comment", $_);
120 }
121 @comments = ();
122 }
123  
124 $info->push_info($img_no, "color_type" => "Indexed-RGB");
125  
126 my($x_pos, $y_pos, $w, $h, $packed) =
127 unpack("vvvvC", my_read($fh, 9));
128 $info->push_info($img_no, "XPosition", $x_pos);
129 $info->push_info($img_no, "YPosition", $y_pos);
130 $info->push_info($img_no, "width", $w);
131 $info->push_info($img_no, "height", $h);
132  
133 if ($packed & 0x80) {
134 # yes, we have a local color table
135 my $ct_size = 1 << (($packed & 0x07) + 1);
136 $info->push_info($img_no, "LColorTableSize" => $ct_size);
137 my $color_table = my_read($fh, $ct_size * 3);
138 }
139  
140 $info->push_info($img_no, "Interlace" => "GIF")
141 if $packed & 0x40;
142  
143 my $lzw_code_size = ord(my_read($fh, 1));
144 #$info->push_info($img_no, "LZW_MininmCodeSize", $lzw_code_size);
145 seek_data_blocks($fh); # skip image data
146 $img_no++;
147 }
148 elsif ($intro == 0x21) { # GIF89a extension
149 push(@warnings, "GIF 89a extensions in 87a")
150 if $version eq "87a";
151  
152 my $label = ord(my_read($fh, 1));
153 my $data = read_data_blocks($fh);
154 if ($label == 0xF9 && length($data) == 4) { # Graphic Control
155 my($packed, $delay, $trans_color) = unpack("CvC", $data);
156 my $disposal_method = ($packed >> 2) & 0x07;
157 $info->push_info($img_no, "DisposalMethod", $disposal_method)
158 if $disposal_method;
159 $info->push_info($img_no, "UserInput", 1)
160 if $packed & 0x02;
161 $info->push_info($img_no, "Delay" => $delay/100) if $delay;
162 $info->push_info($img_no, "TransparencyIndex" => $trans_color)
163 if $packed & 0x01;
164 }
165 elsif ($label == 0xFE) { # Comment
166 $data =~ s/\0+$//; # is often NUL-terminated
167 push(@comments, $data);
168 }
169 elsif ($label == 0xFF) { # Application
170 my $app = substr($data, 0, 11, "");
171 my $auth = substr($app, -3, 3, "");
172 if ($app eq "NETSCAPE" && $auth eq "2.0"
173 && $data =~ /^\01/) {
174 my $loop = unpack("xv", $data);
175 $loop = "forever" unless $loop;
176 $info->push_info(0, "GIF_Loop" => $loop);
177 } else {
178 $info->push_info(0, "APP-$app-$auth" => $data);
179 }
180 }
181 else {
182 $info->push_info($img_no, "GIF_Extension-$label" => $data);
183 }
184 }
185 else {
186 push @warnings, "Unknown introduced code $intro, ignoring following chunks";
187 last;
188 }
189 }
190  
191 for (@comments) {
192 $info->push_info(0, "Comment", $_);
193 }
194  
195 for (@warnings) {
196 $info->push_info(0, "Warn", $_);
197 }
198 }
199  
200 sub color_table
201 {
202 my @n = unpack("C*", shift);
203 die "Color table not a multiple of 3" if @n % 3;
204 my @table;
205 while (@n) {
206 my @triple = splice(@n, -3);
207 push(@table, sprintf("#%02x%02x%02x", @triple));
208 }
209 [reverse @table];
210 }
211  
212 1;
213  
214 __END__