FileFormatDetect.java
9.69 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.scene7.is.util.callbacks.Option
* com.scene7.is.util.collections.CollectionUtil
* com.scene7.is.util.collections.MapEntry
* org.jetbrains.annotations.NotNull
*/
package com.adobe.cq.dam.dm.process.image;
import com.adobe.cq.dam.dm.process.image.FileFormatEnum;
import com.scene7.is.util.callbacks.Option;
import com.scene7.is.util.collections.CollectionUtil;
import com.scene7.is.util.collections.MapEntry;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
class FileFormatDetect {
private static final FileFormat[] QUICK_FORMAT_TESTS = new FileFormat[]{new Bmp(), new Eps(), new Gif(), new Jpeg(), new Psd(), new Pict(), new Png(), new Tiff()};
private static final Map<String, FileFormatEnum> QUICK_FORMAT_MIMETYPES = CollectionUtil.mapOf((MapEntry[])new MapEntry[]{MapEntry.mapEntry((Object)"image/bmp", (Object)((Object)FileFormatEnum.BMP)), MapEntry.mapEntry((Object)"image/eps", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"image/x-eps", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"application/postscript", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"application/eps", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"application/x-eps", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"image/gif", (Object)((Object)FileFormatEnum.GIF)), MapEntry.mapEntry((Object)"image/jpeg", (Object)((Object)FileFormatEnum.JPEG)), MapEntry.mapEntry((Object)"image/pjpeg", (Object)((Object)FileFormatEnum.JPEG)), MapEntry.mapEntry((Object)"image/photoshop", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"image/x-photoshop", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"image/psd", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"application/photoshop", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"application/psd", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"image/vnd.adobe.photoshop", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"image/pict", (Object)((Object)FileFormatEnum.PICT)), MapEntry.mapEntry((Object)"image/x-pict", (Object)((Object)FileFormatEnum.PICT)), MapEntry.mapEntry((Object)"image/png", (Object)((Object)FileFormatEnum.PNG)), MapEntry.mapEntry((Object)"image/tiff", (Object)((Object)FileFormatEnum.TIFF))});
private static final int MAX_IMAGEFORMAT_HEADER_SIZE = FileFormatDetect.calcMaxHeaderSize();
FileFormatDetect() {
}
public static int maxImageFormatHeaderSize() {
return MAX_IMAGEFORMAT_HEADER_SIZE;
}
public static Option<FileFormatEnum> detectImageFormat(byte[] header, int size) {
for (FileFormat t : QUICK_FORMAT_TESTS) {
Option<FileFormatEnum> f = t.quickFormatTest(header, size);
if (!f.isDefined()) continue;
return f;
}
return Option.none();
}
public static Option<FileFormatEnum> detectImageFormat(@NotNull String mimeType) {
return Option.some((Object)((Object)QUICK_FORMAT_MIMETYPES.get(mimeType)));
}
private static int calcMaxHeaderSize() {
int maxsize = 0;
for (FileFormat t : QUICK_FORMAT_TESTS) {
maxsize = Math.max(maxsize, t.getMaxsize());
}
return maxsize;
}
private static int decodeU8(byte[] header, int offset) {
return header[offset] & 255;
}
private static /* varargs */ boolean matches(byte[] header, int offset, int ... codes) {
for (int i = 0; i < codes.length; ++i) {
if (FileFormatDetect.decodeU8(header, offset + i) == codes[i]) continue;
return false;
}
return true;
}
private static boolean matches(byte[] header, int offset, String s) {
for (int i = 0; i < s.length(); ++i) {
if (FileFormatDetect.decodeU8(header, offset + i) == s.charAt(i)) continue;
return false;
}
return true;
}
private static class Tiff
implements FileFormat {
private static final int TIFF_HEADER_SIZE = 8;
private Tiff() {
}
@Override
public int getMaxsize() {
return 8;
}
@Override
public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
if (size < 8) {
return Option.none();
}
if (FileFormatDetect.matches(header, 0, new int[]{77, 77}) || FileFormatDetect.matches(header, 0, new int[]{73, 73})) {
return Option.some((Object)((Object)FileFormatEnum.TIFF));
}
return Option.none();
}
}
private static class Png
implements FileFormat {
private static final int PNG_HEADER_SIZE = 8;
private Png() {
}
@Override
public int getMaxsize() {
return 8;
}
@Override
public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
if (size < 8) {
return Option.none();
}
if (FileFormatDetect.matches(header, 0, new int[]{137, 80, 78, 71, 13, 10, 26, 10})) {
return Option.some((Object)((Object)FileFormatEnum.PNG));
}
return Option.none();
}
}
private static class Pict
implements FileFormat {
private static final int PICT_HEADER_SIZE = 532;
private Pict() {
}
@Override
public int getMaxsize() {
return 532;
}
@Override
public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
if (size < 532) {
return Option.none();
}
if (FileFormatDetect.matches(header, 522, new int[]{0, 17, 2, 255, 12, 0, 255})) {
return Option.some((Object)((Object)FileFormatEnum.PICT));
}
return Option.none();
}
}
private static class Psd
implements FileFormat {
private static final int PSD_HEADER_SIZE = 26;
private Psd() {
}
@Override
public int getMaxsize() {
return 26;
}
@Override
public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
if (size < 26) {
return Option.none();
}
if (FileFormatDetect.matches(header, 0, "8BPS")) {
return Option.some((Object)((Object)FileFormatEnum.PSD));
}
return Option.none();
}
}
private static class Jpeg
implements FileFormat {
private static final int JPEG_MIN_HEADER_SIZE = 5;
private static final int JPEG_MAX_HEADER_SIZE = 256;
private Jpeg() {
}
@Override
public int getMaxsize() {
return 256;
}
@Override
public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
if (size < 5) {
return Option.none();
}
if (FileFormatDetect.matches(header, 0, new int[]{255, 216, 255})) {
return Option.some((Object)((Object)FileFormatEnum.JPEG));
}
if (header[0] == 16 && header[4] == 16 ? FileFormatDetect.matches(header, 124, new int[]{255, 216, 255}) : FileFormatDetect.matches(header, 128, new int[]{255, 216, 255})) {
return Option.some((Object)((Object)FileFormatEnum.JPEG));
}
return Option.none();
}
}
private static class Gif
implements FileFormat {
private static final int GIF_HEADER_SIZE = 13;
private Gif() {
}
@Override
public int getMaxsize() {
return 13;
}
@Override
public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
if (size < 13) {
return Option.none();
}
if (FileFormatDetect.matches(header, 0, "GIF")) {
return Option.some((Object)((Object)FileFormatEnum.GIF));
}
return Option.none();
}
}
private static class Eps
implements FileFormat {
private static final int EPS_HEADER_SIZE = 11;
private static final String EPS_HDR_MAGIC = "\u00c5\u00d0\u00d3\u00c6";
private Eps() {
}
@Override
public int getMaxsize() {
return 11;
}
@Override
public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
if (size < 11) {
return Option.none();
}
if (FileFormatDetect.matches(header, 0, "%!PS-Adobe-")) {
return Option.some((Object)((Object)FileFormatEnum.EPS));
}
if (FileFormatDetect.matches(header, 0, "\u00c5\u00d0\u00d3\u00c6")) {
return Option.some((Object)((Object)FileFormatEnum.EPS));
}
return Option.none();
}
}
private static class Bmp
implements FileFormat {
private static final int BMP_HEADER_SIZE = 2;
private Bmp() {
}
@Override
public int getMaxsize() {
return 2;
}
@Override
public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
if (size < 2) {
return Option.none();
}
if (FileFormatDetect.matches(header, 0, "BM")) {
return Option.some((Object)((Object)FileFormatEnum.BMP));
}
return Option.none();
}
}
private static interface FileFormat {
public int getMaxsize();
public Option<FileFormatEnum> quickFormatTest(byte[] var1, int var2);
}
}