RasterAccessorExt extends RasterAccessor { public static RasterFormatTag[] findCompatibleTags(RenderedImage srcs[], RenderedImage dst) { RasterFormatTag[] tags = RasterAccessor.findCompatibleTags(srcs, dst); if (dst.getSampleModel().getNumBands() > 1) { for (int i = 0; i < srcs.length; i++) { RenderedImage src = srcs[i]; int numBands = src.getSampleModel().getNumBands(); if ((numBands == 1 || numBands == 2) && !(src.getColorModel() instanceof IndexColorModel)) { tags[i] = applyMask(src, tags[i], GRAY_TO_RGB); } } } else if (dst.getSampleModel().getNumBands() == 1 && !(dst.getColorModel() instanceof IndexColorModel)) { int destinationDataType = dst.getSampleModel().getDataType(); for (int i = 0; i < srcs.length; i++) { RenderedImage src = srcs[i]; int sourceDataType = src.getSampleModel().getDataType(); if (destinationDataType != sourceDataType) { tags[i] = applyMask(src, tags[i], GRAY_SCALE); } } } return tags; } RasterAccessorExt(Raster raster, Rectangle rect, RasterFormatTag rft,
            ColorModel theColorModel, int targetBands, int targetDataType); static RasterFormatTag[] findCompatibleTags(RenderedImage srcs[], RenderedImage dst); static Range expandNoData(Range noData, RasterFormatTag rft, RenderedImage sourceImage,
            RenderedImage destImage); static final int GRAY_EXPANSION_MASK; static final int UNEXPANDED; static final int GRAY_TO_RGB; static final int GRAY_SCALE; }
@Test public void testPreserveScanlineStride() { BufferedImage byteGray = new BufferedImage(10, 10, BufferedImage.TYPE_BYTE_GRAY); BufferedImage rgb = new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR); BufferedImage subimage = byteGray.getSubimage(5, 5, 5, 5); RasterFormatTag[] tags = RasterAccessorExt .findCompatibleTags(new RenderedImage[] { subimage }, rgb); Raster raster = subimage.getData(); RasterAccessorExt ra = new RasterAccessorExt(raster, raster.getBounds(), tags[0], subimage.getColorModel(), 3, DataBuffer.TYPE_BYTE); assertEquals(10, ra.getScanlineStride()); }