| 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 |  | 8 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 2717 |  | 
| 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 | 210 | my ($dataPt, $crc, $pos, $len) = @_; | 
| 23 | 98 | 100 |  |  |  | 233 | $crc = 0 unless defined $crc; | 
| 24 | 98 | 100 |  |  |  | 217 | $pos = 0 unless defined $pos; | 
| 25 | 98 | 50 |  |  |  | 220 | $len = length($$dataPt) - $pos unless defined $len; | 
| 26 | 98 |  |  |  |  | 157 | $crc ^= 0xffffffff;         # undo 1's complement | 
| 27 |  |  |  |  |  |  | # build lookup table unless done already | 
| 28 | 98 | 100 |  |  |  | 213 | unless (@crcTable) { | 
| 29 | 1 |  |  |  |  | 4 | my ($c, $n, $k); | 
| 30 | 1 |  |  |  |  | 4 | for ($n=0; $n<256; ++$n) { | 
| 31 | 256 |  |  |  |  | 443 | for ($k=0, $c=$n; $k<8; ++$k) { | 
| 32 | 2048 | 100 |  |  |  | 3901 | $c = ($c & 1) ? 0xedb88320 ^ ($c >> 1) : $c >> 1; | 
| 33 |  |  |  |  |  |  | } | 
| 34 | 256 |  |  |  |  | 480 | $crcTable[$n] = $c; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  | # calculate the CRC | 
| 38 | 98 |  |  |  |  | 976 | foreach (unpack("x${pos}C$len", $$dataPt)) { | 
| 39 | 11061 |  |  |  |  | 17396 | $crc = $crcTable[($crc^$_) & 0xff] ^ ($crc >> 8); | 
| 40 |  |  |  |  |  |  | } | 
| 41 | 98 |  |  |  |  | 458 | 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 | 5 | my $dataPt = shift; | 
| 51 | 1 |  |  |  |  | 5 | my $len = length($$dataPt); | 
| 52 | 1 |  |  |  |  | 4 | my $hex = ''; | 
| 53 | 1 |  |  |  |  | 2 | my $pos; | 
| 54 | 1 |  |  |  |  | 8 | for ($pos = 0; $pos < $len; $pos += 36) { | 
| 55 | 13 |  |  |  |  | 22 | my $n = $len - $pos; | 
| 56 | 13 | 100 |  |  |  | 25 | $n > 36 and $n = 36; | 
| 57 | 13 |  |  |  |  | 50 | $hex .= unpack('H*',substr($$dataPt,$pos,$n)) . "\n"; | 
| 58 |  |  |  |  |  |  | } | 
| 59 | 1 |  |  |  |  | 8 | 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 | 17 | my ($outfile, $rawType, $dataPt, $profile) = @_; | 
| 70 | 4 |  |  |  |  | 12 | my ($buff, $prefix, $chunk, $deflate); | 
| 71 | 4 | 100 | 66 |  |  | 29 | if ($rawType ne $stdCase{exif} and eval { require Compress::Zlib }) { | 
|  | 2 |  |  |  |  | 912 |  | 
| 72 | 2 |  |  |  |  | 66488 | $deflate = Compress::Zlib::deflateInit(); | 
| 73 |  |  |  |  |  |  | } | 
| 74 | 4 | 100 |  |  |  | 1045 | if (not defined $profile) { | 
| 75 |  |  |  |  |  |  | # write ICC profile as compressed iCCP chunk if possible | 
| 76 | 3 | 100 |  |  |  | 11 | if (ref $rawType) { | 
| 77 | 1 | 50 |  |  |  | 6 | return 0 unless $deflate; | 
| 78 | 1 |  |  |  |  | 4 | $chunk = 'iCCP'; | 
| 79 | 1 |  |  |  |  | 3 | $prefix = "$$rawType\0\0"; | 
| 80 |  |  |  |  |  |  | } else { | 
| 81 | 2 |  |  |  |  | 5 | $chunk = $rawType; | 
| 82 | 2 | 50 |  |  |  | 9 | if ($rawType eq $stdCase{zxif}) { | 
| 83 | 0 |  |  |  |  | 0 | $prefix = "\0" . pack('N', length $$dataPt); # (proposed compressed EXIF) | 
| 84 |  |  |  |  |  |  | } else { | 
| 85 | 2 |  |  |  |  | 6 | $prefix = '';   # standard EXIF | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  | } | 
| 88 | 3 | 100 |  |  |  | 11 | if ($deflate) { | 
| 89 | 1 |  |  |  |  | 7 | $buff = $deflate->deflate($$dataPt); | 
| 90 | 1 | 50 |  |  |  | 50 | return 0 unless defined $buff; | 
| 91 | 1 |  |  |  |  | 6 | $buff .= $deflate->flush(); | 
| 92 | 1 |  |  |  |  | 75 | $dataPt = \$buff; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | } else { | 
| 95 |  |  |  |  |  |  | # write as ASCII-hex encoded profile in tEXt or zTXt chunk | 
| 96 | 1 |  |  |  |  | 17 | my $txtHdr = sprintf("\n$profile profile\n%8d\n", length($$dataPt)); | 
| 97 | 1 |  |  |  |  | 21 | $buff = $txtHdr . HexEncode($dataPt); | 
| 98 | 1 |  |  |  |  | 3 | $chunk = 'tEXt';         # write as tEXt if deflate not available | 
| 99 | 1 |  |  |  |  | 5 | $prefix = "Raw profile type $rawType\0"; | 
| 100 | 1 |  |  |  |  | 4 | $dataPt = \$buff; | 
| 101 |  |  |  |  |  |  | # write profile as zTXt chunk if possible | 
| 102 | 1 | 50 |  |  |  | 10 | if ($deflate) { | 
| 103 | 1 |  |  |  |  | 6 | my $buf2 = $deflate->deflate($buff); | 
| 104 | 1 | 50 |  |  |  | 89 | if (defined $buf2) { | 
| 105 | 1 |  |  |  |  | 4 | $dataPt = \$buf2; | 
| 106 | 1 |  |  |  |  | 6 | $buf2 .= $deflate->flush(); | 
| 107 | 1 |  |  |  |  | 110 | $chunk = 'zTXt'; | 
| 108 | 1 |  |  |  |  | 4 | $prefix .= "\0";    # compression type byte (0=deflate) | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  | } | 
| 112 | 4 |  |  |  |  | 36 | my $hdr = pack('Na4', length($prefix) + length($$dataPt), $chunk) . $prefix; | 
| 113 | 4 |  |  |  |  | 21 | my $crc = CalculateCRC(\$hdr, undef, 4); | 
| 114 | 4 |  |  |  |  | 13 | $crc = CalculateCRC($dataPt, $crc); | 
| 115 | 4 |  |  |  |  | 40 | 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 | 27 | my ($et, $outfile) = @_; | 
| 125 | 10 | 100 |  |  |  | 38 | if ($$et{ADD_DIRS}{ICC_Profile}) { | 
| 126 |  |  |  |  |  |  | # write new ICC data | 
| 127 | 1 |  |  |  |  | 6 | my $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::ICC_Profile::Main'); | 
| 128 | 1 |  |  |  |  | 6 | my %dirInfo = ( Parent => 'PNG', DirName => 'ICC_Profile' ); | 
| 129 | 1 |  |  |  |  | 7 | my $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr); | 
| 130 | 1 | 50 | 33 |  |  | 8 | if (defined $buff and length $buff) { | 
| 131 | 1 |  |  |  |  | 6 | my $profileName = $et->GetNewValue($Image::ExifTool::PNG::Main{'iCCP-name'}); | 
| 132 | 1 | 50 |  |  |  | 5 | $profileName = 'icm' unless defined $profileName; | 
| 133 | 1 | 50 |  |  |  | 7 | if (WriteProfile($outfile, \$profileName, \$buff)) { | 
| 134 | 1 |  |  |  |  | 10 | $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 |  |  |  | 37 | if ($$et{ADD_PNG}) { | 
| 141 | 5 |  |  |  |  | 11 | my ($tag, %addBeforePLTE); | 
| 142 | 5 |  |  |  |  | 15 | foreach $tag (qw(sRGB gAMA)) { | 
| 143 | 10 | 50 |  |  |  | 41 | 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 |  |  |  | 20 | 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 |  |  |  |  | 26 | 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 | 14 | my ($et, $dir, $outBuff, $nonStandard) = @_; | 
| 165 | 3 |  |  |  |  | 10 | my $saveDir = $dir; | 
| 166 | 3 | 50 |  |  |  | 41 | $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 |  |  | 14 | if (not $nonStandard) { | 
|  |  | 0 |  |  |  |  |  | 
| 169 | 3 |  |  |  |  | 14 | delete $$et{ADD_DIRS}{$dir}; | 
| 170 | 3 | 50 |  |  |  | 19 | 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 | 54 | my ($et, $tag, $tagInfo, $val, $lang) = @_; | 
| 185 | 15 |  |  |  |  | 30 | my ($xtra, $compVal, $iTXt, $comp); | 
| 186 | 15 | 100 |  |  |  | 36 | if ($$tagInfo{SubDirectory}) { | 
| 187 | 3 | 50 |  |  |  | 16 | if ($$tagInfo{Name} eq 'XMP') { | 
| 188 | 3 |  |  |  |  | 7 | $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 |  |  |  | 40 | $comp = 1 if $et->Options('Compress'); | 
| 196 | 12 | 100 | 66 |  |  | 77 | if ($lang) { | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 197 | 1 |  |  |  |  | 3 | $iTXt = 1;      # write as iTXt if it has a language code | 
| 198 | 1 |  |  |  |  | 23 | $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 |  |  |  | 45 | 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 |  |  |  | 34 | if ($iTXt) { | 
|  |  | 50 |  |  |  |  |  | 
| 230 | 4 |  |  |  |  | 13 | $$et{TextChunkType} = 'iTXt'; | 
| 231 | 4 | 50 | 100 |  |  | 29 | $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 |  |  |  | 20 | $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 |  |  |  |  | 23 | $$et{TextChunkType} = 'tEXt'; | 
| 239 | 11 |  |  |  |  | 19 | $xtra = ''; | 
| 240 |  |  |  |  |  |  | } | 
| 241 | 15 | 50 |  |  |  | 67 | 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 | 49 | my ($et, $outfile, @add) = @_; | 
| 252 | 20 |  |  |  |  | 41 | my ($addTags, $tag, $dir, $err, $tagTablePtr, $specified); | 
| 253 |  |  |  |  |  |  |  | 
| 254 | 20 | 100 |  |  |  | 48 | if (@add) { | 
| 255 | 10 |  |  |  |  | 21 | $addTags = { }; # don't add any PNG tags | 
| 256 | 10 |  |  |  |  | 22 | $specified = 1; | 
| 257 |  |  |  |  |  |  | } else { | 
| 258 | 10 |  |  |  |  | 21 | $addTags = $$et{ADD_PNG};    # add all PNG tags... | 
| 259 | 10 |  |  |  |  | 23 | delete $$et{ADD_PNG};        # ...once | 
| 260 |  |  |  |  |  |  | # add all directories | 
| 261 | 10 |  |  |  |  | 17 | @add = sort keys %{$$et{ADD_DIRS}}; | 
|  | 10 |  |  |  |  | 74 |  | 
| 262 |  |  |  |  |  |  | } | 
| 263 |  |  |  |  |  |  | # write any outstanding PNG tags | 
| 264 | 20 |  |  |  |  | 68 | foreach $tag (sort keys %$addTags) { | 
| 265 | 14 |  |  |  |  | 31 | my $tagInfo = $$addTags{$tag}; | 
| 266 | 14 | 100 |  |  |  | 39 | next if $$tagInfo{FakeTag}; # (iCCP-name) | 
| 267 | 13 |  |  |  |  | 39 | my $nvHash = $et->GetNewValueHash($tagInfo); | 
| 268 |  |  |  |  |  |  | # (native PNG information is always preferred, so don't rely on just IsCreating) | 
| 269 | 13 | 50 | 33 |  |  | 42 | next unless $$nvHash{IsCreating} or $et->IsOverwriting($nvHash) > 0; | 
| 270 | 13 |  |  |  |  | 35 | my $val = $et->GetNewValue($nvHash); | 
| 271 | 13 | 50 |  |  |  | 48 | if (defined $val) { | 
| 272 | 13 | 50 |  |  |  | 32 | next if $$nvHash{EditOnly}; | 
| 273 | 13 |  |  |  |  | 19 | my $data; | 
| 274 | 13 | 100 |  |  |  | 44 | if ($$tagInfo{Table} eq \%Image::ExifTool::PNG::TextualData) { | 
| 275 | 12 |  |  |  |  | 49 | $data = BuildTextChunk($et, $tag, $tagInfo, $val, $$tagInfo{LangCode}); | 
| 276 | 12 |  |  |  |  | 41 | $data = $$et{TextChunkType} . $data; | 
| 277 | 12 |  |  |  |  | 22 | delete $$et{TextChunkType}; | 
| 278 |  |  |  |  |  |  | } else { | 
| 279 | 1 |  |  |  |  | 4 | $data = "$tag$val"; | 
| 280 |  |  |  |  |  |  | } | 
| 281 | 13 |  |  |  |  | 40 | my $hdr = pack('N', length($data) - 4); | 
| 282 | 13 |  |  |  |  | 35 | my $cbuf = pack('N', CalculateCRC(\$data, undef)); | 
| 283 | 13 | 50 |  |  |  | 43 | Write($outfile, $hdr, $data, $cbuf) or $err = 1; | 
| 284 | 13 |  |  |  |  | 66 | $et->VerboseValue("+ PNG:$$tagInfo{Name}", $val); | 
| 285 | 13 |  |  |  |  | 35 | ++$$et{CHANGED}; | 
| 286 |  |  |  |  |  |  | } | 
| 287 |  |  |  |  |  |  | } | 
| 288 |  |  |  |  |  |  | # create any necessary directories | 
| 289 | 20 |  |  |  |  | 41 | foreach $dir (@add) { | 
| 290 | 48 | 100 |  |  |  | 135 | next unless $$et{ADD_DIRS}{$dir}; # make sure we want to add it first | 
| 291 | 10 |  |  |  |  | 17 | my $buff; | 
| 292 | 10 |  |  |  |  | 32 | my %dirInfo = ( | 
| 293 |  |  |  |  |  |  | Parent => 'PNG', | 
| 294 |  |  |  |  |  |  | DirName => $dir, | 
| 295 |  |  |  |  |  |  | ); | 
| 296 | 10 | 100 |  |  |  | 61 | if ($dir eq 'IFD0') { | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 297 | 4 | 100 |  |  |  | 16 | next unless $specified;     # wait until specifically asked to write EXIF 'IFD0' | 
| 298 | 2 |  |  |  |  | 9 | 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 |  |  |  |  | 13 | $et->VPrint(0, "Creating $chunk chunk:\n"); | 
| 308 | 2 |  |  |  |  | 13 | $$et{TIFF_TYPE} = 'APP1'; | 
| 309 | 2 |  |  |  |  | 20 | $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::Exif::Main'); | 
| 310 | 2 |  |  |  |  | 18 | $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr, \&Image::ExifTool::WriteTIFF); | 
| 311 | 2 | 50 | 33 |  |  | 22 | if (defined $buff and length $buff) { | 
| 312 | 2 | 50 |  |  |  | 11 | 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 |  |  |  |  | 8 | $et->Warn('Creating non-standard IPTC in PNG', 1); | 
| 334 | 1 |  |  |  |  | 6 | $et->VPrint(0, "Creating IPTC profile:\n"); | 
| 335 |  |  |  |  |  |  | # write new IPTC data (stored in a Photoshop directory) | 
| 336 | 1 |  |  |  |  | 4 | $dirInfo{DirName} = 'Photoshop'; | 
| 337 | 1 |  |  |  |  | 6 | $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::Photoshop::Main'); | 
| 338 | 1 |  |  |  |  | 7 | $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr); | 
| 339 | 1 | 50 | 33 |  |  | 9 | if (defined $buff and length $buff) { | 
| 340 | 1 | 50 |  |  |  | 6 | 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 |  |  |  |  | 11 | $et->VPrint(0, "Creating pHYs chunk (default 2834 pixels per meter):\n"); | 
| 353 | 1 |  |  |  |  | 6 | $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::PNG::PhysicalPixel'); | 
| 354 | 1 |  |  |  |  | 4 | my $blank = "\0\0\x0b\x12\0\0\x0b\x12\x01"; # 2834 pixels per meter (72 dpi) | 
| 355 | 1 |  |  |  |  | 5 | $dirInfo{DataPt} = \$blank; | 
| 356 | 1 |  |  |  |  | 9 | $buff = $et->WriteDirectory(\%dirInfo, $tagTablePtr); | 
| 357 | 1 | 50 | 33 |  |  | 9 | if (defined $buff and length $buff) { | 
| 358 | 1 |  |  |  |  | 6 | $buff = 'pHYs' . $buff; # CRC includes chunk name | 
| 359 | 1 |  |  |  |  | 4 | my $hdr = pack('N', length($buff) - 4); | 
| 360 | 1 |  |  |  |  | 4 | my $cbuf = pack('N', CalculateCRC(\$buff, undef)); | 
| 361 | 1 | 50 |  |  |  | 7 | Write($outfile, $hdr, $buff, $cbuf) or $err = 1; | 
| 362 |  |  |  |  |  |  | } | 
| 363 |  |  |  |  |  |  | } else { | 
| 364 | 4 |  |  |  |  | 13 | next; | 
| 365 |  |  |  |  |  |  | } | 
| 366 | 4 |  |  |  |  | 29 | delete $$et{ADD_DIRS}{$dir};  # don't add again | 
| 367 |  |  |  |  |  |  | } | 
| 368 | 20 |  |  |  |  | 112 | return not $err; | 
| 369 |  |  |  |  |  |  | } | 
| 370 |  |  |  |  |  |  |  | 
| 371 |  |  |  |  |  |  |  | 
| 372 |  |  |  |  |  |  | 1; # end | 
| 373 |  |  |  |  |  |  |  | 
| 374 |  |  |  |  |  |  | __END__ |