News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Rotating Images

Started by Grincheux, May 28, 2008, 11:20:53 AM

Previous topic - Next topic

Grincheux

Can someone give me url where I can find rotating images formulas (except Gimp) ?

Thanks
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz

Tedd

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.
No snowflake in an avalanche feels responsible.

jj2007

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

Neo

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

Grincheux

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 ?
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz

Grincheux

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 ?
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz

jj2007

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.

Neo

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.

Grincheux

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...
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz