-1

Im writing some code in order to convert an ASCII PGM to a RAW PGM with grey values between 0 and 255 (saved as one byte). Reading the image in works totally fine, however, when saving it theres a strange problem: Everything works fine, unless i have to store a '10'. As soon as i do that, the whole image is shifted in a strange way: Each stored 10 will take up 2 bytes of storage instead of 1.

for (int y=0;y<gs.GetHeight();y++){
     for(int x=0;x<gs.GetWidth();x++){
                tempi=(int)(0.5+g(x,y)*255);
                ***if(tempi==10)tempi=9;
                c=tempi;out<<c;
}

The for loop goes through all pixels of the image, g(x,y) gets the greyvalue as a float between 0 and 1 from a class where the image is stored after being loaded. tempi is an int, c is an unsigned char. out is an ofstream. The line marked with '***' is not supposed to be there, however, if i leave it there it works, if not i get the image shifted as explained above.

Im not too used to C++ or programming at all and i dont have a clue what could be causing my problem. I appreciate any help!

edit: heres the picture im saving, the first is with all tens removed (it has the same shape as the original i loaded, the only difference is that it has no 10 as greyvalue anymore) and the second saved straight away. I converted it to JPG because i couldnt find a place to upload pgms. https://s18.directupload.net/images/190711/temp/fg27mx6q.jpg https://s18.directupload.net/images/190711/temp/q4imakj7.jpg

Jan Maier
  • 17
  • 1

0 Answers0