Compare commits

..

No commits in common. "032891872f041a1fb202dc0550a19ece734440b8" and "c30932de97e4bd50dc82b2cb5cbe17736df22882" have entirely different histories.

9 changed files with 255 additions and 196 deletions

View File

@ -5,7 +5,6 @@
"iostream": "cpp", "iostream": "cpp",
"iosfwd": "cpp", "iosfwd": "cpp",
"fstream": "cpp", "fstream": "cpp",
"xlocmes": "cpp", "xlocmes": "cpp"
"utility": "cpp"
} }
} }

Binary file not shown.

BIN
lab4/images/1rotated.ppm Normal file

Binary file not shown.

BIN
lab4/images/2rotated.ppm Normal file

Binary file not shown.

BIN
lab4/images/3rotated.ppm Normal file

Binary file not shown.

View File

@ -1,14 +1,3 @@
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 build-sharpen: ./a.out
./a.out: image_sharpener.cpp libppm.cpp ./a.out: image_sharpener.cpp libppm.cpp

View File

@ -6,76 +6,23 @@
using namespace std; using namespace std;
float *S1_smoothen(struct image_t *input_image) 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 width = input_image->width;
int height = input_image->height; int height = input_image->height;
// memory allocation // memory allocation
float *smoother = (float *)malloc(height * width * 3 * sizeof(float)); struct image_t *smoother = new struct image_t;
long ind = 0; smoother->height = height;
smoother->width = width;
smoother->image_pixels = new uint8_t **[height];
for (int i = 0; i < height; i++) for (int i = 0; i < height; i++)
{ {
smoother->image_pixels[i] = new uint8_t *[width];
for (int j = 0; j < width; j++) for (int j = 0; j < width; j++)
{ smoother->image_pixels[i][j] = new uint8_t[3];
for (int k = 0; k < 3; k++)
{
// edge cases
if (i == 0 && j == 0)
{ // i-1 and j - 1 doesnt exist
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[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[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[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[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[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[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[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[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;
}
float *S2_find_details(struct image_t *input_image, float *smoothened_image)
{
// TODO
int width = input_image->width;
int height = input_image->height;
float *details = (float *)malloc(height * width * 3 * sizeof(float));
long ind = 0;
for (int i = 1; i < height - 1; i++) for (int i = 1; i < height - 1; i++)
{ {
@ -83,8 +30,79 @@ float *S2_find_details(struct image_t *input_image, float *smoothened_image)
{ {
for (int k = 0; k < 3; k++) for (int k = 0; k < 3; k++)
{ {
details[ind] = max((float)0, (float)input_image->image_pixels[i][j][k] - smoothened_image[ind]); // image->image_pixels[i][j][k] = val;
ind++; // 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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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]);
} }
} }
} }
@ -92,25 +110,33 @@ float *S2_find_details(struct image_t *input_image, float *smoothened_image)
return details; return details;
} }
float *S3_sharpen(struct image_t *input_image, float *details_image) struct image_t *S3_sharpen(struct image_t *input_image, struct image_t *details_image)
{ {
// TODO // TODO
int width = input_image->width; int width = input_image->width;
int height = input_image->height; int height = input_image->height;
float a = 0.8; int a = 1;
float *sharp = (float *)malloc(height * width * 3 * sizeof(float)); 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];
}
long ind = 0;
for (int i = 1; i < height - 1; i++) for (int i = 1; i < height - 1; i++)
{ {
for (int j = 1; j < width - 1; j++) for (int j = 1; j < width - 1; j++)
{ {
for (int k = 0; k < 3; k++) 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->image_pixels[i][j][k] = min(255, input_image->image_pixels[i][j][k] + details_image->image_pixels[i][j][k]) * a;
ind++;
} }
} }
} }

View File

@ -9,9 +9,9 @@ struct image_t
uint8_t*** image_pixels; uint8_t*** image_pixels;
}; };
float* S1_smoothen(struct image_t *input_image); struct image_t* S1_smoothen(struct image_t *input_image);
float* S2_find_details(struct image_t *input_image, float *smoothened_image); struct image_t* S2_find_details(struct image_t *input_image, struct image_t *smoothened_image);
float* S3_sharpen(struct image_t *input_image, float *details_image); struct image_t* S3_sharpen(struct image_t *input_image, struct image_t *details_image);
struct image_t* read_ppm_file(char* path_to_input_file); struct image_t* read_ppm_file(char* path_to_input_file);
void write_ppm_file(char* path_to_output_file, struct image_t* image); void write_ppm_file(char* path_to_output_file, struct image_t* image);

View File

@ -18,40 +18,19 @@ using namespace std;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if(argc != 3) // if(argc != 3)
{ // {
cout << "usage: ./a.out <path-to-original-image> <path-to-transformed-image>\n\n"; // cout << "usage: ./a.out <path-to-original-image> <path-to-transformed-image>\n\n";
exit(0); // 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 *shm1_name = "/shm_parent_child";
const char *shm2_name = "/shm_child_grandchild"; const char *shm2_name = "/shm_child_grandchild";
const char *sem1_name = "/sem_parent_child"; const char *sem1_name = "/sem_parent_child";
const char *sem2_name = "/sem_child_grandchild"; 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); int shm_fd = shm_open(shm1_name, O_CREAT | O_RDWR, 0666);
if (shm_fd == -1) { if (shm_fd == -1) {
perror("shm_open"); perror("shm_open");
@ -65,33 +44,12 @@ int main(int argc, char *argv[]) {
} }
// Map the shared memory object into the process address space // 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); void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED) { if (ptr == MAP_FAILED) {
perror("mmap"); perror("mmap");
return 1; 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_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_child_grandchild = sem_open(sem2_name, O_CREAT, 0666, 0); // Initially locked (0)
@ -101,6 +59,9 @@ 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; pid_t CHILD;
CHILD = fork(); CHILD = fork();
if (CHILD == -1) { if (CHILD == -1) {
@ -110,14 +71,37 @@ int main(int argc, char *argv[]) {
// Parent Process // Parent Process
if(CHILD != 0){ if(CHILD != 0){
char buffer[1024] = {'p', 'a', 'c', 'k', 'e', 't', '\0'};
buffer[7] = '\0';
// Write data to the shared memory // Write data to the shared memory
for(int i = 0; i < reps; i++){ 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
float* smoothImage = S1_smoothen(input_image); // Optional: Print what is being written to shared memory for verification
memcpy(ptr, smoothImage, size); printf("P - Writing to shared memory: %s\n", (char *)ptr);
sem_post(sem_parent_child); 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 // Child process
@ -131,58 +115,68 @@ int main(int argc, char *argv[]) {
// Child Process // Child Process
if(GRAND_CHILD != 0){ 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;
}
for(int i = 0; i < reps; i++){ // 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
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); sem_wait(sem_parent_child);
float *details = S2_find_details(input_image, ptr); // Print the contents of the shared memory
memcpy(ptr2, details, 1024); char *packet = (char *)ptr;
sem_post(sem_child_grandchild); // Signal grandchild 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
} }
exit(EXIT_SUCCESS);
}
// GrandChild Process
else if (GRAND_CHILD == 0) {
printf("GC reading...\n");
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);
}
}
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 // Unmap the shared memory object
if (munmap(ptr, size) == -1) { if (munmap(ptr, size) == -1) {
perror("munmap"); perror("munmap");
return 1; return 1;
} }
// Close the shared memory file descriptor // Close the shared memory file descriptor
if (close(shm_fd) == -1) { if (close(shm_fd) == -1) {
perror("close"); perror("close");
return 1; return 1;
} }
// printf("child writing to shm\n");
// Unmap the shared memory object // Unmap the shared memory object
if (munmap(ptr2, size) == -1) { if (munmap(ptr2, size) == -1) {
@ -197,7 +191,58 @@ int main(int argc, char *argv[]) {
} }
shm_unlink(shm1_name); shm_unlink(shm1_name);
sem_close(sem_parent_child);
sem_close(sem_child_grandchild);
wait(NULL);
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;
}
// 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++){
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);
}
// 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(shm2_name); shm_unlink(shm2_name);
sem_close(sem_parent_child);
sem_close(sem_child_grandchild);
exit(EXIT_SUCCESS);
}
}
sem_close(sem_parent_child); sem_close(sem_parent_child);
sem_close(sem_child_grandchild); sem_close(sem_child_grandchild);