routine push

This commit is contained in:
ItsMAX0112 2024-11-04 02:40:02 +05:30
parent 032891872f
commit 81e9985e58
10 changed files with 1308 additions and 382 deletions

Binary file not shown.

962
lab4/src/1.ppm Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,29 +1,40 @@
SHMDIR = sharedMemory
SHRPDIR = sharpener
THRDDIR = threads
PIPDIR = pipes
INPUT = 1
shm_build: $(SHMDIR)/shared.cpp
build_pipes: $(PIPDIR)/pipes.cpp $(PIPDIR)/important.cpp
g++ -g $(PIPDIR)/pipes.cpp $(PIPDIR)/important.cpp -o $(PIPDIR)/pipes.out
part2_1: build_pipes
$(PIPDIR)/pipes.out $(INPUT).ppm output_$@.ppm
build_shm: $(SHMDIR)/shared.cpp $(SHMDIR)/important.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
part2_2: build_shm
$(SHMDIR)/shared.out $(INPUT).ppm output_$@.ppm
build_threads: $(THRDDIR)/threads.cpp $(THRDDIR)/important.cpp
g++ -g $(THRDDIR)/threads.cpp $(THRDDIR)/important.cpp -o $(THRDDIR)/threads.out
part2_3: build_threads
$(THRDDIR)/threads.out $(INPUT).ppm output_$@.ppm
# ----- OLD MAKEFILE CMDs -----------------
build-sharpen: ./a.out
build_sharpen: $(SHRPDIR)/image_sharpener.cpp $(SHRPDIR)/libppm.cpp
g++ -g $(SHRPDIR)/image_sharpener.cpp $(SHRPDIR)/libppm.cpp -o $(SHRPDIR)/sharpen.out
./a.out: image_sharpener.cpp libppm.cpp
g++ -g image_sharpener.cpp libppm.cpp
run-sharpen: ./a.out
./a.out ../images/$(INPUT).ppm ../images/$(OUTPUT).ppm
./coms.out: coms.cpp important.cpp
g++ -g coms.cpp important.cpp -o coms.out
run-coms: ./coms.out
./coms.out $(F)
part1: build_sharpen
$(SHRPDIR)/sharpen.out $(INPUT).ppm output_$@.ppm
clean:
rm a.out coms.out
.PHONY: run-coms clean
.PHONY: clean

View File

