File Coverage

blib/lib/Image/ExifTool/FLIF.pm
Criterion Covered Total %
statement 104 112 92.8
branch 53 86 61.6
condition 11 22 50.0
subroutine 8 8 100.0
pod 0 5 0.0
total 176 233 75.5


line stmt bran cond sub pod time code
1             #------------------------------------------------------------------------------
2             # File: FLIF.pm
3             #
4             # Description: Read/write FLIF meta information
5             #
6             # Revisions: 2016/10/11 - P. Harvey Created
7             # 2016/10/14 - PH Added write support
8             #
9             # References: 1) http://flif.info/
10             # 2) https://github.com/FLIF-hub/FLIF/blob/master/doc/metadata
11             #------------------------------------------------------------------------------
12              
13             package Image::ExifTool::FLIF;
14              
15 1     1   4540 use strict;
  1         2  
  1         38  
16 1     1   4 use vars qw($VERSION);
  1         11  
  1         41  
17 1     1   6 use Image::ExifTool qw(:DataAccess :Utils);
  1         3  
  1         1821  
18              
19             $VERSION = '1.02';
20              
21             my %flifMap = (
22             EXIF => 'FLIF',
23             XMP => 'FLIF',
24             ICC_Profile => 'FLIF',
25             IFD0 => 'EXIF',
26             IFD1 => 'IFD0',
27             ExifIFD => 'IFD0',
28             GPS => 'IFD0',
29             SubIFD => 'IFD0',
30             GlobParamIFD => 'IFD0',
31             PrintIM => 'IFD0',
32             InteropIFD => 'ExifIFD',
33             MakerNotes => 'ExifIFD',
34             );
35              
36             # FLIF tags
37             %Image::ExifTool::FLIF::Main = (
38             GROUPS => { 0 => 'File', 1 => 'File', 2 => 'Image' },
39             VARS => { HEX_ID => 0 },
40             NOTES => q{
41             Information extracted from Free Lossless Image Format files. See
42             L for more information.
43             },
44             #
45             # header information
46             #
47             0 => {
48             Name => 'ImageType',
49             PrintConv => {
50             '1' => 'Grayscale (non-interlaced)',
51             '3' => 'RGB (non-interlaced)',
52             '4' => 'RGBA (non-interlaced)',
53             'A' => 'Grayscale (interlaced)',
54             'C' => 'RGB (interlaced)',
55             'D' => 'RGBA (interlaced)',
56             'Q' => 'Grayscale Animation (non-interlaced)',
57             'S' => 'RGB Animation (non-interlaced)',
58             'T' => 'RGBA Animation (non-interlaced)',
59             'a' => 'Grayscale Animation (interlaced)',
60             'c' => 'RGB Animation (interlaced)',
61             'd' => 'RGBA Animation (interlaced)',
62             },
63             },
64             1 => {
65             Name => 'BitDepth',
66             PrintConv => {
67             '0' => 'Custom',
68             '1' => 8,
69             '2' => 16,
70             },
71             },
72             2 => 'ImageWidth',
73             3 => 'ImageHeight',
74             4 => 'AnimationFrames',
75             5 => {
76             Name => 'Encoding',
77             PrintConv => {
78             0 => 'FLIF16',
79             },
80             },
81             #
82             # metadata chunks
83             #
84             iCCP => {
85             Name => 'ICC_Profile',
86             SubDirectory => {
87             TagTable => 'Image::ExifTool::ICC_Profile::Main',
88             },
89             },
90             eXif => {
91             Name => 'EXIF',
92             SubDirectory => {
93             TagTable => 'Image::ExifTool::Exif::Main',
94             ProcessProc => \&Image::ExifTool::ProcessTIFF,
95             WriteProc => \&Image::ExifTool::WriteTIFF,
96             Start => 6, # (skip "Exif\0\0" header)
97             Header => "Exif\0\0",
98             },
99             },
100             eXmp => {
101             Name => 'XMP',
102             SubDirectory => {
103             TagTable => 'Image::ExifTool::XMP::Main',
104             },
105             },
106             # tRko - list of truncation offsets
107             # \0 - FLIF16-format image data
108             );
109              
110             #------------------------------------------------------------------------------
111             # Read variable-length FLIF integer
112             # Inputs: 0) raf reference, 1) number to add to returned value
113             # Returns: integer, or undef on EOF
114             sub GetVarInt($;$)
115             {
116 39     39 0 91 my ($raf, $add) = @_;
117 39         72 my ($val, $buff);
118 39         70 for ($val=0; ; $val<<=7) {
119 53 50       123 $raf->Read($buff, 1) or return undef;
120 53         94 my $byte = ord($buff);
121 53         80 $val |= ($byte & 0x7f);
122 53 100       135 last unless $byte & 0x80;
123             }
124 39   100     144 return $val + ($add || 0);
125             }
126              
127             #------------------------------------------------------------------------------
128             # Construct variable-length FLIF integer
129             # Inputs: 0) integer
130             # Returns: FLIF variable-length integer byte stream
131             sub SetVarInt($)
132             {
133 9     9 0 18 my $val = shift;
134 9         37 my $buff = '';
135 9         15 my $high = 0;
136 9         19 for (;;) {
137 15         46 $buff = chr(($val & 0x7f) | $high) . $buff;
138 15 100       39 last unless $val >>= 7;
139 6         11 $high = 0x80;
140             }
141 9         36 return $buff;
142             }
143              
144             #------------------------------------------------------------------------------
145             # Read FLIF header
146             # Inputs: 0) RAF ref
147             # Returns: Scalar context: binary header block
148             # List context: header values (4 or 5 elements: type,depth,width,height[,frames])
149             # or undef if this isn't a valid FLIF file header
150             sub ReadFLIFHeader($)
151             {
152 9     9 0 20 my $raf = shift;
153 9         16 my ($buff, @vals);
154              
155             # verify this is a valid FLIF file
156 9 50 33     27 return () unless $raf->Read($buff, 6) == 6 and $buff =~ /^FLIF([0-\x6f])([0-2])/;
157              
158             # decode header information ("FLIF" chunk)
159 9         64 push @vals, $1, $2; # type, depth
160 9         32 push @vals, GetVarInt($raf,+1), GetVarInt($raf,+1); # width, height (+1 each)
161 9 50       29 push @vals, GetVarInt($raf,+2) if $vals[0] gt 'H'; # frames (+2)
162              
163 9 50       34 return () unless defined $vals[-1];
164 9 100       47 return @vals if wantarray; # return the decoded header values
165              
166             # return the binary header block
167 4         13 my $hdrLen = $raf->Tell();
168 4 50 33     18 return () unless $raf->Seek(0,0) and $raf->Read($buff, $hdrLen) == $hdrLen;
169 4         21 return $buff;
170             }
171              
172             #------------------------------------------------------------------------------
173             # WriteFLIF : Write FLIF image
174             # Inputs: 0) ExifTool object reference, 1) dirInfo reference
175             # Returns: 1 on success, 0 if this wasn't a valid FLIF file, or -1 if
176             # an output file was specified and a write error occurred
177             sub WriteFLIF($$)
178             {
179 4     4 0 17 my ($et, $dirInfo) = @_;
180 4         14 my $raf = $$dirInfo{RAF};
181 4         9 my ($buff, $soi, @addTags, %doneTag);
182              
183             # verify FLIF header and copy it to the output file
184 4 50       15 $buff = ReadFLIFHeader($raf) or return 0;
185 4         12 my $outfile = $$dirInfo{OutFile};
186 4 50       17 Write($outfile, $buff) or return -1;
187              
188 4         29 $et->InitWriteDirs(\%flifMap);
189 4         24 my $tagTablePtr = GetTagTable('Image::ExifTool::FLIF::Main');
190              
191             # loop through the FLIF chunks
192 4         10 for (;;) {
193 16         36 my ($tag, $size, $inflated);
194             # read new tag (or soi) unless we already hit the soi (start of image)
195 16 100       41 if (not defined $soi) {
196 13 50       47 $raf->Read($buff, 4) == 4 or $et->Error('Unexpected EOF'), last;
197 13 100       59 if ($buff lt ' ') {
198 4         13 $soi = $buff; # we have hit the start of image (no more metadata)
199             # make list of new tags to add
200 4         12 foreach $tag ('eXif', 'eXmp', 'iCCP') {
201 12 100 100     68 push @addTags, $tag if $$et{ADD_DIRS}{$$tagTablePtr{$tag}{Name}} and not $doneTag{$tag};
202             }
203             }
204             }
205 16 100       59 if (not defined $soi) {
    100          
206 9         18 $tag = $buff;
207 9         21 $size = GetVarInt($raf); # read the data size
208             } elsif (@addTags) {
209 3         7 $tag = shift @addTags;
210 3         43 ($buff, $size) = ('', 0); # create metadata from scratch
211             } else {
212             # finish copying file (no more metadata to add)
213 4 50       17 Write($outfile, $soi) or return -1;
214 4   50     18 Write($outfile, $buff) or return -1 while $raf->Read($buff, 65536);
215 4         10 last; # all done!
216             }
217 12         46 my $tagInfo = $et->GetTagInfo($tagTablePtr, $tag);
218 12 50 33     113 if ($tagInfo and $$tagInfo{SubDirectory} and $$et{EDIT_DIRS}{$$tagInfo{Name}}) {
    0 33        
219 12         42 $doneTag{$tag} = 1; # prevent adding this back again later
220 12 100       28 unless (defined $soi) {
221 9 50       39 $raf->Read($buff, $size) == $size or $et->Error("Truncated FLIF $tag chunk"), last;
222             }
223             # rewrite the compressed data
224 12 50 33     19 if (eval { require IO::Uncompress::RawInflate } and eval { require IO::Compress::RawDeflate } ) {
  12         105  
  12         63  
225 12 100       51 if (length $buff == 0) {
    50          
226 3         7 $inflated = $buff; # (creating from scratch, so no need to inflate)
227             } elsif (not IO::Uncompress::RawInflate::rawinflate(\$buff => \$inflated)) {
228 0         0 $et->Error("Error inflating FLIF $tag chunk"), last;
229             }
230 12         13907 my $subdir = $$tagInfo{SubDirectory};
231             my %subdirInfo = (
232             DirName => $$tagInfo{Name},
233             DataPt => \$inflated,
234             DirStart => length($inflated) ? $$subdir{Start} : undef,
235 12 100       84 ReadOnly => 1, # (used only by WriteXMP)
236             );
237 12         58 my $subTable = GetTagTable($$subdir{TagTable});
238 12         70 $inflated = $et->WriteDirectory(\%subdirInfo, $subTable, $$subdir{WriteProc});
239 12 50       42 if (defined $inflated) {
240 12 100       36 next unless length $inflated; # (delete directory if length is zero)
241 9 100       31 $inflated = $$subdir{Header} . $inflated if $$subdir{Header}; # (add back header if necessary)
242 9 50       38 unless (IO::Compress::RawDeflate::rawdeflate(\$inflated => \$buff)) {
243 0         0 $et->Error("Error deflating FLIF $tag chunk"), last;
244             }
245             }
246             } else {
247 0         0 $et->WarnOnce('Install IO::Compress::RawDeflate to write FLIF metadata');
248             }
249 9 50       15129 Write($outfile, $tag, SetVarInt(length $buff), $buff) or return -1;
250             } elsif (not defined $soi) {
251 0 0       0 Write($outfile, $tag, SetVarInt($size)) or return -1;
252 0 0       0 Image::ExifTool::CopyBlock($raf, $outfile, $size) or return -1;
253             }
254             }
255 4         19 return 1;
256             }
257              
258             #------------------------------------------------------------------------------
259             # Extract information from an FLIF file
260             # Inputs: 0) ExifTool object reference, 1) DirInfo reference
261             # Returns: 1 on success, 0 if this wasn't a valid FLIF file
262             sub ProcessFLIF($$)
263             {
264 5     5 0 17 my ($et, $dirInfo) = @_;
265 5         16 my $raf = $$dirInfo{RAF};
266 5         10 my ($buff, $tag, $inflated);
267              
268             # verify this is a valid FLIF file and read the header
269 5 50       15 my @vals = ReadFLIFHeader($raf) or return 0;
270              
271 5         28 $et->SetFileType();
272 5         28 my $tagTablePtr = GetTagTable('Image::ExifTool::FLIF::Main');
273 5         28 my $verbose = $et->Options('Verbose');
274              
275             # save the header information
276 5 50       21 $et->VPrint(0, "FLIF header:\n") if $verbose;
277 5         21 for ($tag=0; defined $vals[$tag]; ++$tag) {
278 20         58 $et->HandleTag($tagTablePtr, $tag, $vals[$tag]);
279             }
280              
281             # loop through the FLIF chunks
282 5         14 for (;;) {
283 17 50       66 $raf->Read($tag, 4) == 4 or $et->Warn('Unexpected EOF'), last;
284 17         44 my $byte = ord substr($tag, 0, 1);
285             # all done if we arrived at the image chunk
286 17 100       55 $byte < 32 and $et->HandleTag($tagTablePtr, 5, $byte), last;
287 12         29 my $size = GetVarInt($raf);
288 12 50       37 $et->VPrint(0, "FLIF $tag ($size bytes):\n") if $verbose;
289 12 50       34 if ($$tagTablePtr{$tag}) {
290 12 50       32 $raf->Read($buff, $size) == $size or $et->Warn("Truncated FLIF $tag chunk"), last;
291 12 50       47 $et->VerboseDump(\$buff, Addr => $raf->Tell() - $size) if $verbose > 2;
292             # inflate the compressed data
293 12 50       28 if (eval { require IO::Uncompress::RawInflate }) {
  12         93  
294 12 50       56 if (IO::Uncompress::RawInflate::rawinflate(\$buff => \$inflated)) {
295 12         18609 $et->HandleTag($tagTablePtr, $tag, $inflated,
296             DataPt => \$inflated,
297             Size => length $inflated,
298             Extra => ' inflated',
299             );
300             } else {
301 0         0 $et->Warn("Error inflating FLIF $tag chunk");
302             }
303             } else {
304 0         0 $et->WarnOnce('Install IO::Uncompress::RawInflate to decode FLIF metadata');
305             }
306             } else {
307 0 0       0 $raf->Seek($size, 1) or $et->Warn('Seek error'), last;
308             }
309             }
310 5         29 return 1;
311             }
312              
313             1; # end
314              
315             __END__