Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Create a small simple C program that reads in a ppm image, and creates two new copies, one that is a flipped image and another one that is a gray-scaled image of the original

Create a small simple C program that reads in a ppm image, and creates two new copies, one that is a flipped image and another one that is a gray-scaled image of the original

Computer Science

Create a small simple C program that reads in a ppm image, and creates two new copies, one that is a flipped image and another one that is a gray-scaled image of the original.
 

Learning Objectives:

This assignment will give you practice in the following concepts:

file I/O,

structs,

pointers,

dynamic allocation of memory, more specifically for a 2D array,

passing arrays and other pointers to functions,

working with multiple files,

fprintf and fscanf functions,

command line arguments,

problem solving

 

Specifications:

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

 

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*);

 

Provide the implementation for the prototypes in the ppm.c file. 

 

You will also write the driver.c file.  The driver should have minimal code.  You are only allowed to create variables, and call functions in the driver.

 

ppm.h

The #include’s will go in this file.  Then include ppm.h in the remaining “.c” files.  Use the preprocessor #ifndef  -- #endif to prevent duplicate declaration compile errors.  If you are not sure how to do this ask a TA or review the header guard notes. Points will be deducted for not using the preprocessor #ifndef -- #endif.

You must create two structs:

  1. One for the header information, called header_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 it pixel_t and 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 are REQUIRED to 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 a P5 rather 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.

 

 

     

 

 

 

 

 

 

  1. 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.

 

  1. 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 function readHeader to read the header of the input ppm file.  Next call allocateMemory to dynamically allocate the memory for the 2D pixel_t array. Call the function readImage which will store the pixels of the input image in the 2D array.  Call grayScaleImage and flipImage passing in the header, the input image you read in and the appropriate file pointer.

Option 1

Low Cost Option
Download this past answer in few clicks

50 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE