File Coverage

blib/lib/Image/ExifTool/WritePNG.pl
Criterion Covered Total %
statement 161 204 78.9
branch 80 134 59.7
condition 11 38 28.9
subroutine 8 8 100.0
pod 0 7 0.0
total 260 391 66.5


line stmt bran cond sub pod time code
1             #------------------------------------------------------------------------------
2             # File: WritePNG.pl
3             #
4             # Description: Write PNG meta information
5             #
6             # Revisions: 09/16/2005 - P. Harvey Created
7             #
8             # References: 1) http://www.libpng.org/pub/png/spec/1.2/
9             #------------------------------------------------------------------------------
10             package Image::ExifTool::PNG;
11              
12 1     1   7 use strict;
  1         1  
  1         2169  
13              
14             #------------------------------------------------------------------------------
15             # Calculate CRC or update running CRC (ref 1)
16             # Inputs: 0) data reference, 1) running crc to update (undef initially)
17             # 2) data position (undef for 0), 3) data length (undef for all data),
18             # Returns: updated CRC
19             my @crcTable;
20             sub CalculateCRC($;$$$)
21             {
22 98     98 0 157 my ($dataPt, $crc, $pos, $len) = @_;
23 98 100       173 $crc = 0 unless defined $crc;
24 98 100       167 $pos = 0 unless defined $pos;
25 98 50       165 $len = length($$dataPt) - $pos unless defined $len;
26 98         115 $crc ^= 0xffffffff; # undo 1's complement
27             # build lookup table unless done already
28 98 100       152 unless (@crcTable) {
29 1         2 my ($c, $n, $k);
30 1         3 for ($n=0; $n<256; ++$n) {
31 256         359 for ($k=0, $c=$n; $k<8; ++$k) {
32 2048 100       3251 $c = ($c & 1) ? 0xedb88320 ^ ($c >> 1) : $c >> 1;
33             }
34 256         361 $crcTable[$n] = $c;
35             }
36             }
37             # calculate the CRC
38 98         677 foreach (unpack("x${pos}C$len", $$dataPt)) {
39 11061         13197 $crc = $crcTable[($crc^$_) & 0xff] ^ ($crc >> 8);
40             }
41 98         348 return $crc ^ 0xffffffff; # return 1's complement
42             }
43              
44             #------------------------------------------------------------------------------
45             # Encode data in ASCII Hex
46             # Inputs: 0) input data reference
47             # Returns: Hex-encoded data (max 72 chars per line)
48             sub HexEncode($)
49             {
50 1     1 0 2 my $dataPt = shift;
51 1         3 my $len = length($$dataPt);
52 1         2 my $hex = '';
53 1         2 my $pos;
54 1         3 for ($pos = 0; $pos < $len; $pos += 36) {
55 13         16 my $n = $len - $pos;
56 13 100       18 $n > 36 and $n = 36;
57 13         40 $hex .= unpack('H*',substr($$dataPt,$pos,$n)) . "\n";
58             }
59 1         3 return $hex;
60             }
61              
62             #------------------------------------------------------------------------------
63             # Write profile chunk (possibly compressed if Zlib is available)
64             # Inputs: 0) outfile, 1) Raw profile type (SCALAR ref for iCCP name), 2) data ref
65             # 3) profile header type (undef if not a text profile)
66             # Returns: 1 on success
67             sub WriteProfile($$$;$)
68             {
69 4     4 0 9 my ($outfile, $rawType, $dataPt, $profile) = @_;
70 4         9 my ($buff, $prefix, $chunk, $deflate);
71 4 100 66     14 if ($rawType ne $stdCase{exif} and eval { require Compress::Zlib }) {
  2         527  
72 2         50908 $deflate = Compress::Zlib::deflateInit();
73             }
74 4 100       707 if (not defined $profile) {
75             # write ICC profile as compressed iCCP chunk if possible
76 3 100       7 if (ref $rawType) {
77 1 50       4 return 0 unless $deflate;
78 1         2 $chunk = 'iCCP';
79 1         4 $prefix = "$$rawType\0\0";
80             } else {
81 2         4 $chunk = $rawType;
82 2 50       6 if ($rawType eq $stdCase{zxif}) {
83 0         0 $prefix = "\0" . pack('N', length $$dataPt); # (proposed compressed EXIF)
84             } else {
85 2         3 $prefix = ''; # standard EXIF
86             }
87             }
88 3 100       14 if ($deflate) {
89 1         4 $buff = $deflate->deflate($$dataPt);
90 1 50       38 return 0 unless defined $buff;
91 1         4 $buff .= $deflate->flush();
92 1         55 $dataPt = \$buff;
93             }
94             } else {
95             # write as ASCII-hex encoded profile in tEXt or zTXt chunk
96 1         7 my $txtHdr = sprintf("\n$profile profile\n%8d\n", length($$dataPt));
97 1         4 $buff = $txtHdr . HexEncode($dataPt);
98 1         3 $chunk = 'tEXt'; # write as tEXt if deflate not available
99 1         3 $prefix = "Raw profile type $rawType\0";
100 1         3 $dataPt = \$buff;
101             # write profile as zTXt chunk if possible
102 1 50       6 if ($deflate) {
103 1         3 my $buf2 = $deflate->deflate($buff);
104 1 50       51 if (defined $buf2) {
105 1         3 $dataPt = \$buf2;
106 1         4 $buf2 .= $deflate->flush();
107 1         124 $chunk = 'zTXt';
108 1         3 $prefix .= "\0"; # compression type byte (0=deflate)
109             }
110             }
111             }
112 4         21 my $hdr = pack('Na4', length($prefix) + length($$dataPt), $chunk) . $prefix;
113 4         14 my $crc = CalculateCRC(\$hdr, undef, 4);
114 4         9 $crc = CalculateCRC($dataPt, $crc);
115 4         24 return Write($outfile, $hdr, $$dataPt, pack('N',$crc));
116             }
117              
118             #------------------------------------------------------------------------------
119             # Add iCCP-related chunks to the PNG image if necessary (must come before PLTE and IDAT)
120             # Inputs: 0) ExifTool object ref, 1) output file or scalar ref
121             # Returns: true on success
122             sub Add_iCCP($$)
123             {
124 10     10 0 20 my ($et, $outfile) = @_;
125 10 100       28 if ($$et{ADD_DIRS}{ICC_Profile}) {
126             # write new ICC data
127 1         4 my $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::ICC_Profile::Main');
128 1         4 my %dirInfo = ( Parent => 'PNG', DirName => 'ICC_Profile' );
129 1         5 my $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr);
130 1 50 33     5 if (defined $buff and length $buff) {
131 1         4 my $profileName = $et->GetNewValue($Image::ExifTool::PNG::Main{'iCCP-name'});
132 1 50       5 $profileName = 'icm' unless defined $profileName;
133 1 50       3 if (WriteProfile($outfile, \$profileName, \$buff)) {
134 1         7 $et->VPrint(0, "Created ICC profile with name '${profileName}'\n");
135 1         5 delete $$et{ADD_DIRS}{ICC_Profile}; # don't add it again
136             }
137             }
138             }
139             # must also add sRGB and gAMA before PLTE and IDAT
140 10 100       22 if ($$et{ADD_PNG}) {
141 5         9 my ($tag, %addBeforePLTE);
142 5         9 foreach $tag (qw(sRGB gAMA)) {
143 10 50       24 next unless $$et{ADD_PNG}{$tag};
144 0         0 $addBeforePLTE{$tag} = $$et{ADD_PNG}{$tag};
145 0         0 delete $$et{ADD_PNG}{$tag};
146             }
147 5 50       12 if (%addBeforePLTE) {
148 0         0 my $save = $$et{ADD_PNG};
149 0         0 $$et{ADD_PNG} = \%addBeforePLTE;
150 0         0 AddChunks($et, $outfile);
151 0         0 $$et{ADD_PNG} = $save;
152             }
153             }
154 10         21 return 1;
155             }
156              
157             #------------------------------------------------------------------------------
158             # This routine is called after we edit an existing directory
159             # Inputs: 0) ExifTool ref, 1) dir name, 2) output data ref
160             # 3) flag set if location is non-standard (to update, but not create from scratch)
161             # - on return, $$outBuff is set to '' if the directory is to be deleted
162             sub DoneDir($$$;$)
163             {
164 3     3 0 11 my ($et, $dir, $outBuff, $nonStandard) = @_;
165 3         4 my $saveDir = $dir;
166 3 50       9 $dir = 'EXIF' if $dir eq 'IFD0';
167             # don't add this directory again unless this is in a non-standard location
168 3 50 0     7 if (not $nonStandard) {
    0          
169 3         9 delete $$et{ADD_DIRS}{$dir};
170 3 50       10 delete $$et{ADD_DIRS}{IFD0} if $dir eq 'EXIF';
171             } elsif ($$et{DEL_GROUP}{$dir} or $$et{DEL_GROUP}{$saveDir}) {
172 0         0 $et->VPrint(0," Deleting non-standard $dir\n");
173 0         0 $$outBuff = '';
174             }
175             }
176              
177             #------------------------------------------------------------------------------
178             # Generate tEXt, zTXt or iTXt data for writing
179             # Inputs: 0) ExifTool ref, 1) tagID, 2) tagInfo ref, 3) value string, 4) language code
180             # Returns: chunk data (not including 8-byte chunk header)
181             # Notes: Sets ExifTool TextChunkType member to the type of chunk written
182             sub BuildTextChunk($$$$$)
183             {
184 15     15 0 35 my ($et, $tag, $tagInfo, $val, $lang) = @_;
185 15         24 my ($xtra, $compVal, $iTXt, $comp);
186 15 100       25 if ($$tagInfo{SubDirectory}) {
187 3 50       7 if ($$tagInfo{Name} eq 'XMP') {
188 3         5 $iTXt = 2; # write as iTXt but flag to avoid encoding
189             # (never compress XMP)
190             } else {
191 0         0 $comp = 2; # compress raw profile if possible
192             }
193             } else {
194             # compress if specified
195 12 50       28 $comp = 1 if $et->Options('Compress');
196 12 100 66     56 if ($lang) {
    50          
    50          
197 1         2 $iTXt = 1; # write as iTXt if it has a language code
198 1         15 $tag =~ s/-$lang$//; # remove language code from tagID
199             } elsif ($$et{OPTIONS}{Charset} ne 'Latin' and $val =~ /[\x80-\xff]/) {
200 0         0 $iTXt = 1; # write as iTXt if it contains non-Latin special characters
201             } elsif ($$tagInfo{iTXt}) {
202 0         0 $iTXt = 1; # write as iTXt if specified in user-defined tag
203             }
204             }
205 15 50       32 if ($comp) {
206 0         0 my $warn;
207 0 0       0 if (eval { require Compress::Zlib }) {
  0         0  
208 0         0 my $deflate = Compress::Zlib::deflateInit();
209 0 0       0 $compVal = $deflate->deflate($val) if $deflate;
210 0 0       0 if (defined $compVal) {
211 0         0 $compVal .= $deflate->flush();
212             # only compress if it actually saves space
213 0 0       0 unless (length($compVal) < length($val)) {
214 0         0 undef $compVal;
215 0         0 $warn = 'uncompressed data is smaller';
216             }
217             } else {
218 0         0 $warn = 'deflate error';
219             }
220             } else {
221 0         0 $warn = 'Compress::Zlib not available';
222             }
223             # warn if any user-specified compression fails
224 0 0 0     0 if ($warn and $comp == 1) {
225 0         0 $et->Warn("PNG:$$tagInfo{Name} not compressed ($warn)", 1);
226             }
227             }
228             # decide whether to write as iTXt, zTXt or tEXt
229 15 100       29 if ($iTXt) {
    50          
230 4         9 $$et{TextChunkType} = 'iTXt';
231 4 50 100     16 $xtra = (defined $compVal ? "\x01\0" : "\0\0") . ($lang || '') . "\0\0";
232             # iTXt is encoded as UTF-8 (but note that XMP is already UTF-8)
233 4 100       12 $val = $et->Encode($val, 'UTF8') if $iTXt == 1;
234             } elsif (defined $compVal) {
235 0         0 $$et{TextChunkType} = 'zTXt';
236 0         0 $xtra = "\0";
237             } else {
238 11         15 $$et{TextChunkType} = 'tEXt';
239 11         13 $xtra = '';
240             }
241 15 50       61 return $tag . "\0" . $xtra . (defined $compVal ? $compVal : $val);
242             }
243              
244             #------------------------------------------------------------------------------
245             # Add any outstanding new chunks to the PNG image
246             # Inputs: 0) ExifTool object ref, 1) output file or scalar ref
247             # 2-N) dirs to add (empty to add all except EXIF 'IFD0', including PNG tags)
248             # Returns: true on success
249             sub AddChunks($$;@)
250             {
251 20     20 0 34 my ($et, $outfile, @add) = @_;
252 20         26 my ($addTags, $tag, $dir, $err, $tagTablePtr, $specified);
253              
254 20 100       29 if (@add) {
255 10         18 $addTags = { }; # don't add any PNG tags
256 10         14 $specified = 1;
257             } else {
258 10         16 $addTags = $$et{ADD_PNG}; # add all PNG tags...
259 10         17 delete $$et{ADD_PNG}; # ...once
260             # add all directories
261 10         14 @add = sort keys %{$$et{ADD_DIRS}};
  10         46  
262             }
263             # write any outstanding PNG tags
264 20         57 foreach $tag (sort keys %$addTags) {
265 14         20 my $tagInfo = $$addTags{$tag};
266 14 100       28 next if $$tagInfo{FakeTag}; # (iCCP-name)
267 13         27 my $nvHash = $et->GetNewValueHash($tagInfo);
268             # (native PNG information is always preferred, so don't rely on just IsCreating)
269 13 50 33     32 next unless $$nvHash{IsCreating} or $et->IsOverwriting($nvHash) > 0;
270 13         39 my $val = $et->GetNewValue($nvHash);
271 13 50       22 if (defined $val) {
272 13 50       22 next if $$nvHash{EditOnly};
273 13         14 my $data;
274 13 100       39 if ($$tagInfo{Table} eq \%Image::ExifTool::PNG::TextualData) {
275 12         32 $data = BuildTextChunk($et, $tag, $tagInfo, $val, $$tagInfo{LangCode});
276 12         26 $data = $$et{TextChunkType} . $data;
277 12         20 delete $$et{TextChunkType};
278             } else {
279 1         3 $data = "$tag$val";
280             }
281 13         27 my $hdr = pack('N', length($data) - 4);
282 13         23 my $cbuf = pack('N', CalculateCRC(\$data, undef));
283 13 50       31 Write($outfile, $hdr, $data, $cbuf) or $err = 1;
284 13         48 $et->VerboseValue("+ PNG:$$tagInfo{Name}", $val);
285 13         28 ++$$et{CHANGED};
286             }
287             }
288             # create any necessary directories
289 20         36 foreach $dir (@add) {
290 48 100       88 next unless $$et{ADD_DIRS}{$dir}; # make sure we want to add it first
291 10         10 my $buff;
292 10         24 my %dirInfo = (
293             Parent => 'PNG',
294             DirName => $dir,
295             );
296 10 100       30 if ($dir eq 'IFD0') {
    50          
    100          
    50          
    100          
297 4 100       13 next unless $specified; # wait until specifically asked to write EXIF 'IFD0'
298 2         6 my $chunk = $stdCase{exif};
299             # (zxIf was not adopted)
300             #if ($et->Options('Compress')) {
301             # if (eval { require Compress::Zlib }) {
302             # $chunk = $stdCase{zxif};
303             # } else {
304             # $et->Warn("Creating uncompressed $stdCase{exif} chunk (Compress::Zlib not available)");
305             # }
306             #}
307 2         12 $et->VPrint(0, "Creating $chunk chunk:\n");
308 2         8 $$et{TIFF_TYPE} = 'APP1';
309 2         4 $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::Exif::Main');
310 2         8 $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr, \&Image::ExifTool::WriteTIFF);
311 2 50 33     9 if (defined $buff and length $buff) {
312 2 50       8 WriteProfile($outfile, $chunk, \$buff) or $err = 1;
313             }
314             } elsif ($dir eq 'XMP') {
315 0         0 $et->VPrint(0, "Creating XMP iTXt chunk:\n");
316 0         0 $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::XMP::Main');
317 0         0 $dirInfo{ReadOnly} = 1;
318 0         0 $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr);
319 0 0 0     0 if (defined $buff and length $buff and
      0        
320             # the packet is read-only (because of CRC)
321             Image::ExifTool::XMP::ValidateXMP(\$buff, 'r'))
322             {
323             # (previously, XMP was created as a non-standard XMP profile chunk)
324             # $buff = $Image::ExifTool::xmpAPP1hdr . $buff;
325             # WriteProfile($outfile, 'APP1', \$buff, 'generic') or $err = 1;
326             # (but now write XMP iTXt chunk according to XMP specification)
327 0         0 $buff = "iTXtXML:com.adobe.xmp\0\0\0\0\0" . $buff;
328 0         0 my $hdr = pack('N', length($buff) - 4);
329 0         0 my $cbuf = pack('N', CalculateCRC(\$buff, undef));
330 0 0       0 Write($outfile, $hdr, $buff, $cbuf) or $err = 1;
331             }
332             } elsif ($dir eq 'IPTC') {
333 1         5 $et->Warn('Creating non-standard IPTC in PNG', 1);
334 1         4 $et->VPrint(0, "Creating IPTC profile:\n");
335             # write new IPTC data (stored in a Photoshop directory)
336 1         2 $dirInfo{DirName} = 'Photoshop';
337 1         2 $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::Photoshop::Main');
338 1         4 $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr);
339 1 50 33     10 if (defined $buff and length $buff) {
340 1 50       3 WriteProfile($outfile, 'iptc', \$buff, 'IPTC') or $err = 1;
341             }
342             } elsif ($dir eq 'ICC_Profile') {
343 0         0 $et->VPrint(0, "Creating ICC profile:\n");
344             # write new ICC data (only done if we couldn't create iCCP chunk)
345 0         0 $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::ICC_Profile::Main');
346 0         0 $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr);
347 0 0 0     0 if (defined $buff and length $buff) {
348 0 0       0 WriteProfile($outfile, 'icm', \$buff, 'ICC') or $err = 1;
349 0         0 $et->Warn('Wrote ICC as a raw profile (no Compress::Zlib)');
350             }
351             } elsif ($dir eq 'PNG-pHYs') {
352 1         6 $et->VPrint(0, "Creating pHYs chunk (default 2834 pixels per meter):\n");
353 1         4 $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::PNG::PhysicalPixel');
354 1         2 my $blank = "\0\0\x0b\x12\0\0\x0b\x12\x01"; # 2834 pixels per meter (72 dpi)
355 1         3 $dirInfo{DataPt} = \$blank;
356 1         5 $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr);
357 1 50 33     6 if (defined $buff and length $buff) {
358 1         3 $buff = 'pHYs' . $buff; # CRC includes chunk name
359 1         3 my $hdr = pack('N', length($buff) - 4);
360 1         4 my $cbuf = pack('N', CalculateCRC(\$buff, undef));
361 1 50       4 Write($outfile, $hdr, $buff, $cbuf) or $err = 1;
362             }
363             } else {
364 4         7 next;
365             }
366 4         23 delete $$et{ADD_DIRS}{$dir}; # don't add again
367             }
368 20         77 return not $err;
369             }
370              
371              
372             1; # end
373              
374             __END__