source: gs2-extensions/parallel-building/trunk/src/perllib/cpan/Image/ExifTool/WriteCanonRaw.pl@ 24626

Last change on this file since 24626 was 24626, checked in by jmt12, 13 years ago

An (almost) complete copy of the perllib directory from a (circa SEP2011) head checkout from Greenstone 2 trunk - in order to try and make merging in this extension a little easier later on (as there have been some major changes to buildcol.pl commited in the main trunk but not in the x64 branch)

File size: 24.5 KB
Line 
1#------------------------------------------------------------------------------
2# File: WriteCanonRaw.pl
3#
4# Description: Write Canon RAW (CRW and CR2) meta information
5#
6# Revisions: 01/25/2005 - P. Harvey Created
7#------------------------------------------------------------------------------
8package Image::ExifTool::CanonRaw;
9
10use strict;
11use vars qw($VERSION $AUTOLOAD %crwTagFormat);
12use Image::ExifTool::Fixup;
13
14# map for adding directories to CRW
15my %crwMap = (
16 XMP => 'CanonVRD',
17 CanonVRD => 'Trailer',
18);
19
20# mappings to from RAW tagID to MakerNotes tagID
21# (Note: upper two bits of RawTagID are zero)
22my %mapRawTag = (
23 # RawTagID => Canon TagID
24 0x080b => 0x07, # CanonFirmwareVersion
25 0x0810 => 0x09, # OwnerName
26 0x0815 => 0x06, # CanonImageType
27 0x1028 => 0x03, # (unknown if no tag name specified)
28 0x1029 => 0x02, # FocalLength
29 0x102a => 0x04, # CanonShotInfo
30 0x102d => 0x01, # CanonCameraSettings
31 0x1033 => 0x0f, # CanonCustomFunctions (only verified for 10D)
32 0x1038 => 0x12, # CanonAFInfo
33 0x1039 => 0x13,
34 0x1093 => 0x93,
35 0x10a8 => 0xa8,
36 0x10a9 => 0xa9, # WhiteBalanceTable
37 0x10aa => 0xaa,
38 0x10ae => 0xae, # ColorTemperature
39 0x10b4 => 0xb4, # ColorSpace
40 0x10b5 => 0xb5,
41 0x10c0 => 0xc0,
42 0x10c1 => 0xc1,
43 0x180b => 0x0c, # SerialNumber
44 0x1817 => 0x08, # FileNumber
45 0x1834 => 0x10,
46 0x183b => 0x15,
47);
48# translation from Rotation to Orientation values
49my %mapRotation = (
50 0 => 1,
51 90 => 6,
52 180 => 3,
53 270 => 8,
54);
55
56
57#------------------------------------------------------------------------------
58# Initialize buffers for building MakerNotes from RAW data
59# Inputs: 0) ExifTool object reference
60sub InitMakerNotes($)
61{
62 my $exifTool = shift;
63 $exifTool->{MAKER_NOTE_INFO} = {
64 Entries => { }, # directory entries keyed by tagID
65 ValBuff => "\0\0\0\0", # value data buffer (start with zero nextIFD pointer)
66 FixupTags => { }, # flags for tags with data in value buffer
67 };
68}
69
70#------------------------------------------------------------------------------
71# Build maker notes from CanonRaw information
72# Inputs: 0) ExifTool object reference, 1) raw tag ID, 2) reference to tagInfo
73# 3) reference to value, 4) format name, 5) count
74# Notes: This will build the directory in the order the tags are found in the CRW
75# file, which isn't sequential (but Canon's version isn't sequential either...)
76sub BuildMakerNotes($$$$$$)
77{
78 my ($exifTool, $rawTag, $tagInfo, $valuePt, $formName, $count) = @_;
79
80 my $tagID = $mapRawTag{$rawTag} || return;
81 $formName or warn(sprintf "No format for tag 0x%x!\n",$rawTag), return;
82 # special case: ignore user comment because it gets saved in EXIF
83 # (and has the same raw tagID as CanonFileDescription)
84 return if $tagInfo and $$tagInfo{Name} eq 'UserComment';
85 my $format = $Image::ExifTool::Exif::formatNumber{$formName};
86 my $fsiz = $Image::ExifTool::Exif::formatSize[$format];
87 my $size = length($$valuePt);
88 my $value;
89 if ($count and $size != $count * $fsiz) {
90 if ($size < $count * $fsiz) {
91 warn sprintf("Value too short for raw tag 0x%x\n",$rawTag);
92 return;
93 }
94 # shorten value appropriately
95 $size = $count * $fsiz;
96 $value = substr($$valuePt, 0, $size);
97 } else {
98 $count = $size / $fsiz;
99 $value = $$valuePt;
100 }
101 my $offsetVal;
102 my $makerInfo = $exifTool->{MAKER_NOTE_INFO};
103 if ($size > 4) {
104 my $len = length $makerInfo->{ValBuff};
105 $offsetVal = Set32u($len);
106 $makerInfo->{ValBuff} .= $value;
107 # pad to an even number of bytes
108 $size & 0x01 and $makerInfo->{ValBuff} .= "\0";
109 # set flag indicating that this tag needs a fixup
110 $makerInfo->{FixupTags}->{$tagID} = 1;
111 } else {
112 $offsetVal = $value;
113 $size < 4 and $offsetVal .= "\0" x (4 - $size);
114 }
115 $makerInfo->{Entries}->{$tagID} = Set16u($tagID) . Set16u($format) .
116 Set32u($count) . $offsetVal;
117}
118
119#------------------------------------------------------------------------------
120# Finish building and save MakerNotes
121# Inputs: 0) ExifTool object reference
122sub SaveMakerNotes($)
123{
124 my $exifTool = shift;
125 # save maker notes
126 my $makerInfo = $exifTool->{MAKER_NOTE_INFO};
127 delete $exifTool->{MAKER_NOTE_INFO};
128 my $dirEntries = $makerInfo->{Entries};
129 my $numEntries = scalar(keys %$dirEntries);
130 my $fixup = new Image::ExifTool::Fixup;
131 return unless $numEntries;
132 # build the MakerNotes directory
133 my $makerNotes = Set16u($numEntries);
134 my $tagID;
135 # write the entries in proper tag order (even though Canon doesn't do this...)
136 foreach $tagID (sort { $a <=> $b } keys %$dirEntries) {
137 $makerNotes .= $$dirEntries{$tagID};
138 next unless $makerInfo->{FixupTags}->{$tagID};
139 # add fixup for this pointer
140 $fixup->AddFixup(length($makerNotes) - 4);
141 }
142 # save position of maker notes for pointer fixups
143 $fixup->{Shift} += length($makerNotes);
144 $exifTool->{MAKER_NOTE_FIXUP} = $fixup;
145 $exifTool->{MAKER_NOTE_BYTE_ORDER} = GetByteOrder();
146 # add value data
147 $makerNotes .= $makerInfo->{ValBuff};
148 # get MakerNotes tag info
149 my $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::Exif::Main');
150 my $tagInfo = $exifTool->GetTagInfo($tagTablePtr, 0x927c);
151 # save the MakerNotes
152 $exifTool->FoundTag($tagInfo, $makerNotes);
153 # save the garbage collection some work later
154 delete $makerInfo->{Entries};
155 delete $makerInfo->{ValBuff};
156 delete $makerInfo->{FixupTags};
157 # also generate Orientation tag since Rotation isn't transferred from RAW info
158 my $rotation = $exifTool->GetValue('Rotation', 'ValueConv');
159 if (defined $rotation and defined $mapRotation{$rotation}) {
160 $tagInfo = $exifTool->GetTagInfo($tagTablePtr, 0x112);
161 $exifTool->FoundTag($tagInfo, $mapRotation{$rotation});
162 }
163}
164
165#------------------------------------------------------------------------------
166# Check CanonRaw information
167# Inputs: 0) ExifTool object reference, 1) tagInfo hash reference,
168# 2) raw value reference
169# Returns: error string or undef (and may change value) on success
170sub CheckCanonRaw($$$)
171{
172 my ($exifTool, $tagInfo, $valPtr) = @_;
173 my $tagName = $$tagInfo{Name};
174 if ($tagName eq 'JpgFromRaw' or $tagName eq 'ThumbnailImage') {
175 unless ($$valPtr =~ /^\xff\xd8/ or $exifTool->Options('IgnoreMinorErrors')) {
176 return '[minor] Not a valid image';
177 }
178 } else {
179 my $format = $$tagInfo{Format};
180 my $count = $$tagInfo{Count};
181 unless ($format) {
182 my $tagType = ($$tagInfo{TagID} >> 8) & 0x38;
183 $format = $crwTagFormat{$tagType};
184 }
185 $format and return Image::ExifTool::CheckValue($valPtr, $format, $count);
186 }
187 return undef;
188}
189
190#------------------------------------------------------------------------------
191# Write CR2 file
192# Inputs: 0) ExifTool ref, 1) dirInfo reference (must have read first 16 bytes)
193# 2) tag table reference
194# Returns: true on success
195sub WriteCR2($$$)
196{
197 my ($exifTool, $dirInfo, $tagTablePtr) = @_;
198 my $dataPt = $$dirInfo{DataPt} or return 0;
199 my $outfile = $$dirInfo{OutFile} or return 0;
200 $$dirInfo{RAF} or return 0;
201
202 # check CR2 signature
203 if ($$dataPt !~ /^.{8}CR\x02\0/s) {
204 my ($msg, $minor);
205 if ($$dataPt =~ /^.{8}CR/s) {
206 $msg = 'Unsupported Canon RAW file. May cause problems if rewritten';
207 $minor = 1;
208 } elsif ($$dataPt =~ /^.{8}\xba\xb0\xac\xbb/s) {
209 $msg = 'Can not currently write Canon 1D RAW images';
210 } else {
211 $msg = 'Unrecognized Canon RAW file';
212 }
213 return 0 if $exifTool->Error($msg, $minor);
214 }
215
216 # CR2 has a 16-byte header
217 $$dirInfo{NewDataPos} = 16;
218 my $newData = $exifTool->WriteDirectory($dirInfo, $tagTablePtr);
219 return 0 unless defined $newData;
220 unless ($$dirInfo{LastIFD}) {
221 $exifTool->Error("CR2 image IFD may not be deleted");
222 return 0;
223 }
224
225 if (length($newData)) {
226 # build 16 byte header for Canon RAW file
227 my $header = substr($$dataPt, 0, 16);
228 # set IFD0 pointer (may not be 16 if edited by PhotoMechanic)
229 Set32u(16, \$header, 4);
230 # last 4 bytes of header is pointer to last IFD
231 Set32u($$dirInfo{LastIFD}, \$header, 12);
232 Write($outfile, $header, $newData) or return 0;
233 undef $newData; # free memory
234
235 # copy over image data now if necessary
236 if (ref $$dirInfo{ImageData}) {
237 $exifTool->CopyImageData($$dirInfo{ImageData}, $outfile) or return 0;
238 delete $$dirInfo{ImageData};
239 }
240 }
241 return 1;
242}
243
244#------------------------------------------------------------------------------
245# Write CanonRaw (CRW) information
246# Inputs: 0) ExifTool object reference, 1) source dirInfo reference,
247# 2) tag table reference
248# Returns: true on success
249# Notes: Increments ExifTool CHANGED flag for each tag changed This routine is
250# different from all of the other write routines because Canon RAW files are
251# designed well! So it isn't necessary to buffer the data in memory before
252# writing it out. Therefore this routine doesn't return the directory data as
253# the rest of the Write routines do. Instead, it writes to the dirInfo
254# OutFile on the fly --> much faster, efficient, and less demanding on memory!
255sub WriteCanonRaw($$$)
256{
257 my ($exifTool, $dirInfo, $tagTablePtr) = @_;
258 $exifTool or return 1; # allow dummy access to autoload this package
259 my $blockStart = $$dirInfo{DirStart};
260 my $blockSize = $$dirInfo{DirLen};
261 my $raf = $$dirInfo{RAF} or return 0;
262 my $outfile = $$dirInfo{OutFile} or return 0;
263 my $outPos = $$dirInfo{OutPos} or return 0;
264 my $outBase = $outPos;
265 my $verbose = $exifTool->Options('Verbose');
266 my $out = $exifTool->Options('TextOut');
267 my ($buff, $tagInfo);
268
269 # 4 bytes at end of block give directory position within block
270 $raf->Seek($blockStart+$blockSize-4, 0) or return 0;
271 $raf->Read($buff, 4) == 4 or return 0;
272 my $dirOffset = Get32u(\$buff,0) + $blockStart;
273 $raf->Seek($dirOffset, 0) or return 0;
274 $raf->Read($buff, 2) == 2 or return 0;
275 my $entries = Get16u(\$buff,0); # get number of entries in directory
276 # read the directory (10 bytes per entry)
277 $raf->Read($buff, 10 * $entries) == 10 * $entries or return 0;
278 my $newDir = '';
279
280 # get hash of new information keyed by tagID
281 my $newTags = $exifTool->GetNewTagInfoHash($tagTablePtr);
282
283 # generate list of tags to add or delete (currently, we only allow JpgFromRaw
284 # and ThumbnailImage, to be added or deleted from the root CanonRaw directory)
285 my (@addTags, %delTag);
286 if ($$dirInfo{Nesting} == 0) {
287 my $tagID;
288 foreach $tagID (keys %$newTags) {
289 my $permanent = $newTags->{$tagID}->{Permanent};
290 push(@addTags, $tagID) if defined($permanent) and not $permanent;
291 }
292 }
293
294 my $index;
295 for ($index=0; ; ++$index) {
296 my ($pt, $tag, $size, $valuePtr, $ptr, $value);
297 if ($index<$entries) {
298 $pt = 10 * $index;
299 $tag = Get16u(\$buff, $pt);
300 $size = Get32u(\$buff, $pt+2);
301 $valuePtr = Get32u(\$buff, $pt+6);
302 $ptr = $valuePtr + $blockStart; # all pointers relative to block start
303 }
304 # add any required new tags
305 # NOTE: can't currently add tags where value is stored in directory
306 if (@addTags and (not defined($tag) or $tag >= $addTags[0])) {
307 my $addTag = shift @addTags;
308 $tagInfo = $$newTags{$addTag};
309 my $newVal = $exifTool->GetNewValues($tagInfo);
310 if (defined $newVal) {
311 # pad value to an even length (Canon ImageBrowser and ZoomBrowser
312 # version 6.1.1 have problems with odd-sized embedded JPEG images
313 # even if the value is padded to maintain alignment, so do this
314 # before calculating the size for the directory entry)
315 $newVal .= "\0" if length($newVal) & 0x01;
316 # add new directory entry
317 $newDir .= Set16u($addTag) . Set32u(length($newVal)) .
318 Set32u($outPos - $outBase);
319 # write new value data
320 Write($outfile, $newVal) or return 0;
321 $outPos += length($newVal); # update current position
322 $verbose > 1 and print $out " + CanonRaw:$$tagInfo{Name}\n";
323 ++$exifTool->{CHANGED};
324 }
325 # set flag to delete this tag if found later
326 $delTag{$addTag} = 1;
327 }
328 last unless defined $tag; # all done if no more directory entries
329 return 0 if $tag & 0x8000; # top bit should not be set
330 my $tagID = $tag & 0x3fff; # get tag ID
331 my $tagType = ($tag >> 8) & 0x38; # get tag type
332 my $valueInDir = ($tag & 0x4000); # flag for value in directory
333
334 my $tagInfo = $exifTool->GetTagInfo($tagTablePtr,$tagID);
335 my $format = $crwTagFormat{$tagType};
336 my ($count, $subdir);
337 if ($tagInfo) {
338 $subdir = $$tagInfo{SubDirectory};
339 $format = $$tagInfo{Format} if $$tagInfo{Format};
340 $count = $$tagInfo{Count};
341 }
342 if ($valueInDir) {
343 $size = 8;
344 $value = substr($buff, $pt+2, $size);
345 # set count to 1 by default for normal values in directory
346 $count = 1 if not defined $count and $format and
347 $format ne 'string' and not $subdir;
348 } else {
349 if ($tagType==0x28 or $tagType==0x30) {
350 # this type of tag specifies a raw subdirectory
351 my $name;
352 $tagInfo and $name = $$tagInfo{Name};
353 $name or $name = sprintf("CanonRaw_0x%.4x", $tagID);
354 my %subdirInfo = (
355 DirName => $name,
356 DataLen => 0,
357 DirStart => $ptr,
358 DirLen => $size,
359 Nesting => $$dirInfo{Nesting} + 1,
360 RAF => $raf,
361 Parent => $$dirInfo{DirName},
362 OutFile => $outfile,
363 OutPos => $outPos,
364 );
365 my $result = $exifTool->WriteDirectory(\%subdirInfo, $tagTablePtr);
366 return 0 unless $result;
367 # set size and pointer for this new directory
368 $size = $subdirInfo{OutPos} - $outPos;
369 $valuePtr = $outPos - $outBase;
370 $outPos = $subdirInfo{OutPos};
371 } else {
372 # verify that the value data is within this block
373 $valuePtr + $size <= $blockSize or return 0;
374 # read value from file
375 $raf->Seek($ptr, 0) or return 0;
376 $raf->Read($value, $size) == $size or return 0;
377 }
378 }
379 # set count from tagInfo count if necessary
380 if ($format and not $count) {
381 # set count according to format and size
382 my $fnum = $Image::ExifTool::Exif::formatNumber{$format};
383 my $fsiz = $Image::ExifTool::Exif::formatSize[$fnum];
384 $count = int($size / $fsiz);
385 }
386 # edit subdirectory if necessary
387 if ($tagInfo) {
388 if ($subdir and $$subdir{TagTable}) {
389 my $name = $$tagInfo{Name};
390 my $newTagTable = Image::ExifTool::GetTagTable($$subdir{TagTable});
391 return 0 unless $newTagTable;
392 my $subdirStart = 0;
393 #### eval Start ()
394 $subdirStart = eval $$subdir{Start} if $$subdir{Start};
395 my $dirData = \$value;
396 my %subdirInfo = (
397 Name => $name,
398 DataPt => $dirData,
399 DataLen => $size,
400 DirStart => $subdirStart,
401 DirLen => $size - $subdirStart,
402 Nesting => $$dirInfo{Nesting} + 1,
403 RAF => $raf,
404 Parent => $$dirInfo{DirName},
405 );
406 #### eval Validate ($dirData, $subdirStart, $size)
407 if (defined $$subdir{Validate} and not eval $$subdir{Validate}) {
408 $exifTool->Warn("Invalid $name data");
409 } else {
410 $subdir = $exifTool->WriteDirectory(\%subdirInfo, $newTagTable);
411 if (defined $subdir and length $subdir) {
412 if ($subdirStart) {
413 # add header before data directory
414 $value = substr($value, 0, $subdirStart) . $subdir;
415 } else {
416 $value = $subdir;
417 }
418 }
419 }
420 } elsif ($$newTags{$tagID}) {
421 if ($delTag{$tagID}) {
422 $verbose > 1 and print $out " - CanonRaw:$$tagInfo{Name}\n";
423 ++$exifTool->{CHANGED};
424 next; # next since we already added this tag
425 }
426 my $oldVal;
427 if ($format) {
428 $oldVal = ReadValue(\$value, 0, $format, $count, $size);
429 } else {
430 $oldVal = $value;
431 }
432 my $nvHash = $exifTool->GetNewValueHash($tagInfo);
433 if (Image::ExifTool::IsOverwriting($nvHash, $oldVal)) {
434 my $newVal = Image::ExifTool::GetNewValues($nvHash);
435 my $verboseVal;
436 $verboseVal = $newVal if $verbose > 1;
437 # convert to specified format if necessary
438 if (defined $newVal and $format) {
439 $newVal = WriteValue($newVal, $format, $count);
440 }
441 if (defined $newVal) {
442 $value = $newVal;
443 ++$exifTool->{CHANGED};
444 $exifTool->VerboseValue("- CanonRaw:$$tagInfo{Name}", $oldVal);
445 $exifTool->VerboseValue("+ CanonRaw:$$tagInfo{Name}", $verboseVal);
446 }
447 }
448 }
449 }
450 if ($valueInDir) {
451 my $len = length $value;
452 if ($len < 8) {
453 # pad with original garbage in case it contained something useful
454 $value .= substr($buff, $pt+2+8-$len, 8-$len);
455 } elsif ($len > 8) { # this shouldn't happen
456 warn "Value too long! -- trucated\n";
457 $value = substr($value, 0, 8);
458 }
459 # create new directory entry
460 $newDir .= Set16u($tag) . $value;
461 next; # all done this entry
462 }
463 if (defined $value) {
464 # don't allow value to change length unless Writable is 'resize'
465 my $writable = $$tagInfo{Writable};
466 my $diff = length($value) - $size;
467 if ($diff) {
468 if ($writable and $writable eq 'resize') {
469 $size += $diff; # allow size to change
470 } elsif ($diff > 0) {
471 $value .= ("\0" x $diff);
472 } else {
473 $value = substr($value, 0, $size);
474 }
475 }
476 # pad value if necessary to align on even-byte boundary (as per CIFF spec)
477 $value .= "\0" if $size & 0x01;
478 $valuePtr = $outPos - $outBase;
479 # write out value data
480 Write($outfile, $value) or return 0;
481 $outPos += length($value); # update current position in outfile
482 }
483 # create new directory entry
484 $newDir .= Set16u($tag) . Set32u($size) . Set32u($valuePtr);
485 }
486 # add the directory counts and offset to the directory start,
487 $entries = length($newDir) / 10;
488 $newDir = Set16u($entries) . $newDir . Set32u($outPos - $outBase);
489 # write directory data
490 Write($outfile, $newDir) or return 0;
491
492 # update current output file position in dirInfo
493 $$dirInfo{OutPos} = $outPos + length($newDir);
494 # save outfile directory start (needed for rewriting VRD trailer)
495 $$dirInfo{OutDirStart} = $outPos - $outBase;
496
497 return 1;
498}
499
500#------------------------------------------------------------------------------
501# write Canon RAW (CRW) file
502# Inputs: 0) ExifTool object reference, 1) dirInfo reference
503# Returns: 1 on success, 0 if this wasn't a valid CRW file,
504# or -1 if a write error occurred
505sub WriteCRW($$)
506{
507 my ($exifTool, $dirInfo) = @_;
508 my $outfile = $$dirInfo{OutFile};
509 my $raf = $$dirInfo{RAF};
510 my $rtnVal = 0;
511 my ($buff, $err, $sig);
512
513 $raf->Read($buff,2) == 2 or return 0;
514 SetByteOrder($buff) or return 0;
515 $raf->Read($buff,4) == 4 or return 0;
516 $raf->Read($sig,8) == 8 or return 0; # get file signature
517 $sig =~ /^HEAP(CCDR|JPGM)/ or return 0; # validate signature
518 my $type = $1;
519 my $hlen = Get32u(\$buff, 0); # get header length
520
521 if ($exifTool->{DEL_GROUP}->{MakerNotes}) {
522 if ($type eq 'CCDR') {
523 $exifTool->Error("Can't delete MakerNotes group in CRW file");
524 return 0;
525 } else {
526 ++$exifTool->{CHANGED};
527 return 1;
528 }
529 }
530 # make XMP the preferred group for CRW files
531 if ($$exifTool{FILE_TYPE} eq 'CRW') {
532 $exifTool->InitWriteDirs(\%crwMap, 'XMP');
533 }
534
535 # write header
536 $raf->Seek(0, 0) or return 0;
537 $raf->Read($buff, $hlen) == $hlen or return 0;
538 Write($outfile, $buff) or $err = 1;
539
540 $raf->Seek(0, 2) or return 0; # seek to end of file
541 my $filesize = $raf->Tell() or return 0;
542
543 # build directory information for main raw directory
544 my %dirInfo = (
545 DataLen => 0,
546 DirStart => $hlen,
547 DirLen => $filesize - $hlen,
548 Nesting => 0,
549 RAF => $raf,
550 Parent => 'CRW',
551 OutFile => $outfile,
552 OutPos => $hlen,
553 );
554 # process the raw directory
555 my $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::CanonRaw::Main');
556 my $success = $exifTool->WriteDirectory(\%dirInfo, $tagTablePtr);
557
558 my $trailPt;
559 while ($success) {
560 # check to see if trailer(s) exist(s)
561 my $trailInfo = Image::ExifTool::IdentifyTrailer($raf) or last;
562 # rewrite the trailer(s)
563 $buff = '';
564 $$trailInfo{OutFile} = \$buff;
565 $success = $exifTool->ProcessTrailers($trailInfo) or last;
566 $trailPt = $$trailInfo{OutFile};
567 # nothing to write if trailers were deleted
568 undef $trailPt if length($$trailPt) < 4;
569 last;
570 }
571 if ($success) {
572 # add CanonVRD trailer if writing as a block
573 $trailPt = $exifTool->AddNewTrailers($trailPt,'CanonVRD');
574 if (not $trailPt and $$exifTool{ADD_DIRS}{CanonVRD}) {
575 # create CanonVRD from scratch if necessary
576 my $outbuff = '';
577 my $saveOrder = GetByteOrder();
578 require Image::ExifTool::CanonVRD;
579 if (Image::ExifTool::CanonVRD::ProcessCanonVRD($exifTool, { OutFile => \$outbuff }) > 0) {
580 $trailPt = \$outbuff;
581 }
582 SetByteOrder($saveOrder);
583 }
584 # write trailer
585 if ($trailPt) {
586 # must append DirStart pointer to end of trailer
587 my $newDirStart = Set32u($dirInfo{OutDirStart});
588 my $len = length $$trailPt;
589 my $pad = ($len & 0x01) ? ' ' : ''; # add pad byte if necessary
590 Write($outfile, $pad, substr($$trailPt,0,$len-4), $newDirStart) or $err = 1;
591 }
592 $rtnVal = $err ? -1 : 1;
593 } else {
594 $exifTool->Error('Error rewriting CRW file');
595 }
596 return $rtnVal;
597}
598
5991; # end
600
601__END__
602
603=head1 NAME
604
605Image::ExifTool::WriteCanonRaw.pl - Write Canon RAW (CRW and CR2) information
606
607=head1 SYNOPSIS
608
609These routines are autoloaded by Image::ExifTool::CanonRaw.
610
611=head1 DESCRIPTION
612
613This file contains routines used by ExifTool to write Canon CRW and CR2
614files and metadata.
615
616=head1 NOTES
617
618The CRW format is a pleasure to work with. All pointer offsets are relative
619to the start of the data for each directory. If TIFF/EXIF had implemented
620pointers in this way, it would be MUCH easier to read and write TIFF/JPEG
621files, and would lead to far fewer problems with corrupted metadata.
622
623=head1 AUTHOR
624
625Copyright 2003-2011, Phil Harvey (phil at owl.phy.queensu.ca)
626
627This library is free software; you can redistribute it and/or modify it
628under the same terms as Perl itself.
629
630=head1 SEE ALSO
631
632L<Image::ExifTool::CanonRaw(3pm)|Image::ExifTool::CanonRaw>,
633L<Image::ExifTool(3pm)|Image::ExifTool>,
634L<http://owl.phy.queensu.ca/~phil/exiftool/canon_raw.html>
635
636=cut
Note: See TracBrowser for help on using the repository browser.