-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathGifImageDecoder.java
More file actions
588 lines (500 loc) · 18.6 KB
/
Copy pathGifImageDecoder.java
File metadata and controls
588 lines (500 loc) · 18.6 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package com.hss01248.retrofitdemo.gif;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Created by Administrator on 2016/8/8 0008.
*/
public class GifImageDecoder {
private static final String TAG = GifImageDecoder.class.getSimpleName();
private final GifImageDecoder self = this;
// File read status: No errors.
public static final int STATUS_OK = 0;
// File read status: Error decoding file (may be partially decoded)
public static final int STATUS_FORMAT_ERROR = 1;
// File read status: Unable to open source.
public static final int STATUS_OPEN_ERROR = 2;
// Trailer
private static final byte TRR_CODE = (byte) 0x3B;
// Image Block
private static final byte IMG_CODE = (byte) 0x2C;
// Extension
private static final byte EXT_CODE = (byte) 0x21;
// Graphic Control Extension
private static final byte GC_EXT = (byte) 0xF9;
// Application Extension
private static final byte APP_EXT = (byte) 0xFF;
// Comment Extension
private static final byte CMT_EXT = (byte) 0xFE;
// Plain Text Extension
private static final byte TXT_EXT = (byte) 0x01;
private static final int MIN_DELAY = 100;
private static final int MIN_DELAY_ENFORCE_THRESHOLD = 20;
protected int mStatus;
protected int mWidth; // full mCurrentImage mWidth
protected int mHeight; // full mCurrentImage mHeight
protected Bitmap mCurrentImage; // current frame
protected Bitmap mLastImage; // previous frame
protected int mDispose = 0; // 0=no action; 1=leave in place; 2=restore to bg; 3=restore to prev
protected int mLastDispose = 0;
protected int mDelay = 0; // mDelay in milliseconds
protected ArrayList<GifFrame> mGifFrames; // mGifFrames read from current file
protected int mFrameCount;
private int mOffset = 0;
private GifHeader mGifHeader;
private GraphicControlExtension mGcExt;
private ImageBlock mImageBlock;
//新添加的
private int targetWidth;
private int targetHeight;
public void setResization(int targetWidth,int targetHeight){
this.targetWidth = targetWidth;
this.targetHeight = targetHeight;
}
private static class GifFrame {
public GifFrame(Bitmap im, int del) {
image = im;
delay = del;
}
public Bitmap image;
public int delay;
}
/**
* Gets display duration for specified frame.
*
* @param n int index of frame
* @return delay in milliseconds
*/
public int getDelay(int n) {
mDelay = -1;
if ((n >= 0) && (n < mFrameCount)) {
mDelay = mGifFrames.get(n).delay;
if (mDelay < MIN_DELAY_ENFORCE_THRESHOLD) {
mDelay = MIN_DELAY;
}
}
return mDelay;
}
/**
* Gets the number of GifFrames read from file.
*
* @return frame count
*/
public int getFrameCount() {
return mFrameCount;
}
/**
* Gets the first (or only) image read.
*
* @return BufferedBitmap containing first frame, or null if none.
*/
public Bitmap getBitmap() {
return getFrame(0);
}
/**
* Gets the image contents of frame n.
*
* @return BufferedBitmap representation of frame, or null if n is invalid.
*/
public Bitmap getFrame(int n) {
if (mFrameCount <= 0)
return null;
n = n % mFrameCount;
return (mGifFrames.get(n)).image;
}
/**
* Reads GIF image from stream
*
* @param is containing GIF file.
* @return read status code (0 = no errors)
*/
public int read(InputStream is) throws IOException {
init();
if (is != null) {
byte[] buffer = GifUtils.streamToBytes(is);
mGifHeader = new GifHeader(buffer, mOffset);
mOffset += mGifHeader.size;
mWidth = mGifHeader.getWidth();
mHeight = mGifHeader.getHeight();
if (!mGifHeader.getSignature().equals("GIF")) {
return STATUS_FORMAT_ERROR;
}
while (buffer[mOffset] != TRR_CODE) {
if (buffer[mOffset] == IMG_CODE) {
// ImageBlock
mImageBlock = new ImageBlock(buffer, mOffset);
mOffset += mImageBlock.size;
mFrameCount++;
// create new image to receive frame data
mCurrentImage = extractImage();
if (mLastDispose > 0) {
if (mLastDispose == 3) {
// use image before last
int n = mFrameCount - 2;
if (n > 0) {
mLastImage = getFrame(n - 1);
} else {
mLastImage = null;
}
}
}
mGifFrames.add(new GifFrame(mCurrentImage, mDelay)); // add image to frame
resetFrame();
} else if (buffer[mOffset] == EXT_CODE) {
if (buffer[mOffset + 1] == GC_EXT) {
//GraphicControlExtension
mGcExt = new GraphicControlExtension(buffer, mOffset);
mOffset += mGcExt.size;
mDispose = mGcExt.getDisposalMothod(); // disposal method
if (mDispose == 0) {
mDispose = 1; // elect to keep old image if discretionary
}
mDelay = mGcExt.getDelayTime() * 10; // delay in milliseconds
} else if (buffer[mOffset + 1] == APP_EXT) {
//ApplicationExtension
ApplicationExtension appExt = new ApplicationExtension(buffer, mOffset);
mOffset += appExt.size;
} else if (buffer[mOffset + 1] == CMT_EXT) {
//CommentExtension
CommentExtension cmtExt = new CommentExtension(buffer, mOffset);
mOffset += cmtExt.size;
} else if (buffer[mOffset + 1] == TXT_EXT) {
//PlainTextExtension
PlainTextExtension txtExt = new PlainTextExtension(buffer, mOffset);
mOffset += txtExt.size;
} else {
throw new IOException();
}
} else {
throw new IOException();
}
}
} else {
mStatus = STATUS_OPEN_ERROR;
}
return mStatus;
}
/**
* Initializes or re-initializes reader
*/
protected void init() {
mStatus = STATUS_OK;
mFrameCount = 0;
mGifFrames = new ArrayList<GifFrame>();
}
/**
* Resets frame state for reading next image.
*/
protected void resetFrame() {
mLastDispose = mDispose;
mLastImage = mCurrentImage;
mDispose = 0;
mDelay = 0;
}
/**
* Extract new image
*
* @return image
*/
@SuppressLint("NewApi") private Bitmap extractImage() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
outputStream.write(mGifHeader.bytes);
if (mGcExt != null) {
if ((mWidth != mImageBlock.getImageWidth() || mHeight != mImageBlock.getImageHeight()) &&
mGcExt.getTransparentColorFlag() == 0) {
mGcExt.setTransparentColorFlagTrue();
}
outputStream.write(mGcExt.bytes);
}
outputStream.write(mImageBlock.bytes);
outputStream.write((byte) 0x3B);
outputStream.flush();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
options.inPreferredConfig = Bitmap.Config.RGB_565;
//计算insamplesize:
int inSampleSize = 1;
//先根据宽度进行缩小
while (mWidth / inSampleSize > targetWidth) {
inSampleSize++;
}
//然后根据高度进行缩小
while (mHeight / inSampleSize > targetHeight) {
inSampleSize++;
}
if (inSampleSize <= 0) {
inSampleSize = 1;
}
options.inSampleSize = inSampleSize;
Bitmap newBitmap = BitmapFactory.decodeStream(new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray())),null,options);//加上图片缩略
if (newBitmap != null) {
if (mLastImage == null) {
return newBitmap;
} else {
Bitmap bitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(mLastImage, 0, 0, null);
canvas.drawBitmap(newBitmap, 0, 0, null);
return bitmap;
}
} else {
if (mLastImage != null) {
return mLastImage;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private class GifHeader {
public byte[] bytes;
public int size;
public GifHeader(byte[] bytes, int offset) {
boolean globalColorTableFlag = (bytes[offset + 0x0A] & 0x80) != 0x00;
int globalColorTableSize = (bytes[offset + 0x0A] & 0x07);
// get size
size = 0x0D;
if (globalColorTableFlag) {
size += Math.pow(2, (globalColorTableSize + 1)) * 3;
}
this.bytes = new byte[size];
System.arraycopy(bytes, offset, this.bytes, 0, size);
}
public String getSignature() {
return new String(bytes, 0, 3);
}
public String getVersion() {
return new String(bytes, 3, 3);
}
public int getWidth() {
return (bytes[6] & 0xFF) + ((bytes[7] & 0xFF) << 8);
}
public int getHeight() {
return (bytes[8] & 0xFF) + ((bytes[9] & 0xFF) << 8);
}
public int getGlobalColorTableFlag() {
return (bytes[10] & 0x80) >> 7;
}
public int getColorResolution() {
return (bytes[10] & 0x70) >> 4;
}
public int getSortFlag() {
return (bytes[10] & 0x08) >> 3;
}
public int getSizeOfGlobalColorTable() {
return (bytes[10] & 0x07);
}
public int getBackgroundColorIndex() {
return bytes[11] & 0xFF;
}
public int getPixelAspectRatio() {
return bytes[12];
}
public int[] getGlobalColorTable() {
if (getGlobalColorTableFlag() == 0) {
return new int[0];
}
int[] colors = new int[(int) Math.pow(2, getSizeOfGlobalColorTable() + 1)];
for (int i = 0; i < colors.length; i++) {
colors[i] = ((bytes[13 + (i * 3)] & 0xFF) << 16) + ((bytes[13 + (i * 3) + 1] & 0xFF) << 8) + (bytes[13 + (i * 3) + 2] & 0xFF);
}
return colors;
}
}
private class ImageBlock {
public byte[] bytes;
public int size;
public ImageBlock(byte[] bytes, int offset) {
int blockSize;
boolean localColorTableFlag = (bytes[offset + 0x09] & 0x80) != 0x00;
int localColorTableSize = (bytes[offset + 0x09] & 0x07);
//get size
size = 0x0A;
if (localColorTableFlag) {
size += Math.pow(2, (localColorTableSize + 1)) * 3;
}
size += 1; //LZW Minimum Code Size
//ImageData
blockSize = bytes[offset + size] & 0xFF;
size += 1;
while (blockSize != 0x00) {
size += blockSize;
blockSize = bytes[offset + size] & 0xFF;
size += 1;
}
this.bytes = new byte[size];
System.arraycopy(bytes, offset, this.bytes, 0, size);
}
public int getImageSeparator() {
return bytes[0] & 0xFF;
}
public int ImageLeftPosition() {
return (bytes[1] & 0xFF) + ((bytes[2] & 0xFF) << 8);
}
public int getImageTopPosition() {
return (bytes[3] & 0xFF) + ((bytes[4] & 0xFF) << 8);
}
public int getImageWidth() {
return (bytes[5] & 0xFF) + ((bytes[6] & 0xFF) << 8);
}
public int getImageHeight() {
return (bytes[7] & 0xFF) + ((bytes[8] & 0xFF) << 8);
}
public int getLocalColorTableFlag() {
return (bytes[9] & 0x80) >> 7;
}
public int getInterlaceFlag() {
return (bytes[9] & 0x40) >> 6;
}
public int getSortFlag() {
return (bytes[9] & 0x20) >> 5;
}
public int getReserved() {
return (bytes[9] & 0x18) >> 2;
}
public int getSizeOfLocalColorTable() {
return bytes[9] & 0x03;
}
public int[] getLocalColorTable() {
if (getLocalColorTableFlag() == 0) {
return new int[0];
}
int[] colors = new int[(int) Math.pow(2, getSizeOfLocalColorTable() + 1)];
for (int i = 0; i < colors.length; i++) {
colors[i] = ((bytes[10 + (i * 3)] & 0xFF) << 16) + ((bytes[10 + (i * 3) + 1] & 0xFF) << 8) + (bytes[10 + (i * 3) + 2] & 0xFF);
}
return colors;
}
public int getLZWMinimumCodeSize() {
if (getLocalColorTableFlag() == 0) {
return bytes[10] & 0xFF;
} else {
return bytes[10 + (int) Math.pow(2, getSizeOfLocalColorTable() + 1) * 3] & 0xFF;
}
}
}
private class ApplicationExtension {
public byte[] bytes;
public int size;
public ApplicationExtension(byte[] bytes, int offset) {
int blockSize;
// get size
size = 0x0E;
blockSize = bytes[offset + size] & 0xFF;
size += 1;
while (blockSize != 0x00) {
size += blockSize;
blockSize = bytes[offset + size] & 0xFF;
size += 1;
}
this.bytes = new byte[size];
System.arraycopy(bytes, offset, this.bytes, 0, size);
}
public int getExtensionIntroducer() {
return bytes[0] & 0xFF;
}
public int getExtensionLabel() {
return bytes[1] & 0xFF;
}
public int getBlockSize1() {
return bytes[2] & 0xFF;
}
public String getApplicationIdentifier() {
return new String(bytes, 3, 8);
}
public String getApplicationAuthenticationCode() {
return new String(bytes, 11, 3);
}
}
private class GraphicControlExtension {
public byte[] bytes;
public int size;
public GraphicControlExtension(byte[] bytes, int offset) {
size = 8;
this.bytes = new byte[size];
System.arraycopy(bytes, offset, this.bytes, 0, size);
}
public int getExtensionIntroducer() {
return bytes[0] & 0xFF;
}
public int getGraphicControlLabel() {
return bytes[1] & 0xFF;
}
public int getBlockSize() {
return bytes[2] & 0xFF;
}
public int getReserved() {
return (bytes[3] & 0xE0) >> 5;
}
public int getDisposalMothod() {
return (bytes[3] & 0x1C) >> 2;
}
public int getUserInputFlag() {
return (bytes[3] & 0x02) >> 1;
}
public int getTransparentColorFlag() {
return (bytes[3] & 0x01);
}
public int getDelayTime() {
return (bytes[4] & 0xFF) + ((bytes[5] & 0xFF) << 8);
}
public int getTransparentColorIndex() {
return bytes[6];
}
public void setTransparentColorFlagTrue() {
int value = getReserved() | getDisposalMothod() | getUserInputFlag() | 0x01;
bytes[3] = (byte) Integer.parseInt(GifUtils.toHex(value, 2), 16);
}
}
private class CommentExtension {
public byte[] bytes;
public int size;
public CommentExtension(byte[] bytes, int offset) {
int blockSize;
// get size
size = 0x02;
blockSize = bytes[offset + size] & 0xFF;
size += 1;
while (blockSize != 0x00) {
size += blockSize;
blockSize = bytes[offset + size] & 0xFF;
size += 1;
}
this.bytes = new byte[size];
System.arraycopy(bytes, offset, this.bytes, 0, size);
}
}
private class PlainTextExtension {
public byte[] bytes;
public int size;
public PlainTextExtension(byte[] bytes, int offset) {
int blockSize;
// get size
size = 0x0F;
blockSize = bytes[offset + size] & 0xFF;
size += 1;
while (blockSize != 0x00) {
size += blockSize;
blockSize = bytes[offset + size] & 0xFF;
size += 1;
}
this.bytes = new byte[size];
System.arraycopy(bytes, offset, this.bytes, 0, size);
}
}
}