Beispiel #1
0
        public static Tif Load(string path)
        {
            Tif tif = Load(File.ReadAllBytes(path));

            tif.Source = path;
            return(tif);
        }
Beispiel #2
0
        public static Tif Load(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, (int)stream.Length - 1);
            Tif tif = Load(bytes);

            tif.Source = stream.ToString();
            return(tif);
        }
Beispiel #3
0
        internal static Page Read(byte[] data, int pos, bool flip)
        {
            var    page = new Page();
            ushort tagCount;

            tagCount = BitConverter.ToUInt16(Tif.GetBytes(data, pos, 2, flip), 0);
            if (tagCount == 0)
            {
                return(null);
            }

            pos += 2;
            for (int i = 0; i < tagCount && pos < data.Length; i++)
            {
                try
                {
                    var field = Field.Read(data, pos, flip);
                    if (field != null)
                    {
                        if (field.IsIfdField())
                        {
                            var subIfd = Ifd.ReadSubIfd(field, data, flip);
                            if (subIfd != null)
                            {
                                page.SubIfds.Add(subIfd);
                            }
                        }

                        page.Add(field);
                    }
                }
                catch (OutOfMemoryException)
                {
                    //The field data is f****d
                }

                pos += 12;
            }

            page.NextIfdAddress = BitConverter.ToUInt32(Tif.GetBytes(data, pos, 4, flip), 0);

            var(offsetField, countField) = GetOffsetAndCount(page);

            page.ImageData = offsetField != null
                ? GetImageData(data, offsetField, countField)
                : new List <byte[]>();

            return(page);
        }
Beispiel #4
0
        internal static Tag Read(byte[] data, int startPosition)
        {
            var tag = new Tag
            {
                TagType     = (TagType)BitConverter.ToUInt16(data, startPosition),
                FieldType   = (FieldType)BitConverter.ToUInt16(data, startPosition + 2),
                ValueCount  = BitConverter.ToUInt32(data, startPosition + 4),
                ValueOffset = BitConverter.ToUInt32(data, startPosition + 8),
            };

            byte[] valuebytes = GetValueBytes(data, tag);
            tag.Values = Tif.ReadValues(valuebytes, tag.FieldType, (int)tag.ValueCount);

            return(tag);
        }
Beispiel #5
0
        internal static Tag ReadMM(byte[] data, int startPosition)
        {
            var tag = new Tag
            {
                TagType    = (TagType)(ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(data, startPosition)),
                FieldType  = (FieldType)(ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(data, startPosition + 2)),
                ValueCount = (uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, startPosition + 4)),
            };

            tag.ValueOffset = GetValueOffset(data, startPosition, tag.ValueCount, tag.FieldType);

            byte[] valuebytes = Tif.SwitchEndian(GetValueBytes(data, tag), Tif.ValueLength[tag.FieldType]);

            tag.Values = Tif.ReadValues(valuebytes, tag.FieldType, (int)tag.ValueCount);

            return(tag);
        }
Beispiel #6
0
        private static Tif ReadPages(byte[] data, int pos, bool flip)
        {
            var  file = new Tif();
            Page page;

            do
            {
                page = Page.Read(data, pos, flip);
                if (page != null)
                {
                    file.Add(page);
                    pos = (int)page.NextIfdAddress;
                }
                else
                {
                    pos = 0;
                }
            }while (pos != 0 && pos < data.Length);
            return(file);
        }
Beispiel #7
0
        public static Tif Load(byte[] data)
        {
            Tif  tif;
            int  pos;
            bool flip = false;

            var byteOrder = (ByteOrder)BitConverter.ToUInt16(data, 0);

            switch (byteOrder)
            {
            case ByteOrder.LittleEndian:
                break;

            case ByteOrder.BigEndian:
                flip = true;
                break;

            default:
                throw new NotSupportedException("Not a TIF file");
            }

            ushort meaning = BitConverter.ToUInt16(Tif.GetBytes(data, 2, 2, flip), 0);

            if (meaning != 42)
            {
                throw new NotSupportedException("Not a TIF file");
            }

            pos = (int)BitConverter.ToUInt32(Tif.GetBytes(data, 4, 4, flip), 0);
            if (pos > data.Length)
            {
                throw new NotSupportedException("Not a TIF file");
            }

            tif           = ReadPages(data, pos, flip);
            tif.ByteOrder = byteOrder;
            tif.Source    = data.ToString();
            return(tif);
        }