kinow commented on code in PR #254: URL: https://github.com/apache/commons-imaging/pull/254#discussion_r1339171634
########## src/main/java/org/apache/commons/imaging/formats/webp/chunks/WebPChunk.java: ########## @@ -0,0 +1,83 @@ +/* + * 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 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> Review Comment: Maybe this should be documented in the parser as well. ########## src/main/java/org/apache/commons/imaging/formats/webp/WebPImageParser.java: ########## @@ -0,0 +1,326 @@ +/* + * 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.AbstractImageParser; +import org.apache.commons.imaging.ImageFormat; +import org.apache.commons.imaging.ImageFormats; +import org.apache.commons.imaging.ImageInfo; +import org.apache.commons.imaging.ImagingException; +import org.apache.commons.imaging.bytesource.ByteSource; +import org.apache.commons.imaging.common.XmpEmbeddable; +import org.apache.commons.imaging.common.XmpImagingParameters; +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.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 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; + +/** + * @since 1.0-alpha4 Review Comment: Maybe the description here could be improved, perhaps including a link to where this implementation came from (spec, docs, another library, etc.) ########## src/main/java/org/apache/commons/imaging/formats/webp/WebPImageMetadata.java: ########## @@ -0,0 +1,53 @@ +/* + * 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; + +/** + * @since 1.0-alpha4 Review Comment: Maybe the description here could be improved. ########## src/main/java/org/apache/commons/imaging/formats/webp/chunks/WebPChunkALPH.java: ########## @@ -0,0 +1,41 @@ +/* + * 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; + +/** + * <pre>{@code + * 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | ChunkHeader('ALPH') | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |Rsv| P | F | C | Alpha Bitstream... | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * }</pre> Review Comment: :ok_man: Good docs! Thanks! ########## src/main/java/org/apache/commons/imaging/formats/webp/WebPImageParser.java: ########## @@ -0,0 +1,326 @@ +/* + * 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.AbstractImageParser; +import org.apache.commons.imaging.ImageFormat; +import org.apache.commons.imaging.ImageFormats; +import org.apache.commons.imaging.ImageInfo; +import org.apache.commons.imaging.ImagingException; +import org.apache.commons.imaging.bytesource.ByteSource; +import org.apache.commons.imaging.common.XmpEmbeddable; +import org.apache.commons.imaging.common.XmpImagingParameters; +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.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 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; + +/** + * @since 1.0-alpha4 + */ +public class WebPImageParser extends AbstractImageParser<WebPImagingParameters> implements XmpEmbeddable<WebPImagingParameters> { + + 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}; + } + + /** + * Read the file header of WebP file. + * + * @return file size in file header (including the WebP signature, + * excluding the TIFF signature and the file size field). + */ + private static int readFileHeader(InputStream is) throws IOException, ImagingException { + byte[] buffer = new byte[4]; + if (is.read(buffer) < 4 || !WebPConstants.RIFF_SIGNATURE.equals(buffer)) { + throw new ImagingException("Not a Valid WebP File"); + } + + int fileSize = read4Bytes("File Size", is, "Not a Valid WebP File", ByteOrder.LITTLE_ENDIAN); + if (fileSize < 0) { + throw new ImagingException("File Size is too long:" + fileSize); + } + + if (is.read(buffer) < 4 || !WebPConstants.WEBP_SIGNATURE.equals(buffer)) { + throw new ImagingException("Not a Valid WebP File"); Review Comment: Been too long since I worked on the code, but I **think** there is an `ImageReadException`. Have to confirm if other parsers raise that or this error, then try to keep it consistent. ########## src/main/java/org/apache/commons/imaging/formats/webp/WebPImageParser.java: ########## Review Comment: I went through the other files and they all look OK. Added some minor comments. This is the only class that seems it will take a bit longer to review (which is normal, the parsers are normally the most used classes, with more functions). ########## src/test/java/org/apache/commons/imaging/formats/webp/WebPBaseTest.java: ########## @@ -0,0 +1,39 @@ +/* + * 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.*; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public abstract class WebPBaseTest extends AbstractImagingTest { Review Comment: Note to self: did we use (or started using) the since tag in tests too? Or was it in another component? -- 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]
