示例#1
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);
        }
示例#2
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);
        }