I need to read a .PBM image in binary (p4) for my school project.
I tried something like this:
for (int i = 0; i < img->height; i++)
{
for (int x = 0; x < img->width; x++)
{
c = fgetc(fp);
ungetc(c, fp);
lum_val = fgetc(fp);
/* Decode the image contents byte-by-byte. */
for (k = 0; k < 8; k++) {
printf("%d ", (lum_val >> (7 - k)) & 0x1);
img->pixels[i][x].r = (lum_val >> (7 - k)) & 0x1;
img->pixels[i][x].g = (lum_val >> (7 - k)) & 0x1;
img->pixels[i][x].b = (lum_val >> (7 - k)) & 0x1;
}
}
}
based od this source code: https://github.com/nkkav/libpnmio/blob/master/src/pnmio.c but it doesn't work :( It only gives random 0 and 1 but not the way it is supposed to be.