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


##########
src/main/java/org/apache/commons/imaging/formats/webp/WebPChunkType.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.ImagingException;
+import org.apache.commons.imaging.common.BinaryFunctions;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunk;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkAlph;
+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.WebPChunkXml;
+import org.apache.commons.imaging.formats.webp.chunks.WebPChunkXyzw;
+
+import java.io.IOException;
+
+/**
+ * WebP chunk type.
+ *
+ * @since 1.0-alpha4
+ */
+public enum WebPChunkType {
+
+    /**
+     * @see WebPChunkAlph
+     */
+    ALPH(WebPChunkAlph::new),

Review Comment:
   Added `ALPH` that was not listed here before.



##########
src/main/java/org/apache/commons/imaging/formats/webp/chunks/WebPChunk.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.chunks;
+
+import org.apache.commons.imaging.ImagingException;
+import org.apache.commons.imaging.common.BinaryFileParser;
+import org.apache.commons.imaging.internal.SafeOperations;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * A WebP image is composed of several chunks. This is the base class for the 
chunks,
+ * used by the parser.
+ *
+ * @see <a 
href="https://developers.google.com/speed/webp/docs/riff_container";>WebP 
Container Specification</a>
+ * @since 1.0-alpha4
+ */
+public abstract class WebPChunk extends BinaryFileParser {
+    private final int type;
+    private final int size;
+    protected final byte[] bytes;
+    private final int chunkSize;
+
+    /**
+     * Create a new WebP chunk.
+     *
+     * @param type  chunk type.
+     * @param size  chunk size.
+     * @param bytes chunk data.
+     * @throws ImagingException if the chunk data and the size provided do not 
match.
+     */
+    WebPChunk(int type, int size, byte[] bytes) throws ImagingException {
+        super(ByteOrder.LITTLE_ENDIAN);
+
+        if (size != bytes.length) {
+            throw new ImagingException("Chunk size must match bytes length");
+        }
+
+        this.type = type;
+        this.size = size;
+        this.bytes = bytes;
+
+        // if chunk size is odd, a single padding byte is added
+        int padding = (size % 2) != 0 ? 1 : 0;
+
+        // Chunk FourCC (4 bytes) + Chunk Size (4 bytes) + Chunk Payload (n 
bytes) + Padding
+        this.chunkSize = SafeOperations.add(4, 4, size, padding);
+    }
+
+    /**
+     * @return the chunk type.
+     */
+    public int getType() {
+        return type;
+    }
+
+    /**
+     * @return the description of the chunk type.
+     */
+    public String getTypeDescription() {
+        return new String(new byte[]{
+                (byte) (type & 0xff),
+                (byte) ((type >> 8) & 0xff),
+                (byte) ((type >> 16) & 0xff),
+                (byte) ((type >> 24) & 0xff)}, StandardCharsets.UTF_8);
+    }
+
+    /**
+     * @return the payload size.
+     */
+    public int getPayloadSize() {
+        return size;
+    }
+
+    /**
+     * @return the chunk size.
+     */
+    public int getChunkSize() {
+        return chunkSize;
+    }
+
+    /**
+     * @return a copy of the chunk data as bytes.
+     */
+    public byte[] getBytes() {
+        return bytes.clone();
+    }
+
+    /**
+     * Print the chunk to the given stream.
+     *
+     * @param pw     a stream to write to.
+     * @param offset chunk offset.
+     * @throws ImagingException if the image is invalid.
+     * @throws IOException      if it fails to write to the given stream.
+     */
+    public void dump(PrintWriter pw, int offset) throws ImagingException, 
IOException {
+        pw.printf(
+                "Chunk %s at offset %s, length %d%n, payload size %d%n",
+                getTypeDescription(),
+                offset >= 0 ? String.valueOf(offset) : "unknown",
+                getChunkSize(),
+                getPayloadSize());

Review Comment:
   Modified the `dump` methods to call the getters/setters, this way we stress 
every method in the tests.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to