| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # File: LIF.pm | ||
| 3 | # | ||
| 4 | # Description: Read LIF (Leica Image File) files | ||
| 5 | # | ||
| 6 | # Revisions: 2021-06-21 - P. Harvey Created | ||
| 7 | #------------------------------------------------------------------------------ | ||
| 8 | |||
| 9 | package Image::ExifTool::LIF; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw($VERSION); | ||
| 13 | use Image::ExifTool qw(:DataAccess :Utils); | ||
| 14 | use Image::ExifTool::XMP; | ||
| 15 | |||
| 16 | $VERSION = '1.02'; | ||
| 17 | |||
| 18 | %Image::ExifTool::LIF::Main = ( | ||
| 19 | GROUPS => { 0 => 'XML', 1 => 'XML', 2 => 'Image' }, | ||
| 20 | PROCESS_PROC => \&Image::ExifTool::XMP::ProcessXMP, | ||
| 21 | VARS => { ID_FMT => 'none' }, | ||
| 22 | NOTES => q{ | ||
| 23 | Tags extracted from Leica Image Format (LIF) imaging files. As well as the | ||
| 24 | tags listed below, all available information is extracted from the | ||
| 25 | XML-format metadata in the LIF header. | ||
| 26 | }, | ||
| 27 | TimeStampList => { | ||
| 28 | Groups => { 2 => 'Time' }, | ||
| 29 | ValueConv => q{ | ||
| 30 | my $unixTimeZero = 134774 * 24 * 3600; | ||
| 31 | my @vals = split ' ', $val; | ||
| 32 | foreach (@vals) { | ||
| 33 | if (/[^0-9a-f]/i) { | ||
| 34 | $_ = '0000:00:00 00:00:00'; | ||
| 35 | } elsif (length $_ > 8) { | ||
| 36 | my $lo = hex substr($_, -8); | ||
| 37 | my $hi = hex substr($_, 0, -8); | ||
| 38 | $_ = 1e-7 * ($hi * 4294967296 + $lo); | ||
| 39 | } else { | ||
| 40 | $_ = 1e-7 * hex($_); | ||
| 41 | } | ||
| 42 | # shift from Jan 1, 1601 to Jan 1, 1970 | ||
| 43 | $_ = Image::ExifTool::ConvertUnixTime($_ - $unixTimeZero); | ||
| 44 | } | ||
| 45 | return \@vals; | ||
| 46 | }, | ||
| 47 | }, | ||
| 48 | ); | ||
| 49 | |||
| 50 | #------------------------------------------------------------------------------ | ||
| 51 | # Shorten obscenely long LIF tag names | ||
| 52 | # Inputs: Tag name | ||
| 53 | # Returns: Shortened tag name | ||
| 54 | sub ShortenTagNames($) | ||
| 55 | { | ||
| 56 | local $_; | ||
| 57 | $_ = shift; | ||
| 58 | s/DescriptionDimensionsDimensionDescription/Dimensions/; | ||
| 59 | s/DescriptionChannelsChannelDescription/Channel/; | ||
| 60 | s/ShutterListShutter/Shutter/; | ||
| 61 | s/SettingDefinition/Setting/; | ||
| 62 | s/AdditionalZPositionListAdditionalZPosition/AdditionalZPosition/; | ||
| 63 | s/LMSDataContainerHeader//g; | ||
| 64 | s/FilterWheelWheel/FilterWheel/; | ||
| 65 | s/FilterWheelFilter/FilterWheel/; | ||
| 66 | s/DetectorListDetector/Detector/; | ||
| 67 | s/OnlineDyeSeparationOnlineDyeSeparation/OnlineDyeSeparation/; | ||
| 68 | s/AotfListAotf/Aotf/; | ||
| 69 | s/SettingAotfLaserLineSetting/SettingAotfLaser/; | ||
| 70 | s/DataROISetROISet/DataROISet/; | ||
| 71 | s/AdditionalZPosition/AddZPos/; | ||
| 72 | s/FRAPplusBlock_FRAPBlock_FRAP_PrePost_Info/FRAP_/; | ||
| 73 | s/FRAPplusBlock_FRAPBlock_FRAP_(Master)?/FRAP_/; | ||
| 74 | s/LDM_Block_SequentialLDM_Block_Sequential_/LDM_/; | ||
| 75 | s/ATLConfocalSetting/ATLConfocal/; | ||
| 76 | s/LaserArrayLaser/Laser/; | ||
| 77 | s/LDM_Master/LDM_/; | ||
| 78 | s/(List)?ATLConfocal/ATL_/; | ||
| 79 | s/Separation/Sep/; | ||
| 80 | s/BleachPointsElement/BleachPoint/; | ||
| 81 | s/BeamPositionBeamPosition/BeamPosition/; | ||
| 82 | s/DataROISetPossible(ROI)?/DataROISet/; | ||
| 83 | s/RoiElementChildrenElementDataROISingle(Roi)?/Roi/; | ||
| 84 | s/InfoLaserLineSettingArrayLaserLineSetting/LastLineSetting/; | ||
| 85 | s/FilterWheelWheelNameFilterName/FilterWheelFilterName/; | ||
| 86 | s/LUT_ListLut/Lut/; | ||
| 87 | s/ROI_ListRoiRoidata/ROI_/; | ||
| 88 | s/LaserLineSettingArrayLaserLineSetting/LaserLineSetting/; | ||
| 89 | return $_; | ||
| 90 | } | ||
| 91 | |||
| 92 | #------------------------------------------------------------------------------ | ||
| 93 | # Extract metadata from a LIF image | ||
| 94 | # Inputs: 0) ExifTool object reference, 1) dirInfo reference | ||
| 95 | # Returns: 1 on success, 0 if this wasn't a valid LIF file | ||
| 96 | sub ProcessLIF($$) | ||
| 97 | { | ||
| 98 | my ($et, $dirInfo) = @_; | ||
| 99 | my $raf = $$dirInfo{RAF}; | ||
| 100 | my $buff; | ||
| 101 | |||
| 102 | # verify this is a valid LIF file | ||
| 103 | return 0 unless $raf->Read($buff, 15) == 15 and $buff =~ /^\x70\0{3}.{4}\x2a.{4}<\0/s; | ||
| 104 | |||
| 105 | $et->SetFileType(); | ||
| 106 | SetByteOrder('II'); | ||
| 107 | |||
| 108 | my $size = Get32u(\$buff, 4); # XML chunk size | ||
| 109 | my $len = Get32u(\$buff, 9) * 2; # XML data length | ||
| 110 | |||
| 111 | $size < $len and $et->Error('Corrupted LIF XML block'), return 1; | ||
| 112 | $size > 100000000 and $et->Error('LIF XML block too large'), return 1; | ||
| 113 | |||
| 114 | $raf->Seek(-2, 1) and $raf->Read($buff, $len) == $len or $et->Error('Truncated LIF XML block'), return 1; | ||
| 115 | |||
| 116 | my $tagTablePtr = GetTagTable('Image::ExifTool::LIF::Main'); | ||
| 117 | |||
| 118 | # convert from UTF16 to UTF8 | ||
| 119 | my $xml = Image::ExifTool::Decode($et, $buff, 'UTF16', 'II', 'UTF8'); | ||
| 120 | |||
| 121 | my %dirInfo = ( DataPt => \$xml ); | ||
| 122 | |||
| 123 | $$et{XmpIgnoreProps} = [ 'LMSDataContainerHeader', 'Element', 'Children', 'Data', 'Image', 'Attachment' ]; | ||
| 124 | $$et{ShortenXmpTags} = \&ShortenTagNames; | ||
| 125 | |||
| 126 | $et->ProcessDirectory(\%dirInfo, $tagTablePtr); | ||
| 127 | |||
| 128 | return 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; # end | ||
| 132 | |||
| 133 | __END__ |