| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class TiffReader |
| 37 |
{ |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public void processTiff(@NotNull final RandomAccessReader reader, |
| 49 |
@NotNull final TiffHandler handler, |
| 50 |
final int tiffHeaderOffset) throws TiffProcessingException, IOException |
| 51 |
{ |
| 52 |
|
| 53 |
short byteOrderIdentifier = reader.getInt16(tiffHeaderOffset); |
| 54 |
|
| 55 |
if (byteOrderIdentifier == 0x4d4d) { |
| 56 |
reader.setMotorolaByteOrder(true); |
| 57 |
} else if (byteOrderIdentifier == 0x4949) { |
| 58 |
reader.setMotorolaByteOrder(false); |
| 59 |
} else { |
| 60 |
throw new TiffProcessingException("Unclear distinction between Motorola/Intel byte ordering: " + byteOrderIdentifier); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
final int tiffMarker = reader.getUInt16(2 + tiffHeaderOffset); |
| 65 |
handler.setTiffMarker(tiffMarker); |
| 66 |
|
| 67 |
int firstIfdOffset = reader.getInt32(4 + tiffHeaderOffset) + tiffHeaderOffset; |
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
|
| 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 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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 |
|
| 113 |
if (processedIfdOffsets.contains(Integer.valueOf(ifdOffset))) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 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 |
|
| 126 |
int dirTagCount = reader.getUInt16(ifdOffset); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
|
| 146 |
|
| 147 |
int invalidTiffFormatCodeCount = 0; |
| 148 |
for (int tagNumber = 0; tagNumber < dirTagCount; tagNumber++) { |
| 149 |
final int tagOffset = calculateTagOffset(ifdOffset, tagNumber); |
| 150 |
|
| 151 |
|
| 152 |
final int tagId = reader.getUInt16(tagOffset); |
| 153 |
|
| 154 |
|
| 155 |
final int formatCode = reader.getUInt16(tagOffset + 2); |
| 156 |
final TiffDataFormat format = TiffDataFormat.fromTiffFormatCode(formatCode); |
| 157 |
|
| 158 |
|
| 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 |
|
| 166 |
|
| 167 |
handler.error(String.format("Invalid TIFF tag format code %d for tag 0x%04X", formatCode, tagId)); |
| 168 |
|
| 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 |
|
| 183 |
final long offsetVal = reader.getUInt32(tagOffset + 8); |
| 184 |
if (offsetVal + byteCount > reader.getLength()) { |
| 185 |
|
| 186 |
handler.error("Illegal TIFF tag pointer offset"); |
| 187 |
continue; |
| 188 |
} |
| 189 |
tagValueOffset = tiffHeaderOffset + offsetVal; |
| 190 |
} else { |
| 191 |
|
| 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 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 220 |
if (!isIfdPointer && !handler.customProcessTag((int) tagValueOffset, processedIfdOffsets, tiffHeaderOffset, reader, tagId, (int) byteCount)) { |
| 221 |
|
| 222 |
processTag(handler, tagId, (int) tagValueOffset, (int) componentCount, formatCode, reader); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 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 |
|
| 233 |
|
| 234 |
return; |
| 235 |
} else if (nextIfdOffset < ifdOffset) { |
| 236 |
|
| 237 |
|
| 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 |
|
| 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 * 8)); |
| 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 |
|
| 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 |
|
| 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 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private static int calculateTagOffset(int ifdStartOffset, int entryNumber) |
| 381 |
{ |
| 382 |
|
| 383 |
|
| 384 |
return ifdStartOffset + 2 + (12 * entryNumber); |
| 385 |
} |
| 386 |
} |