Big images cause an array index out of bounds exception

@Lawrence_Anovitz reported on the mailing list that BoneJ’s Fractal Dimension plugin causes an AIOOB when they run it on large data (1360×1360×2293 pixels). The AIOOB traces back to image conversion code so it seems like something is awry in the way the image is being converted or how it is handled after it is converted. Large-ish images of 1024×1024×2048 work OK, but larger images of 1024×1024×4096 cause the AIOOB.

I have started an issue on BoneJ’s issue tracker.

@ctrueden requested I begin this thread here; @stelfrich was helping on Gitter.

the offending code in Common.java:

	/**
	 * Converts the {@link ImgPlus} to a new ImgPlus with {@link BitType}
	 * elements.
	 * <p>
	 * Also copies ImgPlus metadata.
	 * </p>
	 *
	 * @param ops an {@link OpService} to find the necessary ops for conversion.
	 * @param imgPlus an image.
	 * @param <C> type of the elements in the input image.
	 * @return the image converted to bit type.
	 */
	public static <C extends ComplexType<C>> ImgPlus<BitType> toBitTypeImgPlus(
		final OpEnvironment ops, final ImgPlus<C> imgPlus)
	{
		final Img<BitType> convertedImg = ops.convert().bit(imgPlus.getImg());
		final ImgPlus<BitType> convertedImgPlus = new ImgPlus<>(convertedImg);
		copyMetadata(imgPlus, convertedImgPlus);

		return convertedImgPlus;
	}
1 Like

Gitter discussion starting here.


I first thought that the conversion op always creates an ArrayImg when used as a Function. The same exception, however, occurs when @mdoube created a PlanarImg<BitType> manually and provided it to the convert op…

Best,
Stefan

Hi @mdoube

I made a simple bit converter command to see if I could replicate the issue. I was able to repeat the issue, and I think it only occurs if the image has more than 2^31 elements.

I think this is because DefaultChunker (the class where the error occurs) uses integers for the indexing. This is because the Chunk interface uses integers.

I tried changing Chunk so that is uses longs, and that seems to fix the issue. See this pull request.

5 Likes

Cool. Will check it out when I’m back in the office. Thanks for your work on it!