From 0ca320353527365f66514bdc9b3eae36adffcca6 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sun, 29 Mar 2015 01:02:50 +0000 Subject: cleanup git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2424 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- editor/specialty/pgmmorphconv.c | 536 +++++++++++++++++++++++++--------------- 1 file changed, 336 insertions(+), 200 deletions(-) diff --git a/editor/specialty/pgmmorphconv.c b/editor/specialty/pgmmorphconv.c index 5079e19f..e2572081 100644 --- a/editor/specialty/pgmmorphconv.c +++ b/editor/specialty/pgmmorphconv.c @@ -17,263 +17,399 @@ */ #include "pm_c_util.h" +#include "shhopt.h" +#include "mallocvar.h" #include "pgm.h" -/************************************************************ - * Dilate - ************************************************************/ -static int -dilate( bit** template, int trowso2, int tcolso2, - gray** in_image, gray** out_image, - int rows, int cols ){ +enum Operation { ERODE, DILATE, OPEN, CLOSE, GRADIENT }; - int c, r, tc, tr; - int templatecount; - gray source; - for( c=0; c0)?0:-tr) ; r< ((tr>0)?(rows-tr):rows) ; ++r ){ - for( c= ((tc>0)?0:-tc) ; c< ((tc>0)?(cols-tc):cols) ; ++c ){ - source = in_image[r+tr][c+tc]; - out_image[r][c] = MAX(source, out_image[r][c]); - } /* for c */ - } /* for r */ - } /* for tr */ - } /* for tc */ - return templatecount; +static void +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 + was passed to us as the argv array. +-----------------------------------------------------------------------------*/ + optEntry * option_def; + /* Instructions to OptParseOptions3 on how to parse our options. + */ + optStruct3 opt; + unsigned int option_def_index; + unsigned int erode, dilate, open, close, gradient; + + MALLOCARRAY_NOFAIL(option_def, 100); + + option_def_index = 0; /* incremented by OPTENT3 */ + OPTENT3(0, "erode", OPT_FLAG, NULL, &erode, 0); + OPTENT3(0, "dilate", OPT_FLAG, NULL, &dilate, 0); + OPTENT3(0, "open", OPT_FLAG, NULL, &open, 0); + OPTENT3(0, "close", OPT_FLAG, NULL, &close, 0); + OPTENT3(0, "gradient", OPT_FLAG, NULL, &gradient, 0); + + opt.opt_table = option_def; + opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ + opt.allowNegNum = TRUE; /* We may have parms that are negative numbers */ + + pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); + /* Uses and sets argc, argv, and some of *cmdlineP and others. */ + + if (erode + dilate + open + close + gradient > 1) + pm_error("You may specify at most one of -erode, -dilate, " + "-open, -close, or -gradient"); + + if (erode) + cmdlineP->operation = ERODE; + else if (dilate) + cmdlineP->operation = DILATE; + else if (open) + cmdlineP->operation = OPEN; + else if (close) + cmdlineP->operation = CLOSE; + else if (gradient) + cmdlineP->operation = GRADIENT; + else + cmdlineP->operation = DILATE; + + if (argc-1 < 1) + pm_error("You must specify the template file name as an argument"); + else { + cmdlineP->templateFileName = argv[1]; + + if (argc-1 < 2) + cmdlineP->inputFileName = "-"; + else { + cmdlineP->inputFileName = argv[2]; + + if (argc-1 > 2) + pm_error("Too many arguments: %u. " + "The only possible arguments " + "are the template file name and the input file name", + argc-1); + } + } +} -} /* dilate */ +static void +readTemplateMatrix(const char * const fileName, + bit *** const templateP, + unsigned int * const rowsP, + unsigned int * const colsP) { +/*---------------------------------------------------------------------------- + Read in the template matrix. +-----------------------------------------------------------------------------*/ + FILE * templateFileP; + int cols, rows; -/************************************************************ - * Erode: same as dilate except !!!! - ************************************************************/ + templateFileP = pm_openr(fileName); -static int -erode( bit** template, int trowso2, int tcolso2, - gray** in_image, gray** out_image, - int rows, int cols ){ + *templateP = pbm_readpbm(templateFileP, &cols, &rows); - int c, r, tc, tr; - int templatecount; - gray source; + pm_close(templateFileP); - for( c=0; c0)?0:-tr) ; r< ((tr>0)?(rows-tr):rows) ; ++r ){ - for( c= ((tc>0)?0:-tc) ; c< ((tc>0)?(cols-tc):cols) ; ++c ){ - source = in_image[r+tr][c+tc]; - out_image[r][c] = MIN(source, out_image[r][c]); - - } /* for c */ - } /* for r */ +static void +dilate(bit ** const template, + int const trowso2, + int const tcolso2, + gray ** const inImage, + gray ** const outImage, + unsigned int const rows, + unsigned int const cols, + unsigned int * const templateCountP) { + + unsigned int templateCount; + int tr; + + setAllPixel(outImage, rows, cols, 0); + + /* for each non-black pixel of the template add in to out */ + + for (tr = -trowso2, templateCount = 0; tr <= trowso2; ++tr) { + int tc; + for (tc = -tcolso2; tc <= tcolso2; ++tc) { + int r; + if (template[trowso2+tr][tcolso2+tc] != PBM_BLACK) { + ++templateCount; + + for (r = ((tr > 0) ? 0 : -tr); + r < ((tr > 0) ? (rows-tr) : rows); + ++r) { + int c; + for (c = ((tc > 0) ? 0 : -tc); + c < ((tc > 0) ? (cols-tc) : cols); + ++c) { + gray const source = inImage[r+tr][c+tc]; + outImage[r][c] = MAX(source, outImage[r][c]); + } + } + } + } + } + *templateCountP = templateCount; +} - } /* for tr */ - } /* for tc */ - return templatecount; +static void +erode(bit ** const template, + int const trowso2, + int const tcolso2, + gray ** const inImage, + gray ** const outImage, + unsigned int const rows, + unsigned int const cols, + unsigned int * const templateCountP) { + + unsigned int templateCount; + int tr; + + setAllPixel(outImage, rows, cols, PGM_MAXMAXVAL); + + /* For each non-black pixel of the template add in to out */ + + for (tr = -trowso2, templateCount = 0; tr <= trowso2; ++tr) { + int tc; + for (tc = -tcolso2; tc <= tcolso2; ++tc) { + if (template[trowso2+tr][tcolso2+tc] != PBM_BLACK) { + int r; + ++templateCount; + + for (r = ((tr > 0) ? 0 : -tr); + r < ((tr > 0) ? (rows-tr) : rows); + ++r){ + int c; + + for (c = ((tc > 0) ? 0 : -tc); + c < ((tc > 0) ? (cols-tc) : cols); + ++c) { + + gray const source = inImage[r+tr][c+tc]; + outImage[r][c] = MIN(source, outImage[r][c]); + + } + } + } + } + } + *templateCountP = templateCount; +} -} /* erode */ static void -subtract(gray** in1_image, gray** in2_image, gray** out_image, - int rows, int cols ){ - - int c, r; - - for( c=0; c