public virtual void Extract(RandomAccessReader reader, Com.Drew.Metadata.Metadata metadata)
		{
			// TODO review whether the 'tagPtr' values below really do require ICC processing to work with a RandomAccessReader
			IccDirectory directory = metadata.GetOrCreateDirectory<IccDirectory>();
			try
			{
				directory.SetInt(IccDirectory.TagProfileByteCount, reader.GetInt32(IccDirectory.TagProfileByteCount));
				// For these tags, the int value of the tag is in fact it's offset within the buffer.
				Set4ByteString(directory, IccDirectory.TagCmmType, reader);
				SetInt32(directory, IccDirectory.TagProfileVersion, reader);
				Set4ByteString(directory, IccDirectory.TagProfileClass, reader);
				Set4ByteString(directory, IccDirectory.TagColorSpace, reader);
				Set4ByteString(directory, IccDirectory.TagProfileConnectionSpace, reader);
				SetDate(directory, IccDirectory.TagProfileDatetime, reader);
				Set4ByteString(directory, IccDirectory.TagSignature, reader);
				Set4ByteString(directory, IccDirectory.TagPlatform, reader);
				SetInt32(directory, IccDirectory.TagCmmFlags, reader);
				Set4ByteString(directory, IccDirectory.TagDeviceMake, reader);
				int temp = reader.GetInt32(IccDirectory.TagDeviceModel);
				if (temp != 0)
				{
					if (temp <= unchecked((int)(0x20202020)))
					{
						directory.SetInt(IccDirectory.TagDeviceModel, temp);
					}
					else
					{
						directory.SetString(IccDirectory.TagDeviceModel, GetStringFromInt32(temp));
					}
				}
				SetInt32(directory, IccDirectory.TagRenderingIntent, reader);
				SetInt64(directory, IccDirectory.TagDeviceAttr, reader);
				float[] xyz = new float[] { reader.GetS15Fixed16(IccDirectory.TagXyzValues), reader.GetS15Fixed16(IccDirectory.TagXyzValues + 4), reader.GetS15Fixed16(IccDirectory.TagXyzValues + 8) };
				directory.SetObject(IccDirectory.TagXyzValues, xyz);
				// Process 'ICC tags'
				int tagCount = reader.GetInt32(IccDirectory.TagTagCount);
				directory.SetInt(IccDirectory.TagTagCount, tagCount);
				for (int i = 0; i < tagCount; i++)
				{
					int pos = IccDirectory.TagTagCount + 4 + i * 12;
					int tagType = reader.GetInt32(pos);
					int tagPtr = reader.GetInt32(pos + 4);
					int tagLen = reader.GetInt32(pos + 8);
					sbyte[] b = reader.GetBytes(tagPtr, tagLen);
					directory.SetByteArray(tagType, b);
				}
			}
			catch (IOException ex)
			{
				directory.AddError("Exception reading ICC profile: " + ex.Message);
			}
		}
 public virtual void TestGetInt32_OutOfBounds()
 {
     try
     {
         RandomAccessReader reader = CreateReader(new sbyte[3]);
         reader.GetInt32(0);
         NUnit.Framework.Assert.Fail("Exception expected");
     }
     catch (IOException ex)
     {
         Sharpen.Tests.AreEqual("Attempt to read from beyond end of underlying data source (requested index: 0, requested count: 4, max index: 2)", ex.Message);
     }
 }
