FluxJpeg.Core.Decoder.JpegDecoder.TryParseJFIF C# (CSharp) Method

TryParseJFIF() private method

Tries to parse the JFIF APP0 header See http://en.wikipedia.org/wiki/JFIF
private TryParseJFIF ( byte data ) : bool
data byte
return bool
        private bool TryParseJFIF(byte[] data)
        {
            IO.BinaryReader reader = new IO.BinaryReader(new MemoryStream(data));

            int length = data.Length + 2; // Data & length

            if (!(length >= JFIF_FIXED_LENGTH))
                return false;  // Header's too small.

            byte[] identifier = new byte[5];
            reader.Read(identifier, 0, identifier.Length);
            if (identifier[0] != JPEGMarker.JFIF_J
                || identifier[1] != JPEGMarker.JFIF_F
                || identifier[2] != JPEGMarker.JFIF_I
                || identifier[3] != JPEGMarker.JFIF_F
                || identifier[4] != JPEGMarker.X00)
                return false;  // Incorrect bytes

            majorVersion = reader.ReadByte();
            minorVersion = reader.ReadByte();
            if (majorVersion != MAJOR_VERSION
                || (majorVersion == MAJOR_VERSION
                    && minorVersion > MINOR_VERSION)) // changed from <
                return false; // Unsupported version

            Units = (UnitType)reader.ReadByte();
            if (Units != UnitType.None &&
                Units != UnitType.Inches &&
                Units != UnitType.Centimeters)
                return false; // Invalid units

            XDensity = reader.ReadShort();
            YDensity = reader.ReadShort();
            Xthumbnail = reader.ReadByte();
            Ythumbnail = reader.ReadByte();

            // 3 * for RGB data
            int thumbnailLength = 3 * Xthumbnail * Ythumbnail;
            if (length > JFIF_FIXED_LENGTH
                && thumbnailLength != length - JFIF_FIXED_LENGTH)
                return false; // Thumbnail fields invalid

            if (thumbnailLength > 0)
            {
                thumbnail = new byte[thumbnailLength];
                if (reader.Read(thumbnail, 0, thumbnailLength) != thumbnailLength)
                    return false; // Thumbnail data was missing!

            }

            return true;
        }