Ruotare un'immagine mediante le funzioni esterne del API di ImageMagick

Da Gambas-it.org - Wikipedia.
Versione del 7 giu 2017 alle 16:02 di Vuott (Discussione | contributi) (Creata pagina con "Il sistema ''ImageMagick'' consente di creare, modificare e convertire immagini bitmap. Può altresì leggere e scrivere immagini di oltre 200 formati. Per poter fruire in Ga...")

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

Il sistema ImageMagick consente di creare, modificare e convertire immagini bitmap. Può altresì leggere e scrivere immagini di oltre 200 formati.

Per poter fruire in Gambas delle risorse, è necessario avere installata nel sistema e richiamare la seguente libreria dinamica condivisa: "libMagickWand-6.Q16.so.2.0.0" (o altra versione).


Mostriamo di seguito un semplice esempio pratico per ruotare un'immagine:

Library "libMagickWand-6.Q16:2.0.0"

Private Enum MagickFalse = 0, MagickTrue

' void MagickWandGenesis(void)
' Initializes the MagickWand environment.
Private Extern MagickWandGenesis()
 
' MagickWand *NewMagickWand(void)
' Returns a wand required for all other methods in the API.
Private Extern NewMagickWand() As Pointer
 
' MagickBooleanType MagickReadImage(MagickWand *wand,const char *filename)
' Reads an image or image sequence.
Private Extern MagickReadImage(wand As Pointer, filename As String) As Boolean

' PixelWand *NewPixelWand(void)
' Returns a new pixel wand.
Private Extern NewPixelWand() As PixelWand

' void PixelSetRed(PixelWand *wand, const double red)
' Sets the normalized red color of the pixel wand.
Private Extern PixelSetRed(Pwand As PixelWand, red As Float)

' void PixelSetGreen(PixelWand *wand, const double green)
' Sets the normalized green color of the pixel wand.
Private Extern PixelSetGreen(Pwand As PixelWand, green As Float)

' void PixelSetBlue(PixelWand *wand, const double blue)
' Sets the normalized blue color of the pixel wand.
Private Extern PixelSetBlue(Pwand As PixelWand, blue As Float)

' void PixelSetAlpha(PixelWand *wand, const double alpha)
' Sets the normalized alpha color of the pixel wand.
Private Extern PixelSetAlpha(Pwand As PixelWand, alpha As Float)
' MagickBooleanType MagickRotateImage(MagickWand *wand, const PixelWand *background,const double degrees)
' Rotates an image the specified number of degrees.
Private Extern MagickRotateImage(wand As Pointer, background As PixelWand, degrees As Float) As Boolean

' MagickBooleanType MagickWriteImages(MagickWand *wand, const char *filename,const MagickBooleanType adjoin)
' Writes an image or image sequence.
Private Extern MagickWriteImages(wand As Pointer, filename As String, adjoin As Boolean) As Boolean

' PixelWand *DestroyPixelWand(PixelWand *wand)
' Deallocates resources associated with a PixelWand.
Private Extern DestroyPixelWand(Pwand As PixelWand) As PixelWand

' MagickWand *DestroyMagickWand(MagickWand *wand)
' Deallocates memory associated with an MagickWand.
Private Extern DestroyMagickWand(wand As Pointer) As Pointer

' void MagickWandTerminus(void)
' Terminates the MagickWand environment.
Private Extern MagickWandTerminus()


Public Sub Main()
 
 Dim bo As Boolean
 Dim magickwand As Pointer
 Dim fileimmagine, nuovofile As String
 Dim pw As PixelWand
  
  fileimmagine = "/percorso/del/file/immagine"
  nuovofile = "/percorso/del/nuovo/file/dell'immagine/ruotata"
  
  MagickWandGenesis()
  
' I triangoli lasciati vuoti dalla rotazione dell'immagine sono riempiti da un colore di fondo:
  pw = ColorFondo()
  
  magickwand = NewMagickWand()
  
  bo = MagickReadImage(magickwand, fileimmagine)
  If bo = MagickFalse Then
    Termina(magickwand, pw)
    Error.Raise("Impossibile caricare l'immagine !")
  Endif
  
' Ruota l'immegine di 135,00° a destra:
  MagickRotateImage(magickwand, pw, 135.00)
  
  bo = MagickWriteImages(magickwand, nuovofile, MagickTrue)
  If bo = MagickFalse Then
    Termina(magickwand, pw)
    Error.Raise("Impossibile creare il nuovo file dell'immagine ruotata !")
  Endif
  
  Termina(magickwand, pw)
  
End


Private Function ColorFondo() As PixelWand
 
  Dim pxw As PixelWand
  
  pxw = NewPixelWand()
  PixelSetRed(pxw, 255.0)
  PixelSetGreen(pxw, 255.0)
  PixelSetBlue(pxw, 255.0)
' Imposta il canale Alfa a zero per la trasparenza:
  PixelSetAlpha(pxw, 0.0)
  
  Return pxw
  
End


Private Procedure Termina(mw As Pointer, pixw As PixelWand)    ' Libera la memoria e termina il programma
  
  DestroyPixelWand(pixw)
  DestroyMagickWand(mw)
  Wait 0.01
  MagickWandTerminus()
  
End



Riferimenti