/
AliconaReader.java
292 lines (236 loc) · 8.88 KB
/
AliconaReader.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
/*
* #%L
* OME Bio-Formats package for reading and converting biological file formats.
* %%
* Copyright (C) 2005 - 2016 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 loci.common.RandomAccessInputStream;
import loci.formats.CoreMetadata;
import loci.formats.FormatException;
import loci.formats.FormatReader;
import loci.formats.FormatTools;
import loci.formats.MetadataTools;
import loci.formats.meta.MetadataStore;
import ome.units.quantity.ElectricPotential;
import ome.units.quantity.Length;
import ome.units.UNITS;
import ome.xml.model.primitives.PositiveFloat;
/**
* AliconaReader is the file format reader for Alicona AL3D files.
*/
public class AliconaReader extends FormatReader {
// -- Constants --
public static final String AL3D_MAGIC_STRING = "Alicona";
// -- Fields --
/** Image offset. */
private int textureOffset;
/** Number of bytes per pixel (either 1 or 2). */
private int numBytes;
// -- Constructor --
/** Constructs a new Alicona reader. */
public AliconaReader() {
super("Alicona AL3D", "al3d");
domains = new String[] {FormatTools.SEM_DOMAIN};
}
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
final int blockLen = 16;
if (!FormatTools.validStream(stream, blockLen, false)) return false;
return (stream.readString(blockLen)).indexOf(AL3D_MAGIC_STRING) >= 0;
}
/**
* @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
*/
@Override
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 pad = (8 - (getSizeX() % 8)) % 8;
int planeSize = (getSizeX() + pad) * getSizeY();
if (getPixelType() == FormatTools.FLOAT) {
in.seek(textureOffset);
readPlane(in, x, y, w, h, buf);
return buf;
}
// 16-bit images are stored in a non-standard format:
// all of the LSBs are stored together, followed by all of the MSBs
// so instead of LMLMLM... storage, we have LLLLL...MMMMM...
for (int i=0; i<numBytes; i++) {
in.seek(textureOffset + (no * planeSize * (i + 1)));
in.skipBytes(y * (getSizeX() + pad));
if (getSizeX() == w) {
in.read(buf, i * w * h, w * h);
}
else {
for (int row=0; row<h; row++) {
in.skipBytes(x);
in.read(buf, i * w * h + row * w, w);
in.skipBytes(getSizeX() + pad - x - w);
}
}
}
if (numBytes > 1) {
byte[] tmp = new byte[buf.length];
for (int i=0; i<planeSize; i++) {
for (int j=0; j<numBytes; j++) {
tmp[i*numBytes + j] = buf[planeSize*j + i];
}
}
System.arraycopy(tmp, 0, buf, 0, tmp.length);
tmp = null;
}
return buf;
}
/* @see loci.formats.IFormatReader#close(boolean) */
@Override
public void close(boolean fileOnly) throws IOException {
super.close(fileOnly);
if (!fileOnly) {
textureOffset = numBytes = 0;
}
}
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
in = new RandomAccessInputStream(id);
CoreMetadata m = core.get(0);
// check that this is a valid AL3D file
LOGGER.info("Verifying Alicona format");
String magicString = in.readString(17);
if (!magicString.trim().equals("AliconaImaging")) {
throw new FormatException("Invalid magic string : " +
"expected 'AliconaImaging', got " + magicString);
}
// now we read a series of tags
// each one is 52 bytes - 20 byte key + 30 byte value + 2 byte CRLF
LOGGER.info("Reading tags");
int count = 2;
boolean hasC = false;
String voltage = null, magnification = null, workingDistance = null;
String pntX = null, pntY = null;
int depthOffset = 0;
for (int i=0; i<count; i++) {
String key = in.readString(20).trim();
String value = in.readString(30).trim();
addGlobalMeta(key, value);
in.skipBytes(2);
if (key.equals("TagCount")) count += Integer.parseInt(value);
else if (key.equals("Rows")) m.sizeY = Integer.parseInt(value);
else if (key.equals("Cols")) m.sizeX = Integer.parseInt(value);
else if (key.equals("NumberOfPlanes")) {
m.imageCount = Integer.parseInt(value);
}
else if (key.equals("TextureImageOffset")) {
textureOffset = Integer.parseInt(value);
}
else if (key.equals("TexturePtr") && !value.equals("7")) hasC = true;
else if (key.equals("Voltage")) voltage = value;
else if (key.equals("Magnification")) magnification = value;
else if (key.equals("PixelSizeXMeter")) pntX = value;
else if (key.equals("PixelSizeYMeter")) pntY = value;
else if (key.equals("WorkingDistance")) workingDistance = value;
else if (key.equals("DepthImageOffset")) {
depthOffset = Integer.parseInt(value);
}
}
LOGGER.info("Populating metadata");
if (textureOffset != 0) {
numBytes = (int) (in.length() - textureOffset) /
(getSizeX() * getSizeY() * getImageCount());
m.sizeC = hasC ? 3 : 1;
m.sizeZ = 1;
m.sizeT = getImageCount() / getSizeC();
m.pixelType =
FormatTools.pixelTypeFromBytes(numBytes, false, false);
}
else {
textureOffset = depthOffset;
m.pixelType = FormatTools.FLOAT;
m.sizeC = 1;
m.sizeZ = 1;
m.sizeT = 1;
m.imageCount = 1;
}
m.rgb = false;
m.interleaved = false;
m.littleEndian = true;
m.dimensionOrder = "XYCTZ";
m.metadataComplete = true;
m.indexed = false;
m.falseColor = false;
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
// populate Image data
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
// link Image and Instrument
String instrumentID = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrumentID, 0);
store.setImageInstrumentRef(instrumentID, 0);
// populate Detector data
// According to the spec, the voltage and magnification values are those
// used when the dataset was acquired, i.e. detector settings.
if (voltage != null) {
store.setDetectorSettingsVoltage(
new ElectricPotential(new Double(voltage), UNITS.V), 0, 0);
// link DetectorSettings to an actual Detector
String detectorID = MetadataTools.createLSID("Detector", 0, 0);
store.setDetectorID(detectorID, 0, 0);
store.setDetectorSettingsID(detectorID, 0, 0);
// set required Detector type
store.setDetectorType(getDetectorType("Other"), 0, 0);
}
// populate Objective data
if (magnification != null) {
store.setObjectiveCalibratedMagnification(
new Double(magnification), 0, 0);
}
if (workingDistance != null) {
store.setObjectiveWorkingDistance(new Length(new Double(workingDistance), UNITS.MICROM), 0, 0);
}
store.setObjectiveCorrection(getCorrection("Other"), 0, 0);
store.setObjectiveImmersion(getImmersion("Other"), 0, 0);
// link Objective to an Image using ObjectiveSettings
String objectiveID = MetadataTools.createLSID("Objective", 0, 0);
store.setObjectiveID(objectiveID, 0, 0);
store.setObjectiveSettingsID(objectiveID, 0);
// populate Dimensions data
if (pntX != null && pntY != null) {
double pixelSizeX = Double.parseDouble(pntX);
double pixelSizeY = Double.parseDouble(pntY);
Length sizeX = FormatTools.getPhysicalSizeX(pixelSizeX, UNITS.METRE);
Length sizeY = FormatTools.getPhysicalSizeY(pixelSizeY, UNITS.METRE);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
}
}
}
}