-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWebpContainerWriter.java
More file actions
167 lines (135 loc) · 4.04 KB
/
WebpContainerWriter.java
File metadata and controls
167 lines (135 loc) · 4.04 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
package com.n4no.webpencoder.webp.muxer;
import android.util.Log;
import com.n4no.webpencoder.webp.muxer.stream.SeekableOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.BitSet;
/**
* @author Bartlomiej Tadych, b4rtaz
*/
public class WebpContainerWriter {
private final SeekableOutputStream _outputStream;
private int _offset;
public WebpContainerWriter(SeekableOutputStream outputStream) {
_outputStream = outputStream;
}
public void writeHeader() throws IOException {
write(new byte[] { 'R', 'I', 'F', 'F' });
writeUInt32(0);
write(new byte[] { 'W', 'E', 'B', 'P' });
}
public void close() throws IOException {
int fileSize = _offset - 8;
_outputStream.setPosition(4);
writeUInt32(fileSize);
}
public void write(WebpChunk chunk) throws IOException {
System.out.println(chunk.type.toString());
switch (chunk.type) {
case VP8:
writePayloadChunk(chunk, new byte[] { 'V', 'P', '8', ' ' });
break;
case VP8L:
writePayloadChunk(chunk, new byte[] { 'V', 'P', '8', 'L' });
break;
case VP8X:
writeVp8x(chunk);
break;
case ANIM:
writeAnim(chunk);
break;
case ANMF:
writeAnmf(chunk);
break;
default:
throw new IOException("Not supported chunk type.");
}
}
private void writePayloadChunk(WebpChunk chunk, byte[] fourCc) throws IOException {
write(fourCc, 4);
writeUInt32(chunk.payload.length);
write(chunk.payload);
}
private void writeVp8x(WebpChunk chunk) throws IOException {
write(new byte[] { 'V', 'P', '8', 'X' });
writeUInt32(10);
BitSet bs = new BitSet(32);
bs.set(0, chunk.hasIccp);
bs.set(4, chunk.hasAlpha);
bs.set(2, chunk.hasExif);
bs.set(3, chunk.hasXmp);
bs.set(1, chunk.hasAnim);
write(bitSetToBytes(bs, 4));
writeUInt24(chunk.width);
writeUInt24(chunk.height);
}
private void writeAnim(WebpChunk chunk) throws IOException {
write(new byte[] { 'A', 'N', 'I', 'M' });
writeUInt32(6);
writeUInt32(chunk.background);
writeUInt16(chunk.loops);
}
private void writeAnmf(WebpChunk chunk) throws IOException {
write(new byte[] { 'A', 'N', 'M', 'F' });
// if ALPH chunk present, get size
int AlphaSize = 0;
if(chunk.alphaData != null){
AlphaSize = 4 + 4 + chunk.alphaData.length;
// 4 bytes (ALPH header) + 4 bytes (chunk size) + length of Alpha BitStream
}
writeUInt32(chunk.payload.length + 24 + AlphaSize);
writeUInt24(chunk.x); // 3 bytes (3)
writeUInt24(chunk.y); // 3 bytes (6)
writeUInt24(chunk.width); // 3 bytes (9)
writeUInt24(chunk.height); // 3 bytes (12)
writeUInt24(chunk.duration); // 3 bytes (15)
BitSet bs = new BitSet(8);
bs.set(1, chunk.useAlphaBlending);
bs.set(0, chunk.disposeToBackgroundColor);
write(bitSetToBytes(bs, 1)); // 1 byte (16)
// Insert ALPH chunk
if(chunk.alphaData != null){
writeAlph(chunk.alphaData);
}
if (chunk.isLossless)
write(new byte[] { 'V', 'P', '8', 'L' }); // 4 bytes (20)
else
write(new byte[] { 'V', 'P', '8', ' ' });
writeUInt32(chunk.payload.length); // 4 bytes (24)
write(chunk.payload);
}
private void writeAlph(byte[] alphaData) throws IOException {
write(new byte[] { 'A', 'L', 'P', 'H' }); // 4
writeUInt32(alphaData.length); // 4
write(alphaData); // x
}
//
private void write(byte[] bytes) throws IOException {
write(bytes, bytes.length);
}
private void write(byte[] bytes, int length) throws IOException {
_outputStream.write(bytes, length);
_offset += length;
}
private void writeUInt(int value, int bytes) throws IOException {
byte[] b = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(value).array();
write(b, bytes);
}
private void writeUInt16(int value) throws IOException {
writeUInt(value, 2);
}
private void writeUInt24(int value) throws IOException {
writeUInt(value, 3);
}
private void writeUInt32(int value) throws IOException {
writeUInt(value, 4);
}
private byte[] bitSetToBytes(BitSet bs, int bytes) {
byte[] b = new byte[bytes];
byte[] a = bs.toByteArray();
for (int i = 0; i < a.length; i++)
b[i] = a[i];
return b;
}
}