Ruotare un'immagine mediante le funzioni esterne del API di ImageMagick
Da Gambas-it.org - Wikipedia.
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 condivisa: "libMagickWand-7.Q16HDRI.so.10.0.2 ".
Mostriamo di seguito un semplice esempio pratico per ruotare un'immagine:
Library "libMagickWand-7.Q16HDRI:10.0.2" Private Const MagickPathExtent As Integer = 4096 Public Struct MagickWand id As Long name[MagickPathExtent] As Byte images As Pointer image_info As Pointer exception As Pointer insert_before As Integer image_pending As Integer debug_ As Integer signature As Long End Struct 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 MagickWand ' MagickBooleanType MagickReadImage(MagickWand *wand,const char *filename) ' Reads an image or image sequence. Private Extern MagickReadImage(wand As MagickWand, filename As String) As Integer ' PixelWand *NewPixelWand(void) ' Returns a new pixel wand. Private Extern NewPixelWand() As Pointer ' void PixelSetRed(PixelWand *wand, const double red) ' Sets the normalized red color of the pixel wand. Private Extern PixelSetRed(Pwand As Pointer, red As Float) ' void PixelSetGreen(PixelWand *wand, const double green) ' Sets the normalized green color of the pixel wand. Private Extern PixelSetGreen(Pwand As Pointer, green As Float) ' void PixelSetBlue(PixelWand *wand, const double blue) ' Sets the normalized blue color of the pixel wand. Private Extern PixelSetBlue(Pwand As Pointer, blue As Float) ' void PixelSetAlpha(PixelWand *wand, const double alpha) ' Sets the normalized alpha color of the pixel wand. Private Extern PixelSetAlpha(Pwand As Pointer, 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 MagickWand, background As Pointer, degrees As Float) As Integer ' MagickBooleanType MagickWriteImages(MagickWand *wand, const char *filename,const MagickBooleanType adjoin) ' Writes an image or image sequence. Private Extern MagickWriteImages(wand As MagickWand, filename As String, adjoin As Boolean) As Integer ' PixelWand *DestroyPixelWand(PixelWand *wand) ' Deallocates resources associated with a PixelWand. Private Extern DestroyPixelWand(Pwand As Pointer) As Pointer ' MagickWand *DestroyMagickWand(MagickWand *wand) ' Deallocates memory associated with an MagickWand. Private Extern DestroyMagickWand(wand As MagickWand) As MagickWand ' void MagickWandTerminus(void) ' Terminates the MagickWand environment. Private Extern MagickWandTerminus() Public Sub Main() Dim bo As Integer Dim mwand As MagickWand Dim fileimmagine, nuovofile As String Dim pw As Pointer 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() mwand = NewMagickWand() bo = MagickReadImage(mwand, fileimmagine) If bo = MagickFalse Then Termina(mwand, pw) Error.Raise("Impossibile caricare l'immagine !") Endif ' Ruota l'immegine di 135,00° a destra: MagickRotateImage(mwand, pw, 135.00) bo = MagickWriteImages(mwand, nuovofile, MagickTrue) If bo = MagickFalse Then Termina(mwand, pw) Error.Raise("Impossibile creare il nuovo file dell'immagine ruotata !") Endif Termina(mwand, pw) End Private Function ColorFondo() As Pointer Dim pxw As Pointer 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 MagickWand, pixw As Pointer) ' Libera la memoria e termina il programma DestroyPixelWand(pixw) DestroyMagickWand(mw) Wait 0.01 MagickWandTerminus() End