Exemplo n.º 1
0
        public static async Task <ImageFileDirectory> LoadFromStream(Stream stream, bool isLitteleEndian)
        {
            var ifd          = new ImageFileDirectory();
            var binaryReader = new BinaryTools.BinaryReader(stream, isLitteleEndian);

            var count = await binaryReader.ReadUInt16Async();

            for (int i = 0; i < count; ++i)
            {
                var ifdEntry = await IFDEntry.LoadFromStream(stream, isLitteleEndian);

                ifd.IFDEntries.Add(ifdEntry);
            }

            ifd.OffsetOfNextIFD = await binaryReader.ReadUInt32Async();

            return(ifd);
        }
Exemplo n.º 2
0
        public static async Task <ImageFileHeader> LoadFromStream(Stream stream)
        {
            var ifh = new ImageFileHeader();

            var TIFFBOM = new byte[2];
            await stream.ReadAsync(TIFFBOM, 0, 2);

            var BOMValue = System.Text.Encoding.ASCII.GetString(TIFFBOM);

            ifh.IsLittleEndian = BOMValue == "II";

            var binaryReader = new BinaryTools.BinaryReader(stream, ifh.IsLittleEndian);
            await binaryReader.ReadUInt16Async(); // dispose 42

            ifh.OffsetOf1stIFD = await binaryReader.ReadUInt32Async();

            return(ifh);
        }
Exemplo n.º 3
0
        public static async Task <IFDEntry> LoadFromStream(Stream stream, bool isLittleEndian)
        {
            var entryBegin = stream.Position;

            var binaryReader = new BinaryTools.BinaryReader(stream, isLittleEndian);

            var tag = await binaryReader.ReadUInt16Async();

            var fieldType = (FieldTypeEnum)await binaryReader.ReadUInt16Async();

            var count = await binaryReader.ReadUInt32Async();

            var offset = 0U;

            var valueLength = GetValuesLength(fieldType, count);

            if (valueLength > 4)
            {
                offset = await binaryReader.ReadUInt32Async();

                stream.Position = offset;
            }

            IFDEntry ifdEntry;

            switch (fieldType)
            {
            case FieldTypeEnum.BYTE:
            {
                ifdEntry = new BYTEEntry();
                var array = new byte[count];

                for (int i = 0; i < count; ++i)
                {
                    array[i] = await binaryReader.ReadByteAsync();
                }
                (ifdEntry as BYTEEntry).Values = array;
            }
            break;

            case FieldTypeEnum.SHORT:
            {
                ifdEntry = new SHORTEntry();
                var array = new ushort[count];
                for (int i = 0; i < count; ++i)
                {
                    array[i] = await binaryReader.ReadUInt16Async();
                }
                (ifdEntry as SHORTEntry).Values = array;
            }
            break;

            case FieldTypeEnum.LONG:
            {
                ifdEntry = new LONGEntry();
                var array = new uint[count];
                for (int i = 0; i < count; ++i)
                {
                    array[i] = await binaryReader.ReadUInt32Async();
                }
                (ifdEntry as LONGEntry).Values = array;
            }
            break;

            case FieldTypeEnum.SBYTE:
            {
                ifdEntry = new SBYTEEntry();
                var array = new sbyte[count];
                for (int i = 0; i < count; ++i)
                {
                    array[i] = await binaryReader.ReadSByteAsync();
                }
                (ifdEntry as SBYTEEntry).Values = array;
            }
            break;

            case FieldTypeEnum.SSHORT:
            {
                ifdEntry = new SSHORTEntry();
                var array = new short[count];
                for (int i = 0; i < count; ++i)
                {
                    array[i] = await binaryReader.ReadInt16Async();
                }
                (ifdEntry as SSHORTEntry).Values = array;
            }
            break;

            case FieldTypeEnum.SLONG:
            {
                ifdEntry = new SLONGEntry();
                var array = new int[count];
                for (int i = 0; i < count; ++i)
                {
                    array[i] = await binaryReader.ReadInt32Async();
                }
                (ifdEntry as SLONGEntry).Values = array;
            }
            break;

            case FieldTypeEnum.FLOAT:
            {
                ifdEntry = new FLOATEntry();
                var array = new float[count];
                for (int i = 0; i < count; ++i)
                {
                    array[i] = await binaryReader.ReadSingleAsync();
                }
                (ifdEntry as FLOATEntry).Values = array;
            }
            break;

            case FieldTypeEnum.DOUBLE:
            {
                ifdEntry = new DOUBLEEntry();
                var array = new double[count];
                for (int i = 0; i < count; ++i)
                {
                    array[i] = await binaryReader.ReadDoubleAsync();
                }
                (ifdEntry as DOUBLEEntry).Values = array;
            }
            break;

            case FieldTypeEnum.ASCII:
            {
                ifdEntry = new ASCIIEntry();
                var data = new byte[count];

                var read = await binaryReader.ReadAsync(data, 0, data.Length);

                if (read != data.Length)
                {
                    throw new Exception("Can not read enough data.");
                }

                var all     = System.Text.Encoding.ASCII.GetString(data, 0, data.Length);
                var strings = all.Split(new char[] { '\0' });

                (ifdEntry as ASCIIEntry).Values = strings.Take(strings.Length - 1).ToArray();
            }
            break;

            case FieldTypeEnum.UNDEFINED:
            {
                ifdEntry = new UNDEFINEDEntry();
                var data = new byte[count];

                var read = await binaryReader.ReadAsync(data, 0, data.Length);

                if (read != data.Length)
                {
                    throw new Exception("Can not read enough data.");
                }

                (ifdEntry as UNDEFINEDEntry).Value = data;
            }
            break;

            case FieldTypeEnum.RATIONAL:
            {
                ifdEntry = new RATIONALEntry();
                var first  = new uint[count];
                var second = new uint[count];

                for (int i = 0; i < count; ++i)
                {
                    first[i] = await binaryReader.ReadUInt32Async();

                    second[i] = await binaryReader.ReadUInt32Async();
                }

                (ifdEntry as RATIONALEntry).First  = first;
                (ifdEntry as RATIONALEntry).Second = second;
            }
            break;

            case FieldTypeEnum.SRATIONAL:
            {
                ifdEntry = new SRATIONALEntry();
                var first  = new int[count];
                var second = new int[count];

                for (int i = 0; i < count; ++i)
                {
                    first[i] = await binaryReader.ReadInt32Async();

                    second[i] = await binaryReader.ReadInt32Async();
                }

                (ifdEntry as SRATIONALEntry).First  = first;
                (ifdEntry as SRATIONALEntry).Second = second;
            }
            break;

            default:
                throw new Exception("Specified fieldType is not defined in TIFF 6.0 and not supported.");
            }

            stream.Position = entryBegin + 12;

            ifdEntry.Tag         = tag;
            ifdEntry.FieldType   = fieldType;
            ifdEntry.Count       = count;
            ifdEntry.ValueOffset = offset;

            return(ifdEntry);
        }