Boost logo

Boost :

From: Lubomir Bourdev (lbourdev_at_[hidden])
Date: 2006-10-11 17:17:30


> Thorsten wrote:
> > GIL pixels/channels should never advertise that they have larger
> > capacity than they actually do.
> >
> How else would you then implement paletted images?

First, a bit of a background for people who don't know what paletted
(more often called indexed) images are. An indexed image stores the
color as 1-dimensional index to a lookup table that contains the actual
values of the color.

For example, consider this lookup table:
0 [255 0 0] // red
1 [128 128 128] // gray
2 [0 128 0]
3 [0 0 0] // black
...

Then three pixels with values [0 3 1] correspond to [red black gray]
pixels. One obvious advantage to indexed images is that they provide a
more compact representation of the data.

The way indexed images can be modeled in GIL is using a
PixelDereferenceAdaptor over grayscale image of the appropriate channel
depth. Specifically:

1. Create an immutable pixel adapter that references the lookup table.
Its application operator takes an index and returns an RGB pixel value.
Here is a rough synopsis for a concrete version; you can easily make it
more generic and improve it in other ways:

struct indexed_pixel_deref_fn {
    typedef rgb8_pixel_t value_type;
    static const bool is_mutable=false;
    indexed_pixel_deref_fn(const rgb8_pixel_t* table);
    
    rgb8_pixel_t operator()(const gray8_pixel_t& index) const {
        return _table[index];
    }
    ....
};

2. Create your indexed image by attaching your pixel dereference adapter
on a regular grayscale image:

typedef gray8_view_t::add_deref<indexed_pixel_deref_fn>
indexed_factory_t;

typedef indexed_factory_t::type rgb8_indexed_view_t;

rgb8_indexed_view_t indexed_view= indexed_factory_t::make(my_gray_view,
indexed_pixel_deref_fn(my_index_table));

Now your indexed_view will behave like a regular 8-bit RGB interleaved
view. It will be immutable (unless you make an advanced dereference
adapter that can do some sort of reverse lookup). But you should be able
to copy it to a non-indexed rgb view, compute its gradient, make n-th
channel view of its green channel, save it to a file, etc.

And to your earlier question, notice that the indexed view does not
advertise that it has a larger capacity.

Lubomir


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk