Beispiel #1
0
    public float LevelInterval(float _Gap)
    {
        fLevelInterval = (global::LevelInterval)_Gap;
        switch (fLevelInterval)
        {
        case global::LevelInterval.Zero:
            fLevelIntervalAttackValue = 0;
            break;

        case global::LevelInterval.One:
            fLevelIntervalAttackValue = 1;
            break;

        case global::LevelInterval.Two:
            fLevelIntervalAttackValue = 2;
            break;

        case global::LevelInterval.Three:
            fLevelIntervalAttackValue = 4;
            break;

        case global::LevelInterval.Four:
            fLevelIntervalAttackValue = 8;
            break;

        case global::LevelInterval.Five:
            fLevelIntervalAttackValue = 16;
            break;

        case global::LevelInterval.Six:
            fLevelIntervalAttackValue = 32;
            break;

        case global::LevelInterval.Seven:
            fLevelIntervalAttackValue = 64;
            break;

        case global::LevelInterval.Nine:
            fLevelIntervalAttackValue = 128;
            break;

        case global::LevelInterval.Ten:
            fLevelIntervalAttackValue = 256;
            break;

        default:
            fLevelIntervalAttackValue = 512;
            break;
        }
        return(fLevelIntervalAttackValue);
    }
Beispiel #2
0
        public static unsafe void CalculateAverageDithering(byte *pBuffer, WriteableBitmap bitmap, params object[] otherParams)
        {
            int width  = bitmap.PixelWidth;
            int height = bitmap.PixelHeight;
            List <List <LevelInterval> > levelIntervals = new List <List <LevelInterval> >();

            // Set up intervals for each channel
            for (int c = 0; c < 3; c++)
            {
                int K = (int)((int[])otherParams[0])[c];
                List <LevelInterval> channelLevelIntervals = new List <LevelInterval>();
                for (int i = 0; i < K - 1; i++)
                {
                    channelLevelIntervals.Add(new LevelInterval(255 / (K - 1), i));
                }
                levelIntervals.Add(channelLevelIntervals);
            }

            // Sum channel values for each interval to calculate threshold
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    for (int i = 0; i < 3; i++) // For each color channel
                    {
                        int channelValue  = pBuffer[4 * x + (y * bitmap.BackBufferStride) + i];
                        int intervalIndex = (int)Math.Floor((double)(channelValue / (levelIntervals[i][0].intervalLength + 1)));
                        levelIntervals[i][intervalIndex].AddPixelValueToInterval(channelValue);
                    }
                }
            }

            // After last pixel, calculate threshold
            foreach (var levelInterval in levelIntervals)
            {
                foreach (var channelLevelInterval in levelInterval)
                {
                    channelLevelInterval.CalculateAverageThreshold();
                }
            }

            // Set new values
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    for (int i = 0; i < 3; i++) // For each color channel
                    {
                        int           channelValue  = pBuffer[4 * x + (y * bitmap.BackBufferStride) + i];
                        int           intervalIndex = (int)Math.Floor((double)(channelValue / (levelIntervals[i][0].intervalLength + 1)));
                        LevelInterval interval      = levelIntervals[i][intervalIndex];

                        if (channelValue < interval.threshold)
                        {
                            pBuffer[4 * x + (y * bitmap.BackBufferStride) + i] = (byte)interval.intervalStart;
                        }
                        else
                        {
                            pBuffer[4 * x + (y * bitmap.BackBufferStride) + i] = (byte)interval.intervalEnd;
                        }
                    }
                }
            }
        }