By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

save(char* filename)

P: n/a
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[i] = new Color[xres];
}
Image::~Image()
{
if(image)
{
for (int i=0;i<yres;i++)
delete[] image[i];

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

Sep 7 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
George wrote:
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[i] = new Color[xres];
}
Image::~Image()
{
if(image)
{
for (int i=0;i<yres;i++)
delete[] image[i];

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
Sep 7 '05 #2

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

Sep 7 '05 #3

P: n/a
George wrote:
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

Sep 7 '05 #4

P: n/a
"George" <bu*******@hotmail.com> writes:
Sorry to be confusing but I was just wondering how I should go about
creating the void save(char* file).


Have a look at the "Thinking in C++" books, especially Vol. 2, Ch. 4, which
discusses C++'s standard iostream classes.

<http://www.mindview.net/Books>

Also of interest is the group FAQ, esp. section 15:

<http://www.parashift.com/c++-faq-lite/>

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Sep 7 '05 #5

P: n/a
George wrote:
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
Sep 7 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.