You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/12/14 23:06:45 UTC

[GitHub] [commons-imaging] kinow commented on a diff in pull request #254: Basic WebP Support

kinow commented on code in PR #254:
URL: https://github.com/apache/commons-imaging/pull/254#discussion_r1049042869


##########
src/main/java/org/apache/commons/imaging/formats/webp/WebPConstants.java:
##########
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.formats.webp;
+
+import org.apache.commons.imaging.common.BinaryConstant;
+
+public final class WebPConstants {
+
+    public static final BinaryConstant RIFF_SIGNATURE = new BinaryConstant(new byte[]{'R', 'I', 'F', 'F'});
+    public static final BinaryConstant WEBP_SIGNATURE = new BinaryConstant(new byte[]{'W', 'E', 'B', 'P'});

Review Comment:
   Would it be possible include Javadocs for these public fields & for the class, please?



##########
src/main/java/org/apache/commons/imaging/formats/webp/WebPImageParser.java:
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.formats.webp;
+
+import org.apache.commons.imaging.ImageFormat;
+import org.apache.commons.imaging.ImageFormats;
+import org.apache.commons.imaging.ImageInfo;
+import org.apache.commons.imaging.ImageParser;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.XmpEmbeddable;
+import org.apache.commons.imaging.common.XmpImagingParameters;
+import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
+import org.apache.commons.imaging.formats.tiff.TiffImageParser;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunk;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANIM;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANMF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkEXIF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkICCP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8L;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8X;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXMP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXYZW;
+
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+
+import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.skipBytes;
+
+public class WebPImageParser extends ImageParser<WebPImagingParameters> implements XmpEmbeddable {
+
+    private static final String DEFAULT_EXTENSION = ImageFormats.WEBP.getDefaultExtension();
+    private static final String[] ACCEPTED_EXTENSIONS = ImageFormats.WEBP.getExtensions();
+
+    @Override
+    public WebPImagingParameters getDefaultParameters() {
+        return new WebPImagingParameters();
+    }
+
+    @Override
+    public String getName() {
+        return "WebP-Custom";
+    }
+
+    @Override
+    public String getDefaultExtension() {
+        return DEFAULT_EXTENSION;
+    }
+
+    @Override
+    protected String[] getAcceptedExtensions() {
+        return ACCEPTED_EXTENSIONS;
+    }
+
+    @Override
+    protected ImageFormat[] getAcceptedTypes() {
+        return new ImageFormat[]{ImageFormats.WEBP};
+    }
+
+    static int readFileHeader(InputStream is) throws IOException, ImageReadException {
+        byte[] buffer = new byte[4];
+        if (is.read(buffer) < 4 || !WebPConstants.RIFF_SIGNATURE.equals(buffer)) {
+            throw new IOException("Not a Valid WebP File");
+        }
+
+        int fileSize = read4Bytes("File Size", is, "Not a Valid WebP File", ByteOrder.LITTLE_ENDIAN);
+        if (fileSize < 0) {
+            throw new ImageReadException("File Size is too long:" + fileSize);
+        }
+
+        if (is.read(buffer) < 4 || !WebPConstants.WEBP_SIGNATURE.equals(buffer)) {
+            throw new IOException("Not a Valid WebP File");
+        }
+
+        return fileSize;
+    }
+
+    @Override
+    public WebPImageMetadata getMetadata(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource, WebPChunkXMP.TYPE_EXIF)) {
+            WebPChunk chunk = reader.readChunk();
+            return chunk == null ? null : new WebPImageMetadata((TiffImageMetadata) new TiffImageParser().getMetadata(chunk.getBytes()));
+        }
+    }
+
+    @Override
+    public String getXmpXml(ByteSource byteSource, XmpImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource, WebPChunkXMP.TYPE_XMP)) {
+            WebPChunkXMP chunk = (WebPChunkXMP) reader.readChunk();
+            return chunk == null ? null : chunk.getXml();
+        }
+    }
+
+    @Override
+    public ImageInfo getImageInfo(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource,
+                WebPChunk.TYPE_VP8, WebPChunk.TYPE_VP8L, WebPChunk.TYPE_VP8X, WebPChunk.TYPE_ANMF)) {
+            String formatDetails;
+            int width;
+            int height;
+            int numberOfImages;
+            boolean hasAlpha = false;
+            ImageInfo.ColorType colorType = ImageInfo.ColorType.RGB;
+
+            WebPChunk chunk = reader.readChunk();
+            if (chunk instanceof WebPChunkVP8) {
+                formatDetails = "WebP/Lossy";
+                numberOfImages = 1;
+
+                WebPChunkVP8 vp8 = (WebPChunkVP8) chunk;
+                width = vp8.getWidth();
+                height = vp8.getHeight();
+                colorType = ImageInfo.ColorType.YCbCr;
+            } else if (chunk instanceof WebPChunkVP8L) {
+                formatDetails = "WebP/Lossless";
+                numberOfImages = 1;
+
+                WebPChunkVP8L vp8l = (WebPChunkVP8L) chunk;
+                width = vp8l.getImageWidth();
+                height = vp8l.getImageHeight();
+            } else if (chunk instanceof WebPChunkVP8X) {
+                WebPChunkVP8X vp8x = (WebPChunkVP8X) chunk;
+                width = vp8x.getCanvasWidth();
+                height = vp8x.getCanvasHeight();
+                hasAlpha = ((WebPChunkVP8X) chunk).isHasAlpha();
+
+                if (vp8x.isAnimation()) {
+                    formatDetails = "WebP/Animation";

Review Comment:
   Worth moving these strings to constants?



##########
src/main/java/org/apache/commons/imaging/formats/webp/WebPImageParser.java:
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.formats.webp;
+
+import org.apache.commons.imaging.ImageFormat;
+import org.apache.commons.imaging.ImageFormats;
+import org.apache.commons.imaging.ImageInfo;
+import org.apache.commons.imaging.ImageParser;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.XmpEmbeddable;
+import org.apache.commons.imaging.common.XmpImagingParameters;
+import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
+import org.apache.commons.imaging.formats.tiff.TiffImageParser;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunk;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANIM;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANMF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkEXIF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkICCP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8L;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8X;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXMP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXYZW;
+
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+
+import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.skipBytes;
+
+public class WebPImageParser extends ImageParser<WebPImagingParameters> implements XmpEmbeddable {
+
+    private static final String DEFAULT_EXTENSION = ImageFormats.WEBP.getDefaultExtension();
+    private static final String[] ACCEPTED_EXTENSIONS = ImageFormats.WEBP.getExtensions();
+
+    @Override
+    public WebPImagingParameters getDefaultParameters() {
+        return new WebPImagingParameters();
+    }
+
+    @Override
+    public String getName() {
+        return "WebP-Custom";
+    }
+
+    @Override
+    public String getDefaultExtension() {
+        return DEFAULT_EXTENSION;
+    }
+
+    @Override
+    protected String[] getAcceptedExtensions() {
+        return ACCEPTED_EXTENSIONS;
+    }
+
+    @Override
+    protected ImageFormat[] getAcceptedTypes() {
+        return new ImageFormat[]{ImageFormats.WEBP};
+    }
+
+    static int readFileHeader(InputStream is) throws IOException, ImageReadException {
+        byte[] buffer = new byte[4];
+        if (is.read(buffer) < 4 || !WebPConstants.RIFF_SIGNATURE.equals(buffer)) {
+            throw new IOException("Not a Valid WebP File");
+        }
+
+        int fileSize = read4Bytes("File Size", is, "Not a Valid WebP File", ByteOrder.LITTLE_ENDIAN);
+        if (fileSize < 0) {
+            throw new ImageReadException("File Size is too long:" + fileSize);
+        }
+
+        if (is.read(buffer) < 4 || !WebPConstants.WEBP_SIGNATURE.equals(buffer)) {
+            throw new IOException("Not a Valid WebP File");
+        }
+
+        return fileSize;
+    }
+
+    @Override
+    public WebPImageMetadata getMetadata(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource, WebPChunkXMP.TYPE_EXIF)) {
+            WebPChunk chunk = reader.readChunk();
+            return chunk == null ? null : new WebPImageMetadata((TiffImageMetadata) new TiffImageParser().getMetadata(chunk.getBytes()));
+        }
+    }
+
+    @Override
+    public String getXmpXml(ByteSource byteSource, XmpImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource, WebPChunkXMP.TYPE_XMP)) {
+            WebPChunkXMP chunk = (WebPChunkXMP) reader.readChunk();
+            return chunk == null ? null : chunk.getXml();
+        }
+    }
+
+    @Override
+    public ImageInfo getImageInfo(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource,
+                WebPChunk.TYPE_VP8, WebPChunk.TYPE_VP8L, WebPChunk.TYPE_VP8X, WebPChunk.TYPE_ANMF)) {
+            String formatDetails;
+            int width;
+            int height;
+            int numberOfImages;
+            boolean hasAlpha = false;
+            ImageInfo.ColorType colorType = ImageInfo.ColorType.RGB;
+
+            WebPChunk chunk = reader.readChunk();
+            if (chunk instanceof WebPChunkVP8) {
+                formatDetails = "WebP/Lossy";
+                numberOfImages = 1;
+
+                WebPChunkVP8 vp8 = (WebPChunkVP8) chunk;
+                width = vp8.getWidth();
+                height = vp8.getHeight();
+                colorType = ImageInfo.ColorType.YCbCr;
+            } else if (chunk instanceof WebPChunkVP8L) {
+                formatDetails = "WebP/Lossless";
+                numberOfImages = 1;
+
+                WebPChunkVP8L vp8l = (WebPChunkVP8L) chunk;
+                width = vp8l.getImageWidth();
+                height = vp8l.getImageHeight();
+            } else if (chunk instanceof WebPChunkVP8X) {
+                WebPChunkVP8X vp8x = (WebPChunkVP8X) chunk;
+                width = vp8x.getCanvasWidth();
+                height = vp8x.getCanvasHeight();
+                hasAlpha = ((WebPChunkVP8X) chunk).isHasAlpha();
+
+                if (vp8x.isAnimation()) {
+                    formatDetails = "WebP/Animation";
+
+                    numberOfImages = 0;
+                    while ((chunk = reader.readChunk()) != null) {
+                        if (chunk.getType() == WebPChunk.TYPE_ANMF) {
+                            numberOfImages++;
+                        }
+                    }
+
+                } else {
+                    numberOfImages = 1;
+                    chunk = reader.readChunk();
+
+                    if (chunk == null) {
+                        throw new ImageReadException("Image has no content");
+                    }
+
+                    if (chunk.getType() == WebPChunk.TYPE_ANMF) {
+                        throw new ImageReadException("Non animated image should not contain ANMF chunks");
+                    }
+
+                    if (chunk.getType() == WebPChunk.TYPE_VP8) {
+                        formatDetails = "WebP/Lossy (Extended)";
+                        colorType = ImageInfo.ColorType.YCbCr;
+                    } else if (chunk.getType() == WebPChunk.TYPE_VP8L) {
+                        formatDetails = "WebP/Lossless (Extended)";
+                    } else {
+                        throw new ImageReadException("Unknown webp chunk type: " + chunk);
+                    }
+                }
+            } else {
+                throw new ImageReadException("Unknown webp chunk type: " + chunk);
+            }
+
+            return new ImageInfo(
+                    formatDetails, 32, new ArrayList<>(), ImageFormats.WEBP,
+                    "webp", height, "image/webp", numberOfImages,
+                    -1, -1, -1, -1,
+                    width, false, hasAlpha, false,
+                    colorType, ImageInfo.CompressionAlgorithm.UNKNOWN
+            );
+        }
+    }
+
+    @Override
+    public Dimension getImageSize(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource)) {
+            WebPChunk chunk = reader.readChunk();
+            if (chunk instanceof WebPChunkVP8) {
+                WebPChunkVP8 vp8 = (WebPChunkVP8) chunk;
+                return new Dimension(vp8.getWidth(), vp8.getHeight());
+            } else if (chunk instanceof WebPChunkVP8L) {
+                WebPChunkVP8L vp8l = (WebPChunkVP8L) chunk;
+                return new Dimension(vp8l.getImageWidth(), vp8l.getImageHeight());
+            } else if (chunk instanceof WebPChunkVP8X) {
+                WebPChunkVP8X vp8x = (WebPChunkVP8X) chunk;
+                return new Dimension(vp8x.getCanvasWidth(), vp8x.getCanvasHeight());
+            } else {
+                throw new ImageReadException("Unknown webp chunk type: " + chunk);

Review Comment:
   webp & WebP, to be consistent in the error messages.



##########
src/main/java/org/apache/commons/imaging/formats/webp/WebPImageMetadata.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.formats.webp;
+
+import org.apache.commons.imaging.common.GenericImageMetadata;
+import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public final class WebPImageMetadata extends GenericImageMetadata {

Review Comment:
   Ditto here, javadocs for public members.



##########
src/main/java/org/apache/commons/imaging/formats/webp/WebPImageParser.java:
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.formats.webp;
+
+import org.apache.commons.imaging.ImageFormat;
+import org.apache.commons.imaging.ImageFormats;
+import org.apache.commons.imaging.ImageInfo;
+import org.apache.commons.imaging.ImageParser;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.XmpEmbeddable;
+import org.apache.commons.imaging.common.XmpImagingParameters;
+import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
+import org.apache.commons.imaging.formats.tiff.TiffImageParser;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunk;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANIM;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANMF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkEXIF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkICCP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8L;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8X;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXMP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXYZW;
+
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+
+import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.skipBytes;
+
+public class WebPImageParser extends ImageParser<WebPImagingParameters> implements XmpEmbeddable {
+
+    private static final String DEFAULT_EXTENSION = ImageFormats.WEBP.getDefaultExtension();
+    private static final String[] ACCEPTED_EXTENSIONS = ImageFormats.WEBP.getExtensions();
+
+    @Override
+    public WebPImagingParameters getDefaultParameters() {
+        return new WebPImagingParameters();
+    }
+
+    @Override
+    public String getName() {
+        return "WebP-Custom";
+    }
+
+    @Override
+    public String getDefaultExtension() {
+        return DEFAULT_EXTENSION;
+    }
+
+    @Override
+    protected String[] getAcceptedExtensions() {
+        return ACCEPTED_EXTENSIONS;
+    }
+
+    @Override
+    protected ImageFormat[] getAcceptedTypes() {
+        return new ImageFormat[]{ImageFormats.WEBP};
+    }
+
+    static int readFileHeader(InputStream is) throws IOException, ImageReadException {

Review Comment:
   For these internal methods, Javadocs are not mandatory, but still recommended, even if just a short description, to document it for other devs / posterity / etc. :+1: 



##########
src/main/java/org/apache/commons/imaging/formats/webp/WebPImageParser.java:
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.formats.webp;
+
+import org.apache.commons.imaging.ImageFormat;
+import org.apache.commons.imaging.ImageFormats;
+import org.apache.commons.imaging.ImageInfo;
+import org.apache.commons.imaging.ImageParser;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.XmpEmbeddable;
+import org.apache.commons.imaging.common.XmpImagingParameters;
+import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
+import org.apache.commons.imaging.formats.tiff.TiffImageParser;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunk;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANIM;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANMF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkEXIF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkICCP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8L;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8X;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXMP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXYZW;
+
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+
+import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.skipBytes;
+
+public class WebPImageParser extends ImageParser<WebPImagingParameters> implements XmpEmbeddable {
+
+    private static final String DEFAULT_EXTENSION = ImageFormats.WEBP.getDefaultExtension();
+    private static final String[] ACCEPTED_EXTENSIONS = ImageFormats.WEBP.getExtensions();
+
+    @Override
+    public WebPImagingParameters getDefaultParameters() {
+        return new WebPImagingParameters();
+    }
+
+    @Override
+    public String getName() {
+        return "WebP-Custom";
+    }
+
+    @Override
+    public String getDefaultExtension() {
+        return DEFAULT_EXTENSION;
+    }
+
+    @Override
+    protected String[] getAcceptedExtensions() {
+        return ACCEPTED_EXTENSIONS;
+    }
+
+    @Override
+    protected ImageFormat[] getAcceptedTypes() {
+        return new ImageFormat[]{ImageFormats.WEBP};
+    }
+
+    static int readFileHeader(InputStream is) throws IOException, ImageReadException {
+        byte[] buffer = new byte[4];
+        if (is.read(buffer) < 4 || !WebPConstants.RIFF_SIGNATURE.equals(buffer)) {
+            throw new IOException("Not a Valid WebP File");
+        }
+
+        int fileSize = read4Bytes("File Size", is, "Not a Valid WebP File", ByteOrder.LITTLE_ENDIAN);
+        if (fileSize < 0) {
+            throw new ImageReadException("File Size is too long:" + fileSize);
+        }
+
+        if (is.read(buffer) < 4 || !WebPConstants.WEBP_SIGNATURE.equals(buffer)) {
+            throw new IOException("Not a Valid WebP File");

Review Comment:
   Was there some reason for throwing both `IOException` and `ImageReadException` here?
   
   I think the other parsers should be probably throwing `ImageReadException`'s when the bytes read are not what it was expected (I think IPTC is an exception). Other classes that deal with byte reading/processing I think throw `IOException`... or when we are reading a stream.



##########
src/main/java/org/apache/commons/imaging/formats/webp/WebPImageParser.java:
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.formats.webp;
+
+import org.apache.commons.imaging.ImageFormat;
+import org.apache.commons.imaging.ImageFormats;
+import org.apache.commons.imaging.ImageInfo;
+import org.apache.commons.imaging.ImageParser;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.XmpEmbeddable;
+import org.apache.commons.imaging.common.XmpImagingParameters;
+import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
+import org.apache.commons.imaging.formats.tiff.TiffImageParser;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunk;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANIM;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkANMF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkEXIF;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkICCP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8L;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkVP8X;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXMP;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXYZW;
+
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+
+import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.skipBytes;
+
+public class WebPImageParser extends ImageParser<WebPImagingParameters> implements XmpEmbeddable {
+
+    private static final String DEFAULT_EXTENSION = ImageFormats.WEBP.getDefaultExtension();
+    private static final String[] ACCEPTED_EXTENSIONS = ImageFormats.WEBP.getExtensions();
+
+    @Override
+    public WebPImagingParameters getDefaultParameters() {
+        return new WebPImagingParameters();
+    }
+
+    @Override
+    public String getName() {
+        return "WebP-Custom";
+    }
+
+    @Override
+    public String getDefaultExtension() {
+        return DEFAULT_EXTENSION;
+    }
+
+    @Override
+    protected String[] getAcceptedExtensions() {
+        return ACCEPTED_EXTENSIONS;
+    }
+
+    @Override
+    protected ImageFormat[] getAcceptedTypes() {
+        return new ImageFormat[]{ImageFormats.WEBP};
+    }
+
+    static int readFileHeader(InputStream is) throws IOException, ImageReadException {
+        byte[] buffer = new byte[4];
+        if (is.read(buffer) < 4 || !WebPConstants.RIFF_SIGNATURE.equals(buffer)) {
+            throw new IOException("Not a Valid WebP File");
+        }
+
+        int fileSize = read4Bytes("File Size", is, "Not a Valid WebP File", ByteOrder.LITTLE_ENDIAN);
+        if (fileSize < 0) {
+            throw new ImageReadException("File Size is too long:" + fileSize);
+        }
+
+        if (is.read(buffer) < 4 || !WebPConstants.WEBP_SIGNATURE.equals(buffer)) {
+            throw new IOException("Not a Valid WebP File");
+        }
+
+        return fileSize;
+    }
+
+    @Override
+    public WebPImageMetadata getMetadata(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource, WebPChunkXMP.TYPE_EXIF)) {
+            WebPChunk chunk = reader.readChunk();
+            return chunk == null ? null : new WebPImageMetadata((TiffImageMetadata) new TiffImageParser().getMetadata(chunk.getBytes()));
+        }
+    }
+
+    @Override
+    public String getXmpXml(ByteSource byteSource, XmpImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource, WebPChunkXMP.TYPE_XMP)) {
+            WebPChunkXMP chunk = (WebPChunkXMP) reader.readChunk();
+            return chunk == null ? null : chunk.getXml();
+        }
+    }
+
+    @Override
+    public ImageInfo getImageInfo(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource,
+                WebPChunk.TYPE_VP8, WebPChunk.TYPE_VP8L, WebPChunk.TYPE_VP8X, WebPChunk.TYPE_ANMF)) {
+            String formatDetails;
+            int width;
+            int height;
+            int numberOfImages;
+            boolean hasAlpha = false;
+            ImageInfo.ColorType colorType = ImageInfo.ColorType.RGB;
+
+            WebPChunk chunk = reader.readChunk();
+            if (chunk instanceof WebPChunkVP8) {
+                formatDetails = "WebP/Lossy";
+                numberOfImages = 1;
+
+                WebPChunkVP8 vp8 = (WebPChunkVP8) chunk;
+                width = vp8.getWidth();
+                height = vp8.getHeight();
+                colorType = ImageInfo.ColorType.YCbCr;
+            } else if (chunk instanceof WebPChunkVP8L) {
+                formatDetails = "WebP/Lossless";
+                numberOfImages = 1;
+
+                WebPChunkVP8L vp8l = (WebPChunkVP8L) chunk;
+                width = vp8l.getImageWidth();
+                height = vp8l.getImageHeight();
+            } else if (chunk instanceof WebPChunkVP8X) {
+                WebPChunkVP8X vp8x = (WebPChunkVP8X) chunk;
+                width = vp8x.getCanvasWidth();
+                height = vp8x.getCanvasHeight();
+                hasAlpha = ((WebPChunkVP8X) chunk).isHasAlpha();
+
+                if (vp8x.isAnimation()) {
+                    formatDetails = "WebP/Animation";
+
+                    numberOfImages = 0;
+                    while ((chunk = reader.readChunk()) != null) {
+                        if (chunk.getType() == WebPChunk.TYPE_ANMF) {
+                            numberOfImages++;
+                        }
+                    }
+
+                } else {
+                    numberOfImages = 1;
+                    chunk = reader.readChunk();
+
+                    if (chunk == null) {
+                        throw new ImageReadException("Image has no content");
+                    }
+
+                    if (chunk.getType() == WebPChunk.TYPE_ANMF) {
+                        throw new ImageReadException("Non animated image should not contain ANMF chunks");
+                    }
+
+                    if (chunk.getType() == WebPChunk.TYPE_VP8) {
+                        formatDetails = "WebP/Lossy (Extended)";
+                        colorType = ImageInfo.ColorType.YCbCr;
+                    } else if (chunk.getType() == WebPChunk.TYPE_VP8L) {
+                        formatDetails = "WebP/Lossless (Extended)";
+                    } else {
+                        throw new ImageReadException("Unknown webp chunk type: " + chunk);
+                    }
+                }
+            } else {
+                throw new ImageReadException("Unknown webp chunk type: " + chunk);
+            }
+
+            return new ImageInfo(
+                    formatDetails, 32, new ArrayList<>(), ImageFormats.WEBP,
+                    "webp", height, "image/webp", numberOfImages,
+                    -1, -1, -1, -1,
+                    width, false, hasAlpha, false,
+                    colorType, ImageInfo.CompressionAlgorithm.UNKNOWN
+            );
+        }
+    }
+
+    @Override
+    public Dimension getImageSize(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource)) {
+            WebPChunk chunk = reader.readChunk();
+            if (chunk instanceof WebPChunkVP8) {
+                WebPChunkVP8 vp8 = (WebPChunkVP8) chunk;
+                return new Dimension(vp8.getWidth(), vp8.getHeight());
+            } else if (chunk instanceof WebPChunkVP8L) {
+                WebPChunkVP8L vp8l = (WebPChunkVP8L) chunk;
+                return new Dimension(vp8l.getImageWidth(), vp8l.getImageHeight());
+            } else if (chunk instanceof WebPChunkVP8X) {
+                WebPChunkVP8X vp8x = (WebPChunkVP8X) chunk;
+                return new Dimension(vp8x.getCanvasWidth(), vp8x.getCanvasHeight());
+            } else {
+                throw new ImageReadException("Unknown webp chunk type: " + chunk);
+            }
+        }
+    }
+
+    @Override
+    public byte[] getICCProfileBytes(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        try (ChunksReader reader = new ChunksReader(byteSource, WebPChunkXMP.TYPE_ICCP)) {
+            WebPChunk chunk = reader.readChunk();
+            return chunk == null ? null : chunk.getBytes();
+        }
+    }
+
+    @Override
+    public BufferedImage getBufferedImage(ByteSource byteSource, WebPImagingParameters params) throws ImageReadException, IOException {
+        throw new ImageReadException("Reading WebP files is currently not supported");
+    }
+
+    @Override
+    public boolean dumpImageFile(PrintWriter pw, ByteSource byteSource) throws ImageReadException, IOException {
+        pw.println("webp.dumpImageFile");
+        try (ChunksReader reader = new ChunksReader(byteSource)) {
+            int offset = reader.getOffset();
+            WebPChunk chunk = reader.readChunk();
+            if (chunk == null) {
+                throw new ImageReadException("No WebP chunks found");
+            }
+
+            do {
+                chunk.dump(pw, offset);
+
+                offset = reader.getOffset();
+                chunk = reader.readChunk();
+            } while (chunk != null);

Review Comment:
   Adding a note to self: check for possible loops & add/multiply that could be made more defensive/safe to avoid CVE's (we had a few found by users & by the oss-fuzzer)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org