/// <summary>Processes a TIFF data sequence.</summary> /// <param name="reader">the <see cref="IndexedReader"/> from which the data should be read</param> /// <param name="handler">the <see cref="ITiffHandler"/> that will coordinate processing and accept read values</param> /// <exception cref="TiffProcessingException">if an error occurred during the processing of TIFF data that could not be ignored or recovered from</exception> /// <exception cref="System.IO.IOException">an error occurred while accessing the required data</exception> /// <exception cref="TiffProcessingException"/> public static void ProcessTiff(IndexedReader reader, ITiffHandler handler) { // Read byte order. var byteOrder = reader.GetInt16(0); reader = byteOrder switch { 0x4d4d => reader.WithByteOrder(isMotorolaByteOrder: true), 0x4949 => reader.WithByteOrder(isMotorolaByteOrder: false), _ => throw new TiffProcessingException("Unclear distinction between Motorola/Intel byte ordering: " + reader.GetInt16(0)), }; // Check the next two values for correctness. int tiffMarker = reader.GetUInt16(2); handler.SetTiffMarker(tiffMarker); var firstIfdOffset = reader.GetInt32(4); // David Ekholm sent a digital camera image that has this problem // TODO calling Length should be avoided as it causes IndexedCapturingReader to read to the end of the stream if (firstIfdOffset >= reader.Length - 1) { handler.Warn("First IFD offset is beyond the end of the TIFF data segment -- trying default offset"); // First directory normally starts immediately after the offset bytes, so try that firstIfdOffset = 2 + 2 + 4; } var processedIfdOffsets = new HashSet <int>(); ProcessIfd(handler, reader, processedIfdOffsets, firstIfdOffset); }
/// <summary>Processes a TIFF data sequence.</summary> /// <param name="reader">the <see cref="IndexedReader"/> from which the data should be read</param> /// <param name="handler">the <see cref="ITiffHandler"/> that will coordinate processing and accept read values</param> /// <param name="tiffHeaderOffset">the offset within <c>reader</c> at which the TIFF header starts</param> /// <exception cref="TiffProcessingException">if an error occurred during the processing of TIFF data that could not be ignored or recovered from</exception> /// <exception cref="System.IO.IOException">an error occurred while accessing the required data</exception> /// <exception cref="TiffProcessingException"/> public static void ProcessTiff([NotNull] IndexedReader reader, [NotNull] ITiffHandler handler, int tiffHeaderOffset = 0) { // Read byte order. switch (reader.GetInt16(tiffHeaderOffset)) { case 0x4d4d: // MM reader.IsMotorolaByteOrder = true; break; case 0x4949: // II reader.IsMotorolaByteOrder = false; break; default: throw new TiffProcessingException("Unclear distinction between Motorola/Intel byte ordering: " + reader.GetInt16(tiffHeaderOffset)); } // Check the next two values for correctness. int tiffMarker = reader.GetUInt16(2 + tiffHeaderOffset); handler.SetTiffMarker(tiffMarker); var firstIfdOffset = reader.GetInt32(4 + tiffHeaderOffset) + tiffHeaderOffset; // David Ekholm sent a digital camera image that has this problem // TODO calling Length should be avoided as it causes IndexedCapturingReader to read to the end of the stream if (firstIfdOffset >= reader.Length - 1) { handler.Warn("First IFD offset is beyond the end of the TIFF data segment -- trying default offset"); // First directory normally starts immediately after the offset bytes, so try that firstIfdOffset = tiffHeaderOffset + 2 + 2 + 4; } var processedIfdOffsets = new HashSet <int>(); ProcessIfd(handler, reader, processedIfdOffsets, firstIfdOffset, tiffHeaderOffset); handler.Completed(reader, tiffHeaderOffset); }