/// <summary> /// Returns a colour table containing the colours of the quantized image. /// </summary> /// <returns> /// A colour table containing up to 256 colours, being the colours of /// the image after quantization. /// </returns> private ColourTable ColourMap() { ColourTable map = new ColourTable(); int[] originalIndices = new int[_neuronCount]; int[] thisNeuron; // Build an array of the original indices of the neurons in the // network, before the BuildIndex method reordered them. for( int neuronIndex = 0; neuronIndex < _neuronCount; neuronIndex++ ) { thisNeuron = _network[neuronIndex]; originalIndices[thisNeuron[3]] = neuronIndex; } for( int i = 0; i < _neuronCount; i++ ) { int indexInNetwork = originalIndices[i]; map.Add( Color.FromArgb( 255, _network[indexInNetwork][0], _network[indexInNetwork][1], _network[indexInNetwork][2] ) ); } return map; }
/// <summary> /// Creates a colour table directly from the distinct colours in the /// supplied image(s). /// </summary> private void CreateDirectColourTable() { string buildColourTableCounterText = "Creating colour table from images"; AddCounter( buildColourTableCounterText, _distinctColours.Values.Count ); _colourTable = new ColourTable(); int distinctColourIndex = 0; foreach( Color c in _distinctColours.Values ) { MyProgressCounters[buildColourTableCounterText].Value = distinctColourIndex; _colourTable.Add( c ); distinctColourIndex++; } _colourTable.Pad(); RemoveCounter( buildColourTableCounterText ); }
/// <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 ); } }
public void WriteToStreamTest() { ReportStart(); _table = new ColourTable(); _table.Add( Color.FromArgb( 255, 255, 0, 0 ) ); // red _table.Add( Color.FromArgb( 255, 0, 255, 0 ) ); // green _table.Add( Color.FromArgb( 255, 0, 0, 255 ) ); // blue _table.Add( Color.FromArgb( 255, 255, 255, 255 ) ); // white MemoryStream s = new MemoryStream(); _table.WriteToStream( s ); s.Seek( 0, SeekOrigin.Begin ); ColourTable t = new ColourTable( s, 4 ); Assert.AreEqual( ErrorState.Ok, t.ConsolidatedState ); Assert.AreEqual( _table.Colours.Length, t.Colours.Length ); for( int i = 0; i < t.Colours.Length; i++ ) { Assert.AreEqual( _table[i], t[i], "Colour index " + i ); } ReportEnd(); }
private ColourTable SetActiveColourTable() { ColourTable act; // active colour table if( _strategy == ColourTableStrategy.UseLocal ) { if( _quantizerType == QuantizerType.UseSuppliedPalette ) { if( _frames[_encodingFrame].Palette == null ) { // TESTME: SetActiveColourTable - SetActiveColourTable, UseSuppliedPalette, no palette supplied string message = "You have opted to use a local colour table built " + "from a supplied palette, but frame " + _encodingFrame + "does not have a palette."; throw new InvalidOperationException( message ); } else { // Build local colour table from colours in the frame's // supplied palette. act = new ColourTable(); foreach( Color c in _frames[_encodingFrame].Palette ) { act.Add( c ); } act.Pad(); } } else { // Build local colour table based on colours in the image. Image thisImage = _frames[_encodingFrame].TheImage; _pixelAnalysis = new PixelAnalysis( thisImage, _quantizerType ); _pixelAnalysis.ColourQuality = _quality; _pixelAnalysis.Analyse(); // make local colour table active act = _pixelAnalysis.ColourTable; } } else { // make global colour table active act = _globalColourTable; } return act; }