/// <summary> /// Sets the pixels of the decoded image. /// </summary> /// <param name="imageData"> /// Table based image data containing the indices within the active /// colour table of the colours of the pixels in this frame. /// </param> /// <param name="lsd"> /// The logical screen descriptor for the GIF stream. /// </param> /// <param name="id"> /// The image descriptor for this frame. /// </param> /// <param name="activeColourTable"> /// The colour table to use with this frame - either the global colour /// table or a local colour table. /// </param> /// <param name="gce"> /// The graphic control extension, if any, which precedes this image in /// the input stream. /// </param> /// <param name="previousFrame"> /// The frame which precedes this one in the GIF stream, if present. /// </param> /// <param name="previousFrameBut1"> /// The frame which precedes the frame before this one in the GIF stream, /// if present. /// </param> /// <param name="status"> /// GifComponentStatus containing any errors which occurred during the /// creation of the bitmap. /// </param> private static Bitmap CreateBitmap( TableBasedImageData imageData, LogicalScreenDescriptor lsd, ImageDescriptor id, ColourTable activeColourTable, GraphicControlExtension gce, GifFrame previousFrame, GifFrame previousFrameBut1, out GifComponentStatus status ) { status = new GifComponentStatus( ErrorState.Ok, "" ); Color[] pixelsForThisFrame = new Color[lsd.LogicalScreenSize.Width * lsd.LogicalScreenSize.Height]; Bitmap baseImage = GetBaseImage( previousFrame, previousFrameBut1, lsd, gce, activeColourTable ); // copy each source line to the appropriate place in the destination int pass = 1; int interlaceRowIncrement = 8; int interlaceRowNumber = 0; // the row of pixels we're currently // setting in an interlaced image. for( int i = 0; i < id.Size.Height; i++) { int pixelRowNumber = i; if( id.IsInterlaced ) { #region work out the pixel row we're setting for an interlaced image if( interlaceRowNumber >= id.Size.Height ) { pass++; switch( pass ) { case 2 : interlaceRowNumber = 4; break; case 3 : interlaceRowNumber = 2; interlaceRowIncrement = 4; break; case 4 : interlaceRowNumber = 1; interlaceRowIncrement = 2; break; } } #endregion pixelRowNumber = interlaceRowNumber; interlaceRowNumber += interlaceRowIncrement; } // Colour in the pixels for this row pixelRowNumber += id.Position.Y; if( pixelRowNumber < lsd.LogicalScreenSize.Height ) { int k = pixelRowNumber * lsd.LogicalScreenSize.Width; int dx = k + id.Position.X; // start of line in dest int dlim = dx + id.Size.Width; // end of dest line if( (k + lsd.LogicalScreenSize.Width) < dlim ) { // TESTME: CreateBitmap - past dest edge dlim = k + lsd.LogicalScreenSize.Width; // past dest edge } int sx = i * id.Size.Width; // start of line in source while (dx < dlim) { // map color and insert in destination int indexInColourTable = (int) imageData.Pixels[sx++]; // Set this pixel's colour if its index isn't the // transparent colour index, or if this frame doesn't // have a transparent colour. Color c; if( gce.HasTransparentColour && indexInColourTable == gce.TransparentColourIndex ) { c = Color.Empty; // transparent pixel } else { if( indexInColourTable < activeColourTable.Length ) { c = activeColourTable[indexInColourTable]; } else { // TESTME: CreateBitmap - BadColourIndex c = Color.Black; string message = "Colour index: " + indexInColourTable + ", colour table length: " + activeColourTable.Length + " (" + dx + "," + pixelRowNumber + ")"; status = new GifComponentStatus( ErrorState.BadColourIndex, message ); } } pixelsForThisFrame[dx] = c; dx++; } } } return CreateBitmap( baseImage, pixelsForThisFrame ); }
internal static void CheckImageDescriptor( ImageDescriptor id ) { Assert.AreEqual( HasLocalColourTable, id.HasLocalColourTable, "HasLocalColourTable" ); Assert.AreEqual( IsInterlaced, id.IsInterlaced, "IsInterlaced" ); Assert.AreEqual( LocalColourTableIsSorted, id.IsSorted, "LocalColourTableIsSorted" ); Assert.AreEqual( Math.Pow( 2, LocalColourTableSizeBits + 1 ), id.LocalColourTableSize, "LocalColourTableSize" ); Assert.AreEqual( FramePosition, id.Position, "Position" ); Assert.AreEqual( FrameSize, id.Size, "Size" ); }
private static ImageDescriptor CheckImageDescriptor( Stream s, bool shouldHaveLocalColourTable, int localColourTableSizeBits) { // check for image descriptor ImageDescriptor id = new ImageDescriptor( s ); Assert.AreEqual( ErrorState.Ok, id.ConsolidatedState, "Image descriptor consolidated state" ); Assert.AreEqual( shouldHaveLocalColourTable, id.HasLocalColourTable, "Should have local colour table" ); Assert.AreEqual( false, id.IsInterlaced, "Is interlaced" ); Assert.AreEqual( false, id.IsSorted, "Local colour table is sorted" ); Assert.AreEqual( localColourTableSizeBits, id.LocalColourTableSizeBits, "Local colour table size (bits)" ); Assert.AreEqual( 1 << (localColourTableSizeBits + 1), id.LocalColourTableSize, "Local colour table size"); Assert.AreEqual( new Size( 2, 2 ), id.Size, "Image descriptor size" ); Assert.AreEqual( new Point( 0, 0 ), id.Position, "Image descriptor position" ); return id; }
public GifFrame( Stream inputStream, LogicalScreenDescriptor lsd, ColourTable gct, GraphicControlExtension gce, GifFrame previousFrame, GifFrame previousFrameBut1, bool xmlDebugging ) : base( xmlDebugging ) { #region guard against null arguments if( lsd == null ) { throw new ArgumentNullException( "lsd" ); } if( gce == null ) { SetStatus( ErrorState.NoGraphicControlExtension, "" ); // use a default GCE gce = new GraphicControlExtension( GraphicControlExtension.ExpectedBlockSize, DisposalMethod.NotSpecified, false, false, 100, 0 ); } #endregion int transparentColourIndex = gce.TransparentColourIndex; ImageDescriptor imageDescriptor = new ImageDescriptor( inputStream, XmlDebugging ); WriteDebugXmlNode( imageDescriptor.DebugXmlReader ); #region determine the colour table to use for this frame Color backgroundColour = Color.FromArgb( 0 ); // TODO: is this the right background colour? // TODO: use backgroundColourIndex from the logical screen descriptor? ColourTable activeColourTable; if( imageDescriptor.HasLocalColourTable ) { _localColourTable = new ColourTable( inputStream, imageDescriptor.LocalColourTableSize, XmlDebugging ); WriteDebugXmlNode( _localColourTable.DebugXmlReader ); activeColourTable = _localColourTable; // make local table active } else { if( gct == null ) { // We have neither local nor global colour table, so we // won't be able to decode this frame. Bitmap emptyBitmap = new Bitmap( lsd.LogicalScreenSize.Width, lsd.LogicalScreenSize.Height ); _image = emptyBitmap; _delay = gce.DelayTime; SetStatus( ErrorState.FrameHasNoColourTable, "" ); return; } activeColourTable = gct; // make global table active if( lsd.BackgroundColourIndex == transparentColourIndex ) { backgroundColour = Color.FromArgb( 0 ); } } #endregion // decode pixel data int pixelCount = imageDescriptor.Size.Width * imageDescriptor.Size.Height; TableBasedImageData indexedPixels = new TableBasedImageData( inputStream, pixelCount, XmlDebugging ); WriteDebugXmlNode( indexedPixels.DebugXmlReader ); if( indexedPixels.Pixels.Count == 0 ) { // TESTME: constructor - indexedPixels.Pixels.Count == 0 Bitmap emptyBitmap = new Bitmap( lsd.LogicalScreenSize.Width, lsd.LogicalScreenSize.Height ); _image = emptyBitmap; _delay = gce.DelayTime; SetStatus( ErrorState.FrameHasNoImageData, "" ); WriteDebugXmlFinish(); return; } // Skip any remaining blocks up to the next block terminator (in // case there is any surplus data before the next frame) SkipBlocks( inputStream ); _indexedPixels = indexedPixels; _extension = gce; if( gce != null ) { _delay = gce.DelayTime; } _imageDescriptor = imageDescriptor; _backgroundColour = backgroundColour; GifComponentStatus status; _image = CreateBitmap( indexedPixels, lsd, imageDescriptor, activeColourTable, gce, previousFrame, previousFrameBut1, out status ); WriteDebugXmlFinish(); }
private static void CheckImageData( Stream s, ColourTable act, ImageDescriptor id, Bitmap expectedBitmap) { // 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 = id.Size.Width * id.Size.Height; TableBasedImageData tbid = new TableBasedImageData( s, pixelCount ); Assert.AreEqual( ErrorState.Ok, tbid.ConsolidatedState ); for( int y = 0; y < id.Size.Height; y++ ) { for( int x = 0; x < id.Size.Width; x++ ) { int i = (y * id.Size.Width) + x; Assert.AreEqual( expectedBitmap.GetPixel( x, y ), act[tbid.Pixels[i]], "X: " + x + ", Y: " + y ); } } }
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(); }
/// <summary> /// Sets the pixels of the decoded image. /// </summary> /// <param name="imageData"> /// Table based image data containing the indices within the active /// colour table of the colours of the pixels in this frame. /// </param> /// <param name="lsd"> /// The logical screen descriptor for the GIF stream. /// </param> /// <param name="id"> /// The image descriptor for this frame. /// </param> /// <param name="activeColourTable"> /// The colour table to use with this frame - either the global colour /// table or a local colour table. /// </param> /// <param name="gce"> /// The graphic control extension, if any, which precedes this image in /// the input stream. /// </param> /// <param name="previousFrame"> /// The frame which precedes this one in the GIF stream, if present. /// </param> /// <param name="previousFrameBut1"> /// The frame which precedes the frame before this one in the GIF stream, /// if present. /// </param> /// <param name="status"> /// GifComponentStatus containing any errors which occurred during the /// creation of the bitmap. /// </param> private static Bitmap CreateBitmap(TableBasedImageData imageData, LogicalScreenDescriptor lsd, ImageDescriptor id, ColourTable activeColourTable, GraphicControlExtension gce, GifFrame previousFrame, GifFrame previousFrameBut1, out GifComponentStatus status) { status = new GifComponentStatus(ErrorState.Ok, ""); Color[] pixelsForThisFrame = new Color[lsd.LogicalScreenSize.Width * lsd.LogicalScreenSize.Height]; Bitmap baseImage = GetBaseImage(previousFrame, previousFrameBut1, lsd, gce, activeColourTable); // copy each source line to the appropriate place in the destination int pass = 1; int interlaceRowIncrement = 8; int interlaceRowNumber = 0; // the row of pixels we're currently // setting in an interlaced image. for (int i = 0; i < id.Size.Height; i++) { int pixelRowNumber = i; if (id.IsInterlaced) { #region work out the pixel row we're setting for an interlaced image if (interlaceRowNumber >= id.Size.Height) { pass++; switch (pass) { case 2: interlaceRowNumber = 4; break; case 3: interlaceRowNumber = 2; interlaceRowIncrement = 4; break; case 4: interlaceRowNumber = 1; interlaceRowIncrement = 2; break; } } #endregion pixelRowNumber = interlaceRowNumber; interlaceRowNumber += interlaceRowIncrement; } // Colour in the pixels for this row pixelRowNumber += id.Position.Y; if (pixelRowNumber < lsd.LogicalScreenSize.Height) { int k = pixelRowNumber * lsd.LogicalScreenSize.Width; int dx = k + id.Position.X; // start of line in dest int dlim = dx + id.Size.Width; // end of dest line if ((k + lsd.LogicalScreenSize.Width) < dlim) { // TESTME: CreateBitmap - past dest edge dlim = k + lsd.LogicalScreenSize.Width; // past dest edge } int sx = i * id.Size.Width; // start of line in source while (dx < dlim) { // map color and insert in destination int indexInColourTable = (int)imageData.Pixels[sx++]; // Set this pixel's colour if its index isn't the // transparent colour index, or if this frame doesn't // have a transparent colour. Color c; if (gce.HasTransparentColour && indexInColourTable == gce.TransparentColourIndex) { c = Color.Empty; // transparent pixel } else { if (indexInColourTable < activeColourTable.Length) { c = activeColourTable[indexInColourTable]; } else { // TESTME: CreateBitmap - BadColourIndex c = Color.Black; string message = "Colour index: " + indexInColourTable + ", colour table length: " + activeColourTable.Length + " (" + dx + "," + pixelRowNumber + ")"; status = new GifComponentStatus(ErrorState.BadColourIndex, message); } } pixelsForThisFrame[dx] = c; dx++; } } } return(CreateBitmap(baseImage, pixelsForThisFrame)); }
public GifFrame(Stream inputStream, LogicalScreenDescriptor lsd, ColourTable gct, GraphicControlExtension gce, GifFrame previousFrame, GifFrame previousFrameBut1, bool xmlDebugging) : base(xmlDebugging) { #region guard against null arguments if (lsd == null) { throw new ArgumentNullException("lsd"); } if (gce == null) { SetStatus(ErrorState.NoGraphicControlExtension, ""); // use a default GCE gce = new GraphicControlExtension(GraphicControlExtension.ExpectedBlockSize, DisposalMethod.NotSpecified, false, false, 100, 0); } #endregion int transparentColourIndex = gce.TransparentColourIndex; ImageDescriptor imageDescriptor = new ImageDescriptor(inputStream, XmlDebugging); WriteDebugXmlNode(imageDescriptor.DebugXmlReader); #region determine the colour table to use for this frame Color backgroundColour = Color.FromArgb(0); // TODO: is this the right background colour? // TODO: use backgroundColourIndex from the logical screen descriptor? ColourTable activeColourTable; if (imageDescriptor.HasLocalColourTable) { _localColourTable = new ColourTable(inputStream, imageDescriptor.LocalColourTableSize, XmlDebugging); WriteDebugXmlNode(_localColourTable.DebugXmlReader); activeColourTable = _localColourTable; // make local table active } else { if (gct == null) { // We have neither local nor global colour table, so we // won't be able to decode this frame. Bitmap emptyBitmap = new Bitmap(lsd.LogicalScreenSize.Width, lsd.LogicalScreenSize.Height); _image = emptyBitmap; _delay = gce.DelayTime; SetStatus(ErrorState.FrameHasNoColourTable, ""); return; } activeColourTable = gct; // make global table active if (lsd.BackgroundColourIndex == transparentColourIndex) { backgroundColour = Color.FromArgb(0); } } #endregion // decode pixel data int pixelCount = imageDescriptor.Size.Width * imageDescriptor.Size.Height; TableBasedImageData indexedPixels = new TableBasedImageData(inputStream, pixelCount, XmlDebugging); WriteDebugXmlNode(indexedPixels.DebugXmlReader); if (indexedPixels.Pixels.Count == 0) { // TESTME: constructor - indexedPixels.Pixels.Count == 0 Bitmap emptyBitmap = new Bitmap(lsd.LogicalScreenSize.Width, lsd.LogicalScreenSize.Height); _image = emptyBitmap; _delay = gce.DelayTime; SetStatus(ErrorState.FrameHasNoImageData, ""); WriteDebugXmlFinish(); return; } // Skip any remaining blocks up to the next block terminator (in // case there is any surplus data before the next frame) SkipBlocks(inputStream); _indexedPixels = indexedPixels; _extension = gce; if (gce != null) { _delay = gce.DelayTime; } _imageDescriptor = imageDescriptor; _backgroundColour = backgroundColour; GifComponentStatus status; _image = CreateBitmap(indexedPixels, lsd, imageDescriptor, activeColourTable, gce, previousFrame, previousFrameBut1, out status); WriteDebugXmlFinish(); }
/// <summary> /// Writes an image descriptor to the supplied stream. /// </summary> /// <param name="imageSize"> /// The size, in pixels, of the image in this frame. /// </param> /// <param name="position"> /// The position of this image within the logical screen. /// </param> /// <param name="localColourTable"> /// The local colour table for this frame. /// Supply null if the global colour table is to be used for this frame. /// </param> /// <param name="outputStream"> /// The stream to write to. /// </param> private static void WriteImageDescriptor( Size imageSize, Point position, ColourTable localColourTable, Stream outputStream ) { bool hasLocalColourTable; int localColourTableSize; if( localColourTable == null ) { hasLocalColourTable = false; localColourTableSize = 0; } else { hasLocalColourTable = true; localColourTableSize = localColourTable.SizeBits; } bool isInterlaced = false; // encoding of interlaced images not currently supported bool localColourTableIsSorted = false; // sorting of colour tables not currently supported ImageDescriptor id = new ImageDescriptor( position, imageSize, hasLocalColourTable, isInterlaced, localColourTableIsSorted, localColourTableSize ); outputStream.WriteByte( GifComponent.CodeImageSeparator ); id.WriteToStream( outputStream ); }