Image/ExifTool/WriteCanonRaw.pl

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