31 private final int polygon=0, rect=1, oval=2, line=3, freeline=4, polyline=5, noRoi=6, freehand=7, traced=8, angle=9;
40 public Roi
getRoi() throws IOException {
41 File f =
new File(path);
42 int size = (int)f.length();
44 throw new IOException(
"This is not an ImageJ ROI");
45 FileInputStream fis =
new FileInputStream(path);
46 data =
new byte[size];
50 total += fis.read(data, total, size-total);
51 if (getByte(0)!=73 || getByte(1)!=111)
52 throw new IOException(
"This is not an ImageJ ROI");
53 int type = getByte(6);
55 int left = getShort(10);
56 int bottom = getShort(12);
57 int right = getShort(14);
58 int width = right-left;
59 int height = bottom-top;
62 boolean isComposite = getInt(36)>0;
69 roi =
new Roi(left, top, width, height);
72 roi =
new OvalRoi(left, top, width, height);
75 int x1 = (int)getFloat(18);
76 int y1 = (int)getFloat(22);
77 int x2 = (int)getFloat(26);
78 int y2 = (int)getFloat(30);
79 roi =
new Line(x1, y1, x2, y2);
82 case polygon:
case freehand:
case traced:
case polyline:
case freeline:
case angle:
90 int base2 = base1+2*n;
92 for (
int i=0; i<n; i++) {
93 xtmp = getShort(base1+i*2);
95 ytmp = getShort(base2+i*2);
103 roiType = Roi.POLYGON;
104 else if (type==freehand)
105 roiType = Roi.FREEROI;
106 else if (type==traced)
107 roiType = Roi.TRACED_ROI;
108 else if (type==polyline)
109 roiType = Roi.POLYLINE;
110 else if (type==freeline)
111 roiType = Roi.FREELINE;
112 else if (type==angle)
115 roiType = Roi.FREEROI;
116 roi =
new PolygonRoi(x, y, n, roiType);
119 throw new IOException(
"Unrecognized ROI type: "+type);
121 String name = f.getName();
122 if (name.endsWith(
".roi"))
123 name = name.substring(0, name.length()-4);
128 public Roi getShapeRoi() throws IOException {
129 int type = getByte(6);
131 throw new IllegalArgumentException(
"Invalid composite ROI type");
132 int top= getShort(8);
133 int left = getShort(10);
134 int bottom = getShort(12);
135 int right = getShort(14);
136 int width = right-left;
137 int height = bottom-top;
141 float[] shapeArray =
new float[n];
143 for(
int i=0; i<n; i++) {
144 shapeArray[i] = getFloat(base);
147 roi =
new ShapeRoi(shapeArray);
151 int getByte(
int base) {
152 return data[base]&255;
155 int getShort(
int base) {
156 int b0 = data[base]&255;
157 int b1 = data[base+1]&255;
158 return (
short)((b0<<8) + b1);
161 int getInt(
int base) {
162 int b0 = data[base]&255;
163 int b1 = data[base+1]&255;
164 int b2 = data[base+2]&255;
165 int b3 = data[base+3]&255;
166 return ((b0<<24) + (b1<<16) + (b2<<8) + b3);
169 float getFloat(
int base) {
170 return Float.intBitsToFloat(getInt(base));