diff --git a/imageio/imageio-webp/src/main/java/com/twelvemonkeys/imageio/plugins/webp/lossless/VP8LDecoder.java b/imageio/imageio-webp/src/main/java/com/twelvemonkeys/imageio/plugins/webp/lossless/VP8LDecoder.java index 49c52017..1f0d67c0 100644 --- a/imageio/imageio-webp/src/main/java/com/twelvemonkeys/imageio/plugins/webp/lossless/VP8LDecoder.java +++ b/imageio/imageio-webp/src/main/java/com/twelvemonkeys/imageio/plugins/webp/lossless/VP8LDecoder.java @@ -135,38 +135,49 @@ public final class VP8LDecoder { } if (fullSizeRaster != raster && param != null) { - // Copy into destination raster with settings applied - Rectangle sourceRegion = param.getSourceRegion(); - int sourceXSubsampling = param.getSourceXSubsampling(); - int sourceYSubsampling = param.getSourceYSubsampling(); - int subsamplingXOffset = param.getSubsamplingXOffset(); - int subsamplingYOffset = param.getSubsamplingYOffset(); - Point destinationOffset = param.getDestinationOffset(); + copyIntoRasterWithParams(fullSizeRaster, decodeRaster, param); + } + } + + /** + * Copy a source raster into a destination raster with settings applied. + * + * TODO: This piece of was inside readVP8Lossless(), but as it can be used in + * readAlpha(), this utility static method was created here. Probably, it should + * be moved to another class. + */ + public static void copyIntoRasterWithParams(final Raster srcRaster, final WritableRaster dstRaster, + final ImageReadParam param) { + Rectangle sourceRegion = param.getSourceRegion(); + int sourceXSubsampling = param.getSourceXSubsampling(); + int sourceYSubsampling = param.getSourceYSubsampling(); + int subsamplingXOffset = param.getSubsamplingXOffset(); + int subsamplingYOffset = param.getSubsamplingYOffset(); + Point destinationOffset = param.getDestinationOffset(); - if (sourceRegion == null) { - sourceRegion = raster.getBounds(); - } + if (sourceRegion == null) { + sourceRegion = dstRaster.getBounds(); + } - if (sourceXSubsampling == 1 && sourceYSubsampling == 1) { - // Only apply offset (and limit to requested region) - raster.setRect(destinationOffset.x, destinationOffset.y, fullSizeRaster); - } - else { - // Manual copy, more efficient way might exist - byte[] rgba = new byte[4]; - int xEnd = raster.getWidth() + raster.getMinX(); - int yEnd = raster.getHeight() + raster.getMinY(); + if (sourceXSubsampling == 1 && sourceYSubsampling == 1) { + // Only apply offset (and limit to requested region) + dstRaster.setRect(destinationOffset.x, destinationOffset.y, srcRaster); + } + else { + // Manual copy, more efficient way might exist + byte[] rgba = new byte[4]; + int xEnd = dstRaster.getWidth() + dstRaster.getMinX(); + int yEnd = dstRaster.getHeight() + dstRaster.getMinY(); - for (int xDst = destinationOffset.x, xSrc = sourceRegion.x + subsamplingXOffset; xDst < xEnd; xDst++, xSrc += sourceXSubsampling) { - for (int yDst = destinationOffset.y, ySrc = sourceRegion.y + subsamplingYOffset; yDst < yEnd; yDst++, ySrc += sourceYSubsampling) { - fullSizeRaster.getDataElements(xSrc, ySrc, rgba); - raster.setDataElements(xDst, yDst, rgba); - } + for (int xDst = destinationOffset.x, xSrc = sourceRegion.x + subsamplingXOffset; xDst < xEnd; xDst++, xSrc += sourceXSubsampling) { + for (int yDst = destinationOffset.y, ySrc = sourceRegion.y + subsamplingYOffset; yDst < yEnd; yDst++, ySrc += sourceYSubsampling) { + srcRaster.getDataElements(xSrc, ySrc, rgba); + dstRaster.setDataElements(xDst, yDst, rgba); } } } } - + private WritableRaster getRasterForDecoding(WritableRaster raster, ImageReadParam param, Rectangle bounds) { // If the ImageReadParam requires only a subregion of the image, and if the whole image does not fit into the // Raster or subsampling is requested, we need a temporary Raster as we can only decode the whole image at once