P
Philip Pemberton
Hi,
I'm doing some work with the CImg image processing library, and have this
function that applies to a given image:
unsigned char** MapArray(CImg<unsigned char> &image, int channel){
unsigned char **map;
map = new unsigned char*[image.dimy()];
for( int y=0 ; y<image.dimy() ; y++ )
{
map[y] = &image(0,y,0,channel);
}
return map;
}
Briefly, this is necessary because accessing the image directly is
inefficient -- for every scanline, the Y offset in the pixel array is
calculated total_x_pixels times. This is, of course, rather inefficient.
The Y offset should be calculated once per scanline, then the X offset
should be added to it. This is what the function above does.
CImg is a template class -- effectively, you can use any type for your
pixels as long as that type has a few basic operators available (<, >, +,
etc.). A CImg is declared as (eg.) "CImg<unsigned char> image;".
What I'd like to do is make this function less specialised -- turn it
into a template that I can apply to any CImg object based on an
arithmetic type (float, double, int, char, ...)
Now, I've come up with this:
template<class T, class X> X** MapArray(T &image, int channel)
{
X **map = new X*[image.height()];
for( int y=0 ; y<image.height() ; y++ )
{
map[y] = &image(0,y,0,channel);
}
return map;
}
which I can call like this:
basefloat **rmap = MapArray<CImg<basefloat>, basefloat>(image,
C_RED);
What I'd rather have is something like:
basefloat **rmap = MapArray<CImg<basefloat>>(image, C_RED);
Which eliminates the second instance of basefloat, saving me typing and
the potential for a ****-up when refactoring code to use a different type
for image pixels.
The question is, does anyone know how I can do what I want to do?
Alternatively, is there a cleaner/tidier/"better" way to do this?
Thanks,
Phil.
I'm doing some work with the CImg image processing library, and have this
function that applies to a given image:
unsigned char** MapArray(CImg<unsigned char> &image, int channel){
unsigned char **map;
map = new unsigned char*[image.dimy()];
for( int y=0 ; y<image.dimy() ; y++ )
{
map[y] = &image(0,y,0,channel);
}
return map;
}
Briefly, this is necessary because accessing the image directly is
inefficient -- for every scanline, the Y offset in the pixel array is
calculated total_x_pixels times. This is, of course, rather inefficient.
The Y offset should be calculated once per scanline, then the X offset
should be added to it. This is what the function above does.
CImg is a template class -- effectively, you can use any type for your
pixels as long as that type has a few basic operators available (<, >, +,
etc.). A CImg is declared as (eg.) "CImg<unsigned char> image;".
What I'd like to do is make this function less specialised -- turn it
into a template that I can apply to any CImg object based on an
arithmetic type (float, double, int, char, ...)
Now, I've come up with this:
template<class T, class X> X** MapArray(T &image, int channel)
{
X **map = new X*[image.height()];
for( int y=0 ; y<image.height() ; y++ )
{
map[y] = &image(0,y,0,channel);
}
return map;
}
which I can call like this:
basefloat **rmap = MapArray<CImg<basefloat>, basefloat>(image,
C_RED);
What I'd rather have is something like:
basefloat **rmap = MapArray<CImg<basefloat>>(image, C_RED);
Which eliminates the second instance of basefloat, saving me typing and
the potential for a ****-up when refactoring code to use a different type
for image pixels.
The question is, does anyone know how I can do what I want to do?
Alternatively, is there a cleaner/tidier/"better" way to do this?
Thanks,
Phil.