5/4/2021 pa09 https://csn.instructure.com/courses/1339624/assignments/6149830?return_to=https%3A%2F%2Fcsn.instructure.com%2Fcalendar%23view_name%3… 1/8 pa09 Due Wednesday by 11:59pm Points 200...

1 answer below »
Need help with the coding of this assignment


5/4/2021 pa09 https://csn.instructure.com/courses/1339624/assignments/6149830?return_to=https%3A%2F%2Fcsn.instructure.com%2Fcalendar%23view_name%3… 1/8 pa09 Due Wednesday by 11:59pm Points 200 Available until May 6 at 8am pa09 The remainder of your work must be completed independently, please. You may ask and share general information, but please use discretion. Give help, not answers. Accept help, not answers. Introduction For this assignment, you will write an image-editing program that works with Portable Pixel Map (PPM) files. Background PPM Image Format Most file formats are variants of the organization shown below: The PPM image format uses this same structure, but it is in human-readable ASCII text. The following is a very simple PPM file: Sample PPM File P3 4 4 255 0 0 0 100 0 0 0 0 0 255 0 255 0 0 0 0 255 175 0 0 0 0 0 0 0 0 0 0 0 0 0 15 175 0 0 0 255 0 255 0 0 0 0 0 0 255 255 255 5/4/2021 pa09 https://csn.instructure.com/courses/1339624/assignments/6149830?return_to=https%3A%2F%2Fcsn.instructure.com%2Fcalendar%23view_name%3… 2/8 Image Header As you can see, the file consists of two parts: a header and a data segment. The header consists of the following entries: P3 -- magic number 4 4 -- image width and height 255 -- max color value P3 is a "magic number." It indicates what type of PPM image this is (e.g., full color, ASCII encoding). For this assignment, it will always be P3. This is a required entry. Next comes the number of columns and the number of rows in the image (4 x 4). This is a required entry. Finally, we have the maximum color value 255. This can be any value up to 65535, but a common value is 255. This is a required entry. The way you see the header presented is how it should be formatted! Image Data The image data contains the actual picture information. The data block begins with the first pixel of the top line of the image (upper left hand corner), and the data is stored in order from left to right giving the red, green, and blue values for each pixel. Each pixel of the image is a tiny, colored square. The color is determined by how much red, green, and blue are present. So, 0 0 0 is the first color of the image, which is black, and the last pixel in the image is 255 255 255, which is white. By varying the levels of the RGB values, you can come up with any color in between. (Recall that the RGB color space provides for 16,777,216 different color variations.) Note that color values must be separated by at least one space, but after that additional whitespace is ignored by the image viewer. In the sample ppm above we used additional whitespace to format the image so that it is easy for a human to understand, but the computer doesn't care if everything is on one line, if there is one line per line of the image, or some other combination. (To conserve storage requirements, write a single space between values.) Putting it all together The sample PPM image from above would look something like this: 5/4/2021 pa09 https://csn.instructure.com/courses/1339624/assignments/6149830?return_to=https%3A%2F%2Fcsn.instructure.com%2Fcalendar%23view_name%3… 3/8 Keep in mind, each square is one pixel, so the real thing is much smaller (the rendered image was blown up by 5000%)! How to view PPM files While PPM files are easy to view as text (you can use geany, for instance) and easy to work with in code, they are highly inefficient. Most modern image formats use some kind of compression to make their size reasonable while preserving the image appearance. PPM files are commonly used as an intermediate format when converting images from one type to another. On bellagio, you can view a PPM file using the display program: $ display /home/shared/reference.ppm & And you can convert your own images using the convert program: $ convert –compress none myImage.png myImage.ppm You will need to install a program to view these images on a Windows machine. Irfanview is a small download and works quite well. You may also want to investigate The GIMP. It is a full-featured image editor (an open source equivalent to Photoshop). The GIMP is available for all major operating systems. Both programs allow you to convert your own images to PPM so you can practice with your own pictures (keep in mind that you may need to make them very small or the resulting PPM will be quite large!). Your Assignment Whew! That may sound like a lot of work, but it's really pretty simple. To make it easy to code, I've broken the program into phases. In the first phase, you are reading the whole image and writing it to a file without making any changes to the image in the process. Believe it or not, at this point you've written most of the program! Phase I 5/4/2021 pa09 https://csn.instructure.com/courses/1339624/assignments/6149830?return_to=https%3A%2F%2Fcsn.instructure.com%2Fcalendar%23view_name%3… 4/8 The user will specify the name of the input image file as the first command line argument and the name of the output image file as the second command line argument (see lab09 for instructions). The file will be a text file in PPM format as described in the discussion above. Note that you need to check several error conditions: invalid syntax (not the right number of command line arguments), file not found, wrong filetype, input and output filenames the same, ... The purpose of the program in Phase I is to make an "exact" copy of the input file as the output file. The "catch" is that you are limited to one array large enough to hold information for one image with a maximum size of 1024x1024 pixels. For example, if the image is sized 640x480, you will use only a portion of the array, while an 1024x1024 image would use the entire array. This is a realistic restriction; a picture of a larger size could easily become hundreds of thousands (even millions!) of numbers. While the header must be formatted as described above, the image data portion of your output file can be formatted as you like. Whitespace includes newline characters, so you can put them in wherever you wish. The format specification allows for it. However, I recommend you only use a single space between color values to conserve storage requirements. Example interaction with the user: $ ./pa09 Error: no filenames provided $ ./pa09 blahblahblah Error: File not found. $ ./pa09 notes.txt Error: Not a valid PPM image file. $ ./pa09 mom.ppm mom.ppm Error: Output file cannot be the input file. $ ./pa09 mom.ppm mom2.ppm $ # as is traditional with Linux, "silence is golden" At this stage, the output file will be identical to the input file, notwithstanding file formatting. Note: Your program is not responsible for displaying the image in the file! It should only manipulate the pixels and create an output file in the proper PPM format. Test this with small files and large files. You can check to see if they are identical by loading them both into a text editor and comparing number by number. Phase II At this step, your program is nearly done. We're going to write a few functions to manipulate your image. The following examples will use this reference image (available at /home/shared/reference.ppm should you wish to experiment with it). Original image 5/4/2021 pa09 https://csn.instructure.com/courses/1339624/assignments/6149830?return_to=https%3A%2F%2Fcsn.instructure.com%2Fcalendar%23view_name%3… 5/8 flop() Write a function called flop that will reverse the picture horizontally. 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 (remember to preserve RGB order!). After passing original image to flop function flip() Write a function called flip that will reverse the picture vertically. That is, the pixel that is at the top left corner of the picture ends up at the bottom left corner and vice versa. After passing original image to flip function 5/4/2021 pa09 https://csn.instructure.com/courses/1339624/assignments/6149830?return_to=https%3A%2F%2Fcsn.instructure.com%2Fcalendar%23view_name%3… 6/8 negate() Write a function called negate that will inverse the colors of the picture. This is accomplished by subtracting each of the red, green, and blue values from the color depth, 255. After passing original image to negate function You should pass the image to each of the functions one at a time to generate the final output of your program. After passing original image through flip , flop , and negate (in any order) 5/4/2021 pa09 https://csn.instructure.com/courses/1339624/assignments/6149830?return_to=https%3A%2F%2Fcsn.instructure.com%2Fcalendar%23view_name%3… 7/8 The resulting image is the "inverse" of the original, so, if you pass this image back through your program, your program should create an image equivalent to the original. Putting it all together Filenames are provided as command line arguments. If there is an error opening the input file, or if the input file is not an ASCII image file, processing cannot continue. An appropriate message should be displayed to the user. Your program will then transform the image using the function specified above, and write the resulting image to the output as a P3 file. Note: Your program is not responsible for displaying the image! It only reads and writes image files. We'll use the 'display' utility to view the images. Scoring This is the final submission for the course. Treat it as your showcase, ensuring formatting is neat and consistent (use 'astyle') and every identifier is documented. Use the General Programming Guidelines as a "checklist" to make sure you haven't forgotten something simple Review the Grading Rubric so you know what's being evaluated. Submission instructions Name your source code pa09.cpp, and use the following turnin command to submit your work for grading. There is no peer review available for this assignment. $ turnin -c cs135-_____ -p pa09 -v pa09.cpp input.ppm output.ppm Notice you are turning in three files: 5/4/2021 pa09 https://csn.instructure.com/courses/1339624/assignments/6149830?return_to=https%3A%2F%2Fcsn.instructure.com%2Fcalendar%23view_name%3… 8/8 Total Points: 100 Programming Rubic (1) Criteria Ratings Pts 40 pts 20 pts 20 pts 10 pts 10 pts pa09.cpp - This is your final submission of the program, properly formatted and well-documented. This
Answered Same DayMay 05, 2021

Answer To: 5/4/2021 pa09...

Swapnil answered on May 05 2021
151 Votes
#include
#include
#include
using namespace std;
struct pixel_t
{

unsigned char RED,GREEN,BLUE;
}
arr[768][1024];
void gray_scale();
string magicNumber;
int width;
int height;
int maxColorVal;
void gray_scale()
{
unsigned char newpixel;
for(int i = 0; i < height ; i++)
{
for(int j = 0; j < width ; j++)
{
    newpixel = (arr[i][j].RED + arr[i][j].GREEN + arr[i][j].BLUE)/3;
            arr[i][j].RED=newpixel;
            arr[i][j].GREEN=newpixel;
            arr[i][j].BLUE=newpixel;
     }
    }
}
string getFileExt(const string& s)
{
    size_t i = s.rfind('.', s.length());
    if (i != string::npos)
    {
    return(s.substr(i+1, s.length() - i));
    }
    return("");
}
int main(int argc, char * argv[])
{
if(argv[1]==NULL)
{
std::cerr << "Error:...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here