Question

I am currently trying to implement gif support in a general image de-/en-coding library. There we defined callback functions for fwrite and fread to make it more generic.

For the gif implementation, I tried to use the giflib. Now I am wondering, if there is any possibility, to set custom I/O callbacks.

DGifOpen ( void *userPointer, InputFunc *func) 

is taking a input handling function as an argument, but I could not figure out the functionality from the Documentation. It just refers to the source headers, there it only says:

/* func type to read gif data from arbitrary sources (TVT) */
typedef int (*InputFunc) (GifFileType *, GifByteType *, int);

My question is, if this function is used for custom file reading (and if, how), or if the giflib is not designed to work with callbacks.

Was it helpful?

Solution

Okay, I browsed through the source code of the giflib, and realized that the callback should replace fread.

typedef int (*InputFunc) (GifFileType *, GifByteType *, int);

The first argument is the gif handle, the second is the destination buffer, and the third is the length of data, that should be read. Return value is, equivalent to fread, the number of bytes that were read. Additional data can be passed in the userPointer when the callback is registered:

DGifOpen ( void *userPointer, InputFunc *func) 

The userPointer can be accessed as a member of the gif handle (pGifHandle->userData)

The same way, you can use a write callback.

Register your function and a pointer to custom data in

EGifOpen ( void *userPointer, OutputFunc *func)

where OutputFunc takes the gif handle as the first argument, the buffer as the second, and the number of bytes to write as third. The return value is the number of bytes written.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top