GifImageWriter.java
14.8 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Decompiled with CFR 0_118.
*/
package com.day.imageio.plugins;
import com.day.imageio.plugins.GIFImageMetadata;
import com.day.imageio.plugins.GIFStreamMetadata;
import javax.imageio.*;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.nio.ByteOrder;
class GifImageWriter
extends ImageWriter {
protected ImageOutputStream stream = null;
private boolean streamInitialized = false;
private static final int BLOCKLEN = 255;
private static final int BUFLEN = 1000;
int chainlen = 0;
int maxchainlen = 0;
int nodecount = 0;
int lookuptypes = 0;
int nbits;
long obits;
byte[] buffer;
private short need = 8;
GifTree root = new GifTree(76);
protected GifImageWriter(ImageWriterSpi originatingProvider) {
super(originatingProvider);
}
public void setOutput(Object output) {
super.setOutput(output);
try {
this.stream = (ImageOutputStream)output;
}
catch (ClassCastException cce) {
throw new IllegalArgumentException("output not ImageOutputStream");
}
this.streamInitialized = false;
}
public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) {
return new GIFStreamMetadata();
}
public IIOMetadata convertStreamMetadata(IIOMetadata inData, ImageWriteParam param) {
if (inData == null) {
throw new IllegalArgumentException("inData must not be null");
}
if (inData instanceof GIFStreamMetadata) {
return inData;
}
return null;
}
public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType, ImageWriteParam param) {
return new GIFImageMetadata();
}
public IIOMetadata convertImageMetadata(IIOMetadata inData, ImageTypeSpecifier imageType, ImageWriteParam param) {
if (inData == null) {
throw new IllegalArgumentException("inData must not be null");
}
if (inData instanceof GIFImageMetadata) {
return inData;
}
return null;
}
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IIOException {
IndexColorModel icm;
boolean useGlobalColorTable;
if (this.stream == null) {
throw new IllegalStateException("output not yet set");
}
if (image == null) {
throw new IllegalArgumentException("image must not be null");
}
RenderedImage rim = image.getRenderedImage();
if (rim == null) {
throw new UnsupportedOperationException("Image not a RenderedImage");
}
IIOMetadata imd = image.getMetadata();
GIFImageMetadata metadata = null;
if (imd != null) {
metadata = (GIFImageMetadata)this.convertImageMetadata(imd, null, null);
}
if (metadata == null) {
metadata = new GIFImageMetadata();
}
GIFStreamMetadata gifMetaData = streamMetadata == null ? new GIFStreamMetadata() : (GIFStreamMetadata)streamMetadata;
ColorModel cm = rim.getColorModel();
if (cm instanceof IndexColorModel) {
icm = (IndexColorModel)cm;
if (icm.getMapSize() > 256) {
throw new UnsupportedOperationException("Number of colors > 256");
}
} else {
throw new UnsupportedOperationException("Image must have IndexColorModel");
}
int colTabLen = icm.getMapSize();
int depth = colTabLen <= 2 ? 1 : (colTabLen <= 4 ? 2 : (colTabLen <= 8 ? 3 : (colTabLen <= 16 ? 4 : (colTabLen <= 32 ? 5 : (colTabLen <= 64 ? 6 : (colTabLen <= 128 ? 7 : 8))))));
int mapSize = 1 << depth;
int[] rgba = new int[colTabLen];
byte[] gifColTab = new byte[mapSize * 3];
icm.getRGBs(rgba);
int j = 0;
for (int i = 0; i < colTabLen; ++i) {
int col = rgba[i];
gifColTab[j++] = (byte)(col >> 16);
gifColTab[j++] = (byte)(col >> 8);
gifColTab[j++] = (byte)col;
}
boolean bl = useGlobalColorTable = !this.streamInitialized;
if (useGlobalColorTable) {
gifMetaData.globalColorTable = gifColTab;
metadata.localColorTable = null;
} else {
metadata.localColorTable = gifColTab;
}
try {
if (!this.streamInitialized) {
this.startGifStream((GIFStreamMetadata)streamMetadata);
this.streamInitialized = true;
}
this.writeGifImageMetaData(metadata);
this.writeCompressedImage(rim.getData(), depth);
this.stream.flush();
}
catch (IOException ioe) {
throw new IIOException(ioe.getMessage(), ioe);
}
}
protected void setByteOrder() {
this.stream.setByteOrder(ByteOrder.LITTLE_ENDIAN);
}
private void startGifStream(GIFStreamMetadata streamMetadata) throws IOException {
this.setByteOrder();
this.stream.write("GIF89a".getBytes());
this.stream.writeShort(streamMetadata.logicalScreenWidth);
this.stream.writeShort(streamMetadata.logicalScreenHeight);
byte packed = (byte)((streamMetadata.globalColorTable != null ? 1 : 0) << 7);
packed = (byte)(packed | 112);
if (streamMetadata.globalColorTable != null) {
int i = -2;
for (int nc = streamMetadata.globalColorTable.length / 3; nc > 0; nc >>= 1) {
++i;
}
packed = (byte)(packed | i);
}
this.stream.write(packed);
this.stream.write(streamMetadata.backgroundColorIndex);
this.stream.write(0);
if (streamMetadata.globalColorTable != null) {
this.stream.write(streamMetadata.globalColorTable);
}
if (streamMetadata.extensions != null) {
for (int i = 0; i < streamMetadata.extensions.length; ++i) {
GIFStreamMetadata.ApplicationExtension ext = streamMetadata.extensions[i];
this.stream.write(33);
this.stream.write(255);
byte[] block11 = "\u000b ".getBytes();
int idLen = Math.min(ext.identifier.length, 8);
System.arraycopy(ext.identifier, 0, block11, 1, idLen);
idLen = Math.min(ext.authCode.length, 3);
System.arraycopy(ext.authCode, 0, block11, 9, idLen);
this.stream.write(block11);
for (int j = 0; j < ext.subBlocks.length; ++j) {
this.stream.write(ext.subBlocks[j].length);
this.stream.write(ext.subBlocks[j]);
}
this.stream.write(0);
}
}
}
public void reset() {
this.streamInitialized = false;
super.reset();
}
public void dispose() {
if (this.stream != null) {
try {
this.stream.write(59);
}
catch (IOException ioe) {
// empty catch block
}
this.streamInitialized = false;
}
super.dispose();
}
private void writeGifImageMetaData(GIFImageMetadata metadata) throws IOException {
this.stream.write(33);
this.stream.write(249);
this.stream.write(4);
int packed = 0;
packed = (byte)(packed | (metadata.disposalMethod & 7) << 2);
packed = (byte)(packed | (metadata.transparentColorFlag ? 1 : 0));
this.stream.write(packed);
this.stream.writeShort(metadata.delayTime);
this.stream.write(metadata.transparentColorIndex);
this.stream.write(0);
this.stream.write(44);
this.stream.writeShort(metadata.imageLeftPosition);
this.stream.writeShort(metadata.imageTopPosition);
this.stream.writeShort(metadata.imageWidth);
this.stream.writeShort(metadata.imageHeight);
packed = (byte)((metadata.localColorTable != null ? 1 : 0) << 7);
if (metadata.localColorTable != null) {
int i = -2;
for (int nc = metadata.localColorTable.length / 3; nc > 0; nc >>= 1) {
++i;
}
packed = (byte)(packed | i);
}
this.stream.write(packed);
if (metadata.localColorTable != null) {
this.stream.write(metadata.localColorTable);
}
}
private void writeCompressedImage(Raster data, int depth) throws IOException {
int w = data.getWidth();
int h = data.getHeight();
int[] chunk = new int[w];
GifTree first = this.root;
this.buffer = new byte[1000];
int pos = 0;
int cc = depth == 1 ? 4 : 1 << depth;
int cLength = depth == 1 ? 3 : depth + 1;
int eoi = cc + 1;
int next = cc + 2;
this.stream.write(cLength - 1);
this.clearTree(cc, first);
pos = this.addCodeToBuffer(cc, cLength, pos);
GifTree curNode = first;
for (int y = 0; y < h; ++y) {
data.getSamples(0, y, w, 1, 0, chunk);
int x = 0;
while (x < w) {
GifTree newNode;
int curPix = chunk[x];
if (curNode.node != null && curNode.node[curPix] != null) {
curNode = curNode.node[curPix];
++this.chainlen;
++x;
continue;
}
if (curNode.type == 83) {
newNode = curNode.nxt;
while (newNode.alt != null && newNode.idx != curPix) {
newNode = newNode.alt;
}
if (newNode.idx == curPix) {
++this.chainlen;
curNode = newNode;
++x;
continue;
}
}
newNode = new GifTree(84, next, curPix);
switch (curNode.type) {
case 76: {
curNode.node[curPix] = newNode;
break;
}
case 83: {
curNode.node = new GifTree[256];
curNode.type = 76;
curNode.node[curPix] = newNode;
curNode.node[curNode.nxt.idx] = curNode.nxt;
++this.lookuptypes;
curNode.nxt = null;
break;
}
case 84: {
newNode.alt = curNode.nxt;
newNode.nxt = null;
curNode.nxt = newNode;
curNode.type = 83;
break;
}
}
++this.nodecount;
pos = this.addCodeToBuffer(curNode.code, cLength, pos);
if (this.chainlen > this.maxchainlen) {
this.maxchainlen = this.chainlen;
}
this.chainlen = 0;
if (pos >= 255) {
this.stream.write(255);
this.stream.write(this.buffer, 0, 255);
this.buffer[0] = this.buffer[255];
this.buffer[1] = this.buffer[256];
this.buffer[2] = this.buffer[257];
this.buffer[3] = this.buffer[258];
pos -= 255;
}
curNode = first;
if (next == 1 << cLength) {
++cLength;
}
if (++next != 4095) continue;
this.clearTree(cc, first);
pos = this.addCodeToBuffer(cc, cLength, pos);
if (pos >= 255) {
this.stream.write(255);
this.stream.write(this.buffer, 0, 255);
this.buffer[0] = this.buffer[255];
this.buffer[1] = this.buffer[256];
this.buffer[2] = this.buffer[257];
this.buffer[3] = this.buffer[258];
pos -= 255;
}
next = cc + 2;
cLength = depth == 1 ? 3 : depth + 1;
}
}
if ((pos = this.addCodeToBuffer(curNode.code, cLength, pos)) >= 252) {
this.stream.write(252);
this.stream.write(this.buffer, 0, 252);
this.buffer[0] = this.buffer[252];
this.buffer[1] = this.buffer[253];
this.buffer[2] = this.buffer[254];
this.buffer[3] = this.buffer[255];
this.buffer[4] = this.buffer[256];
pos -= 252;
}
pos = this.addCodeToBuffer(eoi, cLength, pos);
pos = this.addCodeToBuffer(0, -1, pos);
this.stream.write(pos);
this.stream.write(this.buffer, 0, pos);
this.stream.write(0);
}
private void clearTree(int cc, GifTree root) {
int i;
this.maxchainlen = 0;
this.lookuptypes = 1;
this.nodecount = cc;
if (root.node == null) {
root.node = new GifTree[256];
} else {
for (i = cc; i < root.node.length; ++i) {
root.node[i] = null;
}
}
for (i = 0; i < cc; ++i) {
root.node[i] = new GifTree(84, i, i);
}
}
private int addCodeToBuffer(int code, int n, int pos) {
int mask;
if (n < 0) {
if (this.need < 8) {
this.buffer[++pos] = 0;
}
this.need = 8;
return pos;
}
while (n >= this.need) {
mask = (1 << this.need) - 1;
byte[] arrby = this.buffer;
int n2 = pos++;
arrby[n2] = (byte)(arrby[n2] + ((mask & code) << 8 - this.need));
this.buffer[pos] = 0;
code >>= this.need;
n -= this.need;
this.need = 8;
}
if (n != 0) {
mask = (1 << n) - 1;
byte[] arrby = this.buffer;
int n3 = pos;
arrby[n3] = (byte)(arrby[n3] + ((mask & code) << 8 - this.need));
this.need = (short)(this.need - n);
}
return pos;
}
private static final class GifTree {
static final byte TERMIN = 84;
static final byte LOOKUP = 76;
static final byte SEARCH = 83;
byte type;
int code;
int idx;
GifTree[] node;
GifTree nxt;
GifTree alt;
GifTree(byte type) {
this.type = type;
}
GifTree(byte type, int code, int idx) {
this.type = type;
this.code = code;
this.idx = idx;
}
GifTree() {
}
}
}