Example #1
0
 public abstract bool MatchesCriteria(Dimensions dimensions);
        public Dimensions ReadDimensions(Stream byteStream)
        {
            byteStream.Seek(0, SeekOrigin.Begin);

            List <HashNode <FileType> > relevantHashNodes = new List <HashNode <FileType> >();

            while (InternalFileType == FileType.UNKNOWN)
            {
                int readByte = byteStream.ReadByte();

                if (readByte == -1)
                {
                    throw new HeaderNotFoundException("Header not found in stream.");
                }

                HashNode <FileType> hashNode;
                for (int i = 0; i < relevantHashNodes.Count; i++)
                {
                    hashNode = relevantHashNodes[i].GetFirstKey((byte)readByte);
                    if (hashNode != null)
                    {
                        if (hashNode.HasValue)
                        {
                            InternalFileType = hashNode.Value;
                            break;
                        }
                        else
                        {
                            relevantHashNodes[i] = hashNode;
                        }
                    }
                    else
                    {
                        relevantHashNodes.RemoveAt(i);
                        i--;
                    }
                }
                hashNode = HeaderMap.GetFirstKey((byte)readByte);
                if (hashNode != null)
                {
                    relevantHashNodes.Add(hashNode);
                }
            }

            Dimensions dimensions = Dimensions.None;

            switch (InternalFileType)
            {
            case FileType.PNG:
                dimensions = ReadPngDimensions(byteStream);
                break;

            case FileType.JPG:
                dimensions = ReadJpgDimensions(byteStream);
                break;

            default:
                Console.WriteLine("Found currently unsupported filetype");
                break;
            }

            EndOfMetadataIndex = byteStream.Position;

            if (byteStream.ReadByte() == -1)
            {
                throw new HeaderNotFoundException("Header only partially present in stream.");
            }

            if (dimensions.Width < 0 || dimensions.Height < 0)
            {
                Console.WriteLine("Bad negative value found - Width: " + dimensions.Width + " Height: " + dimensions.Height);
            }

            return(dimensions);
        }