[Résolu] Rotation d'image avec RotateCoordinates

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
GallyHC
Messages : 1708
Inscription : lun. 17/déc./2007 12:44

[Résolu] Rotation d'image avec RotateCoordinates

Message par GallyHC »

Bonjour,

J’ai un peu de mal pour bien comprendre la lib « Vector ». J’aimerais juste faire une méthode de rotation d’image (avec ajustement de la taille de celle-ci).

Voilà ma routine actuelle :

Code : Tout sélectionner

Procedure.s ImageRotation(filename.s, filedest.s, angle.f)
  
  CompilerIf #PB_Compiler_Debugger
    Define.i itimer   = ElapsedMilliseconds()
  CompilerEndIf  
  
  Protected.s fileresult
  Protected.i entrysize = FileSize(filename)
  If entrysize <> -1 And entrysize <> -2 And entrysize > 0
    Protected.i image = LoadImage(#PB_Any, filename)
    If IsImage(image)
      Protected.d CosA, SinA
      Protected.i height, width, temp, img_w, img_h
  
      CosA    = Cos (Radian  (angle))
      SinA    = Sin (Radian  (angle))
      width   = ImageWidth   (Image )
      height  = ImageHeight  (Image )
      img_w   = Abs(height  * SinA) + Abs(width * CosA)
      img_h   = Abs(height  * CosA) + Abs(width * SinA)
  
      temp = CreateImage(#PB_Any, img_w, img_h, 24, #White)
      If IsImage(temp)
        If StartVectorDrawing(ImageVectorOutput(temp))
          VectorSourceColor(RGBA(255,255,255,255))
          TranslateCoordinates(0, 0)
          MovePathCursor    (0, 0, #PB_Path_Default)
          MovePathCursor    (width * 0.5, height * 0.5, #PB_Path_Default)
          RotateCoordinates (width * 0.5, height * 0.5, angle, #PB_Coordinate_User)
          MovePathCursor    (-width, -height, #PB_Path_Default)
          DrawVectorImage   (ImageID(image), 255, width, height)
          StopVectorDrawing ()
        EndIf
        If SaveImage(temp, filedest, #PB_ImagePlugin_JPEG, QualityCompress, 24)
          fileresult = filedest
        EndIf
        FreeImage(temp)
      EndIf
      FreeImage(image)
    EndIf
  EndIf
  
  CompilerIf #PB_Compiler_Debugger
    Debug "ROTATION : " + FormatDate("%hh:%ii:%ss", (ElapsedMilliseconds() - itimer) / 1000)
    Debug "FILENAME : " + filename
  CompilerEndIf
  ProcedureReturn fileresult

EndProcedure

merci d'avance, si quelqu'un peu m'aider.

Cordialement,
Dernière modification par GallyHC le jeu. 20/juin/2024 11:58, modifié 1 fois.
Configuration : Tower: Windows 10 (Processeur: i7 "x64") (Mémoire: 16Go) (GeForce GTX 760 - 2Go) - PureBasic 5.72 (x86 et x64)
Mesa
Messages : 1126
Inscription : mer. 14/sept./2011 16:59

Re: Rotation d'image avec RotateCoordinates

Message par Mesa »

Code : Tout sélectionner

Procedure.i ImageRotation(filename.s, filedest.s, angle.f)
	;- ATTENTION : AVEC #PB_Image_Transparent, IL Y A UN BUG D'AFFICHAGE SOUS WINDOWS, UNE LIGNE DE PIXEL A DROITE ET UNE EN BAS NE SONT PAS REMPLIES
	
	CompilerIf #PB_Compiler_Debugger
		Define.i itimer   = ElapsedMilliseconds()
	CompilerEndIf  
	
	Protected.s fileresult
	Protected.i entrysize = FileSize(filename)
	If entrysize <> -1 And entrysize <> -2 And entrysize > 0
		Protected.i image = LoadImage(#PB_Any, filename)
		If IsImage(image)
			Protected.d CosA, SinA
			Protected.d height, width, temp, img_w, img_h, cx, cy, w2, h2
			
			CosA    = Cos (Radian  (angle))
			SinA    = Sin (Radian  (angle))
			width   = ImageWidth   (Image )
			height  = ImageHeight  (Image )
			img_w   = Abs(height  * SinA) + Abs(width * CosA)
			img_h   = Abs(height  * CosA) + Abs(width * SinA)
			cx=img_w/2:cy=img_h/2
			w2=width/2:h2=height/2
			; 			temp = CreateImage(#PB_Any, img_w, img_h, 32, #PB_Image_Transparent);Bug d'affichage
			temp = CreateImage(#PB_Any, img_w, img_h, 24, RGB(255,255,255));
			If IsImage(temp)
				If StartVectorDrawing(ImageVectorOutput(temp))
					
					RotateCoordinates (cx, cy, angle)
					MovePathCursor    (cx-w2,cy-h2)
					DrawVectorImage   (ImageID(image))
					StopVectorDrawing ()
				EndIf
				;         If SaveImage(temp, filedest, #PB_ImagePlugin_JPEG, QualityCompress, 24)
				;           fileresult = filedest
				;         EndIf
				;;         FreeImage(temp)
			EndIf
			FreeImage(image)
		EndIf
	EndIf
	
	CompilerIf #PB_Compiler_Debugger
		;     Debug "ROTATION : " + FormatDate("%hh:%ii:%ss", (ElapsedMilliseconds() - itimer) / 1000)
		;     Debug "FILENAME : " + filename
	CompilerEndIf
	ProcedureReturn temp;fileresult
	
EndProcedure


;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Enumeration FormWindow
	#Window_0
EndEnumeration

Enumeration FormGadget
	#Canvas_1
	#Canvas_2
	#ScrollArea_0
	#ScrollArea_1
	#Splitter_0
EndEnumeration

Declare ResizeGadgetsWindow_0()


Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
	OpenWindow(#Window_0, x, y, width, height, "", #PB_Window_SystemMenu)  
	ScrollAreaGadget(#ScrollArea_0, 0, 0, 600, 400, 1000, 1000, 10)
	CanvasGadget(#Canvas_1, 0, 0, 1000, 1000)
	CloseGadgetList()
	ScrollAreaGadget(#ScrollArea_1, 159, 0, 600, 400, 1000, 1000, 10)
	CanvasGadget(#Canvas_2, 0, 0, 1000, 1000)
	CloseGadgetList()
	SplitterGadget(#Splitter_0, 0, 0, 600, 400, #ScrollArea_0, #ScrollArea_1, #PB_Splitter_Vertical)
	SetGadgetState(#Splitter_0, 200)
EndProcedure

Procedure ResizeGadgetsWindow_0()
	Protected FormWindowWidth, FormWindowHeight
	FormWindowWidth = WindowWidth(#Window_0)
	FormWindowHeight = WindowHeight(#Window_0)
	ResizeGadget(#Splitter_0, 0, 0, FormWindowWidth - 6, FormWindowHeight -10)
	;   ResizeGadget(#Canvas_1, 0, 0, GadgetWidth(#ScrollArea_0) - 0, GadgetHeight(#ScrollArea_0) - 0)
	;   ResizeGadget(#Canvas_2, 0, 0, GadgetWidth(#ScrollArea_1) - 0, GadgetHeight(#ScrollArea_1) - 0)
	;   ResizeGadget(#ScrollArea_0, 0, 0, FormWindowWidth - 0, FormWindowHeight - 0)
	;   ResizeGadget(#ScrollArea_1, 0, 0, FormWindowWidth - 0, FormWindowHeight - 0)
	
EndProcedure

Procedure Window_0_Events(event)
	Select event
		Case #PB_Event_SizeWindow
			ResizeGadgetsWindow_0()
		Case #PB_Event_CloseWindow
			ProcedureReturn #False
			
		Case #PB_Event_Menu
			Select EventMenu()
			EndSelect
			
		Case #PB_Event_Gadget
			Select EventGadget()
			EndSelect
	EndSelect
	ProcedureReturn #True
EndProcedure


;- Point d'entrée
OpenWindow_0(0,0,600,400)
SetWindowState(0,#PB_Window_Maximize)

File$="f:\1c.bmp"
If FileSize(File$) = -1
	Debug "Fichier inexistant."
	DebuggerError("Fichier inexistant.")
EndIf


;Originale
imgg=LoadImage(#PB_Any,File$)
If StartVectorDrawing(CanvasVectorOutput(#Canvas_1))
	;       ResetCoordinates()
	MovePathCursor(10, 10)
	DrawVectorImage(ImageID(imgg)) 
	
	StopVectorDrawing()
EndIf

;Rotation
img=ImageRotation(File$, "", 120.0) 

If StartVectorDrawing(CanvasVectorOutput(#Canvas_2))
	;       ResetCoordinates()
	
	MovePathCursor(250, 250)
	DrawVectorImage(ImageID(img))
	;       
	StopVectorDrawing()
EndIf

;- Boucle
Repeat
	Event = WaitWindowEvent()
	
	Select Event
			
		Case #PB_Event_SizeWindow
			ResizeGadgetsWindow_0()
			
			
	EndSelect
Until Event = #PB_Event_CloseWindow

Avatar de l’utilisateur
GallyHC
Messages : 1708
Inscription : lun. 17/déc./2007 12:44

Re: Rotation d'image avec RotateCoordinates

Message par GallyHC »

Bonjour,

Exact, bon va falloir que je comprenne maintenant le pourquoi.


Merci et cordialement,
Configuration : Tower: Windows 10 (Processeur: i7 "x64") (Mémoire: 16Go) (GeForce GTX 760 - 2Go) - PureBasic 5.72 (x86 et x64)
Mesa
Messages : 1126
Inscription : mer. 14/sept./2011 16:59

Re: Rotation d'image avec RotateCoordinates

Message par Mesa »

Pour archive:
Image

M.
Répondre