Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create two .c files (driver.c and ppm.c) and one .h (ppm.h). The function prototypes for the .h file are: header_t* readHeader(FILE*); pixel_t** readPPM(FILE*,header_t*); void writeP6Image(header_t*,

Create two .c files (driver.c and ppm.c) and one .h (ppm.h). The function prototypes for the .h file are:

header_t* readHeader(FILE*);

pixel_t** readPPM(FILE*,header_t*);

void writeP6Image(header_t*, pixel_t**, FILE*);

void grayScaleImage(header_t*, pixel_t**, FILE*);

void flipImage(header_t*, pixel_t**, FILE*);

pixel_t** allocateMemory(header_t*);

void freeMemory(pixel_t**, header_t*);

Implement the functions in theppm.cfile. Thedriver.cfile should have minimal code. Only create variables, and call functions in the driver.

ppm.h

The #includes will go in this file. Then include ppm.h in the remaining .c files. Use the preprocessor #ifndef -- #endif.

Youmustcreate two structs:

  1. One for the header information, calledheader_t. You must use typedef. Header has char magicNum[3], int width, int height, int maxVal.
  2. One for the three unsigned char values in each pixel; unsigned char red, unsigned char green, unsigned char blue. Call itpixel_tand use typedef

ppm.c

ppm.c provides the implementation for the functions listed above.

Below is a brief description of each function listed in ppm.h

  1. readHeader This function reads the input ppm image header information using fscanf. %s should be use for the magicNum, %d for width, height, and maxVal
  1. readPPM This function calls allocateMemory to allocate the memory for a 2D array of pixel_t type. As you can image, the 2D array will be used to store the red, green, and blue values of the ppm image. You areREQUIREDto use a 2D array.
  1. writeP6Image This function prints the image. Use fprintf to print the header then loop through each pixel and print the RGB values, use fprintf.
  1. grayScaleImage This function prints out a gray scaled image of the original image. In a gray scaled image the magicNum is aP5rather than a P6. It also only takes in one value a combination of the red, green, and blue values of a particular pixel in the image. In other words, for each pixel multiply the red value by .299, the green value by .587, and the blue value by .11 Add the multiplied values together and print that value only. Use %c when printing. Remember no need to print three values only one. Use fprintf to print the image. You should pay close attention to the type of data you are working with. You may need to cast. This function does not change the original image values it only uses them to calculate the value of the grayscaled image. Below is an example output.
  2. flipImage This function flips the image (top to bottom). The original image will be passed to this function (pixel_t**). Declare a local variable of type pixel_t that is a 2D array and dynamically allocate the memory for the output 2D array. Loop through the image passed to the function flipping the pixels top to bottom.Your program should be able to handle an image that is either square or rectangular. Below is an example of an output.
  1. allocateMemory This function will allocate the 2D array and return the address of the allocated memory. You are not allowed to statically allocate the memory you must use malloc or calloc. Pixel_t pix[height][width] is not allowed. When [] is used to allocate memory this is stored on the stack. You are not allowed to do this.
  2. freeMemory- This function gives the memory back to the operating system use the C function free.

driver.c

Create three file pointers one for reading, one to write the flipped image, and one to write the grayscale image. Open all three. The names of the files will be supplied using command line arguments. Using command line arguments allows me to provide various images to test your program. Be sure to check that the user entered the correct number of arguments on the command line. Also check that the files opened successful. If the user did not do either, print to stderr a message then exit the program. You may use assert in main or add a function to ppm.h and ppm.c and call the function in main.

If the files opened successfully then call the functionreadHeaderto read the header of the input ppm file. Next callallocateMemoryto dynamically allocate the memory for the 2D pixel_t array. Call the functionreadImagewhich will store the pixels of the input image in the 2D array. CallgrayScaleImageandflipImagepassing in the header, the input image you read in and the appropriate file pointer.

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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions