diff --git a/.vscode/settings.json b/.vscode/settings.json index 521fcd2..caec8de 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ "iostream": "cpp", "iosfwd": "cpp", "fstream": "cpp", - "xlocmes": "cpp" + "xlocmes": "cpp", + "utility": "cpp" } } \ No newline at end of file diff --git a/lab4/images/1_out.ppm b/lab4/images/1_out.ppm index e69de29..586cf1e 100644 Binary files a/lab4/images/1_out.ppm and b/lab4/images/1_out.ppm differ diff --git a/lab4/images/1rotated.ppm b/lab4/images/1rotated.ppm deleted file mode 100644 index 5b25936..0000000 Binary files a/lab4/images/1rotated.ppm and /dev/null differ diff --git a/lab4/images/2rotated.ppm b/lab4/images/2rotated.ppm deleted file mode 100644 index c4203b2..0000000 Binary files a/lab4/images/2rotated.ppm and /dev/null differ diff --git a/lab4/images/3rotated.ppm b/lab4/images/3rotated.ppm deleted file mode 100644 index 9f2920b..0000000 Binary files a/lab4/images/3rotated.ppm and /dev/null differ diff --git a/lab4/src/makefile b/lab4/src/makefile index bd2a5e8..18477ba 100644 --- a/lab4/src/makefile +++ b/lab4/src/makefile @@ -1,3 +1,14 @@ +SHMDIR = sharedMemory + +shm_build: $(SHMDIR)/shared.cpp + g++ -g $(SHMDIR)/shared.cpp $(SHMDIR)/important.cpp -o $(SHMDIR)/shared.out + +shm_run: shm_build + $(SHMDIR)/shared.out ../images/$(INPUT).ppm ../images/$(INPUT)_out.ppm + + +# ----- OLD MAKEFILE CMDs ----------------- + build-sharpen: ./a.out ./a.out: image_sharpener.cpp libppm.cpp diff --git a/lab4/src/sharedMemory/important.cpp b/lab4/src/sharedMemory/important.cpp index 9f72711..6ec1182 100644 --- a/lab4/src/sharedMemory/important.cpp +++ b/lab4/src/sharedMemory/important.cpp @@ -6,95 +6,76 @@ using namespace std; -struct image_t *S1_smoothen(struct image_t *input_image) +float *S1_smoothen(struct image_t *input_image) { - // cout << input_image->width << input_image->height << " " << input_image->image_pixels << endl; int width = input_image->width; int height = input_image->height; // memory allocation - struct image_t *smoother = new struct image_t; - smoother->height = height; - smoother->width = width; - smoother->image_pixels = new uint8_t **[height]; + float *smoother = (float *)malloc(height * width * 3 * sizeof(float)); + long ind = 0; + for (int i = 0; i < height; i++) { - smoother->image_pixels[i] = new uint8_t *[width]; for (int j = 0; j < width; j++) - smoother->image_pixels[i][j] = new uint8_t[3]; - } - - for (int i = 1; i < height - 1; i++) - { - for (int j = 1; j < width - 1; j++) { for (int k = 0; k < 3; k++) { - // image->image_pixels[i][j][k] = val; // edge cases if (i == 0 && j == 0) { // i-1 and j - 1 doesnt exist - smoother->image_pixels[i][j][k] = (input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i][j + 1][k] / 4 + input_image->image_pixels[i + 1][j][k] / 4 + input_image->image_pixels[i + 1][j + 1][k] / 4); + smoother[ind] = (float)(input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i][j + 1][k] / 4 + input_image->image_pixels[i + 1][j][k] / 4 + input_image->image_pixels[i + 1][j + 1][k] / 4); } else if (i == height - 1 && j == 0) { // i+1 and j-1 does not exist - smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j][k] / 4 + input_image->image_pixels[i - 1][j + 1][k] / 4 + input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i][j + 1][k] / 4); + smoother[ind] = (float)(input_image->image_pixels[i - 1][j][k] / 4 + input_image->image_pixels[i - 1][j + 1][k] / 4 + input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i][j + 1][k] / 4); } else if (i == 0 && j == width - 1) { // i-1 and j+1 does not exist - smoother->image_pixels[i][j][k] = (input_image->image_pixels[i][j - 1][k] / 4 + input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i + 1][j - 1][k] / 4 + input_image->image_pixels[i + 1][j][k] / 4); + smoother[ind] = (float)(input_image->image_pixels[i][j - 1][k] / 4 + input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i + 1][j - 1][k] / 4 + input_image->image_pixels[i + 1][j][k] / 4); } else if (i == height - 1 && j == width - 1) { // i+1 and j+1 does not exist - smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j - 1][k] / 4 + input_image->image_pixels[i - 1][j][k] / 4 + input_image->image_pixels[i][j - 1][k] / 4 + input_image->image_pixels[i][j][k] / 4); + smoother[ind] = (float)(input_image->image_pixels[i - 1][j - 1][k] / 4 + input_image->image_pixels[i - 1][j][k] / 4 + input_image->image_pixels[i][j - 1][k] / 4 + input_image->image_pixels[i][j][k] / 4); } else if (i == 0) { // i - 1 does not exist - smoother->image_pixels[i][j][k] = (input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6 + input_image->image_pixels[i + 1][j - 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6 + input_image->image_pixels[i + 1][j + 1][k] / 6); + smoother[ind] = (float)(input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6 + input_image->image_pixels[i + 1][j - 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6 + input_image->image_pixels[i + 1][j + 1][k] / 6); } else if (j == 0) { // j -1 does not exist - smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i - 1][j + 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6 + input_image->image_pixels[i + 1][j + 1][k] / 6); + smoother[ind] = (float)(input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i - 1][j + 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6 + input_image->image_pixels[i + 1][j + 1][k] / 6); } else if (i == height - 1) { // i+1 does not exist - smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j - 1][k] / 6 + input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i - 1][j + 1][k] / 6 + input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6); + smoother[ind] = (float)(input_image->image_pixels[i - 1][j - 1][k] / 6 + input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i - 1][j + 1][k] / 6 + input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6); } else if (j == width - 1) { // j + 1 does not exist - smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j - 1][k] / 6 + input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i + 1][j - 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6); + smoother[ind] = (float)(input_image->image_pixels[i - 1][j - 1][k] / 6 + input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i + 1][j - 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6); } else { - smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j - 1][k] / 9 + input_image->image_pixels[i - 1][j][k] / 9 + input_image->image_pixels[i - 1][j + 1][k] / 9 + input_image->image_pixels[i][j - 1][k] / 9 + input_image->image_pixels[i][j][k] / 9 + input_image->image_pixels[i][j + 1][k] / 9 + input_image->image_pixels[i + 1][j - 1][k] / 9 + input_image->image_pixels[i + 1][j][k] / 9 + input_image->image_pixels[i + 1][j + 1][k] / 9); + smoother[ind] = (float)(input_image->image_pixels[i - 1][j - 1][k] / 9 + input_image->image_pixels[i - 1][j][k] / 9 + input_image->image_pixels[i - 1][j + 1][k] / 9 + input_image->image_pixels[i][j - 1][k] / 9 + input_image->image_pixels[i][j][k] / 9 + input_image->image_pixels[i][j + 1][k] / 9 + input_image->image_pixels[i + 1][j - 1][k] / 9 + input_image->image_pixels[i + 1][j][k] / 9 + input_image->image_pixels[i + 1][j + 1][k] / 9); } + ind++; } } } - return smoother; } -struct image_t *S2_find_details(struct image_t *input_image, struct image_t *smoothened_image) +float *S2_find_details(struct image_t *input_image, float *smoothened_image) { // TODO int width = input_image->width; int height = input_image->height; - struct image_t *details = new struct image_t; - details->height = height; - details->width = width; - details->image_pixels = new uint8_t **[height]; - - for (int i = 0; i < height; i++) - { - details->image_pixels[i] = new uint8_t *[width]; - for (int j = 0; j < width; j++) - details->image_pixels[i][j] = new uint8_t[3]; - } + float *details = (float *)malloc(height * width * 3 * sizeof(float)); + long ind = 0; for (int i = 1; i < height - 1; i++) { @@ -102,7 +83,8 @@ struct image_t *S2_find_details(struct image_t *input_image, struct image_t *smo { for (int k = 0; k < 3; k++) { - details->image_pixels[i][j][k] = max(0, input_image->image_pixels[i][j][k] - smoothened_image->image_pixels[i][j][k]); + details[ind] = max((float)0, (float)input_image->image_pixels[i][j][k] - smoothened_image[ind]); + ind++; } } } @@ -110,33 +92,25 @@ struct image_t *S2_find_details(struct image_t *input_image, struct image_t *smo return details; } -struct image_t *S3_sharpen(struct image_t *input_image, struct image_t *details_image) +float *S3_sharpen(struct image_t *input_image, float *details_image) { // TODO int width = input_image->width; int height = input_image->height; - int a = 1; + float a = 0.8; - struct image_t *sharp = new struct image_t; - sharp->height = height; - sharp->width = width; - sharp->image_pixels = new uint8_t **[height]; - - for (int i = 0; i < height; i++) - { - sharp->image_pixels[i] = new uint8_t *[width]; - for (int j = 0; j < width; j++) - sharp->image_pixels[i][j] = new uint8_t[3]; - } + float *sharp = (float *)malloc(height * width * 3 * sizeof(float)); + long ind = 0; for (int i = 1; i < height - 1; i++) { for (int j = 1; j < width - 1; j++) { for (int k = 0; k < 3; k++) { - sharp->image_pixels[i][j][k] = min(255, input_image->image_pixels[i][j][k] + details_image->image_pixels[i][j][k]) * a; + sharp[ind] = min((float)255, (float)input_image->image_pixels[i][j][k] + a * details_image[ind]); + ind++; } } } diff --git a/lab4/src/sharedMemory/important.h b/lab4/src/sharedMemory/important.h index b536603..e1e6640 100644 --- a/lab4/src/sharedMemory/important.h +++ b/lab4/src/sharedMemory/important.h @@ -9,9 +9,9 @@ struct image_t uint8_t*** image_pixels; }; -struct image_t* S1_smoothen(struct image_t *input_image); -struct image_t* S2_find_details(struct image_t *input_image, struct image_t *smoothened_image); -struct image_t* S3_sharpen(struct image_t *input_image, struct image_t *details_image); +float* S1_smoothen(struct image_t *input_image); +float* S2_find_details(struct image_t *input_image, float *smoothened_image); +float* S3_sharpen(struct image_t *input_image, float *details_image); struct image_t* read_ppm_file(char* path_to_input_file); void write_ppm_file(char* path_to_output_file, struct image_t* image); diff --git a/lab4/src/sharedMemory/shared.cpp b/lab4/src/sharedMemory/shared.cpp index 5cfbb68..099140b 100644 --- a/lab4/src/sharedMemory/shared.cpp +++ b/lab4/src/sharedMemory/shared.cpp @@ -18,19 +18,40 @@ using namespace std; int main(int argc, char *argv[]) { - // if(argc != 3) - // { - // cout << "usage: ./a.out \n\n"; - // exit(0); - // } + if(argc != 3) + { + cout << "usage: ./a.out \n\n"; + exit(0); + } - // struct image_t *input_image = read_ppm_file(argv[1]); + struct image_t *input_image = read_ppm_file(argv[1]); + int height = input_image->height; + int width = input_image->width; + + struct image_t *sharpened_image = new struct image_t; + sharpened_image->height = height; + sharpened_image->width = width; + sharpened_image->image_pixels = new uint8_t**[sharpened_image->height]; + for(int i = 0; i < sharpened_image->height; i++){ + sharpened_image->image_pixels[i] = new uint8_t*[sharpened_image->width]; + for(int j = 0; j < sharpened_image->width; j++){ + sharpened_image->image_pixels[i][j] = new uint8_t[3]; + } + } + + float *sharp; const char *shm1_name = "/shm_parent_child"; const char *shm2_name = "/shm_child_grandchild"; const char *sem1_name = "/sem_parent_child"; const char *sem2_name = "/sem_child_grandchild"; + + size_t size = input_image->height * input_image->width * 3 * sizeof(float); + // size_t size = 1024; + int reps=1000; + + // --------------------------------------------------------------------------------- int shm_fd = shm_open(shm1_name, O_CREAT | O_RDWR, 0666); if (shm_fd == -1) { perror("shm_open"); @@ -44,12 +65,33 @@ int main(int argc, char *argv[]) { } // Map the shared memory object into the process address space - void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + float *ptr = (float*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); if (ptr == MAP_FAILED) { perror("mmap"); return 1; } + // ----------------------------------------------------------------------------------- + int shm2_fd = shm_open(shm2_name, O_CREAT | O_RDWR, 0666); + if (shm2_fd == -1) { + perror("shm_open"); + return 1; + } + + // Resize the shared memory object to the desired size + if (ftruncate(shm2_fd, size) == -1) { + perror("ftruncate"); + return 1; + } + + // Map the shared memory object into the process address space + float *ptr2 = (float*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm2_fd, 0); + if (ptr2 == MAP_FAILED) { + perror("mmap"); + return 1; + } + // -------------------------------------------------------------------------------------- + sem_t *sem_parent_child = sem_open(sem1_name, O_CREAT, 0666, 0); // Initially locked (0) sem_t *sem_child_grandchild = sem_open(sem2_name, O_CREAT, 0666, 0); // Initially locked (0) @@ -59,9 +101,6 @@ int main(int argc, char *argv[]) { } - // size_t size = input_image->height * input_image->width * 3 * sizeof(uint8_t); - size_t size = 1024; - pid_t CHILD; CHILD = fork(); if (CHILD == -1) { @@ -71,37 +110,14 @@ int main(int argc, char *argv[]) { // Parent Process if(CHILD != 0){ - - char buffer[1024] = {'p', 'a', 'c', 'k', 'e', 't', '\0'}; - buffer[7] = '\0'; - // Write data to the shared memory - for(int i = 1; i < 5; i++){ - // printf("parent writing to shm\n"); - buffer[6] = '0' + i; - memcpy(ptr, buffer, 1024); // Copy 1024 bytes to shared memory + for(int i = 0; i < reps; i++){ - // Optional: Print what is being written to shared memory for verification - printf("P - Writing to shared memory: %s\n", (char *)ptr); + float* smoothImage = S1_smoothen(input_image); + memcpy(ptr, smoothImage, size); sem_post(sem_parent_child); - sem_wait(sem_child_grandchild); - } - // Unmap the shared memory object - if (munmap(ptr, size) == -1) { - perror("munmap"); - return 1; } - - // Close the shared memory file descriptor - if (close(shm_fd) == -1) { - perror("close"); - return 1; - } - - sem_close(sem_parent_child); - sem_close(sem_child_grandchild); - wait(NULL); /* Wait for child */ } // Child process @@ -115,135 +131,74 @@ int main(int argc, char *argv[]) { // Child Process if(GRAND_CHILD != 0){ - int shm_fd = shm_open(shm1_name, O_CREAT | O_RDWR, 0666); - if (shm_fd == -1) { - perror("shm_open"); - return 1; - } - // Resize the shared memory object to the desired size - if (ftruncate(shm_fd, size) == -1) { - perror("ftruncate"); - return 1; - } + for(int i = 0; i < reps; i++){ - // Map the shared memory object into the process address space - void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); - if (ptr == MAP_FAILED) { - perror("mmap"); - return 1; - } - - int shm2_fd = shm_open(shm2_name, O_CREAT | O_RDWR, 0666); - if (shm2_fd == -1) { - perror("shm_open"); - return 1; - } - - // Resize the shared memory object to the desired size - if (ftruncate(shm2_fd, size) == -1) { - perror("ftruncate"); - return 1; - } - - // Map the shared memory object into the process address space - void *ptr2 = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm2_fd, 0); - if (ptr2 == MAP_FAILED) { - perror("mmap"); - return 1; - } - - for(int i=0; i<5; i++){ - // printf("Child reading from shm1 and writing to shm2...\n"); sem_wait(sem_parent_child); - // Print the contents of the shared memory - char *packet = (char *)ptr; - printf("C - Shared memory contents: %s\n", packet); - // char buffer[1024]; - // strncpy(buffer, (char *)ptr, 1024); - memcpy(ptr2, ptr, 1024); - sem_post(sem_child_grandchild); // Signal parent - } + float *details = S2_find_details(input_image, ptr); + memcpy(ptr2, details, 1024); + sem_post(sem_child_grandchild); // Signal grandchild - // Unmap the shared memory object - if (munmap(ptr, size) == -1) { - perror("munmap"); - return 1; } - - // Close the shared memory file descriptor - if (close(shm_fd) == -1) { - perror("close"); - return 1; - } - // printf("child writing to shm\n"); - - // Unmap the shared memory object - if (munmap(ptr2, size) == -1) { - perror("munmap"); - return 1; - } - - // Close the shared memory file descriptor - if (close(shm2_fd) == -1) { - perror("close"); - return 1; - } - - shm_unlink(shm1_name); - sem_close(sem_parent_child); - sem_close(sem_child_grandchild); - wait(NULL); - exit(EXIT_SUCCESS); + exit(EXIT_SUCCESS); } // GrandChild Process - else if (GRAND_CHILD == 0) { /* GrandChild reads from pipe */ - int shm2_fd = shm_open(shm2_name, O_CREAT | O_RDWR, 0666); - if (shm2_fd == -1) { - perror("shm_open"); - return 1; - } + else if (GRAND_CHILD == 0) { - // Resize the shared memory object to the desired size - if (ftruncate(shm2_fd, size) == -1) { - perror("ftruncate"); - return 1; - } + printf("GC reading...\n"); + for(int i = 0; i < reps; i++){ - // Map the shared memory object into the process address space - void *ptr2 = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm2_fd, 0); - if (ptr2 == MAP_FAILED) { - perror("mmap"); - return 1; - } - - for(int i=0; i<5; i++){ sem_wait(sem_child_grandchild); - // Print the contents of the shared memory - // printf("Grandchild reading from shm...\n"); - printf("GC - Shared memory contents: %s\n", (char *)ptr2); - } + sharp = S3_sharpen(input_image, ptr2); - // Unmap the shared memory object - if (munmap(ptr2, size) == -1) { - perror("munmap"); - return 1; } + printf("GC reading done...\n"); - // Close the shared memory file descriptor - if (close(shm2_fd) == -1) { - perror("close"); - return 1; - } - - shm_unlink(shm2_name); - sem_close(sem_parent_child); - sem_close(sem_child_grandchild); exit(EXIT_SUCCESS); } } + wait(NULL); + wait(NULL); + + int ind=0; + for(int i = 0; i < sharpened_image->height; i++){ + for(int j = 0; j < sharpened_image->width; j++){ + for(int k = 0; k < 3; k++){ + sharpened_image->image_pixels[i][j][k] = sharp[ind++]; + } + } + } + printf("Writing output file...\n"); + write_ppm_file(argv[2], sharpened_image); + + // Unmap the shared memory object + if (munmap(ptr, size) == -1) { + perror("munmap"); + return 1; + } + // Close the shared memory file descriptor + if (close(shm_fd) == -1) { + perror("close"); + return 1; + } + + // Unmap the shared memory object + if (munmap(ptr2, size) == -1) { + perror("munmap"); + return 1; + } + + // Close the shared memory file descriptor + if (close(shm2_fd) == -1) { + perror("close"); + return 1; + } + + shm_unlink(shm1_name); + shm_unlink(shm2_name); + sem_close(sem_parent_child); sem_close(sem_child_grandchild); sem_unlink(sem1_name);