[SDL] Memory problem
s_ridenour at kewlpc.org
Sun Sep 21 04:24:01 PDT 2003
On Thursday 18 September 2003 4:27 am, Nayan Choudhary wrote:
> Hi guys,
> I have a BIG problem. Though I am not doing game programming right
> now, I am making an application which loads two HUGE bitmaps (250MB+
> each) into memory for some purpose. I am using MFC for developing it.
> Do you have any idea how it can be done without taking a lot of time
> and lot of memory?? I have 256 MB RAM.
> My app was able to load 60 MB bitmap image easily. But images of
> sizes 150 MB bug my computer. In future I will be handling images of
> size like 2GB or so.
> I know that large data can be managed by loading small chunks and
> handling them instead of the whole data. But unfortunately I am new
> to such tricks. Any nice suggestion? Any sample code will help me a
First of all, what exactly will your program do?
Part of the solution could depend on the image format you use. Some formats
will let you load directly from them in pieces (like the SGI image format,
which you can read in one scanline at a time).
Otherwise, just write a system that will spool the images out to disk as it
loads and decompresses them. I don't know a lot about Windows programming, so
the actual method is left up to you.
The trouble is that most image loading libraries expect to be able to load the
entire image into memory at once. Unless you want to write your own image
loader, you're going to have to find a way around this. And idea I just had
that might work (at least on Linux, dunno about Windows) would be to use
memory mapped files. Basically, memory mapped files can be used like a char
array. So, what you do is instead of passing the image library a nice big
250MB memory buffer for it to load the image into, you pass it a pointer to
your memory mapped file (which the image library will think is a pointer to a
memory buffer). This will be slower than loading directly into memory (unless
you're trying to load a 250MB image on a system with 256MB of RAM, in which
case the speed should be about the same), but the advantage would be that you
get the equivalent of a memory dump of the final, loaded and uncompressed
image. You can then close the memory mapped file, open it as a regular file,
load smaller chunks of this "memory dump" into a fixed-size buffer and do
what you want with them.
The method that programs like The Gimp use is to just bite the bullet and
initially load the entire image into memory (not workable for 2GB images
though). After that, it is broken up into fixed-size tiles, which are written
to a temporary cache on disk and then removed from memory. As the tiles are
needed, they are loaded into memory, used, and then flushed back out to the
temporary disk cache. Obviously, the program needs extra mechanisms to keep
track of which tiles are where in the cache, to keep track of what tiles are
needed when (and when they stop being needed), etc.
Play around, possibly have a look at The Gimp's tile manager code, and find
something that works for you. You might also want to check out CinePaint's
(http://cinepaint.sourceforge.net) tile manager code, since CinePaint was
originally based on an older version of The Gimp, and so its tile manager
code might be simpler and easier to understand.
Hope this helps.
More information about the SDL