-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWebpContainerReader.java
More file actions
240 lines (192 loc) · 6.3 KB
/
WebpContainerReader.java
File metadata and controls
240 lines (192 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.n4no.webpencoder.webp.muxer;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.BitSet;
/**
* @author Bartlomiej Tadych, b4rtaz
*/
public class WebpContainerReader {
private final InputStream _inputStream;
private final boolean _debug;
private int _fileSize;
private int _offset;
public WebpContainerReader(InputStream inputStream, boolean debug) {
_inputStream = inputStream;
_debug = debug;
}
public void close() throws IOException {
}
public void readHeader() throws IOException {
byte[] fcc = new byte[4];
read(fcc, 4);
if (!isFourCc(fcc, 'R', 'I', 'F', 'F'))
throw new IOException("Expected RIFF file.");
_fileSize = readUInt32() + 8 - 1;
read(fcc, 4);
if (!isFourCc(fcc, 'W', 'E', 'B', 'P'))
throw new IOException("Expected Webp file.");
}
public WebpChunk read() throws IOException {
byte[] fcc = new byte[4];
if (read(fcc, 4) > 0) {
if (isFourCc(fcc, 'V', 'P', '8', ' '))
return readVp8();
if (isFourCc(fcc, 'V', 'P', '8', 'L'))
return readVp8l();
if (isFourCc(fcc, 'V', 'P', '8', 'X'))
return readVp8x();
if (isFourCc(fcc, 'A', 'N', 'I', 'M'))
return readAnim();
if (isFourCc(fcc, 'A', 'N', 'M', 'F'))
return readAnmf();
if (isFourCc(fcc, 'I', 'C', 'C', 'P'))
return readIccp();
if (isFourCc(fcc, 'A', 'L', 'P', 'H'))
return readAlph();
throw new IOException(String.format("Not supported FourCC: %c.%c.%c.%c.",
fcc[0], fcc[1], fcc[2], fcc[3]));
}
if (_fileSize != _offset)
throw new IOException(String.format("Header has wrong file size: %d, expected: %d",
_fileSize, _offset));
return null;
}
private WebpChunk readAlph() throws IOException {
int chunkSize = readUInt32();
WebpChunk chunk = new WebpChunk(WebpChunkType.ALPH);
chunk.alphaData = readPayload(chunkSize);
chunk.hasAlpha = true;
return chunk;
}
private WebpChunk readIccp() throws IOException {
int chunkSize = readUInt32();
WebpChunk chunk = new WebpChunk(WebpChunkType.ICCP);
readPayload(chunkSize);
// no need to store the payload to this chunk as Animated Webp does not require ICCP
return chunk;
}
private WebpChunk readVp8x() throws IOException {
int chunkSize = readUInt32();
if (chunkSize != 10)
throw new IOException("Expected 10 bytes for VP8X.");
WebpChunk chunk = new WebpChunk(WebpChunkType.VP8X);
byte[] flags = new byte[4];
read(flags, 4);
BitSet bs = BitSet.valueOf(flags);
bs.get(0); // R reserved
chunk.hasAnim = bs.get(1); // A Animation
chunk.hasXmp = bs.get(2); // X XMP
chunk.hasExif = bs.get(3); // E Exif
chunk.hasAlpha = bs.get(4); // L Alpha
chunk.hasIccp = bs.get(5); // I ICCP
chunk.width = readUInt24();
chunk.height = readUInt24();
debug(String.format("VP8X: size = %dx%d", chunk.width, chunk.height));
return chunk;
}
private byte[] readPayload(int bytes) throws IOException {
byte[] payload = new byte[bytes];
if (read(payload, bytes) != bytes)
throw new IOException("Can not read all bytes.");
return payload;
}
private WebpChunk readVp8() throws IOException {
int chunkSize = readUInt32();
WebpChunk chunk = new WebpChunk(WebpChunkType.VP8);
chunk.isLossless = false;
chunk.payload = readPayload(chunkSize);
debug(String.format("VP8: bytes = %d", chunkSize));
return chunk;
}
private WebpChunk readVp8l() throws IOException {
int chunkSize = readUInt32();
WebpChunk chunk = new WebpChunk(WebpChunkType.VP8L);
chunk.isLossless = true;
// chunkSize is not telling the correct size of payload to read.
// chunk.payload = readPayload(chunkSize);
// So reading all bytes remained (Max 1024 bytes)
chunk.payload = readAllBytes();
debug(String.format("VP8L: bytes = %d", chunkSize));
return chunk;
}
private WebpChunk readAnim() throws IOException {
int chunkSize = readUInt32();
if (chunkSize != 6)
throw new IOException("Expected 6 bytes for ANIM.");
WebpChunk chunk = new WebpChunk(WebpChunkType.ANIM);
chunk.background = readUInt32();
chunk.loops = readUInt16();
debug(String.format("ANIM: loops = %d", chunk.loops));
return chunk;
}
private WebpChunk readAnmf() throws IOException {
int chunkSize = readUInt32();
WebpChunk chunk = new WebpChunk(WebpChunkType.ANMF);
chunk.x = readUInt24();
chunk.y = readUInt24();
chunk.width = readUInt24();
chunk.height = readUInt24();
chunk.duration = readUInt24();
byte[] flags = new byte[1];
read(flags, 1);
BitSet bs = BitSet.valueOf(flags);
chunk.useAlphaBlending = bs.get(1);
chunk.disposeToBackgroundColor = bs.get(0);
byte[] cch = new byte[4];
read(cch, 4);
if (isFourCc(cch, 'V', 'P', '8', 'L'))
chunk.isLossless = true;
else if (isFourCc(cch, 'V', 'P', '8', ' '))
chunk.isLossless = false;
else
throw new IOException("Not supported ANMF payload.");
readUInt32(); // Payload size.
int payloadSize = chunkSize - 24;
chunk.payload = readPayload(payloadSize);
debug(String.format("ANMF: size = %dx%d, offset = %dx%d, duration = %d, bytes = %d",
chunk.width, chunk.height, chunk.x, chunk.y, chunk.duration, payloadSize));
return chunk;
}
//
private final int read(byte[] buffer, int bytes) throws IOException {
int count = _inputStream.read(buffer, 0, bytes);
_offset += count;
return count;
}
private final int readUint(int bytes) throws IOException {
byte[] b = new byte[] { 0, 0, 0, 0 };
read(b, bytes);
return ByteBuffer.wrap(b, 0, 4).order(ByteOrder.LITTLE_ENDIAN).getInt();
}
private final int readUInt32() throws IOException {
return readUint(4);
}
private final int readUInt24() throws IOException {
return readUint(3);
}
private final int readUInt16() throws IOException {
return readUint(2);
}
private boolean isFourCc(byte[] h, char a, char b, char c, char d) {
return h[0] == a && h[1] == b && h[2] == c && h[3] == d;
}
private byte[] readAllBytes() throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int numRead;
while ((numRead = _inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, numRead);
_offset += numRead;
}
return outputStream.toByteArray();
}
}
private void debug(String message) {
if (_debug)
System.out.println(message);
}
}