
/**
 * Example showing how the inner image can be processed using the current internal API. This API is for practical
 * purposes no longer accessible in Java 9.
 */
public class ExampleCode {
	public static void bufferedToGray(ByteInterleavedRaster src, byte[] grayImage ) {

		// For sake of simplicity, in this example it is assumed that both images and the same width and height
		int width = src.getWidth();
		int height = src.getHeight();

		byte[] srcData = src.getDataStorage();

		// Images are stored in a row major format.
		// offset specified the start of the image in the array
		// stride is the number of pixels that need to be skipped to go from one row to the next.
		//        if not a sub image stride will be the image width * number of bands
		int srcNumBands = src.getNumBands();
		int srcStride = src.getScanlineStride();
		int srcOffset = src.getDataOffset(0);
		for (int i = 1; i &lt; src.getNumBands(); i++) {
			srcOffset = Math.min(srcOffset,src.getDataOffset(i));
		}
		// Ideally the three functions above would be brought up into WritableRaster or a new interface could be
		// created ImageDataRowMajor (or similar) could be added that would be provide access. In rare cases I
		// believe that there are images which don't implement this format.

		if (srcNumBands == 3) {
			int indexRowSrc = srcOffset;
			for (int y = 0; y &lt; height; y++) {
				int indexSrc = indexRowSrc;
				int indexDst = width * y;
				int indexDstEnd = indexDst + width;
				for (; indexDst &lt; indexDstEnd; indexDst++) {
					int r = srcData[indexSrc++] &amp; 0xFF;
					int g = srcData[indexSrc++] &amp; 0xFF;
					int b = srcData[indexSrc++] &amp; 0xFF;

					int ave = (r + g + b) / 3;

					grayImage[indexDst] = (byte) ave;
				}
				srcOffset += srcStride;
			}
		} else {
			throw new RuntimeException("Ignored in this code example");
		}
	}

	public static void main(String[] args) {
		BufferedImage input = new BufferedImage(800,600,BufferedImage.TYPE_3BYTE_BGR);

		byte[] output = new byte[input.getWidth()*input.getHeight()];

		bufferedToGray((ByteInterleavedRaster)input.getRaster(),output);
	}
}



          