Пример #1
0
        /// <summary>
        /// Reads GIF image from stream
        /// </summary>
        /// <param name="inputStream">
        /// Stream containing GIF file.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The supplied stream is null.
        /// </exception>
        private void ReadStream( Stream inputStream )
        {
            _frames = new Collection<GifFrame>();
            _applicationExtensions = new Collection<ApplicationExtension>();
            _gct = null;

            _readStreamCounterText = "Reading stream byte";
            AddCounter( _readStreamCounterText, (int) inputStream.Length );
            _header = new GifHeader( inputStream, XmlDebugging );
            MyProgressCounters[_readStreamCounterText].Value
                = (int) inputStream.Position;
            WriteDebugXmlNode( _header.DebugXmlReader );
            if( _header.ErrorState != ErrorState.Ok )
            {
                WriteDebugXmlFinish();
                return;
            }

            _lsd = new LogicalScreenDescriptor( inputStream, XmlDebugging );
            MyProgressCounters[_readStreamCounterText].Value
                = (int) inputStream.Position;
            WriteDebugXmlNode( _lsd.DebugXmlReader );
            if( TestState( ErrorState.EndOfInputStream ) )
            {
                WriteDebugXmlFinish();
                return;
            }

            if( _lsd.HasGlobalColourTable )
            {
                _gct = new ColourTable( inputStream,
                                        _lsd.GlobalColourTableSize,
                                        XmlDebugging );
                MyProgressCounters[_readStreamCounterText].Value
                    = (int) inputStream.Position;
                WriteDebugXmlNode( _gct.DebugXmlReader );
            }

            if( ConsolidatedState == ErrorState.Ok )
            {
                ReadContents( inputStream );
                MyProgressCounters[_readStreamCounterText].Value
                    = (int) inputStream.Position;
            }
            inputStream.Close();
            WriteDebugXmlFinish();
            RemoveCounter( _readStreamCounterText );
        }
        public void WikipediaExampleTest()
        {
            ReportStart();
            _e = new AnimatedGifEncoder();
            GifFrame frame = new GifFrame( WikipediaExample.ExpectedBitmap );
            frame.Delay = WikipediaExample.DelayTime;
            _e.AddFrame( frame );

            // TODO: some way of creating/testing a UseLocal version of WikipediaExample
            string fileName = "WikipediaExampleUseGlobal.gif";
            _e.WriteToFile( fileName );
            Stream s = File.OpenRead( fileName );

            int code;

            // check GIF header
            GifHeader gh = new GifHeader( s );
            Assert.AreEqual( ErrorState.Ok, gh.ConsolidatedState );

            // check logical screen descriptor
            LogicalScreenDescriptor lsd = new LogicalScreenDescriptor( s );
            Assert.AreEqual( ErrorState.Ok, lsd.ConsolidatedState );
            WikipediaExample.CheckLogicalScreenDescriptor( lsd );

            // read global colour table
            ColourTable gct
                = new ColourTable( s, WikipediaExample.GlobalColourTableSize );
            Assert.AreEqual( ErrorState.Ok, gct.ConsolidatedState );
            // cannot compare global colour table as different encoders will
            // produce difference colour tables.
            //			WikipediaExample.CheckGlobalColourTable( gct );

            // check for extension introducer
            code = ExampleComponent.CallRead( s );
            Assert.AreEqual( GifComponent.CodeExtensionIntroducer, code );

            // check for app extension label
            code = ExampleComponent.CallRead( s );
            Assert.AreEqual( GifComponent.CodeApplicationExtensionLabel, code );

            // check netscape extension
            ApplicationExtension ae = new ApplicationExtension( s );
            Assert.AreEqual( ErrorState.Ok, ae.ConsolidatedState );
            NetscapeExtension ne = new NetscapeExtension( ae );
            Assert.AreEqual( ErrorState.Ok, ne.ConsolidatedState );
            Assert.AreEqual( 0, ne.LoopCount );

            // check for extension introducer
            code = ExampleComponent.CallRead( s );
            Assert.AreEqual( GifComponent.CodeExtensionIntroducer, code );

            // check for gce label
            code = ExampleComponent.CallRead( s );
            Assert.AreEqual( GifComponent.CodeGraphicControlLabel, code );

            // check graphic control extension
            GraphicControlExtension gce = new GraphicControlExtension( s );
            Assert.AreEqual( ErrorState.Ok, gce.ConsolidatedState );
            WikipediaExample.CheckGraphicControlExtension( gce );

            // check for image separator
            code = ExampleComponent.CallRead( s );
            Assert.AreEqual( GifComponent.CodeImageSeparator, code );

            // check for image descriptor
            ImageDescriptor id = new ImageDescriptor( s );
            Assert.AreEqual( ErrorState.Ok, id.ConsolidatedState );
            WikipediaExample.CheckImageDescriptor( id );

            // read, decode and check image data
            // Cannot compare encoded LZW data directly as different encoders
            // will create different colour tables, so even if the bitmaps are
            // identical, the colour indices will be different
            int pixelCount = WikipediaExample.FrameSize.Width
                            * WikipediaExample.FrameSize.Height;
            TableBasedImageData tbid = new TableBasedImageData( s, pixelCount );
            for( int y = 0; y < WikipediaExample.LogicalScreenSize.Height; y++ )
            {
                for( int x = 0; x < WikipediaExample.LogicalScreenSize.Width; x++ )
                {
                    int i = (y * WikipediaExample.LogicalScreenSize.Width) + x;
                    Assert.AreEqual( WikipediaExample.ExpectedBitmap.GetPixel( x, y ),
                                     gct[tbid.Pixels[i]],
                                     "X: " + x + ", Y: " + y );
                }
            }

            // Check for block terminator after image data
            code = ExampleComponent.CallRead( s );
            Assert.AreEqual( 0x00, code );

            // check for GIF trailer
            code = ExampleComponent.CallRead( s );
            Assert.AreEqual( GifComponent.CodeTrailer, code );

            // check we're at the end of the stream
            code = ExampleComponent.CallRead( s );
            Assert.AreEqual( -1, code );
            s.Close();

            _d = new GifDecoder( fileName );
            _d.Decode();
            Assert.AreEqual( ErrorState.Ok, _d.ConsolidatedState );
            BitmapAssert.AreEqual( WikipediaExample.ExpectedBitmap,
                                  (Bitmap) _d.Frames[0].TheImage,
                                   "" );
            ReportEnd();
        }
 private static void CheckGifHeader( Stream s )
 {
     // check GIF header
     GifHeader gh = new GifHeader( s );
     Assert.AreEqual( ErrorState.Ok, gh.ConsolidatedState );
     Assert.AreEqual( "GIF", gh.Signature );
     Assert.AreEqual( "89a", gh.Version );
 }
Пример #4
0
		private static void WriteGifHeader( Stream outputStream )
		{
			GifHeader header = new GifHeader( "GIF", "89a" );
			header.WriteToStream( outputStream );
		}