Image/ExifTool/WriteRIFF.pl

filter: Statementstmt 66.3% 126 / 190 Branchbran 48.8% 82 / 168 Conditioncond 46.4% 39 / 84 MC/DCmcdc 13.3% 6 / 45 Subroutinesub 100% 2 / 2 Podpod 0.0% 0 / 1 totaltotal 52.0% 255 / 490 CC 112 SCAR 71.1
CC
112
Cov
56%
WriteRIFF
1234.2
BEGIN
1.0
CRAP 1223.3
« Image/ExifTool/WritePostScript.pl ↑ Summary Image/ExifTool/Jpeg2000.pm »
1 #------------------------------------------------------------------------------
2 # File: WriteRIFF.pl
3 #
4 # Description: Write RIFF-format files
5 #
6 # Revisions: 2020-09-26 - P. Harvey Created
7 #
8 # Notes: Currently writes only WEBP files
9 #
10 # References: https://developers.google.com/speed/webp/docs/riff_container
11 #------------------------------------------------------------------------------
12  
13 package Image::ExifTool::RIFF;
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # map of where information is stored in WebP image
18 1 my %webpMap = (
19 'XMP ' => 'RIFF', # (the RIFF chunk name is 'XMP ')
20 EXIF => 'RIFF',
21 ICCP => 'RIFF',
22 C2PA => 'RIFF',
23 JUMBF => 'C2PA',
24 XMP => 'XMP ',
25 IFD0 => 'EXIF',
26 IFD1 => 'IFD0',
27 ICC_Profile => 'ICCP',
28 ExifIFD => 'IFD0',
29 GPS => 'IFD0',
30 SubIFD => 'IFD0',
31 GlobParamIFD => 'IFD0',
32 PrintIM => 'IFD0',
33 InteropIFD => 'ExifIFD',
34 MakerNotes => 'ExifIFD',
35 );
36  
37 1 my %deletableGroup = (
38 "XMP\0" => 'XMP', # delete incorrectly written "XMP\0" tag with XMP group
39 SEAL => 'SEAL', # delete SEAL tag with SEAL group
40 );
41  
42 #------------------------------------------------------------------------------
43 # Write RIFF file (currently WebP-type only)
44 # Inputs: 0) ExifTool object ref, 1) dirInfo ref
45 # Returns: 1 on success, 0 if this wasn't a valid RIFF file, or -1 if
46 # an output file was specified and a write error occurred
47 sub WriteRIFF($$)
48 3 ▶{
Subroutine WriteRIFF: called CC=112 SCAR=71.2
Pod undocumented
49 3 my ($et, $dirInfo) = @_;
50 3 ▶ $et or return 1; # allow dummy access to autoload this package
Branch: 1 branch
BranchTrueFalseTotal
$et or return 1 3 0 3
51 3 my $outfile = $$dirInfo{OutFile};
52 3 my $outsize = 0;
53 3 my $raf = $$dirInfo{RAF};
54 3 my $verbose = $et->Options('Verbose');
55 3 my ($buff, $err, $pass, %has, %dirDat, $imageWidth, $imageHeight);
56  
57 # do this in 2 passes so we can set the size of the containing RIFF chunk
58 # without having to buffer the output (also to set the WebP_Flags)
59 3 for ($pass=0; ; ++$pass) {
60 6 my %doneDir;
61 # verify this is a valid RIFF file
62 6 ▶ return 0 unless $raf->Read($buff, 12) == 12;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($buff, 12) == 12 0 6 6
63 6 ▶ return 0 unless $buff =~ /^(RIFF|RF64)....(.{4})/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /^(RIFF|RF64)....(.{4})/s 0 6 6
64  
65 6 ▶ unless ($1 eq 'RIFF' and $2 eq 'WEBP') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($1 eq "RIFF" and $2 eq "WEBP") 0 6 6
Condition33%1/3
$1 eq "RIFF" and $2 eq "WEBP"
!ll&&!rl&&r
006
MC/DC0%0/2
$1 eq "RIFF" and $2 eq "WEBP"
$1 eq "RIFF" $2 eq "WEBP"
Truth table33%1/3
A and B
A= $1 eq "RIFF"B= $2 eq "WEBP"
ABresult
0X0
100
111
66 0 my $type = $2;
67 0 $type =~ tr/-_a-zA-Z//dc;
68 0 $et->Error("Can't currently write $1 $type files");
69 0 return 1;
70 }
71 6 SetByteOrder('II');
72  
73 # determine which directories we must write for this file type
74 6 ▶ $et->Options(Verbose => 0) if $pass; # (avoid duplicate Verbose options here)
Branch: 1 branch
BranchTrueFalseTotal
if $pass 3 3 6
75 6 $et->InitWriteDirs(\%webpMap);
76 6 my $addDirs = $$et{ADD_DIRS};
77 6 my $editDirs = $$et{EDIT_DIRS};
78 6 ▶ $$addDirs{IFD0} = 'EXIF' if $$addDirs{EXIF}; # set flag to add IFD0 if adding EXIF (don't ask)
Branch: 1 branch
BranchTrueFalseTotal
if $addDirs->{"EXIF"} 4 2 6
79 6 my ($createVP8X, $deleteVP8X);
80  
81 # write header
82 6 ▶ if ($pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) { } 3 3 6
83 3 $et->Options(Verbose => $verbose);
84 my $needsVP8X = ($has{ANIM} or $has{'XMP '} or $has{EXIF} or
85 3 ▶ $has{ALPH} or $has{ICCP});
Condition58%7/12
$has{'ANIM'} || $has{'XMP '} || $has{'EXIF'} || $has{'ALPH'} || $has{'ICCP'}
l!l&&r!l&&!r
300
$has{"ANIM"} or $has{"XMP "} or $has{"EXIF"} or $has{"ALPH"}
l!l&&r!l&&!r
210
$has{"ANIM"} or $has{"XMP "} or $has{"EXIF"}
l!l&&r!l&&!r
201
$has{"ANIM"} or $has{"XMP "}
l!l&&r!l&&!r
021
MC/DC0%0/5
$has{'ANIM'} || $has{'XMP '} || $has{'EXIF'} || $has{'ALPH'} || $has{'ICCP'}
$has{"ANIM"} $has{"XMP "} $has{"EXIF"} $has{"ALPH"} $has{'ICCP'}
Truth table33%2/6
A or B or C or D || E
A= $has{"ANIM"}B= $has{"XMP "}C= $has{"EXIF"}D= $has{"ALPH"}E= $has{'ICCP'}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
86 3 ▶ if ($has{VP8X} and not $needsVP8X and $$et{CHANGED}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has{'VP8X'} and not $needsVP8X and $et->{'CHANGED'}) { } 0 3 3
elsif ($needsVP8X and not $has{'VP8X'}) { } 0 3 3
Condition33%2/6
$has{"VP8X"} and not $needsVP8X and $et->{"CHANGED"}
!ll&&!rl&&r
300
$has{"VP8X"} and not $needsVP8X
!ll&&!rl&&r
030
MC/DC0%0/3
$has{"VP8X"} and not $needsVP8X and $et->{"CHANGED"}
$has{"VP8X"} not $needsVP8X $et->{"CHANGED"}
Truth table25%1/4
A and not(B) and C
A= $has{"VP8X"}B= not $needsVP8XC= $et->{"CHANGED"}
ABCresult
0XX0
10X0
1100
1111
87 0 $deleteVP8X = 1; # delete the VP8X chunk
88 0 $outsize -= 18; # account for missing VP8X
89 ▶ } elsif ($needsVP8X and not $has{VP8X}) {
Condition33%1/3
$needsVP8X and not $has{"VP8X"}
!ll&&!rl&&r
030
MC/DC0%0/2
$needsVP8X and not $has{"VP8X"}
$needsVP8X not $has{"VP8X"}
Truth table33%1/3
A and not(B)
A= $needsVP8XB= not $has{"VP8X"}
ABresult
0X0
100
111
90 0 ▶ if (defined $imageWidth) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $imageWidth) { } 0 0 0
91 0 ++$$et{CHANGED};
92 0 $createVP8X = 1; # add VP8X chunk
93 0 $outsize += 18; # account for VP8X size
94 } else {
95 0 $et->Warn('Error getting image size for required VP8X chunk');
96 }
97 }
98 # finally we can set the overall RIFF chunk size:
99 3 Set32u($outsize - 8, \$buff, 4);
100 3 ▶ Write($outfile, $buff) or $err = 1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff) or $err = 1 3 0 3
101 # create VP8X chunk if necessary
102 3 ▶ if ($createVP8X) {
Branch: 1 branch
BranchTrueFalseTotal
if ($createVP8X) 0 3 3
103 0 $et->VPrint(0," Adding required VP8X chunk (Extended WEBP)\n");
104 0 my $flags = 0;
105 0 ▶ $flags |= 0x02 if $has{ANIM};
Branch: 1 branch
BranchTrueFalseTotal
if $has{"ANIM"} 0 0 0
106 0 ▶ $flags |= 0x04 if $has{'XMP '};
Branch: 1 branch
BranchTrueFalseTotal
if $has{"XMP "} 0 0 0
107 0 ▶ $flags |= 0x08 if $has{EXIF};
Branch: 1 branch
BranchTrueFalseTotal
if $has{"EXIF"} 0 0 0
108 0 ▶ $flags |= 0x10 if $has{ALPH};
Branch: 1 branch
BranchTrueFalseTotal
if $has{"ALPH"} 0 0 0
109 0 ▶ $flags |= 0x20 if $has{ICCP};
Branch: 1 branch
BranchTrueFalseTotal
if $has{"ICCP"} 0 0 0
110 0 Write($outfile, 'VP8X', pack('V3v', 10, $flags,
111 ($imageWidth-1) | ((($imageHeight-1) & 0xff) << 24),
112 ($imageHeight-1) >> 8));
113 # write ICCP after VP8X
114 0 ▶ Write($outfile, $dirDat{ICCP}) or $err = 1 if $dirDat{ICCP};
Branch: 1 branch
BranchTrueFalseTotal
if $dirDat{"ICCP"} 0 0 0
Condition0%0/6
$dirDat{'ICCP'} && (&Write($outfile, $dirDat{'ICCP'}) || ($err = 1))
!ll&&!rl&&r
000
&Write($outfile, $dirDat{"ICCP"}) or $err = 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$dirDat{'ICCP'} && (&Write($outfile, $dirDat{'ICCP'}) || ($err = 1))
$dirDat{'ICCP'} &Write($outfile, $dirDat{"ICCP"}) $err = 1
Truth table0%0/4
A && B or C
A= $dirDat{'ICCP'}B= &Write($outfile, $dirDat{"ICCP"})C= $err = 1
ABCresult
0XX0
1000
11X1
1011
115 }
116 } else {
117 3 $outsize += length $buff;
118 }
119 6 my $pos = 12;
120 #
121 # Read chunks in RIFF image
122 #
123 6 for (;;) {
124 34 my ($tag, $len);
125 34 my $num = $raf->Read($buff, 8);
126 34 ▶ if ($num < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num < 8) { } 8 26 34
127 8 ▶ $num and $et->Error('RIFF format error'), return 1;
Branch: 1 branch
BranchTrueFalseTotal
$num and $et->Error("RIFF format error"), (return 1) 0 8 8
128 # all done if we hit end of file unless we need to add EXIF or XMP
129 8 ▶ last unless $$addDirs{EXIF} or $$addDirs{'XMP '} or $$addDirs{ICCP};
Branch: 1 branch
BranchTrueFalseTotal
unless $addDirs->{"EXIF"} or $addDirs->{"XMP "} or $addDirs->{"ICCP"} 6 2 8
Condition66%4/6
$addDirs->{"EXIF"} or $addDirs->{"XMP "} or $addDirs->{"ICCP"}
l!l&&r!l&&!r
206
$addDirs->{"EXIF"} or $addDirs->{"XMP "}
l!l&&r!l&&!r
206
MC/DC33%1/3
$addDirs->{"EXIF"} or $addDirs->{"XMP "} or $addDirs->{"ICCP"}
$addDirs->{"EXIF"} $addDirs->{"XMP "} $addDirs->{"ICCP"}
Truth table50%2/4
A or B or C
A= $addDirs->{"EXIF"}B= $addDirs->{"XMP "}C= $addDirs->{"ICCP"}
ABCresult
1XX1
01X1
0011
0000
130 # continue to add required EXIF or XMP chunks
131 2 $num = $len = 0;
132 2 $buff = $tag = '';
133 } else {
134 26 $pos += 8;
135 26 ($tag, $len) = unpack('a4V', $buff);
136 26 ▶ if ($len <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len <= 0) 0 26 26
137 0 ▶ if ($len < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len < 0) { } 0 0 0
elsif ($tag eq "\000\000\000\000") { } 0 0 0
138 0 $et->Error('Invalid chunk length');
139 0 return 1;
140 } elsif ($tag eq "\0\0\0\0") {
141 # avoid reading through corrupted files filled with nulls because it takes forever
142 0 $et->Error('Encountered empty null chunk. Processing aborted');
143 0 return 1;
144 } else { # (just in case a tag may have no data)
145 0 ▶ if ($pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) { } 0 0 0
146 0 ▶ Write($outfile, $buff) or $err = 1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff) or $err = 1 0 0 0
147 } else {
148 0 $outsize += length $buff;
149 }
150 0 next;
151 }
152 }
153 }
154 # RIFF chunks are padded to an even number of bytes
155 28 my $len2 = $len + ($len & 0x01);
156 # handle incorrect "XMP\0" chunk ID written by Google software
157 28 ▶ if ($deletableGroup{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deletableGroup{$tag}) 0 28 28
158 0 ▶ if ($$et{DEL_GROUP}{$deletableGroup{$tag}}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($et->{'DEL_GROUP'}{$deletableGroup{$tag}}) { } 0 0 0
elsif ($tag eq "XMP\000") { } 0 0 0
159 # just ignore this chunk if deleting the associated group
160 0 ▶ $raf->Seek($len2, 1) or $et->Error('Seek error'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($len2, 1) or $et->Error("Seek error"), last 0 0 0
161 0 ▶ $et->VPrint(0, " Deleting $deletableGroup{$tag}\n") if $pass;
Branch: 1 branch
BranchTrueFalseTotal
if $pass 0 0 0
162 0 ++$$et{CHANGED};
163 0 next;
164 } elsif ($tag eq "XMP\0") {
165 0 ▶ $et->Warn('Incorrect XMP tag ID',1) if $pass;
Branch: 1 branch
BranchTrueFalseTotal
if $pass 0 0 0
166 }
167 }
168 # edit/add/delete necessary metadata chunks (EXIF must come before XMP)
169 28 ▶ if ($$editDirs{$tag} or $tag eq '' or ($tag eq 'XMP ' and $$addDirs{EXIF})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($editDirs->{$tag} or $tag eq "" or $tag eq "XMP " and $addDirs->{"EXIF"}) 10 18 28
Condition66%6/9
$editDirs->{$tag} or $tag eq "" or $tag eq "XMP " and $addDirs->{"EXIF"}
l!l&&r!l&&!r
10018
$editDirs->{$tag} or $tag eq ""
l!l&&r!l&&!r
8218
$tag eq "XMP " and $addDirs->{"EXIF"}
!ll&&!rl&&r
1800
MC/DC50%2/4
$editDirs->{$tag} or $tag eq "" or $tag eq "XMP " and $addDirs->{"EXIF"}
$editDirs->{$tag} $tag eq "" $tag eq "XMP " $addDirs->{"EXIF"}
Truth table60%3/5
A or B or C and D
A= $editDirs->{$tag}B= $tag eq ""C= $tag eq "XMP "D= $addDirs->{"EXIF"}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
170 10 my $handledTag;
171 10 ▶ if ($len2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len2) { } 8 2 10
172 8 ▶ $et->Warn("Duplicate '${tag}' chunk") if $doneDir{$tag} and not $pass;
Branch: 1 branch
BranchTrueFalseTotal
if $doneDir{$tag} and not $pass 0 8 8
Condition33%1/3
$doneDir{$tag} and not $pass
!ll&&!rl&&r
800
MC/DC0%0/2
$doneDir{$tag} and not $pass
$doneDir{$tag} not $pass
Truth table33%1/3
A and not(B)
A= $doneDir{$tag}B= not $pass
ABresult
0X0
100
111
173 8 $doneDir{$tag} = 1;
174 8 ▶ $raf->Read($buff, $len2) == $len2 or $et->Error("Truncated '${tag}' chunk"), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, $len2) == $len2 or $et->Error("Truncated '${tag}' chunk"), last 8 0 8
175 8 $pos += $len2; # update current position
176 } else {
177 2 $buff = '';
178 }
179 #
180 # add/edit/delete EXIF/XMP/ICCP (note: EXIF must come before XMP, and ICCP is written elsewhere)
181 #
182 10 my %dirName = ( EXIF => 'IFD0', 'XMP ' => 'XMP', ICCP => 'ICC_Profile', C2PA => 'JUMBF' );
183 10 my %tblName = ( EXIF => 'Exif', 'XMP ' => 'XMP', ICCP => 'ICC_Profile', C2PA => 'Jpeg2000' );
184 10 my $dir;
185 10 foreach $dir ('EXIF', 'XMP ', 'ICCP', 'C2PA' ) {
186 40 ▶ next unless $tag eq $dir or ($$addDirs{$dir} and
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq $dir or $addDirs->{$dir} and $tag eq "" || $tag eq "XMP " && $dir eq "EXIF" 28 12 40
Condition75%9/12
$tag eq $dir or $addDirs->{$dir} and $tag eq "" || $tag eq "XMP " && $dir eq "EXIF"
l!l&&r!l&&!r
8428
$addDirs->{$dir} and $tag eq "" || $tag eq "XMP " && $dir eq "EXIF"
!ll&&!rl&&r
2624
$tag eq "" or $tag eq "XMP " and $dir eq "EXIF"
l!l&&r!l&&!r
402
$tag eq "XMP " and $dir eq "EXIF"
!ll&&!rl&&r
200
MC/DC60%3/5
$tag eq $dir or $addDirs->{$dir} and $tag eq "" || $tag eq "XMP " && $dir eq "EXIF"
$tag eq $dir $addDirs->{$dir} $tag eq "" $tag eq "XMP " $dir eq "EXIF"
Truth table66%4/6
A or B and C or D and E
A= $tag eq $dirB= $addDirs->{$dir}C= $tag eq ""D= $tag eq "XMP "E= $dir eq "EXIF"
ABCDEresult
1XXXX1
011XX1
010111
00XXX0
0100X0
010100
187 ($tag eq '' or ($tag eq 'XMP ' and $dir eq 'EXIF')));
188 12 my $start;
189 12 ▶ unless ($pass) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 6 6 12
190 # write the EXIF and save the result for the next pass
191 6 my $dataPt = \$buff;
192 6 ▶ if ($tag eq 'EXIF') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'EXIF') { } 2 4 6
elsif ($dir ne $tag) { } 2 2 4
193 # (only need to set directory $start for EXIF)
194 2 ▶ if ($buff =~ /^Exif\0\0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ /^Exif\0\0/) { } 0 2 2
195 0 ▶ if ($$et{DEL_GROUP}{EXIF}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->{'DEL_GROUP'}{'EXIF'}) { } 0 0 0
196 # remove incorrect header if rewriting anyway
197 0 $buff = substr($buff, 6);
198 0 $len -= 6;
199 0 $len2 -= 6;
200 } else {
201 0 ▶ $et->Warn('Improper EXIF header',1) unless $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless $pass 0 0 0
202 0 $start = 6;
203 }
204 } else {
205 2 $start = 0;
206 }
207 } elsif ($dir ne $tag) {
208 # create from scratch
209 2 my $buf2 = '';
210 2 $dataPt = \$buf2;
211 }
212 # write the new directory to memory
213 my %dirInfo = (
214 DataPt => $dataPt,
215 DataPos => 0, # (relative to Base)
216 DirStart => $start,
217 Base => $pos - $len2,
218 Parent => $dir,
219 6 DirName => $dirName{$dir},
220 );
221 # must pass the TagInfo to enable deletion of C2PA information
222 6 ▶ if (ref $Image::ExifTool::RIFF::Main{$dir} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Image::ExifTool::RIFF::Main{$dir} eq "HASH") 3 3 6
223 3 $dirInfo{TagInfo} = $Image::ExifTool::RIFF::Main{$dir};
224 }
225 6 my $tagTablePtr = GetTagTable("Image::ExifTool::$tblName{$dir}::Main");
226 # (override writeProc for EXIF because it has the TIFF header)
227 6 ▶ my $writeProc = $dir eq 'EXIF' ? \&Image::ExifTool::WriteTIFF : undef;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'EXIF' ? : 3 3 6
228 6 $dirDat{$dir} = $et->WriteDirectory(\%dirInfo, $tagTablePtr, $writeProc);
229 }
230 12 delete $$addDirs{$dir}; # (don't try to add again)
231 12 ▶ if (defined $dirDat{$dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dirDat{$dir}) 12 0 12
232 12 ▶ if ($dir eq $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq $tag) 8 4 12
233 8 $handledTag = 1; # set flag indicating we edited this tag
234 # increment CHANGED count if we are deleting the directory
235 8 ▶ ++$$et{CHANGED} unless length $dirDat{$dir};
Branch: 1 branch
BranchTrueFalseTotal
unless length $dirDat{$dir} 4 4 8
236 }
237 12 ▶ if (length $dirDat{$dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dirDat{$dir}) 8 4 12
238 8 ▶ if ($pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) { } 4 4 8
239 # write metadata chunk now (but not ICCP because it was added earlier)
240 4 ▶ Write($outfile, $dirDat{$dir}) or $err = 1 unless $dir eq 'ICCP';
Branch: 1 branch
BranchTrueFalseTotal
unless $dir eq "ICCP" 4 0 4
Condition50%2/4
$dir eq 'ICCP' || (&Write($outfile, $dirDat{$dir}) || ($err = 1))
l!l
04
&Write($outfile, $dirDat{$dir}) or $err = 1
l!l
40
MC/DC0%0/3
$dir eq 'ICCP' || (&Write($outfile, $dirDat{$dir}) || ($err = 1))
$dir eq 'ICCP' &Write($outfile, $dirDat{$dir}) $err = 1
Truth table0%0/4
A || B or C
A= $dir eq 'ICCP'B= &Write($outfile, $dirDat{$dir})C= $err = 1
ABCresult
1XX1
01X1
0011
0000
241 } else {
242 # preserve (incorrect EXIF) header if it existed
243 4 ▶ my $hdr = $start ? substr($buff,0,$start) : '';
Branch: 1 branch
BranchTrueFalseTotal
$start ? : 0 4 4
244 # (don't overwrite $len here because it may be XMP length)
245 4 my $dirLen = length($dirDat{$dir}) + length($hdr);
246 # add chunk header and padding
247 4 ▶ my $pad = $dirLen & 0x01 ? "\0" : '';
Branch: 1 branch
BranchTrueFalseTotal
$dirLen & 1 ? : 0 4 4
248 4 $dirDat{$dir} = $dir . Set32u($dirLen) . $hdr . $dirDat{$dir} . $pad;
249 4 $outsize += length($dirDat{$dir});
250 4 $has{$dir} = 1;
251 }
252 }
253 }
254 }
255 #
256 # just copy XMP, EXIF or ICC if nothing changed
257 #
258 10 ▶ if (not $handledTag and length $buff) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $handledTag and length $buff) 0 10 10
Condition66%2/3
not $handledTag and length $buff
!ll&&!rl&&r
820
MC/DC0%0/2
not $handledTag and length $buff
not $handledTag length $buff
Truth table66%2/3
not(A) and B
A= not $handledTagB= length $buff
ABresult
0X0
100
111
259 # write the chunk without changes
260 0 ▶ if ($pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) { } 0 0 0
261 0 ▶ Write($outfile, $tag, Set32u($len), $buff) or $err = 1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $tag, &Set32u($len), $buff) or $err = 1 0 0 0
262 } else {
263 0 $outsize += 8 + length($buff);
264 0 $has{$tag} = 1;
265 }
266 }
267 10 next;
268 }
269 18 $pos += $len2; # set read position at end of chunk data
270 #
271 # update necessary flags in VP8X chunk
272 #
273 18 ▶ if ($tag eq 'VP8X') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "VP8X") 6 12 18
274 6 my $buf2;
275 6 ▶ if ($len2 < 10 or $raf->Read($buf2, $len2) != $len2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len2 < 10 or $raf->Read($buf2, $len2) != $len2) 0 6 6
Condition33%1/3
$len2 < 10 or $raf->Read($buf2, $len2) != $len2
l!l&&r!l&&!r
006
MC/DC0%0/2
$len2 < 10 or $raf->Read($buf2, $len2) != $len2
$len2 < 10 $raf->Read($buf2, $len2) != $len2
Truth table33%1/3
A or B
A= $len2 < 10B= $raf->Read($buf2, $len2) != $len2
ABresult
1X1
011
000
276 0 $et->Error('Truncated VP8X chunk');
277 0 return 1;
278 }
279 6 ▶ if ($pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) { } 3 3 6
280 3 ▶ if ($deleteVP8X) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deleteVP8X) 0 3 3
281 0 $et->VPrint(0," Deleting unnecessary VP8X chunk (Standard WEBP)\n");
282 0 next;
283 }
284 # ...but first set the VP8X flags
285 3 my $flags = Get32u(\$buf2, 0);
286 3 $flags &= ~0x2c; # (reset flags for everything we can write)
287 3 ▶ $flags |= 0x04 if $has{'XMP '};
Branch: 1 branch
BranchTrueFalseTotal
if $has{"XMP "} 2 1 3
288 3 ▶ $flags |= 0x08 if $has{EXIF};
Branch: 1 branch
BranchTrueFalseTotal
if $has{"EXIF"} 2 1 3
289 3 ▶ $flags |= 0x20 if $has{ICCP};
Branch: 1 branch
BranchTrueFalseTotal
if $has{"ICCP"} 0 3 3
290 3 Set32u($flags, \$buf2, 0);
291 3 ▶ Write($outfile, $buff, $buf2) or $err = 1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff, $buf2) or $err = 1 3 0 3
292 } else {
293 # get the image size
294 3 $imageWidth = (Get32u(\$buf2, 4) & 0xffffff) + 1;
295 3 $imageHeight = (Get32u(\$buf2, 6) >> 8) + 1;
296 3 $outsize += 8 + $len2;
297 3 $has{$tag} = 1;
298 }
299 # write ICCP after VP8X
300 6 ▶ Write($outfile, $dirDat{ICCP}) or $err = 1 if $dirDat{ICCP};
Branch: 1 branch
BranchTrueFalseTotal
if $dirDat{"ICCP"} 0 6 6
Condition20%1/5
$dirDat{'ICCP'} && (&Write($outfile, $dirDat{'ICCP'}) || ($err = 1))
!ll
60
&Write($outfile, $dirDat{"ICCP"}) or $err = 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$dirDat{'ICCP'} && (&Write($outfile, $dirDat{'ICCP'}) || ($err = 1))
$dirDat{'ICCP'} &Write($outfile, $dirDat{"ICCP"}) $err = 1
Truth table0%0/4
A && B or C
A= $dirDat{'ICCP'}B= &Write($outfile, $dirDat{"ICCP"})C= $err = 1
ABCresult
0XX0
1000
11X1
1011
301 6 next;
302 }
303 #
304 # just copy all other chunks
305 #
306 12 ▶ if ($pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) { } 6 6 12
307 # write chunk header (still in $buff)
308 6 ▶ Write($outfile, $buff) or $err = 1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff) or $err = 1 6 0 6
309 } else {
310 6 $outsize += length $buff;
311 6 $has{$tag} = 1;
312 }
313 12 ▶ unless ($pass or defined $imageWidth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass or defined $imageWidth) 0 12 12
Condition66%2/3
$pass or defined $imageWidth
l!l&&r!l&&!r
660
MC/DC0%0/2
$pass or defined $imageWidth
$pass defined $imageWidth
Truth table66%2/3
A or B
A= $passB= defined $imageWidth
ABresult
1X1
011
000
314 # get WebP image size from VP8 or VP8L header
315 0 ▶ if ($tag eq 'VP8 ' and $len2 >= 16) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'VP8 ' and $len2 >= 16) { } 0 0 0
elsif ($tag eq 'VP8L' and $len2 >= 6) { } 0 0 0
Condition0%0/3
$tag eq "VP8 " and $len2 >= 16
!ll&&!rl&&r
000
MC/DC0%0/2
$tag eq "VP8 " and $len2 >= 16
$tag eq "VP8 " $len2 >= 16
Truth table0%0/3
A and B
A= $tag eq "VP8 "B= $len2 >= 16
ABresult
0X0
100
111
316 0 ▶ $raf->Read($buff, 16) == 16 or $et->Error('Truncated VP8 chunk'), return 1;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 16) == 16 or $et->Error("Truncated VP8 chunk"), (return 1) 0 0 0
317 0 $outsize += 16;
318 0 ▶ if ($buff =~ /^...\x9d\x01\x2a/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ /^...\x9d\x01\x2a/s) 0 0 0
319 0 $imageWidth = Get16u(\$buff, 6) & 0x3fff;
320 0 $imageHeight = Get16u(\$buff, 8) & 0x3fff;
321 }
322 0 $len2 -= 16;
323 ▶ } elsif ($tag eq 'VP8L' and $len2 >= 6) {
Condition0%0/3
$tag eq "VP8L" and $len2 >= 6
!ll&&!rl&&r
000
MC/DC0%0/2
$tag eq "VP8L" and $len2 >= 6
$tag eq "VP8L" $len2 >= 6
Truth table0%0/3
A and B
A= $tag eq "VP8L"B= $len2 >= 6
ABresult
0X0
100
111
324 0 ▶ $raf->Read($buff, 6) == 6 or $et->Error('Truncated VP8L chunk'), return 1;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 6) == 6 or $et->Error("Truncated VP8L chunk"), (return 1) 0 0 0
325 0 $outsize += 6;
326 0 ▶ if ($buff =~ /^\x2f/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ /^\x2f/s) 0 0 0
327 0 my $word = Get32u(\$buff, 2);
328 0 $imageWidth = (Get16u(\$buff, 1) & 0x3fff) + 1;
329 0 $imageHeight = (($word >> 6) & 0x3fff) + 1;
330 0 ▶ $has{ALPH} = 1 if $word & 0x100000; # set alpha flag if necessary
Branch: 1 branch
BranchTrueFalseTotal
if $word & 1048576 0 0 0
331 }
332 0 $len2 -= 6;
333 }
334 }
335 12 ▶ if ($pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) { } 6 6 12
336 # copy the chunk data in 64k blocks
337 6 ▶ while ($len2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len2) 6 6 12
338 6 my $num = $len2;
339 6 ▶ $num = 65536 if $num > 65536;
Branch: 1 branch
BranchTrueFalseTotal
if $num > 65536 0 6 6
340 6 ▶ $raf->Read($buff, $num) == $num or $et->Error('Truncated RIFF chunk'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, $num) == $num or $et->Error("Truncated RIFF chunk"), last 6 0 6
341 6 ▶ Write($outfile, $buff) or $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff) or $err = 1, last 6 0 6
342 6 $len2 -= $num;
343 }
344 } else {
345 6 ▶ $raf->Seek($len2, 1) or $et->Error('Seek error'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($len2, 1) or $et->Error("Seek error"), last 6 0 6
346 6 $outsize += $len2;
347 }
348 }
349 6 ▶ last if $pass;
Branch: 1 branch
BranchTrueFalseTotal
if $pass 3 3 6
350 3 ▶ $raf->Seek(0,0) or $et->Error('Seek error'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek(0, 0) or $et->Error("Seek error"), last 3 0 3
351 }
352 3 ▶ return $err ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 3 3
353 }
354  
355 1 1; # end
356  
357 __END__
« Image/ExifTool/WritePostScript.pl ↑ Summary Image/ExifTool/Jpeg2000.pm »