/// <summary>
        /// Gets the index of the palette for specific color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public Int32 GetPaletteIndex(Color color)
        {
            color = QuantizationHelper.ConvertAlpha(color);

            // retrieves a palette index
            return(root.GetPaletteIndex(color, 0));
        }
Esempio n. 2
0
        /// <summary>
        /// See <see cref="IColorQuantizer.AddColor"/> for more details.
        /// </summary>
        public void AddColor(Color color, Int32 x, Int32 y)
        {
            Int32 key;

            color = QuantizationHelper.ConvertAlpha(color, out key);
            OnAddColor(color, key, x, y);
        }
Esempio n. 3
0
 /// <summary>
 /// See <see cref="INonIndexedPixel.SetColor"/> for more details.
 /// </summary>
 public void SetColor(Color color)
 {
     color = QuantizationHelper.ConvertAlpha(color);
     red   = (UInt16)(color.R << 5);
     green = (UInt16)(color.G << 5);
     blue  = (UInt16)(color.B << 5);
 }
Esempio n. 4
0
 /// <summary>
 /// See <see cref="INonIndexedPixel.SetColor"/> for more details.
 /// </summary>
 public void SetColor(Color color)
 {
     color = QuantizationHelper.ConvertAlpha(color);
     red   = color.R;
     green = color.G;
     blue  = color.B;
 }
