Question: Program outline: 1 . Your C program should read any given grayscale images in pgm format. Grayscale images have pixel values ranging from 0 -

Program outline:
1. Your C program should read any given grayscale images in pgm format. Grayscale
images have pixel values ranging from 0-255. This pixel values can be treated as
unsigned characters.
2. The image data should be loaded as a 2D matrix.
3. The 2D matrix should be manipulated to threshold the given image and convert it to a
binary image.
4. The 2D matrix should be manipulated to rotate the given image left.
5. The manipulated 2D matrices should be converted back to grayscale image. This image
should be viewable.
Your tasks: (READ CAREFULLY)
DO NOT alter the main() function. Four functions are declared and called in the given
program. Your task is to write the function definitions for those 4 functions:
A. PixelGray** readPGM(const char* filename, int* width, int* height);
a. This function should open the file with provided file name.
i. Note: If you use MacOS, it is okay to have the mode in r. If you are using
Windows, you must have the mode to rb.
b. Handle errors!
c. Get the dimensions of the image.
d. DO NOT SKIP THIS STEP, figure out how to eat the newline character after
getting the last line of the headers.
e. Dynamically allocate the storage (2D matrix) for the image dimension.
f. Read from the file and fill the 2D matrix.
g. Close the file.
h. Return the 2D matrix.
B. void writePGM(const char* filename, PixelGray** matrix, int* width, int* height);
a. This function should create a new file with the file name provided. This file should
be opened for writing.
b. Remember there are headers from the original file. Think about how you will write
them to the file before passing the data from the matrix.
i. Note: Autograder on gradescope will check line by line so I would leave no
space after passing your headers beside newline at the end of the last
header entry.
c. Handle errors!
d. Copy data from the provided 2D matrix to the file.
e. Close the file.
C. PixelGray** threshold(PixelGray** matrix, int* width, int* height);
a. This function should create a new 2D matrix. Dynamically allocate the storage
(2D matrix) for the new matrix.
b. Thresholding
i. If original value >80-- store 255
ii. Otherwise store 0
c. Return the new 2D matrix.
D. PixelGray** rotate(PixelGray** matrix, int* width, int* height);
a. This function should create a new 2D matrix. Dynamically allocate the storage
(2D matrix) for the new matrix.
b. Rotate the original 2D matrix and store the result as the new matrix.
i. Rotation can be achieved by matrix transpose (swapping rows & column).
c. Return the new 2D matrix

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!