<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#------------------------------------------------------------------------------
# File:         MRC.pm
#
# Description:  Read MRC (Medical Research Council) image files
#
# Revisions:    2021-04-21 - P. Harvey Created
#
# References:   1) https://www.ccpem.ac.uk/mrc_format/mrc2014.php
#               2) http://legacy.ccp4.ac.uk/html/library.html
#               3) https://github.com/ccpem/mrcfile/blob/master/mrcfile/dtypes.py
#
# Notes:        The header is basically identical to the older CCP4 file format
#------------------------------------------------------------------------------

package Image::ExifTool::MRC;

use strict;
use vars qw($VERSION);
use Image::ExifTool qw(:DataAccess :Utils);

$VERSION = '1.00';

my %bool = (
    Format =&gt; 'int8u',
    PrintConv =&gt; { 0 =&gt; 'No', 1 =&gt; 'Yes' } 
);

%Image::ExifTool::MRC::Main = (
    PROCESS_PROC =&gt; \&amp;Image::ExifTool::ProcessBinaryData,
    GROUPS =&gt; { 0 =&gt; 'File', 1 =&gt; 'File', 2 =&gt; 'Image' },
    VARS =&gt; { NO_LOOKUP =&gt; 1 }, # omit tags from lookup
    FORMAT =&gt; 'int32u',
    NOTES =&gt; q{
        Tags extracted from Medical Research Council (MRC) format imaging files. 
        See L&lt;https://www.ccpem.ac.uk/mrc_format/mrc2014.php&gt; for the specification.
    },
    0 =&gt; 'ImageWidth',
    1 =&gt; 'ImageHeight',
    2 =&gt; {
        Name =&gt; 'ImageDepth',
        Notes =&gt; q{
            number of sections. Use ExtractEmbedded option to extract metadata for all
            sections
        },
        RawConv =&gt; '$$self{ImageDepth} = $val',
    },
    3 =&gt; {
        Name =&gt; 'ImageMode',
        PrintConv =&gt; {
            0 =&gt; '8-bit signed integer',
            1 =&gt; '16-bit signed integer',
            2 =&gt; '32-bit signed real',
            3 =&gt; 'complex 16-bit integer',
            4 =&gt; 'complex 32-bit real',
            6 =&gt; '16-bit unsigned integer',
        },
    },
    4 =&gt; { Name =&gt; 'StartPoint', Format =&gt; 'int32u[3]' },
    7 =&gt; { Name =&gt; 'GridSize',   Format =&gt; 'int32u[3]' },
    10 =&gt; { Name =&gt; 'CellWidth', Format =&gt; 'float', Notes =&gt; 'cell size in angstroms' },
    11 =&gt; { Name =&gt; 'CellHeight',Format =&gt; 'float' },
    12 =&gt; { Name =&gt; 'CellDepth', Format =&gt; 'float' },
    13 =&gt; { Name =&gt; 'CellAlpha', Format =&gt; 'float' },
    14 =&gt; { Name =&gt; 'CellBeta',  Format =&gt; 'float' },
    15 =&gt; { Name =&gt; 'CellGamma', Format =&gt; 'float' },
    16 =&gt; { Name =&gt; 'ImageWidthAxis',  PrintConv =&gt; { 1 =&gt; 'X', 2 =&gt; 'Y', 3 =&gt; 'Z' } },
    17 =&gt; { Name =&gt; 'ImageHeightAxis', PrintConv =&gt; { 1 =&gt; 'X', 2 =&gt; 'Y', 3 =&gt; 'Z' } },
    18 =&gt; { Name =&gt; 'ImageDepthAxis',  PrintConv =&gt; { 1 =&gt; 'X', 2 =&gt; 'Y', 3 =&gt; 'Z' } },
    19 =&gt; { Name =&gt; 'DensityMin', Format =&gt; 'float' },
    20 =&gt; { Name =&gt; 'DensityMax', Format =&gt; 'float' },
    21 =&gt; { Name =&gt; 'DensityMean',Format =&gt; 'float' },
    22 =&gt; 'SpaceGroupNumber',
    23 =&gt; { Name =&gt; 'ExtendedHeaderSize', RawConv =&gt; '$$self{ExtendedHeaderSize} = $val' },
    26 =&gt; { Name =&gt; 'ExtendedHeaderType', Format =&gt; 'string[4]', RawConv =&gt; '$$self{ExtendedHeaderType} = $val' },
    27 =&gt; 'MRCVersion',
    49 =&gt; { Name =&gt; 'Origin',     Format =&gt; 'float[3]' },
    53 =&gt; { Name =&gt; 'MachineStamp', Format =&gt; 'int8u[4]', PrintConv =&gt; 'sprintf("0x%.2x 0x%.2x 0x%.2x 0x%.2x",split " ", $val)' },
    54 =&gt; { Name =&gt; 'RMSDeviation', Format =&gt; 'float' },
    55 =&gt; { Name =&gt; 'NumberOfLabels', RawConv =&gt; '$$self{NLab} = $val' },
    56 =&gt; { Name =&gt; 'Label0', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 0' },
    76 =&gt; { Name =&gt; 'Label1', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 1' },
    96 =&gt; { Name =&gt; 'Label2', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 2' },
   116 =&gt; { Name =&gt; 'Label3', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 3' },
   136 =&gt; { Name =&gt; 'Label4', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 4' },
   156 =&gt; { Name =&gt; 'Label5', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 5' },
   176 =&gt; { Name =&gt; 'Label6', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 6' },
   196 =&gt; { Name =&gt; 'Label7', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 7' },
   216 =&gt; { Name =&gt; 'Label8', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 8' },
   236 =&gt; { Name =&gt; 'Label9', Format =&gt; 'string[80]', Condition =&gt; '$$self{NLab} &gt; 9' },
);

%Image::ExifTool::MRC::FEI12 = (
    PROCESS_PROC =&gt; \&amp;Image::ExifTool::ProcessBinaryData,
    GROUPS =&gt; { 0 =&gt; 'File', 1 =&gt; 'File', 2 =&gt; 'Image' },
    VARS =&gt; { NO_LOOKUP =&gt; 1 }, # omit tags from lookup (way too many!)
    NOTES =&gt; 'Tags extracted from FEI1 and FEI2 extended headers.',
    0 =&gt; { Name =&gt; 'MetadataSize',    Format =&gt; 'int32u', RawConv =&gt; '$$self{MetadataSize} = $val' },
    4 =&gt; { Name =&gt; 'MetadataVersion', Format =&gt; 'int32u' },
    8 =&gt; {
        Name =&gt; 'Bitmask1',
        Format =&gt; 'int32u',
        RawConv =&gt; '$$self{BitM} = $val',
        PrintConv =&gt; 'sprintf("0x%.8x", $val)',
    },
    12 =&gt; {
        Name =&gt; 'TimeStamp',
        Format =&gt; 'double', 
        Condition =&gt; '$$self{BitM} &amp; 0x01',
        Groups =&gt; { 2 =&gt; 'Time'},
        # shift from days since Dec 30, 1899 to Unix epoch of Jan 1, 1970
        # (my sample looks like local time, although it should be UTC)
        ValueConv =&gt; 'ConvertUnixTime(($val-25569)*24*3600)',
        PrintConv =&gt; '$self-&gt;ConvertDateTime($val)',
    },
    20  =&gt; { Name =&gt; 'MicroscopeType',  Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x02' },
    36  =&gt; { Name =&gt; 'MicroscopeID',    Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x04' },
    52  =&gt; { Name =&gt; 'Application',     Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x08' },
    68  =&gt; { Name =&gt; 'AppVersion',      Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x10' },
    84  =&gt; { Name =&gt; 'HighTension',     Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x20', Notes =&gt; 'volts' },
    92  =&gt; { Name =&gt; 'Dose',            Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x40', Notes =&gt; 'electrons/m2' },
    100 =&gt; { Name =&gt; 'AlphaTilt',       Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x80' },
    108 =&gt; { Name =&gt; 'BetaTilt',        Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x100' },
    116 =&gt; { Name =&gt; 'XStage',          Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x200' },
    124 =&gt; { Name =&gt; 'YStage',          Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x400' },
    132 =&gt; { Name =&gt; 'ZStage',          Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x800' },
    140 =&gt; { Name =&gt; 'TiltAxisAngle',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x1000' },
    148 =&gt; { Name =&gt; 'DualAxisRot',     Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x2000' },
    156 =&gt; { Name =&gt; 'PixelSizeX',      Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x4000' },
    164 =&gt; { Name =&gt; 'PixelSizeY',      Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x8000' },
    220 =&gt; { Name =&gt; 'Defocus',         Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x400000' },
    228 =&gt; { Name =&gt; 'STEMDefocus',     Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x800000' },
    236 =&gt; { Name =&gt; 'AppliedDefocus',  Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x1000000' },
    244 =&gt; { Name =&gt; 'InstrumentMode',  Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x2000000', PrintConv =&gt; { 1 =&gt; 'TEM', 2 =&gt; 'STEM' } },
    248 =&gt; { Name =&gt; 'ProjectionMode',  Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x4000000', PrintConv =&gt; { 1 =&gt; 'Diffraction', 2 =&gt; 'Imaging' } },
    252 =&gt; { Name =&gt; 'ObjectiveLens',   Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x8000000' },
    268 =&gt; { Name =&gt; 'HighMagnificationMode', Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x10000000' },
    284 =&gt; { Name =&gt; 'ProbeMode',       Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x20000000', PrintConv =&gt; { 1 =&gt; 'Nano', 2 =&gt; 'Micro' } },
    288 =&gt; { Name =&gt; 'EFTEMOn',         %bool,                  Condition =&gt; '$$self{BitM} &amp; 0x40000000' },
    289 =&gt; { Name =&gt; 'Magnification',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x80000000' },
    297 =&gt; {
        Name =&gt; 'Bitmask2',
        Format =&gt; 'int32u',
        RawConv =&gt; '$$self{BitM} = $val',
        PrintConv =&gt; 'sprintf("0x%.8x", $val)',
    },
    301 =&gt; { Name =&gt; 'CameraLength',    Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x01' },
    309 =&gt; { Name =&gt; 'SpotIndex',       Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x02' },
    313 =&gt; { Name =&gt; 'IlluminationArea',Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x04' },
    321 =&gt; { Name =&gt; 'Intensity',       Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x08' },
    329 =&gt; { Name =&gt; 'ConvergenceAngle',Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x10' },
    337 =&gt; { Name =&gt; 'IlluminationMode',Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x20' },
    353 =&gt; { Name =&gt; 'WideConvergenceAngleRange', %bool,        Condition =&gt; '$$self{BitM} &amp; 0x40' },
    354 =&gt; { Name =&gt; 'SlitInserted',    %bool,                  Condition =&gt; '$$self{BitM} &amp; 0x80' },
    355 =&gt; { Name =&gt; 'SlitWidth',       Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x100' },
    363 =&gt; { Name =&gt; 'AccelVoltOffset', Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x200' },
    371 =&gt; { Name =&gt; 'DriftTubeVolt',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x400' },
    379 =&gt; { Name =&gt; 'EnergyShift',     Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x800' },
    387 =&gt; { Name =&gt; 'ShiftOffsetX',    Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x1000' },
    395 =&gt; { Name =&gt; 'ShiftOffsetY',    Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x2000' },
    403 =&gt; { Name =&gt; 'ShiftX',          Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x4000' },
    411 =&gt; { Name =&gt; 'ShiftY',          Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x8000' },
    419 =&gt; { Name =&gt; 'IntegrationTime', Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x10000' },
    427 =&gt; { Name =&gt; 'BinningWidth',    Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x20000' },
    431 =&gt; { Name =&gt; 'BinningHeight',   Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x40000' },
    435 =&gt; { Name =&gt; 'CameraName',      Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x80000' },
    451 =&gt; { Name =&gt; 'ReadoutAreaLeft', Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x100000' },
    455 =&gt; { Name =&gt; 'ReadoutAreaTop',  Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x200000' },
    459 =&gt; { Name =&gt; 'ReadoutAreaRight',Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x400000' },
    463 =&gt; { Name =&gt; 'ReadoutAreaBottom',Format=&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x800000' },
    467 =&gt; { Name =&gt; 'CetaNoiseReduct', %bool,                  Condition =&gt; '$$self{BitM} &amp; 0x1000000' },
    468 =&gt; { Name =&gt; 'CetaFramesSummed',Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x2000000' },
    472 =&gt; { Name =&gt; 'DirectDetElectronCounting', %bool,        Condition =&gt; '$$self{BitM} &amp; 0x4000000' },
    473 =&gt; { Name =&gt; 'DirectDetAlignFrames',      %bool,        Condition =&gt; '$$self{BitM} &amp; 0x8000000' },
    490 =&gt; {
        Name =&gt; 'Bitmask3',
        Format =&gt; 'int32u',
        RawConv =&gt; '$$self{BitM} = $val',
        PrintConv =&gt; 'sprintf("0x%.8x", $val)',
    },
    518 =&gt; { Name =&gt; 'PhasePlate',      %bool,                  Condition =&gt; '$$self{BitM} &amp; 0x40' },
    519 =&gt; { Name =&gt; 'STEMDetectorName',Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x80' },
    535 =&gt; { Name =&gt; 'Gain',            Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x100' },
    543 =&gt; { Name =&gt; 'Offset',          Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x200' },
    571 =&gt; { Name =&gt; 'DwellTime',       Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x8000' },
    579 =&gt; { Name =&gt; 'FrameTime',       Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x10000' },
    587 =&gt; { Name =&gt; 'ScanSizeLeft',    Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x20000' },
    591 =&gt; { Name =&gt; 'ScanSizeTop',     Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x40000' },
    595 =&gt; { Name =&gt; 'ScanSizeRight',   Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x80000' },
    599 =&gt; { Name =&gt; 'ScanSizeBottom',  Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x100000' },
    603 =&gt; { Name =&gt; 'FullScanFOV_X',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x200000' },
    611 =&gt; { Name =&gt; 'FullScanFOV_Y',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x400000' },
    619 =&gt; { Name =&gt; 'Element',         Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x800000' },
    635 =&gt; { Name =&gt; 'EnergyIntervalLower', Format =&gt; 'double', Condition =&gt; '$$self{BitM} &amp; 0x1000000' },
    643 =&gt; { Name =&gt; 'EnergyIntervalHigher',Format =&gt; 'double', Condition =&gt; '$$self{BitM} &amp; 0x2000000' },
    651 =&gt; { Name =&gt; 'Method',          Format=&gt; 'int32u',      Condition =&gt; '$$self{BitM} &amp; 0x4000000' },
    655 =&gt; { Name =&gt; 'IsDoseFraction',  %bool,                  Condition =&gt; '$$self{BitM} &amp; 0x8000000' },
    656 =&gt; { Name =&gt; 'FractionNumber',  Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x10000000' },
    660 =&gt; { Name =&gt; 'StartFrame',      Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x20000000' },
    664 =&gt; { Name =&gt; 'EndFrame',        Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x40000000' },
    668 =&gt; { Name =&gt;'InputStackFilename',Format=&gt; 'string[80]', Condition =&gt; '$$self{BitM} &amp; 0x80000000' },
    748 =&gt; {
        Name =&gt; 'Bitmask4',
        Format =&gt; 'int32u',
        RawConv =&gt; '$$self{BitM} = $val',
        PrintConv =&gt; 'sprintf("0x%.8x", $val)',
    },
    752 =&gt; { Name =&gt; 'AlphaTiltMin',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x01' },
    760 =&gt; { Name =&gt; 'AlphaTiltMax',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x02' },
#
# FEI2 header starts here
#
    768 =&gt; { Name =&gt; 'ScanRotation',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x04' },
    776 =&gt; { Name =&gt; 'DiffractionPatternRotation',Format=&gt;'double', Condition =&gt; '$$self{BitM} &amp; 0x08' },
    784 =&gt; { Name =&gt; 'ImageRotation',  Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x10' },
    792 =&gt; { Name =&gt; 'ScanModeEnumeration',Format =&gt; 'int32u', Condition =&gt; '$$self{BitM} &amp; 0x20', PrintConv =&gt; { 0 =&gt; 'Other', 1 =&gt; 'Raster', 2 =&gt; 'Serpentine' } },
    796 =&gt; {
        Name =&gt; 'AcquisitionTimeStamp',
        Format =&gt; 'int64u',
        Condition =&gt; '$$self{BitM} &amp; 0x40',
        Groups =&gt; { 2 =&gt; 'Time' },
        # microseconds since 1970 UTC
        ValueConv =&gt; 'ConvertUnixTime($val / 1e6, 1, 6)',
        PrintConv =&gt; '$self-&gt;ConvertDateTime($val)',
    },
    804 =&gt; { Name =&gt; 'DetectorCommercialName', Format =&gt; 'string[16]', Condition =&gt; '$$self{BitM} &amp; 0x80' },
    820 =&gt; { Name =&gt; 'StartTiltAngle', Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x100' },
    828 =&gt; { Name =&gt; 'EndTiltAngle',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x200' },
    836 =&gt; { Name =&gt; 'TiltPerImage',   Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x400' },
    844 =&gt; { Name =&gt; 'TitlSpeed',      Format =&gt; 'double',     Condition =&gt; '$$self{BitM} &amp; 0x800' },
    852 =&gt; { Name =&gt; 'BeamCenterX',    Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x1000' },
    856 =&gt; { Name =&gt; 'BeamCenterY',    Format =&gt; 'int32u',     Condition =&gt; '$$self{BitM} &amp; 0x2000' },
    860 =&gt; {
        Name =&gt; 'CFEGFlashTimeStamp',
        Format =&gt; 'int64u',
        Condition =&gt; '$$self{BitM} &amp; 0x4000',
        Groups =&gt; { 2 =&gt; 'Time' },
        ValueConv =&gt; 'ConvertUnixTime($val / 1e6, 1, 6)',
        PrintConv =&gt; '$self-&gt;ConvertDateTime($val)',
    },
    868 =&gt; { Name =&gt; 'PhasePlatePosition',Format =&gt; 'int32u',  Condition =&gt; '$$self{BitM} &amp; 0x8000' },
    872 =&gt; { Name =&gt; 'ObjectiveAperture', Format=&gt;'string[16]',Condition =&gt; '$$self{BitM} &amp; 0x10000' },
);

#------------------------------------------------------------------------------
# Extract metadata from a MRC image
# Inputs: 0) ExifTool object reference, 1) dirInfo reference
# Returns: 1 on success, 0 if this wasn't a valid MRC file
sub ProcessMRC($$)
{
    my ($et, $dirInfo) = @_;
    my $raf = $$dirInfo{RAF};
    my ($buff, $tagTablePtr, $i);

    # verify this is a valid MRC file
    return 0 unless $raf-&gt;Read($buff, 1024) == 1024;
    # validate axes, "MAP" file type and machine stamp
    return 0 unless $buff =~ /^.{64}[\x01\x02\x03]\0\0\0[\x01\x02\x03]\0\0\0[\x01\x02\x03]\0\0\0.{132}MAP[\0 ](\x44\x44|\x44\x41|\x11\x11)\0\0/s;

    $et-&gt;SetFileType();
    SetByteOrder('II');
    my %dirInfo = (
        DataPt =&gt; \$buff,
        DirStart =&gt; 0,
        DirLen =&gt; length($buff),
    );
    $tagTablePtr = GetTagTable('Image::ExifTool::MRC::Main');
    $et-&gt;ProcessDirectory(\%dirInfo, $tagTablePtr);

    # (I don't have any samples with extended headers for testing, so these are not yet decoded)
    if ($$et{ExtendedHeaderSize} and $$et{ExtendedHeaderType} =~ /^FEI[12]/) {
        unless ($raf-&gt;Read($buff,4)==4 and $raf-&gt;Seek(-4,1)) { # read metadata size
            $et-&gt;Warn('Error reading extended header'); 
            return 1;
        }
        my $size = Get32u(\$buff, 0);
        if ($size * $$et{ImageDepth} &gt; $$et{ExtendedHeaderSize}) {
            $et-&gt;Warn('Corrupted extended header');
            return 1;
        }
        $dirInfo{DirLen} = $size;
        $tagTablePtr = GetTagTable('Image::ExifTool::MRC::FEI12');
        for ($i=0; ;) {
            $dirInfo{DataPos} = $raf-&gt;Tell();
            $raf-&gt;Read($buff, $size) == $size or $et-&gt;Warn("Error reading extended header $i"), last;
            $et-&gt;ProcessDirectory(\%dirInfo, $tagTablePtr);
            last if ++$i &gt;= $$et{ImageDepth};
            unless ($$et{OPTIONS}{ExtractEmbedded}) {
                $et-&gt;Warn('Use the ExtractEmbedded option to read metadata for all frames',3);
                last;
            }
            $$et{DOC_NUM} = ++$$et{DOC_COUNT};
        }
        delete $$et{DOC_NUM};
    }

    return 1;
}

1;  # end

__END__

=head1 NAME

Image::ExifTool::MRC - Read MRC meta information

=head1 SYNOPSIS

This module is used by Image::ExifTool

=head1 DESCRIPTION

This module contains definitions required by Image::ExifTool to read
metadata from Medical Research Council (MRC) images.

=head1 AUTHOR

Copyright 2003-2023, Phil Harvey (philharvey66 at gmail.com)

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=head1 REFERENCES

=over 4

=item L&lt;https://www.ccpem.ac.uk/mrc_format/mrc2014.php&gt;

=item L&lt;http://legacy.ccp4.ac.uk/html/library.html&gt;

=item L&lt;https://github.com/ccpem/mrcfile/blob/master/mrcfile/dtypes.py&gt;

=back

=head1 SEE ALSO

L&lt;Image::ExifTool::TagNames/MRC Tags&gt;,
L&lt;Image::ExifTool(3pm)|Image::ExifTool&gt;

=cut

</pre></body></html>