First, we need to read the ASCII header file that describes the raw data file contents. On the right, this header is shown as it will be opened by ImageJ via drag and drop.
We open a buffered reader to parse the ASCII file:
BufferedReader br = new BufferedReader(new FileReader(MHDfilename));
Next, we read the first line, to initialize the String line
line = br.readLine();
Now we have to go through the file row by row to find the relevant information. We sub-divide each line into variable-name and variable-value. Having done that, we save the value in a predefined variable.
The for us relevant variables are as followed:
- BinaryDataByteOrderMSB
String [] split = line.split(" = ");
if (line.contains("BinaryDataByteOrderMSB")){
boolean value = Boolean.parseBoolean(split[1]);
intelByteOrder = !value;
}
- Offset
if (line.contains("Offset")){
String [] split2 = split[1].split(" ");
offsets = new double [split2.length];
for (int i=0; i < split2.length; i++){
offsets[i] = Double.parseDouble(split2[i]);
}
}
- ElementSpacing
if (line.contains("ElementSpacing")){
String [] split2 = split[1].split(" ");
spacings = new double [split2.length];
for (int i=0; i < split2.length; i++){
spacings[i] = Double.parseDouble(split2[i]);
}
}
- DimSize
if (line.contains("DimSize")){
String [] split2 = split[1].split(" ");
width = Integer.parseInt(split2[0]);
height = Integer.parseInt(split2[1]);
nImages = Integer.parseInt(split2[2]);
}
- ElementType
if (line.contains("ElementType")){
if (split[1].equals("MET_USHORT")) fileType = FileInfo.GRAY16_UNSIGNED;
- ElementDataFile
if (line.contains("ElementDataFile")){
datafile = split[1];
}