public void WriteToStreamTest()
        {
            ReportStart();

            Size screenSize = new Size( 12, 4 );
            bool hasGlobalColourTable = false;
            int colourResolution = 3;
            bool globalColourTableIsSorted = true;
            int globalColourTableSizeBits = 4;
            int backgroundColourIndex = 2;
            int pixelAspectRatio = 1;
            _lsd = new LogicalScreenDescriptor( screenSize,
                                                hasGlobalColourTable,
                                                colourResolution,
                                                globalColourTableIsSorted,
                                                globalColourTableSizeBits,
                                                backgroundColourIndex,
                                                pixelAspectRatio );

            MemoryStream s = new MemoryStream();
            _lsd.WriteToStream( s );
            s.Seek( 0, SeekOrigin.Begin );
            _lsd = new LogicalScreenDescriptor( s );

            Assert.AreEqual( ErrorState.Ok, _lsd.ConsolidatedState );
            Assert.AreEqual( screenSize, _lsd.LogicalScreenSize );
            Assert.AreEqual( hasGlobalColourTable, _lsd.HasGlobalColourTable );
            Assert.AreEqual( colourResolution, _lsd.ColourResolution );
            Assert.AreEqual( globalColourTableIsSorted, _lsd.GlobalColourTableIsSorted );
            Assert.AreEqual( globalColourTableSizeBits, _lsd.GlobalColourTableSizeBits );
            Assert.AreEqual( backgroundColourIndex, _lsd.BackgroundColourIndex );
            Assert.AreEqual( pixelAspectRatio, _lsd.PixelAspectRatio );

            ReportEnd();
        }
        private void ConstructorStreamEndOfStreamTest( bool xmlDebugging )
        {
            Size screenSize = new Size( 12, 4 );
            bool hasGlobalColourTable = false;
            int colourResolution = 3;
            bool globalColourTableIsSorted = true;
            int globalColourTableSizeBits = 4;
            int backgroundColourIndex = 2;
            int pixelAspectRatio = 1;
            _lsd = new LogicalScreenDescriptor( screenSize,
                                                hasGlobalColourTable,
                                                colourResolution,
                                                globalColourTableIsSorted,
                                                globalColourTableSizeBits,
                                                backgroundColourIndex,
                                                pixelAspectRatio );

            MemoryStream s = new MemoryStream();
            _lsd.WriteToStream( s );
            s.SetLength( s.Length - 1 ); // remove final byte from stream
            s.Seek( 0, SeekOrigin.Begin );
            _lsd = new LogicalScreenDescriptor( s, xmlDebugging );

            Assert.AreEqual( ErrorState.EndOfInputStream, _lsd.ConsolidatedState );
            Assert.AreEqual( screenSize, _lsd.LogicalScreenSize );
            Assert.AreEqual( hasGlobalColourTable, _lsd.HasGlobalColourTable );
            Assert.AreEqual( colourResolution, _lsd.ColourResolution );
            Assert.AreEqual( globalColourTableIsSorted, _lsd.GlobalColourTableIsSorted );
            Assert.AreEqual( globalColourTableSizeBits, _lsd.GlobalColourTableSizeBits );
            Assert.AreEqual( backgroundColourIndex, _lsd.BackgroundColourIndex );
            Assert.AreEqual( -1, _lsd.PixelAspectRatio );

            if( xmlDebugging )
            {
                Assert.AreEqual( ExpectedDebugXml, _lsd.DebugXml );
            }
        }
		/// <summary>
		/// Writes a Logical Screen Descriptor to the supplied stream.
		/// Also writes a global colour table if required.
		/// </summary>
		/// <param name="outputStream">
		/// The stream to write to.
		/// </param>
		private void WriteLogicalScreenDescriptor( Stream outputStream )
		{
			bool hasGlobalColourTable = _strategy == ColourTableStrategy.UseGlobal;
			int colourResolution = 7; // TODO: parameterise colourResolution?
			bool globalColourTableIsSorted = false; // Sorting of colour tables is not currently supported
			int backgroundColorIndex = 0; // TODO: parameterise backgroundColourIndex?
			int pixelAspectRatio = 0; // TODO: parameterise pixelAspectRatio?
			if( _strategy == ColourTableStrategy.UseGlobal )
			{
				if( _quantizerType == QuantizerType.UseSuppliedPalette )
				{
					// use supplied palette
					_globalColourTable = new ColourTable();
					string buildColourTableCounterText 
						= "Building colour table from supplied palette";
					AddCounter( buildColourTableCounterText, 
					            _palette.Count );
					int paletteIndex = 0;
					foreach( Color c in _palette )
					{
						_globalColourTable.Add( c );
						MyProgressCounters[buildColourTableCounterText].Value 
							= paletteIndex;
						paletteIndex++;
					}
					_globalColourTable.Pad();
					RemoveCounter( buildColourTableCounterText );
				}
				else
				{
					// Analyse the pixels in all the images to build the
					// global colour table.
					Collection<Image> images = new Collection<Image>();
					foreach( GifFrame thisFrame in _frames )
					{
						Image thisImage = thisFrame.TheImage;
						images.Add( thisImage );
					}
					_pixelAnalysis = new PixelAnalysis( images );
					_pixelAnalysis.ColourQuality = _quality;
					_pixelAnalysis.Analyse();
					_globalColourTable = _pixelAnalysis.ColourTable;
				}
				LogicalScreenDescriptor lsd = 
					new LogicalScreenDescriptor( _logicalScreenSize, 
					                             hasGlobalColourTable, 
					                             colourResolution, 
					                             globalColourTableIsSorted, 
					                             _globalColourTable.SizeBits,
					                             backgroundColorIndex, 
					                             pixelAspectRatio );
				lsd.WriteToStream( outputStream );
				_globalColourTable.WriteToStream( outputStream );
			}
			else
			{
				LogicalScreenDescriptor lsd = 
					new LogicalScreenDescriptor( _logicalScreenSize, 
					                             hasGlobalColourTable, 
					                             colourResolution, 
					                             globalColourTableIsSorted, 
					                             7, 
					                             backgroundColorIndex, 
					                             pixelAspectRatio );
				lsd.WriteToStream( outputStream );
			}
		}