/** * Create an {@link ArrayImgFactory} if an image of the requested <code>targetSize</code> could be * held in an {@link ArrayImg}. Otherwise return a {@link CellImgFactory} with as large as * possible cell size. * * @param targetSize size of image that the factory should be able to create. * @param type type of the factory. * @return an {@link ArrayImgFactory} or a {@link CellImgFactory}. */ public static <T extends NativeType<T>> ImgFactory<T> getArrayOrCellImgFactory( final Dimensions targetSize, final T type) { if (Intervals.numElements(targetSize) <= Integer.MAX_VALUE) return new ArrayImgFactory<T>(); final int cellSize = (int) Math.pow( Integer.MAX_VALUE / type.getEntitiesPerPixel().getRatio(), 1.0 / targetSize.numDimensions()); return new CellImgFactory<T>(cellSize); }