CPSC310A Assignment 1 - Image

Due Wednesday January 25


Create C++ classes for rgb colors and images. The Color class should use three floats with values between 0 and 1 to represents the red, green, and blue channels. Values outside of the range 0 to 1 are not valid for any of the three channels, however, the class should not prevent values from going outside of this range. The Color class should implement all of the functions used in the following code:

Color c1(1.0f, 0.0f, 1.0f);
Color c2(c1);
Color c3 = c1;
c3 = c1 + c2;
c1 += c2;
c3 = c1 - c2;
c1 -= c2;
c3 = c1 * c2;
c3 *= c2;
c3 = c2 * 2.0f;
c3 = 2.0f * c2;
c3 *= 2.0f;
c3 = c1 / c2;
c3 /= c2;
c3 = c2 / 2.0f;
c3 /= 2.0f;
c1 == c2;
cout << "c1.r: " << c1.r() << "\n";
cout << "c1.r: " << c1[0] << "\n";
c1[0] = 0.0f;

The Image class should represent an image using a two-dimensional array of Color objects. The image class should have a constructor that specifies the dimensions of an image, a function to change the color of a pixel in the image, and a function to write a ppm file. These functions are demonstrated in the following code:

int width = 640;
int height = 480;
int row = 0;
int col = 0;
string filename = "untitled.ppm";
Image img(width, height);
Color red(1.0f, 0.0f, 0.0f);
img.SetPixel(row, col, red);
if (!img.WritePPM(filename)) {
    cout << "error writing file: " << filename << "\n";

The WritePPM should return true if successful in writing a binary ppm file. Please refer to the ppm file format specification for details. The color channels in the image must be converted to bytes before being written. These values should be clamped and gamma corrected with a gamma value of 2.2.


Tar your code in a file that contains your name and submit it on the course Inquire site.