Image/ExifTool/Lytro.pm

filter: Statementstmt 93.7% 60 / 64 Branchbran 65.0% 26 / 40 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 79.4% 93 / 117 CC 24 SCAR 26.9
CC
24
Cov
81%
ProcessLFP
21.7
ExtractTags
11.0
BEGIN
1.0
CRAP 14.8
« Image/ExifTool/Fixup.pm ↑ Summary Image/ExifTool/MPF.pm »
1 #------------------------------------------------------------------------------
2 # File: Lytro.pm
3 #
4 # Description: Read Lytro LFP files
5 #
6 # Revisions: 2014-07-17 - P. Harvey Created
7 #
8 # References: 1) http://optics.miloush.net/lytro/TheFileFormat.aspx
9 #------------------------------------------------------------------------------
10  
11 package Image::ExifTool::Lytro;
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Image::ExifTool::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 $VERSION = '1.04';
19  
20 sub ExtractTags($$$);
21  
22 # Lytro LFP tags (ref PH)
23 1 %Image::ExifTool::Lytro::Main = (
24 GROUPS => { 2 => 'Camera' },
25 VARS => { ID_FMT => 'none' },
26 NOTES => q{
27 Tag definitions for Lytro Light Field Picture (LFP) files. ExifTool
28 extracts the full JSON metadata blocks, as well as breaking them down into
29 individual tags. All available tags are extracted from the JSON metadata,
30 even if they don't appear in the table below.
31 },
32 JSONMetadata => {
33 Notes => 'the full JSON-format metadata blocks',
34 Binary => 1,
35 List => 1,
36 },
37 EmbeddedImage => {
38 Notes => 'JPEG image embedded in LFP files written by Lytro Desktop',
39 Groups => { 2 => 'Preview' },
40 Binary => 1,
41 },
42 Type => { Name => 'CameraType' },
43 CameraMake => { Name => 'Make' },
44 CameraModel => { Name => 'Model', Description => 'Camera Model Name' },
45 CameraSerialNumber => { Name => 'SerialNumber'},
46 CameraFirmware => { Name => 'FirmwareVersion'},
47 DevicesAccelerometerSampleArrayTime => { Name => 'AccelerometerTime'},
48 DevicesAccelerometerSampleArrayX => { Name => 'AccelerometerX'},
49 DevicesAccelerometerSampleArrayY => { Name => 'AccelerometerY'},
50 DevicesAccelerometerSampleArrayZ => { Name => 'AccelerometerZ'},
51 DevicesClockZuluTime => {
52 Name => 'DateTimeOriginal',
53 Description => 'Date/Time Original',
54 Groups => { 2 => 'Time' },
55 ValueConv => 'require Image::ExifTool::XMP; Image::ExifTool::XMP::ConvertXMPDate($val)',
56 PrintConv => '$self->ConvertDateTime($val)',
57 },
58 DevicesLensFNumber => {
59 Name => 'FNumber',
60 PrintConv => 'Image::ExifTool::Exif::PrintFNumber($val)',
61 },
62 DevicesLensFocalLength => {
63 Name => 'FocalLength',
64 ValueConv => '$val * 1000', # convert from metres to mm
65 PrintConv => 'sprintf("%.1f mm",$val)',
66 },
67 DevicesLensTemperature => {
68 Name => 'LensTemperature',
69 PrintConv => 'sprintf("%.1f C",$val)',
70 },
71 DevicesSocTemperature => {
72 Name => 'SocTemperature',
73 PrintConv => 'sprintf("%.1f C",$val)',
74 },
75 DevicesShutterFrameExposureDuration => {
76 Name => 'FrameExposureTime',
77 PrintConv => 'Image::ExifTool::Exif::PrintExposureTime($val)',
78 },
79 DevicesShutterPixelExposureDuration => {
80 Name => 'ExposureTime',
81 PrintConv => 'Image::ExifTool::Exif::PrintExposureTime($val)',
82 },
83 DevicesSensorPixelPitch => {
84 Name => 'FocalPlaneXResolution',
85 Notes => 'Y resolution is the same as X resolution',
86 ValueConv => '25.4 / $val / 1000', # convert from metres to pixels/inch
87 },
88 DevicesSensorSensorSerial => { Name => 'SensorSerialNumber'},
89 DevicesSensorIso => { Name => 'ISO' },
90 ImageLimitExposureBias => { Groups => { 2 => 'Image' }, PrintConv => 'sprintf("%+.1f", $val)' },
91 ImageModulationExposureBias => { Groups => { 2 => 'Image' }, PrintConv => 'sprintf("%+.1f", $val)' },
92 ImageOrientation => {
93 Name => 'Orientation',
94 Groups => { 2 => 'Image' },
95 PrintConv => {
96 1 => 'Horizontal (normal)',
97 },
98 },
99 );
100  
101 #------------------------------------------------------------------------------
102 # Extract tags from a parsed JSON hash
103 # Inputs: 0) ExifTool ref, 1) tag hash ref, 2) base tag name
104 sub ExtractTags($$$)
105 44 ▶{
Subroutine ExtractTags: called CC=11 SCAR=24.0
Pod undocumented
106 44 my ($et, $meta, $parent) = @_;
107 44 ▶ ref $meta eq 'HASH' or $et->Warn('Invalid LFP metadata'), return;
Branch: 1 branch
BranchTrueFalseTotal
ref $meta eq "HASH" or $et->Warn("Invalid LFP metadata"), return 44 0 44
108 44 my ($key, $val, $name, $tagTablePtr);
109 44 foreach $key (Image::ExifTool::OrderedKeys($meta)) {
110 130 my $tag = $parent . ucfirst($key);
111 130 ▶ foreach $val (ref $$meta{$key} eq 'ARRAY' ? @{$$meta{$key}} : $$meta{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $meta->{$key} eq 'ARRAY' ? : 9 121 130
112 136 ▶ ref $val eq 'HASH' and ExtractTags($et, $val, $tag), next;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq "HASH" and &ExtractTags($et, $val, $tag), next 41 95 136
113 95 ▶ $tagTablePtr or $tagTablePtr = GetTagTable('Image::ExifTool::Lytro::Main');
Branch: 1 branch
BranchTrueFalseTotal
$tagTablePtr or $tagTablePtr = &GetTagTable("Image::ExifTool::Lytro::Main") 61 34 95
114 95 ▶ unless ($$tagTablePtr{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagTablePtr->{$tag}) 62 33 95
115 62 ($name = $tag) =~ s/[^-_a-zA-Z0-9](.?)/\U$1/g;
116 62 $name =~ s/ParametersVendorContentComLytroTags//;
117 62 my %tagInfo;
118 62 ▶ $tagInfo{Groups} = { 2 => 'Image' } unless $name =~ s/^Devices//;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ s/^Devices// 37 25 62
119 62 ▶ $tagInfo{List} = 1 if ref $$meta{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $meta->{$key} eq "ARRAY" 2 60 62
120 62 $tagInfo{Name} = $name;
121 62 ▶ my $str = $tag eq $name ? '' : " as $name";
Branch: 1 branch
BranchTrueFalseTotal
$tag eq $name ? : 35 27 62
122 62 $et->VPrint(0, " [adding $tag$str]\n");
123 62 AddTagToTable($tagTablePtr, $tag, \%tagInfo);
124 }
125 95 $et->HandleTag($tagTablePtr, $tag, $val);
126 }
127 }
128 }
129  
130 #------------------------------------------------------------------------------
131 # Process segments from a Lytro LFP image
132 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
133 # Returns: 1 on success, 0 if this wasn't a valid Lytro image
134 sub ProcessLFP($$)
135 1 ▶{
Subroutine ProcessLFP: called CC=14 SCAR=30.8
Pod undocumented
136 1 my ($et, $dirInfo) = @_;
137 1 my $raf = $$dirInfo{RAF};
138 1 my $verbose = $et->Options('Verbose');
139 1 my ($buff, $id);
140  
141 # validate the Lytro file header
142 1 ▶ return 0 unless $raf->Read($buff, 16) == 16 and $buff =~ /^\x89LFP\x0d\x0a\x1a\x0a/;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($buff, 16) == 16 and $buff =~ /^\x89LFP\x0d\x0a\x1a\x0a/ 0 1 1
Condition33%1/3
$raf->Read($buff, 16) == 16 and $buff =~ /^\x89LFP\x0d\x0a\x1a\x0a/
!ll&&!rl&&r
001
MC/DC0%0/2
$raf->Read($buff, 16) == 16 and $buff =~ /^\x89LFP\x0d\x0a\x1a\x0a/
$raf->Read($buff, 16) == 16 $buff =~ /^\x89LFP\x0d\x0a\x1a\x0a/
Truth table33%1/3
A and B
A= $raf->Read($buff, 16) == 16B= $buff =~ /^\x89LFP\x0d\x0a\x1a\x0a/
ABresult
0X0
100
111
143 1 $et->SetFileType(); # set the FileType tag
144 1 SetByteOrder('MM');
145 1 my $tagTablePtr = GetTagTable('Image::ExifTool::Lytro::Main');
146 1 ▶ while ($raf->Read($buff, 16) == 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raf->Read($buff, 16) == 16) 3 1 4
147 3 ▶ $buff =~ /^\x89LF/ or $et->Warn('LFP format error'), last;
Branch: 1 branch
BranchTrueFalseTotal
$buff =~ /^\x89LF/ or $et->Warn("LFP format error"), last 3 0 3
148 3 my $size = Get32u(\$buff, 12);
149 3 ▶ $size & 0x80000000 and $et->Warn('Invalid LFP segment size'), last;
Branch: 1 branch
BranchTrueFalseTotal
$size & 2147483648 and $et->Warn("Invalid LFP segment size"), last 0 3 3
150 3 ▶ $raf->Read($id, 80) == 80 or $et->Warn('Truncated LFP segment'), last; # ignore the sha1
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($id, 80) == 80 or $et->Warn("Truncated LFP segment"), last 3 0 3
151 3 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 3 3
152 0 $id =~ s/\0.*//s;
153 0 $et->VPrint(0, substr($buff,1,3), " segment ($size bytes, $id)\n");
154 }
155 3 ▶ if ($size > 20000000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 20000000) { } 0 3 3
156 0 ▶ $raf->Seek($size, 1) or $et->Warn('Seek error in LFP file'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($size, 1) or $et->Warn("Seek error in LFP file"), last 0 0 0
157 } else {
158 3 ▶ $raf->Read($buff,$size) == $size or $et->Warn('Truncated LFP data'), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, $size) == $size or $et->Warn("Truncated LFP data"), last 3 0 3
159 3 $et->VerboseDump(\$buff, Addr=>$raf->Tell()-$size);
160 3 ▶ if ($buff =~ /^\{\s+"/) { # JSON metadata?
Branch: 2 branches
BranchTrueFalseTotal
if ($buff =~ /^\{\s+"/) { } 3 0 3
elsif ($buff =~ /^\xff\xd8\xff/) { } 0 0 0
161 3 pos($buff) = 0;
162 3 $et->HandleTag($tagTablePtr, 'JSONMetadata', $buff);
163 3 my $meta = Image::ExifTool::Import::ReadJSONObject(undef, \$buff);
164 3 ExtractTags($et, $meta, '');
165 } elsif ($buff =~ /^\xff\xd8\xff/) { # embedded JPEG image?
166 0 $et->HandleTag($tagTablePtr, 'EmbeddedImage', $buff);
167 }
168 }
169 # skip padding if necessary
170 3 my $pad = 16 - ($size % 16);
171 3 ▶ $raf->Seek($pad, 1) if $pad != 16;
Branch: 1 branch
BranchTrueFalseTotal
if $pad != 16 3 0 3
172 }
173 1 return 1;
174 }
175  
176 1 1; # end
177  
178 __END__
« Image/ExifTool/Fixup.pm ↑ Summary Image/ExifTool/MPF.pm »