From 71e438847811964b49739987025e522bd5fae68b Mon Sep 17 00:00:00 2001 From: Citlali del Rey Date: Thu, 15 Feb 2024 11:25:03 -0800 Subject: [PATCH] Updates --- .gitignore | 3 + Assignment1/.gitignore | 5 + Assignment1/solution.c | 223 +++++++++++++++++++++++++++++++++++++ Assignment1/starter_code.c | 57 ---------- Week4/week4.c | 18 +++ 5 files changed, 249 insertions(+), 57 deletions(-) create mode 100644 Assignment1/.gitignore create mode 100644 Assignment1/solution.c delete mode 100644 Assignment1/starter_code.c create mode 100644 Week4/week4.c diff --git a/.gitignore b/.gitignore index 26f4128..5cb2b23 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ a.out **/*.dSYM **/*.dSYM/** +.DS_Store +**/.DS_Store + diff --git a/Assignment1/.gitignore b/Assignment1/.gitignore new file mode 100644 index 0000000..2cf2b79 --- /dev/null +++ b/Assignment1/.gitignore @@ -0,0 +1,5 @@ + +rotate.pgm +rotate_again.pgm +threshold.pgm + diff --git a/Assignment1/solution.c b/Assignment1/solution.c new file mode 100644 index 0000000..b5e032c --- /dev/null +++ b/Assignment1/solution.c @@ -0,0 +1,223 @@ +#include +#include + +// 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; +} + +// Function to read a PGM image into a 2D array +PixelGray** readPGM(const char* filename, int* width, int* height) { + // Open file. + FILE* f = fopen(filename, "r"); + + // Handle & print error + if (f == NULL) { + perror("Could not open file"); + exit(1); + } + + // Read the header. It should say "P5\n" + char header1[3]; + + // Read + int r = fread(header1, sizeof(char), 3, f); + if (r != 3) { + perror("Potential error reading"); + exit(1); + } + + // Check header 1. + if(header1[0] != 'P' || header1[1] != '5' || header1[2] != '\n') { + goto pgm_invalid; + } + + // Read width, height. + r = fscanf(f, "%d %d\n", width, height); + + // If two items were not read, something went wrong. + if (r != 2) { + goto pgm_invalid; + } + + // Read pixel depth. + unsigned int depth; + r = fscanf(f, "%d\n", &depth); + if (r != 1 || depth != 255) { + goto pgm_invalid; + } + + // Create buffer of size height. + PixelGray** outBuf = malloc(sizeof(PixelGray*) * *height); + + // Loop through all the rows. + for (int y = 0; y < *height; y++) { + // Create a new buffer for the row. + PixelGray* rowBuf = malloc(sizeof(PixelGray)* *width); + + // Read from the file into the row. + r = fread(rowBuf, sizeof(PixelGray), *width, f); + + // Handle a potential error + if (r != *width) { + if (feof(f)) { + fprintf(stderr, "Unexpected EOF\n"); + exit(1); + } + if (ferror(f)) { + fprintf(stderr, "Read error %d\n", ferror(f)); + exit(1); + } + fprintf(stderr, "Unknown error while reading\n"); + exit(1); + } + + // Write new row into output matrix + outBuf[y] = rowBuf; + } + + // Done! + fclose(f); + return outBuf; + + +pgm_invalid: + fclose(f); + fprintf(stderr, "PGM file is invalid.\n"); + exit(1); +} + +// Function to write a 2D matrix as a PGM image +void writePGM(const char* filename, PixelGray** matrix, int* width, int* height) { + // Open file for writing. + FILE* f = fopen(filename, "w"); + + // Handle & print error + if (f == NULL) { + perror("Could not open file"); + exit(1); + } + + // Write header. + int w = fprintf(f, "P5\n%d %d\n255\n", *width, *height); + + // Handle error + if (ferror(f)) { + fprintf(stderr, "Write error %d\n", ferror(f)); + exit(1); + } + + // Write all the rows. + for (int y = 0; y < *height; y++) { + // Write a row. + w = fwrite(matrix[y], sizeof(PixelGray), *width, f); + + // Handle error + if (w != sizeof(PixelGray)* (*width)) { + if (ferror(f)) { + fprintf(stderr, "Write error %d\n", ferror(f)); + exit(1); + } + fprintf(stderr, "Unknown write error\n"); + exit(1); + } + } + + // Done! + fclose(f); +} + + +// Function to threshold the image matrix +#define THRESH 80 +PixelGray** threshold(PixelGray** matrix, int* width, int* height) { + // Create buffer of size height. + PixelGray** outBuf = malloc(sizeof(PixelGray*) * *height); + + // Fill buffer. + for(int y = 0; y < *height; y++){ + // Allocate row buffer. + outBuf[y] = malloc(sizeof(PixelGray) * *width); + + // Now, loop through the memory and set the threshold values. + for (int x = 0; x < *width; x++) { + if (matrix[y][x].value > THRESH) { + outBuf[y][x].value = 255; + } else { + outBuf[y][x].value = 0; + } + } + } + + return outBuf; +} + +// Function to rotate the image matrix +PixelGray** rotate(PixelGray** matrix, int* width, int* height) { + // Create buffer of size height. + PixelGray** outBuf = malloc(sizeof(PixelGray*) * (*height)); + + // Fill buffer. + for(int y = 0; y < *height; y++){ + // Allocate row buffer. + outBuf[y] = malloc(sizeof(PixelGray) * *width); + + // Go through all x,y. Do a transpose. + for (int x = 0; x < *width; x++) { + // Assume matrix is square... + outBuf[y][x] = matrix[x][y]; + } + } + + return outBuf; +} diff --git a/Assignment1/starter_code.c b/Assignment1/starter_code.c deleted file mode 100644 index 3f99615..0000000 --- a/Assignment1/starter_code.c +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include - -// 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; -} \ No newline at end of file diff --git a/Week4/week4.c b/Week4/week4.c new file mode 100644 index 0000000..cc5a582 --- /dev/null +++ b/Week4/week4.c @@ -0,0 +1,18 @@ + +int numberOfSteps(int n) { + int s = 0; + while (n != 0) { + // Odd + if (n & 1) { + // Subtract 1 + n = n ^ 1; + } else { + // Divide by 2 + n >>= 1; + } + // Increase step count + s ++; + } + + return s; +}