file read / write updated

This commit is contained in:
Rajshekar K K 2024-08-06 14:05:27 +05:30
parent 1df416f718
commit 4a3663485c
1 changed files with 15 additions and 5 deletions

View File

@ -81,7 +81,7 @@ struct image_t* read_ppm_file(char* path_to_input_file)
// get pixel values // get pixel values
val = skip_blanks_comments_while_reading(&read_stream); 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++) 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 val = read_stream.get(); //assuming maxval of <=255
} }
} }
}*/ }
read_stream.seekg(-1, ios_base::cur); //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.read(((char*)image->image_pixels), image->height * image->width * 3); //assuming maxval of <=255
read_stream.close(); 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"; //cout << "height = " << height_string << "\n";
write_stream.write(height_string.c_str(), height_string.length()); write_stream.write(height_string.c_str(), height_string.length());
write_stream.write("\n255\n", 5); 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(); write_stream.close();
} }
else else