Modificare il colore di sfondo di un'immagine GIF con le funzioni del API di GifLib

Da Gambas-it.org - Wikipedia.

La libreria GifLib, scritta da Eric Steven Raymond, consente di caricare, gestire, manipolare, creare ex novo e salvare file immagine in fomato GIF.

E' possibile con alcune sue funzioni caricare un'immagine di tipo GIF, modificarne il colore di sfondo e quindi salvare il risultato in un nuovo file d tipo .gif .

Per poter utilizzare al meglio le risorse della libreria GifLib con Gambas, suggeriamo di utilizzare la relativa libreria condivisa: "libgif.so.7.1.0 "

Mostriamo un semplice esempio:

The GIFLIB distribution is Copyright (c) 1997  Eric S. Raymond

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Library "libgif:7.1.0"

Private Const GIF_FONT_HEIGHT As Integer = 8

' GifFileType * DGifOpenFileName(const char *FileName, int *Error)
' Open a new GIF file for read, given by its name.
Private Extern DGifOpenFileName(FileName As String, ErrorP As Pointer) As Pointer

' int DGifSlurp(GifFileType *GifFile)
' Reads an entire GIF into core, hanging all its state info off the GifFileType pointer.
Private Extern DGifSlurp(GifFileType As Pointer) As Integer

' GifFileType * EGifOpenFileName(const char *FileName, const bool TestExistence, int *Error)
' Open a new GIF file for write, specified by name.
Private Extern EGifOpenFileName(FileName As String, TestExistence As Boolean, ErrorP As Pointer) As Pointer

' ColorMapObject * GifMakeMapObject (int ColorCount, const GifColorType * ColorMap)
' Allocate a color map of given size.
Private Extern GifMakeMapObject(ColorCount As Integer, ColorMap As Pointer) As Pointer

' SavedImage * GifMakeSavedImage(GifFileType *GifFile, const SavedImage *CopyFrom)
' Append an image block to the SavedImages array.
Private Extern GifMakeSavedImage(GifFileType As Pointer, CopyFrom As Pointer) As Pointer

' int EGifSpew(GifFileType *GifFileOut)
Private Extern EGifSpew(GifFileType As Pointer) As Integer

' int DGifCloseFile(GifFileType *GifFile, int *ErrorCode)
' Close the GIF file.
Private Extern DGifCloseFile(GifFileType As Pointer, ErrorP As Pointer) As Integer


Public Sub Main()

 Dim fileGIFIn, fileGIFOut, p1, p2, p3, SavedImage As Pointer
 Dim SWidth, SHeight As Integer
 Dim SColorResolution, SBackGroundColor, ColorCount As Integer
 Dim stIn, colori, rgb, stOut As Stream
 Dim b As Byte

' Apre il file .gif in entrata:
 fileGIFIn = DGifOpenFileName("/percorso/del/file.gif", 0)
 If fileGIFIn == 0 Then Error.Raise("Impossibile creare il file GIF !")

 DGifSlurp(fileGIFIn)

' Apre il file .gif in uscita:
 fileGIFOut = EGifOpenFileName("/percorso/del/nuovo/file.gif", 0, 0)
 If fileGIFOut == 0 Then Error.Raise("Impossibile creare il file GIF !")
  
' Legge i valori RGB del colore di fondo dell'immagine gif caricata:

 stIn = Memory fileGIFIn For Read Write      ' struct GifFileType
 Read #stIn, SWidth
 Read #stIn, SHeight
 Read #stIn, SColorResolution
 Read #stIn, SBackGroundColor
 Seek #stIn, 24                            ' struct ColorMapObject *SColorMap
 Read #stIn, p1
      
 Seek #stIn, 72
 Read #stIn, SavedImage                    ' struct SavedImage
 stIn.Close
 Print "Dimensione: "; Null, SWidth; " pixel x "; SHeight; " pixel"
 Print "Risoluzione: "; Null, SColorResolution
 colori = Memory p1 For Read Write         ' struct ColorMapObject *SColorMap
 Read #colori, ColorCount
 Seek #colori, 16
 Read #colori, p2
 colori.Close
 rgb = Memory p2 For Read                   ' struct GifColorType *Colors
 Print "\nColori dello sfondo:"
 Read #rgb, b
 Print "Rosso: "; Null, Null, b
 Read #rgb, b
 Print "Verde: "; Null, Null, b
 Read #rgb, b
 Print "Verde: "; Null, Null, b
 rgb.Close
  
' Scrive i nuovi valori RGB del colore di fondo dell'immagine gif caricata:
 stIn = Memory fileGIFIn For Read
 Seek #stIn, 24
 Read #stIn, p1
 stIn.Close
 colori = Memory p1 For Read
 Seek #colori, 16
 Read #colori, p2
 colori.Close
 rgb = Memory p2 For Write
 Write #rgb, 255 As Byte
 Write #rgb, 255 As Byte
 Write #rgb, 0 As Byte
 rgb.Close
   
 stOut = Memory fileGIFOut For Read Write
 Write #stOut, SWidth As Integer
 Write #stOut, SHeight As Integer
 Write #stOut, SColorResolution As Integer
 Write #stOut, SBackGroundColor As Integer
     
 p3 = Alloc(SizeOf(gb.Byte), 24)
 colori = Memory p3 For Write               ' struct ColorMapObject
 Seek #colori, 16
 Write #colori, GifMakeMapObject(ColorCount, p2)
 Seek #stOut, 24                           ' struct ColorMapObject *SColorMap
 Write #stOut, p3
 colori.Close
 stOut.Close

 GifMakeSavedImage(fileGIFOut, SavedImage)
 
' Va in chiusura:
 Free(p3)
 EGifSpew(fileGIFOut)
 DGifCloseFile(fileGIFIn, 0)

End


Riferimenti