BioLib  4.1.1
A GUI-less version of Bio .NET library for editing & annotating various microscopy image formats.
Loading...
Searching...
No Matches
BioLib.Fiji.RoiDecoder Class Reference

Public Member Functions

 RoiDecoder (string path)
 
 RoiDecoder (byte[] bytes, string name)
 
ROI getRoi (double physX, double physY, double volX, double volY)
 

Static Public Member Functions

static ROI open (string path, double physX, double physY, double volX, double volY)
 
static ROI openFromByteArray (byte[] bytes, double physX, double physY, double volX, double volY)
 

Static Public Attributes

static int VERSION_OFFSET = 4
 
static int TYPE = 6
 
static int TOP = 8
 
static int LEFT = 10
 
static int BOTTOM = 12
 
static int RIGHT = 14
 
static int N_COORDINATES = 16
 
static int X1 = 18
 
static int Y1 = 22
 
static int X2 = 26
 
static int Y2 = 30
 
static int XD = 18
 
static int YD = 22
 
static int WIDTHD = 26
 
static int HEIGHTD = 30
 
static int SIZE = 18
 
static int STROKE_WIDTH = 34
 
static int SHAPE_ROI_SIZE = 36
 
static int STROKE_COLOR = 40
 
static int FILL_COLOR = 44
 
static int SUBTYPE = 48
 
static int OPTIONS = 50
 
static int ARROW_STYLE = 52
 
static int FLOAT_PARAM = 52
 
static int POINT_TYPE = 52
 
static int ARROW_HEAD_SIZE = 53
 
static int ROUNDED_RECT_ARC_SIZE = 54
 
static int POSITION = 56
 
static int HEADER2_OFFSET = 60
 
static int COORDINATES = 64
 
static int C_POSITION = 4
 
static int Z_POSITION = 8
 
static int T_POSITION = 12
 
static int NAME_OFFSET = 16
 
static int NAME_LENGTH = 20
 
static int OVERLAY_LABEL_COLOR = 24
 
static int OVERLAY_FONT_SIZE = 28
 
static int GROUP = 30
 
static int IMAGE_OPACITY = 31
 
static int IMAGE_SIZE = 32
 
static int FLOAT_STROKE_WIDTH = 36
 
static int ROI_PROPS_OFFSET = 40
 
static int ROI_PROPS_LENGTH = 44
 
static int COUNTERS_OFFSET = 48
 
static int TEXT = 1
 
static int ARROW = 2
 
static int ELLIPSE = 3
 
static int IMAGE = 4
 
static int ROTATED_RECT = 5
 
static int SPLINE_FIT = 1
 
static int DOUBLE_HEADED = 2
 
static int OUTLINE = 4
 
static int OVERLAY_LABELS = 8
 
static int OVERLAY_NAMES = 16
 
static int OVERLAY_BACKGROUNDS = 32
 
static int OVERLAY_BOLD = 64
 
static int SUB_PIXEL_RESOLUTION = 128
 
static int DRAW_OFFSET = 256
 
static int ZERO_TRANSPARENT = 512
 
static int SHOW_LABELS = 1024
 
static int SCALE_LABELS = 2048
 
static int PROMPT_BEFORE_DELETING = 4096
 
static int SCALE_STROKE_WIDTH = 8192
 

Constructor & Destructor Documentation

◆ RoiDecoder() [1/2]

BioLib.Fiji.RoiDecoder.RoiDecoder ( string path)

Constructs an RoiDecoder using a file path.

1105 {
1106 this.path = path;
1107 }

◆ RoiDecoder() [2/2]

BioLib.Fiji.RoiDecoder.RoiDecoder ( byte[] bytes,
string name )

Constructs an RoiDecoder using a byte array.

1111 {
1112 ins = new MemoryStream(bytes);
1113 this.name = name;
1114 this.size = bytes.Length;
1115 }

Member Function Documentation

◆ getRoi()

ROI BioLib.Fiji.RoiDecoder.getRoi ( double physX,
double physY,
double volX,
double volY )

Returns the ROI.

