O
olivier.scalbert
Hello,
I would like to create an Image class. A use of this class could be
something as:
Image image1(1000, 1000);
Image image2(2000, 1000);
Color3d color;
image1.fill(color);
image2.fill(color);
Here is the code of the class:
struct Color3d
{
double r,g,b;
}
class Image
{
public:
Image(int width, int height);
~Image();
void fill(Color3d color);
private:
int _width;
int _height;
Color3d _pixels[][]; // Does not work !
};
Image::Image(int width, int height) : _width(width), _height(height)
{
_pixels = new Color3d[width][height]; // Does not work !
}
Image::~Image()
{
delete [][] _pixels; // Does not work
}
void Image::fill(Color3d color)
{
for (int y = 0; y < _height; y++)
{
for (int x = 0; x < _width; x++)
{
_pixels[x][y] = color;
}
}
}
As you can imagine, I have some problems around the pixels definition.
I see two alternatives:
- using templates;
- allocate a single dimension array of pixels and manage the
coordinates conversion myself.
I am sure there are some more elegant solutions, but I have no ideas.
Thanks to help me,
Olivier
I would like to create an Image class. A use of this class could be
something as:
Image image1(1000, 1000);
Image image2(2000, 1000);
Color3d color;
image1.fill(color);
image2.fill(color);
Here is the code of the class:
struct Color3d
{
double r,g,b;
}
class Image
{
public:
Image(int width, int height);
~Image();
void fill(Color3d color);
private:
int _width;
int _height;
Color3d _pixels[][]; // Does not work !
};
Image::Image(int width, int height) : _width(width), _height(height)
{
_pixels = new Color3d[width][height]; // Does not work !
}
Image::~Image()
{
delete [][] _pixels; // Does not work
}
void Image::fill(Color3d color)
{
for (int y = 0; y < _height; y++)
{
for (int x = 0; x < _width; x++)
{
_pixels[x][y] = color;
}
}
}
As you can imagine, I have some problems around the pixels definition.
I see two alternatives:
- using templates;
- allocate a single dimension array of pixels and manage the
coordinates conversion myself.
I am sure there are some more elegant solutions, but I have no ideas.
Thanks to help me,
Olivier