summaryrefslogtreecommitdiff
path: root/tiff/tools/tiff2bw.c
diff options
context:
space:
mode:
Diffstat (limited to 'tiff/tools/tiff2bw.c')
-rw-r--r--tiff/tools/tiff2bw.c37
1 files changed, 30 insertions, 7 deletions
diff --git a/tiff/tools/tiff2bw.c b/tiff/tools/tiff2bw.c
index 743f455c2..dad54afa1 100644
--- a/tiff/tools/tiff2bw.c
+++ b/tiff/tools/tiff2bw.c
@@ -1,4 +1,4 @@
-/* $Id: tiff2bw.c,v 1.19 2016-08-15 22:01:31 erouault Exp $ */
+/* $Id: tiff2bw.c,v 1.21 2017-11-01 13:41:58 bfriesen Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@@ -131,6 +131,11 @@ main(int argc, char* argv[])
extern int optind;
extern char *optarg;
#endif
+
+ in = (TIFF *) NULL;
+ out = (TIFF *) NULL;
+ inbuf = (unsigned char *) NULL;
+ outbuf = (unsigned char *) NULL;
while ((c = getopt(argc, argv, "c:r:R:G:B:")) != -1)
switch (c) {
@@ -165,24 +170,24 @@ main(int argc, char* argv[])
fprintf(stderr,
"%s: Bad photometric; can only handle RGB and Palette images.\n",
argv[optind]);
- return (-1);
+ goto tiff2bw_error;
}
TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
if (samplesperpixel != 1 && samplesperpixel != 3) {
fprintf(stderr, "%s: Bad samples/pixel %u.\n",
argv[optind], samplesperpixel);
- return (-1);
+ goto tiff2bw_error;
}
if( photometric == PHOTOMETRIC_RGB && samplesperpixel != 3) {
fprintf(stderr, "%s: Bad samples/pixel %u for PHOTOMETRIC_RGB.\n",
argv[optind], samplesperpixel);
- return (-1);
+ goto tiff2bw_error;
}
TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
if (bitspersample != 8) {
fprintf(stderr,
" %s: Sorry, only handle 8-bit samples.\n", argv[optind]);
- return (-1);
+ goto tiff2bw_error;
}
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &h);
@@ -190,7 +195,9 @@ main(int argc, char* argv[])
out = TIFFOpen(argv[optind+1], "w");
if (out == NULL)
- return (-1);
+ {
+ goto tiff2bw_error;
+ }
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, w);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, h);
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
@@ -264,7 +271,7 @@ main(int argc, char* argv[])
for (s = 0; s < 3; s++)
if (TIFFReadScanline(in,
inbuf+s*rowsize, row, s) < 0)
- return (-1);
+ goto tiff2bw_error;
compresssep(outbuf,
inbuf, inbuf+rowsize, inbuf+2*rowsize, w);
if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
@@ -273,8 +280,24 @@ main(int argc, char* argv[])
break;
}
#undef pack
+ if (inbuf)
+ _TIFFfree(inbuf);
+ if (outbuf)
+ _TIFFfree(outbuf);
+ TIFFClose(in);
TIFFClose(out);
return (0);
+
+ tiff2bw_error:
+ if (inbuf)
+ _TIFFfree(inbuf);
+ if (outbuf)
+ _TIFFfree(outbuf);
+ if (out)
+ TIFFClose(out);
+ if (in)
+ TIFFClose(in);
+ return (-1);
}
static int