Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need to add a menu to my program: http://pastebin.com/iYx3a6Ny Add a simple me

ID: 3544432 • Letter: I

Question

i need to add a menu to my program: http://pastebin.com/iYx3a6Ny

Add a simple menu to your program, prompting the user for what operation he/she would like to perform. Something along the lines of this - the user's keyboard input is highlighted: The first menu option to copy the image file is what you just did in step 1. Option 2, "Grayscale", converts the input image into grayscale and writes the resulting image to the output file. Conversion to grayscale is done by averaging the RGB values for a pixel, the red, green and blue, and then replacing them all by that average. So if the RGB values were 25 75 250, the average would be 116, and then all three RGB values would become 116 - i.e. 116 116 116. Note that you can do integer division, and if the computer truncates the result, that's fine; there are no decimal points in RGB values. Option 3, "Flip horizontally", reverses an image so that what's on the left is now on the right, and what's on the right is now on the left. That is, the pixel that is on the far right end of the row ends up on the far left of the row, and vice versa. This is repeated as you move inwards toward the center of the row; remember preserve RGB order for each pixel! Option 4 is your choice. You must add a 4th option, but you can add whatever operation you might find interesting. Some simple operations are things like "Flatten Red", where for each pixel, you reduce that pixel's value of RED to 0. Or "Negate Red", where you reverse the value of RED in each pixel - if the red value is low, it should become high, and vice versa. The maximum color depth number is useful here; if red is 0, it becomes 255; if it's 255 it becomes 0; if red is say 100, then it becomes 155. Another option is "Extreme Contrast", which will change each RGB color value to either the max color value or 0. This change is based on whether the existing RGB value is greater than the midpoint of the color range (0..max color); if it is greater than the midpoint then replace it with max color, otherwise replace it with 0. Finally, a more challenging operation is something along the lines of "Flip Vertically", which will require a 2D array... The program performs the operation selected by the user, and then stops; if the user enters an invalid operation (e.g. 5 or -1), output an error message. You do not need to loop and ask for another operation,

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

//
// Constants:
//
// MAXHEIGHT: max height of an image (# pixels in a column)
// MAXWIDTH:   max width of an image (# pixels in a row)
// MAXCOLS:    max # of RGB values in a row
// MAXROWS:    max # of rows
//
#define MAXHEIGHT 5000
#define MAXWIDTH 5000
#define MAXCOLS   (3*MAXWIDTH)
#define MAXROWS   (MAXHEIGHT)


//
// ppmReadRow:
//
// Reads one row of the image from the input file into the row array;
// numCols is # of pixels in that row.
//
void ppmReadRow(ifstream& infile, int row[MAXCOLS], int numCols)
{
    for (int i = 0; i < 3*numCols; i += 3)
        infile >> row[i] >> row[i+1] >> row[i+2];
}


//
// ppmWriteRow:
//
// Writes one row of the image from the row array to the output file;
// numCols is # of pixels in that row.
//
void ppmWriteRow(ofstream& outfile, int row[MAXCOLS], int numCols)
{
    for (int i = 0; i < 3*numCols; i += 3)
        outfile << row[i] << ' ' << row[i+1] << ' ' << row[i+2] << "    ";
    outfile << endl;
}


//
// ppmCopyTransform:
//
// Read an image in PPM format from the source file,
// apply transformation using provided function ppmTransform, and finally
// write to the destination file.
//
void ppmCopyTransform(const string& srcFilename, const string& destFilename,
                      void (*ppmTransform)(int[MAXCOLS], int, int)=NULL)
{
    ifstream infile;
    ofstream outfile;
    string magicNumber;
    int numCols, numRows, maxColor;
    int row[MAXCOLS];

    // open the files:
    infile.open( srcFilename.c_str() );
    if (!infile.good())
    {
        cout << "**ERROR in ppmCopy: unable to open input file!" << endl;
        exit(-1);
    }

    outfile.open( destFilename.c_str() );
    if (!outfile.good())
    {
        cout << "**ERROR in ppmCopy: unable to open output file!" << endl;
        exit(-1);
    }

    // read header from input file:
    infile >> magicNumber;
    infile >> numCols;
    infile >> numRows;
    infile >> maxColor;

    // write header to output file:
    outfile << magicNumber << endl;
    outfile << numCols << " " << numRows << endl;
    outfile << maxColor << endl;

    // read the rows of the image, one by one,
    // apply transformation to each row, and
    // write to the output file:
    for (int r = 0; r < numRows; r++)
    {
        ppmReadRow(infile, row, numCols);

        // transform row using provided function
        if (ppmTransform) ppmTransform(row, numCols, maxColor);

        ppmWriteRow(outfile, row, numCols);
    }

    // done, must close the files for changes to be saved:
    infile.close();
    outfile.close();
}


//
// ppmTransformGrayscale:
//
// Convert the pixels in input row to grayscale
//
void ppmTransformGrayscale(int row[MAXCOLS], int numCols, int maxColor)
{
    for (int i = 0; i < 3*numCols; i += 3)
        row[i] = row[i+1] = row[i+2] = (row[i] + row[i+1] + row[i+2]) / 3;
}


//
// ppmTransformFlipHorizontal:
//
// Reverse the pixels in input row
//
void ppmTransformFlipHorizontal(int row[MAXCOLS], int numCols, int maxColor)
{
    for (int left = 0, right = 3*numCols-3; left < right; left+=3, right-=3)
    {
        //swap RED value
        int temp = row[left];
        row[left] = row[right];
        row[right] = temp;
        //swap GREEN value
        temp = row[left+1];
        row[left+1] = row[right+1];
        row[right+1] = temp;
        //swap BLUE value
        temp = row[left+2];
        row[left+2] = row[right+2];
        row[right+2] = temp;
    }
}


//
// ppmTransformNegateRed:
//
// Reverse the value of RED in each pixel
//
void ppmTransformNegateRed(int row[MAXCOLS], int numCols, int maxColor)
{
    for (int i = 0; i < 3*numCols; i += 3)
        row[i] = maxColor - row[i];
}


//
// main:
//
int main()
{
    string srcFile;
    string destFile;
    int opt;

    cout << "** PPM (Portable PixMap) Image Editor **" << endl;
    cout << endl;

    cout << "Enter input filename: ";
    cin >> srcFile;

    cout << "Enter output filename: ";
    cin >> destFile;

    cout << endl
         << "What operation would you like to perform?" << endl
         << "1. Copying" << endl
         << "2. Grayscale conversion" << endl
         << "3. Flip horizontally" << endl
         << "4. Flatten red" << endl << endl
         << "Enter the operation 1, 2, 3, or 4: ";
    cin >> opt;

    if (opt < 1 || opt > 4)
    {
        cout << "ERR: operation value must be 1, 2, 3, or 4" << endl;
        exit(-1);
    }

    if      (opt == 1)
    {
        cout << "copying..." << endl;
        ppmCopyTransform(srcFile, destFile);
    }
    else if (opt == 2)
    {
        cout << "converting..." << endl;
        ppmCopyTransform(srcFile, destFile, ppmTransformGrayscale);
    }
    else if (opt == 3)
    {
        cout << "flipping..." << endl;
        ppmCopyTransform(srcFile, destFile, ppmTransformFlipHorizontal);
    }
    else if (opt == 4)
    {
        cout << "negating..." << endl;
        ppmCopyTransform(srcFile, destFile, ppmTransformNegateRed);
    }

    cout << endl;
    cout << "** Done **" << endl;
    cout << endl;

    return 0;
}