I've written a program that takes a PGM image as input and converts it to a PBM file. However, the image I get as output is incorrect.
I determine if a pixel is white if its value is bigger than (max+1)/2 then use putchar() to place the character with the value 0 and if its black 1(ive also tried max instead of 1 and 255). However, I get a bunch of vertical lines as output. I'm running this in a Linux terminal with the command :
./prog < image1.pgm > image2.pbm
This is the function I'm using to read and transform the image (where size is height and width, and max is the max value of each pixel):
void p5_to_p4(int size, int max){
int g1,g2,g3;
int i;
for(i=0; i<size; i++){
g1=getchar();
g2=getchar();
g3=getchar();
if (g1>((max+1)/2)){
putchar(0);
putchar(0);
putchar(0);
}
else {
putchar(max);
putchar(max);
putchar(max);
}
}
}
this is the output image im getting(in jpeg form): output when this is what i should be getting correct output