Debian Codegraph @

src/com/drew/metadata/webp/WebpRiffHandler.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21 package com.drew.metadata.webp;
22
23 import com.drew.imaging.riff.RiffHandler;
24 import com.drew.lang.ByteArrayReader;
25 import com.drew.lang.RandomAccessReader;
26 import com.drew.lang.annotations.NotNull;
27 import com.drew.metadata.Metadata;
28 import com.drew.metadata.exif.ExifReader;
29 import com.drew.metadata.icc.IccReader;
30 import com.drew.metadata.xmp.XmpReader;
31
32 import java.io.IOException;
33
34 /**
35 * Implementation of {@link RiffHandler} specialising in WebP support.
36 *
37 * Extracts data from chunk types:
38 *
39 * <ul>
40 * <li><code>"VP8X"</code>: width, height, is animation, has alpha</li>
41 * <li><code>"EXIF"</code>: full Exif data</li>
42 * <li><code>"ICCP"</code>: full ICC profile</li>
43 * <li><code>"XMP "</code>: full XMP data</li>
44 * </ul>
45 */
46 public class WebpRiffHandler implements RiffHandler
47 {
48 @NotNull
49 private final Metadata _metadata;
50
51 public WebpRiffHandler(@NotNull Metadata metadata)
52 {
53 _metadata = metadata;
54 }
55
56 public boolean shouldAcceptRiffIdentifier(@NotNull String identifier)
57 {
58 return identifier.equals(WebpDirectory.FORMAT);
59 }
60
61 public boolean shouldAcceptChunk(@NotNull String fourCC)
62 {
63 return fourCC.equals(WebpDirectory.CHUNK_VP8X)
64 || fourCC.equals(WebpDirectory.CHUNK_VP8L)
65 || fourCC.equals(WebpDirectory.CHUNK_VP8)
66 || fourCC.equals(WebpDirectory.CHUNK_EXIF)
67 || fourCC.equals(WebpDirectory.CHUNK_ICCP)
68 || fourCC.equals(WebpDirectory.CHUNK_XMP);
69 }
70
71 @Override
72 public boolean shouldAcceptList(@NotNull String fourCC)
73 {
74 return false;
75 }
76
77 public void processChunk(@NotNull String fourCC, @NotNull byte[] payload)
78 {
79 WebpDirectory directory = new WebpDirectory();
80 if (fourCC.equals(WebpDirectory.CHUNK_EXIF)) {
81 // We have seen WebP images with and without the preamble here. It's likely that some software incorrectly
82 // copied an entire JPEG segment into the WebP image. Regardless, we can handle it here.
83 ByteArrayReader reader = ExifReader.startsWithJpegExifPreamble(payload)
84 ? new ByteArrayReader(payload, ExifReader.JPEG_SEGMENT_PREAMBLE.length())
85 : new ByteArrayReader(payload);
86 new ExifReader().extract(reader, _metadata);
87 } else if (fourCC.equals(WebpDirectory.CHUNK_ICCP)) {
88 new IccReader().extract(new ByteArrayReader(payload), _metadata);
89 } else if (fourCC.equals(WebpDirectory.CHUNK_XMP)) {
90 new XmpReader().extract(payload, _metadata);
91 } else if (fourCC.equals(WebpDirectory.CHUNK_VP8X) && payload.length == 10) {
92 RandomAccessReader reader = new ByteArrayReader(payload);
93 reader.setMotorolaByteOrder(false);
94
95 try {
96 // Flags
97 // boolean hasFragments = reader.getBit(0);
98 boolean isAnimation = reader.getBit(1);
99 // boolean hasXmp = reader.getBit(2);
100 // boolean hasExif = reader.getBit(3);
101 boolean hasAlpha = reader.getBit(4);
102 // boolean hasIcc = reader.getBit(5);
103
104 // Image size
105 int widthMinusOne = reader.getInt24(4);
106 int heightMinusOne = reader.getInt24(7);
107
108 directory.setInt(WebpDirectory.TAG_IMAGE_WIDTH, widthMinusOne + 1);
109 directory.setInt(WebpDirectory.TAG_IMAGE_HEIGHT, heightMinusOne + 1);
110 directory.setBoolean(WebpDirectory.TAG_HAS_ALPHA, hasAlpha);
111 directory.setBoolean(WebpDirectory.TAG_IS_ANIMATION, isAnimation);
112
113 _metadata.addDirectory(directory);
114
115 } catch (IOException e) {
116 directory.addError(e.getMessage());
117 }
118 } else if (fourCC.equals(WebpDirectory.CHUNK_VP8L) && payload.length > 4) {
119 RandomAccessReader reader = new ByteArrayReader(payload);
120 reader.setMotorolaByteOrder(false);
121
122 try {
123 // https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification#2_riff_header
124
125 // Expect the signature byte
126 if (reader.getInt8(0) != 0x2F)
127 return;
128 int b1 = reader.getUInt8(1);
129 int b2 = reader.getUInt8(2);
130 int b3 = reader.getUInt8(3);
131 int b4 = reader.getUInt8(4);
132 // 14 bits for width
133 int widthMinusOne = (b2 & 0x3F) << 8 | b1;
134 // 14 bits for height
135 int heightMinusOne = (b4 & 0x0F) << 10 | b3 << 2 | (b2 & 0xC0) >> 6;
136
137 directory.setInt(WebpDirectory.TAG_IMAGE_WIDTH, widthMinusOne + 1);
138 directory.setInt(WebpDirectory.TAG_IMAGE_HEIGHT, heightMinusOne + 1);
139
140 _metadata.addDirectory(directory);
141
142 } catch (IOException e) {
143 directory.addError(e.getMessage());
144 }
145 } else if (fourCC.equals(WebpDirectory.CHUNK_VP8) && payload.length > 9) {
146 RandomAccessReader reader = new ByteArrayReader(payload);
147 reader.setMotorolaByteOrder(false);
148
149 try {
150 // https://tools.ietf.org/html/rfc6386#section-9.1
151 // https://github.com/webmproject/libwebp/blob/master/src/enc/syntax.c#L115
152
153 // Expect the signature bytes
154 if (reader.getUInt8(3) != 0x9D ||
155 reader.getUInt8(4) != 0x01 ||
156 reader.getUInt8(5) != 0x2A)
157 return;
158 int width = reader.getUInt16(6);
159 int height = reader.getUInt16(8);
160
161 directory.setInt(WebpDirectory.TAG_IMAGE_WIDTH, width);
162 directory.setInt(WebpDirectory.TAG_IMAGE_HEIGHT, height);
163
164 _metadata.addDirectory(directory);
165
166 } catch (IOException ex) {
167 directory.addError(ex.getMessage());
168 }
169 }
170 }
171
172 @Override
173 public void addError(@NotNull String message)
174 {
175 WebpDirectory directory = new WebpDirectory();
176 directory.addError(message);
177 _metadata.addDirectory(directory);
178 }
179 }