Example #1
0
        /// <summary>
        /// Average Color Filter
        /// </summary>
        /// <param name="sourceBitmap">Set source Bitmap</param>
        /// <param name="matrixSize">Set matrix size</param>
        /// <param name="applyBlue">Apply Blue</param>
        /// <param name="applyGreen">Apply Green</param>
        /// <param name="applyRed">Apply Red</param>
        /// <param name="shiftType">Set shift type</param>
        /// <returns></returns>
        public static System.Drawing.Bitmap AverageColoursFilter(this System.Drawing.Bitmap sourceBitmap,
                                                                 int matrixSize,
                                                                 bool applyBlue           = true,
                                                                 bool applyGreen          = true,
                                                                 bool applyRed            = true,
                                                                 ColorShiftType shiftType =
                                                                 ColorShiftType.None)
        {
            byte[] pixelBuffer  = sourceBitmap.GETByteArray();
            byte[] resultBuffer = new byte[pixelBuffer.Length];

            int calcOffset = 0;
            int byteOffset = 0;
            int blue = 0; int green = 0; int red = 0;
            int filterOffset = (matrixSize - 1) / 2;

            for (int offsetY = filterOffset; offsetY <
                 sourceBitmap.Height - filterOffset; offsetY++)
            {
                for (int offsetX = filterOffset; offsetX <
                     sourceBitmap.Width - filterOffset; offsetX++)
                {
                    byteOffset = offsetY * sourceBitmap.Width * 4 +
                                 offsetX * 4;

                    blue = 0; green = 0; red = 0;

                    for (int filterY = -filterOffset;
                         filterY <= filterOffset; filterY++)
                    {
                        for (int filterX = -filterOffset;
                             filterX <= filterOffset; filterX++)
                        {
                            calcOffset = byteOffset +
                                         (filterX * 4) +
                                         (filterY * sourceBitmap.Width * 4);

                            blue  += pixelBuffer[calcOffset];
                            green += pixelBuffer[calcOffset + 1];
                            red   += pixelBuffer[calcOffset + 2];
                        }
                    }

                    blue  = blue / matrixSize;
                    green = green / matrixSize;
                    red   = red / matrixSize;

                    if (applyBlue == false)
                    {
                        blue = pixelBuffer[byteOffset];
                    }

                    if (applyGreen == false)
                    {
                        green = pixelBuffer[byteOffset + 1];
                    }

                    if (applyRed == false)
                    {
                        red = pixelBuffer[byteOffset + 2];
                    }

                    if (shiftType == ColorShiftType.None)
                    {
                        resultBuffer[byteOffset]     = (byte)blue;
                        resultBuffer[byteOffset + 1] = (byte)green;
                        resultBuffer[byteOffset + 2] = (byte)red;
                        resultBuffer[byteOffset + 3] = 255;
                    }
                    else if (shiftType == ColorShiftType.ShiftLeft)
                    {
                        resultBuffer[byteOffset]     = (byte)green;
                        resultBuffer[byteOffset + 1] = (byte)red;
                        resultBuffer[byteOffset + 2] = (byte)blue;
                        resultBuffer[byteOffset + 3] = 255;
                    }
                    else if (shiftType == ColorShiftType.ShiftRight)
                    {
                        resultBuffer[byteOffset]     = (byte)red;
                        resultBuffer[byteOffset + 1] = (byte)blue;
                        resultBuffer[byteOffset + 2] = (byte)green;
                        resultBuffer[byteOffset + 3] = 255;
                    }
                }
            }

            System.Drawing.Bitmap resultBitmap = new System.Drawing.Bitmap(sourceBitmap.Width,
                                                                           sourceBitmap.Height);

            BitmapData resultData =
                resultBitmap.LockBits(new Rectangle(0, 0,
                                                    resultBitmap.Width, resultBitmap.Height),
                                      ImageLockMode.WriteOnly,
                                      PixelFormat.Format32bppArgb);

            Marshal.Copy(resultBuffer, 0, resultData.Scan0,
                         resultBuffer.Length);

            resultBitmap.UnlockBits(resultData);

            return(resultBitmap);
        }
