freeimage.h
FreeImage_GetFileType
DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileType(const char *filename, int size FI_DEFAULT(0));
Pide a FreeImage que analice la firma del mapa de bits. La función retorna unas de las constantes predefinidas FREE_IMAGE_FORMAT o un número de identificacion de mapa de bits registrado por un plugin. El parámetro size no se usa actualmente y puede ser 0.
Dado que no todos los formatos pueden ser identificados por su cabecera (algunas imágenes no tienen cabecera o al final del fichero), FreeImage_GetFileType puede retornar FIF_UNKNOWN si no existe un plugin disponible para analizar el fichero. En ese caso, se puede usar FreeImage_GetFIFFromFilename para adivinar el formato del fichero a partir de la extensión del nombre del fichero, pero ésta última función es más lenta y menos precisa.
/** Generic image loader
@param lpszPathName Pointer to the full file name
@param flag Optional load flag constant
@return Returns the loaded dib if successful, returns NULL otherwise
*/
FIBITMAP* GenericLoader(const char* lpszPathName, int flag) {
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
// check the file signature and deduce its format
// (the second argument is currently not used by FreeImage)
fif = FreeImage_GetFileType(lpszPathName, 0);
if(fif == FIF_UNKNOWN) {
// no signature ?
// try to guess the file format from the file extension
fif = FreeImage_GetFIFFromFilename(lpszPathName);
}
// check that the plugin has reading capabilities ...
if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
// ok, let's load the file
FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, flag);
// unless a bad file format, we are done !
return dib;
}
return NULL;
}