예제 #1
0
 public Image QuantizeImage(Bitmap image, int alphaThreshold, int alphaFader, int maxColorCount)
 {
     MaxColor = maxColorCount;
     int colorCount = maxColorCount;
     ColorData moments = QuantizerBase.CalculateMoments(QuantizerBase.BuildHistogram(image, alphaThreshold, alphaFader));
     IEnumerable<Box> cubes = this.SplitData(ref colorCount, moments);
     QuantizedPalette quantizedPalette = this.GetQuantizedPalette(colorCount, moments, cubes, alphaThreshold);
     return (Image)QuantizerBase.ProcessImagePixels((Image)image, quantizedPalette);
 }
예제 #2
0
        private static CubeCut Maximize(
            ColorData data,
            Box cube,
            int direction,
            byte first,
            byte last,
            long wholeAlpha,
            long wholeRed,
            long wholeGreen,
            long wholeBlue,
            long wholeWeight)
        {
            long  num1     = QuantizerBase.Bottom(cube, direction, data.MomentsAlpha);
            long  num2     = QuantizerBase.Bottom(cube, direction, data.MomentsRed);
            long  num3     = QuantizerBase.Bottom(cube, direction, data.MomentsGreen);
            long  num4     = QuantizerBase.Bottom(cube, direction, data.MomentsBlue);
            long  num5     = QuantizerBase.Bottom(cube, direction, data.Weights);
            float result   = 0.0f;
            byte? cutPoint = new byte?();

            for (byte index = first; (int)index < (int)last; ++index)
            {
                long num6  = num1 + QuantizerBase.Top(cube, direction, (int)index, data.MomentsAlpha);
                long num7  = num2 + QuantizerBase.Top(cube, direction, (int)index, data.MomentsRed);
                long num8  = num3 + QuantizerBase.Top(cube, direction, (int)index, data.MomentsGreen);
                long num9  = num4 + QuantizerBase.Top(cube, direction, (int)index, data.MomentsBlue);
                long num10 = num5 + QuantizerBase.Top(cube, direction, (int)index, data.Weights);
                if (num10 != 0L)
                {
                    long num11 = (num6 * num6 + num7 * num7 + num8 * num8 + num9 * num9) / num10;
                    long num12 = wholeAlpha - num6;
                    long num13 = wholeRed - num7;
                    long num14 = wholeGreen - num8;
                    long num15 = wholeBlue - num9;
                    long num16 = wholeWeight - num10;
                    if (num16 != 0L)
                    {
                        long num17 = num12 * num12 + num13 * num13 + num14 * num14 + num15 * num15;
                        long num18 = num11 + num17 / num16;
                        if ((double)num18 > (double)result)
                        {
                            result   = (float)num18;
                            cutPoint = new byte?(index);
                        }
                    }
                }
            }
            return(new CubeCut(cutPoint, result));
        }
예제 #3
0
        private bool Cut(ColorData data, ref Box first, ref Box second)
        {
            long    wholeAlpha  = QuantizerBase.Volume(first, data.MomentsAlpha);
            long    wholeRed    = QuantizerBase.Volume(first, data.MomentsRed);
            long    wholeGreen  = QuantizerBase.Volume(first, data.MomentsGreen);
            long    wholeBlue   = QuantizerBase.Volume(first, data.MomentsBlue);
            long    wholeWeight = QuantizerBase.Volume(first, data.Weights);
            CubeCut cubeCut1    = QuantizerBase.Maximize(data, first, 3, (byte)((uint)first.AlphaMinimum + 1U), first.AlphaMaximum, wholeAlpha, wholeRed, wholeGreen, wholeBlue, wholeWeight);
            CubeCut cubeCut2    = QuantizerBase.Maximize(data, first, 2, (byte)((uint)first.RedMinimum + 1U), first.RedMaximum, wholeAlpha, wholeRed, wholeGreen, wholeBlue, wholeWeight);
            CubeCut cubeCut3    = QuantizerBase.Maximize(data, first, 1, (byte)((uint)first.GreenMinimum + 1U), first.GreenMaximum, wholeAlpha, wholeRed, wholeGreen, wholeBlue, wholeWeight);
            CubeCut cubeCut4    = QuantizerBase.Maximize(data, first, 0, (byte)((uint)first.BlueMinimum + 1U), first.BlueMaximum, wholeAlpha, wholeRed, wholeGreen, wholeBlue, wholeWeight);
            int     num1;

            if ((double)cubeCut1.Value >= (double)cubeCut2.Value && (double)cubeCut1.Value >= (double)cubeCut3.Value && (double)cubeCut1.Value >= (double)cubeCut4.Value)
            {
                num1 = 3;
                byte?position = cubeCut1.Position;
                if (!(position.HasValue ? new int?((int)position.GetValueOrDefault()) : new int?()).HasValue)
                {
                    return(false);
                }
            }
            else
            {
                num1 = (double)cubeCut2.Value < (double)cubeCut1.Value || (double)cubeCut2.Value < (double)cubeCut3.Value || (double)cubeCut2.Value < (double)cubeCut4.Value ? ((double)cubeCut3.Value < (double)cubeCut1.Value || (double)cubeCut3.Value < (double)cubeCut2.Value || (double)cubeCut3.Value < (double)cubeCut4.Value ? 0 : 1) : 2;
            }
            second.AlphaMaximum = first.AlphaMaximum;
            second.RedMaximum   = first.RedMaximum;
            second.GreenMaximum = first.GreenMaximum;
            second.BlueMaximum  = first.BlueMaximum;
            switch (num1)
            {
            case 0:
                ref Box local1    = ref second;
                ref Box local2    = ref first;
                byte?   position1 = cubeCut4.Position;
                int     num2;
                byte    num3 = (byte)(num2 = (int)position1.Value);
                local2.BlueMaximum = (byte)num2;
                int num4 = (int)num3;
                local1.BlueMinimum  = (byte)num4;
                second.AlphaMinimum = first.AlphaMinimum;
                second.RedMinimum   = first.RedMinimum;
                second.GreenMinimum = first.GreenMinimum;
                break;
예제 #4
0
        public Bitmap QuantizeImage(Bitmap image, int alphaThreshold, int alphaFader, int maxColorCount)
        {
            if (image.PixelFormat != PixelFormat.Format32bppArgb)
            {
                image = image.ConvertToFormat32();
            }

            if (Program.DisableQuantization())
            {
                Console.WriteLine("[WARNING] By disabling quantization, only the first 255 unique colors will be taken into account");
                return(image);
            }

            mMaxColor = maxColorCount + 1;
            mMaxColor = Math.Min(mMaxColor, 256);
            int               colorCount       = mMaxColor;
            ColorData         moments          = QuantizerBase.CalculateMoments(QuantizerBase.BuildHistogram(image, alphaThreshold, alphaFader));
            IEnumerable <Box> cubes            = this.SplitData(ref colorCount, moments);
            QuantizedPalette  quantizedPalette = this.GetQuantizedPalette(colorCount, moments, cubes, alphaThreshold);

            return(ProcessImagePixels((Image)image, quantizedPalette));
        }