Differenze tra le versioni di "Ruotare un'immagine mediante le funzioni esterne del API di ImageMagick"

Da Gambas-it.org - Wikipedia.
 
(2 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
Il sistema ''ImageMagick'' consente di creare, modificare e convertire immagini bitmap. Può altresì leggere e scrivere immagini di oltre 200 formati.
 
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).
+
Per poter fruire in Gambas delle risorse, è necessario avere installata nel sistema e richiamare la seguente libreria condivisa: "''libMagickWand-6.Q16.so.6.0.0'' ".
 
 
  
 
Mostriamo di seguito un semplice esempio pratico per ruotare un'immagine:
 
Mostriamo di seguito un semplice esempio pratico per ruotare un'immagine:
  Library "libMagickWand-6.Q16:2.0.0"
+
  Library "libMagickWand-6.Q16:6.0.0"
 +
 +
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
 
  Private Enum MagickFalse = 0, MagickTrue
Riga 15: Riga 28:
 
  <FONT Color=gray>' ''MagickWand *NewMagickWand(void)''
 
  <FONT Color=gray>' ''MagickWand *NewMagickWand(void)''
 
  ' ''Returns a wand required for all other methods in the API.''</font>
 
  ' ''Returns a wand required for all other methods in the API.''</font>
  Private Extern NewMagickWand() As Pointer
+
  Private Extern NewMagickWand() As MagickWand
 
    
 
    
 
  <FONT Color=gray>' ''MagickBooleanType MagickReadImage(MagickWand *wand,const char *filename)''
 
  <FONT Color=gray>' ''MagickBooleanType MagickReadImage(MagickWand *wand,const char *filename)''
 
  ' ''Reads an image or image sequence.''</font>
 
  ' ''Reads an image or image sequence.''</font>
  Private Extern MagickReadImage(wand As Pointer, filename As String) As Boolean
+
  Private Extern MagickReadImage(wand As MagickWand, filename As String) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''PixelWand *NewPixelWand(void)''
 
  <FONT Color=gray>' ''PixelWand *NewPixelWand(void)''
 
  ' ''Returns a new pixel wand.''</font>
 
  ' ''Returns a new pixel wand.''</font>
  Private Extern NewPixelWand() As PixelWand
+
  Private Extern NewPixelWand() As Pointer
 
   
 
   
 
  <FONT Color=gray>' ''void PixelSetRed(PixelWand *wand, const double red)''
 
  <FONT Color=gray>' ''void PixelSetRed(PixelWand *wand, const double red)''
 
  ' ''Sets the normalized red color of the pixel wand.''</font>
 
  ' ''Sets the normalized red color of the pixel wand.''</font>
  Private Extern PixelSetRed(Pwand As PixelWand, red As Float)
+
  Private Extern PixelSetRed(Pwand As Pointer, red As Float)
 
   
 
   
 
  <FONT Color=gray>' ''void PixelSetGreen(PixelWand *wand, const double green)''
 
  <FONT Color=gray>' ''void PixelSetGreen(PixelWand *wand, const double green)''
 
  ' ''Sets the normalized green color of the pixel wand.''</font>
 
  ' ''Sets the normalized green color of the pixel wand.''</font>
  Private Extern PixelSetGreen(Pwand As PixelWand, green As Float)
+
  Private Extern PixelSetGreen(Pwand As Pointer, green As Float)
 
   
 
   
 
  <FONT Color=gray>' ''void PixelSetBlue(PixelWand *wand, const double blue)''
 
  <FONT Color=gray>' ''void PixelSetBlue(PixelWand *wand, const double blue)''
 
  ' ''Sets the normalized blue color of the pixel wand.''</font>
 
  ' ''Sets the normalized blue color of the pixel wand.''</font>
  Private Extern PixelSetBlue(Pwand As PixelWand, blue As Float)
+
  Private Extern PixelSetBlue(Pwand As Pointer, blue As Float)
 
   
 
   
 
  <FONT Color=gray>' ''void PixelSetAlpha(PixelWand *wand, const double alpha)''
 
  <FONT Color=gray>' ''void PixelSetAlpha(PixelWand *wand, const double alpha)''
 
  ' ''Sets the normalized alpha color of the pixel wand.''</font>
 
  ' ''Sets the normalized alpha color of the pixel wand.''</font>
  Private Extern PixelSetAlpha(Pwand As PixelWand, alpha As Float)
+
  Private Extern PixelSetAlpha(Pwand As Pointer, alpha As Float)
 
   
 
   
 
  <FONT Color=gray>' ''MagickBooleanType MagickRotateImage(MagickWand *wand, const PixelWand *background,const double degrees)''
 
  <FONT Color=gray>' ''MagickBooleanType MagickRotateImage(MagickWand *wand, const PixelWand *background,const double degrees)''
 
  ' ''Rotates an image the specified number of degrees.''</font>
 
  ' ''Rotates an image the specified number of degrees.''</font>
  Private Extern MagickRotateImage(wand As Pointer, background As PixelWand, degrees As Float) As Boolean
+
  Private Extern MagickRotateImage(wand As MagickWand, background As Pointer, degrees As Float) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''MagickBooleanType MagickWriteImages(MagickWand *wand, const char *filename,const MagickBooleanType adjoin)''
 
  <FONT Color=gray>' ''MagickBooleanType MagickWriteImages(MagickWand *wand, const char *filename,const MagickBooleanType adjoin)''
 
  ' ''Writes an image or image sequence.''</font>
 
  ' ''Writes an image or image sequence.''</font>
  Private Extern MagickWriteImages(wand As Pointer, filename As String, adjoin As Boolean) As Boolean
