Question
Add the following so that variables colorfulPalette, grayPalette, and palette will be properly allocated/deallocated and copied. Destructor Copy constructor Assignment operator (correctly handle chaining of
Add the following so that variables colorfulPalette, grayPalette, and palette will be properly allocated/deallocated and copied.
Destructor Copy constructor Assignment operator (correctly handle chaining of assignment and self-assignment)
Relevant Code:
// Class to hold a color palette of size n. Each color has a number // (0...n-1) and a corresponding RGB value. class Palette { public: // Initialize palette to hold a max. number of colors // (all black to start). Palette(int initMaxColors);
// Add a color to the palette (if room). The color will be // converted to RGB to be stored internally. void addColor(const Color& color);
// Convert an indexed color (0...n-1), where n is the number // of colors in the palette, to RGB color. void getColor(int colorIndex, int& red, int& green, int& blue) const;
// Convert all colors stored in palette to grayscale. void convertToGrayscale();
private: // Dynamic array of colors in palette. Rgb* colors;
// Size of dynamic array. int maxColors;
// Number of colors actually stored in array // (incremented when add a color). int numColors; };
// Read one color (RGB or HSV) from the file stream and add it to the palette. // Report back whether a color could be read. bool readColor(istream& colorStream, Palette& palette);
// Draw image of a specific size (height x width) that contains indexed colors // on an SDL canvas. Color indices are looked up in the palette passed to // determine actual RGB color to use for each pixel. void drawImage(SDL_Surface* surface, int image[], int height, int width, const Palette& palette);
int main(int argc, char* args[]) { // Open file containing colors (RGB or HSV). const char COLOR_FILENAME[] = "colors.txt"; ifstream colorStream(COLOR_FILENAME);
// Read number of colors in file and make palette of appropriate size. int maxColors; colorStream >> maxColors; Palette colorfulPalette(maxColors);
// Read each color from file stream. while (readColor(colorStream, colorfulPalette)) ;
// Make copy of color palette, then convert all its colors to grayscale. Palette grayPalette(colorfulPalette); grayPalette.convertToGrayscale();
// Window in which to display image. SDL_Window* window = NULL;
// Drawing surface of window. SDL_Surface* screenSurface = NULL;
// Initialize graphics (SDL) subsystems. if (SDL_Init(SDL_INIT_VIDEO) < 0) { cout << "SDL could not initialize! Error: " << SDL_GetError() << endl; return 0; }
// Create window used to display image. window = SDL_CreateWindow("Image Preview", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, IMAGE_WIDTH, IMAGE_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { cout << "Window could not be created! Error: " << SDL_GetError() << endl; return 0; }
// Get surface of window. screenSurface = SDL_GetWindowSurface(window);
// Current palette used to display image (initially the colorful version). // Realistically, should just point to the palette to use, but we need to // test copying/assignment. Palette palette(colorfulPalette);
// Allow user to enter commands to choose color palette used to display image. char option;
do { // Display image translating indexed color to colors from palette. drawImage(screenSurface, image, IMAGE_HEIGHT, IMAGE_WIDTH, palette);
// Update the surface to display color. SDL_UpdateWindowSurface(window);
// Allow user to enter commmands to test color palettes. cout << endl; cout << "Preview image using which palette?" << endl; cout << "c)olor" << endl; cout << "g)rayscale" << endl; cout << "ex)it" << endl; cout << "Palette: "; cin >> option;
switch (option) { case 'c': // color palette = colorfulPalette; break;
case 'g': // grayscale palette = grayPalette; break;
case 'x': // exit break;
default: cout << "Unknown palette: " << option << endl; break; } } while (option != 'x'); // exit?
// Destroy window. SDL_DestroyWindow(window);
// Quit graphics (SDL) subsystems. SDL_Quit();
return 0; }
// Palette member definitions.
// Initialize palette to hold a max. number of colors // (all black to start). Palette::Palette(int initMaxColors) : maxColors(initMaxColors), numColors(0) { // Allocate array to store colors. colors = new Rgb[maxColors];
// Initialize all colors to black. for (int i = 0; i < maxColors; i++) colors[i] = { 0, 0, 0 }; }
// Add a color to the palette (if room). The color will be // converted to RGB to be stored internally. void Palette::addColor(const Color& color) { if (numColors < maxColors) { // How color is converted to RGB should depend on which type of // color class object is passed (polymorphism)! color.getRgb(colors[numColors].red, colors[numColors].green, colors[numColors].blue); numColors++; } }
// Convert an indexed color (0...n-1), where n is the number // of colors in the palette, to RGB color. void Palette::getColor(int colorIndex, int& red, int& green, int& blue) const { if (colorIndex < numColors) { red = colors[colorIndex].red; green = colors[colorIndex].green; blue = colors[colorIndex].blue; } }
// Convert all colors stored in palette to grayscale. void Palette::convertToGrayscale() { for (int i = 0; i < numColors; i++) { // Use the average method to determine gray level. int grayLevel = (colors[i].red + colors[i].green + colors[i].blue) / 3; colors[i].red = grayLevel; colors[i].green = grayLevel; colors[i].blue = grayLevel; } }
// Read one color (RGB or HSV) from the file stream and add it to the palette. // Report back whether a color could be read. bool readColor(istream& colorStream, Palette& palette) { string type; // type of color ("RGB" or "HSV")
// FILL IN FUNCTION.
return false; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started