Class FileMGH
- java.lang.Object
-
- gov.nih.mipav.model.file.FileBase
-
- gov.nih.mipav.model.file.FileMGH
-
public class FileMGH extends FileBase
The class reads MGH files. The 4 by 4 matrix in MGH transforms x,y,z indexes to (right, anterior, superior) coordinates where +x = Right, +y = Anterior, +z = Superior. In MIPAV the 4 by 4 matrix does not imply the axis orientations.The MGH definition is: [right] = [xr yr zr] [ xsize * x] + [rorigin] [anterior] = [xa ya za] [ ysize * y] + [aorigin] [superior] = [xs ys zs] [ zsize * z] + [sorigin] Now in going to MIPAV a change must occur. MGH has L->R and P->A while MIPAV uses R->L in all axes and A->P in axes 0 and 1 and P->A in axis 2, so this would cause xr, yr, zr, rorigin, xa, ya, za, and aorigin to be multiplied by -1. cr, ca, and cs refer to the center point of the scanner coordinate system.
A 284 byte header always precedes the data buffer In version 1 if the goodRASFlag <= 0, then only the first 30 bytes are read. If the goodRASFlag > 0, then the first 90 bytes are read
Immediately after the data buffer other optional data structures may be found: They are: Recovery time in milliseconds float Flip angle in radians float Echo time in milliseconds float Inversion time in millseconds float Field of view in millimeters float Comment about fov field from Nick Schmansky: The FoV field should be ignored. In discussing this field with Bruce Fischl, and looking more closely at the code, it appears to be a field with a long and inconsistent usage history (in how it is set and where the data originates). We do not rely on it in our binaries. Lastly, tags including the Talairach transform file name and a list of commands used to create this data(provenance info) may be present.
I am not reading the contents of the Talairach transform file for 3 reasons: 1.) This information is probably not very useful. 2.) Every sample file that I obtained from MGH gave the same name: talairach.xfm. If this name is always used, then it is impossible to tell which .mgh file it corresponds to. 3.) The Talairach transform file is a clear ascii file, so it can be easily read with any number of common programs.
- See Also:
FileIO,FileInfoMGH,FileRaw
-
-
Field Summary
Fields Modifier and Type Field Description private int[]axisOrientationDOCUMENT ME!private intbytesPerValueDOCUMENT ME!private floatcaDOCUMENT ME!(package private) java.lang.String[]cmdlinesDOCUMENT ME!private floatcrDOCUMENT ME!private floatcsDOCUMENT ME!private intdataTypeDOCUMENT ME!private intdepthDOCUMENT ME!private intdofDOCUMENT ME!private int[]extentsDOCUMENT ME!private java.io.FilefileDOCUMENT ME!private java.lang.StringfileDirDOCUMENT ME!private FileInfoMGHfileInfoDOCUMENT ME!private longfileLengthDOCUMENT ME!private java.lang.StringfileNameDOCUMENT ME!private java.io.FileInputStreamfisDOCUMENT ME!private floatflipAngleDOCUMENT ME!private floatfovDOCUMENT ME!private shortgoodRASFlagDOCUMENT ME!private booleangunzipDOCUMENT ME!private intheightDOCUMENT ME!private ModelImageimageDOCUMENT ME!private TransMatrixmatrixDOCUMENT ME!private static intMAX_CMDSDOCUMENT ME!private intmghTypeDOCUMENT ME!private static intMRI_BITMAPDOCUMENT ME!private static intMRI_FLOATDOCUMENT ME!private static intMRI_INTDOCUMENT ME!private static intMRI_LONGDOCUMENT ME!private static intMRI_SHORTDOCUMENT ME!private static intMRI_TENSORDOCUMENT ME!private static intMRI_UCHARDOCUMENT ME!(package private) intncmdsDOCUMENT ME!private intnFramesDOCUMENT ME!private longoptionalStructuresLocationDOCUMENT ME!private float[]originDOCUMENT ME!private float[]resolutionsDOCUMENT ME!private static intTAG_CMDLINEDOCUMENT ME!private static intTAG_MGH_XFORMDOCUMENT ME!private static intTAG_OLD_COLORTABLEDOCUMENT ME!private static intTAG_OLD_MGH_XFORMDOCUMENT ME!private static intTAG_OLD_SURF_GEOMDOCUMENT ME!private static intTAG_OLD_USEREALRASDOCUMENT ME!private floatteDOCUMENT ME!private floattiDOCUMENT ME!private floattrDOCUMENT ME!private intversionPresent version number is 1.private intwidthDOCUMENT ME!private floatxaDOCUMENT ME!private floatxrDOCUMENT ME!private floatxsDOCUMENT ME!private floatyaDOCUMENT ME!private floatyrDOCUMENT ME!private floatysDOCUMENT ME!private floatzaDOCUMENT ME!private floatzrDOCUMENT ME!private floatzsDOCUMENT ME!-
Fields inherited from class gov.nih.mipav.model.file.FileBase
BIG_ENDIAN, bitsPerPixel, fileNames, LITTLE_ENDIAN, pBarVisible, raFile, READ, READ_WRITE
-
-
Constructor Summary
Constructors Constructor Description FileMGH(java.lang.String fName, java.lang.String fDir)Constructs new file object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidattachFileInfo(FileInfoMGH fileInfo, ModelImage image)DOCUMENT ME!voidfinalize()Prepares this class for cleanup.static int[]getAxisOrientation(TransMatrix mat)Return the 3 axis orientation codes that correspond to the closest standard anatomical orientation of the (i,j,k) axes.FileInfoMGHgetFileInfo()Returns the FileInfoMGH read from the file.private intgetOffset()Helper method to calculate the offset for getting only the middle NIFTI image slice from the 3D file.booleanreadHeader(java.lang.String imageFileName, java.lang.String fileDir)Reads the MGH header and stores the information in fileInfo.ModelImagereadImage(boolean one)Reads a MGH image file by reading the header then making a FileRaw to read the image for all filenames in the file list.voidreadImage(float[] buffer)Reads a MGH image file by reading the header then making a FileRaw to read the file.private voidupdateorigins(FileInfoBase[] fileInfo)Updates the start locations.voidwriteImage(ModelImage image, FileWriteOptions options)Writes a MGH or MGZ format type image.-
Methods inherited from class gov.nih.mipav.model.file.FileBase
addProgressChangeListener, bytesToDouble, bytesToFloat, bytesToInt, bytesToShort, doubleToBytes, fireProgressStateChanged, fireProgressStateChanged, fireProgressStateChanged, floatToBytes, getBufferDouble, getBufferFloat, getBufferInt, getBufferLong, getBufferShort, getBufferUShort, getDataType, getDouble, getFloat, getInt, getLong, getProgressChangeListeners, getRaFile, getSignedShort, getString, getUInt, getUnsignedByte, getUnsignedShort, intToBytes, isBigEndian, isProgressBarVisible, linkProgress, longToBytes, readDouble, readFloat, readInt, readLong, readShort, readString, readUnsignedShort, removeProgressChangeListener, setBigEndian, setBufferFloat, setBufferInt, setBufferLong, setBufferShort, setBufferString, setDataType, setEndianess, shortToBytes, writeBytes, writeDouble, writeFloat, writeInt, writeLong, writeShort
-
-
-
-
Field Detail
-
MRI_UCHAR
private static final int MRI_UCHAR
DOCUMENT ME!- See Also:
- Constant Field Values
-
MRI_INT
private static final int MRI_INT
DOCUMENT ME!- See Also:
- Constant Field Values
-
MRI_LONG
private static final int MRI_LONG
DOCUMENT ME!- See Also:
- Constant Field Values
-
MRI_FLOAT
private static final int MRI_FLOAT
DOCUMENT ME!- See Also:
- Constant Field Values
-
MRI_SHORT
private static final int MRI_SHORT
DOCUMENT ME!- See Also:
- Constant Field Values
-
MRI_BITMAP
private static final int MRI_BITMAP
DOCUMENT ME!- See Also:
- Constant Field Values
-
MRI_TENSOR
private static final int MRI_TENSOR
DOCUMENT ME!- See Also:
- Constant Field Values
-
TAG_OLD_COLORTABLE
private static final int TAG_OLD_COLORTABLE
DOCUMENT ME!- See Also:
- Constant Field Values
-
TAG_OLD_USEREALRAS
private static final int TAG_OLD_USEREALRAS
DOCUMENT ME!- See Also:
- Constant Field Values
-
TAG_CMDLINE
private static final int TAG_CMDLINE
DOCUMENT ME!- See Also:
- Constant Field Values
-
TAG_OLD_SURF_GEOM
private static final int TAG_OLD_SURF_GEOM
DOCUMENT ME!- See Also:
- Constant Field Values
-
TAG_OLD_MGH_XFORM
private static final int TAG_OLD_MGH_XFORM
DOCUMENT ME!- See Also:
- Constant Field Values
-
TAG_MGH_XFORM
private static final int TAG_MGH_XFORM
DOCUMENT ME!- See Also:
- Constant Field Values
-
MAX_CMDS
private static final int MAX_CMDS
DOCUMENT ME!- See Also:
- Constant Field Values
-
cmdlines
java.lang.String[] cmdlines
DOCUMENT ME!
-
ncmds
int ncmds
DOCUMENT ME!
-
axisOrientation
private int[] axisOrientation
DOCUMENT ME!
-
bytesPerValue
private int bytesPerValue
DOCUMENT ME!
-
ca
private float ca
DOCUMENT ME!
-
cr
private float cr
DOCUMENT ME!
-
cs
private float cs
DOCUMENT ME!
-
dataType
private int dataType
DOCUMENT ME!
-
depth
private int depth
DOCUMENT ME!
-
dof
private int dof
DOCUMENT ME!
-
extents
private int[] extents
DOCUMENT ME!
-
file
private java.io.File file
DOCUMENT ME!
-
fileDir
private java.lang.String fileDir
DOCUMENT ME!
-
fileInfo
private FileInfoMGH fileInfo
DOCUMENT ME!
-
fileLength
private long fileLength
DOCUMENT ME!
-
fileName
private java.lang.String fileName
DOCUMENT ME!
-
fis
private java.io.FileInputStream fis
DOCUMENT ME!
-
flipAngle
private float flipAngle
DOCUMENT ME!
-
fov
private float fov
DOCUMENT ME!
-
goodRASFlag
private short goodRASFlag
DOCUMENT ME!
-
gunzip
private boolean gunzip
DOCUMENT ME!
-
height
private int height
DOCUMENT ME!
-
image
private ModelImage image
DOCUMENT ME!
-
matrix
private TransMatrix matrix
DOCUMENT ME!
-
mghType
private int mghType
DOCUMENT ME!
-
nFrames
private int nFrames
DOCUMENT ME!
-
optionalStructuresLocation
private long optionalStructuresLocation
DOCUMENT ME!
-
origin
private float[] origin
DOCUMENT ME!
-
resolutions
private float[] resolutions
DOCUMENT ME!
-
te
private float te
DOCUMENT ME!
-
ti
private float ti
DOCUMENT ME!
-
tr
private float tr
DOCUMENT ME!
-
version
private int version
Present version number is 1.
-
width
private int width
DOCUMENT ME!
-
xa
private float xa
DOCUMENT ME!
-
xr
private float xr
DOCUMENT ME!
-
xs
private float xs
DOCUMENT ME!
-
ya
private float ya
DOCUMENT ME!
-
yr
private float yr
DOCUMENT ME!
-
ys
private float ys
DOCUMENT ME!
-
za
private float za
DOCUMENT ME!
-
zr
private float zr
DOCUMENT ME!
-
zs
private float zs
DOCUMENT ME!
-
-
Method Detail
-
finalize
public void finalize()
Prepares this class for cleanup. Calls thefinalizemethod for existing elements, closes any open files and sets other elements tonull.
-
getFileInfo
public FileInfoMGH getFileInfo()
Returns the FileInfoMGH read from the file.- Returns:
- File info read from file, or null if it has not been read.
-
readHeader
public boolean readHeader(java.lang.String imageFileName, java.lang.String fileDir) throws java.io.IOExceptionReads the MGH 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:
FileInfoMGH
-
readImage
public ModelImage readImage(boolean one) throws java.io.IOException, java.lang.OutOfMemoryError
Reads a MGH 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 filejava.lang.OutOfMemoryError- See Also:
FileRaw
-
readImage
public void readImage(float[] buffer) throws java.io.IOException, java.lang.OutOfMemoryErrorReads a MGH 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 filejava.lang.OutOfMemoryError- See Also:
FileRaw
-
writeImage
public void writeImage(ModelImage image, FileWriteOptions options) throws java.io.IOException
Writes a MGH or MGZ format type image.- Parameters:
image- Image model of data to write.options- options such as starting and ending slices and times- Throws:
java.io.IOException- if there is an error writing the file
-
attachFileInfo
private void attachFileInfo(FileInfoMGH fileInfo, ModelImage image)
DOCUMENT ME!- Parameters:
fileInfo- -- a NIFTI file Info that has already been readimage- -- a ModelImage that the fileInfo needs to be attached to
-
getAxisOrientation
public static int[] getAxisOrientation(TransMatrix mat)
Return the 3 axis orientation codes that correspond to the closest standard anatomical orientation of the (i,j,k) axes.- Parameters:
mat- 4x4 matrix that transforms (i,j,k) indexes to x,y,z coordinates where +x =Left, +y = Posterior, +z = Superior Only the upper-left 3x3 corner of the matrix is used This routine finds the permutation of (x,y,z) which has the smallest angle to the (i,j,k) axes directions, which are columns of the input matrix Errors: The codes returned will be zero.- Returns:
- DOCUMENT ME!
-
getOffset
private int getOffset()
Helper method to calculate the offset for getting only the middle NIFTI image slice from the 3D file.- Returns:
- offset
-
updateorigins
private void updateorigins(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!
-
-