Class FileAnalyze

java.lang.Object
gov.nih.mipav.model.file.FileBase
gov.nih.mipav.model.file.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:
  • Field Details

    • EXTENSIONS

      public static final 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:
    • EXTENTS

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

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

      private String fileDir
      File directory of the image.
    • fileHeader

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

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

      private 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 Details

    • FileAnalyze

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

      public FileAnalyze(String fName, String fDir)
      Constructs new file object.
      Parameters:
      fName - File name.
      fDir - File directory.
  • Method Details

    • 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 IOException
      Flips image. Analyze stores its data "upside down".
      Parameters:
      image - Image to flip.
      Throws:
      IOException - DOCUMENT ME!
    • getCompleteFileNameList

      public static String[] getCompleteFileNameList(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 String getHeaderFile(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 String[] getImageFiles(String[] fileNames)
      Returns the image file list.
      Parameters:
      fileNames - DOCUMENT ME!
      Returns:
      the image file list.
    • isAnalyzeOrSPM

      public static int isAnalyzeOrSPM(String absolutePath) throws FileNotFoundException, 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:
      FileNotFoundException - thrown when the file can't be found.
      IOException - thrown when the I/O error happens.
    • isHeaderFile

      public static boolean isHeaderFile(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(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 IOException
      Take the absolute value of image.
      Parameters:
      image - Image to take absolute value of.
      Throws:
      IOException - DOCUMENT ME!
    • flipTopBottom

      public void flipTopBottom(float[] buffer, FileInfoAnalyze fileInfo) throws 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:
      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 String getHeaderFile()
      Returns the header file.
      Returns:
      DOCUMENT ME!
    • getImageFiles

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

      public boolean readHeader(String imageFileName, String fileDir) throws 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:
      IOException - if there is an error reading the header
      See Also:
    • readImage

      public ModelImage readImage(boolean one) throws IOException, 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:
      IOException - if there is an error reading the file
      OutOfMemoryError
      See Also:
    • readImage

      public void readImage(float[] buffer) throws IOException, 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:
      IOException - if there is an error reading the file
      OutOfMemoryError
      See Also:
    • 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 IOException
      Writes an analyze format type image.
      Parameters:
      image - Image model of data to write.
      Throws:
      IOException - if there is an error writing the file
      See Also:
    • 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, String fileName, String fileDir) throws 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:
      IOException - if there is an error
      See Also:
    • writeHeader3DTo2D

      private void writeHeader3DTo2D(ModelImage image, String fileName, String fileDir, FileWriteOptions options) throws 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:
      IOException - DOCUMENT ME!
    • writeHeader4DTo3D

      private void writeHeader4DTo3D(ModelImage image, String fileName, String fileDir, FileWriteOptions options) throws 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:
      IOException - DOCUMENT ME!