ImageCheck { public static boolean isBinary(final ImagePlus imp) { if (imp == null) { IJ.error("Image is null"); return false; } if (imp.getType() != ImagePlus.GRAY8) { return false; } final ImageStatistics stats = imp.getStatistics(); return stats.histogram[0] + stats.histogram[255] == stats.pixelCount; } private  ImageCheck(); static void dicomVoxelDepth(final ImagePlus imp); static boolean huCalibrated(final ImagePlus imp); static boolean isBinary(final ImagePlus imp); static boolean isSingleSlice(final ImagePlus imp); static boolean isVoxelIsotropic(final ImagePlus imp,
		final double tolerance);  }
@Test public void testIsBinaryReturnsFalseIfImageIsNull() throws Exception { final boolean result = ImageCheck.isBinary(null); assertFalse("Null image should not be binary", result); }
@Test public void testIsBinaryReturnsFalseIfImageHasWrongType() throws Exception { final ImagePlus testImage = mock(ImagePlus.class); final int wrongTypes[] = { ImagePlus.COLOR_256, ImagePlus.COLOR_RGB, ImagePlus.GRAY16, ImagePlus.GRAY32 }; for (final int wrongType : wrongTypes) { when(testImage.getType()).thenReturn(wrongType); final boolean result = ImageCheck.isBinary(testImage); assertFalse("Only ImagePlus.GRAY8 type should be binary", result); } }
@Test public void testIsBinaryUsesHistogramCorrectly() throws Exception { final int EIGHT_BYTES = 256; final ImageStatistics nonBinaryStats = new ImageStatistics(); nonBinaryStats.pixelCount = 3; nonBinaryStats.histogram = new int[EIGHT_BYTES]; nonBinaryStats.histogram[BINARY_BLACK] = 1; nonBinaryStats.histogram[BINARY_WHITE] = 1; final ImagePlus testImage = mock(ImagePlus.class); when(testImage.getStatistics()).thenReturn(nonBinaryStats); boolean result = ImageCheck.isBinary(testImage); assertFalse("Image with more than two colors must not be binary", result); final ImageStatistics wrongBinaryStats = new ImageStatistics(); wrongBinaryStats.pixelCount = 2; wrongBinaryStats.histogram = new int[EIGHT_BYTES]; wrongBinaryStats.histogram[BINARY_BLACK] = 1; wrongBinaryStats.histogram[BINARY_BLACK + 1] = 1; when(testImage.getStatistics()).thenReturn(wrongBinaryStats); result = ImageCheck.isBinary(testImage); assertFalse("Image with wrong two colors (not " + BINARY_BLACK + " & " + BINARY_WHITE + ") must not be binary", result); final ImageStatistics binaryStats = new ImageStatistics(); binaryStats.pixelCount = 2; binaryStats.histogram = new int[EIGHT_BYTES]; binaryStats.histogram[BINARY_BLACK] = 1; binaryStats.histogram[BINARY_WHITE] = 1; when(testImage.getStatistics()).thenReturn(binaryStats); result = ImageCheck.isBinary(testImage); assertTrue("Image with two colors (" + BINARY_BLACK + " & " + BINARY_WHITE + ") should be binary", result); }