The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Grincheux on May 28, 2008, 11:20:53 AM

Title: Rotating Images
Post by: Grincheux on May 28, 2008, 11:20:53 AM
Can someone give me url where I can find rotating images formulas (except Gimp) ?

Thanks
Title: Re: Rotating Images
Post by: Tedd on May 28, 2008, 01:56:56 PM
Simple 2D rotation:

x' = x*cos(a) - y*sin(a)
y' = x*sin(a) + y*cos(a)

('a' is the angle to be rotated through, in radians - turning anti-clockwise)

..although you need to be aware of the point you're rotating around - in your case I suppose you'll want to use the centre of the image, which means translating first, then rotating, then translating back -- you might want to read about matrix transformations.
Title: Re: Rotating Images
Post by: jj2007 on May 29, 2008, 10:39:17 PM
Quote from: Tedd on May 28, 2008, 01:56:56 PM
x' = x*cos(a) - y*sin(a)
y' = x*sin(a) + y*cos(a)
I had always wondered what FSINCOS was good for  :U
Title: Re: Rotating Images
Post by: Neo on June 01, 2008, 05:00:15 AM
If you want to make it relatively fast, since the whole image is rotated the same amount, you can get the angle adjustments once, then use them repeatedly.  You can even avoid the multiplications if you accumulate them instead.  The following should do something similar to what you're looking for (pardon the C code):


float cosa = cos(angle);
float sina = sin(angle);
float srcX = 0;
float srcY = 0;
int i = 0;
for (int y=0;y<height;++y) {
    for (int x=0;x<width;++x) {
        destBitmap[i] = srcBitmap[((int)srcY)*width+((int)srcX)];
        srcX += cosa;
        srcY -= sina;
        ++i;
    }
    srcX += sina;
    srcY += cosa;
}


Of course, you'll need bounds checking on srcX and srcY, the source and destination dimensions might be different, and you'll probably want to adjust so that it rotates around something other than 0,0.  Those should be relatively straightforward, though.  If you want to do something fancy like interpolation between pixels, that'd get a bit more complicated.

Best of luck!  :U
Title: Re: Rotating Images
Post by: Grincheux on June 03, 2008, 08:12:57 AM
Thanks for all, this what I was needed.

Just a question, the bitmap size can grow, how can I get the max width and height ?
Title: Re: Rotating Images
Post by: Grincheux on June 03, 2008, 10:29:39 AM
Suppose that the orginal image is RED.
The destination image has a black background.

Because of rounded values after computing sin/cos I could see black pixels between RED pixels ?
Title: Re: Rotating Images
Post by: jj2007 on June 03, 2008, 12:42:11 PM
Quote from: Grincheux on June 03, 2008, 10:29:39 AM
Suppose that the orginal image is RED.
The destination image has a black background.

Because of rounded values after computing sin/cos I could see black pixels between RED pixels ?

The problem is rounding errors plus varying pixel density. To avoid such gaps, you'll probably have to ask for each pixel what the corresponding colour would have been in the old position.
Title: Re: Rotating Images
Post by: Neo on June 03, 2008, 04:32:12 PM
Quote from: jj2007 on June 03, 2008, 12:42:11 PM
Quote from: Grincheux on June 03, 2008, 10:29:39 AM
Suppose that the orginal image is RED.
The destination image has a black background.

Because of rounded values after computing sin/cos I could see black pixels between RED pixels ?
The problem is rounding errors plus varying pixel density. To avoid such gaps, you'll probably have to ask for each pixel what the corresponding colour would have been in the old position.
What I posted does "ask for each pixel what the corresponding colour would have been in the old position."  Nice way of putting it, though; it helps clarify what the code is trying to accomplish.  I probably should've explained it a bit myself.  :wink

It just only rotates around (0,0) and doesn't expand the bounds, so some of the image will be rotated out of bounds.  You can set the destination bitmap to be a size of abs(width*cos(angle))+abs(height*sin(angle)) for the new width and abs(width*sin(angle))+abs(height*cos(angle)) for the new height, then fill it with some background colour, and rotate/translate the source image into the destination buffer.  There won't be any problems with "empty" pixels (other than the background around the rotated image) as long as you check the source coordinates you calculate against the bounds of the source image.
Title: Re: Rotating Images
Post by: Grincheux on June 03, 2008, 04:52:52 PM
Thanks it is clear now, I will try it asap.

I am writing a program to display images using some math functions to get nice effects.
And I am not very good in math... as you can see...