c - PPM scanning and printing -
i want scan in documents file of type .ppm image files follows following data structure
p3 4 4 255 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 0 0 128 128 128 255 255 0 255 255 0 0 255 0 0 0 255 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0
every 3 integers mark column after first 3 lines, marks pattern of 4x4 table indicated on second line.
my first step read in file height , width can vary , reprint in exact format using scanf , printf.
my attempt @ follows:
scanf(" %d%d %d", &width, &height, &depth); printf("p3\n%d %d\n %d\n", width, height, depth); while(scanf("%c", &input) >= 1) { (int = 0; < width; i++) { printf("%c %c %c ", input, input, input); (int j = 0; j < height; j++) { printf("\n"); } } }
any idea missing?
there number of errors in code:
- you scan
input
once. therefore of pixels have same value. want callscanf
in inner loop. - the format specifier
%c
reads in 1 character , not number. should use%d
though range of pixel values fits within byte. - as mentioned, inner loop prints nothing linefeeds. belongs outer loop.
- the
print
should called in inner loop different arguments unless want write black , white images only:printf("%d %d %d ", r, g, b);
. again need use%d
instead of%c
. - as mentioned in comment, need skip "p3" in header. modify first line
scanf("%*s %d %d %d", &width, &height, &depth);
. '%*s' reads string without assigning variable.
Comments
Post a Comment