-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiffParser.java
More file actions
119 lines (105 loc) · 4.34 KB
/
TiffParser.java
File metadata and controls
119 lines (105 loc) · 4.34 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
package me.tamkungz.codecmedia.internal.image.tiff;
import me.tamkungz.codecmedia.CodecMediaException;
public final class TiffParser {
private TiffParser() {
}
public static boolean isLikelyTiff(byte[] bytes) {
return bytes.length >= 8
&& ((bytes[0] == 'I' && bytes[1] == 'I' && bytes[2] == 42 && bytes[3] == 0)
|| (bytes[0] == 'M' && bytes[1] == 'M' && bytes[2] == 0 && bytes[3] == 42));
}
public static TiffProbeInfo parse(byte[] bytes) throws CodecMediaException {
if (!isLikelyTiff(bytes)) {
throw new CodecMediaException("Not a TIFF file");
}
boolean littleEndian = bytes[0] == 'I';
int ifdOffset = readU32(bytes, 4, littleEndian);
if (ifdOffset < 8 || ifdOffset + 2 > bytes.length) {
throw new CodecMediaException("Invalid TIFF IFD offset");
}
int entryCount = readU16(bytes, ifdOffset, littleEndian);
int pos = ifdOffset + 2;
int maxEntries = (bytes.length - pos) / 12;
if (entryCount > maxEntries) {
throw new CodecMediaException("TIFF IFD entry count exceeds available data");
}
Integer width = null;
Integer height = null;
Integer bitDepth = null;
for (int i = 0; i < entryCount; i++) {
if (pos + 12 > bytes.length) {
throw new CodecMediaException("Invalid TIFF IFD entry bounds");
}
int tag = readU16(bytes, pos, littleEndian);
int type = readU16(bytes, pos + 2, littleEndian);
int count = readU32(bytes, pos + 4, littleEndian);
int valueOrOffset = readU32(bytes, pos + 8, littleEndian);
if ((tag == 256 || tag == 257) && count >= 1) {
Integer v = readTagFirstShortOrLongValue(bytes, type, count, valueOrOffset, littleEndian);
if (v != null && v > 0) {
if (tag == 256) {
width = v;
} else {
height = v;
}
}
} else if (tag == 258 && count >= 1) {
Integer v = readTagFirstShortOrLongValue(bytes, type, count, valueOrOffset, littleEndian);
if (v != null && v > 0) {
bitDepth = v;
}
}
pos += 12;
}
if (width == null || height == null || width <= 0 || height <= 0) {
throw new CodecMediaException("TIFF missing width/height tags");
}
return new TiffProbeInfo(width, height, bitDepth);
}
private static Integer readTagFirstShortOrLongValue(
byte[] bytes,
int type,
int count,
int valueOrOffset,
boolean littleEndian
)
throws CodecMediaException {
if (type == 3) {
if (count == 1) {
return littleEndian ? (valueOrOffset & 0xFFFF) : ((valueOrOffset >>> 16) & 0xFFFF);
}
return readU16(bytes, valueOrOffset, littleEndian);
}
if (type == 4) {
if (count == 1) {
return valueOrOffset;
}
return readU32(bytes, valueOrOffset, littleEndian);
}
return null;
}
private static int readU16(byte[] bytes, int offset, boolean littleEndian) throws CodecMediaException {
if (offset + 2 > bytes.length) {
throw new CodecMediaException("Unexpected end of TIFF data");
}
if (littleEndian) {
return (bytes[offset] & 0xFF) | ((bytes[offset + 1] & 0xFF) << 8);
}
return ((bytes[offset] & 0xFF) << 8) | (bytes[offset + 1] & 0xFF);
}
private static int readU32(byte[] bytes, int offset, boolean littleEndian) throws CodecMediaException {
if (offset + 4 > bytes.length) {
throw new CodecMediaException("Unexpected end of TIFF data");
}
if (littleEndian) {
return (bytes[offset] & 0xFF)
| ((bytes[offset + 1] & 0xFF) << 8)
| ((bytes[offset + 2] & 0xFF) << 16)
| ((bytes[offset + 3] & 0xFF) << 24);
}
return ((bytes[offset] & 0xFF) << 24)
| ((bytes[offset + 1] & 0xFF) << 16)
| ((bytes[offset + 2] & 0xFF) << 8)
| (bytes[offset + 3] & 0xFF);
}
}