Ejemplo n.º 1
0
        /// <summary>
        /// Gets distance between two colours using Euclidean distance function.
        /// Distance = SQRT( (R1-R2)2 + (G1-G2)2 + (B1-B2)2 )
        /// </summary>
        /// <param name="color1">First Color.</param>
        /// <param name="color2">Second Color.</param>
        /// <returns>Distance between colours.</returns>
        private static float ColorDistance(DFBitmap.DFColor color1, DFBitmap.DFColor color2)
        {
            float r = color1.r - color2.r;
            float g = color1.g - color2.g;
            float b = color1.b - color2.b;

            return((float)Mathf.Sqrt((r * r) + (g * g) + (b * b)));
        }
Ejemplo n.º 2
0
        /*
         * Based on Craig's Utility Library (CUL) by James Craig.
         * http://www.gutgames.com/post/Edge-detection-in-C.aspx
         * MIT License (http://www.opensource.org/licenses/mit-license.php)
         */

        /// <summary>
        /// Gets a new bitmap containing edges detected in source bitmap.
        /// The source bitmap is unchanged.
        /// </summary>
        /// <param name="bitmap">DFBitmap source.</param>
        /// <param name="threshold">Edge detection threshold.</param>
        /// <param name="edgeColor">Edge colour to write.</param>
        /// <returns>DFBitmap containing edges.</returns>
        private static DFBitmap FindEdges(DFBitmap bitmap, float threshold, DFBitmap.DFColor edgeColor)
        {
            // Must be a colour format
            if (bitmap.Format == DFBitmap.Formats.Indexed)
            {
                return(null);
            }

            // Clone bitmap settings
            DFBitmap newBitmap = DFBitmap.CloneDFBitmap(bitmap);

            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    DFBitmap.DFColor currentColor = DFBitmap.GetPixel(bitmap, x, y);
                    if (y < newBitmap.Height - 1 && x < newBitmap.Width - 1)
                    {
                        DFBitmap.DFColor tempColor = DFBitmap.GetPixel(bitmap, x + 1, y + 1);
                        if (ColorDistance(currentColor, tempColor) > threshold)
                        {
                            DFBitmap.SetPixel(newBitmap, x, y, edgeColor);
                        }
                    }
                    else if (y < newBitmap.Height - 1)
                    {
                        DFBitmap.DFColor tempColor = DFBitmap.GetPixel(bitmap, x, y + 1);
                        if (ColorDistance(currentColor, tempColor) > threshold)
                        {
                            DFBitmap.SetPixel(newBitmap, x, y, edgeColor);
                        }
                    }
                    else if (x < newBitmap.Width - 1)
                    {
                        DFBitmap.DFColor tempColor = DFBitmap.GetPixel(bitmap, x + 1, y);
                        if (ColorDistance(currentColor, tempColor) > threshold)
                        {
                            DFBitmap.SetPixel(newBitmap, x, y, edgeColor);
                        }
                    }
                }
            }

            return(newBitmap);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates average intensity bitmap.
        /// </summary>
        /// <param name="bitmap">Source DFBitmap.</param>
        /// <returns>Average intensity DFBitmap.</returns>
        public static DFBitmap MakeAverageIntensityBitmap(DFBitmap bitmap)
        {
            // Must be a colour format
            if (bitmap.Format == DFBitmap.Formats.Indexed)
            {
                return(null);
            }

            DFBitmap newBitmap = new DFBitmap();

            newBitmap.Format = bitmap.Format;
            newBitmap.Initialise(bitmap.Width, bitmap.Height);

            // Make each pixel grayscale based on average intensity
            int srcPos = 0, dstPos = 0;

            for (int i = 0; i < bitmap.Width * bitmap.Height; i++)
            {
                // Get source color
                byte r = bitmap.Data[srcPos++];
                byte g = bitmap.Data[srcPos++];
                byte b = bitmap.Data[srcPos++];
                byte a = bitmap.Data[srcPos++];

                // Get average intensity
                int intensity             = (r + g + b) / 3;
                DFBitmap.DFColor dstColor = DFBitmap.DFColor.FromRGBA((byte)intensity, (byte)intensity, (byte)intensity, a);

                // Write destination pixel
                newBitmap.Data[dstPos++] = dstColor.r;
                newBitmap.Data[dstPos++] = dstColor.g;
                newBitmap.Data[dstPos++] = dstColor.b;
                newBitmap.Data[dstPos++] = dstColor.a;
            }

            return(newBitmap);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a grayscale version of bitmap based on colour theory.
        /// </summary>
        /// <param name="bitmap">Source DFBitmap.</param>
        /// <returns>Grayscale DFBitmap image.</returns>
        public static DFBitmap MakeGrayscale(DFBitmap bitmap)
        {
            // Must be a colour format
            if (bitmap.Format == DFBitmap.Formats.Indexed)
            {
                return(null);
            }

            DFBitmap newBitmap = new DFBitmap();

            newBitmap.Format = bitmap.Format;
            newBitmap.Initialise(bitmap.Width, bitmap.Height);

            // Make each pixel grayscale
            int srcPos = 0, dstPos = 0;

            for (int i = 0; i < bitmap.Width * bitmap.Height; i++)
            {
                // Get source color
                byte r = bitmap.Data[srcPos++];
                byte g = bitmap.Data[srcPos++];
                byte b = bitmap.Data[srcPos++];
                byte a = bitmap.Data[srcPos++];

                // Create grayscale color
                int grayscale             = (int)(r * 0.3f + g * 0.59f + b * 0.11f);
                DFBitmap.DFColor dstColor = DFBitmap.DFColor.FromRGBA((byte)grayscale, (byte)grayscale, (byte)grayscale, a);

                // Write destination pixel
                newBitmap.Data[dstPos++] = dstColor.r;
                newBitmap.Data[dstPos++] = dstColor.g;
                newBitmap.Data[dstPos++] = dstColor.b;
                newBitmap.Data[dstPos++] = dstColor.a;
            }

            return(newBitmap);
        }
Ejemplo n.º 5
0
            /// <summary>
            /// Applies the filter to the input image
            /// </summary>
            /// <param name="Input">input image</param>
            /// <returns>Returns a separate image with the filter applied</returns>
            public DFBitmap ApplyFilter(DFBitmap Input)
            {
                // Must be a colour format
                if (Input.Format == DFBitmap.Formats.Indexed)
                {
                    return(null);
                }

                DFBitmap NewBitmap = DFBitmap.CloneDFBitmap(Input);

                for (int x = 0; x < Input.Width; ++x)
                {
                    for (int y = 0; y < Input.Height; ++y)
                    {
                        int RValue   = 0;
                        int GValue   = 0;
                        int BValue   = 0;
                        int AValue   = 0;
                        int Weight   = 0;
                        int XCurrent = -Width / 2;
                        for (int x2 = 0; x2 < Width; ++x2)
                        {
                            if (XCurrent + x < Input.Width && XCurrent + x >= 0)
                            {
                                int YCurrent = -Height / 2;
                                for (int y2 = 0; y2 < Height; ++y2)
                                {
                                    if (YCurrent + y < Input.Height && YCurrent + y >= 0)
                                    {
                                        DFBitmap.DFColor Pixel = DFBitmap.GetPixel(Input, XCurrent + x, YCurrent + y);
                                        RValue += MyFilter[x2, y2] * Pixel.r;
                                        GValue += MyFilter[x2, y2] * Pixel.g;
                                        BValue += MyFilter[x2, y2] * Pixel.b;
                                        AValue  = Pixel.a;
                                        Weight += MyFilter[x2, y2];
                                    }
                                    ++YCurrent;
                                }
                            }
                            ++XCurrent;
                        }

                        DFBitmap.DFColor MeanPixel = DFBitmap.GetPixel(Input, x, y);
                        if (Weight == 0)
                        {
                            Weight = 1;
                        }
                        if (Weight > 0)
                        {
                            if (Absolute)
                            {
                                RValue = System.Math.Abs(RValue);
                                GValue = System.Math.Abs(GValue);
                                BValue = System.Math.Abs(BValue);
                            }

                            RValue    = (RValue / Weight) + Offset;
                            RValue    = Clamp(RValue, 0, 255);
                            GValue    = (GValue / Weight) + Offset;
                            GValue    = Clamp(GValue, 0, 255);
                            BValue    = (BValue / Weight) + Offset;
                            BValue    = Clamp(BValue, 0, 255);
                            MeanPixel = DFBitmap.DFColor.FromRGBA((byte)RValue, (byte)GValue, (byte)BValue, (byte)AValue);
                        }

                        DFBitmap.SetPixel(NewBitmap, x, y, MeanPixel);
                    }
                }

                return(NewBitmap);
            }