From 3ac0c950c6ef26fbb13e72a9e6b52c51f822f763 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sat, 7 Mar 2020 12:44:33 +0100 Subject: pal2rgb: output usage to stdout when -h is used see #17 --- tools/pal2rgb.c | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/tools/pal2rgb.c b/tools/pal2rgb.c index 9492f1cf..bf95cb37 100644 --- a/tools/pal2rgb.c +++ b/tools/pal2rgb.c @@ -39,10 +39,17 @@ #include "tiffio.h" +#ifndef EXIT_SUCCESS +#define EXIT_SUCCESS 0 +#endif +#ifndef EXIT_FAILURE +#define EXIT_FAILURE 1 +#endif + #define streq(a,b) (strcmp(a,b) == 0) #define strneq(a,b,n) (strncmp(a,b,n) == 0) -static void usage(void); +static void usage(int code); static void cpTags(TIFF* in, TIFF* out); static int @@ -85,14 +92,14 @@ main(int argc, char* argv[]) extern char* optarg; #endif - while ((c = getopt(argc, argv, "C:c:p:r:")) != -1) + while ((c = getopt(argc, argv, "C:c:p:r:h")) != -1) switch (c) { case 'C': /* force colormap interpretation */ cmap = atoi(optarg); break; case 'c': /* compression scheme */ if (!processCompressOptions(optarg)) - usage(); + usage(EXIT_FAILURE); break; case 'p': /* planar configuration */ if (streq(optarg, "separate")) @@ -100,33 +107,35 @@ main(int argc, char* argv[]) else if (streq(optarg, "contig")) config = PLANARCONFIG_CONTIG; else - usage(); + usage(EXIT_FAILURE); break; case 'r': /* rows/strip */ rowsperstrip = atoi(optarg); break; + case 'h': + usage(EXIT_SUCCESS); case '?': - usage(); + usage(EXIT_FAILURE); /*NOTREACHED*/ } if (argc - optind != 2) - usage(); + usage(EXIT_FAILURE); in = TIFFOpen(argv[optind], "r"); if (in == NULL) - return (-1); + return (EXIT_FAILURE); if (!TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &shortv) || shortv != PHOTOMETRIC_PALETTE) { fprintf(stderr, "%s: Expecting a palette image.\n", argv[optind]); (void) TIFFClose(in); - return (-1); + return (EXIT_FAILURE); } if (!TIFFGetField(in, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) { fprintf(stderr, "%s: No colormap (not a valid palette image).\n", argv[optind]); (void) TIFFClose(in); - return (-1); + return (EXIT_FAILURE); } bitspersample = 0; TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample); @@ -134,12 +143,12 @@ main(int argc, char* argv[]) fprintf(stderr, "%s: Sorry, can only handle 8-bit images.\n", argv[optind]); (void) TIFFClose(in); - return (-1); + return (EXIT_FAILURE); } out = TIFFOpen(argv[optind+1], "w"); if (out == NULL) { (void) TIFFClose(in); - return (-2); + return (EXIT_FAILURE); } cpTags(in, out); TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &imagewidth); @@ -198,7 +207,7 @@ main(int argc, char* argv[]) * buffer overflow. Go ahead and fail now to prevent that. */ fprintf(stderr, "Could not determine correct image size for output. Exiting.\n"); - return -1; + return EXIT_FAILURE; } ibuf = (unsigned char*)_TIFFmalloc(tss_in); obuf = (unsigned char*)_TIFFmalloc(tss_out); @@ -242,7 +251,7 @@ main(int argc, char* argv[]) done: (void) TIFFClose(in); (void) TIFFClose(out); - return (0); + return (EXIT_SUCCESS); } static int @@ -263,7 +272,7 @@ processCompressOptions(char* opt) else if (cp[1] == 'r' ) jpegcolormode = JPEGCOLORMODE_RAW; else - usage(); + usage(EXIT_FAILURE); cp = strchr(cp+1,':'); } @@ -427,7 +436,7 @@ cpTags(TIFF* in, TIFF* out) } #undef NTAGS -char* stuff[] = { +const char* stuff[] = { "usage: pal2rgb [options] input.tif output.tif", "where options are:", " -p contig pack samples contiguously (e.g. RGBRGB...)", @@ -448,16 +457,15 @@ NULL }; static void -usage(void) +usage(int code) { - char buf[BUFSIZ]; int i; + FILE * out = (code == EXIT_SUCCESS) ? stdout : stderr; - setbuf(stderr, buf); - fprintf(stderr, "%s\n\n", TIFFGetVersion()); + fprintf(out, "%s\n\n", TIFFGetVersion()); for (i = 0; stuff[i] != NULL; i++) - fprintf(stderr, "%s\n", stuff[i]); - exit(-1); + fprintf(out, "%s\n", stuff[i]); + exit(code); } /* vim: set ts=8 sts=8 sw=8 noet: */ -- cgit v1.2.1