Image/ExifTool/WriteIPTC.pl

filter: Statementstmt 77.8% 278 / 357 Branchbran 67.5% 173 / 256 Conditioncond 62.0% 85 / 137 MC/DCmcdc 32.4% 25 / 77 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 10 totaltotal 67.2% 570 / 848 CC 184 SCAR 45.1
CC
184
Cov
71%
DoWriteIPTC
145.4
WriteIPTC
50.8
CheckIPTC
43.8
CRAP 90.6
« Image/ExifTool/Shift.pl ↑ Summary Image/ExifTool/Kandao.pm »
1 #------------------------------------------------------------------------------
2 # File: WriteIPTC.pl
3 #
4 # Description: Write IPTC meta information
5 #
6 # Revisions: 12/15/2004 - P. Harvey Created
7 #------------------------------------------------------------------------------
8  
9 package Image::ExifTool::IPTC;
10  
11 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # mandatory IPTC tags for each record
14 22 my %mandatory = (
15 1 => {
16 0 => 4, # EnvelopeRecordVersion
17 },
18 2 => {
19 0 => 4, # ApplicationRecordVersion
20 },
21 3 => {
22 0 => 4, # NewsPhotoVersion
23 },
24 );
25  
26 # manufacturer strings for IPTCPictureNumber
27 22 my %manufacturer = (
28 1 => 'Associated Press, USA',
29 2 => 'Eastman Kodak Co, USA',
30 3 => 'Hasselblad Electronic Imaging, Sweden',
31 4 => 'Tecnavia SA, Switzerland',
32 5 => 'Nikon Corporation, Japan',
33 6 => 'Coatsworth Communications Inc, Canada',
34 7 => 'Agence France Presse, France',
35 8 => 'T/One Inc, USA',
36 9 => 'Associated Newspapers, UK',
37 10 => 'Reuters London',
38 11 => 'Sandia Imaging Systems Inc, USA',
39 12 => 'Visualize, Spain',
40 );
41  
42 22 my %iptcCharsetInv = ( 'UTF8' => "\x1b%G", 'UTF-8' => "\x1b%G" );
43  
44 # ISO 2022 Character Coding Notes
45 # -------------------------------
46 # Character set designation: (0x1b I F, or 0x1b I I F)
47 # Initial character 0x1b (ESC)
48 # Intermediate character I:
49 # 0x28 ('(') - G0, 94 chars
50 # 0x29 (')') - G1, 94 chars
51 # 0x2a ('*') - G2, 94 chars
52 # 0x2b ('+') - G3, 94 chars
53 # 0x2c (',') - G1, 96 chars
54 # 0x2d ('-') - G2, 96 chars
55 # 0x2e ('.') - G3, 96 chars
56 # 0x24 I ('$I') - multiple byte graphic sets (I from above)
57 # I 0x20 ('I ') - dynamically redefinable character sets
58 # Final character:
59 # 0x30 - 0x3f = private character set
60 # 0x40 - 0x7f = standardized character set
61 # Character set invocation:
62 # G0 : SI = 0x15
63 # G1 : SO = 0x14, LS1R = 0x1b 0x7e ('~')
64 # G2 : LS2 = 0x1b 0x6e ('n'), LS2R = 0x1b 0x7d ('}')
65 # G3 : LS3 = 0x1b 0x6f ('o'), LS3R = 0x1b 0x7c ('|')
66 # (the locking shift "R" codes shift into 0x80-0xff space)
67 # Single character invocation:
68 # G2 : SS2 = 0x1b 0x8e (or 0x4e in 7-bit)
69 # G3 : SS3 = 0x1b 0x8f (or 0x4f in 7-bit)
70 # Control chars (designated and invoked)
71 # C0 : 0x1b 0x21 F (0x21 = '!')
72 # C1 : 0x1b 0x22 F (0x22 = '"')
73 # Complete codes (control+graphics, designated and invoked)
74 # 0x1b 0x25 F (0x25 = '%')
75 # 0x1b 0x25 I F
76 # 0x1b 0x25 0x47 ("\x1b%G") - UTF-8
77 # 0x1b 0x25 0x40 ("\x1b%@") - return to ISO 2022
78 # -------------------------------
79  
80 #------------------------------------------------------------------------------
81 # Inverse print conversion for CodedCharacterSet
82 # Inputs: 0) value
83 sub PrintInvCodedCharset($)
84 2 ▶{
Subroutine PrintInvCodedCharset: called CC=3 SCAR=15.7
Pod undocumented
85 2 my $val = shift;
86 2 my $code = $iptcCharsetInv{uc($val)};
87 2 ▶ unless ($code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 0 2 2
88 0 ▶ if (($code = $val) =~ s/ESC */\x1b/ig) { # translate ESC chars
Branch: 1 branch
BranchTrueFalseTotal
if (($code = $val) =~ s/ESC */\e/gi) { } 0 0 0
89 0 $code =~ s/, \x1b/\x1b/g; # remove comma separators
90 0 $code =~ tr/ //d; # remove spaces
91 } else {
92 0 warn "Bad syntax (use 'UTF8' or 'ESC X Y[, ...]')\n";
93 }
94 }
95 2 return $code;
96 }
97  
98 #------------------------------------------------------------------------------
99 # validate raw values for writing
100 # Inputs: 0) ExifTool object ref, 1) tagInfo hash ref, 2) raw value ref
101 # Returns: error string or undef (and possibly changes value) on success
102 sub CheckIPTC($$$)
103 303 ▶{
Subroutine CheckIPTC: called CC=24 SCAR=37.8
Pod undocumented
104 303 my ($et, $tagInfo, $valPtr) = @_;
105 303 ▶ my $format = $$tagInfo{Format} || $$tagInfo{Table}{FORMAT} || '';
Condition60%3/5
$tagInfo->{'Format'} || $tagInfo->{'Table'}{'FORMAT'} || ''
l!l
3030
$tagInfo->{"Format"} or $tagInfo->{"Table"}{"FORMAT"}
l!l&&r!l&&!r
271320
MC/DC0%0/2
$tagInfo->{'Format'} || $tagInfo->{'Table'}{'FORMAT'} || ''
$tagInfo->{"Format"} $tagInfo->{"Table"}{"FORMAT"}
Truth table66%2/3
A or B
A= $tagInfo->{"Format"}B= $tagInfo->{"Table"}{"FORMAT"}
ABresult
1X1
011
000
106 303 ▶ if ($format =~ /^int(\d+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format =~ /^int(\d+)/) { } 45 258 303
elsif ($format =~ /^(string|digits|undef)\[?(\d+),?(\d*)\]?$/) { } 258 0 258
107 45 ▶ my $bytes = int(($1 || 0) / 8);
Condition50%1/2
$1 || 0
l!l
450
MC/DC0%0/1
$1 || 0
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
108 45 ▶ if ($bytes != 1 and $bytes != 2 and $bytes != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes != 1 and $bytes != 2 and $bytes != 4) 0 45 45
Condition66%4/6
$bytes != 1 and $bytes != 2 and $bytes != 4
!ll&&!rl&&r
13320
$bytes != 1 and $bytes != 2
!ll&&!rl&&r
01332
MC/DC0%0/3
$bytes != 1 and $bytes != 2 and $bytes != 4
$bytes != 1 $bytes != 2 $bytes != 4
Truth table50%2/4
A and B and C
A= $bytes != 1B= $bytes != 2C= $bytes != 4
ABCresult
0XX0
10X0
1100
1111
109 0 return "Can't write $bytes-byte integer";
110 }
111 45 my $val = $$valPtr;
112 45 ▶ unless (Image::ExifTool::IsInt($val)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Image::ExifTool::IsInt($val)) 4 41 45
113 4 ▶ return 'Not an integer' unless Image::ExifTool::IsHex($val);
Branch: 1 branch
BranchTrueFalseTotal
unless &Image::ExifTool::IsHex($val) 4 0 4
114 0 $val = $$valPtr = hex($val);
115 }
116 41 my $n;
117 41 ▶ for ($n=0; $n<$bytes; ++$n) { $val >>= 8; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $bytes) 138 41 179
118 41 ▶ return "Value too large for $bytes-byte format" if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 41 41
119 } elsif ($format =~ /^(string|digits|undef)\[?(\d+),?(\d*)\]?$/) {
120 258 my ($fmt, $minlen, $maxlen) = ($1, $2, $3);
121 258 my $len = length $$valPtr;
122 258 ▶ if ($fmt eq 'digits') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt eq "digits") 25 233 258
123 25 ▶ return 'Non-numeric characters in value' unless $$valPtr =~ /^\d*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $$valPtr =~ /^\d*$/ 0 25 25
124 25 ▶ if ($len < $minlen and $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $minlen and $len) 3 22 25
Condition66%2/3
$len < $minlen and $len
!ll&&!rl&&r
2203
MC/DC50%1/2
$len < $minlen and $len
$len < $minlen $len
Truth table66%2/3
A and B
A= $len < $minlenB= $len
ABresult
0X0
100
111
125 # left pad with zeros if necessary
126 3 $$valPtr = ('0' x ($minlen - $len)) . $$valPtr;
127 3 $len = $minlen;
128 }
129 }
130 258 ▶ if (defined $minlen and $fmt ne 'string') { # (must truncate strings later, after recoding)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $minlen and $fmt ne "string") 25 233 258
Condition66%2/3
defined $minlen and $fmt ne "string"
!ll&&!rl&&r
023325
MC/DC50%1/2
defined $minlen and $fmt ne "string"
defined $minlen $fmt ne "string"
Truth table66%2/3
A and B
A= defined $minlenB= $fmt ne "string"
ABresult
0X0
100
111
131 25 ▶ $maxlen or $maxlen = $minlen;
Branch: 1 branch
BranchTrueFalseTotal
$maxlen or $maxlen = $minlen 0 25 25
132 25 ▶ if ($len < $minlen) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len < $minlen) { } 0 25 25
elsif ($len > $maxlen and not $et->{'OPTIONS'}{'IgnoreMinorErrors'}) { } 0 50 50
133 0 ▶ unless ($$et{OPTIONS}{IgnoreMinorErrors}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($et->{"OPTIONS"}{"IgnoreMinorErrors"}) 0 0 0
134 0 return "[Minor] String too short (minlen is $minlen)";
135 }
136 0 $$et{CHECK_WARN} = "String too short for IPTC:$$tagInfo{Name} (written anyway)";
137 ▶ } elsif ($len > $maxlen and not $$et{OPTIONS}{IgnoreMinorErrors}) {
Condition33%1/3
$len > $maxlen and not $et->{"OPTIONS"}{"IgnoreMinorErrors"}
!ll&&!rl&&r
2500
MC/DC0%0/2
$len > $maxlen and not $et->{"OPTIONS"}{"IgnoreMinorErrors"}
$len > $maxlen not $et->{"OPTIONS"}{"IgnoreMinorErrors"}
Truth table33%1/3
A and not(B)
A= $len > $maxlenB= not $et->{"OPTIONS"}{"IgnoreMinorErrors"}
ABresult
0X0
100
111
138 0 $$et{CHECK_WARN} = "[Minor] IPTC:$$tagInfo{Name} exceeds length limit (truncated)";
139 0 $$valPtr = substr($$valPtr, 0, $maxlen);
140 }
141 }
142 } else {
143 0 return "Bad IPTC Format ($format)";
144 }
145 299 return undef;
146 }
147  
148 #------------------------------------------------------------------------------
149 # format IPTC data for writing
150 # Inputs: 0) ExifTool object ref, 1) tagInfo pointer,
151 # 2) value reference (changed if necessary),
152 # 3) reference to character set for translation (changed if necessary)
153 # 4) record number, 5) flag set to read value (instead of write)
154 sub FormatIPTC($$$$$;$)
155 324 ▶{
Subroutine FormatIPTC: called CC=25 SCAR=33.7
Pod undocumented
156 324 my ($et, $tagInfo, $valPtr, $xlatPtr, $rec, $read) = @_;
157 324 ▶ my $format = $$tagInfo{Format} || $$tagInfo{Table}{FORMAT};
Condition66%2/3
$tagInfo->{'Format'} || $tagInfo->{'Table'}{'FORMAT'}
l!l&&r!l&&!r
32040
MC/DC0%0/2
$tagInfo->{'Format'} || $tagInfo->{'Table'}{'FORMAT'}
$tagInfo->{'Format'} $tagInfo->{'Table'}{'FORMAT'}
Truth table66%2/3
A || B
A= $tagInfo->{'Format'}B= $tagInfo->{'Table'}{'FORMAT'}
ABresult
1X1
011
000
158 324 ▶ return unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 0 324 324
159 324 ▶ if ($format =~ /^int(\d+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format =~ /^int(\d+)/) { } 43 281 324
elsif ($format =~ /^string/) { } 260 21 281
160 43 ▶ if ($read) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read) { } 6 37 43
161 6 my $len = length($$valPtr);
162 6 ▶ if ($len <= 8) { # limit integer conversion to 8 bytes long
Branch: 1 branch
BranchTrueFalseTotal
if ($len <= 8) 6 0 6
163 6 my $val = 0;
164 6 my $i;
165 6 ▶ for ($i=0; $i<$len; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 16 6 22
166 16 $val = $val * 256 + ord(substr($$valPtr, $i, 1));
167 }
168 6 $$valPtr = $val;
169 }
170 } else {
171 37 ▶ my $len = int(($1 || 0) / 8);
Condition50%1/2
$1 || 0
l!l
370
MC/DC0%0/1
$1 || 0
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
172 37 ▶ if ($len == 1) { # 1 byte
Branch: 2 branches
BranchTrueFalseTotal
if ($len == 1) { } 0 37 37
elsif ($len == 2) { } 35 2 37
173 0 $$valPtr = chr($$valPtr & 0xff);
174 } elsif ($len == 2) { # 2-byte integer
175 35 $$valPtr = pack('n', $$valPtr);
176 } else { # 4-byte integer
177 2 $$valPtr = pack('N', $$valPtr);
178 }
179 }
180 } elsif ($format =~ /^string/) {
181 260 ▶ if ($rec == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rec == 1) { } 2 258 260
elsif ($$xlatPtr and $rec < 7 and $$valPtr =~ /[\x80-\xff]/) { } 2 301 303
182 2 ▶ if ($$tagInfo{Name} eq 'CodedCharacterSet') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo->{"Name"} eq "CodedCharacterSet") 2 0 2
183 2 $$xlatPtr = HandleCodedCharset($et, $$valPtr);
184 }
185 ▶ } elsif ($$xlatPtr and $rec < 7 and $$valPtr =~ /[\x80-\xff]/) {
Condition83%5/6
$$xlatPtr and $rec < 7 and $$valPtr =~ /[\x80-\xff]/
!ll&&!rl&&r
452112
$$xlatPtr and $rec < 7
!ll&&!rl&&r
450213
MC/DC66%2/3
$$xlatPtr and $rec < 7 and $$valPtr =~ /[\x80-\xff]/
$$xlatPtr $rec < 7 $$valPtr =~ /[\x80-\xff]/
Truth table75%3/4
A and B and C
A= $$xlatPtrB= $rec < 7C= $$valPtr =~ /[\x80-\xff]/
ABCresult
0XX0
10X0
1100
1111
186 2 TranslateCodedString($et, $valPtr, $xlatPtr, $read);
187 }
188 # must check length now (after any string recoding)
189 260 ▶ if (not $read and $format =~ /^string\[(\d+),?(\d*)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $read and $format =~ /^string\[(\d+),?(\d*)\]$/) 173 87 260
Condition66%2/3
not $read and $format =~ /^string\[(\d+),?(\d*)\]$/
!ll&&!rl&&r
870173
MC/DC50%1/2
not $read and $format =~ /^string\[(\d+),?(\d*)\]$/
not $read $format =~ /^string\[(\d+),?(\d*)\]$/
Truth table66%2/3
not(A) and B
A= not $readB= $format =~ /^string\[(\d+),?(\d*)\]$/
ABresult
0X0
100
111
190 173 my ($minlen, $maxlen) = ($1, $2);
191 173 my $len = length $$valPtr;
192 173 ▶ $maxlen or $maxlen = $minlen;
Branch: 1 branch
BranchTrueFalseTotal
$maxlen or $maxlen = $minlen 170 3 173
193 173 ▶ if ($len < $minlen) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len < $minlen) { } 0 173 173
elsif ($len > $maxlen) { } 2 171 173
194 0 ▶ if ($et->Warn("String too short for IPTC:$$tagInfo{Name} (padded)", 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->Warn(qq[String too short for IPTC:$tagInfo->{"Name"} (padded)], 2)) 0 0 0
195 0 $$valPtr .= ' ' x ($minlen - $len);
196 }
197 } elsif ($len > $maxlen) {
198 2 ▶ if ($et->Warn("IPTC:$$tagInfo{Name} exceeds length limit (truncated)", 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->Warn(qq[IPTC:$tagInfo->{"Name"} exceeds length limit (truncated)], 2)) 2 0 2
199 2 $$valPtr = substr($$valPtr, 0, $maxlen);
200 # make sure UTF-8 is still valid
201 2 ▶ if (($$xlatPtr || $et->Options('Charset')) eq 'UTF8') {
Branch: 1 branch
BranchTrueFalseTotal
if (($$xlatPtr || $et->Options("Charset")) eq "UTF8") 1 1 2
Condition66%2/3
$$xlatPtr || $et->Options('Charset')
l!l&&r!l&&!r
110
MC/DC0%0/2
$$xlatPtr || $et->Options('Charset')
$$xlatPtr $et->Options('Charset')
Truth table66%2/3
A || B
A= $$xlatPtrB= $et->Options('Charset')
ABresult
1X1
011
000
202 1 require Image::ExifTool::XMP;
203 1 Image::ExifTool::XMP::FixUTF8($valPtr,'.');
204 }
205 }
206 }
207 }
208 }
209 }
210  
211 #------------------------------------------------------------------------------
212 # generate IPTC-format date
213 # Inputs: 0) EXIF-format date string (YYYY:mm:dd) or date/time string
214 # Returns: IPTC-format date string (YYYYmmdd), or undef and issue warning on error
215 sub IptcDate($)
216 12 ▶{
Subroutine IptcDate: called CC=2 SCAR=8.4
Pod undocumented
217 12 my $val = shift;
218 12 ▶ unless ($val =~ s{^.*?(\d{4})[-:/.]?(\d{2})[-:/.]?(\d{2}).*}{$1$2$3}s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ s[^.*?(\d{4})[-:/.]?(\d{2})[-:/.]?(\d{2}).*][$1$2$3]s) 0 12 12
219 0 warn "Invalid date format (use YYYY:mm:dd)\n";
220 0 undef $val;
221 }
222 12 return $val;
223 }
224  
225 #------------------------------------------------------------------------------
226 # generate IPTC-format time
227 # Inputs: 0) EXIF-format time string (HH:MM:SS[+/-HH:MM]) or date/time string
228 # Returns: IPTC-format time string (HHMMSS+HHMM), or undef and issue warning on error
229 sub IptcTime($)
230 1 ▶{
Subroutine IptcTime: called CC=9 SCAR=36.3
Pod undocumented
231 1 my $val = shift;
232 1 ▶ if ($val =~ /(.*?)\b(\d{1,2})(:?)(\d{2})(:?)(\d{2})(\S*)\s*$/s and ($3 or not $5)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /(.*?)\b(\d{1,2})(:?)(\d{2})(:?)(\d{2})(\S*)\s*$/s and $3 || !$5) { } 1 0 1
Condition33%2/6
$val =~ /(.*?)\b(\d{1,2})(:?)(\d{2})(:?)(\d{2})(\S*)\s*$/s and $3 || !$5
!ll&&!rl&&r
001
$3 or not $5
l!l&&r!l&&!r
100
MC/DC0%0/3
$val =~ /(.*?)\b(\d{1,2})(:?)(\d{2})(:?)(\d{2})(\S*)\s*$/s and $3 || !$5
$val =~ /(.*?)\b(\d{1,2})(:?)(\d{2})(:?)(\d{2})(\S*)\s*$/s $3 not $5
Truth table25%1/4
A and B or not(C)
A= $val =~ /(.*?)\b(\d{1,2})(:?)(\d{2})(:?)(\d{2})(\S*)\s*$/sB= $3C= not $5
ABCresult
0XX0
1000
11X1
1011
233 1 $val = sprintf("%.2d%.2d%.2d",$2,$4,$6);
234 1 my ($date, $tz) = ($1, $7);
235 1 ▶ if ($tz =~ /([+-]\d{1,2}):?(\d{2})/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tz =~ /([+-]\d{1,2}):?(\d{2})/) { } 1 0 1
elsif ($tz =~ /Z/i) { } 0 0 0
236 1 $tz = sprintf("%+.2d%.2d",$1,$2);
237 } elsif ($tz =~ /Z/i) {
238 0 $tz = '+0000'; # UTC
239 } else {
240 # use local system timezone by default
241 0 my (@tm, $time);
242 0 ▶ if ($date and $date =~ /^(\d{4}):(\d{2}):(\d{2})\s*$/ and eval { require Time::Local }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date and $date =~ /^(\d{4}):(\d{2}):(\d{2})\s*$/ and eval { do { require Time::Local } }) { } 0 0 0
Condition0%0/6
$date and $date =~ /^(\d{4}):(\d{2}):(\d{2})\s*$/ and eval { do { require Time::Local } }
!ll&&!rl&&r
000
$date and $date =~ /^(\d{4}):(\d{2}):(\d{2})\s*$/
!ll&&!rl&&r
000
MC/DC0%0/3
$date and $date =~ /^(\d{4}):(\d{2}):(\d{2})\s*$/ and eval { do { require Time::Local } }
$date $date =~ /^(\d{4}):(\d{2}):(\d{2})\s*$/ eval { do { require Time::Local } }
Truth table0%0/4
A and B and C
A= $dateB= $date =~ /^(\d{4}):(\d{2}):(\d{2})\s*$/C= eval { do { require Time::Local } }
ABCresult
0XX0
10X0
1100
1111
243 # we were given a date too, so determine the local timezone
244 # offset at the specified date/time
245 0 my @d = ($3,$2-1,$1);
246 0 $val =~ /(\d{2})(\d{2})(\d{2})/;
247 0 @tm = ($3,$2,$1,@d);
248 0 $time = Image::ExifTool::TimeLocal(@tm);
249 } else {
250 # it is difficult to get the proper local timezone offset for this
251 # time because the date tag is written separately. (The offset may be
252 # different on a different date due to daylight savings time.) In this
253 # case the best we can do easily is to use the current timezone offset.
254 0 $time = time;
255 0 @tm = localtime($time);
256 }
257 0 ($tz = Image::ExifTool::TimeZoneString(\@tm, $time)) =~ tr/://d;
258 }
259 1 $val .= $tz;
260 } else {
261 0 warn "Invalid time format (use HH:MM:SS[+/-HH:MM])\n";
262 0 undef $val; # time format error
263 }
264 1 return $val;
265 }
266  
267 #------------------------------------------------------------------------------
268 # Inverse print conversion for IPTC date or time value
269 # Inputs: 0) ExifTool ref, 1) IPTC date or 'now'
270 # Returns: IPTC date
271 sub InverseDateOrTime($$)
272 11 ▶{
Subroutine InverseDateOrTime: called CC=2 SCAR=7.1
Pod undocumented
273 11 my ($et, $val) = @_;
274 11 ▶ return $et->TimeNow() if lc($val) eq 'now';
Branch: 1 branch
BranchTrueFalseTotal
if lc $val eq "now" 0 11 11
275 11 return $val;
276 }
277  
278 #------------------------------------------------------------------------------
279 # Convert picture number
280 # Inputs: 0) value
281 # Returns: Converted value
282 sub ConvertPictureNumber($)
283 0 ▶{
Subroutine ConvertPictureNumber: not called CC=4 SCAR=30.0
Pod undocumented
284 0 my $val = shift;
285 0 ▶ if ($val eq "\0" x 16) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val eq "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000") { } 0 0 0
elsif (length $val >= 16) { } 0 0 0
286 0 $val = 'Unknown';
287 } elsif (length $val >= 16) {
288 0 my @vals = unpack('nNA8n', $val);
289 0 $val = $vals[0];
290 0 my $manu = $manufacturer{$val};
291 0 ▶ $val .= " ($manu)" if $manu;
Branch: 1 branch
BranchTrueFalseTotal
if $manu 0 0 0
292 0 $val .= ', equip ' . $vals[1];
293 0 $vals[2] =~ s/(\d{4})(\d{2})(\d{2})/$1:$2:$3/;
294 0 $val .= ", $vals[2], no. $vals[3]";
295 } else {
296 0 $val = '<format error>'
297 }
298 0 return $val;
299 }
300  
301 #------------------------------------------------------------------------------
302 # Inverse picture number conversion
303 # Inputs: 0) value
304 # Returns: Converted value (or undef on error)
305 sub InvConvertPictureNumber($)
306 0 ▶{
Subroutine InvConvertPictureNumber: not called CC=3 SCAR=24.8
Pod undocumented
307 0 my $val = shift;
308 0 $val =~ s/\(.*\)//g; # remove manufacturer description
309 0 $val =~ tr/://d; # remove date separators
310 0 $val =~ tr/0-9/ /c; # turn remaining non-numbers to spaces
311 0 my @vals = split ' ', $val;
312 0 ▶ if (@vals >= 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (@vals >= 4) { } 0 0 0
elsif ($val =~ /unknown/i) { } 0 0 0
313 0 $val = pack('nNA8n', @vals);
314 } elsif ($val =~ /unknown/i) {
315 0 $val = "\0" x 16;
316 } else {
317 0 undef $val;
318 }
319 0 return $val;
320 }
321  
322 #------------------------------------------------------------------------------
323 # Write IPTC data record
324 # Inputs: 0) ExifTool object ref, 1) source dirInfo ref, 2) tag table ref
325 # Returns: IPTC data block (may be empty if no IPTC data)
326 # Notes: Increments ExifTool CHANGED flag for each tag changed
327 sub DoWriteIPTC($$$)
328 67 ▶{
Subroutine DoWriteIPTC: called CC=97 SCAR=49.8
Pod undocumented
329 67 my ($et, $dirInfo, $tagTablePtr) = @_;
330 67 my $verbose = $et->Options('Verbose');
331 67 my $out = $et->Options('TextOut');
332  
333 # avoid editing IPTC directory unless necessary:
334 # - improves speed
335 # - avoids changing current MD5 digest unnecessarily
336 # - avoids adding mandatory tags unless some other IPTC is changed
337 return undef unless exists $$et{EDIT_DIRS}{$$dirInfo{DirName}} or
338 # standard IPTC tags in other locations should be edited too (eg. AFCP_IPTC)
339 67 ▶ ($tagTablePtr eq \%Image::ExifTool::IPTC::Main and exists $$et{EDIT_DIRS}{IPTC});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $et->{"EDIT_DIRS"}{$dirInfo->{"DirName"}} or $tagTablePtr eq \%Image::ExifTool::IPTC::Main and exists $et->{"EDIT_DIRS"}{"IPTC"} 25 42 67
Condition66%4/6
exists $et->{"EDIT_DIRS"}{$dirInfo->{"DirName"}} or $tagTablePtr eq \%Image::ExifTool::IPTC::Main and exists $et->{"EDIT_DIRS"}{"IPTC"}
l!l&&r!l&&!r
42025
$tagTablePtr eq \%Image::ExifTool::IPTC::Main and exists $et->{"EDIT_DIRS"}{"IPTC"}
!ll&&!rl&&r
7180
MC/DC33%1/3
exists $et->{"EDIT_DIRS"}{$dirInfo->{"DirName"}} or $tagTablePtr eq \%Image::ExifTool::IPTC::Main and exists $et->{"EDIT_DIRS"}{"IPTC"}
exists $et->{"EDIT_DIRS"}{$dirInfo->{"DirName"}} $tagTablePtr eq \%Image::ExifTool::IPTC::Main exists $et->{"EDIT_DIRS"}{"IPTC"}
Truth table75%3/4
A or B and C
A= exists $et->{"EDIT_DIRS"}{$dirInfo->{"DirName"}}B= $tagTablePtr eq \%Image::ExifTool::IPTC::MainC= exists $et->{"EDIT_DIRS"}{"IPTC"}
ABCresult
1XX1
0111
00X0
0100
340 42 my $dataPt = $$dirInfo{DataPt};
341 42 ▶ unless ($dataPt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dataPt) 24 18 42
342 24 my $emptyData = '';
343 24 $dataPt = \$emptyData;
344 }
345 42 ▶ my $start = $$dirInfo{DirStart} || 0;
Condition100%2/2
$dirInfo->{'DirStart'} || 0
l!l
1032
MC/DC100%1/1
$dirInfo->{'DirStart'} || 0
$dirInfo->{'DirStart'}
Truth table100%2/2
A
A= $dirInfo->{'DirStart'}
Aresult
11
00
346 42 my $dirLen = $$dirInfo{DirLen};
347 42 my ($tagInfo, %iptcInfo, $tag);
348  
349 # start by assuming default IPTC encoding
350 42 my $xlat = $et->Options('CharsetIPTC');
351 42 ▶ undef $xlat if $xlat eq $et->Options('Charset');
Branch: 1 branch
BranchTrueFalseTotal
if $xlat eq $et->Options("Charset") 1 41 42
352  
353 # make sure our dataLen is defined (note: allow zero length directory)
354 42 ▶ unless (defined $dirLen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dirLen) 24 18 42
355 24 my $dataLen = $$dirInfo{DataLen};
356 24 ▶ $dataLen = length($$dataPt) unless defined $dataLen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dataLen 24 0 24
357 24 $dirLen = $dataLen - $start;
358 }
359 # quick check for improperly byte-swapped IPTC
360 42 ▶ if ($dirLen >= 4 and substr($$dataPt, $start, 1) ne "\x1c" and
Branch: 1 branch
BranchTrueFalseTotal
if ($dirLen >= 4 and substr($$dataPt, $start, 1) ne "\34" and substr($$dataPt, $start + 3, 1) eq "\34") 0 42 42
Condition50%3/6
$dirLen >= 4 and substr($$dataPt, $start, 1) ne "\34" and substr($$dataPt, $start + 3, 1) eq "\34"
!ll&&!rl&&r
4200
$dirLen >= 4 and substr($$dataPt, $start, 1) ne "\34"
!ll&&!rl&&r
25170
MC/DC0%0/3
$dirLen >= 4 and substr($$dataPt, $start, 1) ne "\34" and substr($$dataPt, $start + 3, 1) eq "\34"
$dirLen >= 4 substr($$dataPt, $start, 1) ne "\34" substr($$dataPt, $start + 3, 1) eq "\34"
Truth table50%2/4
A and B and C
A= $dirLen >= 4B= substr($$dataPt, $start, 1) ne "\34"C= substr($$dataPt, $start + 3, 1) eq "\34"
ABCresult
0XX0
10X0
1100
1111
361 substr($$dataPt, $start + 3, 1) eq "\x1c")
362 {
363 0 $et->Warn('IPTC data was improperly byte-swapped');
364 0 my $newData = pack('N*', unpack('V*', substr($$dataPt, $start, $dirLen) . "\0\0\0"));
365 0 $dataPt = \$newData;
366 0 $start = 0;
367 # NOTE: MUST NOT access $dirInfo DataPt, DirStart or DataLen after this!
368 }
369 # generate lookup so we can find the record numbers
370 42 my %recordNum;
371 42 foreach $tag (Image::ExifTool::TagTableKeys($tagTablePtr)) {
372 282 $tagInfo = $$tagTablePtr{$tag};
373 282 ▶ $$tagInfo{SubDirectory} or next;
Branch: 1 branch
BranchTrueFalseTotal
$tagInfo->{"SubDirectory"} or next 282 0 282
374 282 ▶ my $table = $$tagInfo{SubDirectory}{TagTable} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $table = $tagInfo->{"SubDirectory"}{"TagTable"} or next 282 0 282
375 282 my $subTablePtr = Image::ExifTool::GetTagTable($table);
376 282 $recordNum{$subTablePtr} = $tag;
377 }
378  
379 # loop through new values and accumulate all IPTC information
380 # into lists based on their IPTC record type
381 42 foreach $tagInfo ($et->GetNewTagInfoList()) {
382 2378 my $table = $$tagInfo{Table};
383 2378 my $record = $recordNum{$table};
384 # ignore tags we aren't writing to this directory
385 2378 ▶ next unless defined $record;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record 2200 178 2378
386 178 ▶ $iptcInfo{$record} = [] unless defined $iptcInfo{$record};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iptcInfo{$record} 46 132 178
387 178 push @{$iptcInfo{$record}}, $tagInfo;
388 }
389  
390 # get sorted list of records used. Might as well be organized and
391 # write our records in order of record number first, then tag number
392 42 my @recordList = sort { $a <=> $b } keys %iptcInfo;
393 42 my ($record, %set);
394 42 foreach $record (@recordList) {
395 # sort tagInfo lists by tagID
396 46 @{$iptcInfo{$record}} = sort { $$a{TagID} <=> $$b{TagID} } @{$iptcInfo{$record}};
397 # build hash of all tagIDs to set
398 46 foreach $tagInfo (@{$iptcInfo{$record}}) {
399 178 $set{$record}->{$$tagInfo{TagID}} = $tagInfo;
400 }
401 }
402 # run through the old IPTC data, inserting our records in
403 # sequence and deleting existing records where necessary
404 # (the IPTC specification states that records must occur in
405 # numerical order, but tags within records need not be ordered)
406 42 my $pos = $start;
407 42 my $tail = $pos; # old data written up to this point
408 42 my $dirEnd = $start + $dirLen;
409 42 my $newData = '';
410 42 my $lastRec = -1;
411 42 my $lastRecPos = 0;
412 42 my $allMandatory = 0;
413 42 my %foundRec; # found flags: 0x01-existed before, 0x02-deleted, 0x04-created
414 my $addNow;
415  
416 42 for (;;$tail=$pos) {
417 # get next IPTC record from input directory
418 289 my ($id, $rec, $tag, $len, $valuePtr);
419 289 ▶ if ($pos + 5 <= $dirEnd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + 5 <= $dirEnd) 248 41 289
420 248 my $buff = substr($$dataPt, $pos, 5);
421 248 ($id, $rec, $tag, $len) = unpack("CCCn", $buff);
422 248 ▶ if ($id == 0x1c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id == 28) { } 247 1 248
423 247 ▶ if ($rec < $lastRec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec < $lastRec) 0 247 247
424 0 ▶ if ($rec == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec == 0) { } 0 0 0
425 0 ▶ return undef if $et->Warn("IPTC record 0 encountered, subsequent records ignored", 2);
Branch: 1 branch
BranchTrueFalseTotal
if $et->Warn("IPTC record 0 encountered, subsequent records ignored", 2) 0 0 0
426 0 undef $rec;
427 0 $pos = $dirEnd;
428 0 $len = 0;
429 } else {
430 0 ▶ return undef if $et->Warn("IPTC doesn't conform to spec: Records out of sequence", 2);
Branch: 1 branch
BranchTrueFalseTotal
if $et->Warn("IPTC doesn't conform to spec: Records out of sequence", 2) 0 0 0
431 }
432 }
433 # handle extended IPTC entry if necessary
434 247 $pos += 5; # step to after field header
435 247 ▶ if ($len & 0x8000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len & 32768) 0 247 247
436 0 my $n = $len & 0x7fff; # get num bytes in length field
437 0 ▶ if ($pos + $n <= $dirEnd and $n <= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + $n <= $dirEnd and $n <= 8) { } 0 0 0
Condition0%0/3
$pos + $n <= $dirEnd and $n <= 8
!ll&&!rl&&r
000
MC/DC0%0/2
$pos + $n <= $dirEnd and $n <= 8
$pos + $n <= $dirEnd $n <= 8
Truth table0%0/3
A and B
A= $pos + $n <= $dirEndB= $n <= 8
ABresult
0X0
100
111
438 # determine length (a big-endian, variable sized int)
439 0 ▶ for ($len = 0; $n; ++$pos, --$n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 0 0 0
440 0 $len = $len * 256 + ord(substr($$dataPt, $pos, 1));
441 }
442 } else {
443 0 $len = $dirEnd; # invalid length
444 }
445 }
446 247 $valuePtr = $pos;
447 247 $pos += $len; # step $pos to next entry
448 # make sure we don't go past the end of data
449 # (this can only happen if original data is bad)
450 247 ▶ $pos = $dirEnd if $pos > $dirEnd;
Branch: 1 branch
BranchTrueFalseTotal
if $pos > $dirEnd 0 247 247
451 } else {
452 1 undef $rec;
453 }
454 }
455 # write out all our records that come before this one
456 289 ▶ my $writeRec = (not defined $rec or $rec != $lastRec);
Condition100%3/3
!defined($rec) || $rec != $lastRec
l!l&&r!l&&!r
4218229
MC/DC100%2/2
!defined($rec) || $rec != $lastRec
!defined($rec) $rec != $lastRec
Truth table100%3/3
not(A) || B
A= !defined($rec)B= $rec != $lastRec
ABresult
1X1
011
000
457 289 ▶ if ($writeRec or $addNow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writeRec or $addNow) 129 160 289
Condition100%3/3
$writeRec or $addNow
l!l&&r!l&&!r
6069160
MC/DC100%2/2
$writeRec or $addNow
$writeRec $addNow
Truth table100%3/3
A or B
A= $writeRecB= $addNow
ABresult
1X1
011
000
458 129 for (;;) {
459 310 my $newRec = $recordList[0];
460 310 ▶ if ($addNow) {
Branch: 2 branches
BranchTrueFalseTotal
if ($addNow) { } 72 238 310
elsif (not defined $newRec or $newRec != $lastRec) { } 88 150 238
461 72 $tagInfo = $addNow;
462 ▶ } elsif (not defined $newRec or $newRec != $lastRec) {
Condition100%3/3
not defined $newRec or $newRec != $lastRec
l!l&&r!l&&!r
4246150
MC/DC100%2/2
not defined $newRec or $newRec != $lastRec
not defined $newRec $newRec != $lastRec
Truth table100%3/3
not(A) or B
A= not defined $newRecB= $newRec != $lastRec
ABresult
1X1
011
000
463 # handle mandatory tags in last record unless it was empty
464 88 ▶ if (length $newData > $lastRecPos) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $newData > $lastRecPos) 46 42 88
465 46 ▶ if ($allMandatory > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($allMandatory > 1) { } 3 43 46
elsif ($mandatory{$lastRec} and $tagTablePtr eq \%Image::ExifTool::IPTC::Main) { } 41 2 43
466 # entire lastRec contained mandatory tags, and at least one tag
467 # was deleted, so delete entire record unless we specifically
468 # added a mandatory tag
469 3 my $num = 0;
470 3 foreach (keys %{$foundRec{$lastRec}}) {
471 6 my $code = $foundRec{$lastRec}->{$_};
472 6 ▶ $num = 0, last if $code & 0x04;
Branch: 1 branch
BranchTrueFalseTotal
if $code & 4 0 6 6
473 6 ▶ ++$num if ($code & 0x03) == 0x01;
Branch: 1 branch
BranchTrueFalseTotal
if ($code & 3) == 1 3 3 6
474 }
475 3 ▶ if ($num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num) 3 0 3
476 3 $newData = substr($newData, 0, $lastRecPos);
477 3 ▶ $verbose > 1 and print $out " - $num mandatory tags\n";
Branch: 1 branch
BranchTrueFalseTotal
$verbose > 1 and print $out " - $num mandatory tags\n" 0 3 3
478 }
479 ▶ } elsif ($mandatory{$lastRec} and
Condition66%2/3
$mandatory{$lastRec} and $tagTablePtr eq \%Image::ExifTool::IPTC::Main
!ll&&!rl&&r
0241
MC/DC50%1/2
$mandatory{$lastRec} and $tagTablePtr eq \%Image::ExifTool::IPTC::Main
$mandatory{$lastRec} $tagTablePtr eq \%Image::ExifTool::IPTC::Main
Truth table66%2/3
A and B
A= $mandatory{$lastRec}B= $tagTablePtr eq \%Image::ExifTool::IPTC::Main
ABresult
0X0
100
111
480 $tagTablePtr eq \%Image::ExifTool::IPTC::Main)
481 {
482 # add required mandatory tags
483 41 my $mandatory = $mandatory{$lastRec};
484 41 my ($mandTag, $subTablePtr);
485 0 foreach $mandTag (sort { $a <=> $b } keys %$mandatory) {
486 41 ▶ next if $foundRec{$lastRec}->{$mandTag};
Branch: 1 branch
BranchTrueFalseTotal
if $foundRec{$lastRec}{$mandTag} 17 24 41
487 24 ▶ unless ($subTablePtr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subTablePtr) 24 0 24
488 24 $tagInfo = $$tagTablePtr{$lastRec};
489 24 ▶ $tagInfo and $$tagInfo{SubDirectory} or warn("WriteIPTC: Internal error 1\n"), next;
Branch: 1 branch
BranchTrueFalseTotal
$tagInfo and $tagInfo->{"SubDirectory"} or warn("WriteIPTC: Internal error 1\n"), next 24 0 24
Condition33%1/3
$tagInfo and $tagInfo->{"SubDirectory"}
!ll&&!rl&&r
0024
MC/DC0%0/2
$tagInfo and $tagInfo->{"SubDirectory"}
$tagInfo $tagInfo->{"SubDirectory"}
Truth table33%1/3
A and B
A= $tagInfoB= $tagInfo->{"SubDirectory"}
ABresult
0X0
100
111
490 24 ▶ $$tagInfo{SubDirectory}{TagTable} or next;
Branch: 1 branch
BranchTrueFalseTotal
$tagInfo->{"SubDirectory"}{"TagTable"} or next 24 0 24
491 24 $subTablePtr = Image::ExifTool::GetTagTable($$tagInfo{SubDirectory}{TagTable});
492 }
493 24 ▶ $tagInfo = $$subTablePtr{$mandTag} or warn("WriteIPTC: Internal error 2\n"), next;
Branch: 1 branch
BranchTrueFalseTotal
$tagInfo = $subTablePtr->{$mandTag} or warn("WriteIPTC: Internal error 2\n"), next 24 0 24
494 24 my $value = $$mandatory{$mandTag};
495 24 $et->VerboseValue("+ IPTC:$$tagInfo{Name}", $value, ' (mandatory)');
496 # apply necessary format conversions
497 24 FormatIPTC($et, $tagInfo, \$value, \$xlat, $lastRec);
498 24 $len = length $value;
499 # generate our new entry
500 24 my $entry = pack("CCCn", 0x1c, $lastRec, $mandTag, length($value));
501 24 $newData .= $entry . $value; # add entry to new IPTC data
502 # (don't mark as changed if just mandatory tags changed)
503 # ++$$et{CHANGED};
504 }
505 }
506 }
507 88 ▶ last unless defined $newRec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newRec 42 46 88
508 46 $lastRec = $newRec;
509 46 $lastRecPos = length $newData;
510 46 $allMandatory = 1;
511 }
512 268 ▶ unless ($addNow) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($addNow) 196 72 268
513 # compare current entry with entry next in line to write out
514 # (write out our tags in numerical order even though
515 # this isn't required by the IPTC spec)
516 196 ▶ last if defined $rec and $rec <= $newRec;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rec and $rec <= $newRec 18 178 196
Condition100%3/3
defined $rec and $rec <= $newRec
!ll&&!rl&&r
176218
MC/DC100%2/2
defined $rec and $rec <= $newRec
defined $rec $rec <= $newRec
Truth table100%3/3
A and B
A= defined $recB= $rec <= $newRec
ABresult
0X0
100
111
517 178 $tagInfo = ${$iptcInfo{$newRec}}[0];
518 }
519 250 my $newTag = $$tagInfo{TagID};
520 250 my $nvHash = $et->GetNewValueHash($tagInfo);
521 # only add new values if...
522 250 my ($doSet, @values);
523 250 ▶ my $found = $foundRec{$newRec}->{$newTag} || 0;
Condition100%2/2
$foundRec{$newRec}{$newTag} || 0
l!l
15199
MC/DC100%1/1
$foundRec{$newRec}{$newTag} || 0
$foundRec{$newRec}{$newTag}
Truth table100%2/2
A
A= $foundRec{$newRec}{$newTag}
Aresult
11
00
524 250 ▶ if ($found & 0x02) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found & 2) { } 148 102 250
elsif ($tagInfo->{'List'}) { } 32 70 102
525 # ...tag existed before and was deleted (unless we already added it)
526 148 ▶ $doSet = 1 unless $found & 0x04;
Branch: 1 branch
BranchTrueFalseTotal
unless $found & 4 76 72 148
527 } elsif ($$tagInfo{List}) {
528 # ...tag is List and it existed before or we are creating it
529 32 ▶ $doSet = 1 if $found ? not $$nvHash{CreateOnly} : $$nvHash{IsCreating};
Branch: 2 branches
BranchTrueFalseTotal
if $found ? !$nvHash->{"CreateOnly"} : $nvHash->{"IsCreating"} 30 2 32
$found ? : 2 30 32
530 } else {
531 # ...tag didn't exist before and we are creating it
532 70 ▶ $doSet = 1 if not $found and $$nvHash{IsCreating};
Branch: 1 branch
BranchTrueFalseTotal
if not $found and $nvHash->{"IsCreating"} 69 1 70
Condition66%2/3
not $found and $nvHash->{"IsCreating"}
!ll&&!rl&&r
1069
MC/DC50%1/2
not $found and $nvHash->{"IsCreating"}
not $found $nvHash->{"IsCreating"}
Truth table66%2/3
not(A) and B
A= not $foundB= $nvHash->{"IsCreating"}
ABresult
0X0
100
111
533 }
534 250 ▶ if ($doSet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doSet) 175 75 250
535 175 @values = $et->GetNewValue($nvHash);
536 175 ▶ @values and $foundRec{$newRec}->{$newTag} = $found | 0x04;
Branch: 1 branch
BranchTrueFalseTotal
@values and $foundRec{$newRec}{$newTag} = $found | 4 171 4 175
537 # write tags for each value in list
538 175 my $value;
539 175 foreach $value (@values) {
540 201 $et->VerboseValue("+ $$dirInfo{DirName}:$$tagInfo{Name}", $value);
541 # reset allMandatory flag if a non-mandatory tag is written
542 201 ▶ if ($allMandatory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allMandatory) 42 159 201
543 42 my $mandatory = $mandatory{$newRec};
544 42 ▶ $allMandatory = 0 unless $mandatory and $$mandatory{$newTag};
Branch: 1 branch
BranchTrueFalseTotal
unless $mandatory and $mandatory->{$newTag} 35 7 42
Condition66%2/3
$mandatory and $mandatory->{$newTag}
!ll&&!rl&&r
0357
MC/DC50%1/2
$mandatory and $mandatory->{$newTag}
$mandatory $mandatory->{$newTag}
Truth table66%2/3
A and B
A= $mandatoryB= $mandatory->{$newTag}
ABresult
0X0
100
111
545 }
546 # apply necessary format conversions
547 201 FormatIPTC($et, $tagInfo, \$value, \$xlat, $newRec);
548 # (note: IPTC string values are NOT null terminated)
549 201 $len = length $value;
550 # generate our new entry
551 201 my $entry = pack("CCC", 0x1c, $newRec, $newTag);
552 201 ▶ if ($len <= 0x7fff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len <= 32767) { } 201 0 201
553 201 $entry .= pack("n", $len);
554 } else {
555 # extended dataset tag
556 0 $entry .= pack("nN", 0x8004, $len);
557 }
558 201 $newData .= $entry . $value; # add entry to new IPTC data
559 201 ++$$et{CHANGED};
560 }
561 }
562 # continue on with regular programming if done adding tag now
563 250 ▶ if ($addNow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addNow) 72 178 250
564 72 undef $addNow;
565 72 ▶ next if $writeRec;
Branch: 1 branch
BranchTrueFalseTotal
if $writeRec 3 69 72
566 69 last;
567 }
568 # remove this tagID from the sorted write list
569 178 shift @{$iptcInfo{$newRec}};
570 178 ▶ shift @recordList unless @{$iptcInfo{$newRec}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$iptcInfo{$newRec};} 46 132 178
571 }
572 129 ▶ if ($writeRec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writeRec) 60 69 129
573 # all done if no more records to write
574 60 ▶ last unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 42 18 60
575 # update last record variables
576 18 $lastRec = $rec;
577 18 $lastRecPos = length $newData;
578 18 $allMandatory = 1;
579 }
580 }
581 # set flag indicating we found this tag
582 247 ▶ $foundRec{$rec}->{$tag} = ($foundRec{$rec}->{$tag} || 0) || 0x01;
Condition100%4/4
$foundRec{$rec}{$tag} || 0 || 1
l!l
24223
$foundRec{$rec}{$tag} or 0
l!l
24223
MC/DC100%1/1
$foundRec{$rec}{$tag} || 0 || 1
$foundRec{$rec}{$tag}
Truth table100%2/2
A
A= $foundRec{$rec}{$tag}
Aresult
11
00
583 # write out this record unless we are setting it with a new value
584 247 $tagInfo = $set{$rec}->{$tag};
585 247 ▶ if ($tagInfo) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tagInfo) { } 99 148 247
elsif ($rec == 1 and $tag == 90) { } 0 295 295
586 99 my $nvHash = $et->GetNewValueHash($tagInfo);
587 99 $len = $pos - $valuePtr;
588 99 my $val = substr($$dataPt, $valuePtr, $len);
589 # remove null terminator if it exists (written by braindead software like Picasa 2.0)
590 99 ▶ $val =~ s/\0+$// if $$tagInfo{Format} and $$tagInfo{Format} =~ /^string/;
Branch: 1 branch
BranchTrueFalseTotal
if $tagInfo->{"Format"} and $tagInfo->{"Format"} =~ /^string/ 87 12 99
Condition100%3/3
$tagInfo->{"Format"} and $tagInfo->{"Format"} =~ /^string/
!ll&&!rl&&r
21087
MC/DC100%2/2
$tagInfo->{"Format"} and $tagInfo->{"Format"} =~ /^string/
$tagInfo->{"Format"} $tagInfo->{"Format"} =~ /^string/
Truth table100%3/3
A and B
A= $tagInfo->{"Format"}B= $tagInfo->{"Format"} =~ /^string/
ABresult
0X0
100
111
591 99 my $oldXlat = $xlat;
592 99 FormatIPTC($et, $tagInfo, \$val, \$xlat, $rec, 1);
593 99 ▶ if ($et->IsOverwriting($nvHash, $val)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->IsOverwriting($nvHash, $val)) 89 10 99
594 89 $xlat = $oldXlat; # don't change translation (not writing this value)
595 89 $et->VerboseValue("- $$dirInfo{DirName}:$$tagInfo{Name}", $val);
596 89 ++$$et{CHANGED};
597 # set deleted flag to indicate we found and deleted this tag
598 89 $foundRec{$rec}->{$tag} |= 0x02;
599 # increment allMandatory flag to indicate a tag was removed
600 89 ▶ $allMandatory and ++$allMandatory;
Branch: 1 branch
BranchTrueFalseTotal
$allMandatory and ++$allMandatory 14 75 89
601 # write this tag now if overwriting an existing value
602 89 ▶ if ($$nvHash{Value} and @{$$nvHash{Value}} and @recordList and
Branch: 1 branch
BranchTrueFalseTotal
if ($nvHash->{"Value"} and @{$nvHash->{"Value"};} and @recordList and $recordList[0] == $rec and not $foundRec{$rec}{$tag} & 4) 72 17 89
Condition75%9/12
$nvHash->{"Value"} and @{$nvHash->{"Value"};} and @recordList and $recordList[0] == $rec and not $foundRec{$rec}{$tag} & 4
!ll&&!rl&&r
51272
$nvHash->{"Value"} and @{$nvHash->{"Value"};} and @recordList and $recordList[0] == $rec
!ll&&!rl&&r
5084
$nvHash->{"Value"} and @{$nvHash->{"Value"};} and @recordList
!ll&&!rl&&r
5084
$nvHash->{"Value"} and @{$nvHash->{"Value"};}
!ll&&!rl&&r
5084
MC/DC40%2/5
$nvHash->{"Value"} and @{$nvHash->{"Value"};} and @recordList and $recordList[0] == $rec and not $foundRec{$rec}{$tag} & 4
$nvHash->{"Value"} @{$nvHash->{"Value"};} @recordList $recordList[0] == $rec not $foundRec{$rec}{$tag} & 4
Truth table50%3/6
A and B and C and D and not(E)
A= $nvHash->{"Value"}B= @{$nvHash->{"Value"};}C= @recordListD= $recordList[0] == $recE= not $foundRec{$rec}{$tag} & 4
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
603 $recordList[0] == $rec and not $foundRec{$rec}->{$tag} & 0x04)
604 {
605 72 $addNow = $tagInfo;
606 }
607 89 next;
608 }
609 ▶ } elsif ($rec == 1 and $tag == 90) {
Condition66%2/3
$rec == 1 and $tag == 90
!ll&&!rl&&r
14710
MC/DC0%0/2
$rec == 1 and $tag == 90
$rec == 1 $tag == 90
Truth table66%2/3
A and B
A= $rec == 1B= $tag == 90
ABresult
0X0
100
111
610 # handle CodedCharacterSet tag
611 0 my $val = substr($$dataPt, $valuePtr, $pos - $valuePtr);
612 0 $xlat = HandleCodedCharset($et, $val);
613 }
614 # reset allMandatory flag if a non-mandatory tag is written
615 158 ▶ if ($allMandatory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allMandatory) 20 138 158
616 20 my $mandatory = $mandatory{$rec};
617 20 ▶ unless ($mandatory and $$mandatory{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mandatory and $mandatory->{$tag}) 8 12 20
Condition66%2/3
$mandatory and $mandatory->{$tag}
!ll&&!rl&&r
0812
MC/DC50%1/2
$mandatory and $mandatory->{$tag}
$mandatory $mandatory->{$tag}
Truth table66%2/3
A and B
A= $mandatoryB= $mandatory->{$tag}
ABresult
0X0
100
111
618 8 $allMandatory = 0;
619 }
620 }
621 # write out the record
622 158 $newData .= substr($$dataPt, $tail, $pos-$tail);
623 }
624 # make sure the rest of the data is zero
625 42 ▶ if ($tail < $dirEnd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail < $dirEnd) 4 38 42
626 4 my $pad = substr($$dataPt, $tail, $dirEnd-$tail);
627 4 ▶ if ($pad =~ /[^\0]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad =~ /[^\0]/) 0 4 4
628 0 ▶ return undef if $et->Warn('Unrecognized data in IPTC padding', 2);
Branch: 1 branch
BranchTrueFalseTotal
if $et->Warn("Unrecognized data in IPTC padding", 2) 0 0 0
629 }
630 }
631 42 return $newData;
632 }
633  
634 #------------------------------------------------------------------------------
635 # Write IPTC data record and calculate NewIPTCDigest
636 # Inputs: 0) ExifTool object ref, 1) source dirInfo ref, 2) tag table ref
637 # Returns: IPTC data block (may be empty if no IPTC data)
638 # Notes: Increments ExifTool CHANGED flag for each tag changed
639 sub WriteIPTC($$$)
640 445 ▶{
Subroutine WriteIPTC: called CC=24 SCAR=39.3
Pod undocumented
641 445 my ($et, $dirInfo, $tagTablePtr) = @_;
642 445 ▶ $et or return 1; # allow dummy access to autoload this package
Branch: 1 branch
BranchTrueFalseTotal
$et or return 1 67 378 445
643  
644 67 my $newData = DoWriteIPTC($et, $dirInfo, $tagTablePtr);
645  
646 # calculate standard IPTC digests only if we are writing or deleting
647 # Photoshop:IPTCDigest with a value of 'new' or 'old'
648 67 ▶ while ($Image::ExifTool::Photoshop::iptcDigestInfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Image::ExifTool::Photoshop::iptcDigestInfo) 63 4 67
649 63 my $nvHash = $$et{NEW_VALUE}{$Image::ExifTool::Photoshop::iptcDigestInfo};
650 63 ▶ last unless defined $nvHash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nvHash 62 1 63
651 1 ▶ last unless IsStandardIPTC($et->MetadataPath());
Branch: 1 branch
BranchTrueFalseTotal
unless &IsStandardIPTC(scalar $et->MetadataPath) 0 1 1
652 1 my @values = $et->GetNewValue($nvHash);
653 1 ▶ push @values, @{$$nvHash{DelValue}} if $$nvHash{DelValue};
Branch: 1 branch
BranchTrueFalseTotal
if $nvHash->{"DelValue"} 1 0 1
654 1 my $new = grep /^new$/, @values;
655 1 my $old = grep /^old$/, @values;
656 1 ▶ last unless $new or $old;
Branch: 1 branch
BranchTrueFalseTotal
unless $new or $old 0 1 1
Condition33%1/3
$new or $old
l!l&&r!l&&!r
100
MC/DC0%0/2
$new or $old
$new $old
Truth table33%1/3
A or B
A= $newB= $old
ABresult
1X1
011
000
657 1 ▶ unless (eval { require Digest::MD5 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Digest::MD5 } }) 0 1 1
658 0 $et->Warn('Digest::MD5 must be installed to calculate IPTC digest');
659 0 last;
660 }
661 1 my $dataPt;
662 1 ▶ if ($new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) 1 0 1
663 1 ▶ if (defined $newData) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newData) { } 1 0 1
664 1 $dataPt = \$newData;
665 } else {
666 0 $dataPt = $$dirInfo{DataPt};
667 0 ▶ if ($$dirInfo{DirStart} or length($$dataPt) != $$dirInfo{DirLen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirInfo->{"DirStart"} or length $$dataPt != $dirInfo->{"DirLen"}) 0 0 0
Condition0%0/3
$dirInfo->{"DirStart"} or length $$dataPt != $dirInfo->{"DirLen"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$dirInfo->{"DirStart"} or length $$dataPt != $dirInfo->{"DirLen"}
$dirInfo->{"DirStart"} length $$dataPt != $dirInfo->{"DirLen"}
Truth table0%0/3
A or B
A= $dirInfo->{"DirStart"}B= length $$dataPt != $dirInfo->{"DirLen"}
ABresult
1X1
011
000
668 0 my $buff = substr($$dataPt, $$dirInfo{DirStart}, $$dirInfo{DirLen});
669 0 $dataPt = \$buff;
670 }
671 }
672 # set NewIPTCDigest data member unless IPTC is being deleted
673 1 ▶ $$et{NewIPTCDigest} = Digest::MD5::md5($$dataPt) if length $$dataPt;
Branch: 1 branch
BranchTrueFalseTotal
if length $$dataPt 1 0 1
674 }
675 1 ▶ if ($old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old) 1 0 1
676 1 ▶ if ($new and not defined $newData) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new and not defined $newData) { } 0 1 1
elsif ($dirInfo->{'DataPt'}) { } 1 0 1
Condition33%1/3
$new and not defined $newData
!ll&&!rl&&r
010
MC/DC0%0/2
$new and not defined $newData
$new not defined $newData
Truth table33%1/3
A and not(B)
A= $newB= not defined $newData
ABresult
0X0
100
111
677 0 $$et{OldIPTCDigest} = $$et{NewIPTCDigest};
678 } elsif ($$dirInfo{DataPt}) { #(may be undef if creating new IPTC)
679 1 $dataPt = $$dirInfo{DataPt};
680 1 ▶ if ($$dirInfo{DirStart} or length($$dataPt) != $$dirInfo{DirLen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirInfo->{"DirStart"} or length $$dataPt != $dirInfo->{"DirLen"}) 1 0 1
Condition33%1/3
$dirInfo->{"DirStart"} or length $$dataPt != $dirInfo->{"DirLen"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$dirInfo->{"DirStart"} or length $$dataPt != $dirInfo->{"DirLen"}
$dirInfo->{"DirStart"} length $$dataPt != $dirInfo->{"DirLen"}
Truth table33%1/3
A or B
A= $dirInfo->{"DirStart"}B= length $$dataPt != $dirInfo->{"DirLen"}
ABresult
1X1
011
000
681 1 my $buff = substr($$dataPt, $$dirInfo{DirStart}, $$dirInfo{DirLen});
682 1 $dataPt = \$buff;
683 }
684 1 ▶ $$et{OldIPTCDigest} = Digest::MD5::md5($$dataPt) if length $$dataPt;
Branch: 1 branch
BranchTrueFalseTotal
if length $$dataPt 1 0 1
685 }
686 }
687 1 last;
688 }
689 # set changed if ForceWrite tag was set to "IPTC"
690 67 ▶ ++$$et{CHANGED} if defined $newData and length $newData and $$et{FORCE_WRITE}{IPTC};
Branch: 1 branch
BranchTrueFalseTotal
if defined $newData and length $newData and $et->{"FORCE_WRITE"}{"IPTC"} 0 67 67
Condition83%5/6
defined $newData and length $newData and $et->{"FORCE_WRITE"}{"IPTC"}
!ll&&!rl&&r
27400
defined $newData and length $newData
!ll&&!rl&&r
25240
MC/DC0%0/3
defined $newData and length $newData and $et->{"FORCE_WRITE"}{"IPTC"}
defined $newData length $newData $et->{"FORCE_WRITE"}{"IPTC"}
Truth table75%3/4
A and B and C
A= defined $newDataB= length $newDataC= $et->{"FORCE_WRITE"}{"IPTC"}
ABCresult
0XX0
10X0
1100
1111
691 67 return $newData;
692 }
693  
694  
695 22 1; # end
696  
697 __END__
« Image/ExifTool/Shift.pl ↑ Summary Image/ExifTool/Kandao.pm »