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.