Preface: I'm not sure whether to categorize this as more of a math question or a programming question, plus I'm a noob at Linear Algebra and doing this for a hobby. Any help is greatly appreciated.
Let's say I've got some arbitrary programming language that doesn't have any established mechanisms to perform linear transformations on say a bitmap image (or any arbitrary collection of x,y values). Let's say I want to perform some arbitrary rotation, scale, and translation.
Right now I would iterate through each x,y, get the pixel color, and perform my transformation on it, round to the nearest new x,y to copy my pixel color value to, and then produce a final image. It works for a simple rotation that I've precalculated, but takes several seconds to calculate inside my TransformImage method below on an i5 so I'm wondering what's a faster way?
This is my current method just tesing in C#:
Color[,] BackupOriginal = OriginalColorData.Clone() as Color[,];
float angle = 25.0f;
float angleRadians = (float)(angle * (Math.PI / 180f));
float cosAngle = (float)Math.Cos(angleRadians);
float sinAngle = (float)Math.Sin(angleRadians);
BackupOriginal = LinearTransformation.TransformImage(BackupOriginal, new float[2, 2] {
{cosAngle,-1f * sinAngle},
{sinAngle,cosAngle}
});
...
public static Color[,] TransformImage(Color[,] originalImage, float[,] transformationMatrix)
{
if (transformationMatrix.GetUpperBound(1) < 1 || transformationMatrix.GetUpperBound(0) < 1) return null;
int width = originalImage.GetUpperBound(1) + 1;
int height = originalImage.GetUpperBound(0) + 1;
Color[,] newImage = new Color[height, width];
for (int y=0;y<height;y++)
{
for (int x=0;x<width;x++)
{
Color currentPixel = originalImage[y, x];
int newX = (int)Math.Round((x * transformationMatrix[0, 0]) + (y * transformationMatrix[0, 1]));
int newY = (int)Math.Round((x * transformationMatrix[1, 0]) + (y * transformationMatrix[1, 1]));
if (IsValidPixel(newX, newY, width, height))
newImage[newY, newX] = currentPixel;
}
}
return newImage;
}