Пример #1
0
        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();
            }
        }
Пример #2
0
        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));
        }
Пример #3
0
        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());
        }
Пример #4
0
        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));
        }
Пример #5
0
        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));
        }
Пример #6
0
        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));;
        }
Пример #7
0
        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());
        }
Пример #8
0
        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));
        }
Пример #9
0
        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)));
        }
Пример #10
0
        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));
        }
Пример #11
0
        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));
            }
        }
Пример #12
0
        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
            });
        }
Пример #13
0
        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.");
            }
        }
Пример #14
0
 public static int SizeOfIfdEntry(TiffIfdEntry entry) => SIZEOF_IFDENTRY;
Пример #15
0
 public static int SizeOfData(this TiffIfdEntry entry) => TiffReader.SizeOfDataType(entry.Type) * entry.Count;
Пример #16
0
        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());
        }
Пример #17
0
        private static async Task <double> ReadDoubleAsync_Internal(TiffIfdEntry entry, Stream stream, ByteOrder byteOrder)
        {
            var array = await ReadDoubleArrayAsync_Internal(entry, stream, byteOrder);

            return(array[0]);
        }
Пример #18
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]);
        }