/// <summary>
        /// Rotates the bitmap in any degree returns a new rotated WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="angle">Arbitrary angle in 360 Degrees (positive = clockwise).</param>
        /// <param name="crop">if true: keep the size, false: adjust canvas to new size</param>
        /// <returns>A new WriteableBitmap that is a rotated version of the input.</returns>
        public static unsafe BitmapBuffer RotateFree(this BitmapBuffer bmp, double angle, bool crop = true)
        {
            // rotating clockwise, so it's negative relative to Cartesian quadrants
            double cnAngle = -1.0 * (Math.PI / 180) * angle;

            // 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;

            using (var bmpContext = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                iWidth  = bmpContext.Width;
                iHeight = bmpContext.Height;

                if (crop)
                {
                    newWidth  = iWidth;
                    newHeight = iHeight;
                }
                else
                {
                    double rad = angle / (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;

                BitmapBuffer bmBilinearInterpolation = BitmapBufferFactory.New(newWidth, newHeight);

                using (BitmapContext bilinearContext = bmBilinearInterpolation.GetBitmapContext())
                {
                    int *newp = bilinearContext.Pixels._inf32Buffer;
                    int *oldp = bmpContext.Pixels._inf32Buffer;
                    //
                    int oldw = bmpContext.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
                                    newp[i * newWidth + j] = oldp[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;

                            int clrTopLeft     = oldp[iFloorY * oldw + iFloorX];
                            int clrTopRight    = oldp[iFloorY * oldw + iCeilingX];
                            int clrBottomLeft  = oldp[iCeilingY * oldw + iFloorX];
                            int clrBottomRight = oldp[iCeilingY * oldw + iCeilingX];

                            fTopAlpha = (1 - fDeltaX) * ((clrTopLeft >> 24) & 0xFF) + fDeltaX * ((clrTopRight >> 24) & 0xFF);
                            fTopRed   = (1 - fDeltaX) * ((clrTopLeft >> 16) & 0xFF) + fDeltaX * ((clrTopRight >> 16) & 0xFF);
                            fTopGreen = (1 - fDeltaX) * ((clrTopLeft >> 8) & 0xFF) + fDeltaX * ((clrTopRight >> 8) & 0xFF);
                            fTopBlue  = (1 - fDeltaX) * (clrTopLeft & 0xFF) + fDeltaX * (clrTopRight & 0xFF);

                            // linearly interpolate horizontally between bottom neighbors
                            fBottomAlpha = (1 - fDeltaX) * ((clrBottomLeft >> 24) & 0xFF) + fDeltaX * ((clrBottomRight >> 24) & 0xFF);
                            fBottomRed   = (1 - fDeltaX) * ((clrBottomLeft >> 16) & 0xFF) + fDeltaX * ((clrBottomRight >> 16) & 0xFF);
                            fBottomGreen = (1 - fDeltaX) * ((clrBottomLeft >> 8) & 0xFF) + fDeltaX * ((clrBottomRight >> 8) & 0xFF);
                            fBottomBlue  = (1 - fDeltaX) * (clrBottomLeft & 0xFF) + fDeltaX * (clrBottomRight & 0xFF);

                            // 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));

                            // make sure color values are valid
                            if (iRed < 0)
                            {
                                iRed = 0;
                            }
                            if (iRed > 255)
                            {
                                iRed = 255;
                            }
                            if (iGreen < 0)
                            {
                                iGreen = 0;
                            }
                            if (iGreen > 255)
                            {
                                iGreen = 255;
                            }
                            if (iBlue < 0)
                            {
                                iBlue = 0;
                            }
                            if (iBlue > 255)
                            {
                                iBlue = 255;
                            }
                            if (iAlpha < 0)
                            {
                                iAlpha = 0;
                            }
                            if (iAlpha > 255)
                            {
                                iAlpha = 255;
                            }

                            int a = iAlpha + 1;
                            newp[i * newWidth + j] = (iAlpha << 24)
                                                     | ((byte)((iRed * a) >> 8) << 16)
                                                     | ((byte)((iGreen * a) >> 8) << 8)
                                                     | ((byte)((iBlue * a) >> 8));
                        }
                    }
                    return(bmBilinearInterpolation);
                }
            }
        }
        /// <summary>
        /// Rotates the bitmap in 90° steps clockwise and returns a new rotated WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="angle">The angle in degrees the bitmap should be rotated in 90° steps clockwise.</param>
        /// <returns>A new WriteableBitmap that is a rotated version of the input.</returns>
        public static unsafe BitmapBuffer Rotate(this BitmapBuffer bmp, FastRotateAngle angle)
        {
            using (BitmapContext context = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                // Use refs for faster access (really important!) speeds up a lot!
                int  w = context.Width;
                int  h = context.Height;
                int *p = context.Pixels._inf32Buffer;
                int  i = 0;


                switch (angle)
                {
                default:
                {
                    return(bmp.Clone());
                }

                case FastRotateAngle.Rotate90:
                {
                    BitmapBuffer result = BitmapBufferFactory.New(h, w);
                    using (BitmapContext destContext = result.GetBitmapContext())
                    {
                        int *rp = destContext.Pixels._inf32Buffer;
                        for (int x = 0; x < w; x++)
                        {
                            for (int y = h - 1; y >= 0; y--)
                            {
                                int srcInd = y * w + x;
                                rp[i] = p[srcInd];
                                i++;
                            }
                        }
                    }
                    return(result);
                }

                case FastRotateAngle.Rotate180:
                {
                    BitmapBuffer result = BitmapBufferFactory.New(w, h);
                    using (BitmapContext destContext = result.GetBitmapContext())
                    {
                        int *rp = destContext.Pixels._inf32Buffer;
                        for (int y = h - 1; y >= 0; y--)
                        {
                            for (int x = w - 1; x >= 0; x--)
                            {
                                int srcInd = y * w + x;
                                rp[i] = p[srcInd];
                                i++;
                            }
                        }
                    }
                    return(result);
                }

                case FastRotateAngle.Rotate270:
                {
                    var result = BitmapBufferFactory.New(h, w);
                    using (BitmapContext destContext = result.GetBitmapContext())
                    {
                        int *rp = destContext.Pixels._inf32Buffer;
                        for (int x = w - 1; x >= 0; x--)
                        {
                            for (int y = 0; y < h; y++)
                            {
                                int srcInd = y * w + x;
                                rp[i] = p[srcInd];
                                i++;
                            }
                        }
                    }
                    return(result);
                }
                }
            }
        }