So I have a .ppm file and the goal is to read each pixel to corresponding r[] g[] and b[] elements. The code reads the first line (idk correct or not), but it does not go any further. I'm unsure if I need these getc(fp);
in order to skip spaces. Reading each line and parsing it to int is not an option. Thanks for any help.
int main(int argc, char** argv)
{
int height;
int width;
int max;
FILE *fp;
fp = fopen("vit_small.ppm", "r");
fscanf(fp, "%*[^\n]\n", NULL);
fscanf(fp, "%d %d", &width, &height);
printf("Width is %d height is %d \n", width, height);
fscanf(fp, "%d", &max);
printf("Maximum value %d \n", max);
int r [height][width];
int g [height][width];
int b [height][width];
int hist [5];
int w = 0;
int h = 0;
char buffer [1000];
for (;w<height;w++)
{
printf("Row number %d \n", w);
for (;h<width;h++)
{
fread(&r[w][h], 1, 1, fp);
printf("%d ", r[w][h]);
getc(fp);
fread(&g[w][h], 1, 1, fp);
printf("%d ", g[w][h]);
getc(fp);
fread(&b[w][h], 1, 1, fp);
printf("%d ", b[w][h]);
getc(fp);
}
getc(fp);
printf("\n");
}
int i = 0;
int j = 0;
for (;i<height; i++)
{
for (;j<width; j++)
{
printf("%d %d %d ", r[i][j], g[i][j], b[i][j]);
}
printf("\n");
}
fclose(fp);
FILE * res;
res = fopen ("Image_output.ppm", "w");
fprintf (res, "P6\n");
fprintf(res, "%d\n", width);
fprintf(res, "%d\n", height);
fprintf(res, "%d\n", max);
i = 0;
j = 0;
for(; i < height; i++)
{
for(; j < width; j++)
{
fprintf(res, "%d %d %d", r[i][j], g[i][j], b[i][j]);
}
fprintf(res,"\n");
}
return (EXIT_SUCCESS);
}