forked from ome/bioformats
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SISReader.java
252 lines (202 loc) · 7.68 KB
/
SISReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//
// SISReader.java
//
/*
OME Bio-Formats package for reading and converting biological file formats.
Copyright (C) 2005-@year@ UW-Madison LOCI and Glencoe Software, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package loci.formats.in;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import loci.common.DateTools;
import loci.common.IniList;
import loci.common.IniParser;
import loci.common.IniTable;
import loci.common.RandomAccessInputStream;
import loci.formats.FormatException;
import loci.formats.FormatTools;
import loci.formats.MetadataTools;
import loci.formats.meta.MetadataStore;
import loci.formats.tiff.IFD;
import loci.formats.tiff.TiffParser;
import ome.xml.model.primitives.PositiveFloat;
import ome.xml.model.primitives.PositiveInteger;
/**
* SISReader is the file format reader for Olympus Soft Imaging Solutions
* TIFF files.
*
* <dl><dt><b>Source code:</b></dt>
* <dd><a href="http://trac.openmicroscopy.org.uk/ome/browser/bioformats.git/components/bio-formats/src/loci/formats/in/SISReader.java">Trac</a>,
* <a href="http://git.openmicroscopy.org/?p=bioformats.git;a=blob;f=components/bio-formats/src/loci/formats/in/SISReader.java;hb=HEAD">Gitweb</a></dd></dl>
*/
public class SISReader extends BaseTiffReader {
// -- Constants --
private static final int SIS_TAG = 33560;
private static final int SIS_INI_TAG = 33471;
// -- Fields --
private String imageName;
private double magnification;
private String channelName;
private String cameraName;
private double physicalSizeX, physicalSizeY;
private String acquisitionDate;
// -- Constructor --
public SISReader() {
super("Olympus SIS TIFF", new String[] {"tif", "tiff"});
suffixSufficient = false;
domains = new String[] {FormatTools.UNKNOWN_DOMAIN};
}
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
TiffParser tp = new TiffParser(stream);
IFD ifd = tp.getFirstIFD();
if (ifd == null) return false;
return ifd.get(SIS_TAG) != null;
}
/* @see loci.formats.IFormatReader#close(boolean) */
public void close(boolean fileOnly) throws IOException {
super.close(fileOnly);
if (!fileOnly) {
imageName = null;
channelName = null;
cameraName = null;
magnification = 0d;
physicalSizeX = physicalSizeY = 0d;
}
}
// -- Internal BaseTiffReader API methods --
/* @see BaseTiffReader#initStandardMetadata() */
protected void initStandardMetadata() throws FormatException, IOException {
super.initStandardMetadata();
IFD ifd = ifds.get(0);
String iniMetadata = ifd.getIFDTextValue(SIS_INI_TAG);
if (iniMetadata != null) {
IniParser parser = new IniParser();
IniList ini =
parser.parseINI(new BufferedReader(new StringReader(iniMetadata)));
IniTable dimensions = ini.getTable("Dimension");
int z = Integer.parseInt(dimensions.get("Z"));
int c = Integer.parseInt(dimensions.get("Band"));
int t = Integer.parseInt(dimensions.get("Time"));
if (z * c * t == ifds.size()) {
core[0].sizeZ = z;
core[0].sizeT = t;
core[0].sizeC *= c;
}
// TODO : parse more metadata from the INI tables
}
long metadataPointer = ifd.getIFDLongValue(SIS_TAG, 0);
in.seek(metadataPointer);
in.skipBytes(4);
in.skipBytes(6);
int minute = in.readShort();
int hour = in.readShort();
int day = in.readShort();
int month = in.readShort() + 1;
int year = 1900 + in.readShort();
acquisitionDate =
year + "-" + month + "-" + day + " " + hour + ":" + minute;
acquisitionDate = DateTools.formatDate(acquisitionDate, "yyyy-M-d H:m");
in.skipBytes(6);
imageName = in.readCString();
if ((in.getFilePointer() % 2) == 1) {
in.skipBytes(1);
}
short check = in.readShort();
while (check != 7 && check != 8) {
check = in.readShort();
if (check == 0x700 || check == 0x800) {
in.skipBytes(1);
break;
}
}
in.skipBytes(4);
long pos = in.readInt() & 0xffffffffL;
if (pos >= in.length()) {
return;
}
in.seek(pos);
in.skipBytes(12);
physicalSizeX = in.readDouble();
physicalSizeY = in.readDouble();
in.skipBytes(8);
magnification = in.readDouble();
int cameraNameLength = in.readShort();
channelName = in.readCString();
int length = (int) Math.min(cameraNameLength, channelName.length());
if (length > 0) {
cameraName = channelName.substring(0, length);
}
// these are no longer valid
getGlobalMetadata().remove("XResolution");
getGlobalMetadata().remove("YResolution");
addGlobalMeta("Nanometers per pixel (X)", physicalSizeX);
addGlobalMeta("Nanometers per pixel (Y)", physicalSizeY);
addGlobalMeta("Magnification", magnification);
addGlobalMeta("Channel name", channelName);
addGlobalMeta("Camera name", cameraName);
addGlobalMeta("Image name", imageName);
addGlobalMeta("Acquisition date", acquisitionDate);
}
/* @see BaseTiffReader#initMetadataStore() */
protected void initMetadataStore() throws FormatException {
super.initMetadataStore();
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
store.setImageName(imageName, 0);
store.setImageAcquiredDate(acquisitionDate, 0);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
String instrument = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrument, 0);
store.setImageInstrumentRef(instrument, 0);
String objective = MetadataTools.createLSID("Objective", 0, 0);
store.setObjectiveID(objective, 0, 0);
if ((int) magnification > 0) {
store.setObjectiveNominalMagnification(
new PositiveInteger((int) magnification), 0, 0);
}
else {
LOGGER.warn("Expected positive value for NominalMagnification; got {}",
magnification);
}
store.setObjectiveCorrection(getCorrection("Other"), 0, 0);
store.setObjectiveImmersion(getImmersion("Other"), 0, 0);
store.setImageObjectiveSettingsID(objective, 0);
String detector = MetadataTools.createLSID("Detector", 0, 0);
store.setDetectorID(detector, 0, 0);
store.setDetectorModel(cameraName, 0, 0);
store.setDetectorType(getDetectorType("Other"), 0, 0);
store.setDetectorSettingsID(detector, 0, 0);
physicalSizeX /= 1000;
physicalSizeY /= 1000;
if (physicalSizeX > 0.000001) {
store.setPixelsPhysicalSizeX(new PositiveFloat(physicalSizeX), 0);
}
else {
LOGGER.warn("Expected a positive value for PhysicalSizeX; got {}",
physicalSizeX);
}
if (physicalSizeY > 0.000001) {
store.setPixelsPhysicalSizeY(new PositiveFloat(physicalSizeY), 0);
}
else {
LOGGER.warn("Expected a positive value for PhysicalSizeY; got {}",
physicalSizeY);
}
store.setChannelName(channelName, 0, 0);
}
}
}