<p>The issue is that on your system, writing out "\n" is writing out 0x0D0A, whereas on my system, it's only writing out 0x0A. If you explicitly write out 0x0A instead of "\n" I think it will work for you.</p>

<p>Here's a hex dump on macOS Mojave:</p>

<pre><code>50 36 0A 35 
31 32 20 35 
31 32 0A 32 
35 35 0A 
FF 00 00 // &lt;- pixels start here
FF 00 00 
FF 00 00 
FF 00 00 
FF 00 00 
FF 00 00
</code></pre>

CC BY-SA 4.0
2019-05-09T08:06:57.413Z
I'm not too familiar with c++ file i/o , but in c maybe it's the difference between creating the file with textmode, fopen(fname, "w"),  VS binary, fopen(fname, "wb"). It needs to be a binary file.
Simon F
CC BY-SA 4.0
2019-05-09T15:58:06.487Z
Yeah, I think that would work.
user1118321
CC BY-SA 4.0
2019-05-10T15:44:24.550Z
Thanks to both of you. Changing the opening the file with `ofs.open("./example.ppm", std::ofstream::binary);` did it.
Francis Moy
CC BY-SA 4.0
2019-05-09T00:59:46.153Z
2019-05-09T00:59:46.153Z
user1118321
<p>I'm going through the <a href="https://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/rasterization-stage" rel="nofollow noreferrer">rasterizer tutorial from Scratchapixel</a>, but I encountered a problem with the generation of the .ppm file. Since something was off, I reduced the example to the bare minimum, so I currently simply generate a 512x512 image of a plain color.</p>

<pre><code>#include &lt;fstream&gt;

using Rgb = unsigned char[3];
int main()
{
    const uint32_t w = 512;
    const uint32_t h = 512;

    Rgb *framebuffer = new Rgb[w * h];
    for (uint32_t j = 0; j &lt; h; ++j) {
        for (uint32_t i = 0; i &lt; w; ++i) {
            framebuffer[j * w + i][0] = static_cast&lt;unsigned char&gt;(255);
            framebuffer[j * w + i][1] = static_cast&lt;unsigned char&gt;(0);
            framebuffer[j * w + i][2] = static_cast&lt;unsigned char&gt;(0);
        }
    }

    std::ofstream ofs;
    ofs.open("./example.ppm");
    ofs &lt;&lt; "P6\n" &lt;&lt; w &lt;&lt; " " &lt;&lt; h &lt;&lt; "\n255\n";
    ofs.write((char*)framebuffer, w * h * 3);
    ofs.close();

    delete[] framebuffer;

    return 0;
}
</code></pre>

<p>The code above should generate a red square image, but if I open the file with Gimp, I get a green image.</p>

<p><a href="https://i.sstatic.net/LgOJV.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LgOJV.png" alt="enter image description here"></a></p>

<p>Do you have any idea of what's happening?</p>

<p><strong>EDIT</strong>: This is the .ppm file with a hex editor:</p>

<p><a href="https://i.sstatic.net/vA9dG.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vA9dG.png" alt="enter image description here"></a></p>

CC BY-SA 4.0
2019-05-07T05:16:18.190Z
Perhaps something is wrong (or set oddly) in Gimp? On macOS in the Finder and in Preview.app, it shows up as solid red, as expected.
user1118321
CC BY-SA 4.0
2019-05-08T13:40:19.020Z
I couldn't see a link to the binary file to check myself, but can you open it in a hex editor just to make sure you haven't shifted the data by a byte?
Simon F
CC BY-SA 4.0
2019-05-08T18:06:22.413Z
@user1118321 Thanks for pointing this out. How could you manage to open the file with the Preview.app? I'm trying but the app doesn't show any window.
Francis Moy
CC BY-SA 4.0
2019-05-08T18:15:47.443Z
@SimonF I added a screenshot of the binary file with an hex editor, and as far as I can tell, it should be fine... Don't you think?
Francis Moy
<c++><image-processing><raster-image>
Wrong color in .ppm file
Francis Moy
# Wrong color in .ppm file

(Asked by: username_0 on 2019-05-06)

I'm going through the [rasterizer tutorial from Scratchapixel](https://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/rasterization-stage), but I encountered a problem with the generation of the .ppm file. Since something was off, I reduced the example to the bare minimum, so I currently simply generate a 512x512 image of a plain color.

```
#include <fstream>

using Rgb = unsigned char[3];
int main()
{
    const uint32_t w = 512;
    const uint32_t h = 512;

    Rgb *framebuffer = new Rgb[w * h];
    for (uint32_t j = 0; j < h; ++j) {
        for (uint32_t i = 0; i < w; ++i) {
            framebuffer[j * w + i][0] = static_cast<unsigned char>(255);
            framebuffer[j * w + i][1] = static_cast<unsigned char>(0);
            framebuffer[j * w + i][2] = static_cast<unsigned char>(0);
        }
    }

    std::ofstream ofs;
    ofs.open("./example.ppm");
    ofs << "P6\n" << w << " " << h << "\n255\n";
    ofs.write((char*)framebuffer, w * h * 3);
    ofs.close();

    delete[] framebuffer;

    return 0;
}

```

The code above should generate a red square image, but if I open the file with Gimp, I get a green image.

[![enter image description here](https://i.sstatic.net/LgOJV.png)](https://i.sstatic.net/LgOJV.png)

Do you have any idea of what's happening?

**EDIT**: This is the .ppm file with a hex editor:

[![enter image description here](https://i.sstatic.net/vA9dG.png)](https://i.sstatic.net/vA9dG.png)

**Tags:** c++, image-processing, raster-image

> **Comments:**
> **username_1**: Perhaps something is wrong (or set oddly) in Gimp? On macOS in the Finder and in Preview.app, it shows up as solid red, as expected.  
> **username_2**: I couldn't see a link to the binary file to check myself, but can you open it in a hex editor just to make sure you haven't shifted the data by a byte?  
> **username_0**: @username_1 Thanks for pointing this out. How could you manage to open the file with the Preview.app? I'm trying but the app doesn't show any window.  
> **username_0**: @username_2 I added a screenshot of the binary file with an hex editor, and as far as I can tell, it should be fine... Don't you think?  

---

## 1 answers

### Answer by username_1 on 2019-05-09. Score: 2 | Accepted answer

The issue is that on your system, writing out "\n" is writing out 0x0D0A, whereas on my system, it's only writing out 0x0A. If you explicitly write out 0x0A instead of "\n" I think it will work for you.

Here's a hex dump on macOS Mojave:

```
50 36 0A 35 
31 32 20 35 
31 32 0A 32 
35 35 0A 
FF 00 00 // <- pixels start here
FF 00 00 
FF 00 00 
FF 00 00 
FF 00 00 
FF 00 00

```

> **Comments:**
> **username_2**: I'm not too familiar with c++ file i/o , but in c maybe it's the difference between creating the file with textmode, fopen(fname, "w"),  VS binary, fopen(fname, "wb"). It needs to be a binary file.  
> **username_1**: Yeah, I think that would work.  
> **username_0**: Thanks to both of you. Changing the opening the file with `ofs.open("./example.ppm", std::ofstream::binary);` did it.  

---

