Exemplo n.º 1
0
        public static BitmapHolder ToFlipped(BitmapHolder bmp, FlipType flipMode)
        {
            int          width        = bmp.Width;
            int          height       = bmp.Height;
            int          num          = 0;
            BitmapHolder bitmapHolder = new BitmapHolder(new byte[bmp.PixelData.Length], width, height);

            if (flipMode == FlipType.Vertical)
            {
                byte[] pixelData = bitmapHolder.PixelData;
                for (int num2 = height - 1; num2 >= 0; num2--)
                {
                    for (int i = 0; i < width; i++)
                    {
                        int pos = num2 * width + i;
                        bitmapHolder.SetPixel(num, bmp.GetPixel(pos));
                        num++;
                    }
                }
            }
            else
            {
                byte[] pixelData2 = bitmapHolder.PixelData;
                for (int j = 0; j < height; j++)
                {
                    for (int num3 = width - 1; num3 >= 0; num3--)
                    {
                        int pos2 = j * width + num3;
                        bitmapHolder.SetPixel(num, bmp.GetPixel(pos2));
                        num++;
                    }
                }
            }
            return(bitmapHolder);
        }
Exemplo n.º 2
0
        public static void ToReplacedColor(BitmapHolder bmp, int r, int g, int b, int a)
        {
            int   width      = bmp.Width;
            int   height     = bmp.Height;
            int   pixelCount = bmp.PixelCount;
            float num        = (float)a / 255f;
            float num2       = 1f - num;
            int   val        = (int)((float)r - (float)r * num2);
            int   val2       = (int)((float)g - (float)g * num2);
            int   val3       = (int)((float)b - (float)b * num2);
            int   val4       = (int)((float)r + (float)r * num2);
            int   val5       = (int)((float)g + (float)g * num2);
            int   val6       = (int)((float)b + (float)b * num2);

            for (int i = 0; i < pixelCount; i++)
            {
                ColorHolder pixel = bmp.GetPixel(i);
                int         a2    = pixel.A;
                byte        r2    = pixel.R;
                byte        g2    = pixel.G;
                byte        b2    = pixel.B;
                int         val7  = (int)((float)(int)r2 + (float)(255 - r2) * (num * (float)r / 255f));
                int         val8  = (int)((float)(int)g2 + (float)(255 - g2) * (num * (float)g / 255f));
                int         val9  = (int)((float)(int)b2 + (float)(255 - b2) * (num * (float)b / 255f));
                val7 = Math.Min(Math.Max(val, val7), val4);
                val8 = Math.Min(Math.Max(val2, val8), val5);
                val9 = Math.Min(Math.Max(val3, val9), val6);
                bmp.SetPixel(i, new ColorHolder(pixel.A, val7, val8, val9));
            }
        }
Exemplo n.º 3
0
        public static void ToReplacedColor(BitmapHolder bmp, int r, int g, int b, int a)
        {
            var   nWidth     = bmp.Width;
            var   nHeight    = bmp.Height;
            var   len        = bmp.PixelCount;
            float percentage = (float)a / 255;
            float left       = 1 - percentage;
            int   rMin       = (int)(r - (r * left));
            int   gMin       = (int)(g - (g * left));
            int   bMin       = (int)(b - (b * left));
            int   rMax       = (int)(r + (r * left));
            int   gMax       = (int)(g + (g * left));
            int   bMax       = (int)(b + (b * left));

            for (var i = 0; i < len; i++)
            {
                var color        = bmp.GetPixel(i);
                int currentAlpha = color.A;
                var aNew         = currentAlpha == 0 ? 0 : (int)(currentAlpha * (a / currentAlpha));
                var curR         = color.R;
                var curG         = color.G;
                var curB         = color.B;
                int rNew         = (int)(curR + (255 - curR) * (percentage * r / 255));
                int gNew         = (int)(curG + (255 - curG) * (percentage * g / 255));
                int bNew         = (int)(curB + (255 - curB) * (percentage * b / 255));
                rNew = Math.Min(Math.Max(rMin, rNew), rMax);
                gNew = Math.Min(Math.Max(gMin, gNew), gMax);
                bNew = Math.Min(Math.Max(bMin, bNew), bMax);

                bmp.SetPixel(i, new ColorHolder(color.A, rNew, gNew, bNew));
            }
        }
