public object ReduceColourDepth(object o, BackgroundWorker w, DoWorkEventArgs e) { Console.WriteLine("Worker thread: " + this + "," + TargetColours + "," + ColourMap + "," + ColourMap.Count); // This case we're removing colours one at a time, replacing them with closest colours if (TargetColours > 0) { Console.WriteLine("ReduceColourDepth: initial nColours = " + ColourMap.Count + ", Target: " + TargetColours); int toRemove = ColourMap.Count - TargetColours; for (int i = 0; i < toRemove; i++) { if (w.CancellationPending) { e.Cancel = true; return(this); } IColourInfo least = ColourMap.GetLeastCommonColourInfo(true); RemoveFromPalette(least); UpdateColourMapFromImage(); w.ReportProgress(i * 100 / toRemove); } Console.WriteLine("ReduceColourDepth: final number of colours = " + ColourMap.Count); } else // This case we have no idea how many colours, we just want to fit the colour map. { Bitmap orig = mOutput; Bitmap b = new Bitmap(orig.Width, orig.Height, PixelFormat.Format24bppRgb); Console.WriteLine("ReduceColourDepth: initial nColours = " + ColourMap.Count); int i = 0; for (int x = 0; x < b.Width; x++) { for (int y = 0; y < b.Height; y++) { if (w.CancellationPending) { e.Cancel = true; return(this); } Color c = orig.GetPixel(x, y); Color newC = ColourMap.GetNearestColour(c); b.SetPixel(x, y, newC); w.ReportProgress((i++) * 100 / (b.Width * b.Height)); } } mOutput = b; } return(this); }
public void RemoveFromPalette(Color colorToRemove) { ColourMap.RemoveColour(colorToRemove); ReplaceColour(colorToRemove, ColourMap.GetNearestColour(colorToRemove)); }