Writing Image IO Plug-ins
 
 
 

Image IO plug-ins load and save the various file types are derived from class BitmapIO. Examples of this type of plug-in are the GIF, FLC and JPG IO modules. When writing these kind of plug-ins the memory handling of bitmaps requires special attention.

The memory allocated to a bitmap is managed internally by an instance of BitmapStorage. This class provides access to the pixels through a uniform interface.

When a developer creates an image, the memory is allocated by the system. As long as the bitmap is being used within the system, the bitmap remains allocated. This is handled internally by a usage counter that tracks if a bitmap is still being used. If another use of the bitmap takes place, the usage count is incremented. If the use of a bitmap ends, the usage count is decremented. When the usage count for the bitmap goes to zero, the system frees the memory. This happens automatically without intervention from the plug-in.

For example, once a bitmap is loaded (this is never the case when a bitmap is created), a storage for the image is created to hold the actual bitmap. If a second attempt to open this same bitmap is made (the same file and the same frame), instead of creating a new storage, the bitmap is created pointing to the existing storage. In the storage a counter is incremented to tell how many bitmaps are using it. When a developer deletes the bitmap (i.e. delete MyMap;) the destructor calls the storage and asks to be unlinked from the storage. The storage decrements the usage count and if it reaches 0, the storage itself is also deleted.

Note that this is only the case when a bitmap is loaded because when you create a bitmap, it doesn't yet exist in the file system. There is no way for someone else to "open" it. This is why when you create a bitmap, it is said this is a "WRITE ONLY" bitmap, and when you load a bitmap it is said this is a "READ ONLY" bitmap. In order to read and write a bitmap you must load the original bitmap, create a second, copy the data (doing whatever processing in between), and then write the newly created bitmap.