Spostare il contenuto all'interno di 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-6.Q16.so.6.0.0 ".

Le risorse di questa libreria ci consentono - fra l'altro - di spostare il contenuto all'interno di un'immagine lungo uno o entrambi gli assi di spostamento x, y. Così, se l'immagine presenta, come disegno al suo interno, la lettera "A":

[ A ]

essa potrà essere spostata all'interno del campo dell'immagine medesima lungo il suo asse di spostamento x, ad esempio, così:

[  A]

Mostriamo un esempio pratico:

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

' MagickBooleanType MagickRollImage(MagickWand *wand,const ssize_t x, const size_t y)
' Offsets an image as defined by x_offset and y_offset.
Private Extern MagickRollImage(wand As MagickWand, x As Long, y As Long) 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

' 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  

 fileimmagine = "/percorso/del/file/immagine"
 nuovofile = "/percorso/del/file/immagine/ridimensionata"
 
 MagickWandGenesis()
  
 mwand = NewMagickWand()
 
 bo = MagickReadImage(mwand, fileimmagine)
 If bo = MagickFalse Then
   Error.Raise("Impossibile caricare il file immagine !")
   Chiude(mwand)
 Endif
 
' Sposta tutto il disegno all'interno dell'immagine lungo il solo asse di spostamento x:
 MagickRollImage(mwand, 120, 0)

 bo = MagickWriteImages(mwand, nuovofile, MagickTrue)
 If bo = MagickFalse Then
   Error.Raise("Impossibile creare il nuovo file dell'immagine ridimensionata !")
   Chiude(mwand)
 Endif

 Chiude(mwand)
  
End

Private Procedure Chiude(mw As MagickWand)
 
' Libera la memoria e chiude la libreria "ImageMagick":
 DestroyMagickWand(mw)
 Wait 0.01
 MagickWandTerminus()
 
End


Riferimenti