Modificare la risoluzione in PPI 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 ".

Mostriamo di seguito un semplice esempio pratico per modificare la risoluzione in PPI (Pixel Per Inches ) di 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 UndefinedResolution = 0, PixelsPerInchResolution, PixelsPerCentimeterResolution
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 MagickSetImageResolution(MagickWand *wand, const double x_resolution, const double y_resolution)
' Sets the image resolution.
Private Extern MagickSetImageResolution(wand As MagickWand, x_resolution As Float, y_resolution As Float) As Integer

' MagickBooleanType MagickSetImageUnits(MagickWand *wand, const ResolutionType units)
' Sets the image units of resolution.
Private Extern MagickSetImageUnits(wand As MagickWand, units As Integer) 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 mw As MagickWand
  Dim fileimmagine, nuovofile As String  
 
  fileimmagine = "/percorso/del/file/immagine"
  nuovofile = "/percorso/del/nuovo/file/immagine"
  
  MagickWandGenesis()
  
  mw = NewMagickWand()

  bo = MagickReadImage(mw, fileimmagine)
  If bo = MagickFalse Then
    Error.Raise("Impossibile caricare il file immagine !")
    Chiude(mw)
  Endif

' Imposta la risoluzione ("Pixel Per Inches") dell'immagine:
  MagickSetImageResolution(mw, 150.0, 150.0)

' Modifica la risoluzione ("Pixel Per Inches") dell'immagine:
  MagickSetImageUnits(mw, PixelsPerInchResolution)

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

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