/// <summary>
        /// Adds all the colors from a source image to a given color quantizer.
        /// </summary>
        /// <param name="image">The image to be processed.</param>
        /// <param name="quantizer">The target color quantizer.</param>
        public static void AddColorsToQuantizer(this Image image, IColorQuantizer quantizer)
        {
            // checks whether a source image is valid
            if (image == null)
            {
                const String message = "Cannot add colors from a null image.";
                throw new ArgumentNullException(message);
            }

            // checks whether the quantizer is valid
            if (quantizer == null)
            {
                const String message = "Cannot add colors to a null quantizer.";
                throw new ArgumentNullException(message);
            }

            // determines which method of color retrieval to use
            Boolean isImageIndexed = image.PixelFormat.IsIndexed();
            ColorPalette palette = image.Palette;

            // use different scanning method depending whether the image format is indexed
            Action<Pixel> scan = isImageIndexed ? (Action<Pixel>)
                (pixel => quantizer.AddColor(palette.Entries[pixel.Index])) :
                (pixel => quantizer.AddColor(pixel.Color));

            // performs the image scan, using a chosen method
            image.ProcessImagePixels(ImageLockMode.ReadOnly, scan);
        }
예제 #2
0
        /// ============================================================
        /// <summary>加载图形的优化器颜色。</summary>
        /// <param name="bitmap">图形对象</param>
        /// <param name="modeCd">颜色模式</param>
        /// <returns>颜色优化器</returns>
        /// ============================================================
        public IColorQuantizer LoadQuantizerColors(Bitmap bitmap, EQuantizedMode modeCd)
        {
            // 检查参数错误
            if (null == bitmap)
            {
                throw new ArgumentNullException("Cannot quantize a null bitmap.");
            }
            // 锁定原图像,获得所有颜色
            int        width  = bitmap.Width;
            int        height = bitmap.Height;
            Rectangle  bounds = Rectangle.FromLTRB(0, 0, width, height);
            BitmapData data   = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            try {
                // 读取所有颜色
                int[] buffer = new int[width];
                long  offset = data.Scan0.ToInt64();
                for (int row = 0; row < height; row++)
                {
                    // 复制一行颜色到缓冲中
                    Marshal.Copy(new IntPtr(offset), buffer, 0, width);
                    // 增加到优化器中
                    for (int n = 0; n < width; n++)
                    {
                        Color color = Color.FromArgb(buffer[n]);
                        switch (modeCd)
                        {
                        case EQuantizedMode.Color:
                            break;

                        case EQuantizedMode.Rgb:
                            color = Color.FromArgb(255, color);
                            break;

                        case EQuantizedMode.Alpha:
                            color = Color.FromArgb(255, color.A, color.A, color.A);
                            break;

                        default:
                            throw new FFatalException("Unknown type");
                        }
                        _quantizer.AddColor(color);
                    }
                    // 修正行边界
                    offset += data.Stride;
                }
            } finally {
                // 释放图像对象
                bitmap.UnlockBits(data);
            }
            return(_quantizer);
        }
예제 #3
0
        /// <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));
        }
예제 #4
0
        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);
            });
        }
예제 #5
0
        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));
        }
예제 #6
0
        private void UpdateQuantizer()
        {
            if (!_pixelFormat.IsIndexed() || _quantizer != null)
            {
                return;
            }

            _quantizer = new DistinctSelectionQuantizer();
            _quantizer.Prepare(this);

            // Pass: scan
            ImageBuffer.ProcessPerPixel(this, null, ParallelTaskCount, (passIndex, pixel) =>
            {
                var color = pixel.GetColor();
                _quantizer.AddColor(color, pixel.X, pixel.Y);
                return(true);
            });
        }
예제 #7
0
        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);
        }
예제 #8
0
        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);
        }
예제 #9
0
파일: Bitmap.cs 프로젝트: hungdluit/aforge
        private void UpdateQuantizer()
        {
            if (!_pixelFormat.IsIndexed() || _quantizer != null) return;

            _quantizer = new DistinctSelectionQuantizer();
            _quantizer.Prepare(this);

            // Pass: scan
            ImageBuffer.ProcessPerPixel(this, null, ParallelTaskCount, (passIndex, pixel) =>
            {
                var color = pixel.GetColor();
                _quantizer.AddColor(color, pixel.X, pixel.Y);
                return true;
            });
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }
예제 #12
0
        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();
    }