freeimage.h

FreeImage_SaveMultiBitmapToHandle

DLL_API BOOL DLL_CALLCONV FreeImage_SaveMultiBitmapToHandle(FREE_IMAGE_FORMAT fif,
FIMULTIBITMAP *bitmap, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));

Guarda un mapa de bits multipágina en el manipulador especificado. El manipulador debe ser colocado en la posición correcta antes de llamar a la función.

Al igual que FreeImage_SaveToHandle, se debe especificar un puntero a una estructura FreeImageIO y un fi_handle.

BOOL testStreamMultiPageSave(const char *input, const char *output, int input_flag,
int output_flag) {
    // initialize your own IO functions
    FreeImageIO io;
    io.read_proc = myReadProc;
    io.write_proc = myWriteProc;
    io.seek_proc = mySeekProc;
    io.tell_proc = myTellProc;
    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, input_flag);

    if(src) {
        // Open dst stream in read/write mode
        FILE *file = fopen(output, "w+b");
        if (file != NULL) {
            // Save the multi-page file to the stream
            BOOL bSuccess = FreeImage_SaveMultiBitmapToHandle(fif, src, &io,
            (fi_handle)file, output_flag);
            assert(bSuccess);
            // Close the dst stream
            fclose(file);
            // Close src file
            FreeImage_CloseMultiBitmap(src, 0);
            return TRUE;
        }

        // Close src
        FreeImage_CloseMultiBitmap(src, 0);
    }
    return FALSE;
}

El siguiente ejemplo muestra cómo modificar un canal de entrada y cuardar las modificaciones en un canal de salida. Nótese que el canal de entrada permanece inalterado: todas las modificaciones se almacenan en un caché; éste caché es entoces usado para aplicar las modificaciones al guardar.

BOOL testStreamMultiPageOpenSave(const char *input, const char *output, int
input_flag, int output_flag) {
    // initialize your own IO functions
    FreeImageIO io;
    io.read_proc = myReadProc;
    io.write_proc = myWriteProc;
    io.seek_proc = mySeekProc;
    io.tell_proc = myTellProc;
    BOOL bSuccess = FALSE;
    // Open src stream in read-only mode
    FILE *src_file = fopen(input, "r+b");
    assert(src_file);
    if (src_file != NULL) {
        // Open the multi-page file
        FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromHandle(&io, (fi_handle)src_file);
        FIMULTIBITMAP *src = FreeImage_OpenMultiBitmapFromHandle(fif, &io,
            (fi_handle)src_file, input_flag);
        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);
            // Open dst stream in read/write mode
            FILE *dst_file = fopen(output, "w+b");
            assert(dst_file);
            if (dst_file != NULL) {
                // Save the multi-page file to the stream (modifications are applied)
                BOOL bSuccess = FreeImage_SaveMultiBitmapToHandle(fif, src, &io,
                    (fi_handle)dst_file, output_flag);
                assert(bSuccess);
                // Close the dst stream
                fclose(dst_file);
            }
            // Close src file (nothing is done, the cache is cleared)
            bSuccess = FreeImage_CloseMultiBitmap(src, 0);
            assert(bSuccess);
        }
        // Close the src stream
        fclose(src_file);
        return bSuccess;
    }
    return FALSE;
}