Пример #1
0
        private void CheckConstructorStream(Stream s,
                                            string expectedSignature,
                                            string expectedVersion,
                                            ErrorState expectedErrorState,
                                            string expectedErrorMessage,
                                            string expectedXml)
        {
            // Save our current position in the stream, in case we want to go
            // back and read it again later.
            long streamPosition = s.Position;

            // Without XML debugging
            _header = new GifHeader(s);
            CheckProperties(expectedSignature,
                            expectedVersion,
                            expectedErrorState,
                            expectedErrorMessage);
            string message
                = "<Message>There is no DebugXml because XML debugging has not "
                  + "been enabled for this GifHeader instance.</Message>";

            Assert.AreEqual(message, _header.DebugXml);

            // Go back to where we were in the stream before we try reading it again
            s.Seek(streamPosition, SeekOrigin.Begin);

            // With XML debugging
            _header = new GifHeader(s, true);
            CheckProperties(expectedSignature,
                            expectedVersion,
                            expectedErrorState,
                            expectedErrorMessage);
            Assert.AreEqual(expectedXml, _header.DebugXml, "DebugXml");
        }
Пример #2
0
    public static GifHeader ReadHeader(Stream stream)
    {
        var header = new GifHeader();

        header.Read(stream);
        return(header);
    }
Пример #3
0
        /// <summary>
        /// Decodes the supplied GIF stream.
        /// </summary>
        public void Decode()
        {
            _frameDelays           = new List <int>();
            _loadedFrames          = new Queue <GifFrame>();
            _frames                = new Collection <GifFrame>();
            _applicationExtensions = new Collection <ApplicationExtension>();
            _gct = null;

            _header = new GifHeader(_stream);
            if (_header.ErrorState != ErrorState.Ok)
            {
                return;
            }

            _lsd = new LogicalScreenDescriptor(_stream);
            if (TestState(ErrorState.EndOfInputStream))
            {
                return;
            }

            if (_lsd.HasGlobalColourTable)
            {
                _gct = new ColourTable(_stream, _lsd.GlobalColourTableSize);
            }

            if (ConsolidatedState == ErrorState.Ok)
            {
                ReadContents(_stream);
            }

            ApplyMemoryFields();
        }
Пример #4
0
        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);
        }
Пример #5
0
        public void ConstructorTestBadSignature()
        {
            ReportStart();

            _header = new GifHeader("FIG", "89A");
            Assert.AreEqual("FIG", _header.Signature);
            Assert.AreEqual("89A", _header.Version);
            Assert.AreEqual(ErrorState.BadSignature, _header.ErrorState);

            ReportEnd();
        }
Пример #6
0
        public void ConstructorPropertiesTest()
        {
            ReportStart();

            _header = new GifHeader("GIF", "89A");
            Assert.AreEqual("GIF", _header.Signature);
            Assert.AreEqual("89A", _header.Version);
            Assert.AreEqual(ErrorState.Ok, _header.ConsolidatedState);

            ReportEnd();
        }
Пример #7
0
    private void Read(Stream stream, bool metadataOnly)
    {
        Header = GifHeader.ReadHeader(stream);

        if (Header.LogicalScreenDescriptor.HasGlobalColorTable)
        {
            GlobalColorTable = GifHelpers.ReadColorTable(stream, Header.LogicalScreenDescriptor.GlobalColorTableSize);
        }

        ReadFrames(stream, metadataOnly);

        var netscapeExtension = Extensions.OfType <GifApplicationExtension>().FirstOrDefault(GifHelpers.IsNetscapeExtension);

        RepeatCount = netscapeExtension != null?GifHelpers.GetRepeatCount(netscapeExtension) : (ushort)1;
    }
Пример #8
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);
        }
Пример #9
0
        public void WriteToStreamTest()
        {
            ReportStart();

            _header = new GifHeader("GIF", "87a");
            MemoryStream s = new MemoryStream();

            _header.WriteToStream(s);
            s.Seek(0, SeekOrigin.Begin);
            _header = new GifHeader(s);
            Assert.AreEqual(ErrorState.Ok, _header.ConsolidatedState);
            Assert.AreEqual("GIF", _header.Signature);
            Assert.AreEqual("87a", _header.Version);

            ReportEnd();
        }
Пример #10
0
        /// <summary>
        /// <p>Sets new stream to read gif data from</p>
        /// <br/>
        /// <p>GifStream is reusable, you can change stream and read new gif from it.
        /// That way you reuse allocations that you've made, and keep your memory usage to minimum</p>
        /// <br/>
        /// <p>Stream will be reset to its initial state</p>
        /// </summary>
        /// <param name="stream">new stream with gif data</param>
        /// <param name="disposePrevious">Dispose previous stream</param>
        public void SetStream(Stream stream, bool disposePrevious = false)
        {
            if (disposePrevious)
            {
                if (currentStream != null)
                {
                    currentStream.Dispose();
                }
            }

            header          = new GifHeader();
            imageDescriptor = new GifImageDescriptor();
            graphicControl  = new GifGraphicControl();

            currentStream = stream;
            CurrentToken  = Token.Header;
            blockReader.SetStream(stream);
        }
Пример #11
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;

            _header = new GifHeader(inputStream, XmlDebugging);
            WriteDebugXmlNode(_header.DebugXmlReader);
            if (_header.ErrorState != ErrorState.Ok)
            {
                WriteDebugXmlFinish();
                return;
            }

            _lsd = new LogicalScreenDescriptor(inputStream, XmlDebugging);
            WriteDebugXmlNode(_lsd.DebugXmlReader);
            if (TestState(ErrorState.EndOfInputStream))
            {
                WriteDebugXmlFinish();
                return;
            }

            if (_lsd.HasGlobalColourTable)
            {
                _gct = new ColourTable(inputStream,
                                       _lsd.GlobalColourTableSize,
                                       XmlDebugging);
                WriteDebugXmlNode(_gct.DebugXmlReader);
            }

            if (ConsolidatedState == ErrorState.Ok)
            {
                ReadContents(inputStream);
            }
            inputStream.Close();
            WriteDebugXmlFinish();
        }
Пример #12
0
        private static void WriteGifHeader(Stream outputStream)
        {
            GifHeader header = new GifHeader("GIF", "89a");

            header.WriteToStream(outputStream);
        }
Пример #13
0
        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();
        }