From 4a3663485c564ec1ff954163f59c305e897dd4da Mon Sep 17 00:00:00 2001 From: Rajshekar Kalayappan Date: Tue, 6 Aug 2024 14:05:27 +0530 Subject: [PATCH] file read / write updated --- src/libppm.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/libppm.cpp b/src/libppm.cpp index 32c1702..9268f79 100644 --- a/src/libppm.cpp +++ b/src/libppm.cpp @@ -81,7 +81,7 @@ struct image_t* read_ppm_file(char* path_to_input_file) // get pixel values val = skip_blanks_comments_while_reading(&read_stream); - /*for(int i = 0; i < image->height; i++) + for(int i = 0; i < image->height; i++) { for(int j = 0; j < image->width; j++) { @@ -91,9 +91,9 @@ struct image_t* read_ppm_file(char* path_to_input_file) 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.seekg(-1, ios_base::cur); + //read_stream.read(((char*)image->image_pixels), image->height * image->width * 3); //assuming maxval of <=255 read_stream.close(); @@ -123,7 +123,17 @@ void write_ppm_file(char * path_to_output_file, struct image_t* image) //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); + 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