source: josm/trunk/src/com/drew/imaging/tiff/TiffReader.java@ 12212

Last change on this file since 12212 was 10862, checked in by Don-vip, 8 years ago

update to metadata-extractor 2.9.1

File size: 18.6 KB
Line 
1/*
2 * Copyright 2002-2016 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 */
21package com.drew.imaging.tiff;
22
23import com.drew.lang.RandomAccessReader;
24import com.drew.lang.Rational;
25import com.drew.lang.annotations.NotNull;
26
27import java.io.IOException;
28import java.util.HashSet;
29import 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 */
36public 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 handler.completed(reader, tiffHeaderOffset);
81 }
82
83 /**
84 * Processes a TIFF IFD.
85 *
86 * IFD Header:
87 * <ul>
88 * <li><b>2 bytes</b> number of tags</li>
89 * </ul>
90 * Tag structure:
91 * <ul>
92 * <li><b>2 bytes</b> tag type</li>
93 * <li><b>2 bytes</b> format code (values 1 to 12, inclusive)</li>
94 * <li><b>4 bytes</b> component count</li>
95 * <li><b>4 bytes</b> inline value, or offset pointer if too large to fit in four bytes</li>
96 * </ul>
97 *
98 *
99 * @param handler the {@link com.drew.imaging.tiff.TiffHandler} that will coordinate processing and accept read values
100 * @param reader the {@link com.drew.lang.RandomAccessReader} from which the data should be read
101 * @param processedIfdOffsets the set of visited IFD offsets, to avoid revisiting the same IFD in an endless loop
102 * @param ifdOffset the offset within <code>reader</code> at which the IFD data starts
103 * @param tiffHeaderOffset the offset within <code>reader</code> at which the TIFF header starts
104 * @throws IOException an error occurred while accessing the required data
105 */
106 public static void processIfd(@NotNull final TiffHandler handler,
107 @NotNull final RandomAccessReader reader,
108 @NotNull final Set<Integer> processedIfdOffsets,
109 final int ifdOffset,
110 final int tiffHeaderOffset) throws IOException
111 {
112 Boolean resetByteOrder = null;
113 try {
114 // check for directories we've already visited to avoid stack overflows when recursive/cyclic directory structures exist
115 if (processedIfdOffsets.contains(Integer.valueOf(ifdOffset))) {
116 return;
117 }
118
119 // remember that we've visited this directory so that we don't visit it again later
120 processedIfdOffsets.add(ifdOffset);
121
122 if (ifdOffset >= reader.getLength() || ifdOffset < 0) {
123 handler.error("Ignored IFD marked to start outside data segment");
124 return;
125 }
126
127 // First two bytes in the IFD are the number of tags in this directory
128 int dirTagCount = reader.getUInt16(ifdOffset);
129
130 // Some software modifies the byte order of the file, but misses some IFDs (such as makernotes).
131 // The entire test image repository doesn't contain a single IFD with more than 255 entries.
132 // Here we detect switched bytes that suggest this problem, and temporarily swap the byte order.
133 // This was discussed in GitHub issue #136.
134 if (dirTagCount > 0xFF && (dirTagCount & 0xFF) == 0) {
135 resetByteOrder = reader.isMotorolaByteOrder();
136 dirTagCount >>= 8;
137 reader.setMotorolaByteOrder(!reader.isMotorolaByteOrder());
138 }
139
140 int dirLength = (2 + (12 * dirTagCount) + 4);
141 if (dirLength + ifdOffset > reader.getLength()) {
142 handler.error("Illegally sized IFD");
143 return;
144 }
145
146 //
147 // Handle each tag in this directory
148 //
149 int invalidTiffFormatCodeCount = 0;
150 for (int tagNumber = 0; tagNumber < dirTagCount; tagNumber++) {
151 final int tagOffset = calculateTagOffset(ifdOffset, tagNumber);
152
153 // 2 bytes for the tag id
154 final int tagId = reader.getUInt16(tagOffset);
155
156 // 2 bytes for the format code
157 final int formatCode = reader.getUInt16(tagOffset + 2);
158 final TiffDataFormat format = TiffDataFormat.fromTiffFormatCode(formatCode);
159
160 // 4 bytes dictate the number of components in this tag's data
161 final long componentCount = reader.getUInt32(tagOffset + 4);
162
163 final long byteCount;
164 if (format == null) {
165 Long byteCountOverride = handler.tryCustomProcessFormat(tagId, formatCode, componentCount);
166 if (byteCountOverride == null) {
167 // This error suggests that we are processing at an incorrect index and will generate
168 // rubbish until we go out of bounds (which may be a while). Exit now.
169 handler.error(String.format("Invalid TIFF tag format code %d for tag 0x%04X", formatCode, tagId));
170 // TODO specify threshold as a parameter, or provide some other external control over this behaviour
171 if (++invalidTiffFormatCodeCount > 5) {
172 handler.error("Stopping processing as too many errors seen in TIFF IFD");
173 return;
174 }
175 continue;
176 }
177 byteCount = byteCountOverride;
178 } else {
179 byteCount = componentCount * format.getComponentSizeBytes();
180 }
181
182 final long tagValueOffset;
183 if (byteCount > 4) {
184 // If it's bigger than 4 bytes, the dir entry contains an offset.
185 final long offsetVal = reader.getUInt32(tagOffset + 8);
186 if (offsetVal + byteCount > reader.getLength()) {
187 // Bogus pointer offset and / or byteCount value
188 handler.error("Illegal TIFF tag pointer offset");
189 continue;
190 }
191 tagValueOffset = tiffHeaderOffset + offsetVal;
192 } else {
193 // 4 bytes or less and value is in the dir entry itself.
194 tagValueOffset = tagOffset + 8;
195 }
196
197 if (tagValueOffset < 0 || tagValueOffset > reader.getLength()) {
198 handler.error("Illegal TIFF tag pointer offset");
199 continue;
200 }
201
202 // Check that this tag isn't going to allocate outside the bounds of the data array.
203 // This addresses an uncommon OutOfMemoryError.
204 if (byteCount < 0 || tagValueOffset + byteCount > reader.getLength()) {
205 handler.error("Illegal number of bytes for TIFF tag data: " + byteCount);
206 continue;
207 }
208
209 // Some tags point to one or more additional IFDs to process
210 boolean isIfdPointer = false;
211 if (byteCount == 4 * componentCount) {
212 for (int i = 0; i < componentCount; i++) {
213 if (handler.tryEnterSubIfd(tagId)) {
214 isIfdPointer = true;
215 int subDirOffset = tiffHeaderOffset + reader.getInt32((int) (tagValueOffset + i * 4));
216 processIfd(handler, reader, processedIfdOffsets, subDirOffset, tiffHeaderOffset);
217 }
218 }
219 }
220
221 // If it wasn't an IFD pointer, allow custom tag processing to occur
222 if (!isIfdPointer && !handler.customProcessTag((int) tagValueOffset, processedIfdOffsets, tiffHeaderOffset, reader, tagId, (int) byteCount)) {
223 // If no custom processing occurred, process the tag in the standard fashion
224 processTag(handler, tagId, (int) tagValueOffset, (int) componentCount, formatCode, reader);
225 }
226 }
227
228 // at the end of each IFD is an optional link to the next IFD
229 final int finalTagOffset = calculateTagOffset(ifdOffset, dirTagCount);
230 int nextIfdOffset = reader.getInt32(finalTagOffset);
231 if (nextIfdOffset != 0) {
232 nextIfdOffset += tiffHeaderOffset;
233 if (nextIfdOffset >= reader.getLength()) {
234 // Last 4 bytes of IFD reference another IFD with an address that is out of bounds
235 // Note this could have been caused by jhead 1.3 cropping too much
236 return;
237 } else if (nextIfdOffset < ifdOffset) {
238 // TODO is this a valid restriction?
239 // Last 4 bytes of IFD reference another IFD with an address that is before the start of this directory
240 return;
241 }
242
243 if (handler.hasFollowerIfd()) {
244 processIfd(handler, reader, processedIfdOffsets, nextIfdOffset, tiffHeaderOffset);
245 }
246 }
247 } finally {
248 handler.endingIFD();
249 if (resetByteOrder != null)
250 reader.setMotorolaByteOrder(resetByteOrder);
251 }
252 }
253
254 private static void processTag(@NotNull final TiffHandler handler,
255 final int tagId,
256 final int tagValueOffset,
257 final int componentCount,
258 final int formatCode,
259 @NotNull final RandomAccessReader reader) throws IOException
260 {
261 switch (formatCode) {
262 case TiffDataFormat.CODE_UNDEFINED:
263 // this includes exif user comments
264 handler.setByteArray(tagId, reader.getBytes(tagValueOffset, componentCount));
265 break;
266 case TiffDataFormat.CODE_STRING:
267 handler.setString(tagId, reader.getNullTerminatedString(tagValueOffset, componentCount));
268 break;
269 case TiffDataFormat.CODE_RATIONAL_S:
270 if (componentCount == 1) {
271 handler.setRational(tagId, new Rational(reader.getInt32(tagValueOffset), reader.getInt32(tagValueOffset + 4)));
272 } else if (componentCount > 1) {
273 Rational[] array = new Rational[componentCount];
274 for (int i = 0; i < componentCount; i++)
275 array[i] = new Rational(reader.getInt32(tagValueOffset + (8 * i)), reader.getInt32(tagValueOffset + 4 + (8 * i)));
276 handler.setRationalArray(tagId, array);
277 }
278 break;
279 case TiffDataFormat.CODE_RATIONAL_U:
280 if (componentCount == 1) {
281 handler.setRational(tagId, new Rational(reader.getUInt32(tagValueOffset), reader.getUInt32(tagValueOffset + 4)));
282 } else if (componentCount > 1) {
283 Rational[] array = new Rational[componentCount];
284 for (int i = 0; i < componentCount; i++)
285 array[i] = new Rational(reader.getUInt32(tagValueOffset + (8 * i)), reader.getUInt32(tagValueOffset + 4 + (8 * i)));
286 handler.setRationalArray(tagId, array);
287 }
288 break;
289 case TiffDataFormat.CODE_SINGLE:
290 if (componentCount == 1) {
291 handler.setFloat(tagId, reader.getFloat32(tagValueOffset));
292 } else {
293 float[] array = new float[componentCount];
294 for (int i = 0; i < componentCount; i++)
295 array[i] = reader.getFloat32(tagValueOffset + (i * 4));
296 handler.setFloatArray(tagId, array);
297 }
298 break;
299 case TiffDataFormat.CODE_DOUBLE:
300 if (componentCount == 1) {
301 handler.setDouble(tagId, reader.getDouble64(tagValueOffset));
302 } else {
303 double[] array = new double[componentCount];
304 for (int i = 0; i < componentCount; i++)
305 array[i] = reader.getDouble64(tagValueOffset + (i * 4));
306 handler.setDoubleArray(tagId, array);
307 }
308 break;
309 case TiffDataFormat.CODE_INT8_S:
310 if (componentCount == 1) {
311 handler.setInt8s(tagId, reader.getInt8(tagValueOffset));
312 } else {
313 byte[] array = new byte[componentCount];
314 for (int i = 0; i < componentCount; i++)
315 array[i] = reader.getInt8(tagValueOffset + i);
316 handler.setInt8sArray(tagId, array);
317 }
318 break;
319 case TiffDataFormat.CODE_INT8_U:
320 if (componentCount == 1) {
321 handler.setInt8u(tagId, reader.getUInt8(tagValueOffset));
322 } else {
323 short[] array = new short[componentCount];
324 for (int i = 0; i < componentCount; i++)
325 array[i] = reader.getUInt8(tagValueOffset + i);
326 handler.setInt8uArray(tagId, array);
327 }
328 break;
329 case TiffDataFormat.CODE_INT16_S:
330 if (componentCount == 1) {
331 handler.setInt16s(tagId, (int)reader.getInt16(tagValueOffset));
332 } else {
333 short[] array = new short[componentCount];
334 for (int i = 0; i < componentCount; i++)
335 array[i] = reader.getInt16(tagValueOffset + (i * 2));
336 handler.setInt16sArray(tagId, array);
337 }
338 break;
339 case TiffDataFormat.CODE_INT16_U:
340 if (componentCount == 1) {
341 handler.setInt16u(tagId, reader.getUInt16(tagValueOffset));
342 } else {
343 int[] array = new int[componentCount];
344 for (int i = 0; i < componentCount; i++)
345 array[i] = reader.getUInt16(tagValueOffset + (i * 2));
346 handler.setInt16uArray(tagId, array);
347 }
348 break;
349 case TiffDataFormat.CODE_INT32_S:
350 // NOTE 'long' in this case means 32 bit, not 64
351 if (componentCount == 1) {
352 handler.setInt32s(tagId, reader.getInt32(tagValueOffset));
353 } else {
354 int[] array = new int[componentCount];
355 for (int i = 0; i < componentCount; i++)
356 array[i] = reader.getInt32(tagValueOffset + (i * 4));
357 handler.setInt32sArray(tagId, array);
358 }
359 break;
360 case TiffDataFormat.CODE_INT32_U:
361 // NOTE 'long' in this case means 32 bit, not 64
362 if (componentCount == 1) {
363 handler.setInt32u(tagId, reader.getUInt32(tagValueOffset));
364 } else {
365 long[] array = new long[componentCount];
366 for (int i = 0; i < componentCount; i++)
367 array[i] = reader.getUInt32(tagValueOffset + (i * 4));
368 handler.setInt32uArray(tagId, array);
369 }
370 break;
371 default:
372 handler.error(String.format("Invalid TIFF tag format code %d for tag 0x%04X", formatCode, tagId));
373 }
374 }
375
376 /**
377 * Determine the offset of a given tag within the specified IFD.
378 *
379 * @param ifdStartOffset the offset at which the IFD starts
380 * @param entryNumber the zero-based entry number
381 */
382 private static int calculateTagOffset(int ifdStartOffset, int entryNumber)
383 {
384 // Add 2 bytes for the tag count.
385 // Each entry is 12 bytes.
386 return ifdStartOffset + 2 + (12 * entryNumber);
387 }
388}
Note: See TracBrowser for help on using the repository browser.