Fill This Form To Receive Instant Help
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.
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:
ppm.c
ppm.c provides the implementation for the functions listed above.
Below is a brief description of each function listed in ppm.h
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.