tiffdither: print usage on stdout when -h is used

see #17
This commit is contained in:
Thomas Bernard 2020-03-07 15:47:12 +01:00
parent 08b99340ee
commit 68564877c7
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -39,6 +39,13 @@
#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
#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)
@ -49,7 +56,7 @@ uint32 imagewidth;
uint32 imagelength; uint32 imagelength;
int threshold = 128; int threshold = 128;
static void usage(void); static void usage(int code);
/* /*
* Floyd-Steinberg error propragation with threshold. * Floyd-Steinberg error propragation with threshold.
@ -166,7 +173,7 @@ processG3Options(char* cp)
else if (strneq(cp, "fill", 4)) else if (strneq(cp, "fill", 4))
group3options |= GROUP3OPT_FILLBITS; group3options |= GROUP3OPT_FILLBITS;
else else
usage(); usage(EXIT_FAILURE);
} while ((cp = strchr(cp, ':'))); } while ((cp = strchr(cp, ':')));
} }
} }
@ -213,11 +220,11 @@ main(int argc, char* argv[])
extern char *optarg; extern char *optarg;
#endif #endif
while ((c = getopt(argc, argv, "c:f:r:t:")) != -1) while ((c = getopt(argc, argv, "c:f:r:t: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 'f': /* fill order */ case 'f': /* fill order */
if (streq(optarg, "lsb2msb")) if (streq(optarg, "lsb2msb"))
@ -225,7 +232,7 @@ main(int argc, char* argv[])
else if (streq(optarg, "msb2lsb")) else if (streq(optarg, "msb2lsb"))
fillorder = FILLORDER_MSB2LSB; fillorder = FILLORDER_MSB2LSB;
else else
usage(); usage(EXIT_FAILURE);
break; break;
case 'r': /* rows/strip */ case 'r': /* rows/strip */
rowsperstrip = atoi(optarg); rowsperstrip = atoi(optarg);
@ -237,29 +244,31 @@ main(int argc, char* argv[])
else if (threshold > 255) else if (threshold > 255)
threshold = 255; threshold = 255;
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);
TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
if (samplesperpixel != 1) { if (samplesperpixel != 1) {
fprintf(stderr, "%s: Not a b&w image.\n", argv[0]); fprintf(stderr, "%s: Not a b&w image.\n", argv[0]);
return (-1); return (EXIT_FAILURE);
} }
TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample); TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
if (bitspersample != 8) { if (bitspersample != 8) {
fprintf(stderr, fprintf(stderr,
" %s: Sorry, only handle 8-bit samples.\n", argv[0]); " %s: Sorry, only handle 8-bit samples.\n", argv[0]);
return (-1); return (EXIT_FAILURE);
} }
out = TIFFOpen(argv[optind+1], "w"); out = TIFFOpen(argv[optind+1], "w");
if (out == NULL) if (out == NULL)
return (-1); return (EXIT_FAILURE);
CopyField(TIFFTAG_IMAGEWIDTH, imagewidth); CopyField(TIFFTAG_IMAGEWIDTH, imagewidth);
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength); TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, imagelength-1); TIFFSetField(out, TIFFTAG_IMAGELENGTH, imagelength-1);
@ -293,10 +302,10 @@ main(int argc, char* argv[])
fsdither(in, out); fsdither(in, out);
TIFFClose(in); TIFFClose(in);
TIFFClose(out); TIFFClose(out);
return (0); return (EXIT_SUCCESS);
} }
char* stuff[] = { static const char* stuff[] = {
"usage: tiffdither [options] input.tif output.tif", "usage: tiffdither [options] input.tif output.tif",
"where options are:", "where options are:",
" -r # make each strip have no more than # rows", " -r # make each strip have no more than # rows",
@ -323,16 +332,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: */