Image/ExifTool/WritePhotoshop.pl

filter: Statementstmt 78.8% 97 / 123 Branchbran 61.8% 47 / 76 Conditioncond 40.0% 14 / 35 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 100% 3 / 3 Podpod 0.0% 0 / 2 totaltotal 64.3% 166 / 258 CC 52 SCAR 45.4
CC
52
Cov
68%
WritePhotoshop
103.7
SetResourceName
17.3
BEGIN
1.0
CRAP 93.8
« Image/ExifTool/PhaseOne.pm ↑ Summary Image/ExifTool/Shift.pl »
1 #------------------------------------------------------------------------------
2 # File: WritePhotoshop.pl
3 #
4 # Description: Write Photoshop IRB meta information
5 #
6 # Revisions: 12/17/2004 - P. Harvey Created
7 #------------------------------------------------------------------------------
8  
9 package Image::ExifTool::Photoshop;
10  
11 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #------------------------------------------------------------------------------
14 # Strip resource name from value prepare resource name for writing into IRB
15 # Inputs: 0) tagInfo ref, 1) resource name (padded pascal string), 2) new value ref
16 # Returns: none (updates name and value if necessary)
17 sub SetResourceName($$$)
18 68 ▶{
Subroutine SetResourceName: called CC=5 SCAR=28.5
Pod undocumented
19 68 my ($tagInfo, $name, $valPt) = @_;
20 68 my $setName = $$tagInfo{SetResourceName};
21 68 ▶ if (defined $setName) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $setName) 0 68 68
22 # extract resource name from value
23 0 ▶ if ($$valPt =~ m{.*/#(.{0,255})#/$}s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$valPt =~ m[.*/#(.{0,255})#/$]s) { } 0 0 0
elsif ($setName eq '1') { } 0 0 0
24 0 $name = $1;
25 # strip name from value
26 0 $$valPt = substr($$valPt, 0, -4 - length($name));
27 } elsif ($setName eq '1') {
28 0 return; # use old name
29 } else {
30 0 $name = $setName;
31 }
32 # convert to padded pascal string
33 0 $name = chr(length $name) . $name;
34 0 ▶ $name .= "\0" if length($name) & 0x01;
Branch: 1 branch
BranchTrueFalseTotal
if length $name & 1 0 0 0
35 0 $_[1] = $name; # return new name
36 }
37 }
38  
39 #------------------------------------------------------------------------------
40 # Write Photoshop IRB resource
41 # Inputs: 0) ExifTool object reference, 1) source dirInfo reference,
42 # 2) tag table reference
43 # Returns: IRB resource data (may be empty if no Photoshop data)
44 # Notes: Increments ExifTool CHANGED flag for each tag changed
45 sub WritePhotoshop($$$)
46 77 ▶{
Subroutine WritePhotoshop: called CC=48 SCAR=46.4
Pod undocumented
47 77 my ($et, $dirInfo, $tagTablePtr) = @_;
48 77 ▶ $et or return 1; # allow dummy access to autoload this package
Branch: 1 branch
BranchTrueFalseTotal
$et or return 1 32 45 77
49 32 my $dataPt = $$dirInfo{DataPt};
50 32 ▶ unless ($dataPt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dataPt) 21 11 32
51 21 my $emptyData = '';
52 21 $dataPt = \$emptyData;
53 }
54 32 ▶ my $start = $$dirInfo{DirStart} || 0;
Condition100%2/2
$dirInfo->{'DirStart'} || 0
l!l
824
MC/DC100%1/1
$dirInfo->{'DirStart'} || 0
$dirInfo->{'DirStart'}
Truth table100%2/2
A
A= $dirInfo->{'DirStart'}
Aresult
11
00
55 32 ▶ my $dirLen = $$dirInfo{DirLen} || (length($$dataPt) - $start);
Condition66%2/3
$dirInfo->{'DirLen'} || length($$dataPt) - $start
l!l&&r!l&&!r
10022
MC/DC50%1/2
$dirInfo->{'DirLen'} || length($$dataPt) - $start
$dirInfo->{'DirLen'} length($$dataPt) - $start
Truth table66%2/3
A || B
A= $dirInfo->{'DirLen'}B= length($$dataPt) - $start
ABresult
1X1
011
000
56 32 my $dirEnd = $start + $dirLen;
57 32 my $newData = '';
58  
59 # make a hash of new tag info, keyed on tagID
60 32 my $newTags = $et->GetNewTagInfoHash($tagTablePtr);
61  
62 32 my ($addDirs, $editDirs) = $et->GetAddDirHash($tagTablePtr);
63  
64 32 SetByteOrder('MM'); # Photoshop is always big-endian
65 #
66 # rewrite existing tags in the old directory, deleting ones as necessary
67 # (the Photoshop directory entries aren't in any particular order)
68 #
69 # Format: 0) Type, 4 bytes - '8BIM' (or the rare 'PHUT', 'DCSR', 'AgHg' or 'MeSa')
70 # 1) TagID,2 bytes
71 # 2) Name, pascal string padded to even no. bytes
72 # 3) Size, 4 bytes - N
73 # 4) Data, N bytes
74 32 my ($pos, $value, $size, $tagInfo, $tagID);
75 32 ▶ for ($pos=$start; $pos+8<$dirEnd; $pos+=$size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + 8 < $dirEnd) 90 32 122
76 # each entry must be on same even byte boundary as directory start
77 90 ▶ ++$pos if ($pos ^ $start) & 0x01;
Branch: 1 branch
BranchTrueFalseTotal
if ($pos ^ $start) & 1 24 66 90
78 90 my $type = substr($$dataPt, $pos, 4);
79 90 ▶ if ($type !~ /^(8BIM|PHUT|DCSR|AgHg|MeSa)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^(8BIM|PHUT|DCSR|AgHg|MeSa)$/) 0 90 90
80 0 $et->Error("Bad Photoshop IRB resource");
81 0 undef $newData;
82 0 last;
83 }
84 90 $tagID = Get16u($dataPt, $pos + 4);
85 # get resource block name (pascal string padded to an even # of bytes)
86 90 my $namelen = 1 + Get8u($dataPt, $pos + 6);
87 90 ▶ ++$namelen if $namelen & 0x01;
Branch: 1 branch
BranchTrueFalseTotal
if $namelen & 1 90 0 90
88 90 ▶ if ($pos + $namelen + 10 > $dirEnd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + $namelen + 10 > $dirEnd) 0 90 90
89 0 $et->Error("Bad APP13 resource block");
90 0 undef $newData;
91 0 last;
92 }
93 90 my $name = substr($$dataPt, $pos + 6, $namelen);
94 90 $size = Get32u($dataPt, $pos + 6 + $namelen);
95 90 $pos += $namelen + 10;
96 90 ▶ if ($size + $pos > $dirEnd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size + $pos > $dirEnd) 0 90 90
97 0 $et->Error("Bad APP13 resource data size $size");
98 0 undef $newData;
99 0 last;
100 }
101 90 ▶ if ($$newTags{$tagID} and $type eq '8BIM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($newTags->{$tagID} and $type eq '8BIM') { } 3 87 90
Condition66%2/3
$newTags->{$tagID} and $type eq "8BIM"
!ll&&!rl&&r
8703
MC/DC50%1/2
$newTags->{$tagID} and $type eq "8BIM"
$newTags->{$tagID} $type eq "8BIM"
Truth table66%2/3
A and B
A= $newTags->{$tagID}B= $type eq "8BIM"
ABresult
0X0
100
111
102 3 $tagInfo = $$newTags{$tagID};
103 3 delete $$newTags{$tagID};
104 3 my $nvHash = $et->GetNewValueHash($tagInfo);
105 # check to see if we are overwriting this tag
106 3 $value = substr($$dataPt, $pos, $size);
107 3 my $isOverwriting = $et->IsOverwriting($nvHash, $value);
108 # handle special 'new' and 'old' values for IPTCDigest
109 3 ▶ if (not $isOverwriting and $tagInfo eq $iptcDigestInfo) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $isOverwriting and $tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo) 0 3 3
Condition33%1/3
not $isOverwriting and $tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo
!ll&&!rl&&r
300
MC/DC0%0/2
not $isOverwriting and $tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo
not $isOverwriting $tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo
Truth table33%1/3
not(A) and B
A= not $isOverwritingB= $tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo
ABresult
0X0
100
111
110 0 ▶ if (grep /^new$/, @{$$nvHash{DelValue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^new$/, @{$$nvHash{"DelValue"};}) 0 0 0
111 $isOverwriting = 1 if $$et{NewIPTCDigest} and
112 0 ▶ $$et{NewIPTCDigest} eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $et->{"NewIPTCDigest"} and $et->{"NewIPTCDigest"} eq $value 0 0 0
Condition0%0/3
$et->{"NewIPTCDigest"} and $et->{"NewIPTCDigest"} eq $value
!ll&&!rl&&r
000
MC/DC0%0/2
$et->{"NewIPTCDigest"} and $et->{"NewIPTCDigest"} eq $value
$et->{"NewIPTCDigest"} $et->{"NewIPTCDigest"} eq $value
Truth table0%0/3
A and B
A= $et->{"NewIPTCDigest"}B= $et->{"NewIPTCDigest"} eq $value
ABresult
0X0
100
111
113 }
114 0 ▶ if (grep /^old$/, @{$$nvHash{DelValue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^old$/, @{$$nvHash{"DelValue"};}) 0 0 0
115 $isOverwriting = 1 if $$et{OldIPTCDigest} and
116 0 ▶ $$et{OldIPTCDigest} eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $et->{"OldIPTCDigest"} and $et->{"OldIPTCDigest"} eq $value 0 0 0
Condition0%0/3
$et->{"OldIPTCDigest"} and $et->{"OldIPTCDigest"} eq $value
!ll&&!rl&&r
000
MC/DC0%0/2
$et->{"OldIPTCDigest"} and $et->{"OldIPTCDigest"} eq $value
$et->{"OldIPTCDigest"} $et->{"OldIPTCDigest"} eq $value
Truth table0%0/3
A and B
A= $et->{"OldIPTCDigest"}B= $et->{"OldIPTCDigest"} eq $value
ABresult
0X0
100
111
117 }
118 }
119 3 ▶ if ($isOverwriting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isOverwriting) 3 0 3
120 3 $et->VerboseValue("- Photoshop:$$tagInfo{Name}", $value);
121 # handle IPTCDigest specially because we want to write it last
122 # so the new IPTC digest will be known
123 3 ▶ if ($tagInfo eq $iptcDigestInfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo) { } 0 3 3
124 0 $$newTags{$tagID} = $tagInfo; # add later
125 0 $value = undef;
126 } else {
127 3 $value = $et->GetNewValue($nvHash);
128 }
129 3 ++$$et{CHANGED};
130 3 ▶ next unless defined $value; # next if tag is being deleted
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
131 # set resource name if necessary
132 3 SetResourceName($tagInfo, $name, \$value);
133 3 $et->VerboseValue("+ Photoshop:$$tagInfo{Name}", $value);
134 }
135 } else {
136 87 ▶ if ($type eq '8BIM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "8BIM") 87 0 87
137 87 $tagInfo = $$editDirs{$tagID};
138 87 ▶ unless ($tagInfo) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagInfo) 76 11 87
139 # process subdirectory anyway if writable (except EXIF to avoid recursion)
140 # --> this allows IPTC to be processed if found here in TIFF images
141 # (but allow EXIF to be written in PSD files if XMP or IPTC tags are
142 # being written because I have seen cases of XMP in PSD-EXIFInfo-IFD0
143 # and IPTC in PSD-EXIFInfo-IFD0-IPTC, see forum10768 and forum13198)
144 76 my $tmpInfo = $et->GetTagInfo($tagTablePtr, $tagID);
145 76 ▶ if ($tmpInfo and $$tmpInfo{SubDirectory} and
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpInfo and $tmpInfo->{"SubDirectory"} and $tmpInfo->{"SubDirectory"}{"TagTable"} ne "Image::ExifTool::Exif::Main" || $et->{"FILE_TYPE"} eq "PSD" && ($editDirs->{1028} || $editDirs->{1060})) 22 54 76
Condition33%5/15
$tmpInfo and $tmpInfo->{"SubDirectory"} and $tmpInfo->{"SubDirectory"}{"TagTable"} ne "Image::ExifTool::Exif::Main" || $et->{"FILE_TYPE"} eq "PSD" && ($editDirs->{1028} || $editDirs->{1060})
!ll&&!rl&&r
54022
$tmpInfo and $tmpInfo->{"SubDirectory"}
!ll&&!rl&&r
05422
$tmpInfo->{"SubDirectory"}{"TagTable"} ne "Image::ExifTool::Exif::Main" or $et->{"FILE_TYPE"} eq "PSD" and $editDirs->{1028} || $editDirs->{1060}
l!l&&r!l&&!r
2200
$et->{"FILE_TYPE"} eq "PSD" and $editDirs->{1028} || $editDirs->{1060}
!ll&&!rl&&r
000
$editDirs->{1028} or $editDirs->{1060}
l!l&&r!l&&!r
000
MC/DC16%1/6
$tmpInfo and $tmpInfo->{"SubDirectory"} and $tmpInfo->{"SubDirectory"}{"TagTable"} ne "Image::ExifTool::Exif::Main" || $et->{"FILE_TYPE"} eq "PSD" && ($editDirs->{1028} || $editDirs->{1060})
$tmpInfo $tmpInfo->{"SubDirectory"} $tmpInfo->{"SubDirectory"}{"TagTable"} ne "Image::ExifTool::Exif::Main" $et->{"FILE_TYPE"} eq "PSD" $editDirs->{1028} $editDirs->{1060}
Truth table28%2/7
A and B and C or D and E or F
A= $tmpInfoB= $tmpInfo->{"SubDirectory"}C= $tmpInfo->{"SubDirectory"}{"TagTable"} ne "Image::ExifTool::Exif::Main"D= $et->{"FILE_TYPE"} eq "PSD"E= $editDirs->{1028}F= $editDirs->{1060}
ABCDEFresult
0XXXXX0
10XXXX0
1100XX0
1101000
111XXX1
11011X1
1101011
146 ($tmpInfo->{SubDirectory}->{TagTable} ne 'Image::ExifTool::Exif::Main' or
147 ($$et{FILE_TYPE} eq 'PSD' and ($$editDirs{0x0404} or $$editDirs{0x0424}))))
148 {
149 22 my $table = Image::ExifTool::GetTagTable($tmpInfo->{SubDirectory}->{TagTable});
150 22 ▶ $tagInfo = $tmpInfo if $$table{WRITE_PROC};
Branch: 1 branch
BranchTrueFalseTotal
if $table->{"WRITE_PROC"} 18 4 22
151 }
152 }
153 }
154 87 ▶ if ($tagInfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo) { } 29 58 87
155 29 ▶ $$addDirs{$tagID} and delete $$addDirs{$tagID};
Branch: 1 branch
BranchTrueFalseTotal
$addDirs->{$tagID} and delete $addDirs->{$tagID} 8 21 29
156 my %subdirInfo = (
157 DataPt => $dataPt,
158 DirStart => $pos,
159 DataLen => $dirLen,
160 DirLen => $size,
161 Parent => $$dirInfo{DirName},
162 29 );
163 29 my $subTable = Image::ExifTool::GetTagTable($tagInfo->{SubDirectory}->{TagTable});
164 29 my $writeProc = $tagInfo->{SubDirectory}->{WriteProc};
165 29 my $newValue = $et->WriteDirectory(\%subdirInfo, $subTable, $writeProc);
166 29 ▶ if (defined $newValue) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newValue) { } 27 2 29
167 27 ▶ next unless length $newValue; # remove subdirectory entry
Branch: 1 branch
BranchTrueFalseTotal
unless length $newValue 2 25 27
168 25 $value = $newValue;
169 25 SetResourceName($tagInfo, $name, \$value);
170 } else {
171 2 $value = substr($$dataPt, $pos, $size); # rewrite old directory
172 }
173 } else {
174 58 $value = substr($$dataPt, $pos, $size);
175 }
176 }
177 88 my $newSize = length $value;
178 # write this directory entry
179 88 $newData .= $type . Set16u($tagID) . $name . Set32u($newSize) . $value;
180 88 ▶ $newData .= "\0" if $newSize & 0x01; # must null pad to even byte
Branch: 1 branch
BranchTrueFalseTotal
if $newSize & 1 32 56 88
181 }
182 #
183 # write any remaining entries we didn't find in the old directory
184 # (might as well write them in numerical tag order)
185 #
186 32 my @tagsLeft = sort { $a <=> $b } keys(%$newTags), keys(%$addDirs);
187 32 foreach $tagID (@tagsLeft) {
188 44 my $name = "\0\0";
189 44 ▶ if ($$newTags{$tagID}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newTags->{$tagID}) { } 21 23 44
190 21 $tagInfo = $$newTags{$tagID};
191 21 my $nvHash = $et->GetNewValueHash($tagInfo);
192 21 $value = $et->GetNewValue($nvHash);
193 # handle new IPTCDigest value specially
194 21 ▶ if ($tagInfo eq $iptcDigestInfo and defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo and defined $value) { } 1 20 21
Condition66%2/3
$tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo and defined $value
!ll&&!rl&&r
2001
MC/DC50%1/2
$tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo and defined $value
$tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfo defined $value
Truth table66%2/3
A and B
A= $tagInfo eq $Image::ExifTool::Photoshop::iptcDigestInfoB= defined $value
ABresult
0X0
100
111
195 1 ▶ if ($value eq 'new') {
Branch: 2 branches
BranchTrueFalseTotal
if ($value eq 'new') { } 1 0 1
elsif ($value eq 'old') { } 0 0 0
196 1 $value = $$et{NewIPTCDigest};
197 } elsif ($value eq 'old') {
198 0 $value = $$et{OldIPTCDigest};
199 }
200 # (we already know we want to create this tag)
201 } else {
202 # don't add this tag unless specified
203 20 ▶ next unless $$nvHash{IsCreating};
Branch: 1 branch
BranchTrueFalseTotal
unless $nvHash->{"IsCreating"} 4 16 20
204 }
205 17 ▶ next unless defined $value; # next if tag is being deleted
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 17 17
206 17 $et->VerboseValue("+ Photoshop:$$tagInfo{Name}", $value);
207 17 ++$$et{CHANGED};
208 } else {
209 23 $tagInfo = $$addDirs{$tagID};
210 # create new directory
211 my %subdirInfo = (
212 Parent => $$dirInfo{DirName},
213 23 );
214 23 my $subTable = Image::ExifTool::GetTagTable($tagInfo->{SubDirectory}->{TagTable});
215 23 my $writeProc = $tagInfo->{SubDirectory}->{WriteProc};
216 23 $value = $et->WriteDirectory(\%subdirInfo, $subTable, $writeProc);
217 23 ▶ next unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 23 23
218 }
219 # set resource name if necessary
220 40 SetResourceName($tagInfo, $name, \$value);
221 40 $size = length($value);
222 # write the new directory entry
223 40 $newData .= '8BIM' . Set16u($tagID) . $name . Set32u($size) . $value;
224 40 ▶ $newData .= "\0" if $size & 0x01; # must null pad to even numbered byte
Branch: 1 branch
BranchTrueFalseTotal
if $size & 1 22 18 40
225 40 ++$$et{CHANGED};
226 }
227 32 return $newData;
228 }
229  
230  
231 16 1; # end
232  
233 __END__
« Image/ExifTool/PhaseOne.pm ↑ Summary Image/ExifTool/Shift.pl »