The platform independent interface for image buffers.
Image buffers depend on the platform (see Images and Backgrounds in Dw), but have this general, platform independent interface. The purpose of an image buffer is
storing the image data,
handling scaled versions of this buffer, and
The latter must be done independently from the window.
Image buffers are created by calling dw::core::Platform::createImgbuf.
Storing Image Data
dw::core::Imgbuf supports five image types, which are listed in the table below. The representation defines, how the colors are stored within the data, which is passed to dw::core::Imgbuf::copyRow.
The last two types need a colormap, which is set by dw::core::Imgbuf::setCMap, which must be called before dw::core::Imgbuf::copyRow. This function expects the colors as 32 bit unsigned integers, which have the format 0xrrbbgg (for indexed images), or 0xaarrggbb (for indexed alpha), respectively.
The buffer with the original size, which was created by dw::core::Platform::createImgbuf, is called root buffer. Imgbuf provides the ability to scale buffers. Generally, both root buffers, as well as scaled buffers, may be shared, memory management is done by reference counters.
Via dw::core::Imgbuf::getScaledBuf, you can retrieve a scaled buffer. Generally, something like this must work always, in an efficient way:
int width, heigt,
oldBuf = curBuf;
oldBuf may both be a root buffer, or a scaled buffer.
The root buffer keeps a list of all children, and all methods operating on the image data (dw::core::Imgbuf::copyRow and dw::core::Imgbuf::setCMap) are delegated to the scaled buffers, when processed, and inherited, when a new scaled buffer is created. This means, that they must only be performed for the root buffer.
A possible implementation could be (dw::fltk::FltkImgbuf does it this way):
If the method is called with an already scaled image buffer, this is delegated to the root buffer.
If the given size is the original size, the root buffer is returned, with an increased reference counter.
Otherwise, if this buffer has already been scaled to the given size, return this scaled buffer, with an increased reference counter.
Otherwise, return a new scaled buffer with reference counter 1.
Special care is to be taken, when the root buffer is not used anymore, i.e. after dw::core::Imgbuf::unref the reference counter is 0, but there are still scaled buffers. Since all methods operating on the image data (dw::core::Imgbuf::copyRow and dw::core::Imgbuf::setCMap) are called for the root buffer, the root buffer is still needed, and so must not be deleted at this point. This is, how dw::fltk::FltkImgbuf solves this problem:
In the following example:
the root buffer is not deleted, when dw::core::Imgbuf::unref is called, since a scaled buffer is left. After calling dw::core::Imgbuf::unref for the scaled buffer, it is deleted, and after it, the root buffer.
dw::core::Imgbuf provides no methods for drawing, instead, this is done by the views (implementation of dw::core::View).
There are two situations, when drawing is necessary:
To react on expose events, the function dw::core::View::drawImage should be used, with the following parameters:
of course, the image buffer,
where the root of the image would be displayed (as xRoot and yRoot), and
the region within the image, which should be displayed (x, y, width, height).
When a row has been copied, it has to be drawn. To determine the area, which has to be drawn, the dw::core::Imgbuf::getRowArea should be used. The result can then passed to dw::core::View::drawImage.
- See Also
- Images and Backgrounds in Dw