예제 #1
0
        private ColorQuantizerResult CalcStatistics(byte[] image, List <Color> palette)
        {
            var quantizedImage = new ColorQuantizerResult(image.Length / 4);

            for (int i = 0; i < image.Length / 4; i++)
            {
                Color? nearestColor = null;
                double minDistance  = double.MaxValue;
                foreach (var color in palette)
                {
                    int r            = image[(i * 4) + 2] >> (8 - IndexBits);
                    int g            = image[(i * 4) + 1] >> (8 - IndexBits);
                    int b            = image[i * 4] >> (8 - IndexBits);
                    var currDistance = 0;
                    if (currDistance < minDistance)
                    {
                        nearestColor = color;
                        minDistance  = currDistance;
                    }
                }

                quantizedImage.IncrementColor(nearestColor.GetValueOrDefault());
            }

            return(quantizedImage);
        }
예제 #2
0
        /// <summary>
        /// Generates the quantized result.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="colorCount">The color count.</param>
        /// <param name="palette">The cube.</param>
        /// <returns>The result.</returns>
        private ColorQuantizerResult GenerateResult(byte[] image, int colorCount, ColorRange[] cube)
        {
            var quantizedImage = new ColorQuantizerResult(image.Length / 4);
            var palette        = new Color[colorCount];

            for (int k = 0; k < colorCount; k++)
            {
                Mark(cube[k], (byte)k);

                double weight = Volume(cube[k], vwt);
                byte   r      = 0;
                byte   g      = 0;
                byte   b      = 0;
                if (weight != 0)
                {
                    r = (byte)(Volume(cube[k], vmr) / weight);
                    g = (byte)(Volume(cube[k], vmg) / weight);
                    b = (byte)(Volume(cube[k], vmb) / weight);
                }
                palette[k] = Color.FromArgb(r, g, b);
            }

            for (int i = 0; i < image.Length / 4; i++)
            {
                int r = image[(i * 4) + 2] >> (8 - IndexBits);
                int g = image[(i * 4) + 1] >> (8 - IndexBits);
                int b = image[i * 4] >> (8 - IndexBits);

                int  ind   = GetIndex(r + 1, g + 1, b + 1);
                byte label = tag[ind];
                var  color = palette[label];
                quantizedImage.IncrementColor(color);
            }

            return(quantizedImage);
        }