/// <summary> /// Calculate reduced color palette for the specified image. /// </summary> /// /// <param name="image">Image to calculate palette for.</param> /// <param name="paletteSize">Palette size to calculate.</param> /// /// <returns>Return reduced color palette for the specified image.</returns> /// /// <remarks><para>The method processes the specified image and feeds color value of each pixel /// to the specified color quantization algorithm. Finally it returns color palette built by /// that algorithm.</para></remarks> /// /// <exception cref="UnsupportedImageFormatException">Unsupported format of the source image - it must 24 or 32 bpp color image.</exception> /// public Color[] CalculatePalette(UnmanagedImage image, int paletteSize) { if ((image.PixelFormat != PixelFormat.Format24bppRgb) && (image.PixelFormat != PixelFormat.Format32bppRgb) && (image.PixelFormat != PixelFormat.Format32bppArgb) && (image.PixelFormat != PixelFormat.Format32bppPArgb)) { throw new UnsupportedImageFormatException("Unsupported format of the source image."); } quantizer.Clear( ); int width = image.Width; int height = image.Height; int pixelSize = Bitmap.GetPixelFormatSize(image.PixelFormat) / 8; unsafe { byte *ptr = (byte *)image.ImageData.ToPointer( ); int offset = image.Stride - width * pixelSize; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, ptr += pixelSize) { quantizer.AddColor(Color.FromArgb(ptr[RGB.R], ptr[RGB.G], ptr[RGB.B])); } ptr += offset; } } return(quantizer.GetPalette(paletteSize)); }
public unsafe Color[] CalculatePalette(UnmanagedImage image, int paletteSize) { if (image.PixelFormat != PixelFormat.Format24bppRgb && image.PixelFormat != PixelFormat.Format32bppRgb && image.PixelFormat != PixelFormat.Format32bppArgb && image.PixelFormat != PixelFormat.Format32bppPArgb) { throw new UnsupportedImageFormatException("Unsupported format of the source image."); } quantizer.Clear(); int width = image.Width; int height = image.Height; int num = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8; byte *ptr = (byte *)image.ImageData.ToPointer(); int num2 = image.Stride - width * num; for (int i = 0; i < height; i++) { int num3 = 0; while (num3 < width) { quantizer.AddColor(Color.FromArgb(ptr[2], ptr[1], *ptr)); num3++; ptr += num; } ptr += num2; } return(quantizer.GetPalette(paletteSize)); }
internal void DrawImage(Bitmap source, int x, int y, int width, int height) { var targetFormat = _bitmap.PixelFormat; List <Color> palette = null; // indexed formats require 2 passes - one more pass to determines colors for palette beforehand if (targetFormat.IsIndexed()) { _quantizer.Prepare(source); // Pass: scan ImageBuffer.ProcessPerPixel(source, null, ParallelTaskCount, (passIndex, pixel) => { var color = pixel.GetColor(); _quantizer.AddColor(color, pixel.X, pixel.Y); return(true); }); // determines palette palette = _quantizer.GetPalette(targetFormat.GetColorCount()); } // Pass: apply ImageBuffer.TransformImagePerPixel(source, palette, ref _bitmap, null, ParallelTaskCount, (passIndex, sourcePixel, targetPixel) => { var color = sourcePixel.GetColor(); targetPixel.SetColor(color, _quantizer); return(true); }); }
private void ProcessingAction(DelayedLineTask <ErrorDiffusionElement <Color, ColorComponentError>, int[]> delayedLineTask, int index) { // Get reference elements to work with var inputElement = delayedLineTask.Input[index]; var sourceColor = inputElement.Input; var error = inputElement.Error; // Add error component values to source color var errorDiffusedColor = Color.FromArgb( sourceColor.A, GetClampedValue(sourceColor.R + error.RedError, 0, 255), GetClampedValue(sourceColor.G + error.GreenError, 0, 255), GetClampedValue(sourceColor.B + error.BlueError, 0, 255)); // Quantize error diffused source color delayedLineTask.Output[index] = _quantizer.GetPaletteIndex(errorDiffusedColor); // Retrieve new quantized color for this point var targetColor = _quantizer.GetPalette()[delayedLineTask.Output[index]]; // Calculate errors to distribute for this point int redError = errorDiffusedColor.R - targetColor.R; int greenError = errorDiffusedColor.G - targetColor.G; int blueError = errorDiffusedColor.B - targetColor.B; // Retrieve point position var pixelX = index % _width; var pixelY = index / _width; // Process the matrix for (int shiftY = -MatrixSideHeight; shiftY <= MatrixSideHeight; shiftY++) { for (int shiftX = -MatrixSideWidth; shiftX <= MatrixSideWidth; shiftX++) { int targetX = pixelX + shiftX; int targetY = pixelY + shiftY; var coefficient = Matrix[shiftY + MatrixSideHeight, shiftX + MatrixSideWidth]; var errorFactor = ErrorFactorMatrix[shiftY + MatrixSideHeight, shiftX + MatrixSideWidth]; // If substantial error factor and target point in image bounds if (coefficient != 0 && targetX >= 0 && targetX < _width && targetY >= 0 && targetY < _height) { // Add error to target point for later processing var newTarget = delayedLineTask.Input[targetX + targetY * _width]; newTarget.Error.RedError += Convert.ToInt32(errorFactor * redError); newTarget.Error.GreenError += Convert.ToInt32(errorFactor * greenError); newTarget.Error.BlueError += Convert.ToInt32(errorFactor * blueError); } } } }
public List <Color> SynthetizePalette(IColorQuantizer quantizer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(quantizer, "quantizer"); // Step 1 - prepares quantizer for another round quantizer.Prepare(this); // Step 2 - scans the source image for the colors ScanColors(quantizer, parallelTaskCount); // Step 3 - synthetises the palette, and returns the result return(quantizer.GetPalette(colorCount)); }
private Image GetQuantizedImage(Image image) { // checks whether a source image is valid if (image == null) { const String message = "Cannot quantize a null image."; throw new ArgumentNullException(message); } // locks the source image data Bitmap bitmap = (Bitmap)image; Rectangle bounds = Rectangle.FromLTRB(0, 0, bitmap.Width, bitmap.Height); BitmapData sourceData = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); // prepares time statistics variables TimeSpan duration = new TimeSpan(0); DateTime before; try { // initalizes the pixel read buffer Int32[] sourceBuffer = new Int32[image.Width]; // sets the offset to the first pixel in the image Int64 sourceOffset = sourceData.Scan0.ToInt64(); for (Int32 row = 0; row < image.Height; row++) { // copies the whole row of pixels to the buffer Marshal.Copy(new IntPtr(sourceOffset), sourceBuffer, 0, image.Width); // scans all the colors in the buffer foreach (Color color in sourceBuffer.Select(argb => Color.FromArgb(argb))) { before = DateTime.Now; activeQuantizer.AddColor(color); duration += DateTime.Now - before; } // increases a source offset by a row sourceOffset += sourceData.Stride; } editTargetInfo.Text = string.Format("Quantized: {0} colors (duration {1})", 256, duration); // TODO } catch { bitmap.UnlockBits(sourceData); throw; } Bitmap result = new Bitmap(image.Width, image.Height, PixelFormat.Format8bppIndexed); // calculates the palette try { before = DateTime.Now; Int32 colorCount = GetColorCount(); List <Color> palette = activeQuantizer.GetPalette(colorCount); // sets our newly calculated palette to the target image ColorPalette imagePalette = result.Palette; duration += DateTime.Now - before; for (Int32 index = 0; index < palette.Count; index++) { imagePalette.Entries[index] = palette[index]; } result.Palette = imagePalette; } catch (Exception) { bitmap.UnlockBits(sourceData); throw; } // locks the target image data BitmapData targetData = result.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); try { // initializes read/write buffers Byte[] targetBuffer = new Byte[result.Width]; Int32[] sourceBuffer = new Int32[image.Width]; // sets the offsets on the beginning of both source and target image Int64 sourceOffset = sourceData.Scan0.ToInt64(); Int64 targetOffset = targetData.Scan0.ToInt64(); for (Int32 row = 0; row < image.Height; row++) { // reads the pixel row from the source image Marshal.Copy(new IntPtr(sourceOffset), sourceBuffer, 0, image.Width); // goes thru all the pixels, reads the color on the source image, and writes calculated palette index on the target for (Int32 index = 0; index < image.Width; index++) { Color color = Color.FromArgb(sourceBuffer[index]); before = DateTime.Now; targetBuffer[index] = (Byte)activeQuantizer.GetPaletteIndex(color); duration += DateTime.Now - before; } // writes the pixel row to the target image Marshal.Copy(targetBuffer, 0, new IntPtr(targetOffset), result.Width); // increases the offsets (on both images) by a row sourceOffset += sourceData.Stride; targetOffset += targetData.Stride; } } finally { // releases the locks on both images bitmap.UnlockBits(sourceData); result.UnlockBits(targetData); } // spits some duration statistics (those actually slow the processing quite a bit, turn them off to make it quicker) editSourceInfo.Text = string.Format("Original: {0} colors ({1} x {2})", activeQuantizer.GetColorCount(), image.Width, image.Height); // returns the quantized image return(result); }
private Image GetQuantizedImage(Image image) { if (image == null) { const String message = "Cannot quantize your file. Please choose a new file."; throw new ArgumentNullException(message); } Bitmap bitmap = (Bitmap)image; Rectangle bounds = Rectangle.FromLTRB(0, 0, bitmap.Width, bitmap.Height); BitmapData sourceData = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); try { int[] sourceBuffer = new int[image.Width]; Int64 sourceOffset = sourceData.Scan0.ToInt64(); for (int i = 0; i < image.Height; i++) { Marshal.Copy(new IntPtr(sourceOffset), sourceBuffer, 0, image.Width); foreach (Color color in sourceBuffer.Select(argb => Color.FromArgb(argb))) { activeQuantizer.AddColor(color); } sourceOffset += sourceData.Stride; } } catch { bitmap.UnlockBits(sourceData); throw; } Bitmap result = new Bitmap(image.Width, image.Height, PixelFormat.Format8bppIndexed); try { List <Color> palette = activeQuantizer.GetPalette(256); ColorPalette imagePalette = result.Palette; for (Int32 index = 0; index < palette.Count; index++) { imagePalette.Entries[index] = palette[index]; } PaletteBox.Image = GetPalette.GetPaletteBitmap(palette); result.Palette = imagePalette; } catch (Exception) { bitmap.UnlockBits(sourceData); throw; } BitmapData targetData = result.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); try { Byte[] targetBuffer = new Byte[result.Width]; int[] sourceBuffer = new int[image.Width]; Int64 sourceOffset = sourceData.Scan0.ToInt64(); Int64 targetOffset = targetData.Scan0.ToInt64(); for (int i = 0; i < image.Height; i++) { Marshal.Copy(new IntPtr(sourceOffset), sourceBuffer, 0, image.Width); for (int j = 0; j < image.Width; j++) { Color color = Color.FromArgb(sourceBuffer[j]); targetBuffer[j] = (Byte)activeQuantizer.GetPaletteIndex(color); } Marshal.Copy(targetBuffer, 0, new IntPtr(targetOffset), result.Width); sourceOffset += sourceData.Stride; targetOffset += targetData.Stride; } } finally { bitmap.UnlockBits(sourceData); result.UnlockBits(targetData); } this.Result = result; ColorDistanceBox.Text = ColorDistance.GetColorDistance(this.SourceImage, result).ToString(); return(result); }
public List<Color> SynthetizePalette(IColorQuantizer quantizer, Int32 colorCount, Int32 parallelTaskCount = 4) { // checks parameters Guard.CheckNull(quantizer, "quantizer"); // Step 1 - prepares quantizer for another round quantizer.Prepare(this); // Step 2 - scans the source image for the colors ScanColors(quantizer, parallelTaskCount); // Step 3 - synthetises the palette, and returns the result return quantizer.GetPalette(colorCount); }
public Image GetQuantizedImage(Image image) { // checks whether a source image is valid if (image == null) { const String message = "Cannot quantize a null image."; throw new ArgumentNullException(message); } // locks the source image data Bitmap bitmap = (Bitmap)image; Rectangle bounds = Rectangle.FromLTRB(0, 0, bitmap.Width, bitmap.Height); BitmapData sourceData = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); try { Int32[] sourceBuffer = new Int32[image.Width]; Int64 sourceOffset = sourceData.Scan0.ToInt64(); for (Int32 row = 0; row < image.Height; row++) { Marshal.Copy(new IntPtr(sourceOffset), sourceBuffer, 0, image.Width); foreach (Color color in sourceBuffer.Select(argb => Color.FromArgb(argb))) { quantizer.AddColor(color); } // increases a source offset by a row sourceOffset += sourceData.Stride; } } catch { bitmap.UnlockBits(sourceData); throw; } // calculates the palette Bitmap result = new Bitmap(image.Width, image.Height, PixelFormat.Format8bppIndexed); List <Color> palette = quantizer.GetPalette(256); ColorPalette imagePalette = result.Palette; for (Int32 index = 0; index < palette.Count; index++) { try { imagePalette.Entries[index] = palette[index]; } catch { } } result.Palette = imagePalette; // locks the target image data BitmapData targetData = result.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); try { Byte[] targetBuffer = new Byte[result.Width]; Int32[] sourceBuffer = new Int32[image.Width]; Int64 sourceOffset = sourceData.Scan0.ToInt64(); Int64 targetOffset = targetData.Scan0.ToInt64(); for (Int32 row = 0; row < image.Height; row++) { Marshal.Copy(new IntPtr(sourceOffset), sourceBuffer, 0, image.Width); for (Int32 index = 0; index < image.Width; index++) { Color color = Color.FromArgb(sourceBuffer[index]); targetBuffer[index] = quantizer.GetPaletteIndex(color); } Marshal.Copy(targetBuffer, 0, new IntPtr(targetOffset), result.Width); // increases the offsets by a row sourceOffset += sourceData.Stride; targetOffset += targetData.Stride; } } finally { // releases the locks on both images bitmap.UnlockBits(sourceData); result.UnlockBits(targetData); } return(result); }
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 (IEnumerable <IndexData> indeces, IList <Color> palette) Quantize(IEnumerable <Color> colors) { var data = _quantizer.Process(colors); return(data.Select(i => new IndexData(i)), _quantizer.GetPalette()); }
//TODO fix crash if bitmap is not assigned public IEnumerable <Color> GetPalette(Bitmap bitmap, IColorQuantizer quantizer, int colorCount) { var colorFrequency = ScanBitmap(bitmap); return(quantizer.GetPalette(colorCount, colorFrequency)); }
/// <summary> /// Changes the pixel format. /// </summary> /// <param name="image">The image.</param> /// <param name="targetFormat">The target format.</param> /// <param name="quantizer">The color quantizer.</param> /// <returns>The converted image in a target format.</returns> public static Image ChangePixelFormat(this Image image, PixelFormat targetFormat, IColorQuantizer quantizer) { // checks for image validity if (image == null) { const String message = "Cannot change a pixel format for a null image."; throw new ArgumentNullException(message); } // checks whether a target format is supported if (!targetFormat.IsSupported()) { String message = string.Format("A pixel format '{0}' is not supported.", targetFormat); throw new NotSupportedException(message); } // checks whether there is a quantizer for a indexed format if (targetFormat.IsIndexed() && quantizer == null) { String message = string.Format("A quantizer is cannot be null for indexed pixel format '{0}'.", targetFormat); throw new NotSupportedException(message); } // creates an image with the target format Bitmap result = new Bitmap(image.Width, image.Height, targetFormat); ColorPalette imagePalette = image.Palette; // gathers some information about the target format Boolean hasSourceAlpha = image.PixelFormat.HasAlpha(); Boolean hasTargetAlpha = targetFormat.HasAlpha(); Boolean isSourceIndexed = image.PixelFormat.IsIndexed(); Boolean isTargetIndexed = targetFormat.IsIndexed(); Boolean isSourceDeepColor = image.PixelFormat.IsDeepColor(); Boolean isTargetDeepColor = targetFormat.IsDeepColor(); // if palette is needed create one first if (isTargetIndexed) { quantizer.Prepare(image); image.AddColorsToQuantizer(quantizer); Int32 targetColorCount = result.GetPaletteColorCount(); List<Color> palette = quantizer.GetPalette(targetColorCount); result.SetPalette(palette); } Action<Pixel, Pixel> changeFormat = (sourcePixel, targetPixel) => { // if both source and target formats are deep color formats, copies a value directly if (isSourceDeepColor && isTargetDeepColor) { UInt64 value = sourcePixel.Value; targetPixel.SetValue(value); } else { // retrieves a source image color Color color = isSourceIndexed ? imagePalette.Entries[sourcePixel.Index] : sourcePixel.Color; // if alpha is not present in the source image, but is present in the target, make one up if (!hasSourceAlpha && hasTargetAlpha) { Int32 argb = 255 << 24 | color.R << 16 | color.G << 8 | color.B; color = Color.FromArgb(argb); } // sets the color to a target pixel if (isTargetIndexed) { // for the indexed images, determines a color from the octree Byte paletteIndex = (Byte) quantizer.GetPaletteIndex(color); targetPixel.SetIndex(paletteIndex); } else { // for the non-indexed images, sets the color directly targetPixel.SetColor(color); } } }; // process image -> changes format image.ProcessImagePixels(result, changeFormat); // returns the image in the target format return result; }