const char * UsageLines [] = { "p6width: Changes width of P6 ppm image, keeping height the same.", "Specify output width as only argument. Reads standard input,", "writes to standard output." "", "Does NOT recalculate pixel values. Skips or repeats pixels to fit.", "To resize an image without recalculating pixels, use:", "", "p6width (new-width) | pnmflip -ccw | p6width (new-height) | pnmflip -cw", "", "February 4, 2021. Latest is at gopher://sdf.org/1/users/julianbr", }; const int NumUsageLines = sizeof (UsageLines) / sizeof (UsageLines [0] ); #include <stdlib.h> #include <stdio.h> void ChangeWidth (int outwidth) { int inwidth, height, depth, count, r, g, b, across, down; if (getchar () != 'P' || getchar () != '6' || scanf ("%d", & inwidth) != 1 || scanf ("%d", & height) != 1 || scanf ("%d", & depth) != 1 || getchar () != '\n') { fprintf (stderr, "***p6width: Improper input,"); fprintf (stderr, " must be P6 PPM.\n"); return; } printf ("P6\n"); printf ("%d %d\n", outwidth, height); printf ("255\n"); b = EOF; r = getchar (); for (down = 0; down < height; down++) { count = inwidth/2; for (across = 0; across < inwidth; across++) { if (r == EOF) g = EOF; else g = getchar (); if (g == EOF) b = EOF; else b = getchar (); count += outwidth; while (count >= inwidth) { putchar (r); putchar (g); putchar (b); count -= inwidth; } if (b == EOF) r = EOF; else r = getchar (); } } if (b == EOF) fprintf (stderr, "***p6width: Not enough image data.\n"); if (r != EOF) fprintf (stderr, "***p6width: Too much image data.\n"); } int main (int argc, char * * argv) { int outwidth, i; char c; if (argc < 2) { for (i = 0; i < NumUsageLines; i++) printf ("%s\n", UsageLines [i] ); } if (argc != 2 || sscanf (argv [1], "%d%c", & outwidth, & c) != 1) { fprintf (stderr, "Usage: %s", argv [0] ); fprintf (stderr, " (width)\n"); fprintf (stderr, "Reads P6 PPM image, writes PPM image"); fprintf (stderr, " same height but specified width.\n"); } else ChangeWidth (outwidth); return 0; }