Image/ExifTool/FLIF.pm

filter: Statementstmt 93.1% 108 / 116 Branchbran 63.0% 58 / 92 Conditioncond 50.0% 10 / 20 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 5 totaltotal 73.9% 187 / 253 CC 55 SCAR 34.2
CC
55
Cov
77%
WriteFLIF
49.3
ProcessFLIF
16.5
ReadFLIFHeader
9.7
CRAP 30.5
« Image/ExifTool/CanonCustom.pm ↑ Summary Image/ExifTool/FITS.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 $VERSION = '1.02';
20  
21 1 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 1 %Image::ExifTool::FLIF::Main = (
38 GROUPS => { 0 => 'File', 1 => 'File', 2 => 'Image' },
39 VARS => { ID_FMT => 'dec' },
40 NOTES => q{
41 Information extracted from Free Lossless Image Format files. See
42 L<http://flif.info/> 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 39 ▶{
Subroutine GetVarInt: called CC=4 SCAR=13.9
Pod undocumented
116 39 my ($raf, $add) = @_;
117 39 my ($val, $buff);
118 39 for ($val=0; ; $val<<=7) {
119 53 ▶ $raf->Read($buff, 1) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 1) or return undef 53 0 53
120 53 my $byte = ord($buff);
121 53 $val |= ($byte & 0x7f);
122 53 ▶ last unless $byte & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
unless $byte & 128 39 14 53
123 }
124 39 ▶ return $val + ($add || 0);
Condition100%2/2
$add || 0
l!l
1821
MC/DC100%1/1
$add || 0
$add
Truth table100%2/2
A
A= $add
Aresult
11
00
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 9 ▶{
Subroutine SetVarInt: called CC=2 SCAR=6.9
Pod undocumented
133 9 my $val = shift;
134 9 my $buff = '';
135 9 my $high = 0;
136 9 for (;;) {
137 15 $buff = chr(($val & 0x7f) | $high) . $buff;
138 15 ▶ last unless $val >>= 7;
Branch: 1 branch
BranchTrueFalseTotal
unless $val >>= 7 9 6 15
139 6 $high = 0x80;
140 }
141 9 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 9 ▶{
Subroutine ReadFLIFHeader: called CC=8 SCAR=22.7
Pod undocumented
152 9 my $raf = shift;
153 9 my ($buff, @vals);
154  
155 # verify this is a valid FLIF file
156 9 ▶ return () unless $raf->Read($buff, 6) == 6 and $buff =~ /^FLIF([0-\x6f])([0-2])/;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($buff, 6) == 6 and $buff =~ /^FLIF([0-\x6f])([0-2])/ 0 9 9
Condition33%1/3
$raf->Read($buff, 6) == 6 and $buff =~ /^FLIF([0-\x6f])([0-2])/
!ll&&!rl&&r
009
MC/DC0%0/2
$raf->Read($buff, 6) == 6 and $buff =~ /^FLIF([0-\x6f])([0-2])/
$raf->Read($buff, 6) == 6 $buff =~ /^FLIF([0-\x6f])([0-2])/
Truth table33%1/3
A and B
A= $raf->Read($buff, 6) == 6B= $buff =~ /^FLIF([0-\x6f])([0-2])/
ABresult
0X0
100
111
157  
158 # decode header information ("FLIF" chunk)
159 9 push @vals, $1, $2; # type, depth
160 9 push @vals, GetVarInt($raf,+1), GetVarInt($raf,+1); # width, height (+1 each)
161 9 ▶ push @vals, GetVarInt($raf,+2) if $vals[0] gt 'H'; # frames (+2)
Branch: 1 branch
BranchTrueFalseTotal
if $vals[0] gt "H" 0 9 9
162  
163 9 ▶ return () unless defined $vals[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vals[-1] 0 9 9
164 9 ▶ return @vals if wantarray; # return the decoded header values
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 5 4 9
165  
166 # return the binary header block
167 4 my $hdrLen = $raf->Tell();
168 4 ▶ return () unless $raf->Seek(0,0) and $raf->Read($buff, $hdrLen) == $hdrLen;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Seek(0, 0) and $raf->Read($buff, $hdrLen) == $hdrLen 0 4 4
Condition33%1/3
$raf->Seek(0, 0) and $raf->Read($buff, $hdrLen) == $hdrLen
!ll&&!rl&&r
004
MC/DC0%0/2
$raf->Seek(0, 0) and $raf->Read($buff, $hdrLen) == $hdrLen
$raf->Seek(0, 0) $raf->Read($buff, $hdrLen) == $hdrLen
Truth table33%1/3
A and B
A= $raf->Seek(0, 0)B= $raf->Read($buff, $hdrLen) == $hdrLen
ABresult
0X0
100
111
169 4 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 4 ▶{
Subroutine WriteFLIF: called CC=32 SCAR=39.0
Pod undocumented
179 4 my ($et, $dirInfo) = @_;
180 4 my $raf = $$dirInfo{RAF};
181 4 my ($buff, $soi, @addTags, %doneTag);
182  
183 # verify FLIF header and copy it to the output file
184 4 ▶ $buff = ReadFLIFHeader($raf) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$buff = &ReadFLIFHeader($raf) or return 0 4 0 4
185 4 my $outfile = $$dirInfo{OutFile};
186 4 ▶ Write($outfile, $buff) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff) or return -1 4 0 4
187  
188 4 $et->InitWriteDirs(\%flifMap);
189 4 my $tagTablePtr = GetTagTable('Image::ExifTool::FLIF::Main');
190  
191 # loop through the FLIF chunks
192 4 for (;;) {
193 16 my ($tag, $size, $inflated);
194 # read new tag (or soi) unless we already hit the soi (start of image)
195 16 ▶ if (not defined $soi) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $soi) 13 3 16
196 13 ▶ $raf->Read($buff, 4) == 4 or $et->Error('Unexpected EOF'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 4) == 4 or $et->Error("Unexpected EOF"), last 13 0 13
197 13 ▶ if ($buff lt ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff lt " ") 4 9 13
198 4 $soi = $buff; # we have hit the start of image (no more metadata)
199 # make list of new tags to add
200 4 foreach $tag ('eXif', 'eXmp', 'iCCP') {
201 12 ▶ push @addTags, $tag if $$et{ADD_DIRS}{$$tagTablePtr{$tag}{Name}} and not $doneTag{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $et->{"ADD_DIRS"}{$tagTablePtr->{$tag}{"Name"}} and not $doneTag{$tag} 3 9 12
Condition100%3/3
$et->{"ADD_DIRS"}{$tagTablePtr->{$tag}{"Name"}} and not $doneTag{$tag}
!ll&&!rl&&r
363
MC/DC100%2/2
$et->{"ADD_DIRS"}{$tagTablePtr->{$tag}{"Name"}} and not $doneTag{$tag}
$et->{"ADD_DIRS"}{$tagTablePtr->{$tag}{"Name"}} not $doneTag{$tag}
Truth table100%3/3
A and not(B)
A= $et->{"ADD_DIRS"}{$tagTablePtr->{$tag}{"Name"}}B= not $doneTag{$tag}
ABresult
0X0
100
111
202 }
203 }
204 }
205 16 ▶ if (not defined $soi) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $soi) { } 9 7 16
elsif (@addTags) { } 3 4 7
206 9 $tag = $buff;
207 9 $size = GetVarInt($raf); # read the data size
208 } elsif (@addTags) {
209 3 $tag = shift @addTags;
210 3 ($buff, $size) = ('', 0); # create metadata from scratch
211 } else {
212 # finish copying file (no more metadata to add)
213 4 ▶ Write($outfile, $soi) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $soi) or return -1 4 0 4
214 4 ▶ Write($outfile, $buff) or return -1 while $raf->Read($buff, 65536);
Branch: 2 branches
BranchTrueFalseTotal
$raf->Read($buff, 65536) and do { &Write($outfile, $buff) or return -1 } 4 4 8
&Write($outfile, $buff) or return -1 4 0 4
215 4 last; # all done!
216 }
217 12 my $tagInfo = $et->GetTagInfo($tagTablePtr, $tag);
218 12 ▶ if ($tagInfo and $$tagInfo{SubDirectory} and $$et{EDIT_DIRS}{$$tagInfo{Name}}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tagInfo and $tagInfo->{'SubDirectory'} and $et->{'EDIT_DIRS'}{$tagInfo->{'Name'}}) { } 12 0 12
elsif (not defined $soi) { } 0 0 0
Condition33%2/6
$tagInfo and $tagInfo->{"SubDirectory"} and $et->{"EDIT_DIRS"}{$tagInfo->{"Name"}}
!ll&&!rl&&r
0012
$tagInfo and $tagInfo->{"SubDirectory"}
!ll&&!rl&&r
0012
MC/DC0%0/3
$tagInfo and $tagInfo->{"SubDirectory"} and $et->{"EDIT_DIRS"}{$tagInfo->{"Name"}}
$tagInfo $tagInfo->{"SubDirectory"} $et->{"EDIT_DIRS"}{$tagInfo->{"Name"}}
Truth table25%1/4
A and B and C
A= $tagInfoB= $tagInfo->{"SubDirectory"}C= $et->{"EDIT_DIRS"}{$tagInfo->{"Name"}}
ABCresult
0XX0
10X0
1100
1111
219 12 $doneTag{$tag} = 1; # prevent adding this back again later
220 12 ▶ unless (defined $soi) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $soi) 9 3 12
221 9 ▶ $raf->Read($buff, $size) == $size or $et->Error("Truncated FLIF $tag chunk"), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, $size) == $size or $et->Error("Truncated FLIF $tag chunk"), last 9 0 9
222 }
223 # rewrite the compressed data
224 12 ▶ if (eval { require IO::Uncompress::RawInflate } and eval { require IO::Compress::RawDeflate } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require IO::Uncompress::RawInflate } } and eval { do { require IO::Compress::RawDeflate } }) { } 12 0 12
Condition33%1/3
eval { do { require IO::Uncompress::RawInflate } } and eval { do { require IO::Compress::RawDeflate } }
!ll&&!rl&&r
0012
MC/DC0%0/2
eval { do { require IO::Uncompress::RawInflate } } and eval { do { require IO::Compress::RawDeflate } }
eval { do { require IO::Uncompress::RawInflate } } eval { do { require IO::Compress::RawDeflate } }
Truth table33%1/3
A and B
A= eval { do { require IO::Uncompress::RawInflate } }B= eval { do { require IO::Compress::RawDeflate } }
ABresult
0X0
100
111
225 12 ▶ if (length $buff == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $buff == 0) { } 3 9 12
elsif (not IO::Uncompress::RawInflate::rawinflate(\$buff, \$inflated)) { } 0 9 9
226 3 $inflated = $buff; # (creating from scratch, so no need to inflate)
227 } elsif (not IO::Uncompress::RawInflate::rawinflate(\$buff => \$inflated)) {
228 0 $et->Error("Error inflating FLIF $tag chunk"), last;
229 }
230 12 my $subdir = $$tagInfo{SubDirectory};
231 my %subdirInfo = (
232 DirName => $$tagInfo{Name},
233 DataPt => \$inflated,
234 DirStart => length($inflated) ? $$subdir{Start} : undef,
235 12 ▶ ReadOnly => 1, # (used only by WriteXMP)
Branch: 1 branch
BranchTrueFalseTotal
length $inflated ? : 9 3 12
236 );
237 12 my $subTable = GetTagTable($$subdir{TagTable});
238 12 $inflated = $et->WriteDirectory(\%subdirInfo, $subTable, $$subdir{WriteProc});
239 12 ▶ if (defined $inflated) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inflated) 12 0 12
240 12 ▶ next unless length $inflated; # (delete directory if length is zero)
Branch: 1 branch
BranchTrueFalseTotal
unless length $inflated 3 9 12
241 9 ▶ $inflated = $$subdir{Header} . $inflated if $$subdir{Header}; # (add back header if necessary)
Branch: 1 branch
BranchTrueFalseTotal
if $subdir->{"Header"} 3 6 9
242 9 ▶ unless (IO::Compress::RawDeflate::rawdeflate(\$inflated => \$buff)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (IO::Compress::RawDeflate::rawdeflate(\$inflated, \$buff)) 0 9 9
243 0 $et->Error("Error deflating FLIF $tag chunk"), last;
244 }
245 }
246 } else {
247 0 $et->Warn('Install IO::Compress::RawDeflate to write FLIF metadata');
248 }
249 9 ▶ Write($outfile, $tag, SetVarInt(length $buff), $buff) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $tag, &SetVarInt(length $buff), $buff) or return -1 9 0 9
250 } elsif (not defined $soi) {
251 0 ▶ Write($outfile, $tag, SetVarInt($size)) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $tag, &SetVarInt($size)) or return -1 0 0 0
252 0 ▶ Image::ExifTool::CopyBlock($raf, $outfile, $size) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
&Image::ExifTool::CopyBlock($raf, $outfile, $size) or return -1 0 0 0
253 }
254 }
255 4 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 5 ▶{
Subroutine ProcessFLIF: called CC=13 SCAR=28.0
Pod undocumented
264 5 my ($et, $dirInfo) = @_;
265 5 my $raf = $$dirInfo{RAF};
266 5 my ($buff, $tag, $inflated);
267  
268 # verify this is a valid FLIF file and read the header
269 5 ▶ my @vals = ReadFLIFHeader($raf) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@vals) = &ReadFLIFHeader($raf) or return 0 5 0 5
270  
271 5 $et->SetFileType();
272 5 my $tagTablePtr = GetTagTable('Image::ExifTool::FLIF::Main');
273 5 my $verbose = $et->Options('Verbose');
274  
275 # save the header information
276 5 ▶ $et->VPrint(0, "FLIF header:\n") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 5 5
277 5 ▶ for ($tag=0; defined $vals[$tag]; ++$tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vals[$tag]) 20 5 25
278 20 $et->HandleTag($tagTablePtr, $tag, $vals[$tag]);
279 }
280  
281 # loop through the FLIF chunks
282 5 for (;;) {
283 17 ▶ $raf->Read($tag, 4) == 4 or $et->Warn('Unexpected EOF'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($tag, 4) == 4 or $et->Warn("Unexpected EOF"), last 17 0 17
284 17 my $byte = ord substr($tag, 0, 1);
285 # all done if we arrived at the image chunk
286 17 ▶ $byte < 32 and $et->HandleTag($tagTablePtr, 5, $byte), last;
Branch: 1 branch
BranchTrueFalseTotal
$byte < 32 and $et->HandleTag($tagTablePtr, 5, $byte), last 5 12 17
287 12 my $size = GetVarInt($raf);
288 12 ▶ $et->VPrint(0, "FLIF $tag ($size bytes):\n") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 12 12
289 12 ▶ if ($$tagTablePtr{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagTablePtr->{$tag}) { } 12 0 12
290 12 ▶ $raf->Read($buff, $size) == $size or $et->Warn("Truncated FLIF $tag chunk"), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, $size) == $size or $et->Warn("Truncated FLIF $tag chunk"), last 12 0 12
291 12 ▶ $et->VerboseDump(\$buff, Addr => $raf->Tell() - $size) if $verbose > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose > 2 0 12 12
292 # inflate the compressed data
293 12 ▶ if (eval { require IO::Uncompress::RawInflate }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require IO::Uncompress::RawInflate } }) { } 12 0 12
294 12 ▶ if (IO::Uncompress::RawInflate::rawinflate(\$buff => \$inflated)) {
Branch: 1 branch
BranchTrueFalseTotal
if (IO::Uncompress::RawInflate::rawinflate(\$buff, \$inflated)) { } 12 0 12
295 12 $et->HandleTag($tagTablePtr, $tag, $inflated,
296 DataPt => \$inflated,
297 Size => length $inflated,
298 Extra => ' inflated',
299 );
300 } else {
301 0 $et->Warn("Error inflating FLIF $tag chunk");
302 }
303 } else {
304 0 $et->Warn('Install IO::Uncompress::RawInflate to decode FLIF metadata');
305 }
306 } else {
307 0 ▶ $raf->Seek($size, 1) or $et->Warn('Seek error'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($size, 1) or $et->Warn("Seek error"), last 0 0 0
308 }
309 }
310 5 return 1;
311 }
312  
313 1 1; # end
314  
315 __END__
« Image/ExifTool/CanonCustom.pm ↑ Summary Image/ExifTool/FITS.pm »