Ridimensionare 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.1 ".
Mostriamo di seguito un semplice esempio pratico per creare da una immagine iniziale una sua copia ridimensionata come un thumbnail:
Library "libMagickWand-7.Q16HDRI:10.0.1" 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 UndefinedFilter = 0, PointFilter, BoxFilter, TriangleFilter, HermiteFilter, HanningFilter, HammingFilter, BlackmanFilter, GaussianFilter, QuadraticFilter, CubicFilter, CatromFilter, MitchellFilter, JincFilter, SincFilter, SincFastFilter, KaiserFilter, WelshFilter, ParzenFilter, BohmanFilter, BartlettFilter, LagrangeFilter, LanczosFilter, LanczosSharpFilter, Lanczos2Filter, Lanczos2SharpFilter, RobidouxFilter, RobidouxSharpFilter, CosineFilter, SplineFilter, LanczosRadiusFilter, SentinelFilter ' 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 MagickResizeImage(MagickWand *wand, const size_t columns,const size_t rows,const FilterType filter) ' Scales an image to the desired dimensions with a filter. Private Extern MagickResizeImage(wand As MagickWand, columns As Long, rows As Long, filter As Integer, blur 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 ' 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 Boolean 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 ' Ridimensiona l'immagine: MagickResizeImage(mwand, 128, 64, LanczosFilter, 1.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