cs420/Assignment1/starter_code.c

57 lines
2.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
// Structure to hold grayscale pixel data
typedef struct {
unsigned char value;
} PixelGray;
// Function to read a PGM image into a 2D array
PixelGray** readPGM(const char* filename, int* width, int* height);
// Function to write a 2D matrix as a PGM image
void writePGM(const char* filename, PixelGray** matrix, int* width, int* height);
// Function to threshold the image matrix
PixelGray** threshold(PixelGray** matrix, int* width, int* height);
// Function to rotate the image matrix
PixelGray** rotate(PixelGray** matrix, int* width, int* height);
//main function - DO NOT MODIFY
int main() {
int width, height; // variable to hold width and height. Use reference in other functions
PixelGray** image_original = readPGM("lenna.pgm", &width, &height);
// Now you have the grayscale image data in the 'image_original' 2D array
// Access pixel data using image[row][col].value
// For example, to access the pixel at row=2, col=3:
// unsigned char pixel_value = image[2][3].value;
// Create a new 2D array 'image_thresh' to store the threshold image
PixelGray** image_thresh = threshold(image_original, &width, &height);
//write the image data as "threshold.pgm"
writePGM("threshold.pgm", image_thresh, &width, &height);
// Create a new 2D array 'image_rotate' to store the rotated image
PixelGray** image_rotate = rotate(image_original, &width, &height);
//write the image data as "rotate.pgm"
writePGM("rotate.pgm", image_rotate, &width, &height);
image_rotate = rotate(image_rotate, &width, &height);
//write the image data as "rotate_again.pgm"
writePGM("rotate_again.pgm", image_rotate, &width, &height);
// Free the allocated memory when you're done
for (int i = 0; i < height; ++i) {
free(image_original[i]);
free(image_thresh[i]);
free(image_rotate[i]);
}
free(image_original);
free(image_thresh);
free(image_rotate);
return 0;
}