I'm struggling with a problem with image rotation. I'm using the idea of Rotation by area mapping in the following link:http://www.leptonica.com/rotation.htmlMy c# codes are as follows. The idea is first to find the dimension of the rotated image, then find the gray level of each pixel in the rotated image. To find the gray level for each pixel, the pixel coordinates are transformed back to the coordinates in the original image (called original pixel). The original pixel has fractional coordinate values, then the bilinear interpolation method in the link is used to obtain the gray level of the pixel in the rotated image. My codes did most of the job, except there are some clipping at the corners in the rotated image. I put the rotated images of a 16*16 image, we can clearly see the effect: The bottom line in the 45 degree rotated has no gray data; in the 90 degree rotated one, the vertical line in the left has no gray data; and in the 180 degree rotated one, both the vertical line in the left and the horizontal line on the top have no gray. I believe it's caused by the coordinate computation, and I could modify my code by subtracting one of the coordinates by 1 to achieve correct rotation by a certain angle, but it doesn't work for the other case. So I guess there must be something I ignored in my codes. Any suggestions and comments are appreciated!public void RotateImageClockwise(double degree){    Image<double> image = (Image<double>)(PreservedRawImage.Clone());              double sinAngle = Math.Sin(Math.PI * degree / 180.0);        double cosAngle = Math.Cos(Math.PI * degree / 180.0);        double newHeightRaw, newWidthRaw;        ////Find the new dimensions of the rotated image.        if (Math.Abs(degree) <= 90)        {            newHeightRaw = image.Width * sinAngle + image.Height * cosAngle;            newWidthRaw = image.Width * cosAngle + image.Height * sinAngle;        }        else if (Math.Abs(degree) <= 180)        {            newHeightRaw = image.Width * Math.Sin(Math.PI * (180 - degree) / 180.0) + image.Height * Math.Sin(Math.PI * (degree - 90) / 180.0);            newWidthRaw = image.Width * Math.Cos(Math.PI * (180 - degree) / 180.0) + image.Height * Math.Cos(Math.PI * (degree - 90) / 180.0);        }        else            throw new Exception(Rotation angle is not right.);        ////Done with finding the new dimensions        double newWidthHalf = newWidthRaw / 2;        double newHeightHalf = newHeightRaw / 2;        int newWidth = (int)Math.Round(newWidthRaw);        int newHeight = (int)Math.Round(newHeightRaw);        Image<double> newImage = new Image<double>(newWidth, newHeight);        byte[] bitmap = new byte[newWidth * newHeight];        double newMax = double.MinValue, newMin = double.MaxValue;        for (int x = 0; x < newHeight; x++)        {            for (int y = 0; y < newWidth; y++)            {                ////Transform the coordinates to be centered at the center of the image                double xt = x - newHeightHalf;                double yt = y - newWidthHalf;                double originalX = xt * cosAngle - yt * sinAngle + image.Height / 2.0;                double originalY = xt * sinAngle + yt * cosAngle + image.Width / 2.0;                double fractionX = Math.Abs(originalX - (int)Math.Round(originalX));                double fractionY = Math.Abs(originalY - (int)Math.Round(originalY));                ////Find the neighbors of the pixel in the original image                IList<Point<int>> neighbors = new List<Point<int>> { new Point<int>{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY), weight = (1 - fractionX) * (1 - fractionY)},                                                            new Point<int>{x = (int)Math.Round(originalX), y = (int)Math.Round(originalY) + 1, weight = fractionX * (1 - fractionY)},                                                           new Point<int>{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY), weight = (1 - fractionX) * fractionY},                                                           new Point<int>{x = (int)Math.Round(originalX) + 1, y = (int)Math.Round(originalY) + 1, weight = fractionX * fractionY}                                                         };                bool hasData = false;                double newData = 0;                double weight = 0;                foreach (var neighbor in neighbors)                {                    if (neighbor.x >= 0 && neighbor.x < image.Height                        && neighbor.y >= 0 && neighbor.y < image.Width                       )                    {                        hasData = true;                        newData += neighbor.weight * image.Data[(neighbor.x) * image.Width + neighbor.y];                        weight += neighbor.weight;                    }                }                                    if (hasData && weight != 0)                {                    newData /= weight;                    newImage.Data[x * newWidth + y] = newData;                }            }                        }                } 
Rotate image around its center
image processing