static void dopts(int ac, char *av[]) { int c; char *p; int i, j, r; dim_t dim, dimx, dimy; int matches, bestmatch; /* defaults */ backend_lookup("eps", &info.backend); info.debug = 0; info.width_d.x = UNDEF; info.height_d.x = UNDEF; info.rx = UNDEF; info.ry = UNDEF; info.sx = UNDEF; info.sy = UNDEF; info.stretch = 1; info.lmar_d.x = UNDEF; info.rmar_d.x = UNDEF; info.tmar_d.x = UNDEF; info.bmar_d.x = UNDEF; info.angle = 0; info.paperwidth = DEFAULT_PAPERWIDTH; info.paperheight = DEFAULT_PAPERHEIGHT; info.tight = 0; info.unit = 10; info.compress = 1; info.pslevel = 2; info.color = 0x000000; info.gamma = 2.2; info.param = potrace_param_default(); if (!info.param) { fprintf(stderr, ""POTRACE": %s\n", strerror(errno)); exit(1); } info.longcoding = 0; info.outfile = NULL; info.blacklevel = 0.5; info.invert = 0; info.opaque = 0; info.grouping = 1; info.fillcolor = 0xffffff; info.progress = 0; info.progress_bar = DEFAULT_PROGRESS_BAR; while ((c = getopt_long(ac, av, shortopts, longopts, NULL)) != -1) { switch (c) { case 'h': fprintf(stdout, ""POTRACE" "VERSION". Transforms bitmaps into vector graphics.\n\n"); usage(stdout); exit(0); break; case 'v': case 'V': fprintf(stdout, ""POTRACE" "VERSION". Copyright (C) 2001-2015 Peter Selinger.\n"); fprintf(stdout, "Library version: %s\n", potrace_version()); show_defaults(stdout); exit(0); break; case 'l': fprintf(stdout, ""POTRACE" "VERSION". Copyright (C) 2001-2015 Peter Selinger.\n\n"); license(stdout); exit(0); break; case 'W': info.width_d = parse_dimension(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid dimension -- %s\n", optarg); exit(1); } break; case 'H': info.height_d = parse_dimension(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid dimension -- %s\n", optarg); exit(1); } break; case 'r': parse_dimensions(optarg, &p, &dimx, &dimy); if (*p == 0 && dimx.d == 0 && dimy.d == 0 && dimx.x != 0.0 && dimy.x != 0.0) { info.rx = dimx.x; info.ry = dimy.x; break; } dim = parse_dimension(optarg, &p); if (*p == 0 && dim.d == 0 && dim.x != 0.0) { info.rx = info.ry = dim.x; break; } fprintf(stderr, ""POTRACE": invalid resolution -- %s\n", optarg); exit(1); break; case 'x': parse_dimensions(optarg, &p, &dimx, &dimy); if (*p == 0 && dimx.d == 0 && dimy.d == 0) { info.sx = dimx.x; info.sy = dimy.x; break; } dim = parse_dimension(optarg, &p); if (*p == 0 && dim.d == 0) { info.sx = info.sy = dim.x; break; } fprintf(stderr, ""POTRACE": invalid scaling factor -- %s\n", optarg); exit(1); break; case 'S': info.stretch = atof(optarg); break; case 'M': info.lmar_d = parse_dimension(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid dimension -- %s\n", optarg); exit(1); } info.rmar_d = info.tmar_d = info.bmar_d = info.lmar_d; break; case 'L': info.lmar_d = parse_dimension(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid dimension -- %s\n", optarg); exit(1); } break; case 'R': info.rmar_d = parse_dimension(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid dimension -- %s\n", optarg); exit(1); } break; case 'T': info.tmar_d = parse_dimension(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid dimension -- %s\n", optarg); exit(1); } break; case 'B': info.bmar_d = parse_dimension(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid dimension -- %s\n", optarg); exit(1); } break; case OPT_TIGHT: info.tight = 1; break; case 'A': info.angle = strtod(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid angle -- %s\n", optarg); exit(1); } break; case 'P': matches = 0; bestmatch = 0; for (i=0; pageformat[i].name!=NULL; i++) { if (strcasecmp(pageformat[i].name, optarg)==0) { matches = 1; bestmatch = i; break; } else if (strncasecmp(pageformat[i].name, optarg, strlen(optarg))==0) { /* don't allow partial match on "10x14" */ if (optarg[0] != '1') { matches++; bestmatch = i; } } } if (matches == 1) { info.paperwidth = pageformat[bestmatch].w; info.paperheight = pageformat[bestmatch].h; break; } parse_dimensions(optarg, &p, &dimx, &dimy); if (*p == 0) { info.paperwidth = (int)rint(double_of_dim(dimx, DEFAULT_DIM)); info.paperheight = (int)rint(double_of_dim(dimy, DEFAULT_DIM)); break; } if (matches == 0) { fprintf(stderr, ""POTRACE": unrecognized page format -- %s\n", optarg); } else { fprintf(stderr, ""POTRACE": ambiguous page format -- %s\n", optarg); } j = fprintf(stderr, "Use one of: "); for (i=0; pageformat[i].name!=NULL; i++) { if (j + strlen(pageformat[i].name) > 75) { fprintf(stderr, "\n"); j = 0; } j += fprintf(stderr, "%s, ", pageformat[i].name); } fprintf(stderr, "or specify <dim>x<dim>.\n"); exit(1); break; case 't': info.param->turdsize = atoi(optarg); break; case 'u': info.unit = strtod(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid unit -- %s\n", optarg); exit(1); } break; case 'c': info.pslevel = 2; info.compress = 0; break; case '2': info.pslevel = 2; info.compress = 1; break; case '3': #ifdef HAVE_ZLIB info.pslevel = 3; info.compress = 1; #else fprintf(stderr, ""POTRACE": option -3 not supported, using -2 instead.\n"); fflush(stderr); info.pslevel = 2; info.compress = 1; #endif break; case 'e': backend_lookup("eps", &info.backend); break; case 'p': backend_lookup("postscript", &info.backend); break; case 's': backend_lookup("svg", &info.backend); break; case 'g': backend_lookup("pgm", &info.backend); break; case 'b': r = backend_lookup(optarg, &info.backend); if (r==1 || r==2) { if (r==1) { fprintf(stderr, ""POTRACE": unrecognized backend -- %s\n", optarg); } else { fprintf(stderr, ""POTRACE": ambiguous backend -- %s\n", optarg); } j = fprintf(stderr, "Use one of: "); backend_list(stderr, j, 70); fprintf(stderr, ".\n"); exit(1); } break; case 'd': info.debug = atoi(optarg); break; case 'C': info.color = parse_color(optarg); if (info.color == -1) { fprintf(stderr, ""POTRACE": invalid color -- %s\n", optarg); exit(1); } break; case OPT_FILLCOLOR: info.fillcolor = parse_color(optarg); if (info.fillcolor == -1) { fprintf(stderr, ""POTRACE": invalid color -- %s\n", optarg); exit(1); } info.opaque = 1; break; case 'z': matches = 0; bestmatch = 0; for (i=0; turnpolicy[i].name!=NULL; i++) { if (strcasecmp(turnpolicy[i].name, optarg)==0) { matches = 1; bestmatch = i; break; } else if (strncasecmp(turnpolicy[i].name, optarg, strlen(optarg))==0) { matches++; bestmatch = i; } } if (matches == 1) { info.param->turnpolicy = turnpolicy[bestmatch].n; break; } if (matches == 0) { fprintf(stderr, ""POTRACE": unrecognized turnpolicy -- %s\n", optarg); } else { fprintf(stderr, ""POTRACE": ambiguous turnpolicy -- %s\n", optarg); } j = fprintf(stderr, "Use one of: "); for (i=0; turnpolicy[i].name!=NULL; i++) { if (j + strlen(turnpolicy[i].name) > 75) { fprintf(stderr, "\n"); j = 0; } j += fprintf(stderr, "%s%s", turnpolicy[i].name, turnpolicy[i+1].name ? ", " : ""); } fprintf(stderr, ".\n"); exit(1); break; case 'G': info.gamma = atof(optarg); break; case 'n': info.param->opticurve = 0; break; case 'q': info.longcoding = 1; break; case 'a': info.param->alphamax = strtod(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid alphamax -- %s\n", optarg); exit(1); } break; case 'O': info.param->opttolerance = strtod(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid opttolerance -- %s\n", optarg); exit(1); } break; case 'o': free(info.outfile); info.outfile = strdup(optarg); if (!info.outfile) { fprintf(stderr, ""POTRACE": %s\n", strerror(errno)); exit(2); } break; case 'k': info.blacklevel = strtod(optarg, &p); if (*p) { fprintf(stderr, ""POTRACE": invalid blacklevel -- %s\n", optarg); exit(1); } break; case 'i': info.invert = 1; break; case OPT_OPAQUE: info.opaque = 1; break; case OPT_GROUP: info.grouping = 2; break; case OPT_FLAT: info.grouping = 0; break; case OPT_PROGRESS: info.progress = 1; break; case OPT_TTY: if (strcmp(optarg, "dumb") == 0) { info.progress_bar = progress_bar_simplified; } else if (strcmp(optarg, "vt100") == 0) { info.progress_bar = progress_bar_vt100; } else { fprintf(stderr, ""POTRACE": invalid tty mode -- %s. Try --help for more info\n", optarg); exit(1); } break; case '?': fprintf(stderr, "Try --help for more info\n"); exit(1); break; default: fprintf(stderr, ""POTRACE": Unimplemented option -- %c\n", c); exit(1); } } info.infiles = &av[optind]; info.infilecount = ac-optind; info.some_infiles = info.infilecount ? 1 : 0; /* if "--" was used, even an empty list of filenames is considered "some" filenames. */ if (strcmp(av[optind-1], "--") == 0) { info.some_infiles = 1; } }
static PyObject * libtrace_GetVersion (PyObject *self, PyObject *args) { return Py_BuildValue("s", potrace_version()); }
int main(int argc, char **argv) { const std::string sourceInput = argv[1]; cv::VideoCapture sourceCapture(atoi(sourceInput.c_str())); // cv::VideoCapture sourceCapture(sourceInput); if (!sourceCapture.isOpened()) { LOG_ERROR("can't open %s", sourceInput.c_str()); return -1; } cv::namedWindow(WIN_MAIN); cv::namedWindow(WIN_ANALYZE); cv::namedWindow(WIN_DEBUG); cv::Mat img_source, img_gray, img_bw, img_edges, img_gray_blur; printf("using potrace: %s\n", potrace_version()); while (sourceCapture.isOpened()) //Show the image captured in the window and repeat { sourceCapture.read(img_source); cvtColor(img_source, img_gray, CV_BGR2GRAY); uchar med = 0; assert(median(img_gray, med)); cv::threshold(img_gray, img_bw, med, 255, cv::THRESH_BINARY); potrace_bitmap_t* bm = create_bm_from_Mat(img_bw); assert(bm != nullptr); potrace_param_t* param = potrace_param_default(); potrace_state_t *state = potrace_trace(param, bm); assert(state->status == POTRACE_STATUS_OK); potrace_path_t* path = state->plist; std::vector<std::vector<cv::Point> > contours; contours.empty(); while (path != nullptr) { std::vector<cv::Point> contour; for (int i = 0; i < path->curve.n; ++i) { path->curve.tag[i]; contour.push_back(cv::Point(path->curve.c[i][2].x, path->curve.c[i][2].y)); } contours.push_back(contour); path = path->next; } printf("%d\n", contours.size()); cv::drawContours(img_source, contours, -1, cv::Scalar(0, 0, 0), 2); potrace_state_free(state); release_bm(bm); potrace_param_free(param); /// Convert the image to grayscale cv::imshow(WIN_MAIN, img_source); cv::imshow(WIN_DEBUG, img_bw); char c = cv::waitKey(33); if (c == 27) { cv::imwrite("/tmp/lastframe.jpg", img_source); break; } } cv::destroyWindow(WIN_MAIN); cv::destroyWindow(WIN_ANALYZE); cv::destroyWindow(WIN_DEBUG); return 0; }