Creare una GIF animata con le funzioni esterne del API di Libplot

Da Gambas-it.org - Wikipedia.

Libplot è una libreria di funzioni gratuita per disegnare grafica vettoriale bidimensionale. Può produrre, animazioni per il sistema X Window, e può esportare file grafici in molti formati di file.

Per poter utilizzare in Gambas le risorse di LIbplot, è necessario installare e richiamare la libreria dinamica condivisa: "libplot.so.2.2.4"


Mostriamo un semplice esempio, nel quale sarà generato un'immagine GIF animata delle dimensioni 150 x 100 pixel:

Library "libplot:2.2.4"

' plPlotterParams * pl_newplparams (void)
' Constructor constructor for the plPlotterParams type.
Private Extern pl_newplparams() As Pointer

' int pl_setplparam (plPlotterParams *plotter_params, const char *parameter, void *value)
' A function for setting a single Plotter parameter in a plPlotterParams instance.
Private Extern pl_setplparam(plotter_params As Pointer, parameter As String, value As String) As Integer

' plPlotter * pl_newpl_r (const char *type, FILE *infile, FILE *outfile, FILE *errfile, const plPlotterParams *plotter_params)
' Constructor for the plPlotter type.
Private Extern pl_newpl_r(type As String, infile As Pointer, outfile As Pointer, errfile As Pointer, plotter_params As Pointer) As Pointer

' int pl_openpl_r (plPlotter *plotter)
Private Extern pl_openpl_r(plPlotter As Pointer) As Integer

' int pl_fspace_r (plPlotter *plotter, double x0, double y0, double x1, double y1)
Private Extern pl_fspace_r(plPlotter As Pointer, x0 As Float, y0 As Float, x1 As Float, y1 As Float) As Integer

' int pl_pencolorname_r (plPlotter *plotter, const char *name)
Private Extern pl_pencolorname_r(plPlotter As Pointer, name As String) As Integer

' int pl_linewidth_r (plPlotter *plotter, int size)
Private Extern pl_linewidth_r(plPlotter As Pointer, size As Integer) As Integer

' int pl_filltype_r (plPlotter *plotter, int level)
Private Extern pl_filltype_r(plPlotter As Pointer, level As Integer) As Integer

' int pl_fillcolorname_r (plPlotter *plotter, const char *name)
Private Extern pl_fillcolorname_r(plPlotter As Pointer, name As String) As Integer

' int pl_erase_r (plPlotter *plotter)
Private Extern pl_erase_r(plPlotter As Pointer) As Integer

' int pl_ellipse_r (plPlotter *plotter, int x, int y, int rx, int ry, int angle)
Private Extern pl_ellipse_r(plPlotter As Pointer, xI As Integer, yI As Integer, rx As Integer, ry As Integer, angle As Integer) As Integer

' int pl_deleteplparams (plPlotterParams *plotter_params)
' Destructor for the plPlotterParams type.
Private Extern pl_deleteplparams(plotter_params As Pointer) As Integer

' int pl_closepl_r (plPlotter *plotter)
Private Extern pl_closepl_r(plPlotter As Pointer) As Integer

' int pl_deletepl_r (plPlotter *plotter)
' Destructor for the plPlotter type.
Private Extern pl_deletepl_r(plPlotter As Pointer) As Integer


Library "libc:6"

' FILE *fopen(const char *filename, const char *mode)
' Opens the filename pointed to by filename using the given mode.
Private Extern fopen(filename As String, mode As String) As Pointer

' int fclose(FILE *stream)
' Closes the stream. All buffers are flushed.
Private Extern fclose(fs As Pointer) As Integer


Public Sub Main()

 Dim plotter, param, fl As Pointer
 Dim i As Integer

  fl = fopen("/percorso/del/file.gif", "wb")
 
' Imposta i parametri del disegnatore:
  param = pl_newplparams()
  pl_setplparam(param, "BITMAPSIZE", "150x100")
  pl_setplparam(param, "BG_COLOR", "orange")
  pl_setplparam(param, "TRANSPARENT_COLOR", "orange")
  pl_setplparam(param, "GIF_ITERATIONS", "100")
  pl_setplparam(param, "GIF_DELAY", "5")
      
' Crea un disegnatore di GIF con i parametri specificati:
  plotter = pl_newpl_r("gif", 0, fl, 0, param)
   
  pl_openpl_r(plotter)
  pl_fspace_r(plotter, -0.5, -0.5, 149.5, 99.5)
   
' Usa il colore rosso per disegnare l'oggetto grafico:
  pl_pencolorname_r(plotter, "red")
  pl_linewidth_r(plotter, 5)
' L'oggetto grafico viene riempito:
  pl_filltype_r(plotter, 1)
' Imposta il colore di riempimento:
  pl_fillcolorname_r(plotter, "yellow")
   
  For i = 0 To 180 - 1 Step 15
    pl_erase_r(plotter)
' Disegna un'ellisse:
    pl_ellipse_r(plotter, 75, 50, 40, 20, i)
  Next
   
' Va in chiusura:
  pl_deleteplparams(param)
  pl_closepl_r(plotter)
  pl_deletepl_r(plotter)
  fclose(fl)

End



Riferimenti