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.0-alpha4
33 */
34 public abstract class AbstractWebPChunk extends BinaryFileParser {
35
36 private static boolean checkArgs(final int size, final byte[] bytes) throws ImagingException {
37 if (size != bytes.length) {
38 throw new ImagingException("Chunk size must match bytes length");
39 }
40 return true;
41 }
42
43 private final int type;
44 private final int size;
45 protected final byte[] bytes;
46 private final int chunkSize;
47
48 /**
49 * Create a new WebP chunk.
50 *
51 * @param type chunk type.
52 * @param size chunk size.
53 * @param bytes chunk data.
54 * @throws ImagingException if the chunk data and the size provided do not match.
55 */
56 public AbstractWebPChunk(final int type, final int size, final byte[] bytes) throws ImagingException {
57 this(type, size, bytes, checkArgs(size, bytes));
58 }
59
60 private AbstractWebPChunk(final int type, final int size, final byte[] bytes, final boolean ignored) {
61 super(ByteOrder.LITTLE_ENDIAN);
62 this.type = type;
63 this.size = bytes.length;
64 this.bytes = bytes;
65 // if chunk size is odd, a single padding byte is added
66 final int padding = size % 2 != 0 ? 1 : 0;
67 // Chunk FourCC (4 bytes) + Chunk Size (4 bytes) + Chunk Payload (n bytes) + Padding
68 this.chunkSize = SafeOperations.add(4, 4, size, padding);
69 }
70
71 /**
72 * Print the chunk to the given stream.
73 *
74 * @param pw a stream to write to.
75 * @param offset chunk offset.
76 * @throws ImagingException if the image is invalid.
77 * @throws IOException if it fails to write to the given stream.
78 */
79 public void dump(final PrintWriter pw, final int offset) throws ImagingException, IOException {
80 pw.printf("Chunk %s at offset %s, length %d%n, payload size %d%n", getTypeDescription(), offset >= 0 ? String.valueOf(offset) : "unknown",
81 getChunkSize(), getPayloadSize());
82 }
83
84 /**
85 * @return a copy of the chunk data as bytes.
86 */
87 public byte[] getBytes() {
88 return bytes.clone();
89 }
90
91 /**
92 * @return the chunk size.
93 */
94 public int getChunkSize() {
95 return chunkSize;
96 }
97
98 /**
99 * @return the payload size.
100 */
101 public int getPayloadSize() {
102 return size;
103 }
104
105 /**
106 * @return the chunk type.
107 */
108 public int getType() {
109 return type;
110 }
111
112 /**
113 * @return the description of the chunk type.
114 */
115 public String getTypeDescription() {
116 return new String(new byte[] { (byte) (type & 0xff), (byte) (type >> 8 & 0xff), (byte) (type >> 16 & 0xff), (byte) (type >> 24 & 0xff) },
117 StandardCharsets.UTF_8);
118 }
119 }