Exemplo n.º 1
0
        public IEnumerable <int> Process(IEnumerable <Color> colors)
        {
            var colorList = colors.ToArray();

            _quantizer.CreatePalette(colorList);

            var processingAction = new Action <LineTask <Color[], int[]> >(taskModel =>
            {
                var matrixWidth  = Matrix.GetLength(0);
                var matrixHeight = Matrix.GetLength(1);

                for (int i = taskModel.Start; i < taskModel.Start + taskModel.Length; i++)
                {
                    int x = i % _width % matrixWidth;
                    int y = i / _width % matrixHeight;

                    int threshold = Convert.ToInt32(Matrix[x, y]);
                    var color     = taskModel.Input[i];

                    int red   = GetClampedValue(color.R + threshold, 0, 255);
                    int green = GetClampedValue(color.G + threshold, 0, 255);
                    int blue  = GetClampedValue(color.B + threshold, 0, 255);

                    taskModel.Output[i] = _quantizer.GetPaletteIndex(Color.FromArgb(color.A, red, green, blue));
                }
            });

            var indices = new int[colorList.Length];

            ParallelProcessing.ProcessList(colorList, indices, processingAction, _quantizer.TaskCount);

            return(indices);
        }
Exemplo n.º 2
0
        public Bitmap GenerateNewBitmap(Bitmap bitmap, IColorQuantizer quantizer, int colorCount)
        {
            var colors = GetPalette(bitmap, quantizer, colorCount).ToArray();
            Func <Color, Color> func = c => colors[quantizer.GetPaletteIndex(c)];

            return(_imageBuilder.BuildBitmap(bitmap, func));
        }
        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);
                    }
                }
            }
        }
Exemplo n.º 4
0
 public void SetColor(Color color, IColorQuantizer quantizer)
 {
     // determines whether the format is indexed
     if (IsIndexed)
     {
         // last chance if quantizer is provided, use it
         if (quantizer != null)
         {
             Byte index = (Byte)quantizer.GetPaletteIndex(color, X, Y);
             ((IIndexedPixel)pixelData).SetIndex(bitOffset, index);
         }
         else // cannot write color to an index format
         {
             String message = string.Format("Cannot retrieve color for an indexed format. Use GetPixelIndex() instead.");
             throw new NotSupportedException(message);
         }
     }
     else // sets color to a non-indexed format
     {
         ((INonIndexedPixel)pixelData).SetColor(color);
     }
 }
Exemplo n.º 5
0
 public void SetColorToPixel(Pixel pixel, Color color, IColorQuantizer quantizer)
 {
     // determines whether the format is indexed
     if (pixel.IsIndexed)
     {
         // last chance if quantizer is provided, use it
         if (quantizer != null)
         {
             Byte index = (Byte)quantizer.GetPaletteIndex(color, pixel.X, pixel.Y);
             pixel.Index = index;
         }
         else // cannot write color to an index format
         {
             String message = string.Format("Cannot retrieve color for an indexed format. Use GetPixelIndex() instead.");
             throw new NotSupportedException(message);
         }
     }
     else // sets color to a non-indexed format
     {
         pixel.Color = color;
     }
 }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
 public void SetColor(Color color, IColorQuantizer quantizer)
 {
     // determines whether the format is indexed
     if (IsIndexed)
     {
         // last chance if quantizer is provided, use it
         if (quantizer != null)
         {
             Byte index = (Byte) quantizer.GetPaletteIndex(color, X, Y);
             ((IIndexedPixel) pixelData).SetIndex(bitOffset, index);
         }
         else // cannot write color to an index format
         {
             String message = string.Format("Cannot retrieve color for an indexed format. Use GetPixelIndex() instead.");
             throw new NotSupportedException(message);
         }
     }
     else // sets color to a non-indexed format
     {
         ((INonIndexedPixel) pixelData).SetColor(color);
     }
 }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
 public void SetColorToPixel(Pixel pixel, Color color, IColorQuantizer quantizer)
 {
     // determines whether the format is indexed
     if (pixel.IsIndexed)
     {
         // last chance if quantizer is provided, use it
         if (quantizer != null)
         {
             Byte index = (Byte)quantizer.GetPaletteIndex(color, pixel.X, pixel.Y);
             pixel.Index = index;
         }
         else // cannot write color to an index format
         {
             String message = string.Format("Cannot retrieve color for an indexed format. Use GetPixelIndex() instead.");
             throw new NotSupportedException(message);
         }
     }
     else // sets color to a non-indexed format
     {
         pixel.Color = color;
     }
 }
Exemplo n.º 10
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();
    }
Exemplo n.º 12
0
 public static byte GetPaletteIndex(IColorQuantizer q, int x, int y, int r, int g, int b)
 {
     return((byte)q.GetPaletteIndex(Color.FromArgb(r, g, b), x, y));
 }
        /// <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;
        }