Page 3 of 3

Re: Rotate Image

Posted: Wed Nov 18, 2015 10:48 am
by davido
@Dr Soong,

Please take a look at the following code by BasicallyPure. It does 90 degree rotation, simply, but very well:
http://www.purebasic.fr/english/viewtop ... 25#p445225

Re: Rotate Image

Posted: Wed Nov 18, 2015 12:14 pm
by wilbert
With the vector drawing library from PB 5.40+ you can draw rotated images without the need for platform specific api functions.

Re: Rotate Image

Posted: Wed Nov 18, 2015 5:54 pm
by BasicallyPure
This procedure will rotate 90 deg. in either direction no API required.
It's the same as the link provided by davido but you don't have to search through a huge program to find it.

Code: Select all

Procedure ROTATE_90(image, dir)
   ; rotate image +/- 90 deg.
   ; 'image' is the number of the image to rotate
   ; if 'dir' = 0 then rotate left else rotate right
   
   Protected a,b,c,e,f,h,s,w,x,y,ym,xm,tempImg,depth
   
   If IsImage(image) = 0 : ProcedureReturn 0 : EndIf
   
   StartDrawing(ImageOutput(image))
      w = OutputWidth()
      h = OutputHeight()
      f = DrawingBufferPixelFormat() & $7F
   StopDrawing()
      
      If f = #PB_PixelFormat_32Bits_RGB Or f = #PB_PixelFormat_32Bits_BGR
         depth = 32
      ElseIf f = #PB_PixelFormat_24Bits_RGB Or f = #PB_PixelFormat_24Bits_BGR
         depth = 24
      Else
         ProcedureReturn 0
      EndIf
   
   If w > h : s = w : Else : s = h : EndIf ; find the largest dimension
   
   tempImg = CreateImage(#PB_Any,s,s,depth) ; make a square working area
   
   StartDrawing(ImageOutput(tempImg))
      If depth = 32 : DrawingMode(#PB_2DDrawing_AllChannels) : EndIf
      
      DrawImage(ImageID(image),0,0)
      
      ym = s/2-1 ; max y loop value
      xm = s/2-(s!1&1) ; max x value, subtract 1 if 's' is even
      s-1
      
      If dir <> 0 ; rotate right
         For y = 0 To ym
            For x = 0 To xm
               e = Point(x,y)
               a = s-x : Plot(x,y,Point(y,a))
               b = s-y : Plot(y,a,Point(a,b))
               c = s-a : Plot(a,b,Point(b,c))
               Plot(b,c,e)
            Next x
         Next y
      Else ; rotate left
         For y = 0 To ym
            For x = 0 To xm
               e = Point(x,y)
               a = s-y : Plot(x,y,Point(a,x))
               b = s-x : Plot(a,x,Point(b,a))
               c = s-a : Plot(b,a,Point(c,b))
               Plot(c,b,e)
            Next x
         Next y
      EndIf
      
   StopDrawing()
   
   If dir <> 0
      GrabImage(tempImg,image,s-h+1,0,h,w) ; right
   Else
      GrabImage(tempImg,image,0,s-w+1,h,w) ; left
   EndIf
   
   FreeImage(tempImg)
   
   ProcedureReturn 1
EndProcedure
Here is one that uses direct image buffer access so it will be a little faster.
http://www.purebasic.fr/english/viewtop ... 00#p438200
Keep in mind Wilbert's warning on this one though. http://www.purebasic.fr/english/viewtop ... 85#p437885

Re: Rotate Image

Posted: Thu Oct 03, 2019 11:02 am
by dobro
another version ...

Code: Select all



;Rotate image
;***********************************************
;Titre  :*rotate image
;Auteur  : Dobro
;Date  :30/09/2013
;Heure  :11:31:58
;Version Purebasic :  PureBasic 5.20 LTS (Windows - x8
;Version de l'editeur :EPB V2.46
; Libairies necessaire : Aucune 
;***********************************************
; en purebasic 5.70
Declare  rotate_image(id_destination,x2,y2,degres,array tabl1(2))
Declare.l RotateImageEx2(ImageID, Angle.f)

UseJPEGImageDecoder () :UsePNGImageDecoder()
InitSprite ()
#dobro =1
#Police =1
#Sprite =1
#image1 =1
#image2 =2

; ***********************************
FontID = LoadFont ( #Police , "arial" , 50, #PB_Font_Bold )
EcranX = GetSystemMetrics_ ( #SM_CXSCREEN )
;=largeur de l'ecran
EcranY = GetSystemMetrics_ ( #SM_CYSCREEN )
;=hauteur de l'ecran
WindowID = OpenWindow (1, 0, 0, EcranX, EcranY, "hello" , #PB_Window_BorderLess |#PB_Window_ScreenCentered )
WindowID = WindowID (1)
Result = OpenWindowedScreen ( WindowID ,0,0, EcranX, EcranY, 1, 0,0)

; principe de rotation
;x1 = coordonée x du point rotationé
;Y1= coordonée y du point rotationé
; a = angle de rotation
; x1 = x * Cos(a) + Y * Sin(a);
; y1 = -x * Sin(a) + Y * Cos(a)
im$=OpenFileRequester("open jpg","c:\","*.jpg",0)
;im$="D:\Dobro\Mes_Photos\Nouveau dossier\Sv101107.jpg"
LoadImage ( #image1 , im$ ) ;<----- path of the picture
largeur = ImageWidth(#image1)
hauteur = ImageHeight(#image1)
; 
if largeur>1024 or hauteur>768
		ResizeImage(#image1,1024,768)
		largeur = 1024
		hauteur =768
Endif
; 

CreateImage ( #image2 ,largeur*4,hauteur*4 ) ; image destination
; ******* mise en tableau de l'image **********
Dim tabl1(largeur ,hauteur )
StartDrawing ( ImageOutput ( #image1) )
		For Y=1 To hauteur-1
				For x=1 To largeur-1
						tabl1(x,Y)= Point (x,Y)
				Next x
		Next Y
StopDrawing ()
;*****************************************************

Resultat = InitMouse ()
TempsDepart = ElapsedMilliseconds()
Repeat
		ExamineMouse ()
		Event= WindowEvent ()
		Delay (2)
		angle_degres=angle_degres+10
		If angle_degres>360
				Event= #PB_Event_CloseWindow
				TempsEcoule = ElapsedMilliseconds()-TempsDepart
				break
		EndIf
		
		rotate_image(#image2,largeur, hauteur,  angle_degres,tabl1())   ; Call Rotate !
		
		; ****** **on affiche l'image ******** (Display image )
		StartDrawing ( ScreenOutput ())
				DrawImage ( ImageID ( #image2 ), 1, 1)
		StopDrawing ()
		; *******************************
		; affiche l'ecran
		FlipBuffers ()
		;efface l'ecran
		ClearScreen ( RGB (0, 0, 0)) 
		
		Event= WindowEvent ()
		;}
		If MouseButton (2)
				End
		EndIf
Until Event= #PB_Event_CloseWindow


;- **********************procedures Zone *********************************

Procedure rotate_image(id_destination,x2,y2,degres,array tabl1(2))
		; By Dobro	
		x3=x2/2 ; le milieu x de l'image
		y3=y2/2 ; le milieu y de l'image	
		StartDrawing ( ImageOutput ( id_destination ) )
				box(1,1,x2*2,y2*2,$0) ; clearimage
				For Y=1 To y2-1
						For x=1 To x2-1
								; ********* voici la formule de la rotation d'image *********
								x_arr.d= x3+(x-x3) * Cos (degres* #PI /180) +( Y-y3)* Sin (degres* #PI /180)
								Y_arr.d= y3-(x-x3) * Sin (degres* #PI /180) + (Y-y3)* Cos (degres* #PI /180)
								;*****************************************************			
								Plot (x_arr.d+(x2/2),Y_arr.d+(y2/3),tabl1( x,y)) ; on dessine l'image rotaté a l'aide du tableau de points : D
						Next x
				Next Y
		StopDrawing ()
EndProcedure
;


; 


; Epb