Esempio n. 5
0
        /// <summary>
        /// See <see cref="IColorQuantizer.GetPaletteIndex"/> for more details.
        /// </summary>
        public Int32 GetPaletteIndex(Color color, Int32 x, Int32 y)
        {
            Int32 result, key;

            color = QuantizationHelper.ConvertAlpha(color, out key);
            OnGetPaletteIndex(color, key, x, y, out result);
            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the index of the palette for specific color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public Int32 GetPaletteIndex(Color color)
        {
            color = QuantizationHelper.ConvertAlpha(color);
            Int32 redIndex   = color.R >> 5;
            Int32 greenIndex = color.G >> 5;
            Int32 blueIndex  = color.B >> 6;

            return((redIndex << 5) + (greenIndex << 2) + blueIndex);
        }
Esempio n. 7
0
        /// <summary>
        /// See <see cref="BaseColorQuantizer.OnAddColor"/> for more details.
        /// </summary>
        protected override void OnAddColor(Color color, Int32 key, Int32 x, Int32 y)
        {
#if (UseDictionary)
            colorMap.AddOrUpdate(key,
                                 colorKey => new DistinctColorInfo(color),
                                 (colorKey, colorInfo) => colorInfo.IncreaseCount());
#else
            color = QuantizationHelper.ConvertAlpha(color);
            rootBucket.StoreColor(color);
#endif
        }
Esempio n. 8
0
        private void ProcessNeighbor(Pixel targetPixel, Int32 x, Int32 y, Single factor, Int32 redError, Int32 greenError, Int32 blueError)
        {
            Color oldColor = TargetBuffer.ReadColorUsingPixelFrom(targetPixel, x, y);

            oldColor = QuantizationHelper.ConvertAlpha(oldColor);
            Int32 red      = GetClampedColorElementWithError(oldColor.R, factor, redError);
            Int32 green    = GetClampedColorElementWithError(oldColor.G, factor, greenError);
            Int32 blue     = GetClampedColorElementWithError(oldColor.B, factor, blueError);
            Color newColor = Color.FromArgb(255, red, green, blue);

            TargetBuffer.WriteColorUsingPixelAt(targetPixel, x, y, newColor, Quantizer);
        }
Esempio n. 9
0
        /// <summary>
        /// Adds the color to quantizer.
        /// </summary>
        /// <param name="color">The color to be added.</param>
        public void AddColor(Color color)
        {
            color = QuantizationHelper.ConvertAlpha(color);

            Int32 redIndex   = color.R >> 5;
            Int32 greenIndex = color.G >> 5;
            Int32 blueIndex  = color.B >> 6;

            redSlots[redIndex].AddValue(color.R);
            greenSlots[greenIndex].AddValue(color.G);
            blueSlots[blueIndex].AddValue(color.B);
        }
Esempio n. 10
0
        /// <summary>
        /// See <see cref="IColorQuantizer.GetPaletteIndex"/> for more details.
        /// </summary>
        public void GetPaletteIndex(Color color, out Int32 paletteIndex)
        {
            paletteIndex = 0;
            color        = QuantizationHelper.ConvertAlpha(color);

            foreach (MedianCutCube cube in cubeList)
            {
                if (cube.IsColorIn(color))
                {
                    paletteIndex = cube.PaletteIndex;
                    break;
                }
            }
        }
        public int GetPaletteIndex(Color color)
        {
            int result;

            color = QuantizationHelper.ConvertAlpha(color);

            if (!cache.TryGetValue(color, out result))
            {
                result       = QuantizationHelper.GetNearestColor(color, palette);
                cache[color] = result;
            }

            return(result);
        }
Esempio n. 12
0
        /// <summary>
        /// Adds the color to quantizer, only unique colors are added.
        /// </summary>
        /// <param name="color">The color to be added.</param>
        public void AddColor(Color color)
        {
            // if alpha is higher then fully transparent, convert it to a RGB value for more precise processing
            color = QuantizationHelper.ConvertAlpha(color);
            ColorInfo value;

            if (colorMap.TryGetValue(color, out value))
            {
                value.IncreaseCount();
            }
            else
            {
                ColorInfo colorInfo = new ColorInfo(color);
                colorMap.Add(color, colorInfo);
            }
        }
        public void AddColor(Color color)
        {
            PopularityColorSlot slot;

            color = QuantizationHelper.ConvertAlpha(color);
            int index = GetColorIndex(color);

            if (colorMap.TryGetValue(index, out slot))
            {
                slot.AddValue(color);
            }
            else
            {
                colorMap[index] = new PopularityColorSlot(color);
            }
        }
Esempio n. 14
0
        /// <summary>
        /// See <see cref="BaseColorDitherer.OnProcessPixel"/> for more details.
        /// </summary>
        protected override Boolean OnProcessPixel(Pixel sourcePixel, Pixel targetPixel)
        {
            // only process dithering when reducing from truecolor to indexed
            if (!TargetBuffer.IsIndexed)
            {
                return(false);
            }

            // retrieves the colors
            Color sourceColor = SourceBuffer.GetColorFromPixel(sourcePixel);
            Color targetColor = TargetBuffer.GetColorFromPixel(targetPixel);

            // converts alpha to solid color
            sourceColor = QuantizationHelper.ConvertAlpha(sourceColor);

            // calculates the difference (error)
            Int32 redError   = sourceColor.R - targetColor.R;
            Int32 greenError = sourceColor.G - targetColor.G;
            Int32 blueError  = sourceColor.B - targetColor.B;

            // only propagate non-zero error
            if (redError != 0 || greenError != 0 || blueError != 0)
            {
                // processes the matrix
                for (Int32 shiftY = -MatrixSideHeight; shiftY <= MatrixSideHeight; shiftY++)
                {
                    for (Int32 shiftX = -MatrixSideWidth; shiftX <= MatrixSideWidth; shiftX++)
                    {
                        Int32  targetX          = sourcePixel.X + shiftX;
                        Int32  targetY          = sourcePixel.Y + shiftY;
                        Byte   coeficient       = CachedMatrix[shiftY + MatrixSideHeight, shiftX + MatrixSideWidth];
                        Single coeficientSummed = CachedSummedMatrix[shiftY + MatrixSideHeight, shiftX + MatrixSideWidth];

                        if (coeficient != 0 &&
                            targetX >= 0 && targetX < TargetBuffer.Width &&
                            targetY >= 0 && targetY < TargetBuffer.Height)
                        {
                            ProcessNeighbor(targetPixel, targetX, targetY, coeficientSummed, redError, greenError, blueError);
                        }
                    }
                }
            }

            // pixels are not processed, only neighbors are
            return(false);
        }
Esempio n. 15
0
        /// <summary>
        /// Gets the index of the palette for specific color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public Int32 GetPaletteIndex(Color color)
        {
            Int32 result;

            color = QuantizationHelper.ConvertAlpha(color);

            // checks whether color was already requested, in that case returns an index from a cache
            if (!cache.TryGetValue(color, out result))
            {
                // otherwise finds the nearest color
                result       = QuantizationHelper.GetNearestColor(color, palette);
                cache[color] = result;
            }

            // returns a palette index
            return(result);
        }
Esempio n. 16
0
        /// <summary>
        /// See <see cref="IColorQuantizer.Prepare"/> for more details.
        /// </summary>
        public void AddColor(Color color)
        {
            color = QuantizationHelper.ConvertAlpha(color);

            Int32 indexRed   = (color.R >> 3) + 1;
            Int32 indexGreen = (color.G >> 3) + 1;
            Int32 indexBlue  = (color.B >> 3) + 1;

            weights[indexRed, indexGreen, indexBlue]++;
            momentsRed[indexRed, indexGreen, indexBlue]   += color.R;
            momentsGreen[indexRed, indexGreen, indexBlue] += color.G;
            momentsBlue[indexRed, indexGreen, indexBlue]  += color.B;
            moments[indexRed, indexGreen, indexBlue]      += table[color.R] + table[color.G] + table[color.B];

            quantizedPixels[pixelIndex] = (indexRed << 10) + (indexRed << 6) + indexRed + (indexGreen << 5) + indexGreen + indexBlue;
            pixels[pixelIndex]          = color;
            pixelIndex++;
        }
Esempio n. 17
0
        /// <summary>
        /// Gets the index of the palette for specific color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public Int32 GetPaletteIndex(Color color)
        {
            Int32 result;

            color = QuantizationHelper.ConvertAlpha(color);

            if (!cache.TryGetValue(color, out result))
            {
                foreach (MedianCutCube cube in cubeList)
                {
                    if (cube.IsColorIn(color))
                    {
                        result = cube.PaletteIndex;
                        // break;
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// See <see cref="BaseColorDitherer.OnProcessPixel"/> for more details.
        /// </summary>
        protected override Boolean OnProcessPixel(Pixel sourcePixel, Pixel targetPixel)
        {
            // reads the source pixel
            Color oldColor = SourceBuffer.GetColorFromPixel(sourcePixel);

            // converts alpha to solid color
            oldColor = QuantizationHelper.ConvertAlpha(oldColor);

            // retrieves matrix coordinates
            Int32 x = targetPixel.X % MatrixWidth;
            Int32 y = targetPixel.Y % MatrixHeight;

            // determines the threshold
            Int32 threshold = Convert.ToInt32(CachedMatrix[x, y]);

            // only process dithering if threshold is substantial
            if (threshold > 0)
            {
                Int32 red   = GetClampedColorElement(oldColor.R + threshold);
                Int32 green = GetClampedColorElement(oldColor.G + threshold);
                Int32 blue  = GetClampedColorElement(oldColor.B + threshold);

                Color newColor = Color.FromArgb(255, red, green, blue);

                if (TargetBuffer.IsIndexed)
                {
                    Byte newPixelIndex = (Byte)Quantizer.GetPaletteIndex(newColor, targetPixel.X, targetPixel.Y);
                    targetPixel.Index = newPixelIndex;
                }
                else
                {
                    targetPixel.Color = newColor;
                }
            }

            // writes the process pixel
            return(true);
        }
Esempio n. 19
0
 /// <summary>
 /// See <see cref="INonIndexedPixel.SetColor"/> for more details.
 /// </summary>
 public void SetColor(Color color)
 {
     color = QuantizationHelper.ConvertAlpha(color);
     raw   = (UInt16)((color.R >> 3) << 10 | (color.G >> 3) << 5 | color.B >> 3);
 }
 /// <summary>
 /// Adds the color to quantizer.
 /// </summary>
 /// <param name="color">The color to be added.</param>
 public void AddColor(Color color)
 {
     color = QuantizationHelper.ConvertAlpha(color);
     root.AddColor(color, 0, this);
 }
Esempio n. 21
0
 /// <summary>
 /// See <see cref="INonIndexedPixel.SetColor"/> for more details.
 /// </summary>
 public void SetColor(Color color)
 {
     color = QuantizationHelper.ConvertAlpha(color);
     raw   = (UInt16)((color.A < 255 ? Pixel.Zero : Pixel.One) << 15 | (color.R >> 3) << 10 | (color.G >> 3) << 5 | (color.B >> 3));
 }
Esempio n. 22
0
 /// <summary>
 /// Adds the color to quantizer.
 /// </summary>
 /// <param name="color">The color to be added.</param>
 public void AddColor(Color color)
 {
     color = QuantizationHelper.ConvertAlpha(color);
     colorList.Add(color);
 }
Esempio n. 23
0
 /// <summary>
 /// See <see cref="INonIndexedPixel.SetColor"/> for more details.
 /// </summary>
 public void SetColor(Color color)
 {
     color = QuantizationHelper.ConvertAlpha(color);
     raw   = color.ToArgb() & Pixel.RedGreenBlueMask;
 }