tiff2rgba: output usage to stdout when using -h

also uses std C EXIT_FAILURE / EXIT_SUCCESS
see #17
This commit is contained in:
Thomas Bernard 2020-03-07 13:21:56 +01:00
parent 05993b6408
commit 02875964eb
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -39,6 +39,13 @@
#include "tiffiop.h" #include "tiffiop.h"
#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 CopyField(tag, v) \ #define CopyField(tag, v) \
if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v) if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
@ -68,7 +75,7 @@ main(int argc, char* argv[])
extern char *optarg; extern char *optarg;
#endif #endif
while ((c = getopt(argc, argv, "c:r:t:bn8")) != -1) while ((c = getopt(argc, argv, "c:r:t:bn8h")) != -1)
switch (c) { switch (c) {
case 'b': case 'b':
process_by_block = 1; process_by_block = 1;
@ -86,7 +93,7 @@ main(int argc, char* argv[])
else if (streq(optarg, "zip")) else if (streq(optarg, "zip"))
compression = COMPRESSION_DEFLATE; compression = COMPRESSION_DEFLATE;
else else
usage(-1); usage(EXIT_FAILURE);
break; break;
case 'r': case 'r':
@ -105,17 +112,20 @@ main(int argc, char* argv[])
bigtiff_output = 1; bigtiff_output = 1;
break; break;
case 'h':
usage(EXIT_SUCCESS);
/*NOTREACHED*/
case '?': case '?':
usage(0); usage(EXIT_FAILURE);
/*NOTREACHED*/ /*NOTREACHED*/
} }
if (argc - optind < 2) if (argc - optind < 2)
usage(-1); usage(EXIT_FAILURE);
out = TIFFOpen(argv[argc-1], bigtiff_output?"w8":"w"); out = TIFFOpen(argv[argc-1], bigtiff_output?"w8":"w");
if (out == NULL) if (out == NULL)
return (-2); return (EXIT_FAILURE);
for (; optind < argc-1; optind++) { for (; optind < argc-1; optind++) {
in = TIFFOpen(argv[optind], "r"); in = TIFFOpen(argv[optind], "r");
@ -132,7 +142,7 @@ main(int argc, char* argv[])
} }
} }
(void) TIFFClose(out); (void) TIFFClose(out);
return (0); return (EXIT_SUCCESS);
} }
static int static int
@ -166,7 +176,7 @@ cvt_by_tile( TIFF *in, TIFF *out )
if (tile_width != (rastersize / tile_height) / sizeof( uint32)) if (tile_width != (rastersize / tile_height) / sizeof( uint32))
{ {
TIFFError(TIFFFileName(in), "Integer overflow when calculating raster buffer"); TIFFError(TIFFFileName(in), "Integer overflow when calculating raster buffer");
exit(-1); exit(EXIT_FAILURE);
} }
raster = (uint32*)_TIFFmalloc(rastersize); raster = (uint32*)_TIFFmalloc(rastersize);
if (raster == 0) { if (raster == 0) {
@ -182,7 +192,7 @@ cvt_by_tile( TIFF *in, TIFF *out )
if (tile_width != wrk_linesize / sizeof (uint32)) if (tile_width != wrk_linesize / sizeof (uint32))
{ {
TIFFError(TIFFFileName(in), "Integer overflow when calculating wrk_line buffer"); TIFFError(TIFFFileName(in), "Integer overflow when calculating wrk_line buffer");
exit(-1); exit(EXIT_FAILURE);
} }
wrk_line = (uint32*)_TIFFmalloc(wrk_linesize); wrk_line = (uint32*)_TIFFmalloc(wrk_linesize);
if (!wrk_line) { if (!wrk_line) {
@ -279,7 +289,7 @@ cvt_by_strip( TIFF *in, TIFF *out )
if (width != (rastersize / rowsperstrip) / sizeof( uint32)) if (width != (rastersize / rowsperstrip) / sizeof( uint32))
{ {
TIFFError(TIFFFileName(in), "Integer overflow when calculating raster buffer"); TIFFError(TIFFFileName(in), "Integer overflow when calculating raster buffer");
exit(-1); exit(EXIT_FAILURE);
} }
raster = (uint32*)_TIFFmalloc(rastersize); raster = (uint32*)_TIFFmalloc(rastersize);
if (raster == 0) { if (raster == 0) {
@ -295,7 +305,7 @@ cvt_by_strip( TIFF *in, TIFF *out )
if (width != wrk_linesize / sizeof (uint32)) if (width != wrk_linesize / sizeof (uint32))
{ {
TIFFError(TIFFFileName(in), "Integer overflow when calculating wrk_line buffer"); TIFFError(TIFFFileName(in), "Integer overflow when calculating wrk_line buffer");
exit(-1); exit(EXIT_FAILURE);
} }
wrk_line = (uint32*)_TIFFmalloc(wrk_linesize); wrk_line = (uint32*)_TIFFmalloc(wrk_linesize);
if (!wrk_line) { if (!wrk_line) {
@ -528,7 +538,7 @@ tiffcvt(TIFF* in, TIFF* out)
return( cvt_whole_image( in, out ) ); return( cvt_whole_image( in, out ) );
} }
static char* stuff[] = { const static char* stuff[] = {
"usage: tiff2rgba [-c comp] [-r rows] [-b] [-n] [-8] input... output", "usage: tiff2rgba [-c comp] [-r rows] [-b] [-n] [-8] input... output",
"where comp is one of the following compression algorithms:", "where comp is one of the following compression algorithms:",
" jpeg\t\tJPEG encoding", " jpeg\t\tJPEG encoding",
@ -547,13 +557,12 @@ static char* stuff[] = {
static void static void
usage(int code) 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(code); exit(code);
} }