1128 {
1130 data = File.ReadAllBytes(path);
1131 size = data.Length;
1132 if (getByte(0) != 73 || getByte(1) != 111)
1133 throw new IOException("This is not an ImageJ ROI");
1134 int version = getShort(VERSION_OFFSET);
1135 int type = getByte(TYPE);
1136 int subtype = getShort(SUBTYPE);
1137 int top = getShort(TOP);
1138 int left = getShort(LEFT);
1139 int bottom = getShort(BOTTOM);
1140 int right = getShort(RIGHT);
1141 int width = right - left;
1142 int height = bottom - top;
1143 int n = getUnsignedShort(N_COORDINATES);
1144 if (n == 0)
1145 n = getInt(SIZE);
1146 int options = getShort(OPTIONS);
1147 int position = getInt(POSITION);
1148 int hdr2Offset = getInt(HEADER2_OFFSET);
1149 int channel = 0, slice = 0, frame = 0;
1150 int overlayLabelColor = 0;
1151 int overlayFontSize = 0;
1152 int group = 0;
1153 int imageOpacity = 0;
1154 int imageSize = 0;
1155 bool subPixelResolution = (options & SUB_PIXEL_RESOLUTION) != 0 && version >= 222;
1156 bool drawOffset = subPixelResolution && (options & DRAW_OFFSET) != 0;
1157 bool scaleStrokeWidth = true;
1158 if (version >= 228)
1159 scaleStrokeWidth = (options & SCALE_STROKE_WIDTH) != 0;
1160
1161 bool subPixelRect = version >= 223 && subPixelResolution && (type == rect || type == oval);
1162 double xd = 0.0, yd = 0.0, widthd = 0.0, heightd = 0.0;
1163 if (subPixelRect)
1164 {
1165 xd = getFloat(XD);
1166 yd = getFloat(YD);
1167 widthd = getFloat(WIDTHD);
1168 heightd = getFloat(HEIGHTD);
1169 roi.subPixel = true;
1170 }
1171
1172 if (hdr2Offset > 0 && hdr2Offset + IMAGE_SIZE + 4 <= size)
1173 {
1174 channel = getInt(hdr2Offset + C_POSITION);
1175 slice = getInt(hdr2Offset + Z_POSITION);
1176 frame = getInt(hdr2Offset + T_POSITION);
1177 overlayLabelColor = getInt(hdr2Offset + OVERLAY_LABEL_COLOR);
1178 overlayFontSize = getShort(hdr2Offset + OVERLAY_FONT_SIZE);
1179 imageOpacity = getByte(hdr2Offset + IMAGE_OPACITY);
1180 imageSize = getInt(hdr2Offset + IMAGE_SIZE);
1181 group = getByte(hdr2Offset + GROUP);
1182 }
1183
1184 if (name != null && name.EndsWith(".roi"))
1185 name = name.Substring(0, name.Length - 4);
1186 bool isComposite = getInt(SHAPE_ROI_SIZE) > 0;
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 switch (type)
1213 {
1214 case 1:
1215 if (subPixelRect)
1216 roi =
ROI.
CreateRectangle(
new AForge.ZCT(slice - 1, channel - 1, frame - 1), xd, yd, widthd, heightd);
1217 else
1218 roi =
ROI.
CreateRectangle(
new AForge.ZCT(slice - 1, channel - 1, frame - 1), left, top, width, height);
1219 int arcSize = getShort(ROUNDED_RECT_ARC_SIZE);
1220 if (arcSize > 0)
1221 throw new NotSupportedException("Type rounded rectangle not supported.");
1222 break;
1223 case 2:
1224 if (subPixelRect)
1225 roi =
ROI.
CreateEllipse(
new AForge.ZCT(slice - 1, channel - 1, frame - 1), xd, yd, widthd, heightd);
1226 else
1227 roi =
ROI.
CreateEllipse(
new AForge.ZCT(slice - 1, channel - 1, frame - 1), left, top, width, height);
1228 break;
1229 case 3:
1230 float x1 = getFloat(X1);
1231 float y1 = getFloat(Y1);
1232 float x2 = getFloat(X2);
1233 float y2 = getFloat(Y2);
1234
1235 if (subtype == ARROW)
1236 {
1237 throw new NotSupportedException("Type arrow not supported.");
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249 }
1250 else
1251 {
1252 roi =
ROI.
CreateLine(
new AForge.ZCT(slice, channel, frame),
new AForge.PointD(x1, y1),
new AForge.PointD(x2, y2));
1253
1254 }
1255
1256 break;
1257 case 0:
1258 case 5:
1259 case 6:
1260 case 7:
1261 case 8:
1262 case 9:
1263 case 10:
1264
1265
1266
1267 if (n == 0 || n < 0) break;
1268 int[] x = new int[n];
1269 int[] y = new int[n];
1270 float[] xf = null;
1271 float[] yf = null;
1272 int base1 = COORDINATES;
1273 int base2 = base1 + 2 * n;
1274 int xtmp, ytmp;
1275 for (int i = 0; i < n; i++)
1276 {
1277 xtmp = getShort(base1 + i * 2);
1278 if (xtmp < 0) xtmp = 0;
1279 ytmp = getShort(base2 + i * 2);
1280 if (ytmp < 0) ytmp = 0;
1281 x[i] = left + xtmp;
1282 y[i] = top + ytmp;
1283 }
1284 if (subPixelResolution)
1285 {
1286 xf = new float[n];
1287 yf = new float[n];
1288 base1 = COORDINATES + 4 * n;
1289 base2 = base1 + 4 * n;
1290 for (int i = 0; i < n; i++)
1291 {
1292 xf[i] = getFloat(base1 + i * 4);
1293 yf[i] = getFloat(base2 + i * 4);
1294 }
1295 }
1296 if (type == point)
1297 {
1298
1299 if (subPixelResolution)
1300 {
1302 }
1303 else
1305 if (version >= 226)
1306 {
1307
1308 roi.strokeWidth = getShort(STROKE_WIDTH);
1309 }
1310
1311
1312
1313
1314 roi.type =
ROI.Type.Point;
1315 break;
1316 }
1317 if (type == polygon)
1318 roi.type =
ROI.Type.Polygon;
1319 else if (type == freehand)
1320 {
1321 roi.type =
ROI.Type.Freeform;
1322 if (subtype == ELLIPSE || subtype == ROTATED_RECT)
1323 {
1324 throw new NotSupportedException("ROI type not supported.");
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337 }
1338 }
1339 else if (type == traced)
1340 roi.type =
ROI.Type.Polyline;
1341 else if (type == polyline)
1342 roi.type =
ROI.Type.Polyline;
1343 else if (type == freeline)
1344 roi.type =
ROI.Type.Polyline;
1345 else if (type == angle)
1346 roi.type =
ROI.Type.Point;
1347 else
1348 roi.type =
ROI.Type.Freeform;
1349 if (subPixelResolution)
1350 {
1352
1353
1354 }
1355 else
1357 break;
1358 default:
1359 throw new IOException("Unrecognized ROI type: " + type);
1360 }
1361 if (roi == null)
1362 return null;
1363 roi.roiName = getRoiName();
1364
1365
1366 if (version >= 218)
1367 {
1368 getStrokeWidthAndColor(roi, hdr2Offset, scaleStrokeWidth);
1369
1370
1371
1372
1373
1374
1375
1376 }
1377
1378 if (version >= 218 && subtype == TEXT)
1379 {
1380 getTextRoi(roi, version);
1381 roi.type =
ROI.Type.Label;
1382 }
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402 if (version >= 228 && group > 0)
1403 roi.serie = group;
1404
1405 roi.coord.Z = position;
1406 if (channel > 0 || slice > 0 || frame > 0)
1407 roi.coord = new AForge.ZCT(slice - 1, channel - 1, frame - 1);
1408
1409
1410
1411 if (!roi.subPixel)
1412 {
1413 for (int i = 0; i < roi.Points.Count; i++)
1414 {
1416 roi.Points[i] = pd;
1418 }
1419 }
1420 if (roi.type ==
ROI.Type.Polygon || roi.type ==
ROI.Type.Freeform)
1421 roi.closed = true;
1422 return roi;
1423 }
PointD ToStageSpace(PointD p)
Definition Bio.cs:3686
static ROI CreateRectangle(ZCT coord, double x, double y, double w, double h)
Definition ROI.cs:1120
void UpdateBoundingBox()
Definition ROI.cs:1764
static ROI CreateEllipse(ZCT coord, double x, double y, double w, double h)
Definition ROI.cs:1137
void AddPoints(PointD[] p, bool convertPixelSpace=false, double physX=1, double physY=1)
Definition ROI.cs:1653
static ROI CreateLine(ZCT coord, PointD x1, PointD x2)
Definition ROI.cs:1100