freeimage.h

FreeImage_LoadFromMemory

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadFromMemory(FREE_IMAGE_FORMAT fif,
FIMEMORY *stream, int flags FI_DEFAULT(0));

Esta función hace con canales en memoria los mismo que FreeImage_Load hace con canales de fichero.

FreeImage_LoadFromMemory decodifica un mapa de bits, obtiene memoria para él y después lo retorna como un FIBITMAP. El primer parámetro define el tipo de mapa de bits a cargar. Por ejemplo, cuendo se pasa FIF_BMP, se carga un fichero BMP en memoria (una descripción de las constantes posibles FREE_IMAGE_FORMAT está disponible en la tabla 1). El segundo parámetro indica a FreeImage el canal de memoria que debe decodificar. El último parámetro se usa para modificar el comportamiento o activar alguna característica en el plugin de mapas de bits. Cada plugin tiene su propio conjunto de parámetros.

Algunos cargadores de mapas de bits pueden recibir parámetros para modificar el comportamiento de carga (ver tabla 3).

Cuando el parámetro no está disponible o no se use se puede pasar el valor 0 o <TYPE_OF_BITMAP>_DEFAULT (por ejemplo BMP_DEFAULT, ICO_DEFAULT, etc).

void testLoadMemIO(const char *lpszPathName) {
    struct stat buf;
    int result;
    // get data associated with lpszPathName
    result = stat(lpszPathName, &buf);
    if(result == 0) {
        // allocate a memory buffer and load temporary data
        BYTE *mem_buffer = (BYTE*)malloc(buf.st_size * sizeof(BYTE));
        if(mem_buffer) {
            FILE *stream = fopen(lpszPathName, "rb");
            if(stream) {
                fread(mem_buffer, sizeof(BYTE), buf.st_size, stream);
                fclose(stream);
                // attach the binary data to a memory stream
                FIMEMORY *hmem = FreeImage_OpenMemory(mem_buffer, buf.st_size);
                // get the file type
                FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem, 0);
                // load an image from the memory stream
                FIBITMAP *check = FreeImage_LoadFromMemory(fif, hmem, 0);
                // save as a regular file
                FreeImage_Save(FIF_PNG, check, "blob.png", PNG_DEFAULT);
                FreeImage_Unload(check);

                // always close the memory stream
                FreeImage_CloseMemory(hmem);
            }
        }
        // user is responsible for freeing the data
        free(mem_buffer);
    }
}