pal2rgb: output usage to stdout when -h is used

see #17
This commit is contained in:
Thomas Bernard 2020-03-07 12:44:33 +01:00
parent 04e7c51f4e
commit 3ac0c950c6
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

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