save(char* filename)

G

George

This program need to draw the some triangles into a 512 × 512 buffer
(in memory). Or save it to a file.

#include "project3.h"


Image::Image(int xres, int yres): xres(xres), yres(yres)
{
image =new Color*[yres];
for (int i=0;i<yres;i++)
image = new Color[xres];
}


Image::~Image()
{
if(image)
{
for (int i=0;i<yres;i++)
delete[] image;

delete[] image;
}
}


void Image::save(char* filename)
{

}



#ifndef IMAGE_H
#define IMAGE_H 1

/* A Color is a RGB float.
**
** R, G, and B are all in the range [0..1]
**
** This class allows you to add, subtract, and multiply colors,
** It also allows you to get the separate components (e.g.,
myColor.red() ),
** use constructions like "myColor += yourColor;"
*/
class Color
{
float r,g,b;
public:
inline Color(): r(0), g(0), b(0) {}
inline Color(float r, float g, float b) : r(r), g(g), b(b){}
inline ~Color() {}
inline Color operator*(const Color& c) const
{
return Color(r*c.r, g*c.g, b*c.b);
}
inline Color operator+(const Color& c) const
{
return Color(r+c.r, g+c.g, b+c.b);
}
inline Color operator-(const Color& c) const
{
return Color(r-c.r, g-c.g, b-c.b);
}
inline Color operator*(float s) const
{
return Color(r*s, g*s, b*s);
}
inline Color& operator+=(const Color& c)
{
r+=c.r;
g+=c.g;
b+=c.b;
return *this;
}
inline float red() const
{
return r;
}
inline float green() const
{
return g;
}
inline float blue() const
{
return b;
}
inline float luminance() const
{
return (float)(0.3*g + 0.6*r + 0.1*b);
}

inline float max_component() const
{
float temp = (g > r? g : r);
return (b > temp? b : temp);
}
};


/* An image is a collection of xres*yres Colors.
**
** You can write to a pixel by saying "myImage(x,y) = Color(1, 0.5,
0);"
**
** You can save the entire image to a PPM file by calling
myImage.save("output.ppm");
*/
class Image
{
float* buf;
Color** image;
int xres, yres;
public:
Image(int xres, int yres);
~Image();
inline int getXRes() const
{
return xres;
}
inline int getYRes() const
{
return yres;
}
inline Color& operator()(int x, int y)
{
return image[y][x];
}
void save(char* file);
};

#endif
 
V

Victor Bazarov

George said:
This program need to draw the some triangles into a 512 × 512 buffer
(in memory). Or save it to a file.

If you're seeking comments, see below. If you're not, explain what it
is you want next time.
#include "project3.h"


Image::Image(int xres, int yres): xres(xres), yres(yres)
{
image =new Color*[yres];
for (int i=0;i<yres;i++)
image = new Color[xres];
}


Image::~Image()
{
if(image)
{
for (int i=0;i<yres;i++)
delete[] image;

delete[] image;
}
}


Read about "The Rule of Three".
void Image::save(char* filename)

If you intend to provide the file name as a string literal (in double
quotes), then I strongly recommend you use 'const char*' as your argument
instead of a pointer to non-const char:

void Image::save(const char* filename)

Also it would be nice if it has either 'bool' or 'int' return value to
indicate the success or failure or threw an exception...
{

}



#ifndef IMAGE_H
#define IMAGE_H 1

/* A Color is a RGB float.
**
** R, G, and B are all in the range [0..1]
**
** This class allows you to add, subtract, and multiply colors,
** It also allows you to get the separate components (e.g.,
myColor.red() ),
** use constructions like "myColor += yourColor;"
*/
class Color
{
float r,g,b;
public:
inline Color(): r(0), g(0), b(0) {}
inline Color(float r, float g, float b) : r(r), g(g), b(b){}
inline ~Color() {}
inline Color operator*(const Color& c) const
{
return Color(r*c.r, g*c.g, b*c.b);
}
inline Color operator+(const Color& c) const
{
return Color(r+c.r, g+c.g, b+c.b);

Watch out for overflow. If this->r is 0.8 and c.r is 0.7, the resulting
Color will have r == 1.5, which is definitely not in the range [0..1].
}
inline Color operator-(const Color& c) const
{
return Color(r-c.r, g-c.g, b-c.b);

Same notion here. If 'c.r' is smaller than 'this->r', you can slip into
the negative values...
}
inline Color operator*(float s) const
{
return Color(r*s, g*s, b*s);

Same here. No checking apparently is done. You desperately need to make
sure the results are in the range.
}
inline Color& operator+=(const Color& c)
{
r+=c.r;
g+=c.g;
b+=c.b;

Same here.
return *this;
}
inline float red() const
{
return r;
}
inline float green() const
{
return g;
}
inline float blue() const
{
return b;
}
inline float luminance() const
{
return (float)(0.3*g + 0.6*r + 0.1*b);
}

inline float max_component() const
{
float temp = (g > r? g : r);
return (b > temp? b : temp);
}
};


/* An image is a collection of xres*yres Colors.
**
** You can write to a pixel by saying "myImage(x,y) = Color(1, 0.5,
0);"
**
** You can save the entire image to a PPM file by calling
myImage.save("output.ppm");
*/
class Image
{
float* buf;
^^^^^^^^^^^
This member variable doesn't seem to be used...
Color** image;
int xres, yres;
public:
Image(int xres, int yres);
~Image();
inline int getXRes() const
{
return xres;
}
inline int getYRes() const
{
return yres;
}
inline Color& operator()(int x, int y)
{
return image[y][x];
}
void save(char* file);
};

#endif

V
 
G

George

Sorry to be confusing but I was just wondering how I should go about
creating the void save(char* file).
 
P

Puppet_Sock

George said:
Sorry to be confusing but I was just wondering how I should go about
creating the void save(char* file).

When I dream, I have a pony.
Socks
 
V

Victor Bazarov

George said:
Sorry to be confusing but I was just wondering how I should go about
creating the void save(char* file).

You should open a file stream for output, then output the data the way
you need it to be in the file, then close the stream. Then return from
the function. Or just return without closing, it will close itself.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top