View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.webp.chunks;
18  
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  import java.nio.ByteOrder;
22  import java.nio.charset.StandardCharsets;
23  
24  import org.apache.commons.imaging.ImagingException;
25  import org.apache.commons.imaging.common.BinaryFileParser;
26  import org.apache.commons.imaging.internal.SafeOperations;
27  
28  /**
29   * A WebP image is composed of several chunks. This is the base class for the chunks, used by the parser.
30   *
31   * @see <a href="https://developers.google.com/speed/webp/docs/riff_container">WebP Container Specification</a>
32   * @since 1.0-alpha4
33   */
34  public abstract class WebPChunk extends BinaryFileParser {
35      private final int type;
36      private final int size;
37      protected final byte[] bytes;
38      private final int chunkSize;
39  
40      /**
41       * Create a new WebP chunk.
42       *
43       * @param type  chunk type.
44       * @param size  chunk size.
45       * @param bytes chunk data.
46       * @throws ImagingException if the chunk data and the size provided do not match.
47       */
48      WebPChunk(final int type, final int size, final byte[] bytes) throws ImagingException {
49          super(ByteOrder.LITTLE_ENDIAN);
50  
51          if (size != bytes.length) {
52              throw new ImagingException("Chunk size must match bytes length");
53          }
54  
55          this.type = type;
56          this.size = size;
57          this.bytes = bytes;
58  
59          // if chunk size is odd, a single padding byte is added
60          final int padding = size % 2 != 0 ? 1 : 0;
61  
62          // Chunk FourCC (4 bytes) + Chunk Size (4 bytes) + Chunk Payload (n bytes) + Padding
63          this.chunkSize = SafeOperations.add(4, 4, size, padding);
64      }
65  
66      /**
67       * Print the chunk to the given stream.
68       *
69       * @param pw     a stream to write to.
70       * @param offset chunk offset.
71       * @throws ImagingException if the image is invalid.
72       * @throws IOException      if it fails to write to the given stream.
73       */
74      public void dump(final PrintWriter pw, final int offset) throws ImagingException, IOException {
75          pw.printf("Chunk %s at offset %s, length %d%n, payload size %d%n", getTypeDescription(), offset >= 0 ? String.valueOf(offset) : "unknown",
76                  getChunkSize(), getPayloadSize());
77      }
78  
79      /**
80       * @return a copy of the chunk data as bytes.
81       */
82      public byte[] getBytes() {
83          return bytes.clone();
84      }
85  
86      /**
87       * @return the chunk size.
88       */
89      public int getChunkSize() {
90          return chunkSize;
91      }
92  
93      /**
94       * @return the payload size.
95       */
96      public int getPayloadSize() {
97          return size;
98      }
99  
100     /**
101      * @return the chunk type.
102      */
103     public int getType() {
104         return type;
105     }
106 
107     /**
108      * @return the description of the chunk type.
109      */
110     public String getTypeDescription() {
111         return new String(new byte[] { (byte) (type & 0xff), (byte) (type >> 8 & 0xff), (byte) (type >> 16 & 0xff), (byte) (type >> 24 & 0xff) },
112                 StandardCharsets.UTF_8);
113     }
114 }