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