Beispiel #3
0
        public virtual void TestGetInt32()
        {
            Sharpen.Tests.AreEqual(-1, CreateReader(new sbyte[] { unchecked ((sbyte)0xff), unchecked ((sbyte)0xff), unchecked ((sbyte)0xff), unchecked ((sbyte)0xff) }).GetInt32(0));
            sbyte[]            buffer = new sbyte[] { unchecked ((int)(0x00)), unchecked ((int)(0x01)), unchecked ((sbyte)0x7F), unchecked ((sbyte)0xFF), unchecked ((int)(0x02)), unchecked ((int)(0x03)), unchecked ((int)(0x04)) };
            RandomAccessReader reader = CreateReader(buffer);

            Sharpen.Tests.AreEqual(unchecked ((int)(0x00017FFF)), reader.GetInt32(0));
            Sharpen.Tests.AreEqual(unchecked ((int)(0x017FFF02)), reader.GetInt32(1));
            Sharpen.Tests.AreEqual(unchecked ((int)(0x7FFF0203)), reader.GetInt32(2));
            Sharpen.Tests.AreEqual(unchecked ((int)(0xFF020304)), reader.GetInt32(3));
            reader.SetMotorolaByteOrder(false);
            Sharpen.Tests.AreEqual(unchecked ((int)(0xFF7F0100)), reader.GetInt32(0));
            Sharpen.Tests.AreEqual(unchecked ((int)(0x02FF7F01)), reader.GetInt32(1));
            Sharpen.Tests.AreEqual(unchecked ((int)(0x0302FF7F)), reader.GetInt32(2));
            Sharpen.Tests.AreEqual(unchecked ((int)(0x040302FF)), reader.GetInt32(3));
        }
		public virtual void Extract(RandomAccessReader reader, Com.Drew.Metadata.Metadata metadata)
		{
			PsdHeaderDirectory directory = metadata.GetOrCreateDirectory<PsdHeaderDirectory>();
			try
			{
				int signature = reader.GetInt32(0);
				if (signature != unchecked((int)(0x38425053)))
				{
					directory.AddError("Invalid PSD file signature");
					return;
				}
				int version = reader.GetUInt16(4);
				if (version != 1 && version != 2)
				{
					directory.AddError("Invalid PSD file version (must be 1 or 2)");
					return;
				}
				// 6 reserved bytes are skipped here.  They should be zero.
				int channelCount = reader.GetUInt16(12);
				directory.SetInt(PsdHeaderDirectory.TagChannelCount, channelCount);
				// even though this is probably an unsigned int, the max height in practice is 300,000
				int imageHeight = reader.GetInt32(14);
				directory.SetInt(PsdHeaderDirectory.TagImageHeight, imageHeight);
				// even though this is probably an unsigned int, the max width in practice is 300,000
				int imageWidth = reader.GetInt32(18);
				directory.SetInt(PsdHeaderDirectory.TagImageWidth, imageWidth);
				int bitsPerChannel = reader.GetUInt16(22);
				directory.SetInt(PsdHeaderDirectory.TagBitsPerChannel, bitsPerChannel);
				int colorMode = reader.GetUInt16(24);
				directory.SetInt(PsdHeaderDirectory.TagColorMode, colorMode);
			}
			catch (IOException)
			{
				directory.AddError("Unable to read PSD header");
			}
		}
        public virtual void TestGetUInt32()
        {
            Sharpen.Tests.AreEqual(4294967295L, CreateReader(new sbyte[] { unchecked ((sbyte)unchecked ((int)(0xff))), unchecked ((sbyte)unchecked ((int)(0xff))), unchecked ((sbyte)unchecked ((int)(0xff))), unchecked (
                                                                               (sbyte)unchecked ((int)(0xff))) }).GetUInt32(0));
            sbyte[] buffer            = new sbyte[] { unchecked ((int)(0x00)), unchecked ((int)(0x01)), unchecked ((sbyte)unchecked ((int)(0x7F))), unchecked ((sbyte)unchecked ((int)(0xFF))), unchecked ((int)(0x02)), unchecked ((int
                                                                                                                                                                                                                                     )(0x03)), unchecked ((int)(0x04)) };
            RandomAccessReader reader = CreateReader(buffer);

            Sharpen.Tests.AreEqual(unchecked ((long)(0x00017FFFL)), reader.GetUInt32(0));
            Sharpen.Tests.AreEqual(unchecked ((long)(0x017FFF02L)), reader.GetUInt32(1));
            Sharpen.Tests.AreEqual(unchecked ((long)(0x7FFF0203L)), reader.GetUInt32(2));
            Sharpen.Tests.AreEqual(unchecked ((long)(0xFF020304L)), reader.GetUInt32(3));
            reader.SetMotorolaByteOrder(false);
            Sharpen.Tests.AreEqual(4286513408L, reader.GetUInt32(0));
            Sharpen.Tests.AreEqual(unchecked ((long)(0x02FF7F01L)), reader.GetUInt32(1));
            Sharpen.Tests.AreEqual(unchecked ((long)(0x0302FF7FL)), reader.GetUInt32(2));
            Sharpen.Tests.AreEqual(unchecked ((long)(0x040302FFL)), reader.GetInt32(3));
        }
		/// <summary>Processes a TIFF data sequence.</summary>
		/// <param name="reader">
		/// the
		/// <see cref="Com.Drew.Lang.RandomAccessReader"/>
		/// from which the data should be read
		/// </param>
		/// <param name="handler">
		/// the
		/// <see cref="TiffHandler"/>
		/// that will coordinate processing and accept read values
		/// </param>
		/// <param name="tiffHeaderOffset">the offset within <code>reader</code> 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="Com.Drew.Imaging.Tiff.TiffProcessingException"/>
		public virtual void ProcessTiff(RandomAccessReader reader, TiffHandler handler, int tiffHeaderOffset)
		{
			// This must be either "MM" or "II".
			short byteOrderIdentifier = reader.GetInt16(tiffHeaderOffset);
			if (byteOrderIdentifier == unchecked((int)(0x4d4d)))
			{
				// "MM"
				reader.SetMotorolaByteOrder(true);
			}
			else
			{
				if (byteOrderIdentifier == unchecked((int)(0x4949)))
				{
					// "II"
					reader.SetMotorolaByteOrder(false);
				}
				else
				{
					throw new TiffProcessingException("Unclear distinction between Motorola/Intel byte ordering: " + byteOrderIdentifier);
				}
			}
			// Check the next two values for correctness.
			int tiffMarker = reader.GetUInt16(2 + tiffHeaderOffset);
			handler.SetTiffMarker(tiffMarker);
			int firstIfdOffset = reader.GetInt32(4 + tiffHeaderOffset) + tiffHeaderOffset;
			// David Ekholm sent a digital camera image that has this problem
			// TODO getLength should be avoided as it causes RandomAccessStreamReader to read to the end of the stream
			if (firstIfdOffset >= reader.GetLength() - 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;
			}
			ICollection<int> processedIfdOffsets = new HashSet<int>();
			ProcessIfd(handler, reader, processedIfdOffsets, firstIfdOffset, tiffHeaderOffset);
			handler.Completed(reader, tiffHeaderOffset);
		}
		public virtual void Extract(RandomAccessReader reader, Com.Drew.Metadata.Metadata metadata)
		{
			PhotoshopDirectory directory = metadata.GetOrCreateDirectory<PhotoshopDirectory>();
			int pos;
			try
			{
				pos = reader.GetString(0, 13).Equals("Photoshop 3.0") ? 14 : 0;
			}
			catch (IOException)
			{
				directory.AddError("Unable to read header");
				return;
			}
			long length;
			try
			{
				length = reader.GetLength();
			}
			catch (IOException e)
			{
				directory.AddError("Unable to read Photoshop data: " + e.Message);
				return;
			}
			while (pos < length)
			{
				try
				{
					// 4 bytes for the signature.  Should always be "8BIM".
					//String signature = new String(data, pos, 4);
					pos += 4;
					// 2 bytes for the resource identifier (tag type).
					int tagType = reader.GetUInt16(pos);
					// segment type
					pos += 2;
					// A variable number of bytes holding a pascal string (two leading bytes for length).
					int descriptionLength = reader.GetUInt16(pos);
					pos += 2;
					// Some basic bounds checking
					if (descriptionLength < 0 || descriptionLength + pos > length)
					{
						return;
					}
					//String description = new String(data, pos, descriptionLength);
					pos += descriptionLength;
					// The number of bytes is padded with a trailing zero, if needed, to make the size even.
					if (pos % 2 != 0)
					{
						pos++;
					}
					// 4 bytes for the size of the resource data that follows.
					int byteCount = reader.GetInt32(pos);
					pos += 4;
					// The resource data.
					sbyte[] tagBytes = reader.GetBytes(pos, byteCount);
					pos += byteCount;
					// The number of bytes is padded with a trailing zero, if needed, to make the size even.
					if (pos % 2 != 0)
					{
						pos++;
					}
					directory.SetByteArray(tagType, tagBytes);
					// TODO allow rebasing the reader with a new zero-point, rather than copying data here
					if (tagType == PhotoshopDirectory.TagIptc)
					{
						new IptcReader().Extract(new SequentialByteArrayReader(tagBytes), metadata, tagBytes.Length);
					}
					if (tagType >= unchecked((int)(0x0fa0)) && tagType <= unchecked((int)(0x1387)))
					{
						PhotoshopDirectory._tagNameMap.Put(tagType, Sharpen.Extensions.StringFormat("Plug-in %d Data", tagType - unchecked((int)(0x0fa0)) + 1));
					}
				}
				catch (IOException ex)
				{
					directory.AddError(ex.Message);
					return;
				}
			}
		}
		private static void ProcessTag(Com.Drew.Metadata.Directory directory, int tagType, int tagValueOffset, int componentCount, int formatCode, RandomAccessReader reader)
		{
			switch (formatCode)
			{
				case FmtUndefined:
				{
					// Directory simply stores raw values
					// The display side uses a Descriptor class per directory to turn the raw values into 'pretty' descriptions
					// this includes exif user comments
					directory.SetByteArray(tagType, reader.GetBytes(tagValueOffset, componentCount));
					break;
				}

				case FmtString:
				{
					string @string = reader.GetNullTerminatedString(tagValueOffset, componentCount);
					directory.SetString(tagType, @string);
					break;
				}

				case FmtSrational:
				{
					if (componentCount == 1)
					{
						directory.SetRational(tagType, new Rational(reader.GetInt32(tagValueOffset), reader.GetInt32(tagValueOffset + 4)));
					}
					else
					{
						if (componentCount > 1)
						{
							Rational[] rationals = new Rational[componentCount];
							for (int i = 0; i < componentCount; i++)
							{
								rationals[i] = new Rational(reader.GetInt32(tagValueOffset + (8 * i)), reader.GetInt32(tagValueOffset + 4 + (8 * i)));
							}
							directory.SetRationalArray(tagType, rationals);
						}
					}
					break;
				}

				case FmtUrational:
				{
					if (componentCount == 1)
					{
						directory.SetRational(tagType, new Rational(reader.GetUInt32(tagValueOffset), reader.GetUInt32(tagValueOffset + 4)));
					}
					else
					{
						if (componentCount > 1)
						{
							Rational[] rationals = new Rational[componentCount];
							for (int i = 0; i < componentCount; i++)
							{
								rationals[i] = new Rational(reader.GetUInt32(tagValueOffset + (8 * i)), reader.GetUInt32(tagValueOffset + 4 + (8 * i)));
							}
							directory.SetRationalArray(tagType, rationals);
						}
					}
					break;
				}

				case FmtSingle:
				{
					if (componentCount == 1)
					{
						directory.SetFloat(tagType, reader.GetFloat32(tagValueOffset));
					}
					else
					{
						float[] floats = new float[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							floats[i] = reader.GetFloat32(tagValueOffset + (i * 4));
						}
						directory.SetFloatArray(tagType, floats);
					}
					break;
				}

				case FmtDouble:
				{
					if (componentCount == 1)
					{
						directory.SetDouble(tagType, reader.GetDouble64(tagValueOffset));
					}
					else
					{
						double[] doubles = new double[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							doubles[i] = reader.GetDouble64(tagValueOffset + (i * 4));
						}
						directory.SetDoubleArray(tagType, doubles);
					}
					break;
				}

				case FmtSbyte:
				{
					//
					// Note that all integral types are stored as int32 internally (the largest supported by TIFF)
					//
					if (componentCount == 1)
					{
						directory.SetInt(tagType, reader.GetInt8(tagValueOffset));
					}
					else
					{
						int[] bytes = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							bytes[i] = reader.GetInt8(tagValueOffset + i);
						}
						directory.SetIntArray(tagType, bytes);
					}
					break;
				}

				case FmtByte:
				{
					if (componentCount == 1)
					{
						directory.SetInt(tagType, reader.GetUInt8(tagValueOffset));
					}
					else
					{
						int[] bytes = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							bytes[i] = reader.GetUInt8(tagValueOffset + i);
						}
						directory.SetIntArray(tagType, bytes);
					}
					break;
				}

				case FmtUshort:
				{
					if (componentCount == 1)
					{
						int i = reader.GetUInt16(tagValueOffset);
						directory.SetInt(tagType, i);
					}
					else
					{
						int[] ints = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							ints[i] = reader.GetUInt16(tagValueOffset + (i * 2));
						}
						directory.SetIntArray(tagType, ints);
					}
					break;
				}

				case FmtSshort:
				{
					if (componentCount == 1)
					{
						int i = reader.GetInt16(tagValueOffset);
						directory.SetInt(tagType, i);
					}
					else
					{
						int[] ints = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							ints[i] = reader.GetInt16(tagValueOffset + (i * 2));
						}
						directory.SetIntArray(tagType, ints);
					}
					break;
				}

				case FmtSlong:
				case FmtUlong:
				{
					// NOTE 'long' in this case means 32 bit, not 64
					if (componentCount == 1)
					{
						int i = reader.GetInt32(tagValueOffset);
						directory.SetInt(tagType, i);
					}
					else
					{
						int[] ints = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							ints[i] = reader.GetInt32(tagValueOffset + (i * 4));
						}
						directory.SetIntArray(tagType, ints);
					}
					break;
				}

				default:
				{
					directory.AddError("Unknown format code " + formatCode + " for tag " + tagType);
					break;
				}
			}
		}
		private static void ProcessMakernote(int makernoteOffset, ICollection<int> processedIfdOffsets, int tiffHeaderOffset, Com.Drew.Metadata.Metadata metadata, RandomAccessReader reader)
		{
			// Determine the camera model and makernote format
			Com.Drew.Metadata.Directory ifd0Directory = metadata.GetDirectory<ExifIFD0Directory>();
			if (ifd0Directory == null)
			{
				return;
			}
			string cameraMake = ifd0Directory.GetString(ExifIFD0Directory.TagMake);
			string firstThreeChars = reader.GetString(makernoteOffset, 3);
			string firstFourChars = reader.GetString(makernoteOffset, 4);
			string firstFiveChars = reader.GetString(makernoteOffset, 5);
			string firstSixChars = reader.GetString(makernoteOffset, 6);
			string firstSevenChars = reader.GetString(makernoteOffset, 7);
			string firstEightChars = reader.GetString(makernoteOffset, 8);
			string firstTwelveChars = reader.GetString(makernoteOffset, 12);
			bool byteOrderBefore = reader.IsMotorolaByteOrder();
			if ("OLYMP".Equals(firstFiveChars) || "EPSON".Equals(firstFiveChars) || "AGFA".Equals(firstFourChars))
			{
				// Olympus Makernote
				// Epson and Agfa use Olympus makernote standard: http://www.ozhiker.com/electronics/pjmt/jpeg_info/
				ProcessIFD(metadata.GetOrCreateDirectory<OlympusMakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 8, tiffHeaderOffset, metadata, reader);
			}
			else
			{
				if (cameraMake != null && Sharpen.Extensions.Trim(cameraMake).ToUpper().StartsWith("NIKON"))
				{
					if ("Nikon".Equals(firstFiveChars))
					{
						switch (reader.GetUInt8(makernoteOffset + 6))
						{
							case 1:
							{
								/* There are two scenarios here:
                 * Type 1:                  **
                 * :0000: 4E 69 6B 6F 6E 00 01 00-05 00 02 00 02 00 06 00 Nikon...........
                 * :0010: 00 00 EC 02 00 00 03 00-03 00 01 00 00 00 06 00 ................
                 * Type 3:                  **
                 * :0000: 4E 69 6B 6F 6E 00 02 00-00 00 4D 4D 00 2A 00 00 Nikon....MM.*...
                 * :0010: 00 08 00 1E 00 01 00 07-00 00 00 04 30 32 30 30 ............0200
                 */
								ProcessIFD(metadata.GetOrCreateDirectory<NikonType1MakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 8, tiffHeaderOffset, metadata, reader);
								break;
							}

							case 2:
							{
								ProcessIFD(metadata.GetOrCreateDirectory<NikonType2MakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 18, makernoteOffset + 10, metadata, reader);
								break;
							}

							default:
							{
								ifd0Directory.AddError("Unsupported Nikon makernote data ignored.");
								break;
							}
						}
					}
					else
					{
						// The IFD begins with the first Makernote byte (no ASCII name).  This occurs with CoolPix 775, E990 and D1 models.
						ProcessIFD(metadata.GetOrCreateDirectory<NikonType2MakernoteDirectory>(), processedIfdOffsets, makernoteOffset, tiffHeaderOffset, metadata, reader);
					}
				}
				else
				{
					if ("SONY CAM".Equals(firstEightChars) || "SONY DSC".Equals(firstEightChars))
					{
						ProcessIFD(metadata.GetOrCreateDirectory<SonyType1MakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 12, tiffHeaderOffset, metadata, reader);
					}
					else
					{
						if ("SEMC MS\u0000\u0000\u0000\u0000\u0000".Equals(firstTwelveChars))
						{
							// force MM for this directory
							reader.SetMotorolaByteOrder(true);
							// skip 12 byte header + 2 for "MM" + 6
							ProcessIFD(metadata.GetOrCreateDirectory<SonyType6MakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 20, tiffHeaderOffset, metadata, reader);
						}
						else
						{
							if ("SIGMA\u0000\u0000\u0000".Equals(firstEightChars) || "FOVEON\u0000\u0000".Equals(firstEightChars))
							{
								ProcessIFD(metadata.GetOrCreateDirectory<SigmaMakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 10, tiffHeaderOffset, metadata, reader);
							}
							else
							{
								if ("KDK".Equals(firstThreeChars))
								{
									reader.SetMotorolaByteOrder(firstSevenChars.Equals("KDK INFO"));
									ProcessKodakMakernote(metadata.GetOrCreateDirectory<KodakMakernoteDirectory>(), makernoteOffset, reader);
								}
								else
								{
									if (Sharpen.Runtime.EqualsIgnoreCase("Canon", cameraMake))
									{
										ProcessIFD(metadata.GetOrCreateDirectory<CanonMakernoteDirectory>(), processedIfdOffsets, makernoteOffset, tiffHeaderOffset, metadata, reader);
									}
									else
									{
										if (cameraMake != null && cameraMake.ToUpper().StartsWith("CASIO"))
										{
											if ("QVC\u0000\u0000\u0000".Equals(firstSixChars))
											{
												ProcessIFD(metadata.GetOrCreateDirectory<CasioType2MakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 6, tiffHeaderOffset, metadata, reader);
											}
											else
											{
												ProcessIFD(metadata.GetOrCreateDirectory<CasioType1MakernoteDirectory>(), processedIfdOffsets, makernoteOffset, tiffHeaderOffset, metadata, reader);
											}
										}
										else
										{
											if ("FUJIFILM".Equals(firstEightChars) || Sharpen.Runtime.EqualsIgnoreCase("Fujifilm", cameraMake))
											{
												// Note that this also applies to certain Leica cameras, such as the Digilux-4.3
												reader.SetMotorolaByteOrder(false);
												// the 4 bytes after "FUJIFILM" in the makernote point to the start of the makernote
												// IFD, though the offset is relative to the start of the makernote, not the TIFF
												// header (like everywhere else)
												int ifdStart = makernoteOffset + reader.GetInt32(makernoteOffset + 8);
												ProcessIFD(metadata.GetOrCreateDirectory<FujifilmMakernoteDirectory>(), processedIfdOffsets, ifdStart, makernoteOffset, metadata, reader);
											}
											else
											{
												if (cameraMake != null && cameraMake.ToUpper().StartsWith("MINOLTA"))
												{
													// Cases seen with the model starting with MINOLTA in capitals seem to have a valid Olympus makernote
													// area that commences immediately.
													ProcessIFD(metadata.GetOrCreateDirectory<OlympusMakernoteDirectory>(), processedIfdOffsets, makernoteOffset, tiffHeaderOffset, metadata, reader);
												}
												else
												{
													if ("KYOCERA".Equals(firstSevenChars))
													{
														// http://www.ozhiker.com/electronics/pjmt/jpeg_info/kyocera_mn.html
														ProcessIFD(metadata.GetOrCreateDirectory<KyoceraMakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 22, tiffHeaderOffset, metadata, reader);
													}
													else
													{
														if ("LEICA".Equals(firstFiveChars))
														{
															reader.SetMotorolaByteOrder(false);
															if ("Leica Camera AG".Equals(cameraMake))
															{
																ProcessIFD(metadata.GetOrCreateDirectory<LeicaMakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 8, tiffHeaderOffset, metadata, reader);
															}
															else
															{
																if ("LEICA".Equals(cameraMake))
																{
																	// Some Leica cameras use Panasonic makernote tags
																	ProcessIFD(metadata.GetOrCreateDirectory<PanasonicMakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 8, tiffHeaderOffset, metadata, reader);
																}
															}
														}
														else
														{
															if ("Panasonic\u0000\u0000\u0000".Equals(reader.GetString(makernoteOffset, 12)))
															{
																// NON-Standard TIFF IFD Data using Panasonic Tags. There is no Next-IFD pointer after the IFD
																// Offsets are relative to the start of the TIFF header at the beginning of the EXIF segment
																// more information here: http://www.ozhiker.com/electronics/pjmt/jpeg_info/panasonic_mn.html
																ProcessIFD(metadata.GetOrCreateDirectory<PanasonicMakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 12, tiffHeaderOffset, metadata, reader);
															}
															else
															{
																if ("AOC\u0000".Equals(firstFourChars))
																{
																	// NON-Standard TIFF IFD Data using Casio Type 2 Tags
																	// IFD has no Next-IFD pointer at end of IFD, and
																	// Offsets are relative to the start of the current IFD tag, not the TIFF header
																	// Observed for:
																	// - Pentax ist D
																	ProcessIFD(metadata.GetOrCreateDirectory<CasioType2MakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 6, makernoteOffset, metadata, reader);
																}
																else
																{
																	if (cameraMake != null && (cameraMake.ToUpper().StartsWith("PENTAX") || cameraMake.ToUpper().StartsWith("ASAHI")))
																	{
																		// NON-Standard TIFF IFD Data using Pentax Tags
																		// IFD has no Next-IFD pointer at end of IFD, and
																		// Offsets are relative to the start of the current IFD tag, not the TIFF header
																		// Observed for:
																		// - PENTAX Optio 330
																		// - PENTAX Optio 430
																		ProcessIFD(metadata.GetOrCreateDirectory<PentaxMakernoteDirectory>(), processedIfdOffsets, makernoteOffset, makernoteOffset, metadata, reader);
																	}
																	else
																	{
																		//        } else if ("KC".equals(firstTwoChars) || "MINOL".equals(firstFiveChars) || "MLY".equals(firstThreeChars) || "+M+M+M+M".equals(firstEightChars)) {
																		//            // This Konica data is not understood.  Header identified in accordance with information at this site:
																		//            // http://www.ozhiker.com/electronics/pjmt/jpeg_info/minolta_mn.html
																		//            // TODO add support for minolta/konica cameras
																		//            exifDirectory.addError("Unsupported Konica/Minolta data ignored.");
																		if ("SANYO\x0\x1\x0".Equals(firstEightChars))
																		{
																			ProcessIFD(metadata.GetOrCreateDirectory<SanyoMakernoteDirectory>(), processedIfdOffsets, makernoteOffset + 8, makernoteOffset, metadata, reader);
																		}
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			// The makernote is not comprehended by this library.
			// If you are reading this and believe a particular camera's image should be processed, get in touch.
			reader.SetMotorolaByteOrder(byteOrderBefore);
		}
		private static void ProcessIFD(Com.Drew.Metadata.Directory directory, ICollection<int> processedIfdOffsets, int ifdOffset, int tiffHeaderOffset, Com.Drew.Metadata.Metadata metadata, RandomAccessReader reader)
		{
			// check for directories we've already visited to avoid stack overflows when recursive/cyclic directory structures exist
			if (processedIfdOffsets.Contains(Sharpen.Extensions.ValueOf(ifdOffset)))
			{
				return;
			}
			// remember that we've visited this directory so that we don't visit it again later
			processedIfdOffsets.Add(ifdOffset);
			if (ifdOffset >= reader.GetLength() || ifdOffset < 0)
			{
				directory.AddError("Ignored IFD marked to start outside data segment");
				return;
			}
			// First two bytes in the IFD are the number of tags in this directory
			int dirTagCount = reader.GetUInt16(ifdOffset);
			int dirLength = (2 + (12 * dirTagCount) + 4);
			if (dirLength + ifdOffset > reader.GetLength())
			{
				directory.AddError("Illegally sized IFD");
				return;
			}
			// Handle each tag in this directory
			for (int tagNumber = 0; tagNumber < dirTagCount; tagNumber++)
			{
				int tagOffset = CalculateTagOffset(ifdOffset, tagNumber);
				// 2 bytes for the tag type
				int tagType = reader.GetUInt16(tagOffset);
				// 2 bytes for the format code
				int formatCode = reader.GetUInt16(tagOffset + 2);
				if (formatCode < 1 || formatCode > MaxFormatCode)
				{
					// This error suggests that we are processing at an incorrect index and will generate
					// rubbish until we go out of bounds (which may be a while).  Exit now.
					directory.AddError("Invalid TIFF tag format code: " + formatCode);
					return;
				}
				// 4 bytes dictate the number of components in this tag's data
				int componentCount = reader.GetInt32(tagOffset + 4);
				if (componentCount < 0)
				{
					directory.AddError("Negative TIFF tag component count");
					continue;
				}
				// each component may have more than one byte... calculate the total number of bytes
				int byteCount = componentCount * BytesPerFormat[formatCode];
				int tagValueOffset;
				if (byteCount > 4)
				{
					// If it's bigger than 4 bytes, the dir entry contains an offset.
					// dirEntryOffset must be passed, as some makernote implementations (e.g. Fujifilm) incorrectly use an
					// offset relative to the start of the makernote itself, not the TIFF segment.
					int offsetVal = reader.GetInt32(tagOffset + 8);
					if (offsetVal + byteCount > reader.GetLength())
					{
						// Bogus pointer offset and / or byteCount value
						directory.AddError("Illegal TIFF tag pointer offset");
						continue;
					}
					tagValueOffset = tiffHeaderOffset + offsetVal;
				}
				else
				{
					// 4 bytes or less and value is in the dir entry itself
					tagValueOffset = tagOffset + 8;
				}
				if (tagValueOffset < 0 || tagValueOffset > reader.GetLength())
				{
					directory.AddError("Illegal TIFF tag pointer offset");
					continue;
				}
				// Check that this tag isn't going to allocate outside the bounds of the data array.
				// This addresses an uncommon OutOfMemoryError.
				if (byteCount < 0 || tagValueOffset + byteCount > reader.GetLength())
				{
					directory.AddError("Illegal number of bytes for TIFF tag data: " + byteCount);
					continue;
				}
				//
				// Special handling for certain known tags that point to or contain other chunks of data to be processed
				//
				if (tagType == ExifIFD0Directory.TagExifSubIfdOffset && directory is ExifIFD0Directory)
				{
					if (byteCount != 4)
					{
						directory.AddError("Exif SubIFD Offset tag should have a component count of four (bytes) for the offset.");
					}
					else
					{
						int subDirOffset = tiffHeaderOffset + reader.GetInt32(tagValueOffset);
						ProcessIFD(metadata.GetOrCreateDirectory<ExifSubIFDDirectory>(), processedIfdOffsets, subDirOffset, tiffHeaderOffset, metadata, reader);
					}
				}
				else
				{
					if (tagType == ExifSubIFDDirectory.TagInteropOffset && directory is ExifSubIFDDirectory)
					{
						if (byteCount != 4)
						{
							directory.AddError("Exif Interop Offset tag should have a component count of four (bytes) for the offset.");
						}
						else
						{
							int subDirOffset = tiffHeaderOffset + reader.GetInt32(tagValueOffset);
							ProcessIFD(metadata.GetOrCreateDirectory<ExifInteropDirectory>(), processedIfdOffsets, subDirOffset, tiffHeaderOffset, metadata, reader);
						}
					}
					else
					{
						if (tagType == ExifIFD0Directory.TagGpsInfoOffset && directory is ExifIFD0Directory)
						{
							if (byteCount != 4)
							{
								directory.AddError("Exif GPS Info Offset tag should have a component count of four (bytes) for the offset.");
							}
							else
							{
								int subDirOffset = tiffHeaderOffset + reader.GetInt32(tagValueOffset);
								ProcessIFD(metadata.GetOrCreateDirectory<GpsDirectory>(), processedIfdOffsets, subDirOffset, tiffHeaderOffset, metadata, reader);
							}
						}
						else
						{
							if (tagType == ExifSubIFDDirectory.TagMakernote && directory is ExifSubIFDDirectory)
							{
								// The makernote tag contains the encoded makernote data directly.
								// Pass the offset to this tag's value. Manufacturer/Model-specific logic will be used to
								// determine the correct offset for further processing.
								ProcessMakernote(tagValueOffset, processedIfdOffsets, tiffHeaderOffset, metadata, reader);
							}
							else
							{
								ProcessTag(directory, tagType, tagValueOffset, componentCount, formatCode, reader);
							}
						}
					}
				}
			}
			// at the end of each IFD is an optional link to the next IFD
			int finalTagOffset = CalculateTagOffset(ifdOffset, dirTagCount);
			int nextDirectoryOffset = reader.GetInt32(finalTagOffset);
			if (nextDirectoryOffset != 0)
			{
				nextDirectoryOffset += tiffHeaderOffset;
				if (nextDirectoryOffset >= reader.GetLength())
				{
					// Last 4 bytes of IFD reference another IFD with an address that is out of bounds
					// Note this could have been caused by jhead 1.3 cropping too much
					return;
				}
				else
				{
					if (nextDirectoryOffset < ifdOffset)
					{
						// Last 4 bytes of IFD reference another IFD with an address that is before the start of this directory
						return;
					}
				}
				// TODO in Exif, the only known 'follower' IFD is the thumbnail one, however this may not be the case
				ExifThumbnailDirectory nextDirectory = metadata.GetOrCreateDirectory<ExifThumbnailDirectory>();
				ProcessIFD(nextDirectory, processedIfdOffsets, nextDirectoryOffset, tiffHeaderOffset, metadata, reader);
			}
		}
		private static void ExtractTiff(RandomAccessReader reader, Com.Drew.Metadata.Metadata metadata, Com.Drew.Metadata.Directory firstDirectory, int tiffHeaderOffset)
		{
			// this should be either "MM" or "II"
			string byteOrderIdentifier = reader.GetString(tiffHeaderOffset, 2);
			if ("MM".Equals(byteOrderIdentifier))
			{
				reader.SetMotorolaByteOrder(true);
			}
			else
			{
				if ("II".Equals(byteOrderIdentifier))
				{
					reader.SetMotorolaByteOrder(false);
				}
				else
				{
					firstDirectory.AddError("Unclear distinction between Motorola/Intel byte ordering: " + byteOrderIdentifier);
					return;
				}
			}
			// Check the next two values for correctness.
			int tiffMarker = reader.GetUInt16(2 + tiffHeaderOffset);
			int standardTiffMarker = unchecked((int)(0x002A));
			int olympusRawTiffMarker = unchecked((int)(0x4F52));
			// for ORF files
			int panasonicRawTiffMarker = unchecked((int)(0x0055));
			// for RW2 files
			if (tiffMarker != standardTiffMarker && tiffMarker != olympusRawTiffMarker && tiffMarker != panasonicRawTiffMarker)
			{
				firstDirectory.AddError("Unexpected TIFF marker after byte order identifier: 0x" + Sharpen.Extensions.ToHexString(tiffMarker));
				return;
			}
			int firstIfdOffset = reader.GetInt32(4 + tiffHeaderOffset) + tiffHeaderOffset;
			// David Ekholm sent a digital camera image that has this problem
			// TODO getLength should be avoided as it causes RandomAccessStreamReader to read to the end of the stream
			if (firstIfdOffset >= reader.GetLength() - 1)
			{
				firstDirectory.AddError("First Exif directory offset is beyond end of Exif data segment");
				// First directory normally starts 14 bytes in -- try it here and catch another error in the worst case
				firstIfdOffset = 14;
			}
			ICollection<int> processedIfdOffsets = new HashSet<int>();
			ProcessIFD(firstDirectory, processedIfdOffsets, firstIfdOffset, tiffHeaderOffset, metadata, reader);
			// after the extraction process, if we have the correct tags, we may be able to store thumbnail information
			ExifThumbnailDirectory thumbnailDirectory = metadata.GetDirectory<ExifThumbnailDirectory>();
			if (thumbnailDirectory != null && thumbnailDirectory.ContainsTag(ExifThumbnailDirectory.TagThumbnailCompression))
			{
				int? offset = thumbnailDirectory.GetInteger(ExifThumbnailDirectory.TagThumbnailOffset);
				int? length = thumbnailDirectory.GetInteger(ExifThumbnailDirectory.TagThumbnailLength);
				if (offset != null && length != null)
				{
					try
					{
						sbyte[] thumbnailData = reader.GetBytes(tiffHeaderOffset + offset.Value, length.Value);
						thumbnailDirectory.SetThumbnailData(thumbnailData);
					}
					catch (IOException ex)
					{
						firstDirectory.AddError("Invalid thumbnail data specification: " + ex.Message);
					}
				}
			}
		}
		/// <exception cref="System.IO.IOException"/>
		private static void ProcessTag(TiffHandler handler, int tagId, int tagValueOffset, int componentCount, int formatCode, RandomAccessReader reader)
		{
			switch (formatCode)
			{
				case TiffDataFormat.CodeUndefined:
				{
					// this includes exif user comments
					handler.SetByteArray(tagId, reader.GetBytes(tagValueOffset, componentCount));
					break;
				}

				case TiffDataFormat.CodeString:
				{
					handler.SetString(tagId, reader.GetNullTerminatedString(tagValueOffset, componentCount));
					break;
				}

				case TiffDataFormat.CodeRationalS:
				{
					if (componentCount == 1)
					{
						handler.SetRational(tagId, new Rational(reader.GetInt32(tagValueOffset), reader.GetInt32(tagValueOffset + 4)));
					}
					else
					{
						if (componentCount > 1)
						{
							Rational[] array = new Rational[componentCount];
							for (int i = 0; i < componentCount; i++)
							{
								array[i] = new Rational(reader.GetInt32(tagValueOffset + (8 * i)), reader.GetInt32(tagValueOffset + 4 + (8 * i)));
							}
							handler.SetRationalArray(tagId, array);
						}
					}
					break;
				}

				case TiffDataFormat.CodeRationalU:
				{
					if (componentCount == 1)
					{
						handler.SetRational(tagId, new Rational(reader.GetUInt32(tagValueOffset), reader.GetUInt32(tagValueOffset + 4)));
					}
					else
					{
						if (componentCount > 1)
						{
							Rational[] array = new Rational[componentCount];
							for (int i = 0; i < componentCount; i++)
							{
								array[i] = new Rational(reader.GetUInt32(tagValueOffset + (8 * i)), reader.GetUInt32(tagValueOffset + 4 + (8 * i)));
							}
							handler.SetRationalArray(tagId, array);
						}
					}
					break;
				}

				case TiffDataFormat.CodeSingle:
				{
					if (componentCount == 1)
					{
						handler.SetFloat(tagId, reader.GetFloat32(tagValueOffset));
					}
					else
					{
						float[] array = new float[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							array[i] = reader.GetFloat32(tagValueOffset + (i * 4));
						}
						handler.SetFloatArray(tagId, array);
					}
					break;
				}

				case TiffDataFormat.CodeDouble:
				{
					if (componentCount == 1)
					{
						handler.SetDouble(tagId, reader.GetDouble64(tagValueOffset));
					}
					else
					{
						double[] array = new double[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							array[i] = reader.GetDouble64(tagValueOffset + (i * 4));
						}
						handler.SetDoubleArray(tagId, array);
					}
					break;
				}

				case TiffDataFormat.CodeInt8S:
				{
					if (componentCount == 1)
					{
						handler.SetInt8s(tagId, reader.GetInt8(tagValueOffset));
					}
					else
					{
						sbyte[] array = new sbyte[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							array[i] = reader.GetInt8(tagValueOffset + i);
						}
						handler.SetInt8sArray(tagId, array);
					}
					break;
				}

				case TiffDataFormat.CodeInt8U:
				{
					if (componentCount == 1)
					{
						handler.SetInt8u(tagId, reader.GetUInt8(tagValueOffset));
					}
					else
					{
						short[] array = new short[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							array[i] = reader.GetUInt8(tagValueOffset + i);
						}
						handler.SetInt8uArray(tagId, array);
					}
					break;
				}

				case TiffDataFormat.CodeInt16S:
				{
					if (componentCount == 1)
					{
						handler.SetInt16s(tagId, (int)reader.GetInt16(tagValueOffset));
					}
					else
					{
						short[] array = new short[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							array[i] = reader.GetInt16(tagValueOffset + (i * 2));
						}
						handler.SetInt16sArray(tagId, array);
					}
					break;
				}

				case TiffDataFormat.CodeInt16U:
				{
					if (componentCount == 1)
					{
						handler.SetInt16u(tagId, reader.GetUInt16(tagValueOffset));
					}
					else
					{
						int[] array = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							array[i] = reader.GetUInt16(tagValueOffset + (i * 2));
						}
						handler.SetInt16uArray(tagId, array);
					}
					break;
				}

				case TiffDataFormat.CodeInt32S:
				{
					// NOTE 'long' in this case means 32 bit, not 64
					if (componentCount == 1)
					{
						handler.SetInt32s(tagId, reader.GetInt32(tagValueOffset));
					}
					else
					{
						int[] array = new int[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							array[i] = reader.GetInt32(tagValueOffset + (i * 4));
						}
						handler.SetInt32sArray(tagId, array);
					}
					break;
				}

				case TiffDataFormat.CodeInt32U:
				{
					// NOTE 'long' in this case means 32 bit, not 64
					if (componentCount == 1)
					{
						handler.SetInt32u(tagId, reader.GetUInt32(tagValueOffset));
					}
					else
					{
						long[] array = new long[componentCount];
						for (int i = 0; i < componentCount; i++)
						{
							array[i] = reader.GetUInt32(tagValueOffset + (i * 4));
						}
						handler.SetInt32uArray(tagId, array);
					}
					break;
				}

				default:
				{
					handler.Error(Sharpen.Extensions.StringFormat("Unknown format code %d for tag %d", formatCode, tagId));
					break;
				}
			}
		}
		/// <summary>Processes a TIFF IFD.</summary>
		/// <remarks>
		/// Processes a TIFF IFD.
		/// <p/>
		/// IFD Header:
		/// <ul>
		/// <li><b>2 bytes</b> number of tags</li>
		/// </ul>
		/// Tag structure:
		/// <ul>
		/// <li><b>2 bytes</b> tag type</li>
		/// <li><b>2 bytes</b> format code (values 1 to 12, inclusive)</li>
		/// <li><b>4 bytes</b> component count</li>
		/// <li><b>4 bytes</b> inline value, or offset pointer if too large to fit in four bytes</li>
		/// </ul>
		/// </remarks>
		/// <param name="handler">
		/// the
		/// <see cref="TiffHandler"/>
		/// that will coordinate processing and accept read values
		/// </param>
		/// <param name="reader">
		/// the
		/// <see cref="Com.Drew.Lang.RandomAccessReader"/>
		/// from which the data should be read
		/// </param>
		/// <param name="processedIfdOffsets">the set of visited IFD offsets, to avoid revisiting the same IFD in an endless loop</param>
		/// <param name="ifdOffset">the offset within <code>reader</code> at which the IFD data starts</param>
		/// <param name="tiffHeaderOffset">the offset within <code>reader</code> at which the TIFF header starts</param>
		/// <exception cref="System.IO.IOException">an error occurred while accessing the required data</exception>
		public static void ProcessIfd(TiffHandler handler, RandomAccessReader reader, ICollection<int> processedIfdOffsets, int ifdOffset, int tiffHeaderOffset)
		{
			try
			{
				// check for directories we've already visited to avoid stack overflows when recursive/cyclic directory structures exist
				if (processedIfdOffsets.Contains(Sharpen.Extensions.ValueOf(ifdOffset)))
				{
					return;
				}
				// remember that we've visited this directory so that we don't visit it again later
				processedIfdOffsets.Add(ifdOffset);
				if (ifdOffset >= reader.GetLength() || ifdOffset < 0)
				{
					handler.Error("Ignored IFD marked to start outside data segment");
					return;
				}
				// First two bytes in the IFD are the number of tags in this directory
				int dirTagCount = reader.GetUInt16(ifdOffset);
				int dirLength = (2 + (12 * dirTagCount) + 4);
				if (dirLength + ifdOffset > reader.GetLength())
				{
					handler.Error("Illegally sized IFD");
					return;
				}
				//
				// Handle each tag in this directory
				//
				for (int tagNumber = 0; tagNumber < dirTagCount; tagNumber++)
				{
					int tagOffset = CalculateTagOffset(ifdOffset, tagNumber);
					// 2 bytes for the tag id
					int tagId = reader.GetUInt16(tagOffset);
					// 2 bytes for the format code
					int formatCode = reader.GetUInt16(tagOffset + 2);
					TiffDataFormat format = TiffDataFormat.FromTiffFormatCode(formatCode);
					if (format == null)
					{
						// This error suggests that we are processing at an incorrect index and will generate
						// rubbish until we go out of bounds (which may be a while).  Exit now.
						handler.Error("Invalid TIFF tag format code: " + formatCode);
						return;
					}
					// 4 bytes dictate the number of components in this tag's data
					int componentCount = reader.GetInt32(tagOffset + 4);
					if (componentCount < 0)
					{
						handler.Error("Negative TIFF tag component count");
						continue;
					}
					int byteCount = componentCount * format.GetComponentSizeBytes();
					int tagValueOffset;
					if (byteCount > 4)
					{
						// If it's bigger than 4 bytes, the dir entry contains an offset.
						int offsetVal = reader.GetInt32(tagOffset + 8);
						if (offsetVal + byteCount > reader.GetLength())
						{
							// Bogus pointer offset and / or byteCount value
							handler.Error("Illegal TIFF tag pointer offset");
							continue;
						}
						tagValueOffset = tiffHeaderOffset + offsetVal;
					}
					else
					{
						// 4 bytes or less and value is in the dir entry itself.
						tagValueOffset = tagOffset + 8;
					}
					if (tagValueOffset < 0 || tagValueOffset > reader.GetLength())
					{
						handler.Error("Illegal TIFF tag pointer offset");
						continue;
					}
					// Check that this tag isn't going to allocate outside the bounds of the data array.
					// This addresses an uncommon OutOfMemoryError.
					if (byteCount < 0 || tagValueOffset + byteCount > reader.GetLength())
					{
						handler.Error("Illegal number of bytes for TIFF tag data: " + byteCount);
						continue;
					}
					//
					// Special handling for tags that point to other IFDs
					//
					if (byteCount == 4 && handler.IsTagIfdPointer(tagId))
					{
						int subDirOffset = tiffHeaderOffset + reader.GetInt32(tagValueOffset);
						ProcessIfd(handler, reader, processedIfdOffsets, subDirOffset, tiffHeaderOffset);
					}
					else
					{
						if (!handler.CustomProcessTag(tagValueOffset, processedIfdOffsets, tiffHeaderOffset, reader, tagId, byteCount))
						{
							ProcessTag(handler, tagId, tagValueOffset, componentCount, formatCode, reader);
						}
					}
				}
				// at the end of each IFD is an optional link to the next IFD
				int finalTagOffset = CalculateTagOffset(ifdOffset, dirTagCount);
				int nextIfdOffset = reader.GetInt32(finalTagOffset);
				if (nextIfdOffset != 0)
				{
					nextIfdOffset += tiffHeaderOffset;
					if (nextIfdOffset >= reader.GetLength())
					{
						// Last 4 bytes of IFD reference another IFD with an address that is out of bounds
						// Note this could have been caused by jhead 1.3 cropping too much
						return;
					}
					else
					{
						if (nextIfdOffset < ifdOffset)
						{
							// TODO is this a valid restriction?
							// Last 4 bytes of IFD reference another IFD with an address that is before the start of this directory
							return;
						}
					}
					if (handler.HasFollowerIfd())
					{
						ProcessIfd(handler, reader, processedIfdOffsets, nextIfdOffset, tiffHeaderOffset);
					}
				}
			}
			finally
			{
				handler.EndingIFD();
			}
		}
		/// <exception cref="System.IO.IOException"/>
		private void SetInt32(Com.Drew.Metadata.Directory directory, int tagType, RandomAccessReader reader)
		{
			int i = reader.GetInt32(tagType);
			if (i != 0)
			{
				directory.SetInt(tagType, i);
			}
		}
		/// <exception cref="System.IO.IOException"/>
		private void Set4ByteString(Com.Drew.Metadata.Directory directory, int tagType, RandomAccessReader reader)
		{
			int i = reader.GetInt32(tagType);
			if (i != 0)
			{
				directory.SetString(tagType, GetStringFromInt32(i));
			}
		}