tiff2bw: output usage on stdout when using -h

also uses EXIT_SUCCESS / EXIT_FAILURE
see #17
This commit is contained in:
Thomas Bernard 2020-03-07 13:10:07 +01:00
parent da2995eecf
commit ff18e43223
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -40,13 +40,20 @@
#include "tiffio.h" #include "tiffio.h"
#include "tiffiop.h" #include "tiffiop.h"
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
/* x% weighting -> fraction of full color */ /* x% weighting -> fraction of full color */
#define PCT(x) (((x)*256+50)/100) #define PCT(x) (((x)*256+50)/100)
int RED = PCT(30); /* 30% */ int RED = PCT(30); /* 30% */
int GREEN = PCT(59); /* 59% */ int GREEN = PCT(59); /* 59% */
int BLUE = PCT(11); /* 11% */ int BLUE = PCT(11); /* 11% */
static void usage(void); static void usage(int code);
static int processCompressOptions(char*); static int processCompressOptions(char*);
static void static void
@ -133,11 +140,11 @@ main(int argc, char* argv[])
inbuf = (unsigned char *) NULL; inbuf = (unsigned char *) NULL;
outbuf = (unsigned char *) NULL; outbuf = (unsigned char *) NULL;
while ((c = getopt(argc, argv, "c:r:R:G:B:")) != -1) while ((c = getopt(argc, argv, "c:r:R:G:B:h")) != -1)
switch (c) { switch (c) {
case 'c': /* compression scheme */ case 'c': /* compression scheme */
if (!processCompressOptions(optarg)) if (!processCompressOptions(optarg))
usage(); usage(EXIT_FAILURE);
break; break;
case 'r': /* rows/strip */ case 'r': /* rows/strip */
rowsperstrip = atoi(optarg); rowsperstrip = atoi(optarg);
@ -151,15 +158,17 @@ main(int argc, char* argv[])
case 'B': case 'B':
BLUE = PCT(atoi(optarg)); BLUE = PCT(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);
photometric = 0; photometric = 0;
TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric); TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric);
if (photometric != PHOTOMETRIC_RGB && photometric != PHOTOMETRIC_PALETTE ) { if (photometric != PHOTOMETRIC_RGB && photometric != PHOTOMETRIC_PALETTE ) {
@ -306,7 +315,7 @@ main(int argc, char* argv[])
_TIFFfree(outbuf); _TIFFfree(outbuf);
TIFFClose(in); TIFFClose(in);
TIFFClose(out); TIFFClose(out);
return (0); return (EXIT_SUCCESS);
tiff2bw_error: tiff2bw_error:
if (inbuf) if (inbuf)
@ -317,7 +326,7 @@ main(int argc, char* argv[])
TIFFClose(out); TIFFClose(out);
if (in) if (in)
TIFFClose(in); TIFFClose(in);
return (-1); return (EXIT_FAILURE);
} }
static int static int
@ -338,7 +347,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,':');
} }
@ -492,7 +501,7 @@ cpTags(TIFF* in, TIFF* out)
} }
#undef NTAGS #undef NTAGS
char* stuff[] = { const char* stuff[] = {
"usage: tiff2bw [options] input.tif output.tif", "usage: tiff2bw [options] input.tif output.tif",
"where options are:", "where options are:",
" -R % use #% from red channel", " -R % use #% from red channel",
@ -515,16 +524,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: */