-
Notifications
You must be signed in to change notification settings - Fork 218
/
RoiEncoder.java
444 lines (404 loc) · 13.8 KB
/
RoiEncoder.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package ij.io;
import ij.gui.*;
import ij.process.FloatPolygon;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.awt.geom.*;
/** Saves an ROI to a file or stream. RoiDecoder.java has a description of the file format.
@see ij.io.RoiDecoder
@see ij.plugin.RoiReader
*/
public class RoiEncoder {
static final int HEADER_SIZE = 64;
static final int HEADER2_SIZE = 64;
static final int VERSION = 227; // v1.50d (point counters)
private String path;
private OutputStream f;
private final int polygon=0, rect=1, oval=2, line=3, freeline=4, polyline=5, noRoi=6, freehand=7,
traced=8, angle=9, point=10;
private byte[] data;
private String roiName;
private int roiNameSize;
private String roiProps;
private int roiPropsSize;
private int countersSize;
private int[] counters;
/** Creates an RoiEncoder using the specified path. */
public RoiEncoder(String path) {
this.path = path;
}
/** Creates an RoiEncoder using the specified OutputStream. */
public RoiEncoder(OutputStream f) {
this.f = f;
}
/** Saves the specified ROI as a file, returning 'true' if successful. */
public static boolean save(Roi roi, String path) {
RoiEncoder re = new RoiEncoder(path);
try {
re.write(roi);
} catch (IOException e) {
return false;
}
return true;
}
/** Save the Roi to the file of stream. */
public void write(Roi roi) throws IOException {
if (f!=null) {
write(roi, f);
} else {
f = new FileOutputStream(path);
write(roi, f);
f.close();
}
}
/** Saves the specified ROI as a byte array. */
public static byte[] saveAsByteArray(Roi roi) {
if (roi==null) return null;
byte[] bytes = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
RoiEncoder encoder = new RoiEncoder(out);
encoder.write(roi);
out.close();
bytes = out.toByteArray();
} catch (IOException e) {
return null;
}
return bytes;
}
void write(Roi roi, OutputStream f) throws IOException {
Rectangle r = roi.getBounds();
if (r.width>65535||r.height>65535||r.x>65535||r.y>65535)
roi.enableSubPixelResolution();
int roiType = roi.getType();
int type = rect;
int options = 0;
roiName = roi.getName();
if (roiName!=null)
roiNameSize = roiName.length()*2;
else
roiNameSize = 0;
roiProps = roi.getProperties();
if (roiProps!=null)
roiPropsSize = roiProps.length()*2;
else
roiPropsSize = 0;
switch (roiType) {
case Roi.POLYGON: type=polygon; break;
case Roi.FREEROI: type=freehand; break;
case Roi.TRACED_ROI: type=traced; break;
case Roi.OVAL: type=oval; break;
case Roi.LINE: type=line; break;
case Roi.POLYLINE: type=polyline; break;
case Roi.FREELINE: type=freeline; break;
case Roi.ANGLE: type=angle; break;
case Roi.COMPOSITE: type=rect; break; // shape array size (36-39) will be >0 to indicate composite type
case Roi.POINT: type=point; break;
default: type = rect; break;
}
if (roiType==Roi.COMPOSITE) {
saveShapeRoi(roi, type, f, options);
return;
}
int n=0;
int[] x=null, y=null;
float[] xf=null, yf=null;
int floatSize = 0;
if (roi instanceof PolygonRoi) {
PolygonRoi proi = (PolygonRoi)roi;
Polygon p = proi.getNonSplineCoordinates();
n = p.npoints;
x = p.xpoints;
y = p.ypoints;
if (roi.subPixelResolution()) {
FloatPolygon fp = null;
if (proi.isSplineFit())
fp = proi.getNonSplineFloatPolygon();
else
fp = roi.getFloatPolygon();
if (n==fp.npoints) {
options |= RoiDecoder.SUB_PIXEL_RESOLUTION;
if (roi.getDrawOffset())
options |= RoiDecoder.DRAW_OFFSET;
xf = fp.xpoints;
yf = fp.ypoints;
floatSize = n*8;
}
}
}
if (roi instanceof PointRoi) {
countersSize = 0;
counters = ((PointRoi)roi).getCounters();
if (counters!=null && counters.length>=n)
countersSize = n*4;
}
data = new byte[HEADER_SIZE+HEADER2_SIZE+n*4+floatSize+roiNameSize+roiPropsSize+countersSize];
data[0]=73; data[1]=111; data[2]=117; data[3]=116; // "Iout"
putShort(RoiDecoder.VERSION_OFFSET, VERSION);
data[RoiDecoder.TYPE] = (byte)type;
putShort(RoiDecoder.TOP, r.y);
putShort(RoiDecoder.LEFT, r.x);
putShort(RoiDecoder.BOTTOM, r.y+r.height);
putShort(RoiDecoder.RIGHT, r.x+r.width);
if (roi.subPixelResolution() && (type==rect||type==oval)) {
FloatPolygon p = roi.getFloatPolygon();
if (p.npoints==4) {
putFloat(RoiDecoder.XD, p.xpoints[0]);
putFloat(RoiDecoder.YD, p.ypoints[0]);
putFloat(RoiDecoder.WIDTHD, p.xpoints[1]-p.xpoints[0]);
putFloat(RoiDecoder.HEIGHTD, p.ypoints[2]-p.ypoints[1]);
options |= RoiDecoder.SUB_PIXEL_RESOLUTION;
putShort(RoiDecoder.OPTIONS, options);
}
}
putShort(RoiDecoder.N_COORDINATES, n);
putInt(RoiDecoder.POSITION, roi.getPosition());
if (type==rect) {
int arcSize = roi.getCornerDiameter();
if (arcSize>0)
putShort(RoiDecoder.ROUNDED_RECT_ARC_SIZE, arcSize);
}
if (roi instanceof Line) {
Line line = (Line)roi;
putFloat(RoiDecoder.X1, (float)line.x1d);
putFloat(RoiDecoder.Y1, (float)line.y1d);
putFloat(RoiDecoder.X2, (float)line.x2d);
putFloat(RoiDecoder.Y2, (float)line.y2d);
if (roi instanceof Arrow) {
putShort(RoiDecoder.SUBTYPE, RoiDecoder.ARROW);
if (((Arrow)roi).getDoubleHeaded())
options |= RoiDecoder.DOUBLE_HEADED;
if (((Arrow)roi).getOutline())
options |= RoiDecoder.OUTLINE;
putShort(RoiDecoder.OPTIONS, options);
putByte(RoiDecoder.ARROW_STYLE, ((Arrow)roi).getStyle());
putByte(RoiDecoder.ARROW_HEAD_SIZE, (int)((Arrow)roi).getHeadSize());
} else {
if (roi.getDrawOffset())
options |= RoiDecoder.SUB_PIXEL_RESOLUTION+RoiDecoder.DRAW_OFFSET;
}
}
if (roi instanceof PointRoi) {
PointRoi point = (PointRoi)roi;
putByte(RoiDecoder.POINT_TYPE, point.getPointType());
putShort(RoiDecoder.STROKE_WIDTH, point.getSize());
}
if (roi instanceof RotatedRectRoi || roi instanceof EllipseRoi) {
double[] p = null;
if (roi instanceof RotatedRectRoi) {
putShort(RoiDecoder.SUBTYPE, RoiDecoder.ROTATED_RECT);
p = ((RotatedRectRoi)roi).getParams();
} else {
putShort(RoiDecoder.SUBTYPE, RoiDecoder.ELLIPSE);
p = ((EllipseRoi)roi).getParams();
}
putFloat(RoiDecoder.X1, (float)p[0]);
putFloat(RoiDecoder.Y1, (float)p[1]);
putFloat(RoiDecoder.X2, (float)p[2]);
putFloat(RoiDecoder.Y2, (float)p[3]);
putFloat(RoiDecoder.FLOAT_PARAM, (float)p[4]);
}
// save stroke width, stroke color and fill color (1.43i or later)
if (VERSION>=218) {
saveStrokeWidthAndColor(roi);
if ((roi instanceof PolygonRoi) && ((PolygonRoi)roi).isSplineFit()) {
options |= RoiDecoder.SPLINE_FIT;
putShort(RoiDecoder.OPTIONS, options);
}
}
if (n==0 && roi instanceof TextRoi)
saveTextRoi((TextRoi)roi);
else if (n==0 && roi instanceof ImageRoi)
options = saveImageRoi((ImageRoi)roi, options);
else
putHeader2(roi, HEADER_SIZE+n*4+floatSize);
if (n>0) {
int base1 = 64;
int base2 = base1+2*n;
for (int i=0; i<n; i++) {
putShort(base1+i*2, x[i]);
putShort(base2+i*2, y[i]);
}
if (xf!=null) {
base1 = 64+4*n;
base2 = base1+4*n;
for (int i=0; i<n; i++) {
putFloat(base1+i*4, xf[i]);
putFloat(base2+i*4, yf[i]);
}
}
}
saveOverlayOptions(roi, options);
f.write(data);
}
void saveStrokeWidthAndColor(Roi roi) {
BasicStroke stroke = roi.getStroke();
if (stroke!=null)
putShort(RoiDecoder.STROKE_WIDTH, (int)stroke.getLineWidth());
Color strokeColor = roi.getStrokeColor();
if (strokeColor!=null)
putInt(RoiDecoder.STROKE_COLOR, strokeColor.getRGB());
Color fillColor = roi.getFillColor();
if (fillColor!=null)
putInt(RoiDecoder.FILL_COLOR, fillColor.getRGB());
}
void saveShapeRoi(Roi roi, int type, OutputStream f, int options) throws IOException {
float[] shapeArray = ((ShapeRoi)roi).getShapeAsArray();
if (shapeArray==null) return;
BufferedOutputStream bout = new BufferedOutputStream(f);
Rectangle r = roi.getBounds();
data = new byte[HEADER_SIZE+HEADER2_SIZE+shapeArray.length*4+roiNameSize+roiPropsSize];
data[0]=73; data[1]=111; data[2]=117; data[3]=116; // "Iout"
putShort(RoiDecoder.VERSION_OFFSET, VERSION);
data[RoiDecoder.TYPE] = (byte)type;
putShort(RoiDecoder.TOP, r.y);
putShort(RoiDecoder.LEFT, r.x);
putShort(RoiDecoder.BOTTOM, r.y+r.height);
putShort(RoiDecoder.RIGHT, r.x+r.width);
putInt(RoiDecoder.POSITION, roi.getPosition());
//putShort(16, n);
putInt(36, shapeArray.length); // non-zero segment count indicate composite type
if (VERSION>=218) saveStrokeWidthAndColor(roi);
saveOverlayOptions(roi, options);
// handle the actual data: data are stored segment-wise, i.e.,
// the type of the segment followed by 0-6 control point coordinates.
int base = 64;
for (int i=0; i<shapeArray.length; i++) {
putFloat(base, shapeArray[i]);
base += 4;
}
int hdr2Offset = HEADER_SIZE+shapeArray.length*4;
//ij.IJ.log("saveShapeRoi: "+HEADER_SIZE+" "+shapeArray.length);
putHeader2(roi, hdr2Offset);
bout.write(data,0,data.length);
bout.flush();
}
void saveOverlayOptions(Roi roi, int options) {
Overlay proto = roi.getPrototypeOverlay();
if (proto.getDrawLabels())
options |= RoiDecoder.OVERLAY_LABELS;
if (proto.getDrawNames())
options |= RoiDecoder.OVERLAY_NAMES;
if (proto.getDrawBackgrounds())
options |= RoiDecoder.OVERLAY_BACKGROUNDS;
Font font = proto.getLabelFont();
if (font!=null && font.getStyle()==Font.BOLD)
options |= RoiDecoder.OVERLAY_BOLD;
putShort(RoiDecoder.OPTIONS, options);
}
void saveTextRoi(TextRoi roi) {
Font font = roi.getCurrentFont();
String fontName = font.getName();
int size = font.getSize();
int drawStringMode = roi.getDrawStringMode()?1024:0;
int style = font.getStyle() + roi.getJustification()*256+drawStringMode;
String text = roi.getText();
float angle = (float)roi.getAngle();
int angleLength = 4;
int fontNameLength = fontName.length();
int textLength = text.length();
int textRoiDataLength = 16+fontNameLength*2+textLength*2 + angleLength;
byte[] data2 = new byte[HEADER_SIZE+HEADER2_SIZE+textRoiDataLength+roiNameSize+roiPropsSize];
System.arraycopy(data, 0, data2, 0, HEADER_SIZE);
data = data2;
putShort(RoiDecoder.SUBTYPE, RoiDecoder.TEXT);
putInt(HEADER_SIZE, size);
putInt(HEADER_SIZE+4, style);
putInt(HEADER_SIZE+8, fontNameLength);
putInt(HEADER_SIZE+12, textLength);
for (int i=0; i<fontNameLength; i++)
putShort(HEADER_SIZE+16+i*2, fontName.charAt(i));
for (int i=0; i<textLength; i++)
putShort(HEADER_SIZE+16+fontNameLength*2+i*2, text.charAt(i));
int hdr2Offset = HEADER_SIZE+textRoiDataLength;
//ij.IJ.log("saveTextRoi: "+HEADER_SIZE+" "+textRoiDataLength+" "+fontNameLength+" "+textLength);
putFloat(hdr2Offset-angleLength, angle);
putHeader2(roi, hdr2Offset);
}
private int saveImageRoi(ImageRoi roi, int options) {
byte[] bytes = roi.getSerializedImage();
int imageSize = bytes.length;
byte[] data2 = new byte[HEADER_SIZE+HEADER2_SIZE+imageSize+roiNameSize+roiPropsSize];
System.arraycopy(data, 0, data2, 0, HEADER_SIZE);
data = data2;
putShort(RoiDecoder.SUBTYPE, RoiDecoder.IMAGE);
for (int i=0; i<imageSize; i++)
putByte(HEADER_SIZE+i, bytes[i]&255);
int hdr2Offset = HEADER_SIZE+imageSize;
double opacity = roi.getOpacity();
putByte(hdr2Offset+RoiDecoder.IMAGE_OPACITY, (int)(opacity*255.0));
putInt(hdr2Offset+RoiDecoder.IMAGE_SIZE, imageSize);
if (roi.getZeroTransparent())
options |= RoiDecoder.ZERO_TRANSPARENT;
putHeader2(roi, hdr2Offset);
return options;
}
void putHeader2(Roi roi, int hdr2Offset) {
//ij.IJ.log("putHeader2: "+hdr2Offset+" "+roiNameSize+" "+roiName);
putInt(RoiDecoder.HEADER2_OFFSET, hdr2Offset);
putInt(hdr2Offset+RoiDecoder.C_POSITION, roi.getCPosition());
putInt(hdr2Offset+RoiDecoder.Z_POSITION, roi.hasHyperStackPosition()?roi.getZPosition():0);
putInt(hdr2Offset+RoiDecoder.T_POSITION, roi.getTPosition());
Overlay proto = roi.getPrototypeOverlay();
Color overlayLabelColor = proto.getLabelColor();
if (overlayLabelColor!=null)
putInt(hdr2Offset+RoiDecoder.OVERLAY_LABEL_COLOR, overlayLabelColor.getRGB());
Font font = proto.getLabelFont();
if (font!=null)
putShort(hdr2Offset+RoiDecoder.OVERLAY_FONT_SIZE, font.getSize());
if (roiNameSize>0)
putName(roi, hdr2Offset);
double strokeWidth = roi.getStrokeWidth();
if (roi.getStroke()==null)
strokeWidth = 0.0;
putFloat(hdr2Offset+RoiDecoder.FLOAT_STROKE_WIDTH, (float)strokeWidth);
if (roiPropsSize>0)
putProps(roi, hdr2Offset);
if (countersSize>0)
putPointCounters(roi, hdr2Offset);
}
void putName(Roi roi, int hdr2Offset) {
int offset = hdr2Offset+HEADER2_SIZE;
int nameLength = roiNameSize/2;
putInt(hdr2Offset+RoiDecoder.NAME_OFFSET, offset);
putInt(hdr2Offset+RoiDecoder.NAME_LENGTH, nameLength);
for (int i=0; i<nameLength; i++)
putShort(offset+i*2, roiName.charAt(i));
}
void putProps(Roi roi, int hdr2Offset) {
int offset = hdr2Offset+HEADER2_SIZE+roiNameSize;
int roiPropsLength = roiPropsSize/2;
putInt(hdr2Offset+RoiDecoder.ROI_PROPS_OFFSET, offset);
putInt(hdr2Offset+RoiDecoder.ROI_PROPS_LENGTH, roiPropsLength);
for (int i=0; i<roiPropsLength; i++)
putShort(offset+i*2, roiProps.charAt(i));
}
void putPointCounters(Roi roi, int hdr2Offset) {
int offset = hdr2Offset+HEADER2_SIZE+roiNameSize+roiPropsSize;
putInt(hdr2Offset+RoiDecoder.COUNTERS_OFFSET, offset);
for (int i=0; i<countersSize/4; i++)
putInt(offset+i*4, counters[i]);
}
void putByte(int base, int v) {
data[base] = (byte)v;
}
void putShort(int base, int v) {
data[base] = (byte)(v>>>8);
data[base+1] = (byte)v;
}
void putFloat(int base, float v) {
int tmp = Float.floatToIntBits(v);
data[base] = (byte)(tmp>>24);
data[base+1] = (byte)(tmp>>16);
data[base+2] = (byte)(tmp>>8);
data[base+3] = (byte)tmp;
}
void putInt(int base, int i) {
data[base] = (byte)(i>>24);
data[base+1] = (byte)(i>>16);
data[base+2] = (byte)(i>>8);
data[base+3] = (byte)i;
}
}