private static async Task <uint[]> ReadIntegerArrayAsync_Internal(TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { switch (entry.Type) { case TiffType.Byte: { byte[] data = await entry.ReadDataAsync(stream, byteOrder); return(Enumerable.Range(0, entry.Count).Select(index => (uint)DataConverter.ToByte(data, index)).ToArray()); } case TiffType.Short: { byte[] data = await entry.ReadDataAsync(stream, byteOrder); return(Enumerable.Range(0, entry.Count).Select(index => (uint)DataConverter.ToUInt16(data, index * 2, byteOrder)).ToArray()); } case TiffType.Long: case TiffType.Ifd: { byte[] data = await entry.ReadDataAsync(stream, byteOrder); return(Enumerable.Range(0, entry.Count).Select(index => DataConverter.ToUInt32(data, index * 4, byteOrder)).ToArray()); } default: throw new InvalidOperationException(); } }
public static Task <string> ReadStringAsync(this TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { if (entry.Type != TiffType.Ascii) { throw new ImageFormatException($"A value of type '{entry.Type}' cannot be converted to a string."); } return(ReadStringAsync_Internal(entry, stream, byteOrder)); }
private static async Task <double[]> ReadDoubleArrayAsync_Internal(TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { byte[] data = await entry.ReadDataAsync(stream, byteOrder); return(Enumerable.Range(0, entry.Count).Select(index => { return DataConverter.ToDouble(data, index * 8, byteOrder); }).ToArray()); }
public static Task <double[]> ReadDoubleArrayAsync(this TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { if (entry.Type != TiffType.Double) { throw new ImageFormatException($"A value of type '{entry.Type}' cannot be converted to a double."); } return(ReadDoubleArrayAsync_Internal(entry, stream, byteOrder)); }
public static Task <uint[]> ReadIntegerArrayAsync(this TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { var type = entry.Type; if (type != TiffType.Byte && type != TiffType.Short && type != TiffType.Long) { throw new ImageFormatException($"A value of type '{entry.Type}' cannot be converted to an unsigned integer."); } return(ReadIntegerArrayAsync_Internal(entry, stream, byteOrder)); }
public static Task <TiffIfdReference[]> ReadIfdReferenceArrayAsync(this TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { var type = entry.Type; if (type != TiffType.Long && type != TiffType.Ifd) { throw new ImageFormatException($"A value of type '{entry.Type}' cannot be converted to an IFD reference."); } return(ReadIfdReferenceArrayAsync_Internal(entry, stream, byteOrder));; }
private static async Task <Rational[]> ReadRationalArrayAsync_Internal(TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { byte[] data = await entry.ReadDataAsync(stream, byteOrder); return(Enumerable.Range(0, entry.Count).Select(index => { var numerator = DataConverter.ToUInt32(data, index * 8, byteOrder); var denominator = DataConverter.ToUInt32(data, index * 8 + 4, byteOrder); return new Rational(numerator, denominator); }).ToArray()); }
private static async Task <string> ReadStringAsync_Internal(TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { byte[] data = await entry.ReadDataAsync(stream, byteOrder); if (data[data.Length - 1] != 0) { throw new ImageFormatException("The retrieved string is not null terminated."); } return(Encoding.ASCII.GetString(data, 0, data.Length - 1)); }
public static TiffIfdReference GetIfdReference(this TiffIfdEntry entry, ByteOrder byteOrder) { if (entry.Count != 1) { throw new ImageFormatException("Cannot read a single value from an array of multiple items."); } if (entry.Type != TiffType.Long && entry.Type != TiffType.Ifd) { throw new ImageFormatException($"A value of type '{entry.Type}' cannot be converted to an IFD reference."); } return(new TiffIfdReference(DataConverter.ToUInt32(entry.Value, 0, byteOrder))); }
public static Task <double> ReadDoubleAsync(this TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { if (entry.Type != TiffType.Double) { throw new ImageFormatException($"A value of type '{entry.Type}' cannot be converted to a double."); } if (entry.Count != 1) { throw new ImageFormatException("Cannot read a single value from an array of multiple items."); } return(ReadDoubleAsync_Internal(entry, stream, byteOrder)); }
public static Task <byte[]> ReadDataAsync(this TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { var sizeOfData = entry.SizeOfData(); if (sizeOfData <= 4) { return(Task.FromResult(entry.Value)); } else { var dataOffset = DataConverter.ToUInt32(entry.Value, 0, byteOrder); stream.Seek(dataOffset, SeekOrigin.Begin); return(stream.ReadBytesAsync(sizeOfData)); } }
public static async Task <TiffIfd> ReadIfdAsync(Stream stream, ByteOrder byteOrder) { int entryCount = await stream.ReadInt16Async(byteOrder); var entries = new TiffIfdEntry[entryCount]; var bytes = await stream.ReadBytesAsync(entryCount *SIZEOF_IFDENTRY); for (int i = 0; i < entryCount; i++) { entries[i] = ParseIfdEntry(bytes, i * SIZEOF_IFDENTRY, byteOrder); } var nextIfdOffset = await stream.ReadUInt32Async(byteOrder); var nextIfdReference = nextIfdOffset != 0 ? new TiffIfdReference(nextIfdOffset) : (TiffIfdReference?)null; return(new TiffIfd { Entries = entries, NextIfdReference = nextIfdReference }); }
public static int GetSignedInteger(this TiffIfdEntry entry, ByteOrder byteOrder) { if (entry.Count != 1) { throw new ImageFormatException("Cannot read a single value from an array of multiple items."); } switch (entry.Type) { case TiffType.SByte: return(DataConverter.ToSByte(entry.Value, 0)); case TiffType.SShort: return(DataConverter.ToInt16(entry.Value, 0, byteOrder)); case TiffType.SLong: return(DataConverter.ToInt32(entry.Value, 0, byteOrder)); default: throw new ImageFormatException($"A value of type '{entry.Type}' cannot be converted to a signed integer."); } }
public static int SizeOfIfdEntry(TiffIfdEntry entry) => SIZEOF_IFDENTRY;
public static int SizeOfData(this TiffIfdEntry entry) => TiffReader.SizeOfDataType(entry.Type) * entry.Count;
private static async Task <TiffIfdReference[]> ReadIfdReferenceArrayAsync_Internal(TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { byte[] data = await entry.ReadDataAsync(stream, byteOrder); return(Enumerable.Range(0, entry.Count).Select(index => new TiffIfdReference(DataConverter.ToUInt32(data, index * 4, byteOrder))).ToArray()); }
private static async Task <double> ReadDoubleAsync_Internal(TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { var array = await ReadDoubleArrayAsync_Internal(entry, stream, byteOrder); return(array[0]); }
private static async Task <SignedRational> ReadSignedRationalAsync_Internal(TiffIfdEntry entry, Stream stream, ByteOrder byteOrder) { var array = await ReadSignedRationalArrayAsync_Internal(entry, stream, byteOrder); return(array[0]); }