M
mast4as
Hi everyone
I am trying to design some code but can't seem to find the best way of
doing this. Basically I am trying to create an image class. The class
has 4 member variables, the number of channels (how many planes define
the image, for example if it's a B&W image m_numChannels = 1 & if it's
an RGB image then m_numChannels = 3). There's also its width, height
and finally a pointer to the data (stored as float). So the class
looks like this:
class Image
{
public:
uint m_numChannels;
uint m_width;
uint m_height;
float *m_data;
Image(const int &w, const int &h, const int nchannels) :
m_width(w), m_height(h), m_numChannels(nchannels)
{
m_data = new float [nchannels * w * h];
}
~Image()
{
if (m_data) delete [] m_data;
}
/*
Vector<> Interpolate(const float &u, const float &v)
{
}
*/
};
Now what I would like to do is to create functions within the class
that process each pixel of the image. If the image was an RGB image
and always had 3 channels I could use a class Color defined as a set
of 3 floats and have each process function could return a Color.
for (i = 0; i < image->m_height; ++i)
for (j = 0; j < image->m_width; ++j)
Color pixel = image->Process(i, j);
The problem is that an image can have an arbitrary number of channels
so we don't to limit us to returning color. Ideally I would like to
return a Vector who size is the same as the number of channels in the
image. Something like that:
template<uint size>
class Vector
{
public:
float m_data[size];
...
}
for (i = 0; i < image->m_height; ++i)
for (j = 0; j < image->m_width; ++j)
Vector<3> pixel = image->Process(i, j);
That seems like a good idea the problem is that I don't know how to
write the Process method in my image class such as it knows how to
return a Vector which size equal to the number of channels in the
image ? I wonder if I have to turn the image class in a template ?
Would anyone have some experience with a similar problem and could
please give me some advices ?
Thanks a lot -
-coralie
I am trying to design some code but can't seem to find the best way of
doing this. Basically I am trying to create an image class. The class
has 4 member variables, the number of channels (how many planes define
the image, for example if it's a B&W image m_numChannels = 1 & if it's
an RGB image then m_numChannels = 3). There's also its width, height
and finally a pointer to the data (stored as float). So the class
looks like this:
class Image
{
public:
uint m_numChannels;
uint m_width;
uint m_height;
float *m_data;
Image(const int &w, const int &h, const int nchannels) :
m_width(w), m_height(h), m_numChannels(nchannels)
{
m_data = new float [nchannels * w * h];
}
~Image()
{
if (m_data) delete [] m_data;
}
/*
Vector<> Interpolate(const float &u, const float &v)
{
}
*/
};
Now what I would like to do is to create functions within the class
that process each pixel of the image. If the image was an RGB image
and always had 3 channels I could use a class Color defined as a set
of 3 floats and have each process function could return a Color.
for (i = 0; i < image->m_height; ++i)
for (j = 0; j < image->m_width; ++j)
Color pixel = image->Process(i, j);
The problem is that an image can have an arbitrary number of channels
so we don't to limit us to returning color. Ideally I would like to
return a Vector who size is the same as the number of channels in the
image. Something like that:
template<uint size>
class Vector
{
public:
float m_data[size];
...
}
for (i = 0; i < image->m_height; ++i)
for (j = 0; j < image->m_width; ++j)
Vector<3> pixel = image->Process(i, j);
That seems like a good idea the problem is that I don't know how to
write the Process method in my image class such as it knows how to
return a Vector which size equal to the number of channels in the
image ? I wonder if I have to turn the image class in a template ?
Would anyone have some experience with a similar problem and could
please give me some advices ?
Thanks a lot -
-coralie