/// <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)); }
private void UpdateImages() { activeQuantizer.Clear(); try { if (listMethod.SelectedIndex <= 1) { Image targetImage = GetQuantizedImage(SourceImage); pictureSource.Image = SourceImage; ImageSizeLabel.Text = SourceImage.Width + " x " + SourceImage.Height; pictureTarget.Image = targetImage; } else { Bitmap b = new Bitmap(Ditherhelper.Dithering(SourceImage)); ColorDistanceBox.Text = ColorDistance.GetColorDistance(this.SourceImage, b).ToString(); pictureTarget.Image = b; pictureSource.Image = SourceImage; ImageSizeLabel.Text = SourceImage.Width + " x " + SourceImage.Height; //GetPaletteBitmap(palette); } } catch (NotSupportedException ex) { MessageBox.Show(ex.Message); } }
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)); }
private void UpdateImages() { // prepares quantizer activeQuantizer.Clear(); // updates source image UpdateSourceImage(); // tries to retrieve an image based on HSB quantization try { DateTime before = DateTime.Now; Image targetImage = GetQuantizedImage(sourceImage); TimeSpan duration = DateTime.Now - before; TimeSpan perPixel = new TimeSpan(duration.Ticks / (sourceImage.Width * sourceImage.Height)); pictureTarget.Image = targetImage; // new GIF and PNG sizes Int32 newGifSize, newPngSize; // retrieves a GIF image based on our HSB-quantized one GetConvertedImage(targetImage, ImageFormat.Gif, out newGifSize); // retrieves a PNG image based on our HSB-quantized one GetConvertedImage(targetImage, ImageFormat.Png, out newPngSize); // spits out the statistics Text = string.Format("Simple palette quantizer (duration {0}, per pixel {1})", duration, perPixel); editProjectedGifSize.Text = projectedGifSize.ToString(); editProjectedPngSize.Text = sourceFileInfo.Length.ToString(); editNewGifSize.Text = newGifSize.ToString(); editNewPngSize.Text = newPngSize.ToString(); } catch (NotSupportedException ex) { MessageBox.Show(ex.Message); } }
public void UpdateImages() { quantizer = new PaletteQuantizer(); quantizer.Clear(); ergebnis = GetQuantizedImage(sourceImage); }