public void Quantize(IColorQuantizer quantizer, int colorCount) { if (Disposed) { return; } int myWidth = Width; int myHeight = Height; Color *colors = GetColors(); quantizer.Prepare(this); bool usesAlpha = false; // weird stuff to make the quantizer work with alpha Color lastColor = new Color(255, 255, 255, 0); bool firstColorFound = false; for (int y = 0; y < myHeight; y++) { for (int x = 0; x < myWidth; x++) { int idx = x + y * myWidth; var col = *(colors + idx); if (col.A > 128) { col.A = 255; (*(colors + x + y * myWidth)) = col; if (!firstColorFound && usesAlpha) { for (int i = 0; i < idx; i++) { quantizer.AddColor(col, i % myWidth, Mathf.FloorToInt(i / myWidth)); } } firstColorFound = true; lastColor = col; quantizer.AddColor(col, x, y); } else { col = new Color(255, 255, 255, 0); (*(colors + x + y * myWidth)) = col; usesAlpha = true; if (firstColorFound) { quantizer.AddColor(lastColor, x, y); } } } } var palette = quantizer.GetPalette(colorCount); for (int y = 0; y < myHeight; y++) { for (int x = 0; x < myWidth; x++) { if ((*(colors + x + y * myWidth)).A == 255) { int index = quantizer.GetPaletteIndex(*(colors + x + y * myWidth), x, y); //Debug.Log(index); *(colors + x + y * myWidth) = palette[index]; } } } quantizer.Finish(); }
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 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(); }