Fast Rotate /Mirror Image Effect (Win Only :( )

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
Thyphoon
Messages : 2697
Inscription : mer. 25/août/2004 6:31
Localisation : Eragny
Contact :

Fast Rotate /Mirror Image Effect (Win Only :( )

Message par Thyphoon »

Voici une fonction que je viens de faire pour des besoins bien précis qui permet de faire des rotations et des effets miroir rapide sur une image. Peut être que ça intéressera certain d'entre vous.
Si quelqu'un sait comment rendre compatible linux/macOs cette fonction je suis preneur !

Edit 2019-02-04 Mise à jour a cause de quelques bugs
Edit 2020-05-07 Mise à jour pour supporter le canal Alpha si il y a :P

Code : Tout sélectionner

Enumeration  1
   #Effect_Normal
   #Effect_Mirror_horizontal
   #Effect_Rotate_180
   #Effect_Mirror_vertical
   #Effect_Mirror_horizontal_Rotate_270_CW
   #Effect_Rotate_90_CW
   #Effect_Mirror_Horizontal_Rotate_90_CW
   #Effect_Rotate_270_CW
 EndEnumeration
 
Procedure.i RotateImage(Image.i,Effect.l)

  Protected Height.l = ImageHeight(Image)
  Protected Width.l = ImageWidth(Image) 
  Protected sizeMax.l
  Protected TmpImage.i
  If Height > Width
    sizeMax=Height
    Else
    sizeMax=Width
  EndIf
  TmpImage = CreateImage(#PB_Any,sizeMax,sizeMax,ImageDepth(Image))
  Dim p.point(2)
  Protected GrabWidth.l,GrabHeight.l
  Select Effect.l
    Case 0
      FreeImage(TmpImage)
      ProcedureReturn Image
    Case 1 ; Horizontal (normal)
      FreeImage(TmpImage)
      ProcedureReturn Image
    Case 2 ; Mirror horizontal
      p(0)\x=0
      p(0)\y=Height
      p(1)\x=Width
      p(1)\y=Height
      p(2)\x=0 
      p(2)\y=0
      GrabWidth=Width
      GrabHeight=Height
    Case 3 ; Rotate 180
      p(0)\x=Width
      p(0)\y=Height
      p(1)\x=0
      p(1)\y=Height
      p(2)\x=Width 
      p(2)\y=0
      GrabWidth=Width
      GrabHeight=Height
    Case 4 ; Mirror vertical
      p(0)\x=Width
      p(0)\y=0
      p(1)\x=0
      p(1)\y=0
      p(2)\x=Width 
      p(2)\y=Height
      GrabWidth=Width
      GrabHeight=Height
    Case 5 ; Mirror horizontal And rotate 270 CW
      p(0)\x=0
      p(0)\y=0
      p(1)\x=0
      p(1)\y=Width
      p(2)\x=Height
      p(2)\y=0
      GrabWidth=Height
      GrabHeight=Width   
    Case 6 ; Rotate 90 CW
      p(0)\x=Height
      p(0)\y=0
      p(1)\x=Height
      p(1)\y=Width
      p(2)\x=0
      p(2)\y=0
      GrabWidth=Height
      GrabHeight=Width
    Case 7 ; Mirror horizontal And rotate 90 CW
      p(0)\x=0
      p(0)\y=0
      p(1)\x=0
      p(1)\y=Width
      p(2)\x=Height
      p(2)\y=0
      GrabWidth=Height
      GrabHeight=Width
    Case 8 ; Rotate 270 CW
      p(0)\x=0
      p(0)\y=Width
      p(1)\x=0
      p(1)\y=0
      p(2)\x=Height 
      p(2)\y=Width
      GrabWidth=Height 
      GrabHeight=Width
  EndSelect
 
  Protected Dc.i
  Dc = StartDrawing(ImageOutput(TmpImage))
       DrawingMode(#PB_2DDrawing_AlphaChannel)
       Box(0, 0, sizeMax, sizeMax, RGBA(0,0,255,0))
       DrawingMode(#PB_2DDrawing_AlphaBlend)
       DrawAlphaImage(ImageID(Image),0,0)
       PlgBlt_(Dc,p(),Dc,0,0,Width,Height,0,0,0)
       StopDrawing()
       FreeImage(Image)
       Protected NewImage.i
  NewImage = GrabImage(TmpImage,#PB_Any,0,0,GrabWidth,GrabHeight)
  FreeImage(TmpImage)
  ProcedureReturn NewImage
EndProcedure
Dernière modification par Thyphoon le jeu. 07/mai/2020 12:34, modifié 4 fois.
Avatar de l’utilisateur
Ar-S
Messages : 9477
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Fast Rotate /Mirror Image Effect

Message par Ar-S »

Merci, ce sera surement bien plus rapide qu'avec la lib vectorDrawing que j'utilisais pour faire mes rotations de photos.
~~~~Règles du forum ~~~~
⋅.˳˳.⋅ॱ˙˙ॱ⋅.˳Ar-S ˳.⋅ॱ˙˙ॱ⋅.˳˳.⋅
W11x64 PB 6.x
Section HORS SUJET : ICI
LDV MULTIMEDIA : Dépannage informatique & mes Logiciels PB
UPLOAD D'IMAGES : Uploader des images de vos logiciels
Avatar de l’utilisateur
Thyphoon
Messages : 2697
Inscription : mer. 25/août/2004 6:31
Localisation : Eragny
Contact :

Re: Fast Rotate /Mirror Image Effect

Message par Thyphoon »

Ar-S a écrit :Merci, ce sera surement bien plus rapide qu'avec la lib vectorDrawing que j'utilisais pour faire mes rotations de photos.
Oui :P parcontre j'ai fait quelques erreurs ... je met a jour durant le week-end des que j'ai 5 minutes
Avatar de l’utilisateur
Thyphoon
Messages : 2697
Inscription : mer. 25/août/2004 6:31
Localisation : Eragny
Contact :

Re: Fast Rotate /Mirror Image Effect

Message par Thyphoon »

Le code a été mis à jour ...
Avatar de l’utilisateur
Thyphoon
Messages : 2697
Inscription : mer. 25/août/2004 6:31
Localisation : Eragny
Contact :

Re: Fast Rotate /Mirror Image Effect

Message par Thyphoon »

code mis à a jour pour supporter les images avec Canal Alpha :roll:
Ollivier
Messages : 4190
Inscription : ven. 29/juin/2007 17:50
Localisation : Encore ?
Contact :

Re: Fast Rotate /Mirror Image Effect

Message par Ollivier »

Salut Thyphoon, il me semble effectivement que c'est très rapide. Merci pour l'astuce.

Petit rajout d'info : pour Windows seulement.
Avatar de l’utilisateur
Thyphoon
Messages : 2697
Inscription : mer. 25/août/2004 6:31
Localisation : Eragny
Contact :

Re: Fast Rotate /Mirror Image Effect

Message par Thyphoon »

Ollivier a écrit :Salut Thyphoon, il me semble effectivement que c'est très rapide. Merci pour l'astuce.

Petit rajout d'info : pour Windows seulement.
Effectivement je vais rajouter...Mais si quelqu'un connait des API Linux/MACOS qui permet la même chose ... je suis preneur :P
Avatar de l’utilisateur
Ar-S
Messages : 9477
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Fast Rotate /Mirror Image Effect (Win Only :( )

Message par Ar-S »

La fonction d'Ollivier pour un flipsprite horizontale.

Code : Tout sélectionner

Procedure SpriteHorizontalFlip(N.I)
; Ollivier
Structure DWORD
  L.L[1 << 14]
EndStructure

Define.I W = SpriteWidth(N), X, Y
If StartDrawing(SpriteOutput(N) )
  Define *A.DWORD = DrawingBuffer()
  For Y = 0 To (SpriteHeight(N) - 1)
   For X = 0 To (W >> 1 - 1)
    Swap *A\L[X], *A\L[W - X - 1]
   Next
   *A + DrawingBufferPitch()
  Next
  StopDrawing()
EndIf
EndProcedure
~~~~Règles du forum ~~~~
⋅.˳˳.⋅ॱ˙˙ॱ⋅.˳Ar-S ˳.⋅ॱ˙˙ॱ⋅.˳˳.⋅
W11x64 PB 6.x
Section HORS SUJET : ICI
LDV MULTIMEDIA : Dépannage informatique & mes Logiciels PB
UPLOAD D'IMAGES : Uploader des images de vos logiciels
Avatar de l’utilisateur
Thyphoon
Messages : 2697
Inscription : mer. 25/août/2004 6:31
Localisation : Eragny
Contact :

Re: Fast Rotate /Mirror Image Effect (Win Only :( )

Message par Thyphoon »

Ar-S a écrit :La fonction d'Ollivier pour un flipsprite horizontale.

Code : Tout sélectionner

Procedure SpriteHorizontalFlip(N.I)
; Ollivier
Structure DWORD
  L.L[1 << 14]
EndStructure

Define.I W = SpriteWidth(N), X, Y
If StartDrawing(SpriteOutput(N) )
  Define *A.DWORD = DrawingBuffer()
  For Y = 0 To (SpriteHeight(N) - 1)
   For X = 0 To (W >> 1 - 1)
    Swap *A\L[X], *A\L[W - X - 1]
   Next
   *A + DrawingBufferPitch()
  Next
  StopDrawing()
EndIf
EndProcedure
interessant ... je vais voir si on peut le modifier pour faire aussi les rotations
Merci pour le partage
Répondre