Exemplo n.º 4
0
        public static void ToSepia(BitmapHolder bmp)
        {
            int width      = bmp.Width;
            int height     = bmp.Height;
            int pixelCount = bmp.PixelCount;

            for (int i = 0; i < pixelCount; i++)
            {
                ColorHolder pixel = bmp.GetPixel(i);
                int         a     = pixel.A;
                int         r     = pixel.R;
                int         g     = pixel.G;
                int         b     = pixel.B;
                int         num   = (int)Math.Min(0.393 * (double)r + 0.769 * (double)g + 0.189 * (double)b, 255.0);
                int         num2  = (int)Math.Min(0.349 * (double)r + 0.686 * (double)g + 0.168 * (double)b, 255.0);
                int         num3  = (int)Math.Min(0.272 * (double)r + 0.534 * (double)g + 0.131 * (double)b, 255.0);
                if (num > 255)
                {
                    num = 255;
                }
                if (num2 > 255)
                {
                    num2 = 255;
                }
                if (num3 > 255)
                {
                    num3 = 255;
                }
                bmp.SetPixel(i, new ColorHolder(a, num, num2, num3));
            }
        }
        protected override BitmapHolder Transform(BitmapHolder bitmapSource, string path, ImageSource source, bool isPlaceholder, string key)
        {
            int         pixelCount = bitmapSource.PixelCount;
            ColorHolder backColor  = HexColor.ToColorFromHex();

            for (int i = 0; i < pixelCount; i++)
            {
                ColorHolder pixel = bitmapSource.GetPixel(i);
                bitmapSource.SetPixel(i, BlendColor(pixel, backColor));
            }
            return(bitmapSource);
        }
