/******************************************************** Kyle Nase Section 002 Ed Yakabosky Section 003 Homework 6 - Header file with class definition This class is designed to make simple ascii image art by using a 1-D pointer array. *********************************************************/ #include "timage.h" #include using namespace std; // Default Constructor TImage::TImage() { pixels = 0; nRows = 0; nCols = 0; } /* Constructor Parameters: Length and width of desired array. */ TImage::TImage(int Rows,int Cols) { nRows = Rows; nCols = Cols; // Creates a 1 dimensional // array of pixels. pixels = new int[nRows*nCols]; } // Returns rows int TImage::getRows() const { return nRows; } // Returns columns int TImage::getColumns() const { return nCols; } /* This function returns the 1-D index of a specific cross-section of rows and columns. It multiples the current row by every column and adds the specific column being evaluated. Parameters: a specific row and column */ int TImage::getIndex(int r, int c) const { return (r * nCols + c); } /* This function returns the index pixel value of a given r and c. It returns the pixel value at a specific index by calling the getIndex() function. Parameters: a specific row and column. */ int TImage::getPixel(int r, int c) const { return pixels[getIndex(r, c)]; } /* After choosing a value in the main program, this function assigns the pixel value at a specific r and c to an pixel intensity. It assigns this by calling the getIndex() function at a specific cross-section of rows and columns. Parameters: a specific row and column, value of image intensity. */ void TImage::setPixel(int r, int c, int value) { if (value < 1) pixels[getIndex(r,c)] = 0; else if (value == 1) pixels[getIndex(r,c)] = 1; else if (value == 2) pixels[getIndex(r,c)] = 2; else if (value >= 3) pixels[getIndex(r,c)] = 3; } /* This function translates the numerical value of the pixel intensity to an Ascii art value, in order to display an image. It takes a value and returns a different Ascii art depending on that given input value. Parameters: the image intensity value in the pixel array spot. */ char TImage::toAscii(int value) { if (value < 1) return '.'; else if (value == 1) return ':'; else if (value == 2) return '8'; else return 'M'; } // This function is used to set all pixel values to 0. void TImage::clear() { for (int i = 0; i < (nRows*nCols); i++) pixels[i] = 0; } /* This function is used to create a new image by deleting the original array, setting the new rows and cols to nRows and nCols and then allocates a new pointer array for the image. Parameters: the number of rows and columns in the image. */ void TImage::newImage(int Rows, int Cols) { delete [] pixels; nRows = Rows; nCols = Cols; pixels = new int[nRows*nCols]; } /* This function draws images by making boxes by entering all coordinates one by one and then by calling setPixel. Parameters: top left row, top left col, bottom right row, bottom right col and the image intensity value. */ void TImage::drawRect(int r1, int c1, int r2, int c2, int value) { for (int j = r1; j <= r2; j++) for (int k = c1; k <= c2; k++) setPixel(j, k, value); } // This function displays the raw information, or number table of intensities. void TImage::displayRaw() { for (int j = 0; j < nRows; j++) { for (int k = 0; k < nCols; k++) cout << getPixel(j, k); cout << endl; } } /* This function displays the ascii art by using numerical intensities and translating them into ascii values by calling toAscii() at specific locations. */ void TImage::displayAscii() { for (int j = 0; j < nRows; j++) { for (int k = 0; k < nCols; k++) cout << toAscii(pixels[getIndex(j, k)]); cout << endl; } } // Destructor TImage::~TImage() { delete [] pixels; }