FreeImage_LoadFromHandle functions are slightly more advanced than the simple load functions, but still quite easy to use. The idea of these functions is that FreeImage doesn't care how it gets its data as long as it gets it. It shouldn't matter if the bitmap is loaded from file, memory or the Internet since data is data not matter where it comes from.
To make this idea of data abstraction work a new, abstract file I/O system is used: FreeImageIO . FreeImageIO is a structure containing pointers to 4 functions: a read function, write function, seek function and tell function. All these functions have to be implemented so that data is delivered. The handle representing the data is made abstract as well and is named fi_handle .
Loading a bitmap using the ...FromHandle functions involves four things. First the 4 file I/O functions are implemented and a FreeImageIO structure is filled with the pointers. Then the file is opened (that is, if we are in fact dealing with a file). After that one of the ...LoadFromHandle functions can be called. If the bitmap is successfully loaded the file has to be closed.
unsigned
_ReadProc(FIBITMAP *buffer, unsigned s, unsigned c, fi_handle handle) {
return fread(buffer, s, c, (FILE *)handle);
}
unsigned
_WriteProc(FIBITMAP *buffer, unsigned s, unsigned c, fi_handle handle){
return fwrite(buffer, s, c, (FILE *)handle);
}
int
_SeekProc(fi_handle handle, long offset, int origin) {
return fseek((FILE *)handle, offset, origin);
}
long
_TellProc(fi_handle handle) {
return ftell((FILE *)handle);
}
FreeImageIO io;
io.read_proc = _ReadProc;
io.write_proc = _WriteProc;
io.seek_proc = _SeekProc;
io.tell_proc = _TellProc;
FILE *file = fopen("test.bmp", "rb");
if (file != NULL) {
FIBITMAP *dib = FreeImage_LoadBMPFromHandle(&io, (fi_handle)file);
FreeImage_Free(dib);
fclose(file);
}
FIBITMAP *FreeImage_LoadBMPFromHandle(FreeImageIO *io, fi_handle handle, int flags = BMP_DEFAULT);
Loads the given BMP file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadBMPFromHandle returns NULL.
FIBITMAP *FreeImage_LoadICOFromHandle(FreeImageIO *io, fi_handle handle, int flags = ICO_DEFAULT);
Loads the given ICO file into a FreeImage bitmap using the given FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadICOFromHandle returns NULL.
FIBITMAP *FreeImage_LoadJPEGFromHandle(FreeImageIO *io, fi_handle handle, int flags = JPEG_DEFAULT);
Loads the given JPEG file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadJPEGFromHandle returns NULL.
FIBITMAP *FreeImage_LoadKOALAFromHandle(FreeImageIO *io, fi_handle handle, int flags = KOALA_DEFAULT);
Loads the given KOALA file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadKOALAFromHandle returns NULL.
FIBITMAP *FreeImage_LoadLBMFromHandle(FreeImageIO *io, fi_handle handle, int flags = LBM_DEFAULT);
Loads the given Deluxe Paint LBM file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadLBMFromHandle returns NULL.
FIBITMAP *FreeImage_LoadMNGFromHandle(FreeImageIO *io, fi_handle handle, int flags = MNG_DEFAULT);
Loads the Multi Network Graphics (MNG) or JPEG Network Graphics (JNG) file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadMNGFromHandle returns NULL.
FIBITMAP *FreeImage_LoadPCDFromHandle(FreeImageIO *io, fi_handle handle, int flags = PCD_DEFAULT);
Loads the given Kodak PhotoCD file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadPCDFromHandle returns NULL.
FIBITMAP *FreeImage_LoadPCXFromHandle(FreeImageIO *io, fi_handle handle, int flags = PCX_DEFAULT);
Loads the given PCX file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadPCXFromHandle returns NULL.
FIBITMAP *FreeImage_LoadPNMFromHandle(FreeImageIO *io, fi_handle handle, int flags = PNM_DEFAULT);
Loads the given PBM, PGM or PPM file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadPNMFromHandle returns NULL.
FIBITMAP *FreeImage_LoadPNGFromHandle(FreeImageIO *io, fi_handle handle, int flags = PNG_DEFAULT);
Loads the given PNG file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadPNGFromHandle returns NULL.
FIBITMAP *FreeImage_LoadRASFromHandle(FreeImageIO *io, fi_handle handle, int flags = RAS_DEFAULT);
Loads the given Sun Rasterfile into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadRASFromHandle returns NULL.
FIBITMAP *FreeImage_LoadTARGAFromHandle(FreeImageIO *io, fi_handle handle, int flags = TARGA_DEFAULT);
Loads the given TARGA file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadTARGAFromHandle returns NULL.
FIBITMAP *FreeImage_LoadTIFFFromHandle(FreeImageIO *io, fi_handle handle, int flags = TIFF_DEFAULT);
Loads the given TIFF file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadTIFFFromHandle returns NULL.
FIBITMAP *FreeImage_LoadWBMPFromHandle(FreeImageIO *io, fi_handle handle, int flags = WBMP_DEFAULT);
Loads the given WBMP file into a FreeImage bitmap using the specified FreeImageIO struct and fi_handle. If the file is loaded successfully, memory for it is allocated and a bitmap pointer is returned. If the file couldn't be loaded, FreeImage_LoadWBMPFromHandle returns NULL.