File Coverage

blib/lib/Image/ExifTool/PanasonicRaw.pm
Criterion Covered Total %
statement 112 130 86.1
branch 24 48 50.0
condition 12 34 35.2
subroutine 10 10 100.0
pod 0 6 0.0
total 158 228 69.3


line stmt bran cond sub pod time code
1             #------------------------------------------------------------------------------
2             # File: PanasonicRaw.pm
3             #
4             # Description: Read/write Panasonic/Leica RAW/RW2/RWL meta information
5             #
6             # Revisions: 2009/03/24 - P. Harvey Created
7             # 2009/05/12 - PH Added RWL file type (same format as RW2)
8             #
9             # References: 1) https://exiftool.org/forum/index.php/topic,1542.0.html
10             # 2) http://www.cybercom.net/~dcoffin/dcraw/
11             # 3) http://syscall.eu/#pana
12             # 4) Klaus Homeister private communication
13             # IB) Iliah Borg private communication (LibRaw)
14             # JD) Jens Duttke private communication (TZ3,FZ30,FZ50)
15             #------------------------------------------------------------------------------
16              
17             package Image::ExifTool::PanasonicRaw;
18              
19 14     14   120 use strict;
  14         40  
  14         658  
20 14     14   99 use vars qw($VERSION);
  14         68  
  14         781  
21 14     14   119 use Image::ExifTool qw(:DataAccess :Utils);
  14         40  
  14         3680  
22 14     14   151 use Image::ExifTool::Exif;
  14         50  
  14         38515  
