private void ReadLogicalScreenDescriptor() { byte[] buffer = new byte[7]; _stream.Read(buffer, 0, buffer.Length); byte packed = buffer[4]; _logicalScreenDescriptor = new GifLogicalScreenDescriptor(); _logicalScreenDescriptor.Width = BitConverter.ToInt16(buffer, 0); _logicalScreenDescriptor.Height = BitConverter.ToInt16(buffer, 2); _logicalScreenDescriptor.Background = buffer[5]; _logicalScreenDescriptor.GlobalColorTableFlag = ((packed & 0x80) >> 7) == 1; _logicalScreenDescriptor.GlobalColorTableSize = 2 << (packed & 0x07); }
/// <summary> /// Decodes the image from the specified stream and sets /// the data to image. /// </summary> /// <param name="image">The image, where the data should be set to. /// Cannot be null (Nothing in Visual Basic).</param> /// <param name="stream">The stream, where the image should be /// decoded from. Cannot be null (Nothing in Visual Basic).</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="image"/> is null (Nothing in Visual Basic).</para> /// <para>- or -</para> /// <para><paramref name="stream"/> is null (Nothing in Visual Basic).</para> /// </exception> public void Decode(ExtendedImage image, Stream stream) { try { _image = image; _stream = stream; _stream.Seek(6, SeekOrigin.Current); ReadLogicalScreenDescriptor(); if (_logicalScreenDescriptor.GlobalColorTableFlag == true) { _globalColorTable = new byte[_logicalScreenDescriptor.GlobalColorTableSize * 3]; // Read the global color table from the stream stream.Read(_globalColorTable, 0, _globalColorTable.Length); } int nextFlag = stream.ReadByte(); while (nextFlag != -1) { if (nextFlag == ImageLabel) { ReadFrame(); } else if (nextFlag == ExtensionIntroducer) { int gcl = stream.ReadByte(); switch (gcl) { case GraphicControlLabel: ReadGraphicalControlExtension(); break; case CommentLabel: ReadComments(); break; case ApplicationExtensionLabel: Skip(12); break; case PlainTextLabel: Skip(13); break; } } else if (nextFlag == EndIntroducer) { break; } nextFlag = stream.ReadByte(); } } catch (Exception e) { this._currentFrame = null; this._globalColorTable = null; this._graphicsControl = null; if (this._image != null) { this._image.UriSource = null; } this._image = null; this._logicalScreenDescriptor = null; this._stream = null; throw new Exception("Gif failed to decode"); } }