Example #1
0
        public Image ProcessImage(Image inputImage)
        {
            Bitmap origin = new Bitmap(inputImage);
            Bitmap blurred = new Bitmap(inputImage.Width, inputImage.Height);

            using (RawBitmap src = new RawBitmap(origin))
            {
                using (RawBitmap dest = new RawBitmap(blurred))
                {
                    int pixelCount = src.Width * src.Height;
                    int[] b = new int[pixelCount];
                    int[] g = new int[pixelCount];
                    int[] r = new int[pixelCount];
                    int[] a = new int[pixelCount];

                    int[] b2 = new int[pixelCount];
                    int[] g2 = new int[pixelCount];
                    int[] r2 = new int[pixelCount];
                    int[] a2 = new int[pixelCount];

                    int offset = src.GetOffset();
                    int index = 0;

                    unsafe
                    {
                        byte* ptr = src.Begin;
                        for (int i = 0; i < src.Height; i++)
                        {
                            for (int j = 0; j < src.Width; j++)
                            {
                                b[index] = *ptr;
                                ptr++;
                                g[index] = *ptr;
                                ptr++;
                                r[index] = *ptr;
                                ptr++;
                                a[index] = *ptr;
                                ptr++;

                                ++index;
                            }
                            ptr += offset;
                        }

                        int bsum;
                        int gsum;
                        int rsum;
                        int asum;
                        int sum;
                        int read;
                        int start = 0;
                        index = 0;
                        if (_blurType != BlurType.VerticalOnly)
                        {
                            for (int i = 0; i < src.Height; i++)
                            {
                                for (int j = 0; j < src.Width; j++)
                                {
                                    bsum = gsum = rsum = asum = sum = 0;
                                    read = index - _radius;

                                    for (int z = 0; z < _kernel.Length; z++)
                                    {
                                        if (read >= start && read < start + src.Width)
                                        {
                                            bsum += _multable[z, b[read]];
                                            gsum += _multable[z, g[read]];
                                            rsum += _multable[z, r[read]];
                                            asum += _multable[z, a[read]];
                                            sum += _kernel[z];
                                        }
                                        ++read;
                                    }

                                    b2[index] = (bsum / sum);
                                    g2[index] = (gsum / sum);
                                    r2[index] = (rsum / sum);
                                    a2[index] = (asum / sum);

                                    if (_blurType == BlurType.HorizontalOnly)
                                    {
                                        byte* pcell = dest[j, i];
                                        pcell[0] = (byte)(bsum / sum);
                                        pcell[1] = (byte)(gsum / sum);
                                        pcell[2] = (byte)(rsum / sum);
                                        pcell[3] = (byte)(asum / sum);
                                    }

                                    ++index;
                                }
                                start += src.Width;
                            }
                        }
                        if (_blurType == BlurType.HorizontalOnly)
                        {
                            return blurred;
                        }

                        int tempy;
                        for (int i = 0; i < src.Height; i++)
                        {
                            int y = i - _radius;
                            start = y * src.Width;
                            for (int j = 0; j < src.Width; j++)
                            {
                                bsum = gsum = rsum = asum = sum = 0;
                                read = start + j;
                                tempy = y;
                                for (int z = 0; z < _kernel.Length; z++)
                                {
                                    if (tempy >= 0 && tempy < src.Height)
                                    {
                                        if (_blurType == BlurType.VerticalOnly)
                                        {
                                            bsum += _multable[z, b[read]];
                                            gsum += _multable[z, g[read]];
                                            rsum += _multable[z, r[read]];
                                            asum += _multable[z, a[read]];
                                        }
                                        else
                                        {
                                            bsum += _multable[z, b2[read]];
                                            gsum += _multable[z, g2[read]];
                                            rsum += _multable[z, r2[read]];
                                            asum += _multable[z, a2[read]];
                                        }
                                        sum += _kernel[z];
                                    }
                                    read += src.Width;
                                    ++tempy;
                                }

                                byte* pcell = dest[j, i];
                                pcell[0] = (byte)(bsum / sum);
                                pcell[1] = (byte)(gsum / sum);
                                pcell[2] = (byte)(rsum / sum);
                                pcell[3] = (byte)(asum / sum);
                            }
                        }
                    }
                }
            }

            return blurred;
        }
