c++file-formatcolor-spaceppmcolor-depth

PPM wrong color display


I get weird results when displaying a PPM:

pixeldisplay

(this is actually an upscaled PNG)

And here's what the file looks like:

textdisplay

The dimensions are fine (10 rows, 8 columns), just the colors are wrong (or am I wrong?).

As it says in the 3rd line of the file, I want to use a value from 0 to 255 per channel. Using an usual rgb color space like (255, 255, 255) for white, (0, 0, 0) for black, (255, 0, 0) for red and so on. But those colors in the image are apparently not the same as in the file.

Already at the first look, the image seems way to dark.

Do I misunderstand the format? Is the file not interpretated this way?


Solution

  • This document: http://netpbm.sourceforge.net/doc/ppm.html describes the PPM image format.

    When the "magic" value P6 is found at the beginning of the file, the color of the pixels is stored as binary data. Quoting the previously mentioned document (emphasis mine):

    [...] A raster of Height rows, in order from top to bottom. Each row consists of Width pixels, in order from left to right. Each pixel is a triplet of red, green, and blue samples, in that order. Each sample is represented in pure binary by either 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes. The most significant byte is first.

    The file showed by the asker seems to contain their decimal textual representation instead.

    So, the string "\n224 93 229..." (yes, I suspect the windows endline sequence "\r\n") is interpreted as (assuming that the file was saved in ASCII format) {10, 50, 50}, {50, 32, 32}, {57, 51, 32}, ....

    Note the blackish pixel in the middle, which probably correspond to the end of the first line, it may be a {13, 10, 32} ("\r\n ").

    If you change the magic value to P3, it should be interpreted correctly.