@@ -170,15 +170,20 @@ public void setCenterFromROI(final Roi roi) throws IllegalArgumentException {
170170 if (roi == null ) {
171171 throw new IllegalArgumentException ("Cannot retrieve center: ROI is null!" );
172172 }
173- if (roi .getType () == Roi .LINE ) {
174- final Line line = (Line ) roi ;
175- setCenterPx (line .x1 , line .y1 , imp .getZ ());
176- } else if (roi .getType () == Roi .POINT ) {
177- final Rectangle rect = roi .getBounds ();
178- setCenterPx (rect .x , rect .y , imp .getZ ());
179- } else {
180- final double [] ctd = roi .getContourCentroid ();
181- setCenterPx ((int ) Math .round (ctd [0 ]), (int ) Math .round (ctd [1 ]), imp .getZ ());
173+ try {
174+ if (roi .getType () == Roi .LINE ) {
175+ final Line line = (Line ) roi ;
176+ setCenterPx (line .x1 , line .y1 , imp .getZ ());
177+ } else if (roi .getType () == Roi .POINT ) {
178+ final Rectangle rect = roi .getBounds ();
179+ setCenterPx (rect .x , rect .y , imp .getZ ());
180+ } else {
181+ final double [] ctd = roi .getContourCentroid ();
182+ setCenterPx ((int ) Math .round (ctd [0 ]), (int ) Math .round (ctd [1 ]), imp .getZ ());
183+ }
184+ } catch (final IndexOutOfBoundsException ex ) {
185+ throw new IndexOutOfBoundsException (ex .getMessage ()
186+ + ". Does the ROI belong to this image? ROI bounds: " + roi .getBounds ());
182187 }
183188 }
184189
@@ -192,8 +197,10 @@ public void setCenterFromROI(final Roi roi) throws IllegalArgumentException {
192197 * image dimensions
193198 */
194199 public void setCenterPx (final int x , final int y , final int z ) throws IndexOutOfBoundsException {
195- if (x > imp .getWidth () - 1 || y > imp .getHeight () || z > imp .getNSlices ())
196- throw new IndexOutOfBoundsException ("specified coordinates cannot be applied to image" );
200+ if (x < 0 || y < 0 || z < 1 || x > imp .getWidth () - 1 || y > imp .getHeight () - 1 || z > imp .getNSlices ())
201+ throw new IndexOutOfBoundsException (
202+ String .format ("Specified coordinates [%d, %d, %d] outside image bounds [%d, %d, %d]" ,
203+ x , y , z , imp .getWidth (), imp .getHeight (), imp .getNSlices ()));
197204 center = new ShollPoint (x , y , z , cal );
198205 profile .setCenter (center );
199206 xc = x ;
@@ -253,9 +260,9 @@ public void setRadii(final double startRadius, final double step, final double e
253260 }
254261
255262 public double maxPossibleRadius () {
256- final double maxX = imp .getWidth () - 1 * cal .pixelWidth ;
257- final double maxY = imp .getHeight () - 1 * cal .pixelHeight ;
258- final double maxZ = imp .getNSlices () - 1 * cal .pixelDepth ;
263+ final double maxX = ( imp .getWidth () - 1 ) * cal .pixelWidth ;
264+ final double maxY = ( imp .getHeight () - 1 ) * cal .pixelHeight ;
265+ final double maxZ = ( imp .getNSlices () - 1 ) * cal .pixelDepth ;
259266 final ShollPoint [] points = new ShollPoint [8 ];
260267 points [0 ] = new ShollPoint (0 , 0 , 0 );
261268 points [1 ] = new ShollPoint (maxX , maxY , maxZ );
0 commit comments