1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
void testSaveMemIO(const char *lpszPathName) {
FIMEMORY *hmem = NULL;
// load and decode a regular file
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);
// open a memory stream
hmem = FreeImage_OpenMemory();
// encode and save the image to the memory
FreeImage_SaveToMemory(fif, dib, hmem, 0);
// at this point, hmem contains the entire data in memory stored in fif format.
// the amount of space used by the memory is equal to file_size
long file_size = FreeImage_TellMemory(hmem);
printf("File size : %ld\n", file_size);
// its easy to load an image from memory as well
// seek to the start of the memory stream
FreeImage_SeekMemory(hmem, 0L, SEEK_SET);
// get the file type
FREE_IMAGE_FORMAT mem_fif = FreeImage_GetFileTypeFromMemory(hmem, 0);
// load an image from the memory handle
FIBITMAP *check = FreeImage_LoadFromMemory(mem_fif, hmem, 0);
// save as a regular file
FreeImage_Save(FIF_PNG, check, "dump.png", PNG_DEFAULT);
// make sure to close the stream since FreeImage_SaveToMemory
// will cause internal memory allocations and this is the only
// way to free this allocated memory
FreeImage_CloseMemory(hmem);
FreeImage_Unload(check);
FreeImage_Unload(dib);
} |
Partager