ElementUtil { public static <T extends RealType<T> & NativeType<T>> boolean isBinary( final IterableInterval<T> interval) { if (interval.size() == 0) { return false; } if (BooleanType.class.isAssignableFrom(interval.firstElement() .getClass())) { return true; } double a = interval.firstElement().getRealDouble(); double b = a; double c; final Cursor<T> cursor = interval.cursor(); while (cursor.hasNext()){ c = cursor.next().getRealDouble(); if (c != a) { b = c; break; } } while (cursor.hasNext()) { c = cursor.next().getRealDouble(); if (c == a || c == b) continue; return false; } return true; } private  ElementUtil(); static double calibratedSpatialElementSize(final T space, final UnitService unitService); static boolean isBinary(
		final IterableInterval<T> interval);  }
@Test public void testIsBinary() { final IterableInterval<DoubleType> interval = ArrayImgs.doubles(2, 2); final Iterator<Integer> intIterator = IntStream.iterate(0, i -> (i + 1) % 2) .iterator(); interval.cursor().forEachRemaining(e -> e.setReal(intIterator.next())); final boolean result = ElementUtil.isBinary(interval); assertTrue("An image with two values should be binary", result); }
@Test public void testIsBinaryBooleanTypeAssignable() { final Img<BitType> img = ArrayImgs.bits(1); final boolean isBinary = ElementUtil.isBinary(img); assertTrue("A BitType image should be binary", isBinary); }
@Test(expected = NullPointerException.class) public void testIsBinaryThrowsNPEIfIntervalNull() { final boolean result = ElementUtil.isBinary(null); assertFalse("A null interval should not be binary", result); }
@Test public void testIsBinaryReturnsFalseForMultiValue() { final IterableInterval<DoubleType> interval = ArrayImgs.doubles(2, 2); final Iterator<Integer> intIterator = IntStream.iterate(0, i -> i + 1) .iterator(); interval.cursor().forEachRemaining(e -> e.setReal(intIterator.next())); final boolean result = ElementUtil.isBinary(interval); assertFalse( "An image with more than two values should not be binary", result); }
@Test public void testIsBinaryReturnsFalseIfIntervalEmpty() { final IterableInterval<DoubleType> interval = ArrayImgs.doubles(0); final boolean result = ElementUtil.isBinary(interval); assertFalse("An empty image should not be binary", result); }
@Test public void testIsBinaryReturnsTrueForMonochrome() { final IterableInterval<DoubleType> interval = ArrayImgs.doubles(2, 2); final boolean result = ElementUtil.isBinary(interval); assertTrue("Monochrome image should be binary", result); }