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
(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