Bug in ImageStack: can't add new slice at given position
Posted by Jean-Yves Tinevez-2 on Jun 16, 2010; 3:14pm
URL: http://imagej.273.s1.nabble.com/Bug-in-ImageStack-can-t-add-new-slice-at-given-position-tp3687906.html
Dear friends
I have found a bug in ImageStack that appeared somewhere between now
and 1 year ago.
The method:
public void addSlice(String sliceLabel, ImageProcessor ip, int n)
generates an Argument out of range exception when called.
Here is the way to reproduce the bug, by allocating a stack of 10
slices:
ImageStack stack = new ImageStack(2, 2, 10);
stack.addSlice("First slice" ,new FloatProcessor(new float[] {1, 2, 3,
4}), 1);
This snippet tries to add the slice at position 2 in the stack.
However, even if the stack was instantiated with 10 slices, an
exception is thrown.
I have found out that this is due to, in the constructor, the line 41,
which put the number of slice to 0 at start:
// The number of existing slices starts with 0, even if the
// allocated space may be larger
nSlices = 0;
and to an early check in the addSlice(...) method, at lines 96-97:
if (n<0 || n>nSlices)
throw new IllegalArgumentException(outOfRange+n);
which prevents from actually adding slices using this method in an
empty stack.
I understand that this method is meant to be used to insert a slice
between already added slices, but since this change, we have no
possibility to add slices at a given position in an empty stack.
We can't modify this method to actually set the slice, because it
might confuse the method getSize(), which returns the actual number of
slices added, regardless of the initial capacity. So if we set the
slice to erase an existing slice, the number of slice does not
increase. If we add a slice to an empty position, it does.
However, such a method is desirable. If we want to create a stack of
10 slices, where individual slices are added not in order, for
instance by a multi-threading algorithm.
So I am confused.
How did we do before?
Best
jy
--
Jean-Yves Tinevez
PFID - Imagopole
Institut Pasteur
25-28, rue du Docteur Roux
75724 Paris cedex 15
France
tel: +33 1 40 61 31 77