forked from ome/bioformats
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IvisionReader.java
383 lines (322 loc) · 11.4 KB
/
IvisionReader.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//
// IvisionReader.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.IOException;
import loci.common.DateTools;
import loci.common.RandomAccessInputStream;
import loci.common.xml.BaseHandler;
import loci.common.xml.XMLTools;
import loci.formats.FormatException;
import loci.formats.FormatReader;
import loci.formats.FormatTools;
import loci.formats.MetadataTools;
import loci.formats.meta.MetadataStore;
import ome.xml.model.primitives.PositiveInteger;
import org.xml.sax.Attributes;
/**
* IvisionReader is the file format reader for IVision (.IPM) 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/IvisionReader.java">Trac</a>,
* <a href="http://git.openmicroscopy.org/?p=bioformats.git;a=blob;f=components/bio-formats/src/loci/formats/in/IvisionReader.java;hb=HEAD">Gitweb</a></dd></dl>
*
* @author Melissa Linkert melissa at glencoesoftware.com
*/
public class IvisionReader extends FormatReader {
// -- Constants --
public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
// -- Fields --
private boolean color16;
private boolean squareRoot;
private byte[] lut;
private long imageOffset;
private String binX, binY;
private String creationDate;
private String exposureTime;
private String gain, offset;
private String deltaT;
private Integer magnification;
private Double lensNA, refractiveIndex;
private String wavelength;
private boolean hasPaddingByte = false;
// -- Constructor --
/** Constructs a new Ivision reader. */
public IvisionReader() {
super("IVision", "ipm");
suffixSufficient = false;
suffixNecessary = false;
domains = new String[] {FormatTools.UNKNOWN_DOMAIN};
}
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
final int blockCheckLen = 4096;
if (!FormatTools.validStream(stream, blockCheckLen, true)) return false;
String version = stream.readString(3);
try {
Double.parseDouble(version);
return version.indexOf(".") != -1 && version.indexOf("-") == -1;
}
catch (NumberFormatException e) { }
return false;
}
/**
* @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
*/
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
throws FormatException, IOException
{
FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
int planeSize = getSizeX() * getSizeY() * getSizeC();
if (color16) planeSize = 2 * (planeSize / 3);
else if (squareRoot) planeSize *= 2;
else if (hasPaddingByte) {
planeSize += getSizeX() * getSizeY();
}
else planeSize *= FormatTools.getBytesPerPixel(getPixelType());
in.seek(imageOffset + planeSize * no);
if (color16) {
// TODO
throw new FormatException("16-bit color iVision files are not supported");
}
else if (squareRoot) {
// TODO
throw new FormatException("Square-root iVision files are not supported");
}
else if (hasPaddingByte) {
int next = 0;
in.skipBytes(y * getSizeX() * getSizeC());
for (int row=0; row<h; row++) {
in.skipBytes(x * getSizeC());
for (int col=0; col<w; col++) {
in.skipBytes(1);
in.read(buf, next, getSizeC());
next += getSizeC();
}
in.skipBytes(getSizeC() * (getSizeX() - w - x));
}
}
else readPlane(in, x, y, w, h, buf);
return buf;
}
/* @see loci.formats.IFormatReader#close(boolean) */
public void close(boolean fileOnly) throws IOException {
super.close(fileOnly);
if (!fileOnly) {
color16 = false;
squareRoot = false;
lut = null;
imageOffset = 0;
binX = binY = null;
creationDate = null;
exposureTime = null;
gain = offset = null;
deltaT = null;
magnification = null;
lensNA = refractiveIndex = null;
wavelength = null;
hasPaddingByte = false;
}
}
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
in = new RandomAccessInputStream(id);
LOGGER.info("Populating metadata");
String version = in.readString(4);
addGlobalMeta("Version", version);
int fileFormat = in.read();
int dataType = in.read();
core[0].sizeC = 1;
switch (dataType) {
case 0:
core[0].pixelType = FormatTools.UINT8;
break;
case 1:
core[0].pixelType = FormatTools.INT16;
break;
case 2:
core[0].pixelType = FormatTools.INT32;
break;
case 3:
core[0].pixelType = FormatTools.FLOAT;
break;
case 4:
core[0].pixelType = FormatTools.UINT8;
core[0].sizeC = 3;
color16 = true;
break;
case 5:
core[0].pixelType = FormatTools.UINT8;
core[0].sizeC = 3;
hasPaddingByte = true;
break;
case 6:
core[0].pixelType = FormatTools.UINT16;
break;
case 7:
core[0].pixelType = FormatTools.FLOAT;
squareRoot = true;
break;
case 8:
core[0].pixelType = FormatTools.UINT16;
core[0].sizeC = 3;
break;
}
core[0].sizeX = in.readInt();
core[0].sizeY = in.readInt();
in.skipBytes(6);
core[0].sizeZ = in.readShort();
in.skipBytes(50);
core[0].sizeT = 1;
if (getSizeX() > 1 && getSizeY() > 1) {
lut = new byte[2048];
in.read(lut);
}
imageOffset = in.getFilePointer();
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
in.skipBytes(getSizeZ() * getSizeC() * getSizeT() * getSizeX() *
getSizeY() * FormatTools.getBytesPerPixel(getPixelType()));
// look for block of XML data
LOGGER.info("Looking for XML metadata");
in.findString(false, "<?xml");
if (in.getFilePointer() < in.length()) {
in.seek(in.getFilePointer() - 5);
String xml = in.readString((int) (in.length() - in.getFilePointer()));
xml = xml.substring(xml.indexOf("<"), xml.lastIndexOf("plist>") + 6);
IvisionHandler handler = new IvisionHandler();
try {
XMLTools.parseXML(xml, handler);
}
catch (IOException e) {
LOGGER.debug("", e);
}
}
else LOGGER.debug("XML metadata not found");
}
LOGGER.info("Populating core metadata");
core[0].rgb = getSizeC() > 1;
core[0].dimensionOrder = "XYCZT";
core[0].littleEndian = false;
core[0].interleaved = true;
core[0].indexed = false;
core[0].imageCount = getSizeZ() * getSizeT();
LOGGER.info("Populating MetadataStore");
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this, true);
if (creationDate != null) {
String date = DateTools.formatDate(creationDate, DATE_FORMAT);
store.setImageAcquiredDate(date, 0);
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
String instrumentID = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrumentID, 0);
store.setImageInstrumentRef(instrumentID, 0);
if (deltaT != null) {
Double increment = 0d;
try {
increment = new Double(deltaT);
}
catch (NumberFormatException e) {
LOGGER.debug("Failed to parse time increment", e);
}
store.setPixelsTimeIncrement(increment, 0);
}
String objectiveID = MetadataTools.createLSID("Objective", 0, 0);
store.setObjectiveID(objectiveID, 0, 0);
store.setImageObjectiveSettingsID(objectiveID, 0);
store.setObjectiveCorrection(getCorrection("Other"), 0, 0);
store.setObjectiveImmersion(getImmersion("Other"), 0, 0);
if (lensNA != null) store.setObjectiveLensNA(lensNA, 0, 0);
if (magnification != null && magnification > 0) {
store.setObjectiveNominalMagnification(
new PositiveInteger(magnification), 0, 0);
}
else {
LOGGER.warn("Expected positive value for NominalMagnification; got {}",
magnification);
}
if (refractiveIndex != null) {
store.setImageObjectiveSettingsRefractiveIndex(refractiveIndex, 0);
}
String detectorID = MetadataTools.createLSID("Detector", 0, 0);
store.setDetectorID(detectorID, 0, 0);
store.setDetectorSettingsID(detectorID, 0, 0);
store.setDetectorType(getDetectorType("Other"), 0, 0);
store.setDetectorSettingsBinning(getBinning(binX + "x" + binY), 0, 0);
if (gain != null) {
try {
store.setDetectorSettingsGain(new Double(gain), 0, 0);
}
catch (NumberFormatException e) {
LOGGER.debug("Failed to parse detector gain", e);
}
}
}
}
// -- Helper class --
class IvisionHandler extends BaseHandler {
// -- Fields --
private String key, value;
private String currentElement;
// -- DefaultHandler API methods --
public void endElement(String uri, String localName, String qName) {
addGlobalMeta(key, value);
if ("iplab:Bin_X".equals(key)) binX = value;
else if ("iplab:Bin_Y".equals(key)) binY = value;
else if ("iplab:Capture_Date".equals(key)) creationDate = value;
else if ("iplab:Exposure".equals(key)) exposureTime = value;
else if ("iplab:Gain".equals(key)) gain = value;
else if ("iplab:Offset".equals(key)) offset = value;
else if ("iplab:Interval_T".equals(key)) deltaT = value;
else if ("iplab:Objective_Mag".equals(key)) {
try {
magnification = new Integer((int) Double.parseDouble(value));
}
catch (NumberFormatException e) { }
}
else if ("iplab:Objective_NA".equals(key)) {
try {
lensNA = new Double(value);
}
catch (NumberFormatException e) { }
}
else if ("iplab:Objective_RI".equals(key)) {
try {
refractiveIndex = new Double(value);
}
catch (NumberFormatException e) { }
}
else if ("iplab:Wavelength".equals(key)) wavelength = value;
}
public void characters(char[] ch, int start, int length) {
String v = new String(ch, start, length);
if ("key".equals(currentElement)) {
key = v;
}
else value = v;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes)
{
currentElement = qName;
}
}
}