MCPcopy Create free account
hub / github.com/AstroImageJ/astroimagej / roiToShape

Method roiToShape

ij/src/main/java/ij/gui/ShapeRoi.java:269–351  ·  view source on GitHub ↗

Converts the Roi argument to an instance of java.awt.Shape. Currently, the following conversions are supported: Roi class Roi type Shape < <

(Roi roi)

Source from the content-addressed store, hash-verified

267 *
268 */
269 private Shape roiToShape(Roi roi) {
270 if (roi.isLine())
271 roi = Roi.convertLineToArea(roi);
272 Shape shape = null;
273 Rectangle r = roi.getBounds();
274 boolean closeShape = true;
275 int roiType = roi.getType();
276 switch(roiType) {
277 case Roi.LINE:
278 Line line = (Line)roi;
279 shape = new Line2D.Double ((double)(line.x1-r.x), (double)(line.y1-r.y), (double)(line.x2-r.x), (double)(line.y2-r.y) );
280 break;
281 case Roi.RECTANGLE:
282 int arcSize = roi.getCornerDiameter();
283 if (arcSize>0)
284 shape = new RoundRectangle2D.Double(0, 0, r.width, r.height, arcSize, arcSize);
285 else
286 shape = new Rectangle2D.Double(0.0, 0.0, (double)r.width, (double)r.height);
287 break;
288 case Roi.POLYLINE: case Roi.FREELINE: case Roi.ANGLE:
289 closeShape = false;
290 case Roi.POLYGON: case Roi.FREEROI: case Roi.TRACED_ROI: case Roi.OVAL:
291 if (roiType == Roi.OVAL) {
292 //shape = new Ellipse2D.Double(-0.001, -0.001, r.width+0.002, r.height+0.002); //inaccurate (though better with increased diameter)
293 shape = ((OvalRoi)roi).getPolygon(false);
294 } else if (closeShape && !roi.subPixelResolution()) {
295 int nPoints =((PolygonRoi)roi).getNCoordinates();
296 int[] xCoords = ((PolygonRoi)roi).getXCoordinates();
297 int[] yCoords = ((PolygonRoi)roi).getYCoordinates();
298 shape = new Polygon(xCoords, yCoords, nPoints);
299 } else {
300 FloatPolygon floatPoly = roi.getFloatPolygon();
301 if (floatPoly.npoints <=1) break;
302 shape = new GeneralPath(closeShape ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO, floatPoly.npoints);
303 ((GeneralPath)shape).moveTo(floatPoly.xpoints[0] - r.x, floatPoly.ypoints[0] - r.y);
304 for (int i=1; i<floatPoly.npoints; i++)
305 ((GeneralPath)shape).lineTo(floatPoly.xpoints[i] - r.x, floatPoly.ypoints[i] - r.y);
306 if (closeShape)
307 ((GeneralPath)shape).closePath();
308 }
309 break;
310 case Roi.POINT:
311 ImageProcessor mask = roi.getMask();
312 byte[] maskPixels = (byte[])mask.getPixels();
313 int maskWidth = mask.getWidth();
314 Area area = new Area();
315 for (int y=0; y<mask.getHeight(); y++) {
316 int yOffset = y*maskWidth;
317 for (int x=0; x<maskWidth; x++) {
318 if (maskPixels[x+yOffset]!=0)
319 area.add(new Area(new Rectangle(x, y, 1, 1)));
320 }
321 }
322 shape = area;
323 break;
324 case Roi.COMPOSITE: shape = ShapeRoi.cloneShape(((ShapeRoi)roi).getShape());
325 break;
326 default:

Callers 1

ShapeRoiMethod · 0.95

Calls 15

convertLineToAreaMethod · 0.95
getPixelsMethod · 0.95
getWidthMethod · 0.95
getHeightMethod · 0.95
cloneShapeMethod · 0.95
getCornerDiameterMethod · 0.80
getNCoordinatesMethod · 0.80
getXCoordinatesMethod · 0.80
getYCoordinatesMethod · 0.80
getBoundsMethod · 0.65
getTypeMethod · 0.65
addMethod · 0.65

Tested by

no test coverage detected