dilevin / computer-graphics-raster-images

Computer Graphics Assignment about Raster Images
1 stars 7 forks source link

A1 output file cannot be opened #9

Closed wufengta closed 5 years ago

wufengta commented 5 years ago

I used open to open the filename, e.g,

std::ofstream file; file.open(filename); (code from the internet)

and then I used a while loop to go through every element in vector data, and used

file << data[i];

to write every element into the file. I also did flush and close to the stream, and there were truly something written into the output ppm file, but the ppm file cannot be opened at all. The size of each ppm file is about 432 kb, while the gray one is 1/3 of the normal size. When I tried to open the ppm file, there is a tip says "load meta information failed". Any ideas?

dilevin commented 5 years ago

Googling this error message will show you that it indicates the format of your output file is not correct. A good way to debug this is to write a simple example file in ascii format and then check (via text editor) that it is in the proper ppm format.

wufengta commented 5 years ago

I know what the error means...its kind of like some pre-specificed valued for the picture is wrong, e.g, maybe the length or the height. But I checked many times, what I did is just write every byte from data to the output file using a while loop, how can that even be wrong... I tried to open the output file in cat cmd, but it is a mess. The original dog.png file is also a mess if I use cat cmd. I also cannot use the txt file as an input since this will fail the main.cpp in height and width checking part, but I tested with file << "whatever" and the output txt file is working well...

dilevin commented 5 years ago

PNG and PPM are different file formats. Hence examining dog.png will not give you any hints regarding the desired form of a .ppm file. Instead make sure you are exactly replicating the appropriate .ppm file format (linked from the main page). If your code is doing this correctly, you should get a readable image.

dilevin commented 5 years ago

I’ll repost the link here: https://en.m.wikipedia.org/wiki/Netpbm_format#File_format_description

Note that the P3 ppm format is ASCII and thus human readable. It should not be a ‘mess’ if written out correctly.

wufengta commented 5 years ago

is it so difficult to tell me that I need to add ppm header to the file?? 3 lines of code to add headers will fix the problem and it took me three days to know I need to add the header... (╯°□°)╯︵ ┻━┻ but still thanks for your help.