Example #2
0
        public Image ProcessImage(Image inputImage)
        {
            Bitmap origin  = new Bitmap(inputImage);
            Bitmap blurred = new Bitmap(inputImage.Width, inputImage.Height);

            using (RawBitmap src = new RawBitmap(origin))
            {
                using (RawBitmap dest = new RawBitmap(blurred))
                {
                    int   pixelCount = src.Width * src.Height;
                    int[] b          = new int[pixelCount];
                    int[] g          = new int[pixelCount];
                    int[] r          = new int[pixelCount];
                    int[] a          = new int[pixelCount];

                    int[] b2 = new int[pixelCount];
                    int[] g2 = new int[pixelCount];
                    int[] r2 = new int[pixelCount];
                    int[] a2 = new int[pixelCount];

                    int offset = src.GetOffset();
                    int index  = 0;

                    unsafe
                    {
                        byte *ptr = src.Begin;
                        for (int i = 0; i < src.Height; i++)
                        {
                            for (int j = 0; j < src.Width; j++)
                            {
                                b[index] = *ptr;
                                ptr++;
                                g[index] = *ptr;
                                ptr++;
                                r[index] = *ptr;
                                ptr++;
                                a[index] = *ptr;
                                ptr++;

                                ++index;
                            }
                            ptr += offset;
                        }

                        int bsum;
                        int gsum;
                        int rsum;
                        int asum;
                        int sum;
                        int read;
                        int start = 0;
                        index = 0;
                        if (_blurType != BlurType.VerticalOnly)
                        {
                            for (int i = 0; i < src.Height; i++)
                            {
                                for (int j = 0; j < src.Width; j++)
                                {
                                    bsum = gsum = rsum = asum = sum = 0;
                                    read = index - _radius;

                                    for (int z = 0; z < _kernel.Length; z++)
                                    {
                                        if (read >= start && read < start + src.Width)
                                        {
                                            bsum += _multable[z, b[read]];
                                            gsum += _multable[z, g[read]];
                                            rsum += _multable[z, r[read]];
                                            asum += _multable[z, a[read]];
                                            sum  += _kernel[z];
                                        }
                                        ++read;
                                    }

                                    b2[index] = (bsum / sum);
                                    g2[index] = (gsum / sum);
                                    r2[index] = (rsum / sum);
                                    a2[index] = (asum / sum);

                                    if (_blurType == BlurType.HorizontalOnly)
                                    {
                                        byte *pcell = dest[j, i];
                                        pcell[0] = (byte)(bsum / sum);
                                        pcell[1] = (byte)(gsum / sum);
                                        pcell[2] = (byte)(rsum / sum);
                                        pcell[3] = (byte)(asum / sum);
                                    }

                                    ++index;
                                }
                                start += src.Width;
                            }
                        }
                        if (_blurType == BlurType.HorizontalOnly)
                        {
                            return(blurred);
                        }

                        int tempy;
                        for (int i = 0; i < src.Height; i++)
                        {
                            int y = i - _radius;
                            start = y * src.Width;
                            for (int j = 0; j < src.Width; j++)
                            {
                                bsum  = gsum = rsum = asum = sum = 0;
                                read  = start + j;
                                tempy = y;
                                for (int z = 0; z < _kernel.Length; z++)
                                {
                                    if (tempy >= 0 && tempy < src.Height)
                                    {
                                        if (_blurType == BlurType.VerticalOnly)
                                        {
                                            bsum += _multable[z, b[read]];
                                            gsum += _multable[z, g[read]];
                                            rsum += _multable[z, r[read]];
                                            asum += _multable[z, a[read]];
                                        }
                                        else
                                        {
                                            bsum += _multable[z, b2[read]];
                                            gsum += _multable[z, g2[read]];
                                            rsum += _multable[z, r2[read]];
                                            asum += _multable[z, a2[read]];
                                        }
                                        sum += _kernel[z];
                                    }
                                    read += src.Width;
                                    ++tempy;
                                }

                                byte *pcell = dest[j, i];
                                pcell[0] = (byte)(bsum / sum);
                                pcell[1] = (byte)(gsum / sum);
                                pcell[2] = (byte)(rsum / sum);
                                pcell[3] = (byte)(asum / sum);
                            }
                        }
                    }
                }
            }

            return(blurred);
        }