Exemplo n.º 1
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);
        }
        /// <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);
        }