예제 #1
0
        private GifFrame ReadImageData(BinaryReader reader)
        {
            var imageDescriptor = ImageDescriptor.Read(reader);
            var localPalette    = imageDescriptor.HasLocalPalette ? GifPalette.Read(reader, imageDescriptor.LocalPaletteColorCount) : null;

            var lzwInitialCodeSize = reader.ReadByte() + 1;

            var lzwBitReader = new GifLzwBitReader(new GifLzwByteReader(reader));

            var lzwCodeSize  = lzwInitialCodeSize;
            var nextIncValue = (2 << (lzwCodeSize - 1)) - 1;
            var decodeTable  = new GifDecodeTable(_sreenDescriptor.GlobalPaletteColorCount);

            int value;

#if SILVERLIGHT
            var writeableBmp = new WriteableBitmap(_sreenDescriptor.Width, _sreenDescriptor.Height);
#else
            var writeableBmp = new WriteableBitmap(_sreenDescriptor.Width, _sreenDescriptor.Height, 96.0, 96.0, PixelFormats.Bgra32, null);
#endif

            var bmpWriter = new BmpWriter(writeableBmp, _globalPalette);

            while (lzwBitReader.Read(lzwCodeSize, out value))
            {
                if (decodeTable.Count == nextIncValue)
                {
                    lzwCodeSize++;
                    nextIncValue = (2 << (lzwCodeSize - 1)) - 1;
                }

                bool reset;
                if (decodeTable.ProcessNextCode(value, bmpWriter, out reset) == false)
                {
                    break;
                }

                if (reset)
                {
                    lzwCodeSize  = lzwInitialCodeSize;
                    nextIncValue = (2 << (lzwCodeSize - 1)) - 1;
                }

                if (lzwCodeSize > 12)
                {
                    lzwCodeSize = 12;
                }
            }

            bmpWriter.Flush();

            return(new GifFrame(imageDescriptor, localPalette, writeableBmp));
        }
예제 #2
0
        public GifImageSource(Stream stream)
        {
            try
            {
                _gifStream = new MemoryStream();
                stream.CopyTo(_gifStream);
                _gifStream.Seek(0, SeekOrigin.Begin);

                using (var reader = new BinaryReader(_gifStream))
                {
                    var gifSign = new string(reader.ReadChars(6));

                    var gif89a = string.Equals(gifSign, "gif89a", StringComparison.OrdinalIgnoreCase);
                    var gif87a = string.Equals(gifSign, "gif87a", StringComparison.OrdinalIgnoreCase);

                    if ((gif87a || gif89a) == false)
                    {
                        return;
                    }

                    _sreenDescriptor = LogicalScreenDescriptor.Read(reader);
                    _globalPalette   = _sreenDescriptor.HasGlobalPalette ? GifPalette.Read(reader, _sreenDescriptor.GlobalPaletteColorCount) : null;

                    while (true)
                    {
                        var ch = reader.ReadChar();

                        if (ch == ';' && reader.BaseStream.Position == reader.BaseStream.Length)
                        {
                            break;
                        }

                        switch (ch)
                        {
                        case '!':
                            SkipExtension(reader);
                            break;

                        case ',':
                            _frames.Add(ReadImage(reader));
                            break;
                        }
                    }
                }

                _isValid = true;
            }
            catch (Exception)
            {
                _isValid = false;
            }
        }