Class FilePARREC


  • public class FilePARREC
    extends FileBase
    The class reads and writes PAR/REC files. A right handed coordinate system is used with the positive axis pointing (Anterior/Posterior)A->P, (Foot/Head)F->H, and (Right/Left)R->L. * Updated September 16, 2011 by Beth Tyrie // This explains the derivation of the dicom transformation matrix and origin from the PARREC parameters. // The axial PAR file is KKZ_130213_4_1.PAR // The first slice of the axial dicom file is 20131023203338769.MR.dcm // The sagittal PAR file is KKZ_130213_3_1.PAR // The first slice of the sagittal dicom file is 20131107105801812.MR.dcm. // The coronal PAR file is KKZ_130213_10_1.PAR // The first slice of the coronal dicom file is 20131107105805315.MR.dcm. // The angles are the same for the header volume parameters and for every slice int ori; final int axial = 1; final int sagittal = 2; final int coronal = 3; ori = coronal; double angulation_rl = 0.0; double angulation_ap = 0.0; double angulation_fh = 0.0; switch(ori) { case axial: angulation_rl = -8.700; angulation_ap = -4.865; angulation_fh = 3.364; break; case sagittal: angulation_rl = 0.305; angulation_ap = -4.280; angulation_fh = 4.083; break; case coronal: angulation_rl = 3.931; angulation_ap = -4.031; angulation_fh = 4.346; } double angX = 0.0; double angY = 0.0; double angZ = 0.0; switch(ori) { case axial: angX = angulation_rl; angY = angulation_ap; angZ = angulation_fh; break; case sagittal: angX = angulation_ap; angY = -angulation_fh; angZ = -angulation_rl; break; case coronal: angX = angulation_rl; angY = -angulation_fh; angZ = angulation_ap; } double Sx = Math.sin(angX * Math.PI/180.0); double Sy = Math.sin(angY * Math.PI/180.0); double Sz = Math.sin(angZ * Math.PI/180.0); double Cx = Math.cos(angX * Math.PI/180.0); double Cy = Math.cos(angY * Math.PI/180.0); double Cz = Math.cos(angZ * Math.PI/180.0); // EulerOrder = ORDER_XYZ; // This is the Transformation matrix shown in the dicom header // The dicom transformation matrix or image.getMatrix() is the transpose of the matrix given by getPatientOrientation(). // image.getMatrix() and getPatientOrientation() contain just the rotation component. double m00=Cy*Cz; double m01=-Cy*Sz; double m02=Sy; double m10=Cz*Sx*Sy+Cx*Sz; double m11=Cx*Cz-Sx*Sy*Sz; double m12=-Cy*Sx; double m20=-Cx*Cz*Sy+Sx*Sz; double m21=Cz*Sx+Cx*Sy*Sz; double m22=Cx*Cy; TransMatrix tr = new TransMatrix(4); switch (ori) { case axial: tr.M00 = (float)m00; tr.M01 = (float)m01; tr.M02 = (float)m02; tr.M03 = 0; tr.M10 = (float)m10; tr.M11 = (float)m11; tr.M12 = (float)m12; tr.M13 = 0; tr.M20 = (float)m20; tr.M21 = (float)m21; tr.M22 = (float)m22; tr.M23 = 0; tr.M30 = 0; tr.M31 = 0; tr.M32 = 0; tr.M33 = 1; break; case sagittal: tr.M00 = -(float)m20; tr.M01 = -(float)m21; tr.M02 = -(float)m22; tr.M03 = 0; tr.M10 = (float)m00; tr.M11 = (float)m01; tr.M12 = (float)m02; tr.M13 = 0; tr.M20 = -(float)m10; tr.M21 = -(float)m11; tr.M22 = -(float)m12; tr.M23 = 0; tr.M30 = 0; tr.M31 = 0; tr.M32 = 0; tr.M33 = 1; break; case coronal: tr.M00 = (float)m00; tr.M01 = (float)m01; tr.M02 = (float)m02; tr.M03 = 0; tr.M10 = (float)m20; tr.M11 = (float)m21; tr.M12 = (float)m22; tr.M13 = 0; tr.M20 = -(float)m10; tr.M21 = -(float)m11; tr.M22 = -(float)m12; tr.M23 = 0; tr.M30 = 0; tr.M31 = 0; tr.M32 = 0; tr.M33 = 1; } System.out.println("image transMatrix = " + tr); double resX = 0.0; double resY = 0.0; double resZ = 0.0; switch(ori) { case axial: resX = 0.859375; resY = 0.859375; resZ = 1.0; break; case sagittal: resX = 0.42857143; resY = 0.42857143; resZ = 5.0; break; case coronal: resX = 0.3515625; resY = 0.3515625; resZ = 3.3; } // The header volume offsets and the individual slice offsets are all different // The header volume offsets correspond to (xDim - 1)/2, (yDim - 1)/2, (zDim - 1)/2 // The slice offsets correspond to (xDim - 1)/2, (yDim - 1)/2, zSlice. double rl_offset_center = 0.0; double ap_offset_center = 0.0; double fh_offset_center = 0.0; switch(ori) { case axial: rl_offset_center = 6.655; ap_offset_center = 3.537; fh_offset_center = 22.171; break; case sagittal: rl_offset_center = 8.844; ap_offset_center = -3.225; fh_offset_center = -0.535; break; case coronal: rl_offset_center = 6.147; ap_offset_center = 16.846; fh_offset_center = 16.330; } double offsetX = 0.0; double offsetY = 0.0; double offsetZ = 0.0; switch(ori) { case axial: offsetX = rl_offset_center; offsetY = ap_offset_center; offsetZ = fh_offset_center; break; case sagittal: offsetX = ap_offset_center; offsetY = -fh_offset_center; offsetZ = -rl_offset_center; break; case coronal: offsetX = rl_offset_center; offsetY = -fh_offset_center; offsetZ = ap_offset_center; } double dimX = 0.0; double dimY = 0.0; double dimZ = 0.0; switch(ori) { case axial: dimX = 256; dimY = 256; dimZ = 140; break; case sagittal: dimX = 560; dimY = 560; dimZ = 31; case coronal: dimX = 512; dimY = 512; dimZ = 47; } double originX = offsetX - m00 * resX * (dimX-1)/2 - m01 * resY * (dimY-1)/2- m02 * resZ * (dimZ-1)/2; double originY = offsetY - m10 * resX * (dimX-1)/2 - m11 * resY * (dimY-1)/2 - m12 * resZ * (dimZ-1)/2; double originZ = offsetZ - m20 * resX * (dimX-1)/2 - m21 * resY * (dimY-1)/2 - m22 * resZ * (dimZ-1)/2; if (ori == sagittal) { originY = -originY; originZ = -originZ; } else if (ori == coronal) { originY = -originY; } // These are the origins shown for dicom slice 0 System.out.println("originX = " + originX); System.out.println("originY = " + originY); System.out.println("originZ = " + originZ);
    • Field Detail

      • hdrEXTENSIONS

        public static final java.lang.String[] hdrEXTENSIONS
        The extensions of ANALYZE file.
      • imgEXTENSIONS

        public static final java.lang.String[] imgEXTENSIONS
      • fileDir

        private java.lang.String fileDir
        File directory of the image.
      • fileInfo

        private FileInfoPARREC fileInfo
        Reference to the file info. for an Analyze header
      • fileName

        private java.lang.String fileName
        File name of the image.
      • image

        private ModelImage image
        The image read in from the file.
      • dtiparams

        private DTIParameters dtiparams
        Object to save DWI paramters to
      • vox_offset

        private float vox_offset
        Voxel offset tag used to read in the image.
      • VolMap

        private java.util.HashMap<java.lang.String,​java.lang.String> VolMap
        vol map
      • SliceMap

        private java.util.HashMap<java.lang.String,​java.lang.Integer> SliceMap
        slice map
      • VolParameters

        private java.util.HashMap<java.lang.String,​java.lang.String> VolParameters
        vol parameters
      • SliceParameters

        private java.util.Vector<java.lang.String> SliceParameters
        slice parameters
      • Slices

        private java.util.Vector<java.lang.String> Slices
        slices
      • version

        private java.lang.String version
        version
      • patientName

        private java.lang.String patientName
      • examName

        private java.lang.String examName
        exam name
      • protocolName

        private java.lang.String protocolName
        protocol name
      • patientPosition

        private java.lang.String patientPosition
        patient position
      • foldover

        private java.lang.String foldover
      • sliceOrientPos

        private int sliceOrientPos
      • bValuePos

        private int bValuePos
        bFactorIndex
      • gradPos

        private int gradPos
        bFactorIndex
      • echoNumberPos

        private int echoNumberPos
      • dynamicScanNumberPos

        private int dynamicScanNumberPos
      • cardiacPhaseNumberPos

        private int cardiacPhaseNumberPos
      • imageTypeMRPos

        private int imageTypeMRPos
      • scanningSequencePos

        private int scanningSequencePos
      • scanPercentagePos

        private int scanPercentagePos
      • windowCenterPos

        private int windowCenterPos
      • windowWidthPos

        private int windowWidthPos
      • imageAngulationPos

        private int imageAngulationPos
      • imageOffcentrePos

        private int imageOffcentrePos
      • fmriStatusIndicationPos

        private int fmriStatusIndicationPos
      • imageTypeEDESPos

        private int imageTypeEDESPos
      • echoTimePos

        private int echoTimePos
      • dynamicScanBeginTimePos

        private int dynamicScanBeginTimePos
      • triggerTimePos

        private int triggerTimePos
      • numberOfAveragesPos

        private int numberOfAveragesPos
      • flipAnglePos

        private int flipAnglePos
      • cardiacFrequencyPos

        private int cardiacFrequencyPos
      • minimumRRIntervalPos

        private int minimumRRIntervalPos
      • maximumRRIntervalPos

        private int maximumRRIntervalPos
      • turboFactorPos

        private int turboFactorPos
      • inversionDelayPos

        private int inversionDelayPos
      • diffusionBValueNumberPos

        private int diffusionBValueNumberPos
      • gradientOrientationNumberPos

        private int gradientOrientationNumberPos
      • contrastTypePos

        private int contrastTypePos
      • diffusionAnisotropyTypePos

        private int diffusionAnisotropyTypePos
      • labelTypePos

        private int labelTypePos
      • contrastBolusAgentPos

        private int contrastBolusAgentPos
      • contrastBolusRoutePos

        private int contrastBolusRoutePos
      • contrastBolusVolumePos

        private int contrastBolusVolumePos
      • contrastBolusStartTimePos

        private int contrastBolusStartTimePos
      • contrastBolusTotalDosePos

        private int contrastBolusTotalDosePos
      • contrastBolusIngredientPos

        private int contrastBolusIngredientPos
      • contrastBolusIngredientConcentrationPos

        private int contrastBolusIngredientConcentrationPos
      • sliceOrientIndex

        private int sliceOrientIndex
      • bValueIndex

        private int bValueIndex
      • gradIndex

        private int gradIndex
      • echoNumberIndex

        private int echoNumberIndex
      • dynamicScanNumberIndex

        private int dynamicScanNumberIndex
      • cardiacPhaseNumberIndex

        private int cardiacPhaseNumberIndex
      • imageTypeMRIndex

        private int imageTypeMRIndex
      • scanningSequenceIndex

        private int scanningSequenceIndex
      • scanPercentageIndex

        private int scanPercentageIndex
      • windowCenterIndex

        private int windowCenterIndex
      • windowWidthIndex

        private int windowWidthIndex
      • imageAngulationIndex

        private int imageAngulationIndex
      • imageOffcentreIndex

        private int imageOffcentreIndex
      • fmriStatusIndicationIndex

        private int fmriStatusIndicationIndex
      • imageTypeEDESIndex

        private int imageTypeEDESIndex
      • echoTimeIndex

        private int echoTimeIndex
      • dynamicScanBeginTimeIndex

        private int dynamicScanBeginTimeIndex
      • triggerTimeIndex

        private int triggerTimeIndex
      • numberOfAveragesIndex

        private int numberOfAveragesIndex
      • flipAngleIndex

        private int flipAngleIndex
      • cardiacFrequencyIndex

        private int cardiacFrequencyIndex
      • minimumRRIntervalIndex

        private int minimumRRIntervalIndex
      • maximumRRIntervalIndex

        private int maximumRRIntervalIndex
      • turboFactorIndex

        private int turboFactorIndex
      • inversionDelayIndex

        private int inversionDelayIndex
      • diffusionBValueNumberIndex

        private int diffusionBValueNumberIndex
      • gradientOrientationNumberIndex

        private int gradientOrientationNumberIndex
      • contrastTypeIndex

        private int contrastTypeIndex
      • diffusionAnisotropyTypeIndex

        private int diffusionAnisotropyTypeIndex
      • labelTypeIndex

        private int labelTypeIndex
      • contrastBolusAgentIndex

        private int contrastBolusAgentIndex
      • contrastBolusRouteIndex

        private int contrastBolusRouteIndex
      • contrastBolusVolumeIndex

        private int contrastBolusVolumeIndex
      • contrastBolusStartTimeIndex

        private int contrastBolusStartTimeIndex
      • contrastBolusTotalDoseIndex

        private int contrastBolusTotalDoseIndex
      • contrastBolusIngredientIndex

        private int contrastBolusIngredientIndex
      • contrastBolusIngredientConcentrationIndex

        private int contrastBolusIngredientConcentrationIndex
      • echoNumber

        private int[] echoNumber
      • dynamicScanNumber

        private int[] dynamicScanNumber
      • cardiacPhaseNumber

        private int[] cardiacPhaseNumber
      • imageTypeMR

        private int[] imageTypeMR
      • scanningSequence

        private int[] scanningSequence
      • scanPercentage

        private int[] scanPercentage
      • windowCenter

        private int[] windowCenter
      • windowWidth

        private int[] windowWidth
      • imageAngulation

        private float[][] imageAngulation
      • imageOffcentre

        private float[][] imageOffcentre
      • fmriStatusIndication

        private int[] fmriStatusIndication
      • imageTypeEDES

        private int[] imageTypeEDES
      • echoTime

        private float[] echoTime
      • dynamicScanBeginTime

        private float[] dynamicScanBeginTime
      • triggerTime

        private float[] triggerTime
      • numberOfAverages

        private int[] numberOfAverages
      • flipAngle

        private float[] flipAngle
      • cardiacFrequency

        private int[] cardiacFrequency
      • minimumRRInterval

        private int[] minimumRRInterval
      • maximumRRInterval

        private int[] maximumRRInterval
      • turboFactor

        private int[] turboFactor
      • inversionDelay

        private float[] inversionDelay
      • diffusionBFactor

        private float[] diffusionBFactor
      • diffusionBValueNumber

        private int[] diffusionBValueNumber
      • gradientOrientationNumber

        private int[] gradientOrientationNumber
      • contrastType

        private java.lang.String[] contrastType
      • diffusionAnisotropyType

        private java.lang.String[] diffusionAnisotropyType
      • diffusion

        private float[][] diffusion
      • labelType

        private int[] labelType
      • contrastBolusAgent

        private java.lang.String[] contrastBolusAgent
      • contrastBolusRoute

        private float[] contrastBolusRoute
      • contrastBolusVolume

        private java.lang.String[] contrastBolusVolume
      • contrastBolusStartTime

        private java.lang.String[] contrastBolusStartTime
      • contrastBolusTotalDose

        private float[] contrastBolusTotalDose
      • contrastBolusIngredient

        private java.lang.String[] contrastBolusIngredient
      • contrastBolusIngredientConcentration

        private float[] contrastBolusIngredientConcentration
      • counter

        private int counter
        counter
      • rescaleIntercept

        private float[] rescaleIntercept
        floating point value = raw value/scaleSlope + rescaleIntercept/(rescaleSlope*scaleSlope)
      • rescaleSlope

        private float[] rescaleSlope
      • scaleSlope

        private float[] scaleSlope
      • sameSliceScalings

        private boolean sameSliceScalings
        True if the 3 values are the same for all slices
      • originalDataType

        private int originalDataType
      • isSortedByVolumes

        private boolean isSortedByVolumes
        normal par/rec are sorted by volumes...sometime, they are not sorted like this...instead they are based on slices if that is true, then we need to change around the image dataarray to make it sorted by volumes
      • numSlices

        private int numSlices
        num slices per volume
      • numVolumes

        private int numVolumes
        num vols in 4d dataset
      • changeToUnsignedInts

        boolean changeToUnsignedInts
    • Constructor Detail

      • FilePARREC

        public FilePARREC​(java.lang.String[] fileNames)
        Creates a new FileAnalyze object.
        Parameters:
        fileNames - DOCUMENT ME!
      • FilePARREC

        public FilePARREC​(java.lang.String fName,
                          java.lang.String fDir)
        Constructs new file object.
        Parameters:
        fName - File name.
        fDir - File directory.
      • FilePARREC

        public FilePARREC​(java.lang.String fileName,
                          java.lang.String fileDirectory,
                          FileInfoPARREC[] fileInfoArray,
                          boolean changeToUnsignedInts)
        Constructor used by FileIO to write an image
        Parameters:
        fileName -
        fileDirectory -
        fileInfoPARREC - []
        changeToUnsignedInts -
      • FilePARREC

        public FilePARREC​(java.lang.String fileName,
                          java.lang.String fileDirectory,
                          FileInfoBase fileInfo)
        Constructor used by FileIO to write an image
        Parameters:
        fileName -
        fileDirectory -
        fileInfo -
    • Method Detail

      • finalize

        public void finalize()
        Prepares this class for cleanup. Calls the finalize method for existing elements, closes any open files and sets other elements to null.
        Overrides:
        finalize in class FileBase
      • setImage

        public void setImage​(ModelImage image)
        Parameters:
        image -
      • getCompleteFileNameList

        public static java.lang.String[] getCompleteFileNameList​(java.lang.String absolutePath)
        Returns the complete list of file names according to given file name.
        Parameters:
        absolutePath - one file name of PARREC.
        Returns:
        the complete list of file names.
      • getHeaderFile

        public static java.lang.String getHeaderFile​(java.lang.String[] fileNames)
        ======= Returns the header file.
        Parameters:
        fileNames - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
      • getImageFiles

        public static java.lang.String[] getImageFiles​(java.lang.String[] fileNames)
        Returns the image file list.
        Parameters:
        fileNames - DOCUMENT ME!
        Returns:
        the image file list.
      • isHeaderFile

        public static boolean isHeaderFile​(java.lang.String absolutePath)
        Return true if the file specified by absolutePath is header file of PARREC.
        Parameters:
        absolutePath - the file name including path information.
        Returns:
        true if the specified file is header file.
      • isImageFile

        public static boolean isImageFile​(java.lang.String absolutePath)
        Return true if the file specified by absolutePath is image file of PARREC.
        Parameters:
        absolutePath - the file name including path information.
        Returns:
        true if the specified file is image file.
      • createImage

        public ModelImage createImage()
                               throws java.io.IOException,
                                      java.lang.OutOfMemoryError
        DOCUMENT ME!
        Returns:
        DOCUMENT ME!
        Throws:
        java.io.IOException - DOCUMENT ME!
        java.lang.OutOfMemoryError - DOCUMENT ME!
      • ConvertToMIPAVConvention

        public static TransMatrix ConvertToMIPAVConvention​(TransMatrix mat)
        Converts translation matrix to mipav specific format
      • makeTranslationMatrix

        public static TransMatrix makeTranslationMatrix​(double[] translations)
        Makes rotation matrix based on image off-centres that were stored in header and converted to orientation-specific values.
      • makeRotationMatrix

        public static TransMatrix makeRotationMatrix​(int[] size,
                                                     double[] rotations)
        Makes rotation matrix based on image angulations that were stored in header and converted to orientation-specific values.
      • getFileInfo

        public FileInfoPARREC getFileInfo()
        Returns the FileInfoAnalyze read from the file.
        Returns:
        File info read from file, or null if it has not been read.
      • getHeaderFile

        public java.lang.String getHeaderFile()
        Returns:
        DOCUMENT ME!
        • getImageFiles

          public java.lang.String[] getImageFiles()
          Returns the image file list.
          Returns:
          the image file list.
        • readHeader

          public boolean readHeader​(java.lang.String imageFileName,
                                    java.lang.String fileDir)
                             throws java.io.IOException
          Reads the analyze header and stores the information in fileInfo.
          Parameters:
          imageFileName - File name of image.
          fileDir - Directory.
          Returns:
          Flag to confirm a successful read.
          Throws:
          java.io.IOException - if there is an error reading the header
          See Also:
          FileInfoAnalyze
        • readImage

          public ModelImage readImage​(boolean one)
                               throws java.io.IOException,
                                      java.lang.OutOfMemoryError
          Reads an PAR/REC image file by reading the header then making a FileRaw to read the image for all filenames in the file list. Only the one file directory (currently) supported.
          Parameters:
          one - flag indicating one image of a 3D dataset should be read in.
          Returns:
          The image.
          Throws:
          java.io.IOException - if there is an error reading the file
          java.lang.OutOfMemoryError
          See Also:
          FileRaw
        • readImage

          public void readImage​(float[] buffer)
                         throws java.io.IOException,
                                java.lang.OutOfMemoryError
          Reads in a PAR/REC image (first the header file, then the raw file)
          Parameters:
          buffer - Image buffer to store image data into.
          Throws:
          java.io.IOException - if there is an error reading the file
          java.lang.OutOfMemoryError
          See Also:
          FileRaw
        • writeImage

          public void writeImage​(ModelImage image,
                                 FileWriteOptions options)
                          throws java.io.IOException
          Writes a PAR/REC format image and header.
          Parameters:
          image - Image model of data to write.
          Throws:
          java.io.IOException - if there is an error writing the file
          See Also:
          FileInfoAnalyze, FileRaw
        • getOffset

          private int getOffset​(FileInfoPARREC fileInfo)
          Helper method to calculate the offset for getting only the middle analyze image slice from the 3D file.
          Parameters:
          fileInfo - File info.
          Returns:
          offset
        • updateStartLocations

          private void updateStartLocations​(FileInfoBase[] fileInfo)
          Updates the start locations. Each image has a fileinfo where the start locations are stored. Note that the start location for the Z (3rd) dimension change with the change is the slice. The origin is in the upper left corner and we are using the right hand rule. + x -> left to right; + y -> top to bottom and + z -> into screen.
          Parameters:
          fileInfo - DOCUMENT ME!
        • updateUnitsOfMeasure

          private void updateUnitsOfMeasure​(FileInfoPARREC fileInfo,
                                            ModelImage image)
          Updates the units of Measure in the file info based on the voxUnits from an Analyze Header.
          Parameters:
          fileInfo - -- an Analyze file Info that has already been read
          image - -- a ModelImage that the fileInfo needs to be attached to
        • getCompleteFileNameListDefault

          public java.lang.String[] getCompleteFileNameListDefault​(java.lang.String absolutePath)
          Gets the header and image file names given a header or image file name
          Parameters:
          absolutePath - header or image filename
          Returns:
          array [0] headerfilename [1] imagefilename
        • writeHeader

          public void writeHeader​(ModelImage writeImage,
                                  java.lang.String headerFileName,
                                  int beginSlice,
                                  int endSlice,
                                  int beginTime,
                                  int endTime,
                                  boolean rescale)
                           throws java.io.IOException
          Parameters:
          writeImage -
          headerFileName -
          beginSlice -
          endSlice -
          beginTime -
          endTime -
          rescale -
          Throws:
          java.io.IOException
        • buildParVolMap

          private java.util.HashMap<java.lang.String,​java.lang.String> buildParVolMap()
        • buildParSliceMap

          private java.util.HashMap<java.lang.String,​java.lang.Integer> buildParSliceMap()