Ejemplo n.º 1
0
        public CanvasPixel Reduce3(CanvasPixel canvas, int threshold)
        {
            Pixel[]     data   = canvas.Data;
            CanvasPixel result = new CanvasPixel(canvas.Width, canvas.Height);

            for (int y = 0; y < canvas.Height; y++)
            {
                for (int x = 0; x < canvas.Width; x++)
                {
                    int destIndex = y * canvas.Width + x;

                    List <Pixel> allValidPixels = GetAllValidPixels(canvas, x, y);
                    //Pixel avgPixel = GetAVGPixel(allValidPixels);
                    //List<Pixel> allNearest = GetNeightboursValidPixels(canvas,x,y);
                    //Pixel avgPixel = GetAVGPixel(allNearest);
                    Pixel avgPixel = GetMedianPixel2(allValidPixels, data[destIndex]);
                    //Pixel avgPixel =result.Data[destIndex];

                    Pixel nearestPixel = GetNearestPixel(allValidPixels, avgPixel, data[destIndex], threshold);

                    result.Data[destIndex] = nearestPixel;
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public void ReduceByMask(CanvasPixel canvas, int mask)
        {
            Pixel[] data = canvas.Data;

            int maskOne = 0;

            for (int i = 0; i < mask; i++)
            {
                maskOne = (maskOne << 1) | 1;
            }

            for (int i = 0; i < data.Length; i++)
            {
                int b = data[i].CB;

                if (((b >> 7) & 1) == 0)
                {
                    b = b & (~maskOne);
                }
                else
                {
                    b = b | (maskOne);
                }

                data[i].CB = (byte)b;

                int r = data[i].CR;
                if (((r >> 7) & 1) == 0)
                {
                    r = r & (~maskOne);
                }
                else
                {
                    r = r | (maskOne);
                }

                data[i].CR = (byte)r;

                int g = data[i].CG;
                if (((g >> 7) & 1) == 0)
                {
                    g = g & (~maskOne);
                }
                else
                {
                    g = g | (maskOne);
                }

                data[i].CG = (byte)g;
            }
        }
Ejemplo n.º 3
0
        private void Reduce31_OnePixel(CanvasPixel canvas, CanvasPixel result, int x, int y, int threshold)
        {
            int destIndex = y * canvas.Width + x;


            List <Pixel> allValidPixels = GetAllValidPixels(result, x, y);
            //Pixel avgPixel = GetAVGPixel(allValidPixels);
            //List<Pixel> allNearest = GetNeightboursValidPixels(canvas,x,y);
            //Pixel avgPixel = GetAVGPixel(allNearest);
            Pixel avgPixel = GetMedianPixel2(allValidPixels, result.Data[destIndex]);
            //Pixel avgPixel =result.Data[destIndex];

            Pixel nearestPixel = GetNearestPixel(allValidPixels, avgPixel, result.Data[destIndex], threshold);

            result.Data[destIndex] = nearestPixel;
        }
Ejemplo n.º 4
0
        public static CanvasPixel CreateBitmpaFromCanvas(CanvasARGB canvas)
        {
            CanvasPixel result = new CanvasPixel(canvas.WidthPixel, canvas.HeightPixel);

            Pixel[] dest   = result._data;
            byte[]  source = canvas.Data;

            int di = 0;

            for (int i = 0; i < source.Length; i += 4)
            {
                Pixel tmpP = Pixel.Create(source[i + 3], source[i + 2], source[i + 1], source[i]);
                dest[di] = tmpP;
                di++;
            }

            return(result);
        }
Ejemplo n.º 5
0
        public CanvasPixel Reduce31(CanvasPixel canvas, int threshold)
        {
            Pixel[] data = canvas.Data;

            CanvasPixel result = new CanvasPixel(canvas.Width, canvas.Height);

            data.CopyTo(result.Data, 0);

            for (int y = 0; y < canvas.Height; y += 2)
            {
                for (int x = 0; x < canvas.Width; x += 2)
                {
                    Reduce31_OnePixel(canvas, result, x, y, threshold);
                }
            }

            for (int y = 1; y < canvas.Height; y += 2)
            {
                for (int x = 1; x < canvas.Width; x += 2)
                {
                    Reduce31_OnePixel(canvas, result, x, y, threshold);
                }
            }

            for (int y = 0; y < canvas.Height; y += 2)
            {
                for (int x = 1; x < canvas.Width; x += 2)
                {
                    Reduce31_OnePixel(canvas, result, x, y, threshold);
                }
            }

            for (int y = 1; y < canvas.Height; y += 2)
            {
                for (int x = 0; x < canvas.Width; x += 2)
                {
                    Reduce31_OnePixel(canvas, result, x, y, threshold);
                }
            }

            return(result);
        }
Ejemplo n.º 6
0
        public static void PixelFilter(CanvasPixel cp, int tolerance)
        {
            Pixel[] data = cp.Data;

            for (int i = 1; i < data.Length; i++)
            {
                Pixel pixel  = data[i];
                Pixel pixel2 = data[i - 1];

                int color  = pixel.Get_ColorClearAlpha_Int();
                int color2 = pixel2.Get_ColorClearAlpha_Int();

                if (Helpers.BasicHelpers.FastAbs(color - color2) <= tolerance)
                {
                    data[i] = data[i - 1];
                }
            }



            for (int i = 1; i < cp.Width; i++)
            {
                for (int y = 1; y < cp.Height; y++)
                {
                    int indexLast = (y - 1) * cp.Width + i;

                    int index = y * cp.Width + i;

                    Pixel pixel  = data[index];
                    Pixel pixel2 = data[indexLast];

                    int color  = pixel.Get_ColorClearAlpha_Int();
                    int color2 = pixel2.Get_ColorClearAlpha_Int();

                    if (Helpers.BasicHelpers.FastAbs(color - color2) <= tolerance)
                    {
                        data[index] = data[indexLast];
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public void Reduce(CanvasPixel canvas, int threshold)
        {
            Pixel [] data = canvas.Data;

            double coef = 255.0 / threshold;

            for (int i = 0; i < data.Length; i++)
            {
                int b = data[i].CB;
                b          = (int)(Math.Round((b / 255.0) * threshold) * coef);
                data[i].CB = (byte)b;

                int r = data[i].CR;
                r          = (int)(Math.Round((r / 255.0) * threshold) * coef);
                data[i].CR = (byte)r;

                int g = data[i].CG;
                g          = (int)(Math.Round((g / 255.0) * threshold) * coef);
                data[i].CG = (byte)g;
            }
        }
Ejemplo n.º 8
0
        private List <Pixel> GetNeightboursValidPixels(CanvasPixel canvas, int x, int y)
        {
            List <Pixel> result = new List <Pixel>();

            int baseIndex = y * canvas.Width;
            int cy        = -1;
            int cx        = 0;

            if (((x + cx) >= 0 && (x + cx) < canvas.Width) && ((y + cy) >= 0 && (y + cy) < canvas.Height))
            {
                int index = baseIndex + (cy * canvas.Width) + x + cx;
                result.Add(canvas.Data[index]);
            }

            cy = 0;
            cx = -1;
            if (((x + cx) >= 0 && (x + cx) < canvas.Width) && ((y + cy) >= 0 && (y + cy) < canvas.Height))
            {
                int index = baseIndex + (cy * canvas.Width) + x + cx;
                result.Add(canvas.Data[index]);
            }

            cy = 0;
            cx = 1;
            if (((x + cx) >= 0 && (x + cx) < canvas.Width) && ((y + cy) >= 0 && (y + cy) < canvas.Height))
            {
                int index = baseIndex + (cy * canvas.Width) + x + cx;
                result.Add(canvas.Data[index]);
            }
            cy = 1;
            cx = 0;
            if (((x + cx) >= 0 && (x + cx) < canvas.Width) && ((y + cy) >= 0 && (y + cy) < canvas.Height))
            {
                int index = baseIndex + (cy * canvas.Width) + x + cx;
                result.Add(canvas.Data[index]);
            }

            return(result);
        }
Ejemplo n.º 9
0
        public void Reduce2(CanvasPixel canvas, int threshold)
        {
            //0.2126 * R + 0.7152 * G + 0.0722

            Pixel[] data = canvas.Data;

            double coefb = 255.0 / threshold * 0.0722;
            double coefr = 255.0 / (threshold * (0.2126));
            double coefg = 255.0 / (threshold * (0.7152));


            for (int i = 0; i < data.Length; i++)
            {
                int b = data[i].CB;
                b = (int)(Math.Floor((b / 255.0) * threshold * 0.0722) * coefb);
                if (b >= 256)
                {
                    throw new Exception("");
                }
                data[i].CB = (byte)b;

                int r  = data[i].CR;
                int tr = (int)(Math.Floor((r / 255.0) * (threshold * (0.2126))) * coefr);
                if (tr >= 256)
                {
                    throw new Exception("");
                }
                data[i].CR = (byte)tr;

                int g = data[i].CG;
                g = (int)(Math.Floor((g / 255.0) * (threshold * (0.7152))) * coefg);
                if (g >= 256)
                {
                    throw new Exception("");
                }
                data[i].CG = (byte)g;
            }
        }
Ejemplo n.º 10
0
        public static CanvasARGB CreateBitmpaFromCanvas(CanvasPixel canvasPixel)
        {
            CanvasARGB result = new CanvasARGB(canvasPixel.Width, canvasPixel.Height);

            byte[]  dest   = result.Data;
            Pixel[] source = canvasPixel.Data;

            int di = 0;

            for (int i = 0; i < source.Length; i++)
            {
                Pixel p = source[i];

                dest[di]     = p.B0;
                dest[di + 1] = p.B1;
                dest[di + 2] = p.B2;
                dest[di + 3] = p.B3;

                di += 4;
            }

            return(result);
        }
Ejemplo n.º 11
0
        private List <Pixel> GetAllValidPixels(CanvasPixel canvas, int x, int y)
        {
            const int    kernelSize = 1;
            List <Pixel> result     = new List <Pixel>();

            int baseIndex = y * canvas.Width;

            for (int cy = -(kernelSize); cy < (kernelSize + 1); cy++)
            {
                for (int cx = -(kernelSize); cx < (kernelSize + 1); cx++)
                {
                    //if (cx == 0 && cy == 0) continue;

                    if (((x + cx) >= 0 && (x + cx) < canvas.Width) &&
                        ((y + cy) >= 0 && (y + cy) < canvas.Height))
                    {
                        int index = baseIndex + (cy * canvas.Width) + x + cx;
                        result.Add(canvas.Data[index]);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 12
0
        public static void BlackWhite(CanvasPixel cp)
        {
            Pixel[] data = cp.Data;

            for (int i = 0; i < data.Length; i++)
            {
                Pixel pixel = data[i];

                if ((pixel.UInt & 0xffffff) > 0x7fffff)
                {
                    pixel.CB = 255;
                    pixel.CR = 255;
                    pixel.CG = 255;
                }
                else
                {
                    pixel.CB = 0;
                    pixel.CR = 0;
                    pixel.CG = 0;
                }

                data[i] = pixel;
            }
        }
Ejemplo n.º 13
0
        public static void PixelFilter2(CanvasPixel cp, int tolerance)
        {
            Pixel[] data = cp.Data;

            Pixel[] result = new Pixel[data.Length];

            Array.Copy(data, 0, result, 0, data.Length);


            Pixel white = new Pixel();
            Pixel black = new Pixel();
            Pixel red   = new Pixel();

            white.CR = 255;
            white.CG = 255;
            white.CB = 255;
            red.CR   = 255;
            red.CA   = 255;
            white.CA = 255;
            black.CA = 255;


            for (int y = 1; y < cp.Height - 1; y++)
            {
                for (int i = 1; i < cp.Width - 1; i++)
                {
                    int rowindex1 = (y - 1) * cp.Width + i;
                    int rowindex2 = (y) * cp.Width + i;
                    int rowindex3 = (y + 1) * cp.Width + i;


                    int middleColor = data[rowindex2].Get_ColorClearAlpha_Int();



                    bool b2 = Helpers.BasicHelpers.FastAbs(middleColor - data[rowindex1].Get_ColorClearAlpha_Int()) <= tolerance;
                    bool b4 = Helpers.BasicHelpers.FastAbs(middleColor - data[rowindex2 - 1].Get_ColorClearAlpha_Int()) <= tolerance;
                    bool b6 = Helpers.BasicHelpers.FastAbs(middleColor - data[rowindex2 + 1].Get_ColorClearAlpha_Int()) <= tolerance;
                    bool b8 = Helpers.BasicHelpers.FastAbs(middleColor - data[rowindex3].Get_ColorClearAlpha_Int()) <= tolerance;

                    if (b2 || b4 || b6 || b8)
                    {
                        bool b1 = Helpers.BasicHelpers.FastAbs(middleColor - data[rowindex1 - 1].Get_ColorClearAlpha_Int()) <= tolerance;
                        bool b3 = Helpers.BasicHelpers.FastAbs(middleColor - data[rowindex1 + 1].Get_ColorClearAlpha_Int()) <= tolerance;
                        bool b7 = Helpers.BasicHelpers.FastAbs(middleColor - data[rowindex3 - 1].Get_ColorClearAlpha_Int()) <= tolerance;
                        bool b9 = Helpers.BasicHelpers.FastAbs(middleColor - data[rowindex3 + 1].Get_ColorClearAlpha_Int()) <= tolerance;

                        if (b2 && b4 && b6 && b8 && b1 && b3 && b7 && b9)
                        {
                            result[rowindex2] = white;
                        }
                        else
                        {
                            result[rowindex2] = red;
                        }
                    }
                    else
                    {
                        result[rowindex2] = black;
                    }
                }
            }

            Array.Copy(result, 0, data, 0, data.Length);
        }