public void CorrectGamma(Single gamma, IColorQuantizer quantizer, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(quantizer, "quantizer"); // determines which method of color retrieval to use IList <Point> path = quantizer.GetPointPath(Width, Height); // calculates gamma ramp Int32[] gammaRamp = new Int32[256]; for (Int32 index = 0; index < 256; ++index) { gammaRamp[index] = Clamp((Int32)((255.0f * Math.Pow(index / 255.0f, 1.0f / gamma)) + 0.5f)); } // use different scanning method depending whether the image format is indexed ProcessPixelFunction correctGamma = pixel => { Color oldColor = GetColorFromPixel(pixel); Int32 red = gammaRamp[oldColor.R]; Int32 green = gammaRamp[oldColor.G]; Int32 blue = gammaRamp[oldColor.B]; Color newColor = Color.FromArgb(red, green, blue); SetColorToPixel(pixel, newColor, quantizer); return(true); }; // performs the image scan, using a chosen method ProcessPerPixel(path, correctGamma, parallelTaskCount); }
public void ScanColors(IColorQuantizer quantizer, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(quantizer, "quantizer"); // determines which method of color retrieval to use IList <Point> path = quantizer.GetPointPath(Width, Height); // use different scanning method depending whether the image format is indexed ProcessPixelFunction scanColors = pixel => { quantizer.AddColor(GetColorFromPixel(pixel), pixel.X, pixel.Y); return(false); }; // performs the image scan, using a chosen method ProcessPerPixel(path, scanColors, parallelTaskCount); }
public void ScanColors(IColorQuantizer quantizer, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(quantizer, "quantizer"); // determines which method of color retrieval to use IList<Point> path = quantizer.GetPointPath(Width, Height); // use different scanning method depending whether the image format is indexed ProcessPixelFunction scanColors = pixel => { quantizer.AddColor(GetColorFromPixel(pixel), pixel.X, pixel.Y); return false; }; // performs the image scan, using a chosen method ProcessPerPixel(path, scanColors, parallelTaskCount); }
public void Quantize(ImageBuffer target, IColorQuantizer quantizer, IColorDitherer ditherer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(target, "target"); Guard.CheckNull(quantizer, "quantizer"); // initializes quantization parameters Boolean isTargetIndexed = target.PixelFormat.IsIndexed(); // step 1 - prepares the palettes List<Color> targetPalette = isTargetIndexed ? SynthetizePalette(quantizer, colorCount, parallelTaskCount) : null; // step 2 - updates the bitmap palette target.bitmap.SetPalette(targetPalette); target.UpdatePalette(true); // step 3 - prepares ditherer (optional) if (ditherer != null) ditherer.Prepare(quantizer, colorCount, this, target); // step 4 - prepares the quantization function TransformPixelFunction quantize = (sourcePixel, targetPixel) => { // reads the pixel color Color color = GetColorFromPixel(sourcePixel); // converts alpha to solid color color = QuantizationHelper.ConvertAlpha(color); // quantizes the pixel SetColorToPixel(targetPixel, color, quantizer); // marks pixel as processed by default Boolean result = true; // preforms inplace dithering (optional) if (ditherer != null && ditherer.IsInplace) { result = ditherer.ProcessPixel(sourcePixel, targetPixel); } // returns the result return result; }; // step 5 - generates the target image IList<Point> path = quantizer.GetPointPath(Width, Height); TransformPerPixel(target, path, quantize, parallelTaskCount); // step 6 - preforms non-inplace dithering (optional) if (ditherer != null && !ditherer.IsInplace) { Dither(target, ditherer, quantizer, colorCount, 1); } // step 7 - finishes the dithering (optional) if (ditherer != null) ditherer.Finish(); // step 8 - clean-up quantizer.Finish(); }
public void CorrectGamma(Single gamma, IColorQuantizer quantizer, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(quantizer, "quantizer"); // determines which method of color retrieval to use IList<Point> path = quantizer.GetPointPath(Width, Height); // calculates gamma ramp Int32[] gammaRamp = new Int32[256]; for (Int32 index = 0; index < 256; ++index) { gammaRamp[index] = Clamp((Int32) ((255.0f*Math.Pow(index/255.0f, 1.0f/gamma)) + 0.5f)); } // use different scanning method depending whether the image format is indexed ProcessPixelFunction correctGamma = pixel => { Color oldColor = GetColorFromPixel(pixel); Int32 red = gammaRamp[oldColor.R]; Int32 green = gammaRamp[oldColor.G]; Int32 blue = gammaRamp[oldColor.B]; Color newColor = Color.FromArgb(red, green, blue); SetColorToPixel(pixel, newColor, quantizer); return true; }; // performs the image scan, using a chosen method ProcessPerPixel(path, correctGamma, parallelTaskCount); }
public void Quantize(ImageBuffer target, IColorQuantizer quantizer, IColorDitherer ditherer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(target, "target"); Guard.CheckNull(quantizer, "quantizer"); // initializes quantization parameters Boolean isTargetIndexed = target.PixelFormat.IsIndexed(); // step 1 - prepares the palettes List <Color> targetPalette = isTargetIndexed ? SynthetizePalette(quantizer, colorCount, parallelTaskCount) : null; // step 2 - updates the bitmap palette target.bitmap.SetPalette(targetPalette); target.UpdatePalette(true); // step 3 - prepares ditherer (optional) if (ditherer != null) { ditherer.Prepare(quantizer, colorCount, this, target); } // step 4 - prepares the quantization function TransformPixelFunction quantize = (sourcePixel, targetPixel) => { // reads the pixel color Color color = GetColorFromPixel(sourcePixel); // converts alpha to solid color color = QuantizationHelper.ConvertAlpha(color); // quantizes the pixel SetColorToPixel(targetPixel, color, quantizer); // marks pixel as processed by default Boolean result = true; // preforms inplace dithering (optional) if (ditherer != null && ditherer.IsInplace) { result = ditherer.ProcessPixel(sourcePixel, targetPixel); } // returns the result return(result); }; // step 5 - generates the target image IList <Point> path = quantizer.GetPointPath(Width, Height); TransformPerPixel(target, path, quantize, parallelTaskCount); // step 6 - preforms non-inplace dithering (optional) if (ditherer != null && !ditherer.IsInplace) { Dither(target, ditherer, quantizer, colorCount, 1); } // step 7 - finishes the dithering (optional) if (ditherer != null) { ditherer.Finish(); } // step 8 - clean-up quantizer.Finish(); }