Class FileAnalyze


  • public class FileAnalyze
    extends FileBase
    The class reads and writes Analyze Version 7.5.x files. Note that there are actually 3 variants of Analyze, all using .img and .hdr files, with very slight differences: Mayo Analyze 7.5, SPM99, and SPM2 Variant differences: Location 0 is an int32 with sizeof_hdr. In Non-SPM2, always 348, used to test whether the file is big-endian or little-endian. SPM2 can be 348 or greater. If SPM2 > 348, indicates extended header. Original Mayo Analyze 7.5, SPM99, and SPM2 all have at location 60 a uchar[8] for cal_units and at location 68 an int16 for unused1. However, MIPAV analyze only preserves at location 60 a uchar[4] for cal_units and has hacked locations 64, 66, and 68 with short integers for axis orientation. MIPAV calls these locations unused1, unused2, and unused3. Note that MIPAV's unused3 is at the same location as unused1 in any original format. Original Mayo Analyze 7.5 has at locations 112, 116, and 120 3 unused floats, funused1, funused2, and funused3. SPM99 and SPM2 have at location 112 a float scale factor. SPM2 has at location 116 a float dcoff, which is an intensity zero-intercept. MIPAV analyze has hacked these 3 locations to store 3 floats giving the x-origin, y-origin, and z-origin locations. Mayo Analyze 7.5 has at location 253 a uchar[10] called originator. SPM99 and SPM2 have at location 253 5-int16 called origin[0] thru origin[4]. In SPM99 X, Y, and Z are near the anterior commissure. If the first 3 shorts of a 3D image are set to 0, 0, 0, the origin is assumed to be at the center of the volume, since in SPM the corner voxel is at 1, 1, 1. The position(x,y,z) in mm. is determined by the distance(x,y,z) from the origin multiplied by the vox_units(x,y,z).

    In SPM the signed byte datatype was added with DT_BYTE = 130. MIPAV ANALYZE uses UNSIGNED_SHORT = 6 while SPM uses DT_UNSIGNED_SHORT = 132. The SPM standard also provides for an unsigned int = 136, but MIPAV does not used the unsigned int data type. Note that in SPM while DATA = datatype * 256 for swapped bytes, only datatype and not DATA is written to the file. The most obvious way to test for whether a file is Mayo or SPM would be to see if location 112 is a funused1 = 0 or a nonzero scale factor, but this is complicated by the MIPAV hacking at 112 to store the x-origin location as a float. Asking the users what variant there analyze file is would probably cause massive confusion, so it is probably best just to default to the hacked 7.5 code here unless a pressing need arises.

    Version:
    0.1 Oct 14, 1997
    Author:
    Matthew J. McAuliffe, Ph.D.
    See Also:
    FileIO, FileInfoAnalyze, FileRaw
    • Field Detail

      • EXTENSIONS

        public static final java.lang.String[] EXTENSIONS
        The extensions of ANALYZE file.
      • HEADER_SIZE

        public static final int HEADER_SIZE
        The size of the header, always 348 for Analyze images.
        See Also:
        Constant Field Values
      • EXTENTS

        private static final int EXTENTS
        The extent size of the ANALYZE file.
        See Also:
        Constant Field Values
      • bufferImageHeader

        private byte[] bufferImageHeader
        Storage buffer for the header.
      • fileDir

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

        private java.io.File fileHeader
        Reference to the header file (*.hdr).
      • fileInfo

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

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

        private int headerSize
        Header size for an Analyze image is 348.
      • image

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

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

        private boolean zerofunused
        If true, zero funused fields
    • Constructor Detail

      • FileAnalyze

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

        public FileAnalyze​(java.lang.String fName,
                           java.lang.String fDir)
        Constructs new file object.
        Parameters:
        fName - File name.
        fDir - File directory.
    • 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
      • setZerofunused

        public void setZerofunused​(boolean zerofunused)
        Parameters:
        zerofunused -
      • flipTopBottom

        public static final void flipTopBottom​(ModelImage image)
                                        throws java.io.IOException
        Flips image. Analyze stores its data "upside down".
        Parameters:
        image - Image to flip.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • 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 ANALYZE.
        Returns:
        the complete list of file names.
      • getHeaderFile

        public static java.lang.String getHeaderFile​(java.lang.String[] fileNames)
        Returns the header file (ends in .hdr) as a string.
        Parameters:
        fileNames - DOCUMENT ME!
        Returns:
        The header file (ends in .hdr)
      • 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.
      • isAnalyzeOrSPM

        public static int isAnalyzeOrSPM​(java.lang.String absolutePath)
                                  throws java.io.FileNotFoundException,
                                         java.io.IOException
        Determines whether this file is ANALYZE file or not based on three fields of the header file: sizeof_hdr, extent and regular. Note that all Mayo Analyze 7.5 and all SPM99 variants will pass this test. SPM2 variants with a regular header size of 348 will also pass. Only SPM2 files with an extended header > 348 in size will fail.
        Parameters:
        absolutePath - the file name.
        Returns:
        FileUtility.ANALYZE, FileUtility.SPM, or FileUtility.UNDEFINED
        Throws:
        java.io.FileNotFoundException - thrown when the file can't be found.
        java.io.IOException - thrown when the I/O error happens.
      • isHeaderFile

        public static boolean isHeaderFile​(java.lang.String absolutePath)
        Return true if the file specified by absolutePath is header file of ANALYZE.
        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 ANALYZE.
        Parameters:
        absolutePath - the file name including path information.
        Returns:
        true if the specified file is image file.
      • absoluteValue

        public void absoluteValue​(ModelImage image)
                           throws java.io.IOException
        Take the absolute value of image.
        Parameters:
        image - Image to take absolute value of.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • flipTopBottom

        public void flipTopBottom​(float[] buffer,
                                  FileInfoAnalyze fileInfo)
                           throws java.io.IOException
        Flips image. Analyze stores its data "upside down".
        Parameters:
        buffer - Buffer holding image to flip.
        fileInfo - File info structure for image to flip.
        Throws:
        java.io.IOException - DOCUMENT ME!
      • getFileInfo

        public FileInfoAnalyze 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 the header file.
        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 analyze 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 an analyze image file by reading the header then making a FileRaw to read the file. Image data is left in buffer. If the fileInfo cannot be found, the header will be located and read first. Image is not 'flipped', and neither units of measure nor orientation are set.
        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
      • reOrgInfo

        public void reOrgInfo​(ModelImage image,
                              FileInfoAnalyze fileInfo)
        Takes the image and sets it to analyze defaults, using the specified info.
        Parameters:
        image - Image to set to analyze defaults.
        fileInfo - File info structure to change.
      • writeImage

        public void writeImage​(ModelImage image,
                               FileWriteOptions options)
                        throws java.io.IOException
        Writes an analyze format type image.
        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​(FileInfoAnalyze 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​(FileInfoAnalyze 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
      • writeHeader

        private boolean writeHeader​(ModelImage image,
                                    int nImagesSaved,
                                    int nTimeSaved,
                                    java.lang.String fileName,
                                    java.lang.String fileDir)
                             throws java.io.IOException
        Writes an Analyze header to a separate file.
        Parameters:
        image - Image model of data to write.
        fileName - File name.
        fileDir - File directory.
        Returns:
        Flag to confirm a successful read.
        Throws:
        java.io.IOException - if there is an error
        See Also:
        FileInfoAnalyze
      • writeHeader3DTo2D

        private void writeHeader3DTo2D​(ModelImage image,
                                       java.lang.String fileName,
                                       java.lang.String fileDir,
                                       FileWriteOptions options)
                                throws java.io.IOException
        This method is used when saving a 3D image in an array of 2D files. The file name has numbers appended to correctly order the images.
        Parameters:
        image - the image dataset to be saved
        fileName - the file name
        fileDir - the file directory
        options - file options indicate how to save the image
        Throws:
        java.io.IOException - DOCUMENT ME!
      • writeHeader4DTo3D

        private void writeHeader4DTo3D​(ModelImage image,
                                       java.lang.String fileName,
                                       java.lang.String fileDir,
                                       FileWriteOptions options)
                                throws java.io.IOException
        This method is used when saving a 4D image in an array of 3D files. The file name has numbers appended to correctly order the images.
        Parameters:
        image - the image dataset to be saved
        fileName - the file name
        fileDir - the file directory
        options - file options indicate how to save the image
        Throws:
        java.io.IOException - DOCUMENT ME!