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


##########
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:
   I think these could be constants in the class, like the extensions. Will 
move them as it's harmless :man_shrugging: 



-- 
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-unsubscr...@commons.apache.org

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

Reply via email to