Returns the ROI.
393 {
394 ROI roi = new ROI();
395 data = File.ReadAllBytes(path);
396 size = data.Length;
397 if (getByte(0) != 73 || getByte(1) != 111)
398 throw new IOException("This is not an ImageJ ROI");
399 int version = getShort(VERSION_OFFSET);
400 int type = getByte(TYPE);
401 int subtype = getShort(SUBTYPE);
402 int top = getShort(TOP);
403 int left = getShort(LEFT);
404 int bottom = getShort(BOTTOM);
405 int right = getShort(RIGHT);
406 int width = right - left;
407 int height = bottom - top;
408 int n = getUnsignedShort(N_COORDINATES);
409 if (n == 0)
410 n = getInt(SIZE);
411 int options = getShort(OPTIONS);
412 int position = getInt(POSITION);
413 int hdr2Offset = getInt(HEADER2_OFFSET);
414 int channel = 0, slice = 0, frame = 0;
415 int overlayLabelColor = 0;
416 int overlayFontSize = 0;
417 int group = 0;
418 int imageOpacity = 0;
419 int imageSize = 0;
420 bool subPixelResolution = (options & SUB_PIXEL_RESOLUTION) != 0 && version >= 222;
421 bool drawOffset = subPixelResolution && (options & DRAW_OFFSET) != 0;
422 bool scaleStrokeWidth = true;
423 if (version >= 228)
424 scaleStrokeWidth = (options & SCALE_STROKE_WIDTH) != 0;
425
426 bool subPixelRect = version >= 223 && subPixelResolution && (type == rect || type == oval);
427 double xd = 0.0, yd = 0.0, widthd = 0.0, heightd = 0.0;
428 if (subPixelRect)
429 {
430 xd = getFloat(XD);
431 yd = getFloat(YD);
432 widthd = getFloat(WIDTHD);
433 heightd = getFloat(HEIGHTD);
434 }
435
436 if (hdr2Offset > 0 && hdr2Offset + IMAGE_SIZE + 4 <= size)
437 {
438 channel = getInt(hdr2Offset + C_POSITION);
439 slice = getInt(hdr2Offset + Z_POSITION);
440 frame = getInt(hdr2Offset + T_POSITION);
441 overlayLabelColor = getInt(hdr2Offset + OVERLAY_LABEL_COLOR);
442 overlayFontSize = getShort(hdr2Offset + OVERLAY_FONT_SIZE);
443 imageOpacity = getByte(hdr2Offset + IMAGE_OPACITY);
444 imageSize = getInt(hdr2Offset + IMAGE_SIZE);
445 group = getByte(hdr2Offset + GROUP);
446 }
447
448 if (name != null && name.EndsWith(".roi"))
449 name = name.Substring(0, name.Length - 4);
450 bool isComposite = getInt(SHAPE_ROI_SIZE) > 0;
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476 switch (type)
477 {
478 case 1:
479 if (subPixelRect)
480 roi = ROI.CreateRectangle(new ZCT(slice - 1, channel - 1, frame - 1), xd, yd, widthd, heightd);
481 else
482 roi = ROI.CreateRectangle(new ZCT(slice - 1, channel - 1, frame - 1), left, top, width, height);
483 int arcSize = getShort(ROUNDED_RECT_ARC_SIZE);
484 if (arcSize > 0)
485 throw new NotSupportedException("Type rounded rectangle not supported.");
486 break;
487 case 2:
488 if (subPixelRect)
489 roi = ROI.CreateEllipse(new ZCT(slice - 1, channel - 1, frame - 1), xd, yd, widthd, heightd);
490 else
491 roi = ROI.CreateEllipse(new ZCT(slice - 1, channel - 1, frame - 1), left, top, width, height);
492 break;
493 case 3:
494 float x1 = getFloat(X1);
495 float y1 = getFloat(Y1);
496 float x2 = getFloat(X2);
497 float y2 = getFloat(Y2);
498
499 if (subtype == ARROW)
500 {
501 throw new NotSupportedException("Type arrow not supported.");
502
503
504
505
506
507
508
509
510
511
512
513 }
514 else
515 {
516 roi = ROI.CreateLine(new ZCT(slice, channel, frame), new PointD(x1, y1), new PointD(x2, y2));
517
518 }
519
520 break;
521 case 0:
522 case 5:
523 case 6:
524 case 7:
525 case 8:
526 case 9:
527 case 10:
528
529
530
531 if (n == 0 || n < 0) break;
532 int[] x = new int[n];
533 int[] y = new int[n];
534 float[] xf = null;
535 float[] yf = null;
536 int base1 = COORDINATES;
537 int base2 = base1 + 2 * n;
538 int xtmp, ytmp;
539 for (int i = 0; i < n; i++)
540 {
541 xtmp = getShort(base1 + i * 2);
542 if (xtmp < 0) xtmp = 0;
543 ytmp = getShort(base2 + i * 2);
544 if (ytmp < 0) ytmp = 0;
545 x[i] = left + xtmp;
546 y[i] = top + ytmp;
547 }
548 if (subPixelResolution)
549 {
550 xf = new float[n];
551 yf = new float[n];
552 base1 = COORDINATES + 4 * n;
553 base2 = base1 + 4 * n;
554 for (int i = 0; i < n; i++)
555 {
556 xf[i] = getFloat(base1 + i * 4);
557 yf[i] = getFloat(base2 + i * 4);
558 }
559 }
560 if (type == point)
561 {
562
563 if (subPixelResolution)
564 {
565 roi.AddPoints(xf, yf);
566 }
567 else
568 roi.AddPoints(x, y);
569 if (version >= 226)
570 {
571
572 roi.strokeWidth = getShort(STROKE_WIDTH);
573 }
574
575
576
577
578 roi.type = ROI.Type.Point;
579 break;
580 }
581 if (type == polygon)
582 roi.type = ROI.Type.Polygon;
583 else if (type == freehand)
584 {
585 roi.type = ROI.Type.Freeform;
586 if (subtype == ELLIPSE || subtype == ROTATED_RECT)
587 {
588 throw new NotSupportedException("ROI type not supported.");
589
590
591
592
593
594
595
596
597
598
599
600
601 }
602 }
603 else if (type == traced)
604 roi.type = ROI.Type.Polyline;
605 else if (type == polyline)
606 roi.type = ROI.Type.Polyline;
607 else if (type == freeline)
608 roi.type = ROI.Type.Polyline;
609 else if (type == angle)
610 roi.type = ROI.Type.Point;
611 else
612 roi.type = ROI.Type.Freeform;
613 if (subPixelResolution)
614 {
615 roi.AddPoints(xf, yf);
616
617
618 }
619 else
620 roi.AddPoints(x, y);
621 break;
622 default:
623 throw new IOException("Unrecognized ROI type: " + type);
624 }
625 if (roi == null)
626 return null;
627 roi.roiName = getRoiName();
628
629
630 if (version >= 218)
631 {
632 getStrokeWidthAndColor(roi, hdr2Offset, scaleStrokeWidth);
633
634
635
636
637
638
639
640 }
641
642 if (version >= 218 && subtype == TEXT)
643 {
644 getTextRoi(roi, version);
645 roi.type = ROI.Type.Label;
646 }
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666 if (version >= 228 && group > 0)
667 roi.serie = group;
668
669 roi.coord.Z = position;
670 if (channel > 0 || slice > 0 || frame > 0)
671 roi.coord = new ZCT(slice - 1, channel - 1, frame - 1);
672
673
674
675
676 for (int i = 0; i < roi.Points.Count; i++)
677 {
678 PointD pd = ImageView.SelectedImage.ToStageSpace(roi.Points[i]);
679 roi.Points[i] = pd;
680 roi.UpdateBoundingBox();
681 }
682
683 if (roi.type == ROI.Type.Polygon || roi.type == ROI.Type.Freeform)
684 roi.closed = true;
685 return roi;
686 }