C image manipulation on pgm files