freeimage.h
FreeImage_SaveMultiBitmapToMemory
DLL_API BOOL DLL_CALLCONV FreeImage_SaveMultiBitmapToMemory(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP *bitmap, FIMEMORY *stream, int flags);
Guarda un mapa de bits multipágina en el manipulador de memoria especificado. El manipulador debe ser asignado en la posición correcta antes de llamar a la función.
BOOL testSaveMultiBitmapToMemory(const char *input, const char *output, int
output_flag) {
BOOL bSuccess;
BOOL bCreateNew = FALSE;
BOOL bReadOnly = TRUE;
BOOL bMemoryCache = TRUE;
// Open src file (read-only, use memory cache)
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(input);
FIMULTIBITMAP *src = FreeImage_OpenMultiBitmap(fif, input, bCreateNew, bReadOnly,
bMemoryCache);
if(src) {
// open and allocate a memory stream
FIMEMORY *dst_memory = FreeImage_OpenMemory();
// save the file to memory
bSuccess = FreeImage_SaveMultiBitmapToMemory(fif, src, dst_memory, output_flag);
assert(bSuccess);
// src is no longer needed: close and free src file
FreeImage_CloseMultiBitmap(src, 0);
// get the buffer from the memory stream
BYTE *mem_buffer = NULL;
DWORD size_in_bytes = 0;
bSuccess = FreeImage_AcquireMemory(dst_memory, &mem_buffer, &size_in_bytes);
assert(bSuccess);
// save the buffer in a file stream
FILE *stream = fopen(output, "wb");
if(stream) {
fwrite(mem_buffer, sizeof(BYTE), size_in_bytes, stream);
fclose(stream);
}
// close and free the memory stream
FreeImage_CloseMemory(dst_memory);
return TRUE;
}
return FALSE;
}
El siguiente ejemplo muestra como modificar un canal de memoria de entrada y guardar las modificaciones en un canal de memoria de salida. El canal de entrada se deja sin modificar: todas las modificaciones son almacenadas en un caché; ese caché es entonces usado para aplicar las modificaciones al guardar.
static BOOL
loadBuffer(const char *lpszPathName, BYTE **buffer, DWORD *length) {
struct stat file_info;
int result;
// get data associated with lpszPathName
result = stat(lpszPathName, &file_info);
if(result == 0) {
// allocate a memory buffer and load temporary data
*buffer = (BYTE*)malloc(file_info.st_size * sizeof(BYTE));
if(*buffer) {
FILE *stream = fopen(lpszPathName, "rb");
if(stream) {
*length = (DWORD)fread(*buffer, sizeof(BYTE), file_info.st_size, stream);
fclose(stream);
return TRUE;
}
}
}
return FALSE;
}
BOOL testMemoryStreamMultiPageOpenSave(const char *lpszPathName, char *output, int input_flag, int output_flag) {
BOOL bSuccess = FALSE;
BYTE *buffer = NULL;
DWORD buffer_size = 0;
// load source stream as a buffer, i.e.
// allocate a memory buffer and load temporary data
bSuccess = loadBuffer(lpszPathName, &buffer, &buffer_size);
assert(bSuccess);
// attach the binary data to a memory stream
FIMEMORY *src_stream = FreeImage_OpenMemory(buffer, buffer_size);
assert(src_stream);
// open the multipage bitmap stream
FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(src_stream, 0);
FIMULTIBITMAP *src = FreeImage_LoadMultiBitmapFromMemory(fif, src_stream, input_flag);
// apply some modifications (everything being stored to the cache) ...
if(src) {
// get the page count
int count = FreeImage_GetPageCount(src);
assert(count > 2);
// Load the bitmap at position '2'
FIBITMAP *dib = FreeImage_LockPage(src, 2);
if(dib) {
FreeImage_Invert(dib);
// Unload the bitmap (apply change to src, modifications are stored to the
cache)
FreeImage_UnlockPage(src, dib, TRUE);
}
// delete page 0 (modifications are stored to the cache)
FreeImage_DeletePage(src, 0);
// insert a new page at position '0' (modifications are stored to the cache)
FIBITMAP *page = createZonePlateImage(512, 512, 128);
FreeImage_InsertPage(src, 0, page);
FreeImage_Unload(page);
}
// save the modification into the output stream ...
if(src) {
// open and allocate a memory stream
FIMEMORY *dst_stream = FreeImage_OpenMemory();
assert(dst_stream);
// save the file to memory
FreeImage_SaveMultiBitmapToMemory(fif, src, dst_stream, output_flag);
// src is no longer needed
// close and free the memory stream
FreeImage_CloseMemory(src_stream);
// close and free src file (nothing is done, the cache is cleared)
FreeImage_CloseMultiBitmap(src, 0);
// at this point, the input buffer is no longer needed
// !!! user is responsible for freeing the initial source buffer !!!
free(buffer); buffer = NULL;
// get the dst buffer from the memory stream
BYTE *dst_buffer = NULL;
DWORD size_in_bytes = 0;
FreeImage_AcquireMemory(dst_stream, &dst_buffer, &size_in_bytes);
// save the buffer in a file stream
FILE *stream = fopen(output, "wb");
if(stream) {
fwrite(dst_buffer, sizeof(BYTE), size_in_bytes, stream);
fclose(stream);
}
// close and free the memory stream
FreeImage_CloseMemory(dst_stream);
return TRUE;
}
if(buffer) {
free(buffer);
}
return FALSE;
}