This commit is contained in:
ItsMAX0112 2024-08-13 15:33:27 +05:30
commit f21cd3abcc
15 changed files with 553104 additions and 0 deletions

962
lab1/images/1.ppm Normal file

File diff suppressed because one or more lines are too long

BIN
lab1/images/2.ppm Normal file

Binary file not shown.

BIN
lab1/images/3.ppm Normal file

Binary file not shown.

20666
lab1/images/4.ppm Normal file

File diff suppressed because one or more lines are too long

2128
lab1/images/5.ppm Normal file

File diff suppressed because one or more lines are too long

36083
lab1/images/6.ppm Normal file

File diff suppressed because one or more lines are too long

492904
lab1/images/7.ppm Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,112 @@
#include <iostream>
#include "libppm.h"
#include <cstdint>
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;
//cout << unsigned( input_image->image_pixels[i][j][k]) << endl;
//break;
// 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);
cout << unsigned(smoother->image_pixels[i][j][k]) << endl;
}
}
}
}
for(int i = 0; i < height; ++i) {
break;
for(int j = 0; j < width; ++j) {
cout << unsigned(smoother->image_pixels[i][j][0]) << "" ;
}
cout << endl;
}
return smoother;
}
struct image_t* S2_find_details(struct image_t *input_image, struct image_t *smoothened_image)
{
// TODO
return 0;
}
struct image_t* S3_sharpen(struct image_t *input_image, struct image_t *details_image)
{
// TODO
return input_image; //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);
}
struct image_t *input_image = read_ppm_file(argv[1]);
struct image_t *smoothened_image = S1_smoothen(input_image);
struct image_t *details_image = S2_find_details(input_image, smoothened_image);
struct image_t *sharpened_image = S3_sharpen(input_image, details_image);
write_ppm_file(argv[2], smoothened_image);
return 0;
}

144
lab1/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
lab1/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

10
lab1/src/makefile Normal file
View File

@ -0,0 +1,10 @@
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
clean:
rm a.out

1
lab2/src/file.txt Normal file

File diff suppressed because one or more lines are too long

13
lab2/src/makefile Normal file
View File

@ -0,0 +1,13 @@
build-part1: part1.out
part1.out: part1_searcher.cpp
g++ -g part1_searcher.cpp -o part1.out
run-part1: part1.out
./part1.out file.txt NGTNIJGK 0 67108863
clean-part1:
rm part1.out
clean:
rm *.out

38
lab2/src/partitioner.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
using namespace std;
int main(int argc, char **argv)
{
if(argc != 6)
{
cout <<"usage: ./partitioner.out <path-to-file> <pattern> <search-start-position> <search-end-position> <max-chunk-size>\nprovided arguments:\n";
for(int i = 0; i < argc; i++)
cout << argv[i] << "\n";
return -1;
}
char *file_to_search_in = argv[1];
char *pattern_to_search_for = argv[2];
int search_start_position = atoi(argv[3]);
int search_end_position = atoi(argv[4]);
int max_chunk_size = atoi(argv[5]);
//TODO
//cout << "[" << my_pid << "] start position = " << search_start_position << " ; end position = " << search_end_position << "\n";
//cout << "[" << my_pid << "] forked left child " << my_children[0] << "\n";
//cout << "[" << my_pid << "] forked right child " << my_children[1] << "\n";
//cout << "[" << my_pid << "] left child returned\n";
//cout << "[" << my_pid << "] right child returned\n";
//cout << "[" << my_pid << "] left child returned\n";
//cout << "[" << my_pid << "] right child returned\n";*/
//cout << "[" << my_pid << "] forked searcher child " << searcher_pid << "\n";
//cout << "[" << my_pid << "] searcher child returned \n";
//cout << "[" << my_pid << "] received SIGTERM\n"; //applicable for Part III of the assignment
return 0;
}

28
lab2/src/searcher.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <iostream>
#include <fstream>
#include <cstring>
#include <unistd.h>
#include <signal.h>
using namespace std;
int main(int argc, char **argv)
{
if(argc != 5)
{
cout <<"usage: ./partitioner.out <path-to-file> <pattern> <search-start-position> <search-end-position>\nprovided arguments:\n";
for(int i = 0; i < argc; i++)
cout << argv[i] << "\n";
return -1;
}
char *file_to_search_in = argv[1];
char *pattern_to_search_for = argv[2];
int search_start_position = atoi(argv[3]);
int search_end_position = atoi(argv[4]);
//TODO
cout << "[-1] didn't find\n";
return 0;
}