Page 1 of 1

.gif lib for purebasic?

Posted: Sun Oct 13, 2013 5:52 am
by es_91
I want to encode some pictures to gif. I need full control over the parameters used by the .gif engine, like dithering-value. I have seen these options in Paint.net.

Is there a user lib for such purpose?

greetings,

es_91.

Re: .gif lib for purebasic?

Posted: Sun Oct 13, 2013 6:28 am
by Shield
There are codes here on the forums regarding gifs, because this question has been asked dozens of times before. :wink:
In addition, dithering doesn't really have anything to do with the gif format or its encoding. This is your application's
responsibility to create the "best looking" output. There are dithering algorithms on this forum as well.

Re: .gif lib for purebasic?

Posted: Sun Oct 13, 2013 8:37 am
by es_91
What dithering algorithmn might Paint.NET use? I want my pictures to look like its value 4. I am keen to write a dithering algorithm.


AND BTW IF YOU HAVEN'T ALREADY KNOWN IT: RAR COMPRESSION OVER DITHERED 8 BIT BMP-FILES DOES BETTER COMPRESSION THAN .GIF!


Thanks.

Re: .gif lib for purebasic?

Posted: Sun Oct 13, 2013 9:15 am
by netmaestro
I wrote code for converting images to 8 bits some time ago and incorporated Floyd-Steinberg dithering. I added saving as GIF as well. The code itself can be found in T&T, but I include here for your convenience the compiled dll which I've used regularly for around 5 years now:

http://lloydsplace.com/256colors.dll

and here is a sample code showing how to use it:

Code: Select all

Prototype ConvertTo8bit(hBitmap, dither)
Prototype Save8bitImage(hBitmap8, outpath$, memory=0)
Prototype SaveGIF(hBitmap, outfile$)

OpenLibrary(0, "256Colors.dll")
ConvertTo8bit.ConvertTo8bit = GetFunction(0, "ConvertTo8bit")
Save8bitImage.Save8bitImage = GetFunction(0, "Save8bitImage")
SaveGIF.SaveGIF             = GetFunction(0, "SaveGIF")

UsePNGImageDecoder()
UseJPEGImageDecoder() 
UseTIFFImageDecoder()

pattern$ = "PNG, BMP, JPEG, TIFF|*.png;*.bmp;*.jpg;*.jpeg;*.tiff|PNG (*.png)|*.png|BMP (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|TIFF (*.tif)|*.tif"
inpath$ = OpenFileRequester("Choose an image to convert to GIF:","",pattern$, 0)
prompt$ = RemoveString(GetFilePart(inpath$),"."+GetExtensionPart(inpath$))
prompt$ +".gif"
If FileSize(inpath$) < 1
  MessageRequester("Info:","No file selected. Ending...",#MB_ICONINFORMATION)
  End
EndIf

If inpath$
  hImage = LoadImage(#PB_Any, inpath$) 
Else
  End
EndIf

i = ConvertTo8bit(ImageID(hImage), 1) 

pattern$ = "GIF (*.gif)|*.gif;"
outpath$ = SaveFileRequester("Choose a path to save the .gif file:",prompt$,pattern$, 0)
outpath$ = RemoveString(outpath$, ".gif")
If outpath$
  outpath$ + ".gif"
  Debug Save8bitImage(i, "test.bmp",0)
  Debug SaveGIF(i, outpath$)
Else
  End 
EndIf

; Let's take a look at the results...
GetObject_(i, SizeOf(BITMAP), bmp.BITMAP)
w = bmp\bmWidth
h = bmp\bmHeight
OpenWindow(0,0,0,w,h,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) 
ImageGadget(0,0,0,0,0,i)

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
I hope it's of use to you, however if not I won't be doing any more work on it.

Also, as it's a compiled library I should mention that all the credit for it isn't mine. For generating the colortable it uses Anthony Dekker's neural network code ported to PureBasic by luis and for the conversion to GIF I used the gdiplus library from Microsoft.

Re: .gif lib for purebasic?

Posted: Mon Oct 14, 2013 8:49 pm
by netmaestro
Update: Better version (no dll needed) here: http://purebasic.fr/english/viewtopic.p ... 92#p428192