Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that consists of multiple source files to parse the header information from a ppm image. Some ppm images and some starter code

Write a program that consists of multiple source files to parse the header information from a ppm image. Some ppm images and some starter code will be provided. You will need to write the function to parse the header, as well as a makefile, and a header file. The files from this lab will be starter files for the third programming assignment. Background Information For this lab, you will write a program that parses the header of a ppm image and prints out the dimensions of the image to the user. This will be used as starter code for your parsing function for PA3 in lecture. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ppm Images As you have learned in lecture, ppm images are a type of image format. It is easy to write and analyze programs to process this type of image. There are two parts to ppm images: the header and the pixel data. The header consists of a special tag indicating the type of image we will be using images with the P6 tag. The dimensions follow that, with columns stated first then rows; and then the largest value for any pixel, which is usually 255. If you open an image file using an editor like vim, you can see the header information at the top of the file. The image pixel data immediately follows the newline character after the 255, and is binary data, so it is not anything that will make any sense when viewing the file with an editor like vim. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Command-Line Arguments You also saw in lecture that when programs that use command-line arguments, the main() function signature changes from int main(void) { to int main(int argc, char *argv[]) { where argc holds the number of items entered at the command-line, and argv is the array that holds each item (or actually, a pointer to each item) that was entered at the command-line. For example, with the reverse_echo program from the previous lab, you saw that to run that program, you would type something like the following: ./reverse_echo testing 1 2 3 In that case, the value of argc would be 5, and the argv array would look like the following: argv[0] ------- argv[1] ------- argv[2] ------- argv[3] ------- argv[4] ------- argv[5] ------- * * * * * * ./reverse_echo testing 1 2 3 \0 2 For this lab, and for the third assignment, your program will use command-line arguments. To run the program, you will type the executable followed by the image file name, such as: ./a.out tiger.ppm - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Pointers In yesterdays lecture, you were introduced to pointers. You will use pointers in this lab for the parameters of the parseHeader() function that you will be writing. As you know, functions can only return one value, but we need to get two values from the header information of the image file: the number of columns and the number of rows. You also know by now that variables that are passed by value to functions are copied into the function parameters. When the function ends, those local copies go away and any changes that were made to those local copies in the function are not reflected back in the arguments that were sent in to that function. So to be able to get more than one value changed or assigned or modified from a function, addresses to the variables are sent in to the function. This is called pass by reference. An example of a function using pass by reference can be found in the swapPBR.c source file found on Canvas in the module for Chapter 10, Pointers). When the function assigns the values to those address locations sent in to the function parameters, the variables sent in will contain those values too because those function parameters point to those very same memory locations. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - File Pointers Also covered in lecture earlier were file pointers. The general form for declaring a file pointer is the following: FILE * So the following line declares a variable called inFile, which is a file pointer type. FILE *inFile; Then, you can assign to the file pointer variable the result of opening a file (assign it to point to a file). If you are trying to open a file to read from called tiger.ppm, the code would look like: inFile = fopen("tiger.ppm", "r"); If the file doesnt exist or that fopen() function fails for some reason, the value of inFile would be NULL. You can add some error checking code after trying to open a file, like the following: if (inputFile == NULL) { fprintf(stderr, "File open error. Exiting program. "); exit(1); // need to #include } But, when providing the name of the file as the second argument on the command-line, you can open the file contained in argv[1] rather than hardcoding the file name in your code; that way you can run your program using different files without changing the hardcoded name in your code. 3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - File I/O Functions C provides quite a few functions that can be used to read characters or strings or entire lines from a file. See the notes posted on Canvas in the module for Chapter 15, File I/O. Or you can use the appendix at the back of your book, or use an online reference such as cplusplus.com: http://www.cplusplus.com/reference/cstdio/ Lab Assignment 1. While logged in to one of the School of Computing servers (adas, babbages, or joeys e.g. ada1.computing.clemson.edu) type the following copy command: cp /group/course/cpsc111/public_html/F18Labs/lab11/* . Dont forget the . (dot) at the end. That copy command above says the following: copy all the files (the wildcard * means all) at that location /group/course/cpsc111/public_html/F18Labs/lab11/) to your current location (indicated by the . (dot)). The following files will be copied to your directory: mainDriver.c with starter code; need to fill in some code parse.c with header comment and #include for defs.h this is where the parseHeader() function goes defs.h with #includes and a spot for the prototype of the parseHeader() function two image files You will also need to write a makefile that at the very least contains a target for compilation. 2. Your parseHeader() function will need the file pointer sent to it from main() as well as pointers to the column value and the row value. Since the 3 parts of the header can be on separate lines or not, and may or may not have comments in between them, parsing the header can be tricky. Take a look at each of the image files to see what the headers look. If your program for this lab parses the columns and rows from those two files, then it will be sufficient for the lab. For PA3 in the lecture, however, you should make sure to add code to handle other variations of the header. You will use file I/O functions to read in character by character, or string by string, or an entire line at a time. There are many different ways that this can be done. You will have to take a look at the different functions available and think about what makes the most sense to you. Remember, that for this lab, the goal is just to parse from the header the columns and the rows and print those values to the user. But for PA3, you will have to add more code to continue parsing through the header so that you move the file pointer to the next item after the 255; then at that point, the image pixel data can be read in. SAMPLE RUNS OF THE PROGRAM: ./a.out tiger.ppm width is 690, height is 461 ./a.out julia.ppm width is 500, height is 500

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

More Books

Students also viewed these Databases questions