Package gov.nih.mipav.model.file
Class FileSPM
- java.lang.Object
-
- gov.nih.mipav.model.file.FileBase
-
- gov.nih.mipav.model.file.FileSPM
-
public class FileSPM extends FileBase
The class reads and writes SPM files.- See Also:
FileIO
,FileInfoSPM
,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. The user could change .img to .spm to indicate SPM99 or SPM2. The .hdr extension would remain unchanged. 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 112 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. Note that in SPM while DATA = datatype * 256 for swapped bytes, only datatype and not DATA is written to the file, so in this program we need never consider the multiplicative factors of 256. 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 for MIPAV just to default to the hacked 7.5 code in FileAnalyze unless a pressing need arises.
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]
bufferByte
DOCUMENT ME!private java.io.File
dataFile
private java.lang.String
dataFileName
private static int
DT_BINARY
DOCUMENT ME!private static int
DT_BYTE
DOCUMENT ME!private static int
DT_COMPLEX
DOCUMENT ME!private static int
DT_DOUBLE
DOCUMENT ME!private static int
DT_FLOAT
DOCUMENT ME!private static int
DT_NONE
DOCUMENT ME!private static int
DT_RGB
DOCUMENT ME!private static int
DT_SIGNED_INT
DOCUMENT ME!private static int
DT_SIGNED_SHORT
DOCUMENT ME!private static int
DT_UNSIGNED_CHAR
DOCUMENT ME!private static int
DT_UNSIGNED_INT
DOCUMENT ME!private static int
DT_UNSIGNED_SHORT
DOCUMENT ME!private java.lang.String
fileDir
DOCUMENT ME!private FileInfoSPM
fileInfo
DOCUMENT ME!private java.lang.String
fileName
DOCUMENT ME!private java.io.File
headerFile
DOCUMENT ME!private java.lang.String
headerFileName
private int
headerSize
DOCUMENT ME!private ModelImage
image
DOCUMENT 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 FileSPM(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 void
finalize()
Prepares this class for cleanup.void
flipTopBottom(float[] buffer, FileInfoSPM fileInfo)
Flips image.void
flipTopBottom(ModelImage image)
Flips image.FileInfoSPM
getFileInfo()
Returns the FileInfoSPM read from the file.private int
getOffset(FileInfoSPM fileInfo)
Helper method to calculate the offset for getting only the middle SPM image slice from the 3D file.static boolean
isSPM(java.lang.String absolutePath)
Determines whether this file is SPM file or not based on three fields of the header file: sizeof_hdr, extent and regular.boolean
readHeader()
DOCUMENT ME!ModelImage
readImage(boolean one)
Reads an SPM image file by reading the header then making a FileRaw to read the image for all filenames in the file list.void
readImage(float[] buffer)
Reads an SPM image file by reading the header then making a FileRaw to read the file.void
reOrgInfo(ModelImage image, FileInfoSPM fileInfo)
Takes the image and sets it to SPM defaults, using the specified info.protected void
updateUnitsOfMeasure(FileInfoSPM fileInfo)
updates the units of Measure in the file info based on the voxUnits from an SPM Header.protected void
updateUnitsOfMeasure(FileInfoSPM fileInfo, ModelImage image)
updates the units of Measure in the file info based on the voxUnits from an SPM Header.private boolean
writeHeader(ModelImage image, int nImagesSaved, int nTimeSaved, java.lang.String fileName, java.lang.String fileDir)
Writes an SPM header to a separate file.void
writeHeader3DTo2D(ModelImage image, java.lang.String fileName, java.lang.String fileDir, FileWriteOptions options)
DOCUMENT ME!void
writeHeader4DTo3D(ModelImage image, java.lang.String fileName, java.lang.String fileDir, FileWriteOptions options)
DOCUMENT ME!void
writeImage(ModelImage image, FileWriteOptions options)
Writes an SPM 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
-
DT_NONE
private static final int DT_NONE
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_BINARY
private static final int DT_BINARY
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_BYTE
private static final int DT_BYTE
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_UNSIGNED_CHAR
private static final int DT_UNSIGNED_CHAR
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_SIGNED_SHORT
private static final int DT_SIGNED_SHORT
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_UNSIGNED_SHORT
private static final int DT_UNSIGNED_SHORT
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_SIGNED_INT
private static final int DT_SIGNED_INT
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_UNSIGNED_INT
private static final int DT_UNSIGNED_INT
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_FLOAT
private static final int DT_FLOAT
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_COMPLEX
private static final int DT_COMPLEX
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_DOUBLE
private static final int DT_DOUBLE
DOCUMENT ME!- See Also:
- Constant Field Values
-
DT_RGB
private static final int DT_RGB
DOCUMENT ME!- See Also:
- Constant Field Values
-
bufferByte
private byte[] bufferByte
DOCUMENT ME!
-
fileDir
private java.lang.String fileDir
DOCUMENT ME!
-
fileInfo
private FileInfoSPM fileInfo
DOCUMENT ME!
-
fileName
private java.lang.String fileName
DOCUMENT ME!
-
headerSize
private int headerSize
DOCUMENT ME!
-
image
private ModelImage image
DOCUMENT ME!
-
headerFileName
private java.lang.String headerFileName
-
headerFile
private java.io.File headerFile
DOCUMENT ME!
-
dataFileName
private java.lang.String dataFileName
-
dataFile
private java.io.File dataFile
-
-
Method Detail
-
finalize
public void finalize()
Prepares this class for cleanup. Calls thefinalize
method for existing elements, closes any open files and sets other elements tonull
.
-
isSPM
public static boolean isSPM(java.lang.String absolutePath) throws java.io.FileNotFoundException, java.io.IOException
Determines whether this file is SPM file or not based on three fields of the header file: sizeof_hdr, extent and regular.- Parameters:
absolutePath
- the file name.- Returns:
- true if the file is ANALYZE file.
- Throws:
java.io.FileNotFoundException
- thrown when the file can't be found.java.io.IOException
- thrown when the I/O error happens.
-
flipTopBottom
public void flipTopBottom(ModelImage image) throws java.io.IOException
Flips image. SPM stores its data "upside down".- Parameters:
image
- Image to flip.- Throws:
java.io.IOException
- DOCUMENT ME!
-
flipTopBottom
public void flipTopBottom(float[] buffer, FileInfoSPM fileInfo) throws java.io.IOException
Flips image. SPM 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 FileInfoSPM getFileInfo()
Returns the FileInfoSPM read from the file.- Returns:
- File info read from file, or null if it has not been read.
-
readHeader
public boolean readHeader() throws java.io.IOException
DOCUMENT ME!- Returns:
- true if read successfully
- Throws:
java.io.IOException
- DOCUMENT ME!
-
readImage
public ModelImage readImage(boolean one) throws java.io.IOException, java.lang.OutOfMemoryError
Reads an SPM 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.OutOfMemoryError
Reads an SPM 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
-
reOrgInfo
public void reOrgInfo(ModelImage image, FileInfoSPM fileInfo)
Takes the image and sets it to SPM defaults, using the specified info.- Parameters:
image
- Image to set to SPM defaults.fileInfo
- File info structure to change.
-
writeHeader3DTo2D
public void writeHeader3DTo2D(ModelImage image, java.lang.String fileName, java.lang.String fileDir, FileWriteOptions options) throws java.io.IOException
DOCUMENT ME!- Parameters:
image
- DOCUMENT ME!fileName
- DOCUMENT ME!fileDir
- DOCUMENT ME!options
- DOCUMENT ME!- Throws:
java.io.IOException
- DOCUMENT ME!
-
writeHeader4DTo3D
public void writeHeader4DTo3D(ModelImage image, java.lang.String fileName, java.lang.String fileDir, FileWriteOptions options) throws java.io.IOException
DOCUMENT ME!- Parameters:
image
- DOCUMENT ME!fileName
- DOCUMENT ME!fileDir
- DOCUMENT ME!options
- DOCUMENT ME!- Throws:
java.io.IOException
- DOCUMENT ME!
-
writeImage
public void writeImage(ModelImage image, FileWriteOptions options) throws java.io.IOException
Writes an SPM 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:
FileInfoSPM
,FileRaw
-
updateUnitsOfMeasure
protected void updateUnitsOfMeasure(FileInfoSPM fileInfo)
updates the units of Measure in the file info based on the voxUnits from an SPM Header. This version simply updates a single FileInfo. It does not have an image to attach to.- Parameters:
fileInfo
- -- an SPM file Info that has already been read
-
updateUnitsOfMeasure
protected void updateUnitsOfMeasure(FileInfoSPM fileInfo, ModelImage image)
updates the units of Measure in the file info based on the voxUnits from an SPM Header.- Parameters:
fileInfo
- -- an SPM file Info that has already been readimage
- -- a ModelImage that the fileInfo needs to be attached to
-
getOffset
private int getOffset(FileInfoSPM fileInfo)
Helper method to calculate the offset for getting only the middle SPM image slice from the 3D file.- Parameters:
fileInfo
- File info.- Returns:
- offset
-
writeHeader
private boolean writeHeader(ModelImage image, int nImagesSaved, int nTimeSaved, java.lang.String fileName, java.lang.String fileDir) throws java.io.IOException
Writes an SPM 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:
FileInfoSPM
-
-