Login  Register

Re: Bug in ImageStack: can't add new slice at given position

Posted by dscho on Jun 16, 2010; 4:48pm
URL: http://imagej.273.s1.nabble.com/Bug-in-ImageStack-can-t-add-new-slice-at-given-position-tp3687906p3687908.html

Hi,

On Wed, 16 Jun 2010, Jean-Yves Tinevez wrote:

> 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.

I _think_ it might be related to bug 183. Wayne said that in his test, it
was still a bug in the current ImageJA. It must be something about that
commit:

http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=ImageJA.git;a=commitdiff;h=9b44e71f55e721a52796cd2284316e96a26d967c

> 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.

Actually, my understanding is that inserting should be allowed with an
index pointing just after the last slice, in which case a slice would be
added.

Ciao,
Dscho