1 | /*
|
---|
2 | * Copyright 2002-2017 Drew Noakes
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | * More information about this project is available at:
|
---|
17 | *
|
---|
18 | * https://drewnoakes.com/code/exif/
|
---|
19 | * https://github.com/drewnoakes/metadata-extractor
|
---|
20 | */
|
---|
21 | package com.drew.imaging.tiff;
|
---|
22 |
|
---|
23 | import com.drew.lang.RandomAccessReader;
|
---|
24 | import com.drew.lang.Rational;
|
---|
25 | import com.drew.lang.annotations.NotNull;
|
---|
26 |
|
---|
27 | import java.io.IOException;
|
---|
28 | import java.util.HashSet;
|
---|
29 | import java.util.Set;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Processes TIFF-formatted data, calling into client code via that {@link TiffHandler} interface.
|
---|
33 | *
|
---|
34 | * @author Drew Noakes https://drewnoakes.com
|
---|
35 | */
|
---|
36 | public class TiffReader
|
---|
37 | {
|
---|
38 | /**
|
---|
39 | * Processes a TIFF data sequence.
|
---|
40 | *
|
---|
41 | * @param reader the {@link RandomAccessReader} from which the data should be read
|
---|
42 | * @param handler the {@link TiffHandler} that will coordinate processing and accept read values
|
---|
43 | * @param tiffHeaderOffset the offset within <code>reader</code> at which the TIFF header starts
|
---|
44 | * @throws TiffProcessingException if an error occurred during the processing of TIFF data that could not be
|
---|
45 | * ignored or recovered from
|
---|
46 | * @throws IOException an error occurred while accessing the required data
|
---|
47 | */
|
---|
48 | public void processTiff(@NotNull final RandomAccessReader reader,
|
---|
49 | @NotNull final TiffHandler handler,
|
---|
50 | final int tiffHeaderOffset) throws TiffProcessingException, IOException
|
---|
51 | {
|
---|
52 | // This must be either "MM" or "II".
|
---|
53 | short byteOrderIdentifier = reader.getInt16(tiffHeaderOffset);
|
---|
54 |
|
---|
55 | if (byteOrderIdentifier == 0x4d4d) { // "MM"
|
---|
56 | reader.setMotorolaByteOrder(true);
|
---|
57 | } else if (byteOrderIdentifier == 0x4949) { // "II"
|
---|
58 | reader.setMotorolaByteOrder(false);
|
---|
59 | } else {
|
---|
60 | throw new TiffProcessingException("Unclear distinction between Motorola/Intel byte ordering: " + byteOrderIdentifier);
|
---|
61 | }
|
---|
62 |
|
---|
63 | // Check the next two values for correctness.
|
---|
64 | final int tiffMarker = reader.getUInt16(2 + tiffHeaderOffset);
|
---|
65 | handler.setTiffMarker(tiffMarker);
|
---|
66 |
|
---|
67 | int firstIfdOffset = reader.getInt32(4 + tiffHeaderOffset) + tiffHeaderOffset;
|
---|
68 |
|
---|
69 | // David Ekholm sent a digital camera image that has this problem
|
---|
70 | // TODO getLength should be avoided as it causes RandomAccessStreamReader to read to the end of the stream
|
---|
71 | if (firstIfdOffset >= reader.getLength() - 1) {
|
---|
72 | handler.warn("First IFD offset is beyond the end of the TIFF data segment -- trying default offset");
|
---|
73 | // First directory normally starts immediately after the offset bytes, so try that
|
---|
74 | firstIfdOffset = tiffHeaderOffset + 2 + 2 + 4;
|
---|
75 | }
|
---|
76 |
|
---|
77 | Set<Integer> processedIfdOffsets = new HashSet<Integer>();
|
---|
78 | processIfd(handler, reader, processedIfdOffsets, firstIfdOffset, tiffHeaderOffset);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Processes a TIFF IFD.
|
---|
83 | *
|
---|
84 | * IFD Header:
|
---|
85 | * <ul>
|
---|
86 | * <li><b>2 bytes</b> number of tags</li>
|
---|
87 | * </ul>
|
---|
88 | * Tag structure:
|
---|
89 | * <ul>
|
---|
90 | * <li><b>2 bytes</b> tag type</li>
|
---|
91 | * <li><b>2 bytes</b> format code (values 1 to 12, inclusive)</li>
|
---|
92 | * <li><b>4 bytes</b> component count</li>
|
---|
93 | * <li><b>4 bytes</b> inline value, or offset pointer if too large to fit in four bytes</li>
|
---|
94 | * </ul>
|
---|
95 | *
|
---|
96 | *
|
---|
97 | * @param handler the {@link com.drew.imaging.tiff.TiffHandler} that will coordinate processing and accept read values
|
---|
98 | * @param reader the {@link com.drew.lang.RandomAccessReader} from which the data should be read
|
---|
99 | * @param processedIfdOffsets the set of visited IFD offsets, to avoid revisiting the same IFD in an endless loop
|
---|
100 | * @param ifdOffset the offset within <code>reader</code> at which the IFD data starts
|
---|
101 | * @param tiffHeaderOffset the offset within <code>reader</code> at which the TIFF header starts
|
---|
102 | * @throws IOException an error occurred while accessing the required data
|
---|
103 | */
|
---|
104 | public static void processIfd(@NotNull final TiffHandler handler,
|
---|
105 | @NotNull final RandomAccessReader reader,
|
---|
106 | @NotNull final Set<Integer> processedIfdOffsets,
|
---|
107 | final int ifdOffset,
|
---|
108 | final int tiffHeaderOffset) throws IOException
|
---|
109 | {
|
---|
110 | Boolean resetByteOrder = null;
|
---|
111 | try {
|
---|
112 | // check for directories we've already visited to avoid stack overflows when recursive/cyclic directory structures exist
|
---|
113 | if (processedIfdOffsets.contains(Integer.valueOf(ifdOffset))) {
|
---|
114 | return;
|
---|
115 | }
|
---|
116 |
|
---|
117 | // remember that we've visited this directory so that we don't visit it again later
|
---|
118 | processedIfdOffsets.add(ifdOffset);
|
---|
119 |
|
---|
120 | if (ifdOffset >= reader.getLength() || ifdOffset < 0) {
|
---|
121 | handler.error("Ignored IFD marked to start outside data segment");
|
---|
122 | return;
|
---|
123 | }
|
---|
124 |
|
---|
125 | // First two bytes in the IFD are the number of tags in this directory
|
---|
126 | int dirTagCount = reader.getUInt16(ifdOffset);
|
---|
127 |
|
---|
128 | // Some software modifies the byte order of the file, but misses some IFDs (such as makernotes).
|
---|
129 | // The entire test image repository doesn't contain a single IFD with more than 255 entries.
|
---|
130 | // Here we detect switched bytes that suggest this problem, and temporarily swap the byte order.
|
---|
131 | // This was discussed in GitHub issue #136.
|
---|
132 | if (dirTagCount > 0xFF && (dirTagCount & 0xFF) == 0) {
|
---|
133 | resetByteOrder = reader.isMotorolaByteOrder();
|
---|
134 | dirTagCount >>= 8;
|
---|
135 | reader.setMotorolaByteOrder(!reader.isMotorolaByteOrder());
|
---|
136 | }
|
---|
137 |
|
---|
138 | int dirLength = (2 + (12 * dirTagCount) + 4);
|
---|
139 | if (dirLength + ifdOffset > reader.getLength()) {
|
---|
140 | handler.error("Illegally sized IFD");
|
---|
141 | return;
|
---|
142 | }
|
---|
143 |
|
---|
144 | //
|
---|
145 | // Handle each tag in this directory
|
---|
146 | //
|
---|
147 | int invalidTiffFormatCodeCount = 0;
|
---|
148 | for (int tagNumber = 0; tagNumber < dirTagCount; tagNumber++) {
|
---|
149 | final int tagOffset = calculateTagOffset(ifdOffset, tagNumber);
|
---|
150 |
|
---|
151 | // 2 bytes for the tag id
|
---|
152 | final int tagId = reader.getUInt16(tagOffset);
|
---|
153 |
|
---|
154 | // 2 bytes for the format code
|
---|
155 | final int formatCode = reader.getUInt16(tagOffset + 2);
|
---|
156 | final TiffDataFormat format = TiffDataFormat.fromTiffFormatCode(formatCode);
|
---|
157 |
|
---|
158 | // 4 bytes dictate the number of components in this tag's data
|
---|
159 | final long componentCount = reader.getUInt32(tagOffset + 4);
|
---|
160 |
|
---|
161 | final long byteCount;
|
---|
162 | if (format == null) {
|
---|
163 | Long byteCountOverride = handler.tryCustomProcessFormat(tagId, formatCode, componentCount);
|
---|
164 | if (byteCountOverride == null) {
|
---|
165 | // This error suggests that we are processing at an incorrect index and will generate
|
---|
166 | // rubbish until we go out of bounds (which may be a while). Exit now.
|
---|
167 | handler.error(String.format("Invalid TIFF tag format code %d for tag 0x%04X", formatCode, tagId));
|
---|
168 | // TODO specify threshold as a parameter, or provide some other external control over this behaviour
|
---|
169 | if (++invalidTiffFormatCodeCount > 5) {
|
---|
170 | handler.error("Stopping processing as too many errors seen in TIFF IFD");
|
---|
171 | return;
|
---|
172 | }
|
---|
173 | continue;
|
---|
174 | }
|
---|
175 | byteCount = byteCountOverride;
|
---|
176 | } else {
|
---|
177 | byteCount = componentCount * format.getComponentSizeBytes();
|
---|
178 | }
|
---|
179 |
|
---|
180 | final long tagValueOffset;
|
---|
181 | if (byteCount > 4) {
|
---|
182 | // If it's bigger than 4 bytes, the dir entry contains an offset.
|
---|
183 | final long offsetVal = reader.getUInt32(tagOffset + 8);
|
---|
184 | if (offsetVal + byteCount > reader.getLength()) {
|
---|
185 | // Bogus pointer offset and / or byteCount value
|
---|
186 | handler.error("Illegal TIFF tag pointer offset");
|
---|
187 | continue;
|
---|
188 | }
|
---|
189 | tagValueOffset = tiffHeaderOffset + offsetVal;
|
---|
190 | } else {
|
---|
191 | // 4 bytes or less and value is in the dir entry itself.
|
---|
192 | tagValueOffset = tagOffset + 8;
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (tagValueOffset < 0 || tagValueOffset > reader.getLength()) {
|
---|
196 | handler.error("Illegal TIFF tag pointer offset");
|
---|
197 | continue;
|
---|
198 | }
|
---|
199 |
|
---|
200 | // Check that this tag isn't going to allocate outside the bounds of the data array.
|
---|
201 | // This addresses an uncommon OutOfMemoryError.
|
---|
202 | if (byteCount < 0 || tagValueOffset + byteCount > reader.getLength()) {
|
---|
203 | handler.error("Illegal number of bytes for TIFF tag data: " + byteCount);
|
---|
204 | continue;
|
---|
205 | }
|
---|
206 |
|
---|
207 | // Some tags point to one or more additional IFDs to process
|
---|
208 | boolean isIfdPointer = false;
|
---|
209 | if (byteCount == 4 * componentCount) {
|
---|
210 | for (int i = 0; i < componentCount; i++) {
|
---|
211 | if (handler.tryEnterSubIfd(tagId)) {
|
---|
212 | isIfdPointer = true;
|
---|
213 | int subDirOffset = tiffHeaderOffset + reader.getInt32((int) (tagValueOffset + i * 4));
|
---|
214 | processIfd(handler, reader, processedIfdOffsets, subDirOffset, tiffHeaderOffset);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | // If it wasn't an IFD pointer, allow custom tag processing to occur
|
---|
220 | if (!isIfdPointer && !handler.customProcessTag((int) tagValueOffset, processedIfdOffsets, tiffHeaderOffset, reader, tagId, (int) byteCount)) {
|
---|
221 | // If no custom processing occurred, process the tag in the standard fashion
|
---|
222 | processTag(handler, tagId, (int) tagValueOffset, (int) componentCount, formatCode, reader);
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | // at the end of each IFD is an optional link to the next IFD
|
---|
227 | final int finalTagOffset = calculateTagOffset(ifdOffset, dirTagCount);
|
---|
228 | int nextIfdOffset = reader.getInt32(finalTagOffset);
|
---|
229 | if (nextIfdOffset != 0) {
|
---|
230 | nextIfdOffset += tiffHeaderOffset;
|
---|
231 | if (nextIfdOffset >= reader.getLength()) {
|
---|
232 | // Last 4 bytes of IFD reference another IFD with an address that is out of bounds
|
---|
233 | // Note this could have been caused by jhead 1.3 cropping too much
|
---|
234 | return;
|
---|
235 | } else if (nextIfdOffset < ifdOffset) {
|
---|
236 | // TODO is this a valid restriction?
|
---|
237 | // Last 4 bytes of IFD reference another IFD with an address that is before the start of this directory
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (handler.hasFollowerIfd()) {
|
---|
242 | processIfd(handler, reader, processedIfdOffsets, nextIfdOffset, tiffHeaderOffset);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | } finally {
|
---|
246 | handler.endingIFD();
|
---|
247 | if (resetByteOrder != null)
|
---|
248 | reader.setMotorolaByteOrder(resetByteOrder);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | private static void processTag(@NotNull final TiffHandler handler,
|
---|
253 | final int tagId,
|
---|
254 | final int tagValueOffset,
|
---|
255 | final int componentCount,
|
---|
256 | final int formatCode,
|
---|
257 | @NotNull final RandomAccessReader reader) throws IOException
|
---|
258 | {
|
---|
259 | switch (formatCode) {
|
---|
260 | case TiffDataFormat.CODE_UNDEFINED:
|
---|
261 | // this includes exif user comments
|
---|
262 | handler.setByteArray(tagId, reader.getBytes(tagValueOffset, componentCount));
|
---|
263 | break;
|
---|
264 | case TiffDataFormat.CODE_STRING:
|
---|
265 | handler.setString(tagId, reader.getNullTerminatedStringValue(tagValueOffset, componentCount, null));
|
---|
266 | break;
|
---|
267 | case TiffDataFormat.CODE_RATIONAL_S:
|
---|
268 | if (componentCount == 1) {
|
---|
269 | handler.setRational(tagId, new Rational(reader.getInt32(tagValueOffset), reader.getInt32(tagValueOffset + 4)));
|
---|
270 | } else if (componentCount > 1) {
|
---|
271 | Rational[] array = new Rational[componentCount];
|
---|
272 | for (int i = 0; i < componentCount; i++)
|
---|
273 | array[i] = new Rational(reader.getInt32(tagValueOffset + (8 * i)), reader.getInt32(tagValueOffset + 4 + (8 * i)));
|
---|
274 | handler.setRationalArray(tagId, array);
|
---|
275 | }
|
---|
276 | break;
|
---|
277 | case TiffDataFormat.CODE_RATIONAL_U:
|
---|
278 | if (componentCount == 1) {
|
---|
279 | handler.setRational(tagId, new Rational(reader.getUInt32(tagValueOffset), reader.getUInt32(tagValueOffset + 4)));
|
---|
280 | } else if (componentCount > 1) {
|
---|
281 | Rational[] array = new Rational[componentCount];
|
---|
282 | for (int i = 0; i < componentCount; i++)
|
---|
283 | array[i] = new Rational(reader.getUInt32(tagValueOffset + (8 * i)), reader.getUInt32(tagValueOffset + 4 + (8 * i)));
|
---|
284 | handler.setRationalArray(tagId, array);
|
---|
285 | }
|
---|
286 | break;
|
---|
287 | case TiffDataFormat.CODE_SINGLE:
|
---|
288 | if (componentCount == 1) {
|
---|
289 | handler.setFloat(tagId, reader.getFloat32(tagValueOffset));
|
---|
290 | } else {
|
---|
291 | float[] array = new float[componentCount];
|
---|
292 | for (int i = 0; i < componentCount; i++)
|
---|
293 | array[i] = reader.getFloat32(tagValueOffset + (i * 4));
|
---|
294 | handler.setFloatArray(tagId, array);
|
---|
295 | }
|
---|
296 | break;
|
---|
297 | case TiffDataFormat.CODE_DOUBLE:
|
---|
298 | if (componentCount == 1) {
|
---|
299 | handler.setDouble(tagId, reader.getDouble64(tagValueOffset));
|
---|
300 | } else {
|
---|
301 | double[] array = new double[componentCount];
|
---|
302 | for (int i = 0; i < componentCount; i++)
|
---|
303 | array[i] = reader.getDouble64(tagValueOffset + (i * 4));
|
---|
304 | handler.setDoubleArray(tagId, array);
|
---|
305 | }
|
---|
306 | break;
|
---|
307 | case TiffDataFormat.CODE_INT8_S:
|
---|
308 | if (componentCount == 1) {
|
---|
309 | handler.setInt8s(tagId, reader.getInt8(tagValueOffset));
|
---|
310 | } else {
|
---|
311 | byte[] array = new byte[componentCount];
|
---|
312 | for (int i = 0; i < componentCount; i++)
|
---|
313 | array[i] = reader.getInt8(tagValueOffset + i);
|
---|
314 | handler.setInt8sArray(tagId, array);
|
---|
315 | }
|
---|
316 | break;
|
---|
317 | case TiffDataFormat.CODE_INT8_U:
|
---|
318 | if (componentCount == 1) {
|
---|
319 | handler.setInt8u(tagId, reader.getUInt8(tagValueOffset));
|
---|
320 | } else {
|
---|
321 | short[] array = new short[componentCount];
|
---|
322 | for (int i = 0; i < componentCount; i++)
|
---|
323 | array[i] = reader.getUInt8(tagValueOffset + i);
|
---|
324 | handler.setInt8uArray(tagId, array);
|
---|
325 | }
|
---|
326 | break;
|
---|
327 | case TiffDataFormat.CODE_INT16_S:
|
---|
328 | if (componentCount == 1) {
|
---|
329 | handler.setInt16s(tagId, (int)reader.getInt16(tagValueOffset));
|
---|
330 | } else {
|
---|
331 | short[] array = new short[componentCount];
|
---|
332 | for (int i = 0; i < componentCount; i++)
|
---|
333 | array[i] = reader.getInt16(tagValueOffset + (i * 2));
|
---|
334 | handler.setInt16sArray(tagId, array);
|
---|
335 | }
|
---|
336 | break;
|
---|
337 | case TiffDataFormat.CODE_INT16_U:
|
---|
338 | if (componentCount == 1) {
|
---|
339 | handler.setInt16u(tagId, reader.getUInt16(tagValueOffset));
|
---|
340 | } else {
|
---|
341 | int[] array = new int[componentCount];
|
---|
342 | for (int i = 0; i < componentCount; i++)
|
---|
343 | array[i] = reader.getUInt16(tagValueOffset + (i * 2));
|
---|
344 | handler.setInt16uArray(tagId, array);
|
---|
345 | }
|
---|
346 | break;
|
---|
347 | case TiffDataFormat.CODE_INT32_S:
|
---|
348 | // NOTE 'long' in this case means 32 bit, not 64
|
---|
349 | if (componentCount == 1) {
|
---|
350 | handler.setInt32s(tagId, reader.getInt32(tagValueOffset));
|
---|
351 | } else {
|
---|
352 | int[] array = new int[componentCount];
|
---|
353 | for (int i = 0; i < componentCount; i++)
|
---|
354 | array[i] = reader.getInt32(tagValueOffset + (i * 4));
|
---|
355 | handler.setInt32sArray(tagId, array);
|
---|
356 | }
|
---|
357 | break;
|
---|
358 | case TiffDataFormat.CODE_INT32_U:
|
---|
359 | // NOTE 'long' in this case means 32 bit, not 64
|
---|
360 | if (componentCount == 1) {
|
---|
361 | handler.setInt32u(tagId, reader.getUInt32(tagValueOffset));
|
---|
362 | } else {
|
---|
363 | long[] array = new long[componentCount];
|
---|
364 | for (int i = 0; i < componentCount; i++)
|
---|
365 | array[i] = reader.getUInt32(tagValueOffset + (i * 4));
|
---|
366 | handler.setInt32uArray(tagId, array);
|
---|
367 | }
|
---|
368 | break;
|
---|
369 | default:
|
---|
370 | handler.error(String.format("Invalid TIFF tag format code %d for tag 0x%04X", formatCode, tagId));
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Determine the offset of a given tag within the specified IFD.
|
---|
376 | *
|
---|
377 | * @param ifdStartOffset the offset at which the IFD starts
|
---|
378 | * @param entryNumber the zero-based entry number
|
---|
379 | */
|
---|
380 | private static int calculateTagOffset(int ifdStartOffset, int entryNumber)
|
---|
381 | {
|
---|
382 | // Add 2 bytes for the tag count.
|
---|
383 | // Each entry is 12 bytes.
|
---|
384 | return ifdStartOffset + 2 + (12 * entryNumber);
|
---|
385 | }
|
---|
386 | }
|
---|