/
DefaultImageHeaderParser.java
493 lines (432 loc) · 15.8 KB
/
DefaultImageHeaderParser.java
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
package com.bumptech.glide.load.resource.bitmap;
import static com.bumptech.glide.load.ImageHeaderParser.ImageType.GIF;
import static com.bumptech.glide.load.ImageHeaderParser.ImageType.JPEG;
import static com.bumptech.glide.load.ImageHeaderParser.ImageType.PNG;
import static com.bumptech.glide.load.ImageHeaderParser.ImageType.PNG_A;
import static com.bumptech.glide.load.ImageHeaderParser.ImageType.UNKNOWN;
import android.support.annotation.NonNull;
import android.util.Log;
import com.bumptech.glide.load.ImageHeaderParser;
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool;
import com.bumptech.glide.util.Preconditions;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
/**
* A class for parsing the exif orientation and other data from an image header.
*/
public final class DefaultImageHeaderParser implements ImageHeaderParser {
// Due to https://code.google.com/p/android/issues/detail?id=97751.
// TAG needs to be under 23 chars, so "Default" > "Dflt".
private static final String TAG = "DfltImageHeaderParser";
private static final int GIF_HEADER = 0x474946;
private static final int PNG_HEADER = 0x89504E47;
static final int EXIF_MAGIC_NUMBER = 0xFFD8;
// "MM".
private static final int MOTOROLA_TIFF_MAGIC_NUMBER = 0x4D4D;
// "II".
private static final int INTEL_TIFF_MAGIC_NUMBER = 0x4949;
private static final String JPEG_EXIF_SEGMENT_PREAMBLE = "Exif\0\0";
static final byte[] JPEG_EXIF_SEGMENT_PREAMBLE_BYTES =
JPEG_EXIF_SEGMENT_PREAMBLE.getBytes(Charset.forName("UTF-8"));
private static final int SEGMENT_SOS = 0xDA;
private static final int MARKER_EOI = 0xD9;
static final int SEGMENT_START_ID = 0xFF;
static final int EXIF_SEGMENT_TYPE = 0xE1;
private static final int ORIENTATION_TAG_TYPE = 0x0112;
private static final int[] BYTES_PER_FORMAT = { 0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8 };
// WebP-related
// "RIFF"
private static final int RIFF_HEADER = 0x52494646;
// "WEBP"
private static final int WEBP_HEADER = 0x57454250;
// "VP8" null.
private static final int VP8_HEADER = 0x56503800;
private static final int VP8_HEADER_MASK = 0xFFFFFF00;
private static final int VP8_HEADER_TYPE_MASK = 0x000000FF;
// 'X'
private static final int VP8_HEADER_TYPE_EXTENDED = 0x00000058;
// 'L'
private static final int VP8_HEADER_TYPE_LOSSLESS = 0x0000004C;
private static final int WEBP_EXTENDED_ALPHA_FLAG = 1 << 4;
private static final int WEBP_LOSSLESS_ALPHA_FLAG = 1 << 3;
@NonNull
@Override
public ImageType getType(@NonNull InputStream is) throws IOException {
return getType(new StreamReader(Preconditions.checkNotNull(is)));
}
@NonNull
@Override
public ImageType getType(@NonNull ByteBuffer byteBuffer) throws IOException {
return getType(new ByteBufferReader(Preconditions.checkNotNull(byteBuffer)));
}
@Override
public int getOrientation(@NonNull InputStream is, @NonNull ArrayPool byteArrayPool)
throws IOException {
return getOrientation(new StreamReader(Preconditions.checkNotNull(is)),
Preconditions.checkNotNull(byteArrayPool));
}
@Override
public int getOrientation(@NonNull ByteBuffer byteBuffer, @NonNull ArrayPool byteArrayPool)
throws IOException {
return getOrientation(new ByteBufferReader(Preconditions.checkNotNull(byteBuffer)),
Preconditions.checkNotNull(byteArrayPool));
}
@NonNull
private ImageType getType(Reader reader) throws IOException {
final int firstTwoBytes = reader.getUInt16();
// JPEG.
if (firstTwoBytes == EXIF_MAGIC_NUMBER) {
return JPEG;
}
final int firstFourBytes = (firstTwoBytes << 16 & 0xFFFF0000) | (reader.getUInt16() & 0xFFFF);
// PNG.
if (firstFourBytes == PNG_HEADER) {
// See: http://stackoverflow.com/questions/2057923/how-to-check-a-png-for-grayscale-alpha
// -color-type
reader.skip(25 - 4);
int alpha = reader.getByte();
// A RGB indexed PNG can also have transparency. Better safe than sorry!
return alpha >= 3 ? PNG_A : PNG;
}
// GIF from first 3 bytes.
if (firstFourBytes >> 8 == GIF_HEADER) {
return GIF;
}
// WebP (reads up to 21 bytes). See https://developers.google.com/speed/webp/docs/riff_container
// for details.
if (firstFourBytes != RIFF_HEADER) {
return UNKNOWN;
}
// Bytes 4 - 7 contain length information. Skip these.
reader.skip(4);
final int thirdFourBytes =
(reader.getUInt16() << 16 & 0xFFFF0000) | (reader.getUInt16() & 0xFFFF);
if (thirdFourBytes != WEBP_HEADER) {
return UNKNOWN;
}
final int fourthFourBytes =
(reader.getUInt16() << 16 & 0xFFFF0000) | (reader.getUInt16() & 0xFFFF);
if ((fourthFourBytes & VP8_HEADER_MASK) != VP8_HEADER) {
return UNKNOWN;
}
if ((fourthFourBytes & VP8_HEADER_TYPE_MASK) == VP8_HEADER_TYPE_EXTENDED) {
// Skip some more length bytes and check for transparency/alpha flag.
reader.skip(4);
return (reader.getByte() & WEBP_EXTENDED_ALPHA_FLAG) != 0 ? ImageType.WEBP_A : ImageType.WEBP;
}
if ((fourthFourBytes & VP8_HEADER_TYPE_MASK) == VP8_HEADER_TYPE_LOSSLESS) {
// See chromium.googlesource.com/webm/libwebp/+/master/doc/webp-lossless-bitstream-spec.txt
// for more info.
reader.skip(4);
return (reader.getByte() & WEBP_LOSSLESS_ALPHA_FLAG) != 0 ? ImageType.WEBP_A : ImageType.WEBP;
}
return ImageType.WEBP;
}
/**
* Parse the orientation from the image header. If it doesn't handle this image type (or this is
* not an image) it will return a default value rather than throwing an exception.
*
* @return The exif orientation if present or -1 if the header couldn't be parsed or doesn't
* contain an orientation
*/
private int getOrientation(Reader reader, ArrayPool byteArrayPool) throws IOException {
final int magicNumber = reader.getUInt16();
if (!handles(magicNumber)) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Parser doesn't handle magic number: " + magicNumber);
}
return UNKNOWN_ORIENTATION;
} else {
int exifSegmentLength = moveToExifSegmentAndGetLength(reader);
if (exifSegmentLength == -1) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Failed to parse exif segment length, or exif segment not found");
}
return UNKNOWN_ORIENTATION;
}
byte[] exifData = byteArrayPool.get(exifSegmentLength, byte[].class);
try {
return parseExifSegment(reader, exifData, exifSegmentLength);
} finally {
byteArrayPool.put(exifData);
}
}
}
private int parseExifSegment(Reader reader, byte[] tempArray, int exifSegmentLength)
throws IOException {
int read = reader.read(tempArray, exifSegmentLength);
if (read != exifSegmentLength) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Unable to read exif segment data"
+ ", length: " + exifSegmentLength
+ ", actually read: " + read);
}
return UNKNOWN_ORIENTATION;
}
boolean hasJpegExifPreamble = hasJpegExifPreamble(tempArray, exifSegmentLength);
if (hasJpegExifPreamble) {
return parseExifSegment(new RandomAccessReader(tempArray, exifSegmentLength));
} else {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Missing jpeg exif preamble");
}
return UNKNOWN_ORIENTATION;
}
}
private boolean hasJpegExifPreamble(byte[] exifData, int exifSegmentLength) {
boolean result =
exifData != null && exifSegmentLength > JPEG_EXIF_SEGMENT_PREAMBLE_BYTES.length;
if (result) {
for (int i = 0; i < JPEG_EXIF_SEGMENT_PREAMBLE_BYTES.length; i++) {
if (exifData[i] != JPEG_EXIF_SEGMENT_PREAMBLE_BYTES[i]) {
result = false;
break;
}
}
}
return result;
}
/**
* Moves reader to the start of the exif segment and returns the length of the exif segment or
* {@code -1} if no exif segment is found.
*/
private int moveToExifSegmentAndGetLength(Reader reader) throws IOException {
while (true) {
short segmentId = reader.getUInt8();
if (segmentId != SEGMENT_START_ID) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Unknown segmentId=" + segmentId);
}
return -1;
}
short segmentType = reader.getUInt8();
if (segmentType == SEGMENT_SOS) {
return -1;
} else if (segmentType == MARKER_EOI) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Found MARKER_EOI in exif segment");
}
return -1;
}
// Segment length includes bytes for segment length.
int segmentLength = reader.getUInt16() - 2;
if (segmentType != EXIF_SEGMENT_TYPE) {
long skipped = reader.skip(segmentLength);
if (skipped != segmentLength) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Unable to skip enough data"
+ ", type: " + segmentType
+ ", wanted to skip: " + segmentLength
+ ", but actually skipped: " + skipped);
}
return -1;
}
} else {
return segmentLength;
}
}
}
private static int parseExifSegment(RandomAccessReader segmentData) {
final int headerOffsetSize = JPEG_EXIF_SEGMENT_PREAMBLE.length();
short byteOrderIdentifier = segmentData.getInt16(headerOffsetSize);
final ByteOrder byteOrder;
switch (byteOrderIdentifier) {
case MOTOROLA_TIFF_MAGIC_NUMBER:
byteOrder = ByteOrder.BIG_ENDIAN;
break;
case INTEL_TIFF_MAGIC_NUMBER:
byteOrder = ByteOrder.LITTLE_ENDIAN;
break;
default:
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Unknown endianness = " + byteOrderIdentifier);
}
byteOrder = ByteOrder.BIG_ENDIAN;
break;
}
segmentData.order(byteOrder);
int firstIfdOffset = segmentData.getInt32(headerOffsetSize + 4) + headerOffsetSize;
int tagCount = segmentData.getInt16(firstIfdOffset);
for (int i = 0; i < tagCount; i++) {
final int tagOffset = calcTagOffset(firstIfdOffset, i);
final int tagType = segmentData.getInt16(tagOffset);
// We only want orientation.
if (tagType != ORIENTATION_TAG_TYPE) {
continue;
}
final int formatCode = segmentData.getInt16(tagOffset + 2);
// 12 is max format code.
if (formatCode < 1 || formatCode > 12) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got invalid format code = " + formatCode);
}
continue;
}
final int componentCount = segmentData.getInt32(tagOffset + 4);
if (componentCount < 0) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Negative tiff component count");
}
continue;
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got tagIndex=" + i + " tagType=" + tagType + " formatCode=" + formatCode
+ " componentCount=" + componentCount);
}
final int byteCount = componentCount + BYTES_PER_FORMAT[formatCode];
if (byteCount > 4) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got byte count > 4, not orientation, continuing, formatCode=" + formatCode);
}
continue;
}
final int tagValueOffset = tagOffset + 8;
if (tagValueOffset < 0 || tagValueOffset > segmentData.length()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Illegal tagValueOffset=" + tagValueOffset + " tagType=" + tagType);
}
continue;
}
if (byteCount < 0 || tagValueOffset + byteCount > segmentData.length()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Illegal number of bytes for TI tag data tagType=" + tagType);
}
continue;
}
//assume componentCount == 1 && fmtCode == 3
return segmentData.getInt16(tagValueOffset);
}
return -1;
}
private static int calcTagOffset(int ifdOffset, int tagIndex) {
return ifdOffset + 2 + 12 * tagIndex;
}
private static boolean handles(int imageMagicNumber) {
return (imageMagicNumber & EXIF_MAGIC_NUMBER) == EXIF_MAGIC_NUMBER
|| imageMagicNumber == MOTOROLA_TIFF_MAGIC_NUMBER
|| imageMagicNumber == INTEL_TIFF_MAGIC_NUMBER;
}
private static final class RandomAccessReader {
private final ByteBuffer data;
RandomAccessReader(byte[] data, int length) {
this.data = (ByteBuffer) ByteBuffer.wrap(data)
.order(ByteOrder.BIG_ENDIAN)
.limit(length);
}
void order(ByteOrder byteOrder) {
this.data.order(byteOrder);
}
int length() {
return data.remaining();
}
int getInt32(int offset) {
return isAvailable(offset, 4) ? data.getInt(offset) : -1;
}
short getInt16(int offset) {
return isAvailable(offset, 2) ? data.getShort(offset) : -1;
}
private boolean isAvailable(int offset, int byteSize) {
return data.remaining() - offset >= byteSize;
}
}
private interface Reader {
int getUInt16() throws IOException;
short getUInt8() throws IOException;
long skip(long total) throws IOException;
int read(byte[] buffer, int byteCount) throws IOException;
int getByte() throws IOException;
}
private static final class ByteBufferReader implements Reader {
private final ByteBuffer byteBuffer;
ByteBufferReader(ByteBuffer byteBuffer) {
this.byteBuffer = byteBuffer;
byteBuffer.order(ByteOrder.BIG_ENDIAN);
}
@Override
public int getUInt16() {
return (getByte() << 8 & 0xFF00) | (getByte() & 0xFF);
}
@Override
public short getUInt8() {
return (short) (getByte() & 0xFF);
}
@Override
public long skip(long total) {
int toSkip = (int) Math.min(byteBuffer.remaining(), total);
byteBuffer.position(byteBuffer.position() + toSkip);
return toSkip;
}
@Override
public int read(byte[] buffer, int byteCount) {
int toRead = Math.min(byteCount, byteBuffer.remaining());
if (toRead == 0) {
return -1;
}
byteBuffer.get(buffer, 0 /*dstOffset*/, toRead);
return toRead;
}
@Override
public int getByte() {
if (byteBuffer.remaining() < 1) {
return -1;
}
return byteBuffer.get();
}
}
private static final class StreamReader implements Reader {
private final InputStream is;
// Motorola / big endian byte order.
StreamReader(InputStream is) {
this.is = is;
}
@Override
public int getUInt16() throws IOException {
return (is.read() << 8 & 0xFF00) | (is.read() & 0xFF);
}
@Override
public short getUInt8() throws IOException {
return (short) (is.read() & 0xFF);
}
@Override
public long skip(long total) throws IOException {
if (total < 0) {
return 0;
}
long toSkip = total;
while (toSkip > 0) {
long skipped = is.skip(toSkip);
if (skipped > 0) {
toSkip -= skipped;
} else {
// Skip has no specific contract as to what happens when you reach the end of
// the stream. To differentiate between temporarily not having more data and
// having finished the stream, we read a single byte when we fail to skip any
// amount of data.
int testEofByte = is.read();
if (testEofByte == -1) {
break;
} else {
toSkip--;
}
}
}
return total - toSkip;
}
@Override
public int read(byte[] buffer, int byteCount) throws IOException {
int toRead = byteCount;
int read;
while (toRead > 0 && ((read = is.read(buffer, byteCount - toRead, toRead)) != -1)) {
toRead -= read;
}
return byteCount - toRead;
}
@Override
public int getByte() throws IOException {
return is.read();
}
}
}