+
  Private Extern MagickWriteImages(wand As MagickWand, filename As String, adjoin As Boolean) As Integer
 
   
 
   
 
  <FONT Color=gray>' ''PixelWand *DestroyPixelWand(PixelWand *wand)''
 
  <FONT Color=gray>' ''PixelWand *DestroyPixelWand(PixelWand *wand)''
 
  ' ''Deallocates resources associated with a PixelWand.''</font>
 
  ' ''Deallocates resources associated with a PixelWand.''</font>
  Private Extern DestroyPixelWand(Pwand As PixelWand) As PixelWand
+
  Private Extern DestroyPixelWand(Pwand As Pointer) As Pointer
 
   
 
   
 
  <FONT Color=gray>' ''MagickWand *DestroyMagickWand(MagickWand *wand)''
 
  <FONT Color=gray>' ''MagickWand *DestroyMagickWand(MagickWand *wand)''
 
  ' ''Deallocates memory associated with an MagickWand.''</font>
 
  ' ''Deallocates memory associated with an MagickWand.''</font>
  Private Extern DestroyMagickWand(wand As Pointer) As Pointer
+
  Private Extern DestroyMagickWand(wand As MagickWand) As MagickWand
 
   
 
   
 
  <FONT Color=gray>' ''void MagickWandTerminus(void)''
 
  <FONT Color=gray>' ''void MagickWandTerminus(void)''
Riga 64: Riga 77:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
    
 
    
  Dim bo As Boolean
+
  Dim bo As Integer
  Dim magickwand As Pointer
+
  Dim mwand As MagickWand
  Dim fileimmagine, nuovofile As String
+
  Dim fileimmagine, nuovofile As String
  Dim pw As PixelWand
+
  Dim pw As Pointer
 
+
 
   fileimmagine = "<FONT Color=gray>''/percorso/del/file/immagine''</font>"
 
   fileimmagine = "<FONT Color=gray>''/percorso/del/file/immagine''</font>"
 
   nuovofile = "<FONT Color=gray>''/percorso/del/nuovo/file/dell'immagine/ruotata''</font>"
 
   nuovofile = "<FONT Color=gray>''/percorso/del/nuovo/file/dell'immagine/ruotata''</font>"
Riga 77: Riga 90:
 
   pw = ColorFondo()
 
   pw = ColorFondo()
 
    
 
    
   magickwand = NewMagickWand()
+
   mwand = NewMagickWand()
 
    
 
    
   bo = MagickReadImage(magickwand, fileimmagine)
+
   bo = MagickReadImage(mwand, fileimmagine)
 
   If bo = MagickFalse Then
 
   If bo = MagickFalse Then
     Termina(magickwand, pw)
+
     Termina(mwand, pw)
 
     Error.Raise("Impossibile caricare l'immagine !")
 
     Error.Raise("Impossibile caricare l'immagine !")
 
   Endif
 
   Endif
 
    
 
    
 
  <FONT Color=gray>' ''Ruota l'immegine di 135,00° a destra:''</font>
 
  <FONT Color=gray>' ''Ruota l'immegine di 135,00° a destra:''</font>
   MagickRotateImage(magickwand, pw, 135.00)
+
   MagickRotateImage(mwand, pw, 135.00)
 
    
 
    
   bo = MagickWriteImages(magickwand, nuovofile, MagickTrue)
+
   bo = MagickWriteImages(mwand, nuovofile, MagickTrue)
 
   If bo = MagickFalse Then
 
   If bo = MagickFalse Then
     Termina(magickwand, pw)
+
     Termina(mwand, pw)
 
     Error.Raise("Impossibile creare il nuovo file dell'immagine ruotata !")
 
     Error.Raise("Impossibile creare il nuovo file dell'immagine ruotata !")
 
   Endif
 
   Endif
 
    
 
    
   Termina(magickwand, pw)
+
   Termina(mwand, pw)
 
    
 
    
 
  '''End'''
 
  '''End'''
 
   
 
   
+
  '''Private''' Function ColorFondo() As Pointer
  '''Private''' Function ColorFondo() As PixelWand
 
 
    
 
    
   Dim pxw As PixelWand
+
   Dim pxw As Pointer
 
    
 
    
 
   pxw = NewPixelWand()
 
   pxw = NewPixelWand()
Riga 114: Riga 126:
 
  '''End'''
 
  '''End'''
 
   
 
   
+
  '''Private''' Procedure Termina(mw As MagickWand, pixw As Pointer)    <FONT Color=gray>' ''Libera la memoria e termina il programma''</font>
  '''Private''' Procedure Termina(mw As Pointer, pixw As PixelWand)    <FONT Color=gray>' ''Libera la memoria e termina il programma''</font>
 
 
    
 
    
 
   DestroyPixelWand(pixw)
 
   DestroyPixelWand(pixw)
Riga 123: Riga 134:
 
    
 
    
 
  '''End'''
 
  '''End'''
 
  
  
  
 
=Riferimenti=
 
=Riferimenti=
 +
* https://imagemagick.org/index.php
 
* http://imagemagick.org/api/magick-wand.php
 
* http://imagemagick.org/api/magick-wand.php
* http://www.ummon.org/Linux/index/ImageMagick_R.html
 

Versione attuale delle 02:25, 20 nov 2022

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-6.Q16.so.6.0.0 ".

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

Library "libMagickWand-6.Q16:6.0.0"

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


Riferimenti