23              
24             $VERSION = '1.27';
25              
26             sub ProcessJpgFromRaw($$$);
27             sub WriteJpgFromRaw($$$);
28             sub WriteDistortionInfo($$$);
29             sub ProcessDistortionInfo($$$);
30              
31             my %jpgFromRawMap = (
32             IFD1 => 'IFD0',
33             EXIF => 'IFD0', # to write EXIF as a block
34             ExifIFD => 'IFD0',
35             GPS => 'IFD0',
36             SubIFD => 'IFD0',
37             GlobParamIFD => 'IFD0',
38             PrintIM => 'IFD0',
39             InteropIFD => 'ExifIFD',
40             MakerNotes => 'ExifIFD',
41             IFD0 => 'APP1',
42             MakerNotes => 'ExifIFD',
43             Comment => 'COM',
44             );
45              
46             my %wbTypeInfo = (
47             PrintConv => \%Image::ExifTool::Exif::lightSource,
48             SeparateTable => 'EXIF LightSource',
49             );
50              
51             my %panasonicWhiteBalance = ( #forum9396
52             0 => 'Auto',
53             1 => 'Daylight',
54             2 => 'Cloudy',
55             3 => 'Tungsten',
56             4 => 'n/a',
57             5 => 'Flash',
58             6 => 'n/a',
59             7 => 'n/a',
60             8 => 'Custom#1',
61             9 => 'Custom#2',
62             10 => 'Custom#3',
63             11 => 'Custom#4',
64             12 => 'Shade',
65             13 => 'Kelvin',
66             16 => 'AWBc', # GH5 and G9 (Makernotes WB==19)
67             );
68              
69             # Tags found in Panasonic RAW/RW2/RWL images (ref PH)
70             %Image::ExifTool::PanasonicRaw::Main = (
71             GROUPS => { 0 => 'EXIF', 1 => 'IFD0', 2 => 'Image'},
72             WRITE_PROC => \&Image::ExifTool::Exif::WriteExif,
73             CHECK_PROC => \&Image::ExifTool::Exif::CheckExif,
74             WRITE_GROUP => 'IFD0', # default write group
75             NOTES => 'These tags are found in IFD0 of Panasonic/Leica RAW, RW2 and RWL images.',
76             0x01 => {
77             Name => 'PanasonicRawVersion',
78             Writable => 'undef',
79             },
80             0x02 => 'SensorWidth', #1/PH
81             0x03 => 'SensorHeight', #1/PH
82             0x04 => 'SensorTopBorder', #JD
83             0x05 => 'SensorLeftBorder', #JD
84             0x06 => 'SensorBottomBorder', #PH
85             0x07 => 'SensorRightBorder', #PH
86             # observed values for unknown tags - PH
87             # 0x08: 1
88             # 0x09: 1,3,4
89             # 0x0a: 12
90             # (IB gave 0x08-0x0a as BlackLevel tags, but Klaus' decoding makes more sense)
91             0x08 => { Name => 'SamplesPerPixel', Writable => 'int16u', Protected => 1 }, #4
92             0x09 => { #4
93             Name => 'CFAPattern',
94             Writable => 'int16u',
95             Protected => 1,
96             PrintConv => {
97             0 => 'n/a',
98             1 => '[Red,Green][Green,Blue]', # (CM-1, FZ70)
99             2 => '[Green,Red][Blue,Green]', # (LX-7)
100             3 => '[Green,Blue][Red,Green]', # (ZS100, FZ2500, FZ1000, ...)
101             4 => '[Blue,Green][Green,Red]', # (LC-100, G-7, V-LUX1, ...)
102             },
103             },
104             0x0a => { Name => 'BitsPerSample', Writable => 'int16u', Protected => 1 }, #4
105             0x0b => { #4
106             Name => 'Compression',
107             Writable => 'int16u',
108             Protected => 1,
109             PrintConv => {
110             34316 => 'Panasonic RAW 1', # (most models - RAW/RW2/RWL)
111             34826 => 'Panasonic RAW 2', # (DIGILUX 2 - RAW)
112             34828 => 'Panasonic RAW 3', # (D-LUX2,D-LUX3,FZ30,LX1 - RAW)
113             34830 => 'Panasonic RAW 4', #IB (Leica DIGILUX 3, Panasonic DMC-L1)
114             },
115             },
116             # 0x0c: 2 (only Leica Digilux 2)
117             # 0x0d: 0,1
118             # 0x0e,0x0f,0x10: 4095
119             0x0e => { Name => 'LinearityLimitRed', Writable => 'int16u' }, #IB
120             0x0f => { Name => 'LinearityLimitGreen', Writable => 'int16u' }, #IB
121             0x10 => { Name => 'LinearityLimitBlue', Writable => 'int16u' }, #IB
122             0x11 => { #JD
123             Name => 'RedBalance',
124             Writable => 'int16u',
125             ValueConv => '$val / 256',
126             ValueConvInv => 'int($val * 256 + 0.5)',
127             Notes => 'found in Digilux 2 RAW images',
128             },
129             0x12 => { #JD
130             Name => 'BlueBalance',
131             Writable => 'int16u',
132             ValueConv => '$val / 256',
133             ValueConvInv => 'int($val * 256 + 0.5)',
134             },
135             0x13 => { #IB
136             Name => 'WBInfo',
137             SubDirectory => { TagTable => 'Image::ExifTool::PanasonicRaw::WBInfo' },
138             },
139             0x17 => { #1
140             Name => 'ISO',
141             Writable => 'int16u',
142             },
143             # 0x18,0x19,0x1a: 0
144             0x18 => { #IB
145             Name => 'HighISOMultiplierRed',
146             Writable => 'int16u',
147             ValueConv => '$val / 256',
148             ValueConvInv => 'int($val * 256 + 0.5)',
149             },
150             0x19 => { #IB
151             Name => 'HighISOMultiplierGreen',
152             Writable => 'int16u',
153             ValueConv => '$val / 256',
154             ValueConvInv => 'int($val * 256 + 0.5)',
155             },
156             0x1a => { #IB
157             Name => 'HighISOMultiplierBlue',
158             Writable => 'int16u',
159             ValueConv => '$val / 256',
160             ValueConvInv => 'int($val * 256 + 0.5)',
161             },
162             # 0x1b: [binary data] (something to do with the camera ISO cababilities: int16u count N,
163             # followed by table of N entries: int16u ISO, int16u[3] RGB gains - ref IB)
164             0x1c => { Name => 'BlackLevelRed', Writable => 'int16u' }, #IB
165             0x1d => { Name => 'BlackLevelGreen', Writable => 'int16u' }, #IB
166             0x1e => { Name => 'BlackLevelBlue', Writable => 'int16u' }, #IB
167             0x24 => { #2
168             Name => 'WBRedLevel',
169             Writable => 'int16u',
170             },
171             0x25 => { #2
172             Name => 'WBGreenLevel',
173             Writable => 'int16u',
174             },
175             0x26 => { #2
176             Name => 'WBBlueLevel',
177             Writable => 'int16u',
178             },
179             0x27 => { #IB
180             Name => 'WBInfo2',
181             SubDirectory => { TagTable => 'Image::ExifTool::PanasonicRaw::WBInfo2' },
182             },
183             # 0x27,0x29,0x2a,0x2b,0x2c: [binary data]
184             0x2d => { #IB
185             Name => 'RawFormat',
186             Writable => 'int16u',
187             Protected => 1,
188             # 2 - RAW DMC-FZ8/FZ18
189             # 3 - RAW DMC-L10
190             # 4 - RW2 for most other models, including G9 in "pixel shift off" mode and YUNEEC CGO4
191             # (must add 15 to black levels for RawFormat == 4)
192             # 5 - RW2 DC-GH5s; G9 in "pixel shift on" mode
193             # 6 - RW2 DC-S1, DC-S1r in "pixel shift off" mode
194             # 7 - RW2 DC-S1r (and probably DC-S1, have no raw samples) in "pixel shift on" mode
195             # not used - DMC-LX1/FZ30/FZ50/L1/LX1/LX2
196             # (modes 5 and 7 are lossless)
197             },
198             0x2e => { #JD
199             Name => 'JpgFromRaw', # (writable directory!)
200             Groups => { 2 => 'Preview' },
201             Writable => 'undef',
202             # protect this tag because it contains all the metadata
203             Flags => [ 'Binary', 'Protected', 'NestedHtmlDump', 'BlockExtract' ],
204             Notes => 'processed as an embedded document because it contains full EXIF',
205             WriteCheck => '$val eq "none" ? undef : $self->CheckImage(\$val)',
206             DataTag => 'JpgFromRaw',
207             RawConv => '$self->ValidateImage(\$val,$tag)',
208             SubDirectory => {
209             # extract information from embedded image since it is metadata-rich,
210             # unless HtmlDump option set (note that the offsets will be relative,
211             # not absolute like they should be in verbose mode)
212             TagTable => 'Image::ExifTool::JPEG::Main',
213             WriteProc => \&WriteJpgFromRaw,
214             ProcessProc => \&ProcessJpgFromRaw,
215             },
216             },
217             0x2f => { Name => 'CropTop', Writable => 'int16u' },
218             0x30 => { Name => 'CropLeft', Writable => 'int16u' },
219             0x31 => { Name => 'CropBottom', Writable => 'int16u' },
220             0x32 => { Name => 'CropRight', Writable => 'int16u' },
221             # 0x44 - may contain another pointer to the raw data starting at byte 2 in this data (DC-GH6)
222             0x10f => {
223             Name => 'Make',
224             Groups => { 2 => 'Camera' },
225             Writable => 'string',
226             DataMember => 'Make',
227             # save this value as an ExifTool member variable
228             RawConv => '$self->{Make} = $val',
229             },
230             0x110 => {
231             Name => 'Model',
232             Description => 'Camera Model Name',
233             Groups => { 2 => 'Camera' },
234             Writable => 'string',
235             DataMember => 'Model',
236             # save this value as an ExifTool member variable
237             RawConv => '$self->{Model} = $val',
238             },
239             0x111 => {
240             Name => 'StripOffsets',
241             # (this value is 0xffffffff for some models, and RawDataOffset must be used)
242             Flags => [ 'IsOffset', 'PanasonicHack' ],
243             OffsetPair => 0x117, # point to associated byte counts
244             ValueConv => 'length($val) > 32 ? \$val : $val',
245             },
246             0x112 => {
247             Name => 'Orientation',
248             Writable => 'int16u',
249             PrintConv => \%Image::ExifTool::Exif::orientation,
250             Priority => 0, # so IFD1 doesn't take precedence
251             },
252             0x116 => {
253             Name => 'RowsPerStrip',
254             Priority => 0,
255             },
256             0x117 => {
257             Name => 'StripByteCounts',
258             # (note that this value may represent something like uncompressed byte count
259             # for RAW/RW2/RWL images from some models, and is zero for some other models)
260             OffsetPair => 0x111, # point to associated offset
261             ValueConv => 'length($val) > 32 ? \$val : $val',
262             },
263             0x118 => {
264             Name => 'RawDataOffset', #PH (RW2/RWL)
265             IsOffset => '$$et{TIFF_TYPE} =~ /^(RW2|RWL)$/', # (invalid in DNG-converted files)
266             PanasonicHack => 1,
267             OffsetPair => 0x117, # (use StripByteCounts as the offset pair)
268             NotRealPair => 1, # (to avoid Validate warning)
269             },
270             0x119 => {
271             Name => 'DistortionInfo',
272             SubDirectory => { TagTable => 'Image::ExifTool::PanasonicRaw::DistortionInfo' },
273             },
274             # 0x11b - chromatic aberration correction (ref 3) (also see forum9366)
275             0x11c => { #forum9373
276             Name => 'Gamma',
277             Writable => 'int16u',
278             # unfortunately it seems that the scaling factor varies with model...
279             ValueConv => '$val / ($val >= 1024 ? 1024 : ($val >= 256 ? 256 : 100))',
280             ValueConvInv => 'int($val * 256 + 0.5)',
281             },
282             0x120 => {
283             Name => 'CameraIFD',
284             SubDirectory => {
285             TagTable => 'Image::ExifTool::PanasonicRaw::CameraIFD',
286             Base => '$start',
287             ProcessProc => \&Image::ExifTool::ProcessTIFF,
288             },
289             },
290             0x121 => { #forum9295
291             Name => 'Multishot',
292             Writable => 'int32u',
293             PrintConv => {
294             0 => 'Off',
295             65536 => 'Pixel Shift',
296             },
297             },
298             # 0x122 - int32u: RAWDataOffset for the GH5s/GX9, or pointer to end of raw data for G9 (forum9295)
299             0x2bc => { # PH Extension!!
300             Name => 'ApplicationNotes', # (writable directory!)
301             Writable => 'int8u',
302             Format => 'undef',
303             Flags => [ 'Binary', 'Protected' ],
304             SubDirectory => {
305             DirName => 'XMP',
306             TagTable => 'Image::ExifTool::XMP::Main',
307             },
308             },
309             0x001b => { #forum9250
310             Name => 'NoiseReductionParams',
311             Writable => 'undef',
312             Format => 'int16u',
313             Count => -1,
314             Flags => 'Protected',
315             Notes => q{
316             the camera's default noise reduction setup. The first number is the number
317             of entries, then for each entry there are 4 numbers: an ISO speed, and
318             noise-reduction strengths the R, G and B channels
319             },
320             },
321             0x83bb => { # PH Extension!!
322             Name => 'IPTC-NAA', # (writable directory!)
323             Format => 'undef', # convert binary values as undef
324             Writable => 'int32u', # but write int32u format code in IFD
325             WriteGroup => 'IFD0',
326             Flags => [ 'Binary', 'Protected' ],
327             SubDirectory => {
328             DirName => 'IPTC',
329             TagTable => 'Image::ExifTool::IPTC::Main',
330             },
331             },
332             0x8769 => {
333             Name => 'ExifOffset',
334             Groups => { 1 => 'ExifIFD' },
335             Flags => 'SubIFD',
336             SubDirectory => {
337             TagTable => 'Image::ExifTool::Exif::Main',
338             DirName => 'ExifIFD',
339             Start => '$val',
340             },
341             },
342             0x8825 => {
343             Name => 'GPSInfo',
344             Groups => { 1 => 'GPS' },
345             Flags => 'SubIFD',
346             SubDirectory => {
347             DirName => 'GPS',
348             TagTable => 'Image::ExifTool::GPS::Main',
349             Start => '$val',
350             },
351             },
352             # 0xffff => 'DCSHueShiftValues', #exifprobe (NC)
353             );
354              
355             # white balance information (ref IB)
356             # (PanasonicRawVersion<200: Digilux 2)
357             %Image::ExifTool::PanasonicRaw::WBInfo = (
358             PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
359             WRITE_PROC => \&Image::ExifTool::WriteBinaryData,
360             CHECK_PROC => \&Image::ExifTool::CheckBinaryData,
361             WRITABLE => 1,
362             FORMAT => 'int16u',
363             FIRST_ENTRY => 0,
364             0 => 'NumWBEntries',
365             1 => { Name => 'WBType1', %wbTypeInfo },
366             2 => { Name => 'WB_RBLevels1', Format => 'int16u[2]' },
367             4 => { Name => 'WBType2', %wbTypeInfo },
368             5 => { Name => 'WB_RBLevels2', Format => 'int16u[2]' },
369             7 => { Name => 'WBType3', %wbTypeInfo },
370             8 => { Name => 'WB_RBLevels3', Format => 'int16u[2]' },
371             10 => { Name => 'WBType4', %wbTypeInfo },
372             11 => { Name => 'WB_RBLevels4', Format => 'int16u[2]' },
373             13 => { Name => 'WBType5', %wbTypeInfo },
374             14 => { Name => 'WB_RBLevels5', Format => 'int16u[2]' },
375             16 => { Name => 'WBType6', %wbTypeInfo },
376             17 => { Name => 'WB_RBLevels6', Format => 'int16u[2]' },
377             19 => { Name => 'WBType7', %wbTypeInfo },
378             20 => { Name => 'WB_RBLevels7', Format => 'int16u[2]' },
379             );
380              
381             # white balance information (ref IB)
382             # (PanasonicRawVersion>=200: D-Lux2, D-Lux3, DMC-FZ18/FZ30/LX1/L10)
383             %Image::ExifTool::PanasonicRaw::WBInfo2 = (
384             PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
385             WRITE_PROC => \&Image::ExifTool::WriteBinaryData,
386             CHECK_PROC => \&Image::ExifTool::CheckBinaryData,
387             WRITABLE => 1,
388             FORMAT => 'int16u',
389             FIRST_ENTRY => 0,
390             0 => 'NumWBEntries',
391             1 => { Name => 'WBType1', %wbTypeInfo },
392             2 => { Name => 'WB_RGBLevels1', Format => 'int16u[3]' },
393             5 => { Name => 'WBType2', %wbTypeInfo },
394             6 => { Name => 'WB_RGBLevels2', Format => 'int16u[3]' },
395             9 => { Name => 'WBType3', %wbTypeInfo },
396             10 => { Name => 'WB_RGBLevels3', Format => 'int16u[3]' },
397             13 => { Name => 'WBType4', %wbTypeInfo },
398             14 => { Name => 'WB_RGBLevels4', Format => 'int16u[3]' },
399             17 => { Name => 'WBType5', %wbTypeInfo },
400             18 => { Name => 'WB_RGBLevels5', Format => 'int16u[3]' },
401             21 => { Name => 'WBType6', %wbTypeInfo },
402             22 => { Name => 'WB_RGBLevels6', Format => 'int16u[3]' },
403             25 => { Name => 'WBType7', %wbTypeInfo },
404             26 => { Name => 'WB_RGBLevels7', Format => 'int16u[3]' },
405             );
406              
407             # lens distortion information (ref 3)
408             # (distortion correction equation: Ru = scale*(Rd + a*Rd^3 + b*Rd^5 + c*Rd^7), ref 3)
409             %Image::ExifTool::PanasonicRaw::DistortionInfo = (
410             PROCESS_PROC => \&ProcessDistortionInfo,
411             WRITE_PROC => \&WriteDistortionInfo,
412             CHECK_PROC => \&Image::ExifTool::CheckBinaryData,
413             # (don't make this family 0 MakerNotes because we don't want it to be a deletable group)
414             GROUPS => { 0 => 'PanasonicRaw', 1 => 'PanasonicRaw', 2 => 'Image'},
415             WRITABLE => 1,
416             FORMAT => 'int16s',
417             FIRST_ENTRY => 0,
418             NOTES => 'Lens distortion correction information.',
419             # 0,1 - checksums
420             2 => {
421             Name => 'DistortionParam02',
422             ValueConv => '$val / 32768',
423             ValueConvInv => '$val * 32768',
424             },
425             # 3 - usually 0, but seen 0x026b when value 5 is non-zero
426             4 => {
427             Name => 'DistortionParam04',
428             ValueConv => '$val / 32768',
429             ValueConvInv => '$val * 32768',
430             },
431             5 => {
432             Name => 'DistortionScale',
433             ValueConv => '1 / (1 + $val/32768)',
434             ValueConvInv => '(1/$val - 1) * 32768',
435             },
436             # 6 - seen 0x0000-0x027f
437             7.1 => {
438             Name => 'DistortionCorrection',
439             Mask => 0x0f,
440             # (have seen the upper 4 bits set for GF5 and GX1, giving a value of -4095 - PH)
441             PrintConv => { 0 => 'Off', 1 => 'On' },
442             },
443             8 => {
444             Name => 'DistortionParam08',
445             ValueConv => '$val / 32768',
446             ValueConvInv => '$val * 32768',
447             },
448             9 => {
449             Name => 'DistortionParam09',
450             ValueConv => '$val / 32768',
451             ValueConvInv => '$val * 32768',
452             },
453             # 10 - seen 0xfc,0x0101,0x01f4,0x021d,0x0256
454             11 => {
455             Name => 'DistortionParam11',
456             ValueConv => '$val / 32768',
457             ValueConvInv => '$val * 32768',
458             },
459             12 => {
460             Name => 'DistortionN',
461             Unknown => 1,
462             },
463             # 13 - seen 0x0000,0x01f9-0x02b2
464             # 14,15 - checksums
465             );
466              
467             # Panasonic RW2 camera IFD written by GH5 (ref PH)
468             # (doesn't seem to be valid for the GF7 or GM5 -- encrypted?)
469             %Image::ExifTool::PanasonicRaw::CameraIFD = (
470             GROUPS => { 0 => 'PanasonicRaw', 1 => 'CameraIFD', 2 => 'Camera'},
471             # (don't know what format codes 0x101 and 0x102 are for, so just
472             # map them into 4 = int32u for now)
473             VARS => { MAP_FORMAT => { 0x101 => 4, 0x102 => 4 } },
474             0x1001 => { #forum9388
475             Name => 'MultishotOn',
476             Writable => 'int32u',
477             PrintConv => { 0 => 'No', 1 => 'Yes' },
478             },
479             0x1100 => { #forum9274
480             Name => 'FocusStepNear',
481             Writable => 'int16s',
482             },
483             0x1101 => { #forum9274 (was forum8484)
484             Name => 'FocusStepCount',
485             Writable => 'int16s',
486             },
487             0x1102 => { #forum9417
488             Name => 'FlashFired',
489             Writable => 'int32u',
490             PrintConv => { 0 => 'No', 1 => 'Yes' },
491             },
492             # 0x1104 - set when camera shoots on lowest possible Extended-ISO (forum9290)
493             0x1105 => { #forum9392
494             Name => 'ZoomPosition',
495             Notes => 'in the range 0-255 for most cameras',
496             Writable => 'int32u',
497             },
498             0x1200 => { #forum9278
499             Name => 'LensAttached',
500             Notes => 'many CameraIFD tags are invalid if there is no lens attached',
501             Writable => 'int32u',
502             PrintConv => { 0 => 'No', 1 => 'Yes' },
503             },
504             # Note: LensTypeMake and LensTypeModel are combined into a Composite LensType tag
505             # defined in Olympus.pm which has the same values as Olympus:LensType
506             0x1201 => { #IB
507             Name => 'LensTypeMake',
508             Condition => '$format eq "int16u"',
509             Writable => 'int16u',
510             # when format is int16u, these values have been observed:
511             # 0 - Olympus or unknown lens
512             # 2 - Leica or Lumix lens
513             # when format is int32u (S models), these values have been observed (ref IB):
514             # 256 - Leica lens
515             # 257 - Lumix lens
516             # 258 - ? (seen once)
517             },
518             0x1202 => { #IB
519             Name => 'LensTypeModel',
520             Condition => '$format eq "int16u"',
521             Writable => 'int16u',
522             RawConv => q{
523             return undef unless $val;
524             require Image::ExifTool::Olympus; # (to load Composite LensID)
525             return $val;
526             },
527             ValueConv => '$_=sprintf("%.4x",$val); s/(..)(..)/$2 $1/; $_',
528             ValueConvInv => '$val =~ s/(..) (..)/$2$1/; hex($val)',
529             },
530             0x1203 => { #4
531             Name => 'FocalLengthIn35mmFormat',
532             Writable => 'int16u',
533             PrintConv => '"$val mm"',
534             PrintConvInv => '$val=~s/\s*mm$//;$val',
535             },
536             # 0x1300 - incident light value? (ref forum11395)
537             0x1301 => { #forum11395
538             Name => 'ApertureValue',
539             Writable => 'int16s',
540             Priority => 0,
541             ValueConv => '2 ** ($val / 512)',
542             ValueConvInv => '$val>0 ? 512*log($val)/log(2) : 0',
543             PrintConv => 'sprintf("%.1f",$val)',
544             PrintConvInv => '$val',
545             },
546             0x1302 => { #forum11395
547             Name => 'ShutterSpeedValue',
548             Writable => 'int16s',
549             Priority => 0,
550             ValueConv => 'abs($val/256)<100 ? 2**(-$val/256) : 0',
551             ValueConvInv => '$val>0 ? -256*log($val)/log(2) : -25600',
552             PrintConv => 'Image::ExifTool::Exif::PrintExposureTime($val)',
553             PrintConvInv => 'Image::ExifTool::Exif::ConvertFraction($val)',
554             },
555             0x1303 => { #forum11395
556             Name => 'SensitivityValue',
557             Writable => 'int16s',
558             ValueConv => '$val / 256',
559             ValueConvInv => 'int($val * 256)',
560             },
561             0x1305 => { #forum9384
562             Name => 'HighISOMode',
563             Writable => 'int16u',
564             RawConv => '$val || undef',
565             PrintConv => { 1 => 'On', 2 => 'Off' },
566             },
567             # 0x1306 EV for some models like the GX8 (forum11395)
568             # 0x140b - scaled overall black level? (ref forum9281)
569             # 0x1411 - scaled black level per channel difference (ref forum9281)
570             0x1412 => { #forum11397
571             Name => 'FacesDetected',
572             Writable => 'int8u',
573             PrintConv => { 0 => 'No', 1 => 'Yes' },
574             },
575             # 0x2000 - WB tungsten=3, daylight=4 (ref forum9467)
576             # 0x2009 - scaled black level per channel (ref forum9281)
577             # 0x3000-0x310b - red/blue balances * 1024 (ref forum9467)
578             # 0x3000 modifiedTungsten-Red (-2?)
579             # 0x3001 modifiedTungsten-Blue (-2?)
580             # 0x3002 modifiedDaylight-Red (-2?)
581             # 0x3003 modifiedDaylight-Blue (-2?)
582             # 0x3004 modifiedTungsten-Red (-1?)
583             # 0x3005 modifiedTungsten-Blue (-1?)
584             # 0x3006 modifiedDaylight-Red (-1?)
585             # 0x3007 modifiedDaylight-Blue (-1?)
586             # 0x3100 DefaultTungsten-Red
587             # 0x3101 DefaultTungsten-Blue
588             # 0x3102 DefaultDaylight-Red
589             # 0x3103 DefaultDaylight-Blue
590             # 0x3104 modifiedTungsten-Red (+1?)
591             # 0x3105 modifiedTungsten-Blue (+1?)
592             # 0x3106 modifiedDaylight-Red (+1?)
593             # 0x3107 modifiedDaylight-Blue (+1?)
594             # 0x3108 modifiedTungsten-Red (+2?)
595             # 0x3109 modifiedTungsten-Blue (+2?)
596             # 0x310a modifiedDaylight-Red (+2?)
597             # 0x310b modifiedDaylight-Blue (+2?)
598             0x3200 => { #forum9275
599             Name => 'WB_CFA0_LevelDaylight',
600             Writable => 'int16u',
601             },
602             0x3201 => { #forum9275
603             Name => 'WB_CFA1_LevelDaylight',
604             Writable => 'int16u',
605             },
606             0x3202 => { #forum9275
607             Name => 'WB_CFA2_LevelDaylight',
608             Writable => 'int16u',
609             },
610             0x3203 => { #forum9275
611             Name => 'WB_CFA3_LevelDaylight',
612             Writable => 'int16u',
613             },
614             # 0x3204-0x3207 - user multipliers * 1024 ? (ref forum9275)
615             # 0x320a - scaled maximum value of raw data (scaling = 4x) (ref forum9281)
616             # 0x3209 - gamma (x256) (ref forum9281)
617             0x3300 => { #forum9296/9396
618             Name => 'WhiteBalanceSet',
619             Writable => 'int8u',
620             PrintConv => \%panasonicWhiteBalance,
621             SeparateTable => 'WhiteBalance',
622             },
623             0x3420 => { #forum9276
624             Name => 'WB_RedLevelAuto',
625             Writable => 'int16u',
626             },
627             0x3421 => { #forum9276
628             Name => 'WB_BlueLevelAuto',
629             Writable => 'int16u',
630             },
631             0x3501 => { #4
632             Name => 'Orientation',
633             Writable => 'int8u',
634             PrintConv => \%Image::ExifTool::Exif::orientation,
635             },
636             # 0x3504 = Tag 0x1301+0x1302-0x1303 (Bv = Av+Tv-Sv) (forum11395)
637             # 0x3505 - same as 0x1300 (forum11395)
638             0x3600 => { #forum9396
639             Name => 'WhiteBalanceDetected',
640             Writable => 'int8u',
641             PrintConv => \%panasonicWhiteBalance,
642             SeparateTable => 'WhiteBalance',
643             },
644             );
645              
646             # PanasonicRaw composite tags
647             %Image::ExifTool::PanasonicRaw::Composite = (
648             ImageWidth => {
649             Require => {
650             0 => 'IFD0:SensorLeftBorder',
651             1 => 'IFD0:SensorRightBorder',
652             },
653             ValueConv => '$val[1] - $val[0]',
654             },
655             ImageHeight => {
656             Require => {
657             0 => 'IFD0:SensorTopBorder',
658             1 => 'IFD0:SensorBottomBorder',
659             },
660             ValueConv => '$val[1] - $val[0]',
661             },
662             );
663              
664             # add our composite tags
665             Image::ExifTool::AddCompositeTags('Image::ExifTool::PanasonicRaw');
666              
667              
668             #------------------------------------------------------------------------------
669             # checksum algorithm for lens distortion correction information (ref 3)
670             # Inputs: 0) data ref, 1) start position, 2) number of bytes, 3) incement
671             # Returns: checksum value
672             sub Checksum($$$$)
673             {
674 12     12 0 24 my ($dataPt, $start, $num, $inc) = @_;
675 12         17 my $csum = 0;
676 12         18 my $i;
677 12         30 for ($i=0; $i<$num; ++$i) {
678 156         326 $csum = (73 * $csum + Get8u($dataPt, $start + $i * $inc)) % 0xffef;
679             }
680 12         34 return $csum;
681             }
682              
683             #------------------------------------------------------------------------------
684             # Read lens distortion information
685             # Inputs: 0) ExifTool ref, 1) dirInfo ref, 2) tag table ref
686             # Returns: 1 on success
687             sub ProcessDistortionInfo($$$)
688             {
689 2     2 0 10 my ($et, $dirInfo, $tagTablePtr) = @_;
690 2         6 my $dataPt = $$dirInfo{DataPt};
691 2   50     11 my $start = $$dirInfo{DirStart} || 0;
692 2   33     9 my $size = $$dirInfo{DirLen} || (length($$dataPt) - $start);
693 2 50       6 if ($size == 32) {
694             # verify the checksums (ref 3)
695 2         8 my $csum1 = Checksum($dataPt, $start + 4, 12, 1);
696 2         8 my $csum2 = Checksum($dataPt, $start + 16, 12, 1);
697 2         6 my $csum3 = Checksum($dataPt, $start + 2, 14, 2);
698 2         7 my $csum4 = Checksum($dataPt, $start + 3, 14, 2);
699 2         8 my $res = $csum1 ^ Get16u($dataPt, $start + 2) ^
700             $csum2 ^ Get16u($dataPt, $start + 28) ^
701             $csum3 ^ Get16u($dataPt, $start + 0) ^
702             $csum4 ^ Get16u($dataPt, $start + 30);
703 2 50       10 $et->Warn('Invalid DistortionInfo checksum',1) if $res;
704             } else {
705 0         0 $et->Warn('Invalid DistortionInfo',1);
706             }
707 2         13 return $et->ProcessBinaryData($dirInfo, $tagTablePtr);
708             }
709              
710             #------------------------------------------------------------------------------
711             # Write lens distortion information
712             # Inputs: 0) ExifTool ref, 1) dirInfo ref, 2) tag table ref
713             # Returns: updated distortion information or undef on error
714             sub WriteDistortionInfo($$$)
715             {
716 9     9 0 21 my ($et, $dirInfo, $tagTablePtr) = @_;
717 9 100       50 $et or return 1; # (allow dummy access)
718 1         6 my $dat = $et->WriteBinaryData($dirInfo, $tagTablePtr);
719 1 50 33     10 if (defined $dat and length($dat) == 32) {
720             # fix checksums (ref 3)
721 1         6 Set16u(Checksum(\$dat, 4, 12, 1), \$dat, 2);
722 1         4 Set16u(Checksum(\$dat, 16, 12, 1), \$dat, 28);
723 1         4 Set16u(Checksum(\$dat, 2, 14, 2), \$dat, 0);
724 1         3 Set16u(Checksum(\$dat, 3, 14, 2), \$dat, 30);
725             } else {
726 0         0 $et->Warn('Error wriing DistortionInfo',1);
727             }
728 1         4 return $dat;
729             }
730              
731             #------------------------------------------------------------------------------
732             # Patch for writing non-standard Panasonic RAW/RW2/RWL raw data
733             # Inputs: 0) offset info ref, 1) raf ref, 2) IFD number
734             # Returns: error string, or undef on success
735             # OffsetInfo is a hash by tag ID of lists with the following elements:
736             # 0 - tag info ref
737             # 1 - pointer to int32u offset in IFD or value data
738             # 2 - value count
739             # 3 - reference to list of original offset values
740             # 4 - IFD format number
741             # 5 - (pointer to StripOffsets value added by this PatchRawDataOffset routine)
742             # 6 - flag set if this is a fixed offset (Panasonic GH6 fixed-offset hack)
743             sub PatchRawDataOffset($$$)
744             {
745 1     1 0 6 my ($offsetInfo, $raf, $ifd) = @_;
746 1         2 my $stripOffsets = $$offsetInfo{0x111};
747 1         3 my $stripByteCounts = $$offsetInfo{0x117};
748 1         3 my $rawDataOffset = $$offsetInfo{0x118};
749 1         2 my $err;
750 1 50       4 $err = 1 unless $ifd == 0;
751 1 50 33     5 if ($stripOffsets or $stripByteCounts) {
752 1 50 33     11 $err = 1 unless $stripOffsets and $stripByteCounts and $$stripOffsets[2] == 1;
      33        
753             } else {
754             # the DC-GH6 and DC-GH5M2 write RawDataOffset with no Strip tags, so we need
755             # to create fake StripByteCounts information for copying the data
756 0 0       0 if ($$offsetInfo{0x118}) { # (just to be safe)
757 0         0 $stripByteCounts = $$offsetInfo{0x117} = [ $PanasonicRaw::Main{0x117}, 0, 1, [ 0 ], 4 ];
758             # set flag so the offset will be fixed (GH6 hack, see https://exiftool.org/forum/index.php?topic=13861.0)
759             # (of course, fixing up the offset is now unnecessary, but continue to do this even
760             # though the fixup adjustment will be 0 because this allows us to delete the following
761             # line to remove the fix-offset restriction if Panasonic ever sees the light, but note
762             # that in this case we should investigate the purpose of the seemily-duplicate raw
763             # data offset contained within PanasonicRaw_0x0044)
764 0         0 $$offsetInfo{0x118}[6] = 1;
765             }
766             }
767 1 50 33     7 if ($rawDataOffset and not $err) {
768 1 50       4 $err = 1 unless $$rawDataOffset[2] == 1;
769 1 50       3 if ($stripOffsets) {
770 1 50 33     7 $err = 1 unless $$stripOffsets[3][0] == 0xffffffff or $$stripByteCounts[3][0] == 0;
771             }
772             }
773 1 50       3 $err and return 'Unsupported Panasonic/Leica RAW variant';
774 1 50       4 if ($rawDataOffset) {
775             # update StripOffsets along with this tag if it contains a reasonable value
776 1 50 33     8 if ($stripOffsets and $$stripOffsets[3][0] != 0xffffffff) {
777             # save pointer to StripOffsets value for updating later
778 0         0 push @$rawDataOffset, $$stripOffsets[1];
779             }
780             # handle via RawDataOffset instead of StripOffsets
781 1         4 $stripOffsets = $$offsetInfo{0x111} = $rawDataOffset;
782 1         2 delete $$offsetInfo{0x118};
783             }
784             # determine the length of the raw data
785 1         6 my $pos = $raf->Tell();
786 1 50       5 $raf->Seek(0, 2) or $err = 1; # seek to end of file
787 1         5 my $len = $raf->Tell() - $$stripOffsets[3][0];
788 1         4 $raf->Seek($pos, 0);
789             # quick check to be sure the raw data length isn't unreasonable
790             # (the 22-byte length is for '' in our tests)
791 1 50 33     10 $err = 1 if ($len < 1000 and $len != 22) or $len & 0x80000000;
      33        
792 1 50       4 $err and return 'Error reading Panasonic raw data';
793             # update StripByteCounts info with raw data length
794             # (note that the original value is maintained in the file)
795 1         4 $$stripByteCounts[3][0] = $len;
796              
797 1         3 return undef;
798             }
799              
800             #------------------------------------------------------------------------------
801             # Write meta information to Panasonic JpgFromRaw in RAW/RW2/RWL image
802             # Inputs: 0) ExifTool object ref, 1) dirInfo ref, 2) tag table ref
803             # Returns: updated image data, or undef if nothing changed
804             sub WriteJpgFromRaw($$$)
805             {
806 1     1 0 18 my ($et, $dirInfo, $tagTablePtr) = @_;
807 1         4 my $dataPt = $$dirInfo{DataPt};
808 1         5 my $byteOrder = GetByteOrder();
809 1         3 my $fileType = $$et{TIFF_TYPE}; # RAW, RW2 or RWL
810 1         3 my $dirStart = $$dirInfo{DirStart};
811 1 50       4 if ($dirStart) { # DirStart is non-zero in DNG-converted RW2/RWL
812 0         0 my $dirLen = $$dirInfo{DirLen} | length($$dataPt) - $dirStart;
813 0         0 my $buff = substr($$dataPt, $dirStart, $dirLen);
814 0         0 $dataPt = \$buff;
815             }
816 1         5 my $raf = new File::RandomAccess($dataPt);
817 1         3 my $outbuff;
818 1         4 my %dirInfo = (
819             RAF => $raf,
820             OutFile => \$outbuff,
821             );
822 1         3 $$et{BASE} = $$dirInfo{DataPos};
823 1         3 $$et{FILE_TYPE} = $$et{TIFF_TYPE} = 'JPEG';
824             # use a specialized map so we don't write XMP or IPTC (or other junk) into the JPEG
825 1         2 my $editDirs = $$et{EDIT_DIRS};
826 1         3 my $addDirs = $$et{ADD_DIRS};
827 1         6 $et->InitWriteDirs(\%jpgFromRawMap);
828             # don't add XMP segment (IPTC won't get added because it is in Photoshop record)
829 1         5 delete $$et{ADD_DIRS}{XMP};
830 1         6 my $result = $et->WriteJPEG(\%dirInfo);
831             # restore variables we changed
832 1         5 $$et{BASE} = 0;
833 1         3 $$et{FILE_TYPE} = 'TIFF';
834 1         4 $$et{TIFF_TYPE} = $fileType;
835 1         5 $$et{EDIT_DIRS} = $editDirs;
836 1         4 $$et{ADD_DIRS} = $addDirs;
837 1         4 SetByteOrder($byteOrder);
838 1 50       14 return $result > 0 ? $outbuff : $$dataPt;
839             }
840              
841             #------------------------------------------------------------------------------
842             # Extract meta information from an Panasonic JpgFromRaw
843             # Inputs: 0) ExifTool object reference, 1) dirInfo reference
844             # Returns: 1 on success, 0 if this wasn't a valid JpgFromRaw image
845             sub ProcessJpgFromRaw($$$)
846             {
847 2     2 0 7 my ($et, $dirInfo, $tagTablePtr) = @_;
848 2         5 my $dataPt = $$dirInfo{DataPt};
849 2         8 my $byteOrder = GetByteOrder();
850 2         6 my $fileType = $$et{TIFF_TYPE}; # RAW, RW2 or RWL
851 2         5 my $tagInfo = $$dirInfo{TagInfo};
852 2         41 my $verbose = $et->Options('Verbose');
853 2         6 my ($indent, $out);
854 2 50       13 $tagInfo or $et->Warn('No tag info for Panasonic JpgFromRaw'), return 0;
855 2         5 my $dirStart = $$dirInfo{DirStart};
856 2 50       10 if ($dirStart) { # DirStart is non-zero in DNG-converted RW2/RWL
857 0         0 my $dirLen = $$dirInfo{DirLen} | length($$dataPt) - $dirStart;
858 0         0 my $buff = substr($$dataPt, $dirStart, $dirLen);
859 0         0 $dataPt = \$buff;
860             }
861 2   50     11 $$et{BASE} = $$dirInfo{DataPos} + ($dirStart || 0);
862 2         9 $$et{FILE_TYPE} = $$et{TIFF_TYPE} = 'JPEG';
863 2         5 $$et{DOC_NUM} = 1;
864             # extract information from embedded JPEG
865 2         20 my %dirInfo = (
866             Parent => 'RAF',
867             RAF => new File::RandomAccess($dataPt),
868             );
869 2 50       7 if ($verbose) {
870 0         0 my $indent = $$et{INDENT};
871 0         0 $$et{INDENT} = ' ';
872 0         0 $out = $et->Options('TextOut');
873 0         0 print $out '--- DOC1:JpgFromRaw ',('-'x56),"\n";
874             }
875             # fudge HtmlDump base offsets to show as a stand-alone JPEG
876 2         7 $$et{BASE_FUDGE} = $$et{BASE};
877 2         11 my $rtnVal = $et->ProcessJPEG(\%dirInfo);
878 2         6 $$et{BASE_FUDGE} = 0;
879             # restore necessary variables for continued RW2/RWL processing
880 2         5 $$et{BASE} = 0;
881 2         5 $$et{FILE_TYPE} = 'TIFF';
882 2         6 $$et{TIFF_TYPE} = $fileType;
883 2         5 delete $$et{DOC_NUM};
884 2         10 SetByteOrder($byteOrder);
885 2 50       7 if ($verbose) {
886 0         0 $$et{INDENT} = $indent;
887 0         0 print $out ('-'x76),"\n";
888             }
889 2         14 return $rtnVal;
890             }
891              
892             1; # end
893              
894             __END__