Ignore:
Timestamp:
2011-06-01T12:33:42+12:00 (13 years ago)
Author:
sjm84
Message:

Updating the ExifTool perl modules

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/cpan/Image/ExifTool/WritePNG.pl

    r16842 r24107  
    115115    if ($exifTool->{ADD_DIRS}->{ICC_Profile}) {
    116116        # write new ICC data
    117         my $tagTablePtr = GetTagTable('Image::ExifTool::ICC_Profile::Main');
     117        my $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::ICC_Profile::Main');
    118118        my %dirInfo = ( Parent => 'PNG', DirName => 'ICC_Profile' );
    119119        my $buff = $exifTool->WriteDirectory(\%dirInfo, $tagTablePtr);
     
    127127
    128128#------------------------------------------------------------------------------
     129# Generate tEXt, zTXt or iTXt data for writing
     130# Inputs: 0) ExifTool ref, 1) tagID, 2) tagInfo ref, 3) value string, 4) language code
     131# Returns: chunk data (not including 8-byte chunk header)
     132# Notes: Sets ExifTool TextChunkType member to the type of chunk written
     133sub BuildTextChunk($$$$$)
     134{
     135    my ($exifTool, $tag, $tagInfo, $val, $lang) = @_;
     136    my ($xtra, $compVal, $iTXt, $comp);
     137    if ($$tagInfo{SubDirectory}) {
     138        if ($$tagInfo{Name} eq 'XMP') {
     139            $iTXt = 2;      # write as iTXt but flag to avoid encoding
     140            # (never compress XMP)
     141        } else {
     142            $comp = 2;      # compress raw profile if possible
     143        }
     144    } else {
     145        # compress if specified
     146        $comp = 1 if $exifTool->Options('Compress');
     147        if ($lang) {
     148            $iTXt = 1;      # write as iTXt if it has a language code
     149            $tag =~ s/-$lang$//;    # remove language code from tagID
     150        } elsif ($$exifTool{OPTIONS}{Charset} ne 'Latin' and $val =~  /[\x80-\xff]/) {
     151            $iTXt = 1;      # write as iTXt if it contains non-Latin special characters
     152        }
     153    }
     154    if ($comp) {
     155        my $warn;
     156        if (eval 'require Compress::Zlib') {
     157            my $deflate = Compress::Zlib::deflateInit();
     158            $compVal = $deflate->deflate($val) if $deflate;
     159            if (defined $compVal) {
     160                $compVal .= $deflate->flush();
     161                # only compress if it actually saves space
     162                unless (length($compVal) < length($val)) {
     163                    undef $compVal;
     164                    $warn = 'uncompressed data is smaller';
     165                }
     166            } else {
     167                $warn = 'deflate error';
     168            }
     169        } else {
     170            $warn = 'Compress::Zlib not available';
     171        }
     172        # warn if any user-specified compression fails
     173        if ($warn and $comp == 1) {
     174            $exifTool->Warn("PNG:$$tagInfo{Name} not compressed ($warn)", 1);
     175        }
     176    }
     177    # decide whether to write as iTXt, zTXt or tEXt
     178    if ($iTXt) {
     179        $$exifTool{TextChunkType} = 'iTXt';
     180        $xtra = (defined $compVal ? "\x01\0" : "\0\0") . ($lang || '') . "\0\0";
     181        # iTXt is encoded as UTF-8 (but note that XMP is already UTF-8)
     182        $val = $exifTool->Encode($val, 'UTF8') if $iTXt == 1;
     183    } elsif (defined $compVal) {
     184        $$exifTool{TextChunkType} = 'zTXt';
     185        $xtra = "\0";
     186    } else {
     187        $$exifTool{TextChunkType} = 'tEXt';
     188        $xtra = '';
     189    }
     190    return $tag . "\0" . $xtra . (defined $compVal ? $compVal : $val);
     191}
     192
     193#------------------------------------------------------------------------------
    129194# Add any outstanding new chunks to the PNG image
    130195# Inputs: 0) ExifTool object ref, 1) output file or scalar ref
     
    140205    foreach $tag (sort keys %$addTags) {
    141206        my $tagInfo = $$addTags{$tag};
    142         my $newValueHash = $exifTool->GetNewValueHash($tagInfo);
     207        my $nvHash = $exifTool->GetNewValueHash($tagInfo);
    143208        # (always create native PNG information, so don't check IsCreating())
    144         next unless Image::ExifTool::IsOverwriting($newValueHash) > 0;
    145         my $val = Image::ExifTool::GetNewValues($newValueHash);
     209        next unless Image::ExifTool::IsOverwriting($nvHash) > 0;
     210        my $val = Image::ExifTool::GetNewValues($nvHash);
    146211        if (defined $val) {
    147212            my $data;
    148213            if ($$tagInfo{Table} eq \%Image::ExifTool::PNG::TextualData) {
    149                 $data = "tEXt$tag\0$val";
     214                $data = BuildTextChunk($exifTool, $tag, $tagInfo, $val, $$tagInfo{LangCode});
     215                $data = $$exifTool{TextChunkType} . $data;
     216                delete $$exifTool{TextChunkType};
    150217            } else {
    151218                $data = "$tag$val";
    152             }
    153             # write as compressed zTXt if specified
    154             if ($exifTool->Options('Compress')) {
    155                 my $warn;
    156                 if (eval 'require Compress::Zlib') {
    157                     my $buff;
    158                     my $deflate = Compress::Zlib::deflateInit();
    159                     $buff = $deflate->deflate($val) if $deflate;
    160                     if (defined $buff) {
    161                         $buff .= $deflate->flush();
    162                         # only write as zTXt if it actually saves space
    163                         if (length($buff) < length($val) - 1) {
    164                             $data = "zTXt$tag\0\0$buff";
    165                         } else {
    166                             $warn = 'uncompressed data is smaller';
    167                         }
    168                     } else {
    169                         $warn = 'deflate error';
    170                     }
    171                 } else {
    172                     $warn = 'Compress::Zlib not available';
    173                 }
    174                 $warn and $exifTool->Warn("PNG:$$tagInfo{Name} not compressed ($warn)", 1);
    175219            }
    176220            my $hdr = pack('N', length($data) - 4);
    177221            my $cbuf = pack('N', CalculateCRC(\$data, undef));
    178222            Write($outfile, $hdr, $data, $cbuf) or $err = 1;
    179             $exifTool->VPrint(1, "    + PNG:$$tagInfo{Name} = '",$exifTool->Printable($val),"'\n");
     223            $exifTool->VerboseValue("+ PNG:$$tagInfo{Name}", $val);
    180224            ++$exifTool->{CHANGED};
    181225        }
     
    192236            $exifTool->VPrint(0, "Creating EXIF profile:\n");
    193237            $exifTool->{TIFF_TYPE} = 'APP1';
    194             $tagTablePtr = GetTagTable('Image::ExifTool::Exif::Main');
    195             # use specified byte ordering or ordering from maker notes if set
    196             my $byteOrder = $exifTool->Options('ByteOrder') ||
    197                 $exifTool->GetNewValues('ExifByteOrder') || $exifTool->{MAKER_NOTE_BYTE_ORDER} || 'MM';
    198             unless (SetByteOrder($byteOrder)) {
    199                 warn "Invalid byte order '$byteOrder'\n";
    200                 $byteOrder = $exifTool->{MAKER_NOTE_BYTE_ORDER} || 'MM';
    201                 SetByteOrder($byteOrder);
    202             }
    203             $dirInfo{NewDataPos} = 8,   # new data will come after TIFF header
    204             $buff = $exifTool->WriteDirectory(\%dirInfo, $tagTablePtr);
     238            $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::Exif::Main');
     239            $buff = $exifTool->WriteDirectory(\%dirInfo, $tagTablePtr, \&Image::ExifTool::WriteTIFF);
    205240            if (defined $buff and length $buff) {
    206                 my $tiffHdr = $byteOrder . Set16u(42) . Set32u(8);
    207                 $buff = $Image::ExifTool::exifAPP1hdr . $tiffHdr . $buff;
     241                $buff = $Image::ExifTool::exifAPP1hdr . $buff;
    208242                WriteProfile($outfile, 'APP1', \$buff, 'generic') or $err = 1;
    209243            }
    210244        } elsif ($dir eq 'XMP') {
    211245            $exifTool->VPrint(0, "Creating XMP iTXt chunk:\n");
    212             $tagTablePtr = GetTagTable('Image::ExifTool::XMP::Main');
     246            $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::XMP::Main');
    213247            $dirInfo{ReadOnly} = 1;
    214248            $buff = $exifTool->WriteDirectory(\%dirInfo, $tagTablePtr);
     
    229263            $exifTool->VPrint(0, "Creating IPTC profile:\n");
    230264            # write new IPTC data
    231             $tagTablePtr = GetTagTable('Image::ExifTool::Photoshop::Main');
     265            $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::Photoshop::Main');
    232266            $buff = $exifTool->WriteDirectory(\%dirInfo, $tagTablePtr);
    233267            if (defined $buff and length $buff) {
     
    237271            $exifTool->VPrint(0, "Creating ICC profile:\n");
    238272            # write new ICC data (only done if we couldn't create iCCP chunk)
    239             $tagTablePtr = GetTagTable('Image::ExifTool::ICC_Profile::Main');
     273            $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::ICC_Profile::Main');
    240274            $buff = $exifTool->WriteDirectory(\%dirInfo, $tagTablePtr);
    241275            if (defined $buff and length $buff) {
    242276                WriteProfile($outfile, 'icm', \$buff, 'ICC') or $err = 1;
    243                 $exifTool->Warn('Wrote ICC as generic profile (no Compress::Zlib)');
     277                $exifTool->Warn('Wrote ICC as a raw profile (no Compress::Zlib)');
    244278            }
    245279        }
     
    282316=head1 AUTHOR
    283317
    284 Copyright 2003-2007, Phil Harvey (phil at owl.phy.queensu.ca)
     318Copyright 2003-2011, Phil Harvey (phil at owl.phy.queensu.ca)
    285319
    286320This library is free software; you can redistribute it and/or modify it
Note: See TracChangeset for help on using the changeset viewer.