-
Notifications
You must be signed in to change notification settings - Fork 242
/
NikonTiffReader.java
317 lines (276 loc) · 10.6 KB
/
NikonTiffReader.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
/*
* #%L
* OME Bio-Formats package for reading and converting biological file formats.
* %%
* Copyright (C) 2005 - 2017 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* 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, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
package loci.formats.in;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import loci.common.DataTools;
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.units.quantity.Length;
import ome.units.UNITS;
/**
* NikonTiffReader is the file format reader for Nikon TIFF files.
*/
public class NikonTiffReader extends BaseTiffReader {
// -- Constants --
private static final String[] TOP_LEVEL_KEYS = new String[] {
"document document", "document", "history Acquisition", "history objective",
"history history", "history laser", "history step", "history",
"sensor s_params", "sensor", "view"
};
// -- Fields --
private double physicalSizeX, physicalSizeY, physicalSizeZ;
private List<String> filterModels, dichroicModels, laserIDs;
private Double magnification;
private double lensNA, workingDistance, pinholeSize;
private String correction, immersion;
private List<Double> gain;
private List<Double> wavelength, emWave, exWave;
// -- Constructor --
public NikonTiffReader() {
super("Nikon TIFF", new String[] {"tif", "tiff"});
suffixSufficient = false;
domains = new String[] {FormatTools.LM_DOMAIN};
}
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
TiffParser tp = new TiffParser(stream);
IFD ifd = tp.getFirstIFD();
if (ifd == null) return false;
String software = ifd.getIFDTextValue(IFD.SOFTWARE);
return software != null && software.toString().indexOf("EZ-C1") != -1;
}
/* @see loci.formats.IFormatReader#close(boolean) */
@Override
public void close(boolean fileOnly) throws IOException {
super.close(fileOnly);
if (!fileOnly) {
physicalSizeX = physicalSizeY = physicalSizeZ = 0;
dichroicModels = filterModels = laserIDs = null;
magnification = null;
lensNA = workingDistance = pinholeSize = 0;
correction = immersion = null;
gain = null;
wavelength = emWave = exWave = null;
}
}
// -- Internal BaseTiffReader API methods --
/* @see BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
super.initStandardMetadata();
if (getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM) {
return;
}
filterModels = new ArrayList<String>();
dichroicModels = new ArrayList<String>();
laserIDs = new ArrayList<String>();
gain = new ArrayList<Double>();
wavelength = new ArrayList<Double>();
emWave = new ArrayList<Double>();
exWave = new ArrayList<Double>();
// parse key/value pairs in the comment
String comment = ifds.get(0).getComment();
metadata.remove("Comment");
String[] lines = comment.split("\n");
String[] dimensionLabels = null, dimensionSizes = null;
for (String line : lines) {
String[] tokens = line.split("\t");
line = line.replaceAll("\t", " ");
int nTokensInKey = 0;
for (String key : TOP_LEVEL_KEYS) {
if (line.startsWith(key)) {
nTokensInKey = key.indexOf(' ') != -1 ? 3 : 2;
break;
}
}
final StringBuilder k = new StringBuilder();
for (int i=0; i<nTokensInKey; i++) {
k.append(tokens[i]);
if (i < nTokensInKey - 1) k.append(" ");
}
final StringBuilder v = new StringBuilder();
for (int i=nTokensInKey; i<tokens.length; i++) {
v.append(tokens[i]);
if (i < tokens.length - 1) v.append(" ");
}
String key = k.toString();
String value = v.toString();
if (key.equals("document label")) {
dimensionLabels = value.toLowerCase().split(" ");
}
else if (key.equals("document scale")) {
dimensionSizes = value.split(" ");
}
else if (key.startsWith("history Acquisition") &&
key.indexOf("Filter") != -1)
{
filterModels.add(value);
}
else if (key.startsWith("history Acquisition") &&
key.indexOf("Dichroic") != -1)
{
dichroicModels.add(value);
}
else if (key.equals("history objective Type")) {
correction = value;
}
else if (key.equals("history objective Magnification")) {
magnification = DataTools.parseDouble(value);
}
else if (key.equals("history objective NA")) {
lensNA = Double.parseDouble(value);
}
else if (key.equals("history objective WorkingDistance")) {
workingDistance = Double.parseDouble(value);
}
else if (key.equals("history objective Immersion")) {
immersion = value;
}
else if (key.startsWith("history gain")) {
gain.add(DataTools.parseDouble(value));
}
else if (key.equals("history pinhole")) {
pinholeSize = DataTools.parseDouble(value.substring(0, value.indexOf(' ')));
}
else if (key.startsWith("history laser") && key.endsWith("wavelength")) {
wavelength.add(DataTools.parseDouble(value.replaceAll("\\D", "")));
}
else if (key.startsWith("history laser") && key.endsWith("name")) {
laserIDs.add(value);
}
else if (key.equals("sensor s_params LambdaEx")) {
for (int i=nTokensInKey; i<tokens.length; i++) {
exWave.add(DataTools.parseDouble(tokens[i]));
}
}
else if (key.equals("sensor s_params LambdaEm")) {
for (int i=nTokensInKey; i<tokens.length; i++) {
emWave.add(DataTools.parseDouble(tokens[i]));
}
}
addGlobalMeta(key, value);
}
parseDimensionSizes(dimensionLabels, dimensionSizes);
}
/* @see BaseTiffReader#initMetadataStore() */
@Override
protected void initMetadataStore() throws FormatException {
super.initMetadataStore();
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
store.setImageDescription("", 0);
Length sizeX = FormatTools.getPhysicalSizeX(physicalSizeX);
Length sizeY = FormatTools.getPhysicalSizeY(physicalSizeY);
Length sizeZ = FormatTools.getPhysicalSizeZ(physicalSizeZ);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
if (sizeZ != null) {
store.setPixelsPhysicalSizeZ(sizeZ, 0);
}
String instrumentID = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrumentID, 0);
store.setImageInstrumentRef(instrumentID, 0);
String objectiveID = MetadataTools.createLSID("Objective", 0, 0);
store.setObjectiveID(objectiveID, 0, 0);
store.setObjectiveSettingsID(objectiveID, 0);
store.setObjectiveNominalMagnification(magnification, 0, 0);
if (correction == null) correction = "Other";
store.setObjectiveCorrection(MetadataTools.getCorrection(correction), 0, 0);
store.setObjectiveLensNA(lensNA, 0, 0);
store.setObjectiveWorkingDistance(new Length(workingDistance, UNITS.MICROMETER), 0, 0);
if (immersion == null) immersion = "Other";
store.setObjectiveImmersion(MetadataTools.getImmersion(immersion), 0, 0);
for (int i=0; i<wavelength.size(); i++) {
String laser = MetadataTools.createLSID("LightSource", 0, i);
store.setLaserID(laser, 0, i);
store.setLaserModel(laserIDs.get(i), 0, i);
Length wave = FormatTools.getWavelength(wavelength.get(i));
if (wave != null) {
store.setLaserWavelength(wave, 0, i);
}
store.setLaserType(MetadataTools.getLaserType("Other"), 0, i);
store.setLaserLaserMedium(MetadataTools.getLaserMedium("Other"), 0, i);
}
for (int i=0; i<gain.size(); i++) {
store.setDetectorID(MetadataTools.createLSID("Detector", 0, i), 0, i);
store.setDetectorGain(gain.get(i), 0, i);
store.setDetectorType(MetadataTools.getDetectorType("Other"), 0, i);
}
for (int c=0; c<getEffectiveSizeC(); c++) {
store.setChannelPinholeSize(new Length(pinholeSize, UNITS.MICROMETER), 0, c);
if (c < exWave.size()) {
Length wave = FormatTools.getExcitationWavelength(exWave.get(c));
if (wave != null) {
store.setChannelExcitationWavelength(wave, 0, c);
}
}
if (c < emWave.size()) {
Length wave = FormatTools.getEmissionWavelength(emWave.get(c));
if (wave != null) {
store.setChannelEmissionWavelength(wave, 0, c);
}
}
}
for (int i=0; i<filterModels.size(); i++) {
String filter = MetadataTools.createLSID("Filter", 0, i);
store.setFilterID(filter, 0, i);
store.setFilterModel(filterModels.get(i), 0, i);
}
for (int i=0; i<dichroicModels.size(); i++) {
String dichroic = MetadataTools.createLSID("Dichroic", 0, i);
store.setDichroicID(dichroic, 0, i);
store.setDichroicModel(dichroicModels.get(i), 0, i);
}
}
}
// -- Helper methods --
private void parseDimensionSizes(String[] labels, String[] sizes) {
for (int i=0; i<labels.length; i++) {
if (labels[i].startsWith("z")) {
physicalSizeZ = Double.parseDouble(sizes[i]);
}
else if (labels[i].equals("x")) {
physicalSizeX = Double.parseDouble(sizes[i]);
}
else if (labels[i].equals("y")) {
physicalSizeY = Double.parseDouble(sizes[i]);
}
}
}
}