Exemplo n.º 6
0
        protected override BitmapHolder Transform(BitmapHolder bitmapSource, string path, ImageSource source, bool isPlaceholder, string key)
        {
            var len       = bitmapSource.PixelCount;
            var backColor = HexColor.ToColorFromHex();

            for (var i = 0; i < len; i++)
            {
                var color = bitmapSource.GetPixel(i);
                bitmapSource.SetPixel(i, BlendColor(color, backColor));
            }

            return(bitmapSource);
        }
        public static BitmapHolder ToFlipped(BitmapHolder bmp, FlipType flipMode)
        {
            // Use refs for faster access (really important!) speeds up a lot!
            var          w      = bmp.Width;
            var          h      = bmp.Height;
            var          i      = 0;
            BitmapHolder result = new BitmapHolder(new byte[bmp.PixelData.Length], w, h);

            if (flipMode == FlipType.Vertical)
            {
                var rp = result.PixelData;
                for (var y = h - 1; y >= 0; y--)
                {
                    for (var x = 0; x < w; x++)
                    {
                        var srcInd = y * w + x;
                        result.SetPixel(i, bmp.GetPixel(srcInd));
                        i++;
                    }
                }
            }
            else
            {
                var rp = result.PixelData;
                for (var y = 0; y < h; y++)
                {
                    for (var x = w - 1; x >= 0; x--)
                    {
                        var srcInd = y * w + x;
                        result.SetPixel(i, bmp.GetPixel(srcInd));
                        i++;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 8
0
        public static void ToColorSpace(BitmapHolder bmp, float[][] rgbawMatrix)
        {
            var r0 = rgbawMatrix[0][0];
            var r1 = rgbawMatrix[0][1];
            var r2 = rgbawMatrix[0][2];
            var r3 = rgbawMatrix[0][3];

            var g0 = rgbawMatrix[1][0];
            var g1 = rgbawMatrix[1][1];
            var g2 = rgbawMatrix[1][2];
            var g3 = rgbawMatrix[1][3];

            var b0 = rgbawMatrix[2][0];
            var b1 = rgbawMatrix[2][1];
            var b2 = rgbawMatrix[2][2];
            var b3 = rgbawMatrix[2][3];

            var a0 = rgbawMatrix[3][0];
            var a1 = rgbawMatrix[3][1];
            var a2 = rgbawMatrix[3][2];
            var a3 = rgbawMatrix[3][3];

            var rOffset = rgbawMatrix[4][0];
            var gOffset = rgbawMatrix[4][1];
            var bOffset = rgbawMatrix[4][2];
            var aOffset = rgbawMatrix[4][3];

            var nWidth  = bmp.Width;
            var nHeight = bmp.Height;
            var len     = bmp.PixelCount;

            for (var i = 0; i < len; i++)
            {
                var color = bmp.GetPixel(i);
                var a     = color.A;
                var r     = color.R;
                var g     = color.G;
                var b     = color.B;

                var rNew = (int)(r * r0 + g * g0 + b * b0 + a * a0 + rOffset);
                var gNew = (int)(r * r1 + g * g1 + b * b1 + a * a1 + gOffset);
                var bNew = (int)(r * r2 + g * g2 + b * b2 + a * a2 + bOffset);
                var aNew = (int)(r * r3 + g * g3 + b * b3 + a * a3 + aOffset);

                bmp.SetPixel(i, new ColorHolder(aNew, rNew, gNew, bNew));
            }
        }
        public static void ToGrayscale(BitmapHolder bmp)
        {
            int width      = bmp.Width;
            int height     = bmp.Height;
            int pixelCount = bmp.PixelCount;

            for (int i = 0; i < pixelCount; i++)
            {
                ColorHolder pixel = bmp.GetPixel(i);
                int         a     = pixel.A;
                int         r     = pixel.R;
                int         g     = pixel.G;
                int         b     = pixel.B;
                int         num   = r * 6966 + g * 23436 + b * 2366 >> 15;
                r = (g = (b = num));
                bmp.SetPixel(i, new ColorHolder(a, r, g, b));
            }
        }
Exemplo n.º 10
0
        public static void ToColorSpace(BitmapHolder bmp, float[][] rgbawMatrix)
        {
            float num        = rgbawMatrix[0][0];
            float num2       = rgbawMatrix[0][1];
            float num3       = rgbawMatrix[0][2];
            float num4       = rgbawMatrix[0][3];
            float num5       = rgbawMatrix[1][0];
            float num6       = rgbawMatrix[1][1];
            float num7       = rgbawMatrix[1][2];
            float num8       = rgbawMatrix[1][3];
            float num9       = rgbawMatrix[2][0];
            float num10      = rgbawMatrix[2][1];
            float num11      = rgbawMatrix[2][2];
            float num12      = rgbawMatrix[2][3];
            float num13      = rgbawMatrix[3][0];
            float num14      = rgbawMatrix[3][1];
            float num15      = rgbawMatrix[3][2];
            float num16      = rgbawMatrix[3][3];
            float num17      = rgbawMatrix[4][0];
            float num18      = rgbawMatrix[4][1];
            float num19      = rgbawMatrix[4][2];
            float num20      = rgbawMatrix[4][3];
            int   width      = bmp.Width;
            int   height     = bmp.Height;
            int   pixelCount = bmp.PixelCount;

            for (int i = 0; i < pixelCount; i++)
            {
                ColorHolder pixel = bmp.GetPixel(i);
                byte        a     = pixel.A;
                byte        r     = pixel.R;
                byte        g     = pixel.G;
                byte        b     = pixel.B;
                int         r2    = (int)((float)(int)r * num + (float)(int)g * num5 + (float)(int)b * num9 + (float)(int)a * num13 + num17);
                int         g2    = (int)((float)(int)r * num2 + (float)(int)g * num6 + (float)(int)b * num10 + (float)(int)a * num14 + num18);
                int         b2    = (int)((float)(int)r * num3 + (float)(int)g * num7 + (float)(int)b * num11 + (float)(int)a * num15 + num19);
                int         a2    = (int)((float)(int)r * num4 + (float)(int)g * num8 + (float)(int)b * num12 + (float)(int)a * num16 + num20);
                bmp.SetPixel(i, new ColorHolder(a2, r2, g2, b2));
            }
        }
        public static void ToGrayscale(BitmapHolder bmp)
        {
            var nWidth  = bmp.Width;
            var nHeight = bmp.Height;

            var len = bmp.PixelCount;

            for (var i = 0; i < len; i++)
            {
                var color = bmp.GetPixel(i);
                int a     = color.A;
                int r     = color.R;
                int g     = color.G;
                int b     = color.B;

                // Convert to gray with constant factors 0.2126, 0.7152, 0.0722
                var gray = (r * 6966 + g * 23436 + b * 2366) >> 15;
                r = g = b = gray;

                bmp.SetPixel(i, new ColorHolder(a, r, g, b));
            }
        }
Exemplo n.º 12
0
        public static void ToSepia(BitmapHolder bmp)
        {
            var nWidth  = bmp.Width;
            var nHeight = bmp.Height;
            var len     = bmp.PixelCount;

            for (var i = 0; i < len; i++)
            {
                var color = bmp.GetPixel(i);
                int a     = color.A;
                int r     = color.R;
                int g     = color.G;
                int b     = color.B;

                var rNew = (int)Math.Min((.393 * r) + (.769 * g) + (.189 * (b)), 255.0);
                var gNew = (int)Math.Min((.349 * r) + (.686 * g) + (.168 * (b)), 255.0);
                var bNew = (int)Math.Min((.272 * r) + (.534 * g) + (.131 * (b)), 255.0);

                if (rNew > 255)
                {
                    rNew = 255;
                }

                if (gNew > 255)
                {
                    gNew = 255;
                }

                if (bNew > 255)
                {
                    bNew = 255;
                }

                bmp.SetPixel(i, new ColorHolder(a, rNew, gNew, bNew));
            }
        }
        public static BitmapHolder ToRotated(BitmapHolder source, double degrees, bool ccw, bool resize)
        {
            if (degrees == 0.0 || degrees % 360.0 == 0.0)
            {
                return(source);
            }
            if (ccw)
            {
                degrees = 360.0 - degrees;
            }
            double num    = -Math.PI / 180.0 * degrees;
            int    width  = source.Width;
            int    height = source.Height;
            int    num2;
            int    num3;

            if (!resize || degrees % 180.0 == 0.0)
            {
                num2 = width;
                num3 = height;
            }
            else
            {
                double num4 = degrees / (180.0 / Math.PI);
                num2 = (int)Math.Ceiling(Math.Abs(Math.Sin(num4) * (double)height) + Math.Abs(Math.Cos(num4) * (double)width));
                num3 = (int)Math.Ceiling(Math.Abs(Math.Sin(num4) * (double)width) + Math.Abs(Math.Cos(num4) * (double)height));
            }
            int          num5         = width / 2;
            int          num6         = height / 2;
            int          num7         = num2 / 2;
            int          num8         = num3 / 2;
            BitmapHolder bitmapHolder = new BitmapHolder(new byte[num2 * num3 * 4], num2, num3);
            int          width2       = source.Width;

            for (int i = 0; i < num3; i++)
            {
                for (int j = 0; j < num2; j++)
                {
                    int    num9  = j - num7;
                    int    num10 = num8 - i;
                    double num11 = Math.Sqrt(num9 * num9 + num10 * num10);
                    double num12;
                    if (num9 == 0)
                    {
                        if (num10 == 0)
                        {
                            bitmapHolder.SetPixel(i * num2 + j, source.GetPixel(num6 * width2 + num5));
                            continue;
                        }
                        num12 = ((num10 >= 0) ? (Math.PI / 2.0) : 4.71238898038469);
                    }
                    else
                    {
                        num12 = Math.Atan2(num10, num9);
                    }
                    num12 -= num;
                    double num13 = num11 * Math.Cos(num12);
                    double num14 = num11 * Math.Sin(num12);
                    num13 += (double)num5;
                    num14  = (double)num6 - num14;
                    int num15 = (int)Math.Floor(num13);
                    int num16 = (int)Math.Floor(num14);
                    int num17 = (int)Math.Ceiling(num13);
                    int num18 = (int)Math.Ceiling(num14);
                    if (num15 >= 0 && num17 >= 0 && num15 < width && num17 < width && num16 >= 0 && num18 >= 0 && num16 < height && num18 < height)
                    {
                        double      num19  = num13 - (double)num15;
                        double      num20  = num14 - (double)num16;
                        ColorHolder pixel  = source.GetPixel(num16 * width2 + num15);
                        ColorHolder pixel2 = source.GetPixel(num16 * width2 + num17);
                        ColorHolder pixel3 = source.GetPixel(num18 * width2 + num15);
                        ColorHolder pixel4 = source.GetPixel(num18 * width2 + num17);
                        double      num21  = (1.0 - num19) * (double)(int)pixel.A + num19 * (double)(int)pixel2.A;
                        double      num22  = (1.0 - num19) * (double)(int)pixel.R + num19 * (double)(int)pixel2.R;
                        double      num23  = (1.0 - num19) * (double)(int)pixel.G + num19 * (double)(int)pixel2.G;
                        double      num24  = (1.0 - num19) * (double)(int)pixel.B + num19 * (double)(int)pixel2.B;
                        double      num25  = (1.0 - num19) * (double)(int)pixel3.A + num19 * (double)(int)pixel4.A;
                        double      num26  = (1.0 - num19) * (double)(int)pixel3.R + num19 * (double)(int)pixel4.R;
                        double      num27  = (1.0 - num19) * (double)(int)pixel3.G + num19 * (double)(int)pixel4.G;
                        double      num28  = (1.0 - num19) * (double)(int)pixel3.B + num19 * (double)(int)pixel4.B;
                        int         r      = (int)Math.Round((1.0 - num20) * num22 + num20 * num26);
                        int         g      = (int)Math.Round((1.0 - num20) * num23 + num20 * num27);
                        int         b      = (int)Math.Round((1.0 - num20) * num24 + num20 * num28);
                        int         num29  = (int)Math.Round((1.0 - num20) * num21 + num20 * num25);
                        int         num30  = num29 + 1;
                        bitmapHolder.SetPixel(i * num2 + j, new ColorHolder(num29, r, g, b));
                    }
                }
            }
            return(bitmapHolder);
        }
        public static void ToLegacyBlurred(BitmapHolder source, int radius)
        {
            int width  = source.Width;
            int height = source.Height;
            int num    = width - 1;
            int val    = height - 1;
            int num2   = width * height;
            int num3   = radius + radius + 1;

            int[] array  = new int[num2];
            int[] array2 = new int[num2];
            int[] array3 = new int[num2];
            int[] array4 = new int[Math.Max(width, height)];
            int[] array5 = new int[Math.Max(width, height)];
            int[] array6 = new int[256 * num3];
            for (int i = 0; i < 256 * num3; i++)
            {
                array6[i] = i / num3;
            }
            int num4;
            int num5 = (num4 = 0);

            for (int j = 0; j < height; j++)
            {
                int num7;
                int num6;
                int num8 = (num7 = (num6 = 0));
                for (int i = -radius; i <= radius; i++)
                {
                    ColorHolder pixel = source.GetPixel(num4 + Math.Min(num, Math.Max(i, 0)));
                    num8 += pixel.R;
                    num7 += pixel.G;
                    num6 += pixel.B;
                }
                for (int k = 0; k < width; k++)
                {
                    array[num4]  = array6[num8];
                    array2[num4] = array6[num7];
                    array3[num4] = array6[num6];
                    if (j == 0)
                    {
                        array4[k] = Math.Min(k + radius + 1, num);
                        array5[k] = Math.Max(k - radius, 0);
                    }
                    ColorHolder pixel2 = source.GetPixel(num5 + array4[k]);
                    ColorHolder pixel3 = source.GetPixel(num5 + array5[k]);
                    num8 += pixel2.R - pixel3.R;
                    num7 += pixel2.G - pixel3.G;
                    num6 += pixel2.B - pixel3.B;
                    num4++;
                }
                num5 += width;
            }
            for (int k = 0; k < width; k++)
            {
                int num7;
                int num6;
                int num8 = (num7 = (num6 = 0));
                int num9 = -radius * width;
                for (int i = -radius; i <= radius; i++)
                {
                    num4  = Math.Max(0, num9) + k;
                    num8 += array[num4];
                    num7 += array2[num4];
                    num6 += array3[num4];
                    num9 += width;
                }
                num4 = k;
                for (int j = 0; j < height; j++)
                {
                    ColorHolder color = new ColorHolder(source.GetPixel(num4).A, array6[num8], array6[num7], array6[num6]);
                    source.SetPixel(num4, color);
                    if (k == 0)
                    {
                        array4[j] = Math.Min(j + radius + 1, val) * width;
                        array5[j] = Math.Max(j - radius, 0) * width;
                    }
                    int num10 = k + array4[j];
                    int num11 = k + array5[j];
                    num8 += array[num10] - array[num11];
                    num7 += array2[num10] - array2[num11];
                    num6 += array3[num10] - array3[num11];
                    num4 += width;
                }
            }
        }
        public static BitmapHolder ToRotated(BitmapHolder source, double degrees, bool ccw, bool resize)
        {
            if (degrees == 0 || degrees % 360 == 0)
            {
                return(source);
            }

            if (ccw)
            {
                degrees = 360d - degrees;
            }

            // rotating clockwise, so it's negative relative to Cartesian quadrants
            double cnAngle = -1.0 * (Math.PI / 180) * degrees;

            // general iterators
            int i, j;
            // calculated indices in Cartesian coordinates
            int    x, y;
            double fDistance, fPolarAngle;
            // for use in neighboring indices in Cartesian coordinates
            int iFloorX, iCeilingX, iFloorY, iCeilingY;
            // calculated indices in Cartesian coordinates with trailing decimals
            double fTrueX, fTrueY;
            // for interpolation
            double fDeltaX, fDeltaY;

            // interpolated "top" pixels
            double fTopRed, fTopGreen, fTopBlue, fTopAlpha;

            // interpolated "bottom" pixels
            double fBottomRed, fBottomGreen, fBottomBlue, fBottomAlpha;

            // final interpolated color components
            int iRed, iGreen, iBlue, iAlpha;

            int iCentreX, iCentreY;
            int iDestCentreX, iDestCentreY;
            int iWidth, iHeight, newWidth, newHeight;

            iWidth  = source.Width;
            iHeight = source.Height;

            if (!resize || (degrees % 180 == 0))
            {
                newWidth  = iWidth;
                newHeight = iHeight;
            }
            else
            {
                var rad = degrees / (180 / Math.PI);
                newWidth  = (int)Math.Ceiling(Math.Abs(Math.Sin(rad) * iHeight) + Math.Abs(Math.Cos(rad) * iWidth));
                newHeight = (int)Math.Ceiling(Math.Abs(Math.Sin(rad) * iWidth) + Math.Abs(Math.Cos(rad) * iHeight));
            }


            iCentreX = iWidth / 2;
            iCentreY = iHeight / 2;

            iDestCentreX = newWidth / 2;
            iDestCentreY = newHeight / 2;

            var newSource = new BitmapHolder(new byte[newWidth * newHeight * 4], newWidth, newHeight);
            var oldw      = source.Width;

            // assigning pixels of destination image from source image
            // with bilinear interpolation
            for (i = 0; i < newHeight; ++i)
            {
                for (j = 0; j < newWidth; ++j)
                {
                    // convert raster to Cartesian
                    x = j - iDestCentreX;
                    y = iDestCentreY - i;

                    // convert Cartesian to polar
                    fDistance = Math.Sqrt(x * x + y * y);
                    if (x == 0)
                    {
                        if (y == 0)
                        {
                            // center of image, no rotation needed
                            newSource.SetPixel(i * newWidth + j, source.GetPixel(iCentreY * oldw + iCentreX));
                            continue;
                        }
                        if (y < 0)
                        {
                            fPolarAngle = 1.5 * Math.PI;
                        }
                        else
                        {
                            fPolarAngle = 0.5 * Math.PI;
                        }
                    }
                    else
                    {
                        fPolarAngle = Math.Atan2(y, x);
                    }

                    // the crucial rotation part
                    // "reverse" rotate, so minus instead of plus
                    fPolarAngle -= cnAngle;

                    // convert polar to Cartesian
                    fTrueX = fDistance * Math.Cos(fPolarAngle);
                    fTrueY = fDistance * Math.Sin(fPolarAngle);

                    // convert Cartesian to raster
                    fTrueX = fTrueX + iCentreX;
                    fTrueY = iCentreY - fTrueY;

                    iFloorX   = (int)(Math.Floor(fTrueX));
                    iFloorY   = (int)(Math.Floor(fTrueY));
                    iCeilingX = (int)(Math.Ceiling(fTrueX));
                    iCeilingY = (int)(Math.Ceiling(fTrueY));

                    // check bounds
                    if (iFloorX < 0 || iCeilingX < 0 || iFloorX >= iWidth || iCeilingX >= iWidth || iFloorY < 0 ||
                        iCeilingY < 0 || iFloorY >= iHeight || iCeilingY >= iHeight)
                    {
                        continue;
                    }

                    fDeltaX = fTrueX - iFloorX;
                    fDeltaY = fTrueY - iFloorY;

                    var clrTopLeft     = source.GetPixel(iFloorY * oldw + iFloorX);
                    var clrTopRight    = source.GetPixel(iFloorY * oldw + iCeilingX);
                    var clrBottomLeft  = source.GetPixel(iCeilingY * oldw + iFloorX);
                    var clrBottomRight = source.GetPixel(iCeilingY * oldw + iCeilingX);

                    fTopAlpha = (1 - fDeltaX) * (clrTopLeft.A) + fDeltaX * (clrTopRight.A);
                    fTopRed   = (1 - fDeltaX) * (clrTopLeft.R) + fDeltaX * (clrTopRight.R);
                    fTopGreen = (1 - fDeltaX) * (clrTopLeft.G) + fDeltaX * (clrTopRight.G);
                    fTopBlue  = (1 - fDeltaX) * (clrTopLeft.B) + fDeltaX * (clrTopRight.B);

                    // linearly interpolate horizontally between bottom neighbors
                    fBottomAlpha = (1 - fDeltaX) * (clrBottomLeft.A) + fDeltaX * (clrBottomRight.A);
                    fBottomRed   = (1 - fDeltaX) * (clrBottomLeft.R) + fDeltaX * (clrBottomRight.R);
                    fBottomGreen = (1 - fDeltaX) * (clrBottomLeft.G) + fDeltaX * (clrBottomRight.G);
                    fBottomBlue  = (1 - fDeltaX) * (clrBottomLeft.B) + fDeltaX * (clrBottomRight.B);

                    // linearly interpolate vertically between top and bottom interpolated results
                    iRed   = (int)(Math.Round((1 - fDeltaY) * fTopRed + fDeltaY * fBottomRed));
                    iGreen = (int)(Math.Round((1 - fDeltaY) * fTopGreen + fDeltaY * fBottomGreen));
                    iBlue  = (int)(Math.Round((1 - fDeltaY) * fTopBlue + fDeltaY * fBottomBlue));
                    iAlpha = (int)(Math.Round((1 - fDeltaY) * fTopAlpha + fDeltaY * fBottomAlpha));

                    var a = iAlpha + 1;

                    newSource.SetPixel(i * newWidth + j, new ColorHolder(iAlpha, iRed, iGreen, iBlue));
                }
            }

            return(newSource);
        }
Exemplo n.º 16
0
        // Source: http://incubator.quasimondo.com/processing/superfast_blur.php
        public static void ToLegacyBlurred(BitmapHolder source, int radius)
        {
            int w   = source.Width;
            int h   = source.Height;
            int wm  = w - 1;
            int hm  = h - 1;
            int wh  = w * h;
            int div = radius + radius + 1;

            int[] r = new int[wh];
            int[] g = new int[wh];
            int[] b = new int[wh];
            int   rsum, gsum, bsum, x, y, i, yp, yi, yw;

            int[] vmin = new int[Math.Max(w, h)];
            int[] vmax = new int[Math.Max(w, h)];

            int[] dv = new int[256 * div];
            for (i = 0; i < 256 * div; i++)
            {
                dv[i] = (i / div);
            }

            yw = yi = 0;

            for (y = 0; y < h; y++)
            {
                rsum = gsum = bsum = 0;
                for (i = -radius; i <= radius; i++)
                {
                    var p = source.GetPixel(yi + Math.Min(wm, Math.Max(i, 0)));
                    rsum += p.R;
                    gsum += p.G;
                    bsum += p.B;
                }
                for (x = 0; x < w; x++)
                {
                    r[yi] = dv[rsum];
                    g[yi] = dv[gsum];
                    b[yi] = dv[bsum];

                    if (y == 0)
                    {
                        vmin[x] = Math.Min(x + radius + 1, wm);
                        vmax[x] = Math.Max(x - radius, 0);
                    }
                    var p1 = source.GetPixel(yw + vmin[x]);
                    var p2 = source.GetPixel(yw + vmax[x]);

                    rsum += p1.R - p2.R;
                    gsum += p1.G - p2.G;
                    bsum += p1.B - p2.B;
                    yi++;
                }
                yw += w;
            }

            for (x = 0; x < w; x++)
            {
                rsum = gsum = bsum = 0;
                yp   = -radius * w;
                for (i = -radius; i <= radius; i++)
                {
                    yi    = Math.Max(0, yp) + x;
                    rsum += r[yi];
                    gsum += g[yi];
                    bsum += b[yi];
                    yp   += w;
                }
                yi = x;
                for (y = 0; y < h; y++)
                {
                    // Preserve alpha channel: ( 0xff000000 & pix[yi] )
                    var oldColor = source.GetPixel(yi);
                    var newColor = new ColorHolder(oldColor.A, dv[rsum], dv[gsum], dv[bsum]);
                    source.SetPixel(yi, newColor);
                    if (x == 0)
                    {
                        vmin[y] = Math.Min(y + radius + 1, hm) * w;
                        vmax[y] = Math.Max(y - radius, 0) * w;
                    }
                    var p1 = x + vmin[y];
                    var p2 = x + vmax[y];

                    rsum += r[p1] - r[p2];
                    gsum += g[p1] - g[p2];
                    bsum += b[p1] - b[p2];

                    yi += w;
                }
            }
        }