[SciPy-User] FreeImage <-> numpy IO wrappers

Zachary Pincus zachary.pincus at yale.edu
Wed May 12 13:10:19 EDT 2010


> Do you know if FreeImage does anything via memory-mapping ? I'm mostly
> interested in TIFF-memmap, which exists according to libtif, but I
> have now idea how useful it is .....  (I need memmap for GB-size
> multipage images)

I don't know a ton about how memmapping works, but check out these  
functions from FreeImage:

> FreeImage_OpenMemory
> DLL_API FIMEMORY *DLL_CALLCONV FreeImage_OpenMemory(BYTE *data  
> FI_DEFAULT(0), DWORD
> size_in_bytes FI_DEFAULT(0));
>
> Open a memory stream. The function returns a pointer to the opened  
> memory stream.
> When called with default arguments (0), this function opens a memory  
> stream for read / write
> access. The stream will support loading and saving of FIBITMAP in a  
> memory file (managed
> internally by FreeImage). It will also support seeking and telling  
> in the memory file.
> This function can also be used to wrap a memory buffer provided by  
> the application driving
> FreeImage. A buffer containing image data is given as function  
> arguments data (start of the
> buffer) and size_in_bytes (buffer size in bytes). A memory buffer  
> wrapped by FreeImage is
> read only. Images can be loaded but cannot be saved.

> FreeImage_LoadFromHandle
> DLL_API FIBITMAP *DLL_CALLCONV  
> FreeImage_LoadFromHandle(FREE_IMAGE_FORMAT fif,
> FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));
>
> FreeImage has the unique feature to load a bitmap from an arbitrary  
> source. This source
> might for example be a cabinet file, a zip file or an Internet  
> stream. Handling of these arbitrary
> sources is not directly handled in the FREEIMAGE.DLL, but can be  
> easily added by using a
> FreeImageIO structure as defined in FREEIMAGE.H.
> FreeImageIO is a structure that contains 4 function pointers: one to  
> read from a source, one
> to write to a source, one to seek in the source and one to tell  
> where in the source we currently
> are. When you populate the FreeImageIO structure with pointers to  
> functions and pass that
> structure to FreeImage_LoadFromHandle, FreeImage will call your  
> functions to read, seek
> and tell in a file. The handle-parameter (third parameter from the  
> left) is used in this to
> differentiate between different contexts, e.g. different files or  
> different Internet streams.

With the first, I think you could just pass the void* pointer returned  
from memmapping a file; with the second, I think you could wrap a  
memmapped file with a file-like interface (implemented in python  
callbacks, even). Not sure, of course, if that will work OK... Might  
be easier to work with wrappers to libtiff directly?

Zach




More information about the SciPy-User mailing list