diff options
Diffstat (limited to 'converter/other/pbmtopgm.c')
-rw-r--r-- | converter/other/pbmtopgm.c | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/converter/other/pbmtopgm.c b/converter/other/pbmtopgm.c index c35e1cbe..817fb5b3 100644 --- a/converter/other/pbmtopgm.c +++ b/converter/other/pbmtopgm.c @@ -6,6 +6,7 @@ #include <limits.h> #include "pm_c_util.h" +#include "nstring.h" #include "pgm.h" int @@ -17,26 +18,23 @@ main(int argc, char *argv[]) { int rows, cols; FILE *ifd; int row; - int width, height; + unsigned int width, height; const char * const usage = "<w> <h> [pbmfile]"; - + const char * error; /* error message of pm_string_to_uint */ pgm_init( &argc, argv ); if (argc > 4 || argc < 3) pm_usage(usage); - width = atoi(argv[1]); - height = atoi(argv[2]); + pm_string_to_uint(argv[1], &width, &error); + if (error) + pm_error("Invalid width argument: %s", error); + pm_string_to_uint(argv[2], &height, &error); + if (error) + pm_error("Invalid height argument: %s", error); if (width < 1 || height < 1) pm_error("width and height must be > 0"); - if (width > INT_MAX / height) - /* prevent overflow of "value" below */ - pm_error("sample area (%u columns %u rows) too large", - width, height); - - left=width/2; right=width-left; - up=height/2; down=height-up; if (argc == 4) ifd = pm_openr(argv[3]); @@ -44,13 +42,22 @@ main(int argc, char *argv[]) { ifd = stdin ; inbits = pbm_readpbm(ifd, &cols, &rows) ; - + if (width > cols) pm_error("You specified a sample width (%u columns) which is greater " "than the image width (%u columns)", height, rows); if (height > rows) pm_error("You specified a sample height (%u rows) which is greater " "than the image height (%u rows)", height, rows); + if (width > INT_MAX / height) + /* prevent overflow of "value" below */ + pm_error("sample area (%u columns %u rows) too large", + width, height); + + left = width / 2; right = width - left; + up = height / 2; down = height - up; + + outrow = pgm_allocrow(cols) ; maxval = MIN(PGM_OVERALLMAXVAL, width*height); @@ -65,7 +72,7 @@ main(int argc, char *argv[]) { int const l = (col > left) ? (col-left) : 0; int const r = (col+right < cols) ? (col+right) : cols; int const onh = width - (l-col+left) - (col+right-r); - int value; + int value; /* See above */ int x; value = 0; /* initial value */ @@ -73,10 +80,10 @@ main(int argc, char *argv[]) { for (x = l; x < r; ++x) { int y; for (y = t; y < b; ++y) - if (inbits[y][x] == PBM_WHITE) + if (inbits[y][x] == PBM_WHITE) ++value; } - outrow[col] = maxval*value/(onh*onv); + outrow[col] = (gray) ((double) maxval*value/(onh*onv)); } pgm_writepgmrow(stdout, outrow, cols, maxval, 0) ; } |