コード例 #1
0
ファイル: Graphics.cs プロジェクト: mcintyre321/aforge
        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);
            });
        }
コード例 #2
0
ファイル: ImageBuffer.cs プロジェクト: nyaachen/PixelStacker
        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));
        }
コード例 #3
0
ファイル: Bitmap.cs プロジェクト: eunkyoumkim/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);
            });
        }
コード例 #4
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;
            });
        }
コード例 #5
0
ファイル: ImageBuffer.cs プロジェクト: RHY3756547/FreeSO
        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);
        }
コード例 #6
0
    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();
    }
コード例 #7
0
        /// <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;
        }