#define PROG_NAME "pz_boundary" #define PROG_DESC "extracts a fragment contour from a PGM image, writes it as a pz_r3_chain_t" #define PROG_VERS "1.0" /* Copyright © 2001 by the State University of Campinas (UNICAMP). */ /* See the copyright, authorship, and warranty notice at end of file. */ /* Last edited on 2015-01-20 16:43:22 by stolfilocal */ #define PROG_HELP \ " " PROG_NAME " \\\n" \ " -inFile \\\n" \ " -black \\\n" \ " -grey \\\n" \ " [ -pixelSize ] \\\n" \ " [ -outUnit ] \\\n" \ " -outFile \\\n" \ " [ -inside | -outside ]\n" #define PROG_INFO \ "NAME\n" \ " " PROG_NAME " - " PROG_DESC "\n" \ "\n" \ "SYNOPSIS\n" \ PROG_HELP "\n" \ "\n" \ "AUTHORS\n" \ " Created in 2001 by Helena C. G. Leitão and Jorge Stolfi at IC-UNICAMP.\n" \ " Converted from Modula-3 to C in 2004-2006 by Jorge Stolfi." #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include typedef struct options_t { char *inFile; /* Input image name, without ".pgm" */ int black; /* Maximum black level */ int grey; /* grey level */ pz_side_t side; /* Which contour to write */ double pixelSize; /* Pixel size in use units. */ double outUnit; /* Output scale factor. */ char *outFile; /* Output float curve name, without ".flc" */ } options_t; options_t *pz_get_options ( int argc, char **argv ); char *pz_comments ( options_t *o ); void pz_write_curve ( char *name, char *cmt, pz_r3_chain_t *c, double unit ); int main ( int argc, char **argv ) { options_t *o = pz_get_options(argc, argv); char *cc = pz_comments(o); fprintf(stderr,"reading input file..."); pz_image_t *img = pz_image_read_pgm(o->inFile); fprintf(stderr, " %d×%d\n", img->cols, img->rows); pz_r3_chain_t a = pz_image_extract_boundary(img, (uint16_t)o->grey, o->side); if (o->pixelSize != 1.0) { int i; for (i = 0; i < a.ne; i++) { r3_scale(o->pixelSize, &(a.el[i]), &(a.el[i])); } } pz_write_curve(o->outFile, cc, &a, o->outUnit); return 0; } void pz_write_curve( char *name, char *cmt, pz_r3_chain_t *c, double unit ) { char *fileName = txtcat(name, ".flc"); FILE *wr = open_write(fileName, TRUE); double adjUnit = pz_r3_chain_adjust_unit(unit, c); pz_r3_chain_write(wr, cmt, c, unit = adjUnit); } options_t *pz_get_options(int argc, char **argv) { argparser_t *pp = argparser_new(stderr, argc, argv); argparser_set_help(pp, PROG_NAME " version " PROG_VERS ", usage:\n" PROG_HELP); argparser_set_info(pp, PROG_INFO); argparser_process_help_info_options(pp); options_t *o = (options_t *)malloc(sizeof(options_t)); argparser_get_keyword(pp, "-inFile"); o->inFile = argparser_get_next(pp); argparser_get_keyword(pp, "-black"); o->black = argparser_get_next_int(pp, 0, 254); argparser_get_keyword(pp, "-grey"); o->grey = argparser_get_next_int(pp, 0, 255); if (argparser_keyword_present(pp, "-pixelSize")) { o->pixelSize = argparser_get_next_double(pp, 0.1e-10, 0.1e10); } else { o->pixelSize = 1.0e0; } if (argparser_keyword_present(pp, "-outUnit")) { o->outUnit = argparser_get_next_double(pp, 0.1e-10, 0.1e10); } else { o->outUnit = 0.01e0; } argparser_get_keyword(pp, "-outFile"); o->outFile = argparser_get_next(pp); if (argparser_keyword_present(pp, "-inside")) { o->side = pz_side_inner; } else if (argparser_keyword_present(pp, "-outside")) { o->side = pz_side_outer; } else { o->side = pz_side_interpolated; } argparser_finish(pp); return o; } char *pz_comments(options_t *o) { char *buf = NULL; char *buf = jsprintf( "pz_boundary\n" " inFile = %s\n" " black = %d\n" " grey = %d\n" " pixelSize = %.8f\n" " outUnit = %.8f\n", o->inFile, o->black, o->grey, o->pixelSize, o->outUnit ); return buf; } /* Copyright © 2001 Universidade Estadual de Campinas (UNICAMP). Authors: Helena C. G. Leitão and Jorge Stolfi. This file can be freely distributed, used, and modified, provided that this copyright and authorship notice is preserved, and that any modified versions are clearly marked as such. This software has NO WARRANTY of correctness or applicability for any purpose. Neither the authors nor their employers chall be held responsible for any losses or damages that may result from its use. */