Mesa wrote:Hello,
I've got a problem with image's rotation.
With a rotation of -90 degrees the image is truncated but not with an angle of -30°, -60° etc.
An idea ?
The problem seems to be your line:
It changes the origin and the box you paint later is too small.
If you change:
to:
it works correctly again.
I added a TrackbarGadget at the top for the rotation.
Look at the line with comment "FIX" and try it.
Code: Select all
;{- Enumerations / DataSections
XIncludeFile "gDrawing.pbi"
;{ Windows
Enumeration
#Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
#Button_0
#ScrollArea_1
#ImageGadget_1
#Image_1
#Image_2
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;Define image$,outFichier$
;}
Global Dim Tableau(8000, 8000)
Procedure.s openimage()
Protected FichierParDefaut$,Filtre$,Filtre,image$
FichierParDefaut$ = ""
Filtre$ = "Images (*.bmp)|*.bmp|All (*.*)|*.*"
Filtre = 0
image$ = OpenFileRequester("Image", FichierParDefaut$, Filtre$, Filtre)
ProcedureReturn image$
EndProcedure
Procedure OpenWindow_Window_0()
If OpenWindow(#Window_0, 3, 2, 1020, 760, "Window_0", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
ButtonGadget(#Button_0, 20, 15, 135, 30, "Open image")
ScrollAreaGadget(#ScrollArea_1, 5, 60, 1000, 650, 4000, 3000, 100, #PB_ScrollArea_Single)
ImageGadget(#ImageGadget_1, 10, 10, 565, 450, 0, #PB_Image_Border)
CloseGadgetList()
EndIf
EndProcedure
Procedure rotation(image.l, pivotX.l, PivotY.l, Angle.f, WrapMode.l)
;beware pivotX in % and pivotY in %
wi=ImageWidth(image)
hi=ImageHeight(image)
Anglerad.f=angle*#PI/180 ; in radian
wf = wi * Abs(Cos(Anglerad)) + hi * Abs(Sin(Anglerad))+5 ; width final
hf = hi * Abs(Cos(Anglerad)) + wi * Abs(Sin(Anglerad))+5 ; height final
NewImageID = CreateImage(#PB_Any, wf, hf)
; Debug wf
; Debug hf
pivotX=pivotX*wi/100 ; transformation % to pix
pivotY=pivotY*hi/100 ; transformation % to pix
xi=pivotX+(0-pivotX)*Cos(Anglerad)+(0-PivotY)*Sin(Anglerad)
yi=PivotY-(0-pivotX)*Sin(Anglerad)+(0-PivotY)*Cos(Anglerad)
yi=-yi
; Debug xi
; Debug yi
If angle<-90
xi=pivotX+(wi-pivotX)*Cos(Anglerad)+(0-PivotY)*Sin(Anglerad)
yi=PivotY-(0-pivotX)*Sin(Anglerad)+(hi-PivotY)*Cos(Anglerad)
Debug xi
Debug yi
yi=-yi
EndIf
If gStartDrawing(ImageOutput(NewImageID))
Box(0,0,wf,hf,#White)
logo = gBufferImage(ImageID(image))
;logo=image
gSetPenImage(logo,WrapMode)
;gStartTransform()
gSetOrigin(-xi,yi)
;gSetPenTransform(pivotX,PivotY,Angle) ; Texture :pivot par rapport à ???
gRotateAt(pivotX,PivotY,Angle)
;gRotate(Angle)
; draw a cross at the pivot
gBox(0,0, wf , hf) ; <-- WITHOUT FIX
;gBox(0,0, wf+xi, hf+yi) ; <-- FIX
gResetTransform()
gLineXY(0,0+yi,wf,yi,$FFFFFF00)
;gResetTransform()
gLineXY(wi/2-xi,0,wi/2-xi,hf,$FFFFFF00)
gStopdrawing()
EndIf
ProcedureReturn NewImageID
EndProcedure
OpenWindow_Window_0()
If gInit()
OpenWindow_Window_0()
; rotation 0 to -150°
rotationInDegrees = -85;-90 ; I've got a problem with -90°, the image is truncated
pivotX=50 ; 50% of the with
pivotY=0
trackbar=TrackBarGadget(#PB_Any,200,20,400,20,0,180)
SetGadgetState(trackbar,rotationInDegrees+180)
;{- Event loop
Repeat
Event = WaitWindowEvent()
Select Event
; ///////////////////
Case #PB_Event_Gadget
EventGadget = EventGadget()
EventType = EventType()
If EventGadget = #Button_0
image$=openimage()
If image$
LoadImage(#Image_1,image$)
SetGadgetState(#ImageGadget_1,ImageID(#Image_1))
image2=rotation(#Image_1, pivotX, 0, rotationInDegrees, #WrapModeClamp)
; Enumeration ; WrapMode
; #WrapModeTile ; 0 ; fonctionne avec une grande image
; #WrapModeTileFlipX ; 1
; #WrapModeTileFlipY ; 2
; #WrapModeTileFlipXY ; 3
; #WrapModeClamp ; 4
; EndEnumeration
SetGadgetState(#ImageGadget_1,ImageID(Image2))
EndIf
ElseIf EventGadget=trackbar
rotationInDegrees = GetGadgetState(trackbar)-180
FreeImage(image2)
image2=rotation(#Image_1, pivotX, 0, rotationInDegrees, #WrapModeClamp)
SetGadgetState(#ImageGadget_1,ImageID(Image2))
EndIf
; ////////////////////////
Case #PB_Event_CloseWindow
EventWindow = EventWindow()
If EventWindow = #Window_0
CloseWindow(#Window_0)
Break
EndIf
EndSelect
ForEver
;
gEnd()
EndIf
;}
If you don't need image tiling, you could use gDrawImage directly:
Code: Select all
Procedure rotation(image.l, pivotX.l, PivotY.l, Angle.f, WrapMode.l)
;beware pivotX in % and pivotY in %
wi=ImageWidth(image)
hi=ImageHeight(image)
Anglerad.f=angle*#PI/180 ; in radian
wf = wi * Abs(Cos(Anglerad)) + hi * Abs(Sin(Anglerad))+5 ; width final
hf = hi * Abs(Cos(Anglerad)) + wi * Abs(Sin(Anglerad))+5 ; height final
NewImageID = CreateImage(#PB_Any, wf, hf)
; Debug wf
; Debug hf
pivotX=pivotX*wi/100 ; transformation % to pix
pivotY=pivotY*hi/100 ; transformation % to pix
xi=pivotX+(0-pivotX)*Cos(Anglerad)+(0-PivotY)*Sin(Anglerad)
yi=PivotY-(0-pivotX)*Sin(Anglerad)+(0-PivotY)*Cos(Anglerad)
yi=-yi
; Debug xi
; Debug yi
If angle<-90
xi=pivotX+(wi-pivotX)*Cos(Anglerad)+(0-PivotY)*Sin(Anglerad)
yi=PivotY-(0-pivotX)*Sin(Anglerad)+(hi-PivotY)*Cos(Anglerad)
Debug xi
Debug yi
yi=-yi
EndIf
If gStartDrawing(ImageOutput(NewImageID))
Box(0,0,wf,hf,#White)
gSetOrigin(-xi,yi)
gRotateAt(pivotX,PivotY,Angle)
gDrawImage(image,0,0)
gResetTransform()
gLineXY(0,0+yi,wf,yi,$FFFFFF00)
gLineXY(wi/2-xi,0,wi/2-xi,hf,$FFFFFF00)
gStopdrawing()
EndIf
ProcedureReturn NewImageID
EndProcedure