From 33eac436b92bbfc193e3b41e8b672a78cb637b51 Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Sat, 18 Mar 2023 11:40:47 +0100 Subject: [PATCH] WebP cleanup. (cherry picked from commit eabb8fd02b6525b8688972f1a5bc5d22c9966cd8) --- .../plugins/webp/lossless/VP8LDecoder.java | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) 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 08cd8142..0b979699 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 @@ -117,7 +117,7 @@ public final class VP8LDecoder { if (topLevel) { Rectangle bounds = new Rectangle(width, height); - fullSizeRaster = getRasterForDecoding(raster, param, bounds); + fullSizeRaster = createDecodeRaster(raster, param, bounds); // If multiple indices packed into one pixel xSize is different from raster width decodeRaster = fullSizeRaster.createWritableChild(0, 0, xSize, height, 0, 0, null); @@ -134,11 +134,41 @@ public final class VP8LDecoder { transform.applyInverse(fullSizeRaster); } - if (fullSizeRaster != raster && param != null) { + if (fullSizeRaster != raster) { copyIntoRasterWithParams(fullSizeRaster, raster, param); } } - + + private WritableRaster createDecodeRaster(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 + boolean originSet = false; + + if (param != null) { + if (param.getSourceRegion() != null && !param.getSourceRegion().contains(bounds) || + param.getSourceXSubsampling() != 1 || param.getSourceYSubsampling() != 1) { + // Can't reuse existing + return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bounds.width, bounds.height, + 4 * bounds.width, 4, new int[] {0, 1, 2, 3}, null); + } + else { + bounds.setLocation(param.getDestinationOffset()); + originSet = true; + } + } + + if (!raster.getBounds().contains(bounds)) { + // Can't reuse existing + return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bounds.width, bounds.height, 4 * bounds.width, + 4, new int[] {0, 1, 2, 3}, null); + } + + return originSet ? + // Recenter to (0, 0) + raster.createWritableChild(bounds.x, bounds.y, bounds.width, bounds.height, 0, 0, null) : + raster; + } + /** * Copy a source raster into a destination raster with optional settings applied. */ @@ -169,36 +199,6 @@ public final class VP8LDecoder { } } - 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 - boolean originSet = false; - - if (param != null) { - if (param.getSourceRegion() != null && !param.getSourceRegion().contains(bounds) || - param.getSourceXSubsampling() != 1 || param.getSourceYSubsampling() != 1) { - // Can't reuse existing - return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bounds.width, bounds.height, - 4 * bounds.width, 4, new int[] {0, 1, 2, 3}, null); - } - else { - bounds.setLocation(param.getDestinationOffset()); - originSet = true; - - } - } - if (!raster.getBounds().contains(bounds)) { - // Can't reuse existing - return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bounds.width, bounds.height, 4 * bounds.width, - 4, new int[] {0, 1, 2, 3}, null); - } - - return originSet ? - // Recenter to (0, 0) - raster.createWritableChild(bounds.x, bounds.y, bounds.width, bounds.height, 0, 0, null) : - raster; - } - private void decodeImage(WritableRaster raster, HuffmanInfo huffmanInfo, ColorCache colorCache) throws IOException { int width = raster.getWidth(); int height = raster.getHeight();