Best approach for WebP lib

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Best approach for WebP lib

Post by UEZ »

I want to try to load and display WebP images using this source https://developers.google.com/speed/webp/docs/api.

There is a libwebp.lib - can I use this easily or do I have to convert it somehow?

I tried this (function is listed in decode.h) but it fails with a lot of error messages!

Code: Select all

#Inclib "libwebp"
#include "file.bi"

Type uint8_t As Ubyte
Type size_t As ulong

Extern "C"
	Declare Function WebPGetDecoderVersion Alias "WebPGetDecoderVersion" (Byval data As uint8_t Ptr, Byval data_size As size_t, Byref w As Long Ptr, Byref h As Long Ptr) As Ulong
End Extern


Dim As String sFile = "Picture.webp"
Dim As Uinteger iSize = Filelen(sFile)

Dim pMem As Ubyte Ptr
pMem = Allocate(iSize)

Dim As Integer hFile = Freefile()
Open sFile For Binary Access Read As #hFile
Get #hFile, 0, pMem[0], iSize
Close #hFile

Dim As Long w, h
? WebPGetDecoderVersion(pMem, iSize, @w, @h), w, h
Deallocate (pMem)
Sleep
Download: https://storage.googleapis.com/download ... index.html

What is the best approach to convert such libs to FB?

@Mods: you may move it to "Libraries Questions".
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Best approach for WebP lib

Post by dodicat »

I downloaded "libwebp-0.4.1-rc1-windows-x86-no-wic"
libwebp.a is 32 bits.
running this (your code) with #Inclib "webp" only changed :

Code: Select all

#Inclib "webp"
#include "file.bi"

Type uint8_t As Ubyte
Type size_t As ulong

Extern "C"
	Declare Function WebPGetDecoderVersion Alias "WebPGetDecoderVersion" (Byval data As uint8_t Ptr, Byval data_size As size_t, Byref w As Long Ptr, Byref h As Long Ptr) As Ulong
End Extern


Dim As String sFile = "Picture.webp"
Dim As Uinteger iSize = Filelen(sFile)
print isize
Dim pMem As Ubyte Ptr
pMem = Allocate(iSize)

Dim As Integer hFile = Freefile()
Open sFile For Binary Access Read As #hFile
Get #hFile, 0, pMem[0], iSize
Close #hFile

Dim As Long w, h
? WebPGetDecoderVersion(pMem, iSize, @w, @h), w, h
Deallocate (pMem)
Sleep 
I get

Code: Select all

0
1025           0             0
 
No errors.
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Best approach for WebP lib

Post by UEZ »

Thanks dodicat. I didn't check the other archives for .a file. Yes, now it works.

Code: Select all

'Coded by UEZ
#Inclib "webp"
#include "file.bi"
#Include "fbgfx.bi"

Using FB

Type uint8_t As Ubyte
Type size_t As Ulong

Type tWebPBitstreamFeatures
	As Long width
	As Long height
	As Long has_alpha
End Type
								
Extern "C"
	Declare Function WebPGetFeatures Alias "WebPGetFeatures" (Byval data As uint8_t Ptr, Byval data_size As size_t, Byval feature As tWebPBitstreamFeatures Ptr) As Ulong
	Declare Function WebPGetInfo Alias "WebPGetInfo" (Byval data As uint8_t Ptr, Byval data_size As size_t, Byval w As Long Ptr, Byval h As Long Ptr) As Ulong
	Declare Function WebPDecodeRGBA Alias "WebPDecodeRGBA" (Byval data As uint8_t Ptr, Byval data_size As size_t, Byval w As Long Ptr, Byval h As Long Ptr) As ULong Ptr
	Declare Function WebPDecodeARGB Alias "WebPDecodeARGB" (Byval data As uint8_t Ptr, Byval data_size As size_t, Byval w As Long Ptr, Byval h As Long Ptr) As ULong Ptr
	Declare Function WebPDecodeBGRA Alias "WebPDecodeBGRA" (Byval data As uint8_t Ptr, Byval data_size As size_t, Byval w As Long Ptr, Byval h As Long Ptr) As ULong Ptr
	Declare Function WebPGetDecoderVersion Alias "WebPGetDecoderVersion" () As Ulong
	Declare Function WebPFree Alias "WebPFree" (Byval As ULong Ptr) As Long
End Extern

Dim As String sFile = "Picture.webp"
Dim As Uinteger iSize = Filelen(sFile)

Dim pMem As Ubyte Ptr
pMem = Allocate(iSize)

Dim As Integer hFile = Freefile()
Open sFile For Binary Access Read As #hFile
Get #hFile, 0, pMem[0], iSize
Close #hFile

Dim As Long w, h, i = 0
Dim As tWebPBitstreamFeatures WebPBitstreamFeatures
Dim As ULong Ptr pColor
? WebPGetInfo(pMem, iSize, @w, @h), w, h
? Hex(WebPGetDecoderVersion)
pColor = WebPDecodeBGRA(pMem, iSize, @w, @h)

Screenres w, h, 32, 1, GFX_ALPHA_PRIMITIVES Or GFX_NO_SWITCH
For y As Long = 0 To h - 1
	For x As Long = 0 To w - 1
		Pset (x, y), pColor[i]
		i += 1
	Next
Next
WebPFree(pColor)
Deallocate (pMem)
Sleep
The output is as expected:

Code: Select all

1              1024          772
Post Reply