Example #2
0
        /// <summary>
        /// Color Filter
        /// </summary>
        /// <param name="sourceBitmap">Set source Bitmap</param>
        /// <param name="matrixSize">Set matrix size</param>
        /// <param name="edgeThreshold">Set edge threshold</param>
        /// <param name="applyBlue">Apply Blue</param>
        /// <param name="applyGreen">Apply Green</param>
        /// <param name="applyRed">Apply Red</param>
        /// <param name="edgeType">Set edge type</param>
        /// <param name="shiftType">Set shift type</param>
        /// <returns></returns>
        public static System.Drawing.Bitmap AbstractColorsFilter(this System.Drawing.Bitmap sourceBitmap,
                                                                 int matrixSize,
                                                                 byte edgeThreshold,
                                                                 bool applyBlue           = true,
                                                                 bool applyGreen          = true,
                                                                 bool applyRed            = true,
                                                                 EdgeTracingType edgeType =
                                                                 EdgeTracingType.Black,
                                                                 ColorShiftType shiftType =
                                                                 ColorShiftType.None)
        {
            System.Drawing.Bitmap edgeBitmap =
                sourceBitmap.GradientBasedEdgeDetectionFilter(edgeThreshold);

            System.Drawing.Bitmap colorBitmap =
                sourceBitmap.AverageColoursFilter(matrixSize,
                                                  applyBlue, applyGreen, applyRed, shiftType);

            byte[] edgeBuffer   = edgeBitmap.GETByteArray();
            byte[] colorBuffer  = colorBitmap.GETByteArray();
            byte[] resultBuffer = colorBitmap.GETByteArray();

            for (int k = 0; k + 4 < edgeBuffer.Length; k += 4)
            {
                if (edgeBuffer[k] == 255)
                {
                    switch (edgeType)
                    {
                    case EdgeTracingType.Black:
                        resultBuffer[k]     = 0;
                        resultBuffer[k + 1] = 0;
                        resultBuffer[k + 2] = 0;
                        break;

                    case EdgeTracingType.White:
                        resultBuffer[k]     = 255;
                        resultBuffer[k + 1] = 255;
                        resultBuffer[k + 2] = 255;
                        break;

                    case EdgeTracingType.HalfIntensity:
                        resultBuffer[k]     = ClipByte(resultBuffer[k] * 0.5);
                        resultBuffer[k + 1] = ClipByte(resultBuffer[k + 1] * 0.5);
                        resultBuffer[k + 2] = ClipByte(resultBuffer[k + 2] * 0.5);
                        break;

                    case EdgeTracingType.DoubleIntensity:
                        resultBuffer[k]     = ClipByte(resultBuffer[k] * 2);
                        resultBuffer[k + 1] = ClipByte(resultBuffer[k + 1] * 2);
                        resultBuffer[k + 2] = ClipByte(resultBuffer[k + 2] * 2);
                        break;

                    case EdgeTracingType.ColorInversion:
                        resultBuffer[k]     = ClipByte(255 - resultBuffer[k]);
                        resultBuffer[k + 1] = ClipByte(255 - resultBuffer[k + 1]);
                        resultBuffer[k + 2] = ClipByte(255 - resultBuffer[k + 2]);
                        break;
                    }
                }
            }

            System.Drawing.Bitmap resultBitmap = new System.Drawing.Bitmap(sourceBitmap.Width,
                                                                           sourceBitmap.Height);

            BitmapData resultData =
                resultBitmap.LockBits(new Rectangle(0, 0,
                                                    resultBitmap.Width, resultBitmap.Height),
                                      ImageLockMode.WriteOnly,
                                      PixelFormat.Format32bppArgb);

            Marshal.Copy(resultBuffer, 0, resultData.Scan0,
                         resultBuffer.Length);

            resultBitmap.UnlockBits(resultData);

            return(resultBitmap);
        }