21
IN THIS CHAPTER
C++ Concepts
Image Framework Concepts
Image Object (Prototype 1)
Memory Allocation Object
Singleton Object
Reference Counting
Template Primer
Handles and Rep objects
Prototyping Strategy
Templated Image Object (Prototype 2)
Image Storage (Prototype 3)
3
Design
Techniques
In this chapter, we lay the groundwork for extending our digital imaging framework. We
begin by designing a memory allocation object, and then continue with a templates primer
that provides a road map to subtleties of templates and their use. Finally, we apply C++
constructs to specific aspects of the design by creating detailed prototypes, and discussing
the advantages and disadvantages of each technique.
3.1 Memory Allocation
In our test application’s image class described in Section 2.3.1 on page 12, we use the
operators new and delete to allocate and free storage for our image pixels in
apImage::init() and apImage::cleanup(). Our test application employs this simple
memory management scheme to demonstrate that it can be trivial to come up with a
working solution. This simple mechanism, however, is very inefficient and breaks down
quickly as you try to extend it. Managing memory is critical for a fully functional image
framework. Therefore, before we delve into adding functionality, we will design an object
that performs and manages memory allocation.
3.1.1 Why a Memory Allocation Object Is Needed
Images require a great deal of memory storage to hold the pixel data. It is very inefficient to
copy these images, in terms of both memory storage and time, as the images are
manipulated and processed. You can easily run out of memory if there are a large number of
images. In addition, the heap could become fragmented if there isn’t a large enough block
of memory left after all of the allocations.
You really have to think about the purpose of an image before duplicating it. Duplication of
image data should only happen when there is a good reason to retain a copy of the image
(for example, you want to keep the original image and t