Rotating Images
Posted: Sun Feb 10, 2008 7:49 pm
Some sharing... 

Code: Select all
; credits - http://www.leunen.com/cbuilder/rotbmp.html
Procedure.f Max(ffirst.f, fsecond.f)
If ffirst > fsecond
ProcedureReturn ffirst
Else
ProcedureReturn fsecond
EndIf
EndProcedure
Procedure.f Min(ffirst.f, fsecond.f)
If ffirst > fsecond
ProcedureReturn fsecond
Else
ProcedureReturn ffirst
EndIf
EndProcedure
UseJPEGImageDecoder()
Procedure RotateImage(ImageNumber.l, Angle.l)
Protected radians.f = (2 * #PI * Angle) / 360
Protected imgcos.f = Cos(radians)
Protected imgsin.f = Sin(radians)
Protected imgwidth = ImageWidth(ImageNumber)
Protected imgheight = ImageWidth(ImageNumber)
Protected point1x.f = -imgheight * imgsin
Protected point1y.f = imgheight * imgcos
Protected point2x.f = imgwidth * imgcos - imgheight * imgsin
Protected point2y.f = imgheight * imgcos + imgwidth * imgsin
Protected point3x.f = imgwidth * imgcos
Protected point3y.f = imgwidth * imgsin
Protected minx.f = min(0, min(point1x, min(point2x, point3x)))
Protected miny.f = min(0, min(point1y, min(point2y, point3y)))
Protected maxx.f = max(0, max(point1x, max(point2x, point3x)))
Protected maxy.f = max(0, max(point1y, max(point2y, point3y)))
Protected timgw = Int(Abs(maxx) - minx)
Protected timgh = Int(Abs(maxy) - miny)
Protected timg = CreateImage(#PB_Any, timgw, timgh)
Protected tempx, tempy, tempc, findx, findy
For tempx = 0 To timgw
For tempy = 0 To timgh
findx = (tempx + minx) * imgcos + (tempy + miny) * imgsin
findy = (tempy + miny) * imgcos - (tempx + minx) * imgsin
If (findx >= 0 And findx < imgwidth) And (findy >= 0 And findy < imgheight)
StartDrawing(ImageOutput(ImageNumber))
tempc = Point(findx, findy)
StopDrawing()
StartDrawing(ImageOutput(timg))
Plot(tempx, tempy, tempc)
StopDrawing()
EndIf
Next
Next
GrabImage(timg, ImageNumber, 0, 0, timgw, timgh)
FreeImage(timg)
EndProcedure
LoadImage(1,"c:\path_to_your_pic.jpg") ;TODO
angle = 0
If OpenWindow(0,0,0,600,500,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
If CreateGadgetList(WindowID(0))
ImageGadget(0,30,30,400,400,ImageID(1))
ButtonGadget(1,0,0,40,20,"")
Repeat
a = WaitWindowEvent()
Select a
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case 1
If angle >= 360
angle = 0
Else
angle = angle + 10
EndIf
SetGadgetText(1, Str(angle))
CopyImage(1, 2)
RotateImage(2, angle)
SetGadgetState(0, ImageID(2))
EndSelect
EndSelect
ForEver
EndIf
EndIf