diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2016-12-31 17:44:44 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2016-12-31 17:44:44 +0000 |
commit | 9e214efb9dda4c352a6f23f9c678eb883dd7e9c2 (patch) | |
tree | c950c92f1367c9be8db1a41a3674ed08fcec5b10 | |
parent | 30936b8c113e3b9aa564ea3ce306de39415a6d7a (diff) | |
download | netpbm-mirror-9e214efb9dda4c352a6f23f9c678eb883dd7e9c2.tar.gz netpbm-mirror-9e214efb9dda4c352a6f23f9c678eb883dd7e9c2.tar.xz netpbm-mirror-9e214efb9dda4c352a6f23f9c678eb883dd7e9c2.zip |
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2866 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r-- | editor/ppmchange.c | 68 |
1 files changed, 42 insertions, 26 deletions
diff --git a/editor/ppmchange.c b/editor/ppmchange.c index 18644b25..cfc34769 100644 --- a/editor/ppmchange.c +++ b/editor/ppmchange.c @@ -29,10 +29,12 @@ struct CmdlineInfo { */ const char *input_filespec; /* Filespecs of input files */ int ncolors; /* Number of valid entries in color0[], color1[] */ - char * oldcolorname[TCOLS]; /* colors user wants replaced */ - char * newcolorname[TCOLS]; /* colors with which he wants them replaced */ + const char * oldcolorname[TCOLS]; + /* colors user wants replaced */ + const char * newcolorname[TCOLS]; + /* colors with which he wants them replaced */ float closeness; - char * remainder_colorname; + const char * remainder_colorname; /* Color user specified for -remainder. Null pointer if he didn't specify -remainder. */ @@ -42,7 +44,7 @@ struct CmdlineInfo { static void -parseCommandLine(int argc, char ** argv, +parseCommandLine(int argc, const char ** const argv, struct CmdlineInfo * const cmdlineP) { /*---------------------------------------------------------------------------- Note that the file spec array we return is stored in the storage that @@ -70,7 +72,7 @@ parseCommandLine(int argc, char ** argv, opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ opt.allowNegNum = FALSE; /* We may have parms that are negative numbers */ - pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); + pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); /* Uses and sets argc, argv, and some of *cmdlineP and others. */ if (!remainderSpec) @@ -82,6 +84,10 @@ parseCommandLine(int argc, char ** argv, if (cmdlineP->closeness < 0.0) pm_error("-closeness value %f is negative", cmdlineP->closeness); + if (cmdlineP->closeness > 100) + pm_error("-closeness value %f is more than 100%%", + cmdlineP->closeness); + if ((argc-1) % 2 == 0) cmdlineP->input_filespec = "-"; else @@ -102,19 +108,19 @@ parseCommandLine(int argc, char ** argv, -static int -colormatch(pixel const comparand, - pixel const comparator, - double const closeness) { +static bool +colorMatches(pixel const comparand, + pixel const comparator, + unsigned int const allowableDiff) { /*---------------------------------------------------------------------------- - Return true iff 'comparand' matches 'comparator' in color within the - fuzz factor 'closeness'. + The colors 'comparand' and 'comparator' are within 'allowableDiff' + color levels of each other, in cartesian distance. -----------------------------------------------------------------------------*/ /* Fast path for usual case */ - if (closeness < EPSILON) + if (allowableDiff < EPSILON) return PPM_EQUAL(comparand, comparator); - return PPM_DISTANCE(comparand, comparator) <= SQR(closeness); + return PPM_DISTANCE(comparand, comparator) <= SQR(allowableDiff); } @@ -128,15 +134,18 @@ changeRow(const pixel * const inrow, const pixel colorto[], bool const remainder_specified, pixel const remainder_color, - double const closeness) { + unsigned int const allowableDiff) { /*---------------------------------------------------------------------------- Replace the colors in a single row. There are 'ncolors' colors to replace. The to-replace colors are in the array colorfrom[], and the replace-with colors are in corresponding elements of colorto[]. Iff 'remainder_specified' is true, replace all colors not mentioned - in colorfrom[] with 'remainder_color'. Use the closeness factor - 'closeness' in determining if something in the input row matches - a color in colorfrom[]. + in colorfrom[] with 'remainder_color'. + + Consider the color in inrow[] to match a color in colorfrom[] if it is + within 'allowableDiff' color levels of it, in cartesian distance (e.g. + color (1,1,1) is sqrt(12) = 3.5 color levels distant from (3,3,3), + so if 'allowableDiff' is 4, they match). The input row is 'inrow'. The output is returned as 'outrow', in storage which must be already allocated. Both are 'cols' columns wide. @@ -153,7 +162,7 @@ changeRow(const pixel * const inrow, haveMatch = FALSE; /* haven't found a match yet */ for (i = 0; i < ncolors && !haveMatch; ++i) { - haveMatch = colormatch(inrow[col], colorfrom[i], closeness); + haveMatch = colorMatches(inrow[col], colorfrom[i], allowableDiff); newcolor = colorto[i]; } if (haveMatch) @@ -168,16 +177,23 @@ changeRow(const pixel * const inrow, int -main(int argc, char *argv[]) { +main(int argc, const char ** const argv) { + struct CmdlineInfo cmdline; FILE * ifP; int format; int rows, cols; pixval maxval; - float closeness; + unsigned int allowableDiff; + /* The amount of difference between two colors we allow and still + consider those colors to be the same, for the purposes of + determining which pixels in the image to change. This is a + cartesian distance between the color triples, on a maxval scale + (which means it can be as high as sqrt(3) * maxval) + */ int row; - pixel* inrow; - pixel* outrow; + pixel * inrow; + pixel * outrow; pixel oldcolor[TCOLS]; /* colors user wants replaced */ pixel newcolor[TCOLS]; /* colors with which he wants them replaced */ @@ -186,7 +202,7 @@ main(int argc, char *argv[]) { specify -remainder. */ - ppm_init(&argc, argv); + pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); @@ -206,8 +222,8 @@ main(int argc, char *argv[]) { cmdline.closeok); } } - closeness = sqrt3 * maxval * cmdline.closeness/100; - + allowableDiff = ROUNDU(sqrt3 * maxval * cmdline.closeness/100); + ppm_writeppminit( stdout, cols, rows, maxval, 0 ); inrow = ppm_allocrow(cols); outrow = ppm_allocrow(cols); @@ -218,7 +234,7 @@ main(int argc, char *argv[]) { changeRow(inrow, outrow, cols, cmdline.ncolors, oldcolor, newcolor, cmdline.remainder_colorname != NULL, - remainder_color, closeness); + remainder_color, allowableDiff); ppm_writeppmrow(stdout, outrow, cols, maxval, 0); } |