EllipsoidFactorWrapper extends BoneJCommand { static byte[][] imgPlusToByteArray(final ImgPlus<UnsignedByteType> imgPlus) { final int w = (int) imgPlus.dimension(0); final int h = (int) imgPlus.dimension(1); final int d = (int) imgPlus.dimension(2); final byte[][] pixels = new byte[d][w * h]; final Cursor<UnsignedByteType> cursor = imgPlus.localizingCursor(); final int[] position = new int[imgPlus.numDimensions()]; while (cursor.hasNext()) { cursor.fwd(); if (cursor.get().getRealDouble() != 0.0) { cursor.localize(position); pixels[position[2]][position[1] * w + position[0]] = (byte) 0xFF; } } return pixels; }  @Override void run();  }
@Test public void testImgToByteArray(){ final int fg = 0xFF; final ArrayImg<UnsignedByteType, ByteArray> unsignedIntTypes = ArrayImgs.unsignedBytes(3, 3, 3); final ArrayRandomAccess<UnsignedByteType> access = unsignedIntTypes.randomAccess(); access.setPosition(new int[]{1,1,1}); access.get().setInteger(fg); final byte[][] bytes = EllipsoidFactorWrapper.imgPlusToByteArray(new ImgPlus<>(unsignedIntTypes)); assertEquals("Central pixel should be FG", (byte) fg, bytes[1][4]); assertEquals("Pixel at (0,0,0) should be BG", 0, bytes[0][0]); }