1128 {
1129 ROI roi = new ROI();
1130 data = File.ReadAllBytes(path);
1131 size = data.Length;
1132 if (getByte(0) != 73 || getByte(1) != 111) //"Iout"
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 if (isComposite)
1191 {
1192 roi = getShapeRoi();
1193 if (version >= 218)
1194 getStrokeWidthAndColor(roi, hdr2Offset, scaleStrokeWidth);
1195 roi.coord.Z = position;
1196 if (channel > 0 || slice > 0 || frame > 0)
1197 {
1198 roi.coord.C = channel; roi.coord.Z = slice; roi.coord.T = frame;
1199 }
1200 decodeOverlayOptions(roi, version, options, overlayLabelColor, overlayFontSize);
1201 if (version >= 224)
1202 {
1203 string props = getRoiProps();
1204 if (props != null)
1205 roi.properties = props;
1206 }
1207 if (version >= 228 && group > 0)
1208 roi.serie = group;
1209 return roi;
1210 }
1211 */
1212 switch (type)
1213 {
1214 case 1: //Rect
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: //Ellipse
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: //Line
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 roi = new Arrow(x1, y1, x2, y2);
1240 ((Arrow)roi).setDoubleHeaded((options & DOUBLE_HEADED) != 0);
1241 ((Arrow)roi).setOutline((options & OUTLINE) != 0);
1242 int style = getByte(ARROW_STYLE);
1243 if (style >= Arrow.FILLED && style <= Arrow.BAR)
1244 ((Arrow)roi).setStyle(style);
1245 int headSize = getByte(ARROW_HEAD_SIZE);
1246 if (headSize >= 0 && style <= 30)
1247 ((Arrow)roi).setHeadSize(headSize);
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 //roi.setDrawOffset(drawOffset);
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 //IJ.log("type: "+type);
1265 //IJ.log("n: "+n);
1266 //ij.IJ.log("rect: "+left+","+top+" "+width+" "+height);
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 //TODO implement non subpizel ROI
1299 if (subPixelResolution)
1300 {
1301 roi.AddPoints(xf, yf);
1302 }
1303 else
1304 roi.AddPoints(x, y);
1305 if (version >= 226)
1306 {
1307 //((PointRoi)roi).setPointType(getByte(POINT_TYPE));
1308 roi.strokeWidth = getShort(STROKE_WIDTH);
1309 }
1310 //if ((options & SHOW_LABELS) != 0 && !ij.Prefs.noPointLabels)
1311 // ((PointRoi)roi).setShowLabels(true);
1312 //if ((options & PROMPT_BEFORE_DELETING) != 0)
1313 // ((PointRoi)roi).promptBeforeDeleting(true);
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 double ex1 = getFloat(X1);
1327 double ey1 = getFloat(Y1);
1328 double ex2 = getFloat(X2);
1329 double ey2 = getFloat(Y2);
1330 double param = getFloat(FLOAT_PARAM);
1331 if (subtype == ROTATED_RECT)
1332 roi = new RotatedRectRoi(ex1, ey1, ex2, ey2, param);
1333 else
1334 roi = new EllipseRoi(ex1, ey1, ex2, ey2, param);
1335 break;
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 {
1351 roi.AddPoints(xf, yf);
1352 //roi = new PolygonRoi(xf, yf, n, roiType);
1353 //roi.setDrawOffset(drawOffset);
1354 }
1355 else
1356 roi.AddPoints(x, y);
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 // read stroke width, stroke color and fill color (1.43i or later)
1366 if (version >= 218)
1367 {
1368 getStrokeWidthAndColor(roi, hdr2Offset, scaleStrokeWidth);
1369 /*
1370 if (type == point)
1371 roi.setStrokeWidth(0);
1372 bool splineFit = (options & SPLINE_FIT) != 0;
1373 if (splineFit && roi instanceof PolygonRoi)
1374 ((PolygonRoi)roi).fitSpline();
1375 */
1376 }
1377
1378 if (version >= 218 && subtype == TEXT)
1379 {
1380 getTextRoi(roi, version);
1381 roi.type = ROI.Type.Label;
1382 }
1383 /*
1384 if (version >= 221 && subtype == IMAGE)
1385 roi = getImageRoi(roi, imageOpacity, imageSize, options);
1386
1387 if (version >= 224)
1388 {
1389 string props = getRoiProps();
1390 if (props != null)
1391 roi.setProperties(props);
1392 }
1393
1394 if (version >= 227)
1395 {
1396 int[] counters = getPointCounters(n);
1397 if (counters != null && (roi instanceof PointRoi))
1398 ((PointRoi)roi).setCounters(counters);
1399 }
1400 */
1401 // set group (1.52t or later)
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); //-1 because our ROI coordinates are 0 based
1408 //decodeOverlayOptions(roi, version, options, overlayLabelColor, overlayFontSize);
1409
1410 //We convert pixel to subpixel
1411 if (!roi.subPixel)
1412 {
1413 for (int i = 0; i < roi.Points.Count; i++)
1414 {
1415 AForge.PointD pd = BioImage.ToStageSpace(roi.Points[i], physX,physY,volX,volY);
1416 roi.Points[i] = pd;
1417 roi.UpdateBoundingBox();
1418 }
1419 }
1420 if (roi.type == ROI.Type.Polygon || roi.type == ROI.Type.Freeform)
1421 roi.closed = true;
1422 return roi;
1423 }
Definition Bio.cs:1872
PointD ToStageSpace(PointD p)
Definition Bio.cs:3686
Definition ROI.cs:27
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

◆ open()

static ROI BioLib.Fiji.RoiDecoder.open ( string path,
double physX,
double physY,
double volX,
double volY )
static

Opens the ROI at the specified path. Returns null if there is an error.

1119 {
1120 ROI roi = null;
1121 RoiDecoder rd = new RoiDecoder(path);
1122 roi = rd.getRoi(physX,physY,volX,volY);
1123 return roi;
1124 }
RoiDecoder(string path)
Definition Fiji.cs:1104

◆ openFromByteArray()

static ROI BioLib.Fiji.RoiDecoder.openFromByteArray ( byte[] bytes,
double physX,
double physY,
double volX,
double volY )
static

Opens an ROI from a byte array.

1632 {
1633 ROI roi = null;
1634 if (bytes == null || bytes.Length == 0)
1635 return roi;
1636 try
1637 {
1638 RoiDecoder decoder = new RoiDecoder(bytes, null);
1639 roi = decoder.getRoi(physX,physY,volX,volY);
1640 }
1641 catch (IOException e)
1642 {
1643 return null;
1644 }
1645 return roi;
1646 }

The documentation for this class was generated from the following file: