This commit is contained in:
Citlali del Rey 2024-02-15 11:25:03 -08:00
parent abf9c56110
commit 71e4388478
Signed by: nullobsi
GPG key ID: 933A1F44222C2634
5 changed files with 249 additions and 57 deletions

3
.gitignore vendored
View file

@ -4,3 +4,6 @@ a.out
**/*.dSYM
**/*.dSYM/**
.DS_Store
**/.DS_Store

5
Assignment1/.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
rotate.pgm
rotate_again.pgm
threshold.pgm

223
Assignment1/solution.c Normal file
View file

@ -0,0 +1,223 @@
#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;
}
// 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;
}

View file

@ -1,57 +0,0 @@
#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;
}

18
Week4/week4.c Normal file
View file

@ -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;
}