WritePhotoshop.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #------------------------------------------------------------------------------
  2. # File: WritePhotoshop.pl
  3. #
  4. # Description: Write Photoshop IRB meta information
  5. #
  6. # Revisions: 12/17/2004 - P. Harvey Created
  7. #------------------------------------------------------------------------------
  8. package Image::ExifTool::Photoshop;
  9. use strict;
  10. #------------------------------------------------------------------------------
  11. # Strip resource name from value prepare resource name for writing into IRB
  12. # Inputs: 0) tagInfo ref, 1) resource name (padded pascal string), 2) new value ref
  13. # Returns: none (updates name and value if necessary)
  14. sub SetResourceName($$$)
  15. {
  16. my ($tagInfo, $name, $valPt) = @_;
  17. my $setName = $$tagInfo{SetResourceName};
  18. if (defined $setName) {
  19. # extract resource name from value
  20. if ($$valPt =~ m{.*/#(.{0,255})#/$}s) {
  21. $name = $1;
  22. # strip name from value
  23. $$valPt = substr($$valPt, 0, -4 - length($name));
  24. } elsif ($setName eq '1') {
  25. return; # use old name
  26. } else {
  27. $name = $setName;
  28. }
  29. # convert to padded pascal string
  30. $name = chr(length $name) . $name;
  31. $name .= "\0" if length($name) & 0x01;
  32. $_[1] = $name; # return new name
  33. }
  34. }
  35. #------------------------------------------------------------------------------
  36. # Write Photoshop IRB resource
  37. # Inputs: 0) ExifTool object reference, 1) source dirInfo reference,
  38. # 2) tag table reference
  39. # Returns: IRB resource data (may be empty if no Photoshop data)
  40. # Notes: Increments ExifTool CHANGED flag for each tag changed
  41. sub WritePhotoshop($$$)
  42. {
  43. my ($et, $dirInfo, $tagTablePtr) = @_;
  44. $et or return 1; # allow dummy access to autoload this package
  45. my $dataPt = $$dirInfo{DataPt};
  46. unless ($dataPt) {
  47. my $emptyData = '';
  48. $dataPt = \$emptyData;
  49. }
  50. my $start = $$dirInfo{DirStart} || 0;
  51. my $dirLen = $$dirInfo{DirLen} || (length($$dataPt) - $start);
  52. my $dirEnd = $start + $dirLen;
  53. my $newData = '';
  54. # make a hash of new tag info, keyed on tagID
  55. my $newTags = $et->GetNewTagInfoHash($tagTablePtr);
  56. my ($addDirs, $editDirs) = $et->GetAddDirHash($tagTablePtr);
  57. SetByteOrder('MM'); # Photoshop is always big-endian
  58. #
  59. # rewrite existing tags in the old directory, deleting ones as necessary
  60. # (the Photoshop directory entries aren't in any particular order)
  61. #
  62. # Format: 0) Type, 4 bytes - '8BIM' (or the rare 'PHUT', 'DCSR' or 'AgHg')
  63. # 1) TagID,2 bytes
  64. # 2) Name, pascal string padded to even no. bytes
  65. # 3) Size, 4 bytes - N
  66. # 4) Data, N bytes
  67. my ($pos, $value, $size, $tagInfo, $tagID);
  68. for ($pos=$start; $pos+8<$dirEnd; $pos+=$size) {
  69. # each entry must be on same even byte boundary as directory start
  70. ++$pos if ($pos ^ $start) & 0x01;
  71. my $type = substr($$dataPt, $pos, 4);
  72. if ($type !~ /^(8BIM|PHUT|DCSR|AgHg)$/) {
  73. $et->Error("Bad Photoshop IRB resource");
  74. undef $newData;
  75. last;
  76. }
  77. $tagID = Get16u($dataPt, $pos + 4);
  78. # get resource block name (pascal string padded to an even # of bytes)
  79. my $namelen = 1 + Get8u($dataPt, $pos + 6);
  80. ++$namelen if $namelen & 0x01;
  81. if ($pos + $namelen + 10 > $dirEnd) {
  82. $et->Error("Bad APP13 resource block");
  83. undef $newData;
  84. last;
  85. }
  86. my $name = substr($$dataPt, $pos + 6, $namelen);
  87. $size = Get32u($dataPt, $pos + 6 + $namelen);
  88. $pos += $namelen + 10;
  89. if ($size + $pos > $dirEnd) {
  90. $et->Error("Bad APP13 resource data size $size");
  91. undef $newData;
  92. last;
  93. }
  94. if ($$newTags{$tagID} and $type eq '8BIM') {
  95. $tagInfo = $$newTags{$tagID};
  96. delete $$newTags{$tagID};
  97. my $nvHash = $et->GetNewValueHash($tagInfo);
  98. # check to see if we are overwriting this tag
  99. $value = substr($$dataPt, $pos, $size);
  100. my $isOverwriting = $et->IsOverwriting($nvHash, $value);
  101. # handle special 'new' and 'old' values for IPTCDigest
  102. if (not $isOverwriting and $tagInfo eq $iptcDigestInfo) {
  103. if (grep /^new$/, @{$$nvHash{DelValue}}) {
  104. $isOverwriting = 1 if $$et{NewIPTCDigest} and
  105. $$et{NewIPTCDigest} eq $value;
  106. }
  107. if (grep /^old$/, @{$$nvHash{DelValue}}) {
  108. $isOverwriting = 1 if $$et{OldIPTCDigest} and
  109. $$et{OldIPTCDigest} eq $value;
  110. }
  111. }
  112. if ($isOverwriting) {
  113. $et->VerboseValue("- Photoshop:$$tagInfo{Name}", $value);
  114. # handle IPTCDigest specially because we want to write it last
  115. # so the new IPTC digest will be known
  116. if ($tagInfo eq $iptcDigestInfo) {
  117. $$newTags{$tagID} = $tagInfo; # add later
  118. $value = undef;
  119. } else {
  120. $value = $et->GetNewValue($nvHash);
  121. }
  122. ++$$et{CHANGED};
  123. next unless defined $value; # next if tag is being deleted
  124. # set resource name if necessary
  125. SetResourceName($tagInfo, $name, \$value);
  126. $et->VerboseValue("+ Photoshop:$$tagInfo{Name}", $value);
  127. }
  128. } else {
  129. if ($type eq '8BIM') {
  130. $tagInfo = $$editDirs{$tagID};
  131. unless ($tagInfo) {
  132. # process subdirectory anyway if writable (except EXIF to avoid recursion)
  133. # --> this allows IPTC to be processed if found here in TIFF images
  134. my $tmpInfo = $et->GetTagInfo($tagTablePtr, $tagID);
  135. if ($tmpInfo and $$tmpInfo{SubDirectory} and
  136. $tmpInfo->{SubDirectory}->{TagTable} ne 'Image::ExifTool::Exif::Main')
  137. {
  138. my $table = Image::ExifTool::GetTagTable($tmpInfo->{SubDirectory}->{TagTable});
  139. $tagInfo = $tmpInfo if $$table{WRITE_PROC};
  140. }
  141. }
  142. }
  143. if ($tagInfo) {
  144. $$addDirs{$tagID} and delete $$addDirs{$tagID};
  145. my %subdirInfo = (
  146. DataPt => $dataPt,
  147. DirStart => $pos,
  148. DataLen => $dirLen,
  149. DirLen => $size,
  150. Parent => $$dirInfo{DirName},
  151. );
  152. my $subTable = Image::ExifTool::GetTagTable($tagInfo->{SubDirectory}->{TagTable});
  153. my $writeProc = $tagInfo->{SubDirectory}->{WriteProc};
  154. my $newValue = $et->WriteDirectory(\%subdirInfo, $subTable, $writeProc);
  155. if (defined $newValue) {
  156. next unless length $newValue; # remove subdirectory entry
  157. $value = $newValue;
  158. SetResourceName($tagInfo, $name, \$value);
  159. } else {
  160. $value = substr($$dataPt, $pos, $size); # rewrite old directory
  161. }
  162. } else {
  163. $value = substr($$dataPt, $pos, $size);
  164. }
  165. }
  166. my $newSize = length $value;
  167. # write this directory entry
  168. $newData .= $type . Set16u($tagID) . $name . Set32u($newSize) . $value;
  169. $newData .= "\0" if $newSize & 0x01; # must null pad to even byte
  170. }
  171. #
  172. # write any remaining entries we didn't find in the old directory
  173. # (might as well write them in numerical tag order)
  174. #
  175. my @tagsLeft = sort { $a <=> $b } keys(%$newTags), keys(%$addDirs);
  176. foreach $tagID (@tagsLeft) {
  177. my $name = "\0\0";
  178. if ($$newTags{$tagID}) {
  179. $tagInfo = $$newTags{$tagID};
  180. my $nvHash = $et->GetNewValueHash($tagInfo);
  181. $value = $et->GetNewValue($nvHash);
  182. # handle new IPTCDigest value specially
  183. if ($tagInfo eq $iptcDigestInfo and defined $value) {
  184. if ($value eq 'new') {
  185. $value = $$et{NewIPTCDigest};
  186. } elsif ($value eq 'old') {
  187. $value = $$et{OldIPTCDigest};
  188. }
  189. # (we already know we want to create this tag)
  190. } else {
  191. # don't add this tag unless specified
  192. next unless $$nvHash{IsCreating};
  193. }
  194. next unless defined $value; # next if tag is being deleted
  195. $et->VerboseValue("+ Photoshop:$$tagInfo{Name}", $value);
  196. ++$$et{CHANGED};
  197. } else {
  198. $tagInfo = $$addDirs{$tagID};
  199. # create new directory
  200. my %subdirInfo = (
  201. Parent => $$dirInfo{DirName},
  202. );
  203. my $subTable = Image::ExifTool::GetTagTable($tagInfo->{SubDirectory}->{TagTable});
  204. my $writeProc = $tagInfo->{SubDirectory}->{WriteProc};
  205. $value = $et->WriteDirectory(\%subdirInfo, $subTable, $writeProc);
  206. next unless $value;
  207. }
  208. # set resource name if necessary
  209. SetResourceName($tagInfo, $name, \$value);
  210. $size = length($value);
  211. # write the new directory entry
  212. $newData .= '8BIM' . Set16u($tagID) . $name . Set32u($size) . $value;
  213. $newData .= "\0" if $size & 0x01; # must null pad to even numbered byte
  214. ++$$et{CHANGED};
  215. }
  216. return $newData;
  217. }
  218. 1; # end
  219. __END__
  220. =head1 NAME
  221. Image::ExifTool::WritePhotoshop.pl - Write Photoshop IRB meta information
  222. =head1 SYNOPSIS
  223. This file is autoloaded by Image::ExifTool::Photoshop.
  224. =head1 DESCRIPTION
  225. This file contains routines to write Photoshop metadata.
  226. =head1 NOTES
  227. Photoshop IRB blocks may have an associated resource name. By default, the
  228. existing name is preserved when rewriting a resource, and an empty name is
  229. used when creating a new resource. However, a different resource name may
  230. be specified by defining a C<SetResourceName> entry in the tag information
  231. hash. With this defined, a new resource name may be appended to the value
  232. in the form "VALUE/#NAME#/" (the slashes and hashes are literal). If
  233. C<SetResourceName> is anything other than '1', the value is used as a
  234. default resource name, and applied if no appended name is provided.
  235. =head1 AUTHOR
  236. Copyright 2003-2016, Phil Harvey (phil at owl.phy.queensu.ca)
  237. This library is free software; you can redistribute it and/or modify it
  238. under the same terms as Perl itself.
  239. =head1 SEE ALSO
  240. L<Image::ExifTool::Photoshop(3pm)|Image::ExifTool::Photoshop>,
  241. L<Image::ExifTool(3pm)|Image::ExifTool>
  242. =cut