first version

This commit is contained in:
Rajshekar K K 2024-07-16 17:53:34 +05:30
commit 1df416f718
12 changed files with 552949 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/src/a.out

962
images/1.ppm Normal file

File diff suppressed because one or more lines are too long

BIN
images/2.ppm Normal file

Binary file not shown.

BIN
images/3.ppm Normal file

Binary file not shown.

20666
images/4.ppm Normal file

File diff suppressed because one or more lines are too long

2128
images/5.ppm Normal file

File diff suppressed because one or more lines are too long

36083
images/6.ppm Normal file

File diff suppressed because one or more lines are too long

492904
images/7.ppm Normal file

File diff suppressed because one or more lines are too long

45
src/image_sharpener.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <iostream>
#include "libppm.h"
#include <cstdint>
using namespace std;
struct image_t* S1_smoothen(struct image_t *input_image)
{
// TODO
// remember to allocate space for smoothened_image. See read_ppm_file() in libppm.c for some help.
return 0;
}
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], sharpened_image);
return 0;
}

134
src/libppm.cpp Normal file
View File

@ -0,0 +1,134 @@
#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);
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
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
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