const char * UsageLines [] = { "Usage: p4show", "Reads P4 PBM image from standard input.", "Writes text representation to standard output.", "For an input pixel of '1', writes '*'.", "For an input pixel of '0', writes '-', '+', or '|' along outside", "edges, and (space) otherwise.", "May 20, 2020. Newest is at gopher -p users/julianbr sdf.org", "(Use 'p4paint' with a key of \"*\" for the opposite of 'p4show'.)", }; const int NumUsageLines = sizeof (UsageLines)/sizeof (UsageLines [0] ); #include <stdio.h> void Show (int width, int height) { int across, down, InputValue, InputWeight; for (down = 0; down < height; down++) { InputWeight = 128; InputValue = 0; /* Just to make it != EOF */ for (across = 0; across < width; across++) { if (InputWeight == 128 && InputValue != EOF) InputValue = getchar (); if (InputValue != EOF && InputValue >= InputWeight) putchar ('*'); else if (down > 0 && down + 1 < height && (across == 0 || across + 1 == width) ) putchar ('|'); else if (across > 0 && across + 1 < width && (down == 0 || down + 1 == height) ) putchar ('-'); else if ((down == 0 || down + 1 == height) && (across == 0 || across + 1 == width) ) putchar ('+'); else putchar (' '); if (InputValue != EOF && InputValue >= InputWeight) InputValue -= InputWeight; InputWeight /= 2; if (InputWeight == 0) InputWeight = 128; } putchar ('\n'); } if (InputValue == EOF) { fprintf (stderr, "***p4show: Premature end of"); fprintf (stderr, " input image data.\n"); } else if (getchar () != EOF) { fprintf (stderr, "***p4show: Extra image data"); fprintf (stderr, " in input PBM.\n"); } } int main (int argc, char * argv [] ) { int i, width, height; if (argc > 1) { fprintf (stderr, "***p4show: Unrecognized"); fprintf (stderr, " \"%s\".\n", argv [1] ); for (i = 0; i < NumUsageLines; i++) printf ("%s\n", UsageLines [i] ); } else { if (getchar () != 'P' || getchar () != '4' || scanf ("%d", & width) != 1 || width < 1 || scanf ("%d", & height) != 1 || height < 1 || getchar () == EOF) { fprintf (stderr, "***p4show: Wrong input type,"); fprintf (stderr, " must be P4 PBM.\n"); } else Show (width, height); } return 0; }