-
Notifications
You must be signed in to change notification settings - Fork 241
/
CellH5Writer.java
251 lines (216 loc) · 8.99 KB
/
CellH5Writer.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
/*
* #%L
* BSD implementations of Bio-Formats readers and writers
* %%
* Copyright (C) 2005 - 2017 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package loci.formats.out;
import ch.systemsx.cisd.base.mdarray.MDByteArray;
import ch.systemsx.cisd.base.mdarray.MDIntArray;
import ch.systemsx.cisd.base.mdarray.MDShortArray;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import loci.common.services.DependencyException;
import loci.common.services.ServiceFactory;
import loci.formats.FormatException;
import loci.formats.FormatTools;
import loci.formats.FormatWriter;
import loci.formats.MissingLibraryException;
import loci.formats.in.CellH5Reader;
import loci.formats.in.CellH5Reader.CellH5Constants;
import loci.formats.meta.MetadataRetrieve;
import loci.formats.services.JHDFService;
import loci.formats.services.JHDFServiceImpl;
import ome.xml.model.enums.DimensionOrder;
/**
* CellH5Writer is the file format writer for CellH5 format.
*/
public class CellH5Writer extends FormatWriter {
// -- Constants --
// -- Fields --
private transient JHDFService jhdf;
private long bpp;
private String outputPath;
// -- Constructors --
public CellH5Writer() {
this("CellH5 File Format", new String[] {"ch5"});
}
public CellH5Writer(String format, String[] exts) {
super(format, exts);
}
// -- FormatWriter API methods --
/* @see loci.formats.FormatWriter#setId(String) */
@Override
public void setId(String id) throws FormatException, IOException {
if (id.equals(currentId)) return;
super.setId(id);
try {
ServiceFactory factory = new ServiceFactory();
jhdf = factory.getInstance(JHDFService.class);
jhdf.setFileForWrite(id);
} catch (DependencyException e) {
throw new MissingLibraryException(JHDFServiceImpl.NO_JHDF_MSG, e);
}
MetadataRetrieve retrieve = getMetadataRetrieve();
int sizeX = retrieve.getPixelsSizeX(0).getValue();
int sizeY = retrieve.getPixelsSizeY(0).getValue();
int sizeZ = retrieve.getPixelsSizeZ(0).getValue();
int sizeC = retrieve.getPixelsSizeC(0).getValue();
int sizeT = retrieve.getPixelsSizeT(0).getValue();
int type = FormatTools.pixelTypeFromString(retrieve.getPixelsType(0).toString());
bpp = FormatTools.getBytesPerPixel(type);
LOGGER.info("CellH5Writer: Found image with dimensions XYZCT {}x{}x{}x{}x{}, and bits per pixel {}", sizeX, sizeY, sizeZ, sizeC, sizeT, bpp);
String plate = "PLATE_00";
String well = "WELL_00";
int site = 1;
if (retrieve.getPlateCount()>0) {
plate = retrieve.getPlateName(0);
well = retrieve.getWellExternalIdentifier(0, 0);
site = retrieve.getWellSampleIndex(0, 0, 0).getValue();
LOGGER.info("CellH5Writer: Found plate information Plate / Well / Site {} / {} / {}", plate, well, site);
} else {
LOGGER.info("CellH5Writer: No plate information found. Using default values...");
}
jhdf.createGroup(CellH5Constants.DEFINITION + CellH5Reader.CellH5Constants.OBJECT);
jhdf.createGroup(CellH5Constants.DEFINITION + CellH5Reader.CellH5Constants.FEATURE);
jhdf.createGroup(CellH5Constants.DEFINITION + CellH5Reader.CellH5Constants.IMAGE);
outputPath = String.format("/sample/0/plate/%s/experiment/%s/position/%d/image/channel", plate, well, site);
jhdf.initIntArray(outputPath, new long[] {sizeC, sizeT, sizeZ, sizeY, sizeX}, bpp);
}
/**
* Saves the given image to the specified (possibly already open) file.
*/
@Override
public void saveBytes(int no, byte[] buf)
throws IOException, FormatException
{
LOGGER.info("CellH5Writer: Save image to HDF5 path: " + outputPath);
MetadataRetrieve r = getMetadataRetrieve();
int sizeX = r.getPixelsSizeX(series).getValue();
int sizeY = r.getPixelsSizeY(series).getValue();
int sizeC = r.getPixelsSizeC(series).getValue();
int sizeT = r.getPixelsSizeT(series).getValue();
int sizeZ = r.getPixelsSizeZ(series).getValue();
DimensionOrder dimo = r.getPixelsDimensionOrder(0);
int c, z, t;
if (dimo.equals(DimensionOrder.XYCZT)) {
c = no % sizeC;
z = ((no - c) / sizeC) % sizeZ;
t = (((no - c) / sizeC)) / sizeZ;
} else if (dimo.equals(DimensionOrder.XYCTZ)){
c = no % sizeC;
t = ((no - c) / sizeC) % sizeT;
z = (((no - c) / sizeC)) / sizeT;
} else if (dimo.equals(DimensionOrder.XYZTC)){
z = no % sizeZ;
t = ((no - z) / sizeZ) % sizeT;
c = (((no - z) / sizeZ)) / sizeT;
} else {
throw new FormatException("CellH5Writer: Dimension order not understood: " + dimo.getValue());
}
LOGGER.info("CellH5Writer.saveBytes(): Current c, t, z == {} {} {}", c,t,z);
LOGGER.info("CellH5Writer.saveBytes(): bpp {} byte buffer len {}", bpp, buf.length);
if (bpp==1) {
MDByteArray image = new MDByteArray(new int[] {1, 1, 1, sizeY, sizeX});
for (int x_i=0; x_i < sizeX; x_i++) {
for (int y_i=0; y_i < sizeY; y_i++) {
byte value = (byte) buf[y_i*sizeX + x_i];
image.set(value, 0, 0, 0, y_i, x_i );
}
}
jhdf.writeArraySlice(outputPath, image, new long[] {c,t,z, 0, 0});
} else if (bpp==2) {
ByteBuffer bb = ByteBuffer.wrap(buf);
ShortBuffer sb = bb.asShortBuffer();
MDShortArray image = new MDShortArray(new int[] {1, 1, 1, sizeY, sizeX});
for (int x_i=0; x_i < sizeX; x_i++) {
for (int y_i=0; y_i < sizeY; y_i++) {
short value = sb.get(y_i*sizeX + x_i);
image.set(value, 0, 0, 0, y_i, x_i );
}
}
jhdf.writeArraySlice(outputPath, image, new long[] {c,t,z, 0, 0});
} else if (bpp==4) {
ByteBuffer bb = ByteBuffer.wrap(buf);
IntBuffer ib = bb.asIntBuffer();
MDIntArray image = new MDIntArray(new int[] {1, 1, 1, sizeY, sizeX});
for (int x_i=0; x_i < sizeX; x_i++) {
for (int y_i=0; y_i < sizeY; y_i++) {
int value = (int) ib.get(y_i*sizeX + x_i);
image.set(value, 0, 0, 0, y_i, x_i );
}
}
jhdf.writeArraySlice(outputPath, image, new long[] {c,t,z, 0, 0});
} else {
throw new FormatException("CellH5Writer: Pixel type not supported");
}
}
// -- FormatWriter API methods --
/* (non-Javadoc)
* @see loci.formats.FormatWriter#close()
*/
@Override
public void close() throws IOException {
jhdf.close();
super.close();
}
/* @see loci.formats.FormatWriter#getPlaneCount() */
@Override
public int getPlaneCount() {
MetadataRetrieve retrieve = getMetadataRetrieve();
int c = getSamplesPerPixel();
int type = FormatTools.pixelTypeFromString(
retrieve.getPixelsType(series).toString());
int bytesPerPixel = FormatTools.getBytesPerPixel(type);
if (bytesPerPixel > 1 && c != 1 && c != 3) {
return super.getPlaneCount() * c;
}
return super.getPlaneCount();
}
// -- IFormatWriter API methods --
/**
* @see loci.formats.IFormatWriter#saveBytes(int, byte[], int, int, int, int)
*/
@Override
public void saveBytes(int no, byte[] buf, int x, int y, int w, int h)
throws FormatException, IOException
{
// always save to full image
saveBytes(no, buf);
}
/* @see loci.formats.IFormatWriter#canDoStacks(String) */
@Override
public boolean canDoStacks() { return true; }
/* @see loci.formats.IFormatWriter#getPixelTypes(String) */
@Override
public int[] getPixelTypes(String codec) {
return new int[] {FormatTools.UINT8, FormatTools.UINT16, FormatTools.INT32};
}
}