Search code examples
freeimage

FreeImage - read from base64-encoded char*


I have a base64-encoded string (char[]) that represents a PNG image.

I want to create a FIBITMAP from that string - what's the easiest way to achieve this?


Solution

  • According to the documentation something like the following code should do the task:

    /* Needed here: data -> The string you want to read
                    len  -> The length of the string */
    char *decoded;
    size_t decoded_len;
    base64_decode(data, len, &decoded, &decoded_len); /* See note 1 below! */
    FIMEMORY *mem = FreeImage_OpenMemory(decoded, decoded_len);
    FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(mem, 0);
    FIBITMAP *result = FreeImage_LoadFromMemory(fif, mem, 0);
    /* [...] */
    FreeImage_Unload(result);
    FreeImage_CloseMemory(mem);    
    

    Note 1: FreeImage seems not to be capable of reading Base64-coded data directly. Thus, the data you want to use is decoded here. Please note that you have to provide an implementation for base64_decode (or modify the function call to match an existing implementation).