lab4 init

This commit is contained in:
jazzy1902 2024-10-08 17:05:44 +05:30
parent 785b4a63ac
commit 9ea749581f
27 changed files with 553902 additions and 0 deletions

59
lab4/.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,59 @@
{
"files.associations": {
"iostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"format": "cpp"
}
}

Binary file not shown.

962
lab4/images/1.ppm Normal file

File diff suppressed because one or more lines are too long

BIN
lab4/images/1rotated.ppm Normal file

Binary file not shown.

BIN
lab4/images/2.ppm Normal file

Binary file not shown.

BIN
lab4/images/2rotated.ppm Normal file

Binary file not shown.

BIN
lab4/images/3.ppm Normal file

Binary file not shown.

BIN
lab4/images/3rotated.ppm Normal file

Binary file not shown.

20666
lab4/images/4.ppm Normal file

File diff suppressed because one or more lines are too long

2128
lab4/images/5.ppm Normal file

File diff suppressed because one or more lines are too long

36083
lab4/images/6.ppm Normal file

File diff suppressed because one or more lines are too long

492904
lab4/images/7.ppm Normal file

File diff suppressed because one or more lines are too long

117
lab4/src/coms.cpp Normal file
View File

@ -0,0 +1,117 @@
#include "important.h"
#include <iostream>
#include <cstdint>
#include <chrono>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
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();
int pipe1[2];
int pipe2[2];
char buf;
pid_t CHILD;
if (pipe(pipe1) == -1) {
perror("pipe1");
exit(EXIT_FAILURE);
}
CHILD = fork();
if (CHILD == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (CHILD == 0) { /* Child reads from pipe */
if (pipe(pipe2) == -1) {
perror("pipe2");
exit(EXIT_FAILURE);
}
pid_t GRAND_CHILD = fork();
if (GRAND_CHILD == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (GRAND_CHILD == 0) { /* Child reads from pipe */
close(pipe1[1]); /* Close unused write end for s1_s2 pipe */
close(pipe1[0]); /* Close unused read end for s1_s2 pipe */
close(pipe2[1]); /* Close unused write end for s2_s3 pipe */
char to_print[] = "C : x\n";
char buf1;
while (read(pipe2[0], &buf1, 1) > 0)
{
cout << "in child ka child" << endl;
to_print[4] = buf;
}
close(pipe2[0]);
exit(EXIT_SUCCESS);
}
else {
close(pipe1[1]); /* Close unused write end for s1_s2 pipe */
close(pipe2[0]); /* Close unused read end for s2_s3 pipe */
char to_print[] = "C : x\n";
while (read(pipe1[0], &buf, 1) > 0)
{
printf("child reading from pipe\n");
to_print[4] = buf;
write(STDOUT_FILENO, &to_print, strlen(to_print));
}
printf("child writing to pipe\n");
write(STDOUT_FILENO, "\n", 1);
write(pipe2[1], argv[1], strlen(argv[1]));
close(pipe1[0]);
close(pipe2[1]);
wait(NULL);
exit(EXIT_SUCCESS);
}
}
else { /* Parent writes argv[1] to pipe */
close(pipe1[0]); /* Close unused read end of pipe 1 */
close(pipe2[0]); /* Close unused read end of pipe 2*/
close(pipe2[1]); /* Close unused write end of pipe 2*/
printf("parent writing to pipe\n");
write(pipe1[1], argv[1], strlen(argv[1]));
close(pipe1[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
}
cout << "success" << endl;
return 0;
}

38
lab4/src/detail_times.txt Normal file
View File

@ -0,0 +1,38 @@
../images/1.ppm 0.0240894
../images/2.ppm 0.0798028
../images/3.ppm 0.136535
../images/4.ppm 0.248314
../images/5.ppm 0.363927
../images/6.ppm 0.814739
../images/7.ppm 3.20648
../images/1.ppm 0.0255179
../images/2.ppm 0.082453
../images/3.ppm 0.187293
../images/4.ppm 0.504723
../images/5.ppm 0.421034
../images/6.ppm 0.905171
../images/7.ppm 4.12279
../images/1.ppm 0.0105311
../images/2.ppm 0.0422888
../images/3.ppm 0.0912897
../images/4.ppm 0.204927
../images/5.ppm 0.497162
../images/6.ppm 0.452158
../images/7.ppm 1.66216
../images/1.ppm 0.0135349
../images/2.ppm 0.0437362
../images/3.ppm 0.0821199
../images/4.ppm 0.54249
../images/5.ppm 0.39126
../images/6.ppm 0.555806
../images/7.ppm 2.16977
../images/1.ppm 0.0217065
../images/2.ppm 0.0748607
../images/3.ppm 0.174253
../images/4.ppm 0.201994
../images/5.ppm 0.352959
../images/6.ppm 0.497504
../images/7.ppm 1.79435
../images/1.ppm 0.00378666
../images/2.ppm 0.0142673
../images/3.ppm 0.0612914

26
lab4/src/graph.py Normal file
View File

@ -0,0 +1,26 @@
import matplotlib.pyplot as plt
pixels = [81000, 308898, 588800, 1164800, 1500000, 3406500, 9262500]
read_time = [0.0372315, 0.1104362, 0.162849, 0.3120326, 0.4681494, 0.9840036, 1.665642]
smoothened_time = [0.03347448, 0.1221104, 0.2514082, 0.4795412, 0.6044902, 1.1334092, 2.453936]
details_time = [0.01907596, 0.0646283, 0.13429812, 0.3404896, 0.4052684, 0.6450756, 2.591112]
sharpened_time = [0.02013736, 0.07459558, 0.13045596, 0.2987406, 0.4569202, 0.699245, 3.019178]
write_time = [0.010278664, 0.03954574, 0.05814792, 0.1842653, 0.2484992, 0.304828, 1.119439]
plt.figure(figsize=(12, 8))
plt.plot(pixels, read_time, marker='o', label='Read Time')
plt.plot(pixels, smoothened_time, marker='o', label='Smoothened Time')
plt.plot(pixels, details_time, marker='o', label='Details Time')
plt.plot(pixels, sharpened_time, marker='o', label='Sharpened Time')
plt.plot(pixels, write_time, marker='o', label='Write Time')
plt.xlabel('Pixels')
plt.ylabel('Time (seconds)')
plt.title('Pixels vs Time')
plt.legend()
plt.grid(True)
plt.xscale('log')
plt.yscale('log')
plt.show()

View File

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

277
lab4/src/important.cpp Normal file
View File

@ -0,0 +1,277 @@
#include <iostream>
#include "important.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* 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
}
uint8_t skip_blanks_comments_while_reading(ifstream *read_stream) //returns the byte at the first position after the skipping of the blank space
{
uint8_t val;
while(true)
{
val = read_stream->get();
if(val == '#')
{
while(val != '\n')
{
val = read_stream->get();
}
}
if(val == '\n' || val == ' ' || val == '\t')
{
continue;
}
else if(val != '#')
return val;
}
return val;
}
struct image_t* read_ppm_file(char* path_to_input_file)
{
//cout << "input image file = " << path_to_input_file << "\n";
ifstream read_stream(path_to_input_file, ios::binary | ios::in);
if(read_stream.is_open())
{
struct image_t* image = new struct image_t;
uint8_t val = skip_blanks_comments_while_reading(&read_stream); // 'P'
val = read_stream.get(); //'6'
// width
val = skip_blanks_comments_while_reading(&read_stream);
while(true)
{
if(val == ' ' || val == '\t' || val == '\n')
break;
image->width = image->width * 10 + (val - '0');
val = read_stream.get();
}
//cout << "width = " << image->width << "\n";
// height
val = skip_blanks_comments_while_reading(&read_stream);
while(true)
{
if(val == ' ' || val == '\t' || val == '\n')
break;
image->height = image->height * 10 + (val - '0');
val = read_stream.get();
}
//cout << "height = " << image->height << "\n";
image->image_pixels = new uint8_t**[image->height];
for(int i = 0; i < image->height; i++)
{
image->image_pixels[i] = new uint8_t*[image->width];
for(int j = 0; j < image->width; j++)
image->image_pixels[i][j] = new uint8_t[3];
}
// maxval
val = skip_blanks_comments_while_reading(&read_stream);
while(true)
{
if(val == ' ' || val == '\t' || val == '\n')
break;
val = read_stream.get();
}
// get pixel values
val = skip_blanks_comments_while_reading(&read_stream);
for(int i = 0; i < image->height; i++)
{
for(int j = 0; j < image->width; j++)
{
for(int k = 0; k < 3; k++)
{
image->image_pixels[i][j][k] = val;
val = read_stream.get(); //assuming maxval of <=255
}
}
}
//read_stream.seekg(-1, ios_base::cur);
//read_stream.read(((char*)image->image_pixels), image->height * image->width * 3); //assuming maxval of <=255
read_stream.close();
return image;
}
else
{
cerr << "failed to open file " << path_to_input_file << "\n\n";
exit(1);
}
}
void write_ppm_file(char * path_to_output_file, struct image_t* image)
{
//cout << "output image file = " << path_to_output_file << "\n";
ofstream write_stream(path_to_output_file, ios::binary | ios::out);
if(write_stream.is_open())
{
write_stream.write("P6\n", 3);
std::string width_string = std::to_string(image->width);
//cout << "width = " << width_string << "\n";
write_stream.write(width_string.c_str(), width_string.length());
write_stream.write(" ", 1);
std::string height_string = std::to_string(image->height);
//cout << "height = " << height_string << "\n";
write_stream.write(height_string.c_str(), height_string.length());
write_stream.write("\n255\n", 5);
for(int i = 0; i < image->height; i++)
{
for(int j = 0; j < image->width; j++)
{
for(int k = 0; k < 3; k++)
{
write_stream.put(image->image_pixels[i][j][k]); //assuming maxval of <=255
}
}
}
//write_stream.write(((char*)image->image_pixels), image->height * image->width * 3);
write_stream.close();
}
else
{
cerr << "failed to open file " << path_to_output_file << "\n\n";
exit(1);
}
}

19
lab4/src/important.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef IMPORTANT_H
#define IMPORTANT_H
#include <cstdint>
struct image_t
{
int width;
int height;
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);
struct image_t* read_ppm_file(char* path_to_input_file);
void write_ppm_file(char* path_to_output_file, struct image_t* image);
#endif

144
lab4/src/libppm.cpp Normal file
View File

@ -0,0 +1,144 @@
#include "libppm.h"
#include <iostream>
#include <fstream>
using namespace std;
uint8_t skip_blanks_comments_while_reading(ifstream *read_stream) //returns the byte at the first position after the skipping of the blank space
{
uint8_t val;
while(true)
{
val = read_stream->get();
if(val == '#')
{
while(val != '\n')
{
val = read_stream->get();
}
}
if(val == '\n' || val == ' ' || val == '\t')
{
continue;
}
else if(val != '#')
return val;
}
return val;
}
struct image_t* read_ppm_file(char* path_to_input_file)
{
//cout << "input image file = " << path_to_input_file << "\n";
ifstream read_stream(path_to_input_file, ios::binary | ios::in);
if(read_stream.is_open())
{
struct image_t* image = new struct image_t;
uint8_t val = skip_blanks_comments_while_reading(&read_stream); // 'P'
val = read_stream.get(); //'6'
// width
val = skip_blanks_comments_while_reading(&read_stream);
while(true)
{
if(val == ' ' || val == '\t' || val == '\n')
break;
image->width = image->width * 10 + (val - '0');
val = read_stream.get();
}
//cout << "width = " << image->width << "\n";
// height
val = skip_blanks_comments_while_reading(&read_stream);
while(true)
{
if(val == ' ' || val == '\t' || val == '\n')
break;
image->height = image->height * 10 + (val - '0');
val = read_stream.get();
}
//cout << "height = " << image->height << "\n";
image->image_pixels = new uint8_t**[image->height];
for(int i = 0; i < image->height; i++)
{
image->image_pixels[i] = new uint8_t*[image->width];
for(int j = 0; j < image->width; j++)
image->image_pixels[i][j] = new uint8_t[3];
}
// maxval
val = skip_blanks_comments_while_reading(&read_stream);
while(true)
{
if(val == ' ' || val == '\t' || val == '\n')
break;
val = read_stream.get();
}
// get pixel values
val = skip_blanks_comments_while_reading(&read_stream);
for(int i = 0; i < image->height; i++)
{
for(int j = 0; j < image->width; j++)
{
for(int k = 0; k < 3; k++)
{
image->image_pixels[i][j][k] = val;
val = read_stream.get(); //assuming maxval of <=255
}
}
}
//read_stream.seekg(-1, ios_base::cur);
//read_stream.read(((char*)image->image_pixels), image->height * image->width * 3); //assuming maxval of <=255
read_stream.close();
return image;
}
else
{
cerr << "failed to open file " << path_to_input_file << "\n\n";
exit(1);
}
}
void write_ppm_file(char * path_to_output_file, struct image_t* image)
{
//cout << "output image file = " << path_to_output_file << "\n";
ofstream write_stream(path_to_output_file, ios::binary | ios::out);
if(write_stream.is_open())
{
write_stream.write("P6\n", 3);
std::string width_string = std::to_string(image->width);
//cout << "width = " << width_string << "\n";
write_stream.write(width_string.c_str(), width_string.length());
write_stream.write(" ", 1);
std::string height_string = std::to_string(image->height);
//cout << "height = " << height_string << "\n";
write_stream.write(height_string.c_str(), height_string.length());
write_stream.write("\n255\n", 5);
for(int i = 0; i < image->height; i++)
{
for(int j = 0; j < image->width; j++)
{
for(int k = 0; k < 3; k++)
{
write_stream.put(image->image_pixels[i][j][k]); //assuming maxval of <=255
}
}
}
//write_stream.write(((char*)image->image_pixels), image->height * image->width * 3);
write_stream.close();
}
else
{
cerr << "failed to open file " << path_to_output_file << "\n\n";
exit(1);
}
}

15
lab4/src/libppm.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef LIBPPM_H
#define LIBPPM_H
#include <cstdint>
struct image_t
{
int width;
int height;
uint8_t*** image_pixels;
};
struct image_t* read_ppm_file(char* path_to_input_file);
void write_ppm_file(char* path_to_output_file, struct image_t* image);
#endif

18
lab4/src/makefile Normal file
View File

@ -0,0 +1,18 @@
build-sharpen: ./a.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)
clean:
rm a.out coms.out
.PHONY: run-coms

38
lab4/src/read_times.txt Normal file
View File

@ -0,0 +1,38 @@
../images/1.ppm 0.0436004
../images/2.ppm 0.200632
../images/3.ppm 0.142189
../images/4.ppm 0.328569
../images/5.ppm 0.584479
../images/6.ppm 1.57548
../images/7.ppm 1.44286
../images/1.ppm 0.0362725
../images/2.ppm 0.121128
../images/3.ppm 0.199664
../images/4.ppm 0.402522
../images/5.ppm 0.51656
../images/6.ppm 0.831204
../images/7.ppm 1.68258
../images/1.ppm 0.0502397
../images/2.ppm 0.0540259
../images/3.ppm 0.104212
../images/4.ppm 0.217633
../images/5.ppm 0.566316
../images/6.ppm 1.06233
../images/7.ppm 1.76924
../images/1.ppm 0.0191352
../images/2.ppm 0.0632993
../images/3.ppm 0.0990502
../images/4.ppm 0.304179
../images/5.ppm 0.306761
../images/6.ppm 0.740531
../images/7.ppm 1.62881
../images/1.ppm 0.0369097
../images/2.ppm 0.113096
../images/3.ppm 0.26913
../images/4.ppm 0.30726
../images/5.ppm 0.366631
../images/6.ppm 0.711473
../images/7.ppm 1.80572
../images/1.ppm 0.0197896
../images/2.ppm 0.0329337
../images/3.ppm 0.199503

24
lab4/src/runall.sh Normal file
View File

@ -0,0 +1,24 @@
#!/bin/bash
# List of input and output file pairs
input_files=("1" "2" "3" "4" "5" "6" "7")
output_files=("output1" "output2" "output3" "output4" "output5" "output6" "output7")
# Loop through each file pair and run the make command
for i in "${!input_files[@]}"
do
input_file="${input_files[$i]}"
output_file="${output_files[$i]}"
echo "Running make for input: $input_file and output: $output_file"
make run-sharpen INPUT="$input_file" OUTPUT="$output_file"
# Check if make was successful
if [ $? -ne 0 ]; then
echo "Make failed for input: $input_file and output: $output_file"
exit 1
fi
done
echo "Make completed successfully for all file pairs."

38
lab4/src/sharp_times.txt Normal file
View File

@ -0,0 +1,38 @@
../images/1.ppm 0.0285746
../images/2.ppm 0.118526
../images/3.ppm 0.133181
../images/4.ppm 0.333111
../images/5.ppm 0.687696
../images/6.ppm 0.919815
../images/7.ppm 5.18353
../images/1.ppm 0.0278552
../images/2.ppm 0.080822
../images/3.ppm 0.201076
../images/4.ppm 0.509533
../images/5.ppm 0.535679
../images/6.ppm 0.747966
../images/7.ppm 4.45165
../images/1.ppm 0.0100292
../images/2.ppm 0.0381673
../images/3.ppm 0.0739177
../images/4.ppm 0.223357
../images/5.ppm 0.433985
../images/6.ppm 0.450129
../images/7.ppm 1.67468
../images/1.ppm 0.0115579
../images/2.ppm 0.040362
../images/3.ppm 0.0973441
../images/4.ppm 0.257049
../images/5.ppm 0.231088
../images/6.ppm 0.573983
../images/7.ppm 2.23737
../images/1.ppm 0.0226699
../images/2.ppm 0.0941006
../images/3.ppm 0.146761
../images/4.ppm 0.170653
../images/5.ppm 0.396153
../images/6.ppm 0.804332
../images/7.ppm 2.54863
../images/1.ppm 0.00357817
../images/2.ppm 0.0139955
../images/3.ppm 0.0578726

38
lab4/src/smooth_times.txt Normal file
View File

@ -0,0 +1,38 @@
../images/1.ppm 0.0422791
../images/2.ppm 0.118747
../images/3.ppm 0.26533
../images/4.ppm 0.377423
../images/5.ppm 0.673011
../images/6.ppm 1.36309
../images/7.ppm 2.34904
../images/1.ppm 0.0396053
../images/2.ppm 0.14362
../images/3.ppm 0.398949
../images/4.ppm 0.59141
../images/5.ppm 0.730647
../images/6.ppm 0.869892
../images/7.ppm 2.32653
../images/1.ppm 0.0165813
../images/2.ppm 0.102202
../images/3.ppm 0.143081
../images/4.ppm 0.43276
../images/5.ppm 0.646602
../images/6.ppm 1.36732
../images/7.ppm 2.33917
../images/1.ppm 0.0341145
../images/2.ppm 0.079444
../images/3.ppm 0.163173
../images/4.ppm 0.501014
../images/5.ppm 0.52402
../images/6.ppm 1.03143
../images/7.ppm 2.75437
../images/1.ppm 0.0347922
../images/2.ppm 0.166539
../images/3.ppm 0.287508
../images/4.ppm 0.495099
../images/5.ppm 0.448171
../images/6.ppm 1.03631
../images/7.ppm 2.50061
../images/1.ppm 0.00590421
../images/2.ppm 0.0222273
../images/3.ppm 0.0992293

26
lab4/src/text_parser.py Normal file
View File

@ -0,0 +1,26 @@
import os
from collections import defaultdict
file_names = ["read_times.txt", "sharp_times.txt", "smooth_times.txt", "detail_times.txt", "write_times.txt"]
def calculate_average_times(file_path):
times = defaultdict(list)
with open(file_path, 'r') as file:
for line in file:
parts = line.strip().split()
if len(parts) == 2:
image, time = parts
times[image].append(float(time))
average_times = {image: sum(time_list) / len(time_list) for image, time_list in times.items()}
return average_times
for file_name in file_names:
average_times = calculate_average_times(file_name)
print(f"Averages for {file_name}:")
for image, avg_time in sorted(average_times.items()):
print(f"{image}: {avg_time}")
print()

38
lab4/src/write_times.txt Normal file
View File

@ -0,0 +1,38 @@
../images/1.ppm 0.0129698
../images/2.ppm 0.0731423
../images/3.ppm 0.0586685
../images/4.ppm 0.391597
../images/5.ppm 0.441327
../images/6.ppm 0.362438
../images/7.ppm 1.01397
../images/1.ppm 0.0104656
../images/2.ppm 0.0389386
../images/3.ppm 0.102665
../images/4.ppm 0.229473
../images/5.ppm 0.370227
../images/6.ppm 0.359944
../images/7.ppm 1.65748
../images/1.ppm 0.00587877
../images/2.ppm 0.0390165
../images/3.ppm 0.0345854
../images/4.ppm 0.0927168
../images/5.ppm 0.184159
../images/6.ppm 0.231483
../images/7.ppm 1.05401
../images/1.ppm 0.00660735
../images/2.ppm 0.0207068
../images/3.ppm 0.0402921
../images/4.ppm 0.0976897
../images/5.ppm 0.119102
../images/6.ppm 0.249217
../images/7.ppm 0.822115
../images/1.ppm 0.0164718
../images/2.ppm 0.0259248
../images/3.ppm 0.0545286
../images/4.ppm 0.10985
../images/5.ppm 0.127681
../images/6.ppm 0.322058
../images/7.ppm 1.0496
../images/1.ppm 0.00731258
../images/2.ppm 0.02392
../images/3.ppm 0.152686