Convert Any Image to GIF Format [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Convert Any Image to GIF Format [Windows]

Post by RASHAD »

- Code by srod
- Added and Modified by RASHAD
- It can use GDI+ directly to load the image but not TGA
- I hate to see some questions not answered
Have fun

Code: Select all

UsePNGImageDecoder()
UseJPEGImageDecoder()
UseJPEG2000ImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()

#GDIPLUS_OK = 0

Structure GdiplusStartupInput
  GdiPlusVersion.l
  *DebugEventCallback.DebugEventProc
  SuppressBackgroundThread.l
  SuppressExternalCodecs.l
EndStructure

Import "gdiplus.lib"
  GdiplusStartup(token, *input.GdiplusStartupInput, output)
  GdiplusShutdown(token)
  GdipCreateBitmapFromFile(filename.p-unicode, *bitmap)
  GdipCreateBitmapFromHBITMAP(hbm, hpal, *bitmap)
  GdipSaveImageToFile(image, filename.p-unicode, *clsidEncoder.CLSID, *encoderParams)
  GdipDisposeImage(image)
EndImport

pattern$ = "PNG, BMP, JPEG, TIFF|*.png;*.bmp;*.jpg;*.jpeg;*.tiff|PNG (*.png)|*.png|BMP (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|TIFF (*.tif)|*.tif"
Filename$ = OpenFileRequester("Choose an image to convert to GIF:","",pattern$, 0)
If Filename$
  If LoadImage(0,Filename$) = 0
    MessageRequester("Error","Image not loaded",#MB_OK)
    End
  EndIf
EndIf

input.GdiplusStartupInput
input\GdiPlusVersion = 1
GdiplusStartup(@token, @input, #Null)
If token
  If GdipCreateBitmapFromHBITMAP(ImageID(0),0, @image) = #GDIPLUS_OK
    GdipSaveImageToFile(image, GetTemporaryDirectory()+"test.gif", ?clsid_giff, 0)
    GdipDisposeImage(image)
  EndIf
  If IsImage(0)
    FreeImage(0)
  EndIf
  GdiplusShutdown(token)
EndIf
End

DataSection
  clsid_giff:
  Data.l $557CF402
  Data.w $1A04, $11D3
  Data.b $9A, $73, $00, $00, $F8, $1E, $F3, $2E
EndDataSection
Egypt my love
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: Convert Any Image to GIF Format [Windows]

Post by VB6_to_PBx »

- I hate to see some questions not answered
Rashad ... thank you for this Code !

a few questions :

is it possible to set or change a .Gif picture quality value ?
is this Code saving a .Gif at its highest possible picture quality ?

and
DataSection
clsid_giff:
Data.l $557CF402
Data.w $1A04, $11D3
Data.b $9A, $73, $00, $00, $F8, $1E, $F3, $2E
EndDataSection
how and where are you getting the clsid values ?
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
BarryG
Addict
Addict
Posts: 3322
Joined: Thu Apr 18, 2019 8:17 am

Re: Convert Any Image to GIF Format [Windows]

Post by BarryG »

Rashad, thanks, but it seems the GIF image with your code is dithered or something, because the quality is lower than the original code.

(a) Original test image -> https://i.imgur.com/ADfHBip.png
(b) Original code to gif -> https://i.imgur.com/5FeUsRg.png
(c) Your new code to gif -> https://i.imgur.com/t0C6Agl.png

See what I mean? Compare (b) and (c). Can your routine make it look like the (b) image above?

(BTW, I saved the results as PNGs simply out of habit, but the visuals are the same as PNG is lossless).
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Convert Any Image to GIF Format [Windows]

Post by Saki »

With a GIF photo you go back to the computer stone age.
This is how it will look like.
That is normal.
Just have a look in Wikipedia how a GIF works.
I myself don't see any use for a mono frame gif because it has only disadvantages, sorry.
地球上の平和
BarryG
Addict
Addict
Posts: 3322
Joined: Thu Apr 18, 2019 8:17 am

Re: Convert Any Image to GIF Format [Windows]

Post by BarryG »

Saki, Rashad's modification of the original code leads to a dithered image. The only difference is that the same original image was converted from a file (b) or from RAM (c). The output should be the same, but the file version is clearly visually superior.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Convert Any Image to GIF Format [Windows]

Post by Saki »

Hmm, I can't say anything about that.
Gif is devil's work. :wink:
What would be needed would be the full support of PB.
But this is all very complex.
The hype after GIF is largely over again and it will probably disappear again completely, i think
地球上の平和
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Convert Any Image to GIF Format [Windows]

Post by RASHAD »

Hi VB6_to_PBx

Next are the gdiplus_GUID for different formats

Code: Select all

GUID_ImageEncoderBMP:
  Data.l $557cf400
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderJPEG:
  Data.l $557cf401
  Data.w $1a04, $11d3 
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderGIF:
  Data.l $557cf402
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageDecoderEMF:
  Data.l $557cf403
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageDecoderWMF:
  Data.l $557cf404
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderTIFF:
  Data.l $557cf405
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderPNG:
  Data.l $557cf406
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
Egypt my love
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: Convert Any Image to GIF Format [Windows]

Post by VB6_to_PBx »

RASHAD wrote:Hi VB6_to_PBx

Next are the gdiplus_GUID for different formats

Code: Select all

GUID_ImageEncoderBMP:
  Data.l $557cf400
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderJPEG:
  Data.l $557cf401
  Data.w $1a04, $11d3 
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderGIF:
  Data.l $557cf402
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageDecoderEMF:
  Data.l $557cf403
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageDecoderWMF:
  Data.l $557cf404
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderTIFF:
  Data.l $557cf405
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
GUID_ImageEncoderPNG:
  Data.l $557cf406
  Data.w $1a04, $11d3
  Data.b $9a, $73, $00, $00, $f8, $1e, $f3, $2e
thank you Rashad , you are always helpful !
i wish we could Vote or give Stars in this Forum .
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
Post Reply