@ -1,244 +0,0 @@
#include <iostream>
#include "libppm.h"
#include <cstdint>
#include <chrono>
#include <fstream>
using namespace std;
struct image_t* 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];
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] / 9+ input_image->image_pixels[i][j+1][k] / 9 + input_image->image_pixels[i+1][j][k] / 9 + input_image->image_pixels[i+1][j+1][k] / 9);
}
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] / 9 + input_image->image_pixels[i-1][j+1][k] / 9+ input_image->image_pixels[i][j][k] / 9+ input_image->image_pixels[i][j+1][k] / 9);
}
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] / 9 + input_image->image_pixels[i][j][k] / 9+ input_image->image_pixels[i+1][j-1][k] / 9 + input_image->image_pixels[i+1][j][k] / 9);
}
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] / 9 + input_image->image_pixels[i-1][j][k] / 9 + input_image->image_pixels[i][j-1][k] / 9 + input_image->image_pixels[i][j][k] / 9);
}
else if (i == 0) { // i - 1 does not exist
smoother->image_pixels[i][j][k] = ( 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);
}
else if(j == 0) { // j -1 does not exist
smoother->image_pixels[i][j][k] = ( 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][k] / 9+ input_image->image_pixels[i][j+1][k] / 9+ input_image->image_pixels[i+1][j][k] / 9+ input_image->image_pixels[i+1][j+1][k]/ 9);
}
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] / 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);
}
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] / 9+ input_image->image_pixels[i-1][j][k] / 9+ input_image->image_pixels[i][j-1][k] / 9+ input_image->image_pixels[i][j][k]/ 9 + input_image->image_pixels[i+1][j-1][k] / 9+ input_image->image_pixels[i+1][j][k] / 9);
}
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);
}
}
}
}
return smoother;
}
struct image_t* S2_find_details(struct image_t *input_image, struct image_t *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];
}
for(int i = 1; i < height-1; i++)
{
for(int j = 1; j < width-1; j++)
{
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]);
}
}
}
return details;
}
struct image_t* Rotate_image(struct image_t *input_image)
{
// TODO
int width = input_image->width;
int height = input_image->height;
struct image_t* rotated = new struct image_t;
rotated->height = width;
rotated->width = height;
rotated->image_pixels = new uint8_t**[height];
for(int i = 0; i < rotated->height; i++)
{
rotated->image_pixels[i] = new uint8_t*[rotated->width];
for(int j = 0; j < rotated->width; j++)
rotated->image_pixels[i][j] = new uint8_t[3];
}
for(int i = 1; i < rotated->height-1; i++)
{
for(int j = 1; j < rotated->width-1; j++)
{
for(int k = 0; k < 3; k++)
{
rotated->image_pixels[i][j][k] = input_image->image_pixels[j][i][k];
}
}
}
return rotated;
}
struct image_t* S3_sharpen(struct image_t *input_image, struct image_t *details_image)
{
// TODO
int width = input_image->width;
int height = input_image->height;
int a = 1;
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];
}
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;
}
}
}
return sharp; //TODO remove this line when adding your code
}
int main(int argc, char **argv)
{
if(argc != 3)
{
cout << "usage: ./a.out <path-to-original-image> <path-to-transformed-image>\n\n";
exit(0);
}
auto startread = chrono::high_resolution_clock::now();
struct image_t *input_image = read_ppm_file(argv[1]);
auto endread = std::chrono::high_resolution_clock::now();
auto startsmooth = chrono::high_resolution_clock::now();
struct image_t *smoothened_image = S1_smoothen(input_image);
auto endsmooth = std::chrono::high_resolution_clock::now();
auto startdetail = chrono::high_resolution_clock::now();
struct image_t *details_image = S2_find_details(input_image, smoothened_image);
auto enddetail = std::chrono::high_resolution_clock::now();
auto startsharp = chrono::high_resolution_clock::now();
struct image_t *sharpened_image = S3_sharpen(input_image, details_image);
auto endsharp = std::chrono::high_resolution_clock::now();
struct image_t *rotated_image = Rotate_image(input_image);
auto startwrite = chrono::high_resolution_clock::now();
write_ppm_file(argv[2], rotated_image);
auto endwrite = std::chrono::high_resolution_clock::now();
chrono::duration<double> readduration = endread - startread;
chrono::duration<double> smoothduration = endsmooth - startsmooth;
chrono::duration<double> detailduration = enddetail - startdetail;
chrono::duration<double> sharpduration = endsharp - startsharp;
chrono::duration<double> writeduration = endwrite - startwrite;
ofstream logFile("read_times.txt", std::ios_base::app);
if (logFile.is_open()) {
logFile << argv[1] << " ";
logFile << readduration.count() << endl;
logFile.close();
}
logFile.open("smooth_times.txt", std::ios_base::app);
if (logFile.is_open()) {
logFile << argv[1] << " ";
logFile << smoothduration.count() << endl;
logFile.close();
}
logFile.open("detail_times.txt", std::ios_base::app);
if (logFile.is_open()) {
logFile << argv[1] << " ";
logFile << detailduration.count() << endl;
logFile.close();
}
logFile.open("sharp_times.txt", std::ios_base::app);
if (logFile.is_open()) {
logFile << argv[1] << " ";
logFile << sharpduration.count() << endl;
logFile.close();
}
logFile.open("write_times.txt", std::ios_base::app);
if (logFile.is_open()) {
logFile << argv[1] << " ";
logFile << writeduration.count() << endl;
logFile.close();
}
return 0;
}

View File

View File

