Commit 1373f8da authored by Thomas Bernard's avatar Thomas Bernard 💬

Merge branch 'issue-17' into 'master'

normalize tools behaviour regarding -h

Closes #17

See merge request !115
parents 8a583191 aea16700
Pipeline #153609870 passed with stages
in 5 minutes and 33 seconds
......@@ -48,6 +48,13 @@
#include "tiffiop.h"
#include "tiffio.h"
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
float defxres = 204.; /* default x resolution (pixels/inch) */
float defyres = 98.; /* default y resolution (lines/inch) */
const float half = 0.5;
......@@ -330,7 +337,7 @@ main(int argc, char** argv)
int c, dowarnings = 0; /* if 1, enable library warnings */
TIFF* tif;
while ((c = getopt(argc, argv, "l:p:x:y:W:H:wS")) != -1)
while ((c = getopt(argc, argv, "l:p:x:y:W:H:wSh")) != -1)
switch (c) {
case 'H': /* page height */
pageHeight = (float)atof(optarg);
......@@ -350,7 +357,7 @@ main(int argc, char** argv)
if( pages == NULL )
{
fprintf(stderr, "Out of memory\n");
exit(-1);
exit(EXIT_FAILURE);
}
pages[npages++] = pageNumber;
break;
......@@ -366,8 +373,10 @@ main(int argc, char** argv)
case 'l':
maxline = atoi(optarg);
break;
case 'h':
usage(EXIT_SUCCESS);
case '?':
usage(-1);
usage(EXIT_FAILURE);
}
if (npages > 0)
qsort(pages, npages, sizeof(uint16), pcompar);
......@@ -391,7 +400,7 @@ main(int argc, char** argv)
fd = tmpfile();
if (fd == NULL) {
fprintf(stderr, "Could not obtain temporary file.\n");
exit(-2);
exit(EXIT_FAILURE);
}
#if defined(HAVE_SETMODE) && defined(O_BINARY)
setmode(fileno(stdin), O_BINARY);
......@@ -401,7 +410,7 @@ main(int argc, char** argv)
fclose(fd);
fprintf(stderr,
"Could not copy stdin to temporary file.\n");
exit(-2);
exit(EXIT_FAILURE);
}
}
_TIFF_lseek_f(fileno(fd), 0, SEEK_SET);
......@@ -421,10 +430,10 @@ main(int argc, char** argv)
printf("%%%%Pages: %u\n", totalPages);
printf("%%%%EOF\n");
return (0);
return (EXIT_SUCCESS);
}
char* stuff[] = {
const char* stuff[] = {
"usage: fax2ps [options] [input.tif ...]",
"where options are:",
" -w suppress warning messages",
......@@ -441,13 +450,12 @@ NULL
static 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]);
fprintf(out, "%s\n", stuff[i]);
exit(code);
}
......
......@@ -68,7 +68,7 @@ uint16 badfaxrun;
uint32 badfaxlines;
int copyFaxFile(TIFF* tifin, TIFF* tifout);
static void usage(void);
static void usage(int code);
/*
Struct to carry client data. Note that it does not appear that the client
......@@ -110,7 +110,7 @@ main(int argc, char* argv[])
extern char* optarg;
#endif
while ((c = getopt(argc, argv, "R:X:o:r:1234ABLMPUW5678abcflmprsuvwz?")) != -1)
while ((c = getopt(argc, argv, "R:X:o:r:1234ABLMPUW5678abcflmprsuvwzh")) != -1)
switch (c) {
/* input-related options */
case '3': /* input is g3-encoded */
......@@ -216,13 +216,15 @@ main(int argc, char* argv[])
case 'v': /* -v for info */
verbose++;
break;
case 'h':
usage(EXIT_SUCCESS);
case '?':
usage();
usage(EXIT_FAILURE);
/*NOTREACHED*/
}
npages = argc - optind;
if (npages < 1)
usage();
usage(EXIT_FAILURE);
rowbuf = _TIFFmalloc(TIFFhowmany8(xsize));
refbuf = _TIFFmalloc(TIFFhowmany8(xsize));
......@@ -426,7 +428,7 @@ copyFaxFile(TIFF* tifin, TIFF* tifout)
return (row);
}
char* stuff[] = {
const char* stuff[] = {
"usage: fax2tiff [options] input.raw...",
"where options are:",
" -3 input data is G3-encoded [default]",
......@@ -463,16 +465,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(EXIT_FAILURE);
fprintf(out, "%s\n", stuff[i]);
exit(code);
}
/* vim: set ts=8 sts=8 sw=8 noet: */
......
......@@ -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: */
......
......@@ -51,6 +51,13 @@
extern int getopt(int argc, char * const argv[], const char *optstring);
#endif
#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)
......@@ -60,7 +67,7 @@ static int quality = 75; /* JPEG quality */
static int jpegcolormode = JPEGCOLORMODE_RGB;
static uint32 g3opts;
static void usage(void);
static void usage(int code);
static int processCompressOptions(char*);
static void
......@@ -162,7 +169,7 @@ static void
BadPPM(char* file)
{
fprintf(stderr, "%s: Not a PPM file.\n", file);
exit(-2);
exit(EXIT_FAILURE);
}
......@@ -202,13 +209,13 @@ main(int argc, char* argv[])
if (argc < 2) {
fprintf(stderr, "%s: Too few arguments\n", argv[0]);
usage();
usage(EXIT_FAILURE);
}
while ((c = getopt(argc, argv, "c:r:R:")) != -1)
while ((c = getopt(argc, argv, "c:r:R:h")) != -1)
switch (c) {
case 'c': /* compression scheme */
if (!processCompressOptions(optarg))
usage();
usage(EXIT_FAILURE);
break;
case 'r': /* rows/strip */
rowsperstrip = atoi(optarg);
......@@ -216,14 +223,16 @@ main(int argc, char* argv[])
case 'R': /* resolution */
resolution = atof(optarg);
break;
case 'h':
usage(EXIT_SUCCESS);
case '?':
usage();
usage(EXIT_FAILURE);
/*NOTREACHED*/
}
if (optind + 2 < argc) {
fprintf(stderr, "%s: Too many arguments\n", argv[0]);
usage();
usage(EXIT_FAILURE);
}
/*
......@@ -235,7 +244,7 @@ main(int argc, char* argv[])
in = fopen(infile, "rb");
if (in == NULL) {
fprintf(stderr, "%s: Can not open.\n", infile);
return (-1);
return (EXIT_FAILURE);
}
} else {
infile = "<stdin>";
......@@ -306,7 +315,7 @@ main(int argc, char* argv[])
if (0 != (prec & (prec + 1))) {
fprintf(stderr, "%s: unsupported maxval %u.\n",
infile, prec);
exit(-2);
exit(EXIT_FAILURE);
}
bpp = 0;
if ((prec + 1) & 0xAAAAAAAA) bpp |= 1;
......@@ -339,7 +348,7 @@ main(int argc, char* argv[])
}
out = TIFFOpen(argv[optind], "w");
if (out == NULL)
return (-4);
return (EXIT_FAILURE);
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) w);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h);
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
......@@ -379,13 +388,13 @@ main(int argc, char* argv[])
if (linebytes == 0) {
fprintf(stderr, "%s: scanline size overflow\n", infile);
(void) TIFFClose(out);
exit(-2);
exit(EXIT_FAILURE);
}
scanline_size = TIFFScanlineSize(out);
if (scanline_size == 0) {
/* overflow - TIFFScanlineSize already printed a message */
(void) TIFFClose(out);
exit(-2);
exit(EXIT_FAILURE);
}
if (scanline_size < linebytes)
buf = (unsigned char *)_TIFFmalloc(linebytes);
......@@ -394,7 +403,7 @@ main(int argc, char* argv[])
if (buf == NULL) {
fprintf(stderr, "%s: Not enough memory\n", infile);
(void) TIFFClose(out);
exit(-2);
exit(EXIT_FAILURE);
}
if (resolution > 0) {
TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
......@@ -416,7 +425,7 @@ main(int argc, char* argv[])
(void) TIFFClose(out);
if (buf)
_TIFFfree(buf);
return (0);
return (EXIT_SUCCESS);
}
static void
......@@ -433,7 +442,7 @@ processG3Options(char* cp)
else if (strneq(cp, "fill", 4))
g3opts |= GROUP3OPT_FILLBITS;
else
usage();
usage(EXIT_FAILURE);
} while( (cp = strchr(cp, ':')) );
}
}
......@@ -456,7 +465,7 @@ processCompressOptions(char* opt)
else if (cp[1] == 'r' )
jpegcolormode = JPEGCOLORMODE_RAW;
else
usage();
usage(EXIT_FAILURE);
cp = strchr(cp+1,':');
}
......@@ -480,7 +489,7 @@ processCompressOptions(char* opt)
return (1);
}
char* stuff[] = {
const char* stuff[] = {
"usage: ppm2tiff [options] input.ppm output.tif",
"where options are:",
" -r # make each strip have no more than # rows",
......@@ -504,16 +513,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: */
......
......@@ -59,6 +59,13 @@
#include "tiffiop.h"
#include "tiffio.h"
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#ifndef HAVE_GETOPT
extern int getopt(int argc, char * const argv[], const char *optstring);
#endif
......@@ -81,7 +88,7 @@ static void swapBytesInScanline(void *, uint32, TIFFDataType);
static int guessSize(int, TIFFDataType, _TIFF_off_t, uint32, int,
uint32 *, uint32 *);
static double correlation(void *, void *, uint32, TIFFDataType);
static void usage(void);
static void usage(int);
static int processCompressOptions(char*);
int
......@@ -114,7 +121,7 @@ main(int argc, char* argv[])
switch (c) {
case 'c': /* compression scheme */
if (!processCompressOptions(optarg))
usage();
usage(EXIT_FAILURE);
break;
case 'r': /* rows/strip */
rowsperstrip = atoi(optarg);
......@@ -193,24 +200,24 @@ main(int argc, char* argv[])
outfilename = optarg;
break;
case 'h':
usage();
usage(EXIT_SUCCESS);
default:
break;
}
}
if (argc - optind < 2)
usage();
usage(EXIT_FAILURE);
fd = open(argv[optind], O_RDONLY|O_BINARY, 0);
if (fd < 0) {
fprintf(stderr, "%s: %s: Cannot open input file.\n",
argv[0], argv[optind]);
return (-1);
return (EXIT_FAILURE);
}
if (guessSize(fd, dtype, hdr_size, nbands, swab, &width, &length) < 0)
return 1;
return EXIT_FAILURE;
if (outfilename == NULL)
outfilename = argv[optind+1];
......@@ -218,7 +225,7 @@ main(int argc, char* argv[])
if (out == NULL) {
fprintf(stderr, "%s: %s: Cannot open file for output.\n",
argv[0], outfilename);
return (-1);
return (EXIT_FAILURE);
}
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);
......@@ -336,7 +343,7 @@ main(int argc, char* argv[])
if (buf1)
_TIFFfree(buf1);
TIFFClose(out);
return (0);
return (EXIT_SUCCESS);
}
static void
......@@ -598,7 +605,7 @@ processCompressOptions(char* opt)
else if (cp[1] == 'r' )
jpegcolormode = JPEGCOLORMODE_RAW;
else
usage();
usage(EXIT_FAILURE);
cp = strchr(cp+1,':');
}
......@@ -617,7 +624,7 @@ processCompressOptions(char* opt)
return (1);
}
static char* stuff[] = {
static const char* stuff[] = {
"raw2tiff --- tool for converting raw byte sequences in TIFF images",
"usage: raw2tiff [options] input.raw output.tif",
"where options are:",
......@@ -678,16 +685,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: */
......
......@@ -39,6 +39,13 @@
#include "tiffiop.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 CopyField(tag, v) \
if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
......@@ -89,17 +96,17 @@ main(int argc, char* argv[])
else if (streq(optarg, "zip"))
compression = COMPRESSION_ADOBE_DEFLATE;
else
usage(-1);
usage(EXIT_FAILURE);
break;
case 'h':
horizSubSampling = atoi(optarg);
if( horizSubSampling != 1 && horizSubSampling != 2 && horizSubSampling != 4 )
usage(-1);
usage(EXIT_FAILURE);
break;
case 'v':
vertSubSampling = atoi(optarg);
if( vertSubSampling != 1 && vertSubSampling != 2 && vertSubSampling != 4 )
usage(-1);
usage(EXIT_FAILURE);
break;
case 'r':
rowsperstrip = atoi(optarg);
......@@ -113,14 +120,14 @@ main(int argc, char* argv[])
refBlackWhite[5] = 240.;
break;
case '?':
usage(0);
usage(EXIT_FAILURE);
/*NOTREACHED*/
}
if (argc - optind < 2)
usage(-1);
usage(EXIT_FAILURE);
out = TIFFOpen(argv[argc-1], "w");
if (out == NULL)
return (-2);
return (EXIT_FAILURE);
setupLumaTables();
for (; optind < argc-1; optind++) {
in = TIFFOpen(argv[optind], "r");
......@@ -137,7 +144,7 @@ main(int argc, char* argv[])
}
}
(void) TIFFClose(out);
return (0);
return (EXIT_SUCCESS);
}
float *lumaRed;
......@@ -357,7 +364,7 @@ tiffcvt(TIFF* in, TIFF* out)
return result;
}
char* stuff[] = {
const char* stuff[] = {
"usage: rgb2ycbcr [-c comp] [-r rows] [-h N] [-v N] input... output\n",
"where comp is one of the following compression algorithms:\n",
" jpeg\t\tJPEG encoding\n",
......@@ -375,14 +382,12 @@ char* stuff[] = {
static 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]);
fprintf(out, "%s\n", stuff[i]);
exit(code);