|
-
May 28th, 2002, 01:42 AM
#1
Thread Starter
Addicted Member
Translating my bitmap rotation (Zaei?)
I'm testing out an algorithm I found on a few C++ sites for doing a destination-source mapped bitmap rotate, and it all works, except I can't translate the rotation from 0,0 to the centerpoint. The first time I messed with it, I got it to work, but I can't figure out how I did it. I figure Zaei or someone else mathy can help me (I still haven't taken any advanced math or geometry).
Don't worry about the calls and everything, they all work fine.
RotateRect calculates the necessary Width and Height to contain the rotated image.
Resize resizes the buffer to hold it.
GetPixelAARolloff is basically GetPixel, but BiLinear.
VB Code:
sAngle = Angle * (c_dblPi / 180)
duCol = Sin(-sAngle) * (1)
dvCol = Cos(-sAngle) * (1)
m_dblX2 = SourceImage.Width
m_dblY2 = SourceImage.Height
SoftFX.RotateRect m_dblX2, m_dblY2, m_dblXO, m_dblYO, CDbl(sAngle)
DestImage.Resize m_dblX2, m_dblY2
X = m_dblX2 / 2
Y = m_dblY2 / 2
DestImage.Clear F2RGB(64, 96, 128, 0)
duRow = dvCol
dvRow = -duCol
startu = (SourceImage.Width / 2)
startv = (SourceImage.Height / 2)
rowu = startu - ((X * duCol) + (Y * duRow))
rowv = startv - ((X * dvCol) + (Y * dvRow))
For dY = 0 To DestImage.Height - 1
sX = rowu
sY = rowv
For dX = 0 To DestImage.Width - 1
DestImage.SetPixel dX, dY, SourceImage.GetPixelAARolloff(sX, sY)
sX = sX + duRow
sY = sY + dvRow
Next dX
rowu = rowu + duCol
rowv = rowv + dvCol
Next dY
"1 4m 4 1337 #4xz0r!'
Janus
-
May 28th, 2002, 06:33 AM
#2
Frenzied Member
alright... I looked at this, and I think you need some major changing before this code will rotate around the center...
take a look at the rotation tutorial on www.ur.co.nz ... I am pretty sure that that's the best way to turn around the center...
http://www.ur.co.nz/urcorp/default.a...ata_article=69
you need a log in for that though.. it's free and I never got unwanted emails...
Sanity is a full time job
Puh das war harter Stoff!
-
May 28th, 2002, 07:27 AM
#3
Good Ol' Platypus
Can you not just add or subtract 0.5(newwidth) and 0.5(newheight) to get the centrepoint? (ex. if it's rotated around the top-left corner, add both. If its around the bottom-right corner, subtract both).
Or is it rotated in some supremely odd way?
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
May 28th, 2002, 09:57 AM
#4
Code:
Dim cA as Single
Dim sA as Single
cA = Cos(Theta*(3.14159/180))
sA = Sin(Theta*(3.14159/180))
Dim l as Single
Dim i as Long
Dim j as Long
For i = 0 to width
For j = 0 to height
l = sqr(i^2 + j^2)
SetPixel(l*cA, l*sA, GetPixel(i, j))
Next j
Next i
That should do a standard rotation, around the upper left corner.
Now, for the translation...
Code:
Dim cA as Single
Dim sA as Single
Dim cX as Long
Dim cY as Long
Dim dcX as Long
Dim dcY as Long
cA = Cos(Theta*(3.14159/180))
sA = Sin(Theta*(3.14159/180))
Dim l as Single
Dim i as Long
Dim j as Long
cX = width/2
cY = height/2
dcX = destWidth/2
dcY = destHeight/2
For i = 0 to width
For j = 0 to height
l = sqr((cX+i)^2 + (cY + j)^2)
SetPixel(l*cA+dcX, l*sA+dcY, GetPixel(i, j))
Next j
Next i
Try that... Im not sure if it will work (its off the top of my head, and I cant test it).
Z.
-
May 28th, 2002, 11:58 AM
#5
Thread Starter
Addicted Member
I wasn't sure this code would rotate around the center either... the annoying thing is, I know it does, because I got it to by accident once, and it does in a sample in another language. However, the sample is too simple to be used for this.
Zaei, I appreciate the try, but I have to use this algorithm. I'll see if I can find any tricks to use from that one.
Adding and subtracting doesn't seem to actually translate the rotation, it just translates the bitmap.
"1 4m 4 1337 #4xz0r!'
Janus
-
May 28th, 2002, 03:06 PM
#6
Good Ol' Platypus
Oohhh I get it then.
Well what you do is use cos() and sin() with the radius as the number of pixels away from the centre pixel...
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
May 28th, 2002, 08:27 PM
#7
Thread Starter
Addicted Member
Using Cos() or Sin() in the main loop would totally ruin my performance - this function's very fast.
"1 4m 4 1337 #4xz0r!'
Janus
-
May 29th, 2002, 04:40 AM
#8
Frenzied Member
well the advantage of this function is that it calculates sin and cos once... and than goes through the picture at an angle with help of the x and y component (found through cos and sin).
But using sin and cos does not have to slow down your function completely...
sin(x)=cos(x-90) or sin(x)=cos(x-Pi/2) if you are using radians
so all you have to do now is precalculate a little array from one to 360.
Pseudocode (you'd need to do it for radians)
For x = 0 to 359
lsin(x) = sin(x)
next
now get cos of an angle like lsin(angle-90) and the sin lsin(angle)
this still is pretty fast.
Sanity is a full time job
Puh das war harter Stoff!
-
May 29th, 2002, 09:20 AM
#9
You only need compute the sine and cosine once, as you only have one angle.
Z.
-
May 29th, 2002, 12:01 PM
#10
Frenzied Member
how does the algorithm look than? (I rember having seen something like that.. pretty close to the one posted here I guess... well I really do not feel like figuring it out again...)
it can be done really easy with a whole table of sin and cos... the other way must be faster though...
Sanity is a full time job
Puh das war harter Stoff!
-
May 29th, 2002, 05:53 PM
#11
Thread Starter
Addicted Member
The Cos and Sin aren't the only part that's slow, the Floating Point Multiply is much slower as well. I guess I could try and integerize it.
"1 4m 4 1337 #4xz0r!'
Janus
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|