@ -77,9 +77,9 @@ float *S2_find_details(struct image_t *input_image, float *smoothened_image)
float *details = (float *)malloc(height * width * 3 * sizeof(float));
long ind = 0;
for (int i = 1; i < height - 1; i++)
for (int i = 0; i < height; i++)
{
for (int j = 1; j < width - 1; j++)
for (int j = 0; j < width; j++)
{
for (int k = 0; k < 3; k++)
{
@ -98,18 +98,18 @@ float *S3_sharpen(struct image_t *input_image, float *details_image)
int width = input_image->width;
int height = input_image->height;
float a = 0.8;
float a = 1;
float *sharp = (float *)malloc(height * width * 3 * sizeof(float));
long ind = 0;
for (int i = 1; i < height - 1; i++)
for (int i = 0; i < height; i++)
{
for (int j = 1; j < width - 1; j++)
for (int j = 0; j < width; j++)
{
for (int k = 0; k < 3; k++)
{
sharp[ind] = min((float)255, (float)input_image->image_pixels[i][j][k] + a * details_image[ind]);
sharp[ind] = min((float)255, (float)input_image->image_pixels[i][j][k] + details_image[ind]);
ind++;
}
}

View File

@ -16,7 +16,8 @@
using namespace std;
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
if (argc != 3)
{
@ -28,19 +29,6 @@ int main(int argc, char *argv[]) {
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";
@ -52,152 +40,169 @@ int main(int argc, char *argv[]) {
int reps = 1000;
// ---------------------------------------------------------------------------------
int shm_fd = shm_open(shm1_name, O_CREAT | O_RDWR, 0666);
if (shm_fd == -1) {
perror("shm_open");
return 1;
}
// 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;
}
// // Resize the shared memory object to the desired size
// if (ftruncate(shm_fd, size) == -1) {
// perror("ftruncate");
// return 1;
// }
// Map the shared memory object into the process address space
float *ptr = (float*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED) {
float *ptr = (float *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 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;
}
// ---------------------------------------------------------------------------------
// 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;
}
// // 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) {
float *ptr2 = (float *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 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)
sem_t *sem_parent_child = sem_open(sem1_name, O_CREAT | O_EXCL, 0666, 0); // Initially locked (0)
sem_t *sem_child_grandchild = sem_open(sem2_name, O_CREAT | O_EXCL, 0666, 0); // Initially locked (0)
if (sem_parent_child == SEM_FAILED || sem_child_grandchild == SEM_FAILED) {
if (sem_parent_child == SEM_FAILED || sem_child_grandchild == SEM_FAILED)
{
perror("sem_open failed");
exit(EXIT_FAILURE);
}
pid_t CHILD;
CHILD = fork();
if (CHILD == -1) {
pid_t pid1;
pid1 = fork();
if (pid1 == -1)
{
perror("fork failed!");
exit(EXIT_FAILURE);
}
// Parent Process
if(CHILD != 0){
// Write data to the shared memory
for(int i = 0; i < reps; i++){
// Process 1
if (pid1 == 0)
{
for (int i = 0; i < reps; i++)
{
float *smoothImage = S1_smoothen(input_image);
memcpy(ptr, smoothImage, size);
sem_post(sem_parent_child);
}
return 0;
}
// Child process
else if (CHILD == 0) { /* Child reads from pipe */
pid_t GRAND_CHILD = fork();
else
{
pid_t pid2 = fork();
if (GRAND_CHILD == -1) {
if (pid2 == -1)
{
perror("fork failed!");
exit(EXIT_FAILURE);
}
// Child Process
if(GRAND_CHILD != 0){
for(int i = 0; i < reps; i++){
// Process 2
if (pid2 == 0)
{
for (int i = 0; i < reps; i++)
{
sem_wait(sem_parent_child);
float *details = S2_find_details(input_image, ptr);
memcpy(ptr2, details, 1024);
memcpy(ptr2, details, size);
sem_post(sem_child_grandchild); // Signal grandchild
}
exit(EXIT_SUCCESS);
return 0;
}
// GrandChild Process
else if (GRAND_CHILD == 0) {
else
{
float *sharp;
printf("GC reading...\n");
for(int i = 0; i < reps; i++){
for (int i = 0; i < reps; i++)
{
sem_wait(sem_child_grandchild);
sharp = S3_sharpen(input_image, ptr2);
}
printf("GC reading done...\n");
exit(EXIT_SUCCESS);
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];
}
}
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++];
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];
ind++;
}
}
}
printf("Writing output file...\n");
write_ppm_file(argv[2], sharpened_image);
}
}
// Unmap the shared memory object
if (munmap(ptr, size) == -1) {
if (munmap(ptr, size) == -1)
{
perror("munmap");
return 1;
}
// Close the shared memory file descriptor
if (close(shm_fd) == -1) {
perror("close");
return 1;
}
// if (close(shm_fd) == -1) {
// perror("close");
// return 1;
// }
// Unmap the shared memory object
if (munmap(ptr2, size) == -1) {
if (munmap(ptr2, size) == -1)
{
perror("munmap");
return 1;
}
// Close the shared memory file descriptor
if (close(shm2_fd) == -1) {
perror("close");
return 1;
}
// if (close(shm2_fd) == -1) {
// perror("close");
// return 1;
// }
shm_unlink(shm1_name);
shm_unlink(shm2_name);
// shm_unlink(shm1_name);
// shm_unlink(shm2_name);
sem_close(sem_parent_child);
sem_close(sem_child_grandchild);

View File

@ -0,0 +1,192 @@
#include <iostream>
#include "libppm.h"
#include <cstdint>
#include <chrono>
#include <fstream>
using namespace std;
struct image_t *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];
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 = 0; i < height; i++)
{
for (int j = 0; j < width; 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] / 9 + input_image->image_pixels[i][j + 1][k] / 9 + input_image->image_pixels[i + 1][j][k] / 9 + input_image->image_pixels[i + 1][j + 1][k] / 9);
}
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] / 9 + input_image->image_pixels[i - 1][j + 1][k] / 9 + input_image->image_pixels[i][j][k] / 9 + input_image->image_pixels[i][j + 1][k] / 9);
}
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] / 9 + input_image->image_pixels[i][j][k] / 9 + input_image->image_pixels[i + 1][j - 1][k] / 9 + input_image->image_pixels[i + 1][j][k] / 9);
}
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] / 9 + input_image->image_pixels[i - 1][j][k] / 9 + input_image->image_pixels[i][j - 1][k] / 9 + input_image->image_pixels[i][j][k] / 9);
}
else if (i == 0)
{ // i - 1 does not exist
smoother->image_pixels[i][j][k] = (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);
}
else if (j == 0)
{ // j -1 does not exist
smoother->image_pixels[i][j][k] = (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][k] / 9 + input_image->image_pixels[i][j + 1][k] / 9 + input_image->image_pixels[i + 1][j][k] / 9 + input_image->image_pixels[i + 1][j + 1][k] / 9);
}
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] / 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);
}
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] / 9 + input_image->image_pixels[i - 1][j][k] / 9 + input_image->image_pixels[i][j - 1][k] / 9 + input_image->image_pixels[i][j][k] / 9 + input_image->image_pixels[i + 1][j - 1][k] / 9 + input_image->image_pixels[i + 1][j][k] / 9);
}
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);
}
}
}
}
return smoother;
}
struct image_t *S2_find_details(struct image_t *input_image, struct image_t *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];
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
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]);
}
}
}
return details;
}
struct image_t *S3_sharpen(struct image_t *input_image, struct image_t *details_image)
{
// TODO
int width = input_image->width;
int height = input_image->height;
int a = 1;
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];
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
for (int k = 0; k < 3; k++)
{
sharp->image_pixels[i][j][k] = min(255, input_image->image_pixels[i][j][k] + a * details_image->image_pixels[i][j][k]);
}
}
}
return sharp;
}
void free_image(struct image_t *image) {
int height = image->height;
int width = image->width;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
delete[] image->image_pixels[i][j];
}
delete[] image->image_pixels[i];
}
delete[] image->image_pixels;
delete image;
}
int main(int argc, char **argv)
{
if (argc != 3)
{
cout << "usage: ./a.out <path-to-original-image> <path-to-transformed-image>\n\n";
exit(0);
}
auto start = chrono::high_resolution_clock::now();
struct image_t *input_image = read_ppm_file(argv[1]);
struct image_t *smoothened_image;
struct image_t *details_image;
struct image_t *sharpened_image;
for (int i = 0; i < 100; i++) {
smoothened_image = S1_smoothen(input_image);
details_image = S2_find_details(input_image, smoothened_image);
sharpened_image = S3_sharpen(input_image, details_image);
// Free memory to avoid memory leaks
free_image(smoothened_image);
free_image(details_image);
free_image(sharpened_image);
}
write_ppm_file(argv[2], sharpened_image);
auto end = std::chrono::high_resolution_clock::now();
chrono::duration<double> duration = end - start;
cout << "Total time: " << duration.count() << endl;
return 0;
}