From: ishmal Date: Tue, 28 Feb 2006 23:16:27 +0000 (+0000) Subject: Add some items from Peter Selinger's patch to Potrace 1.7 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=95d09ab8af04cda04811aad963938d6526f55b36;p=inkscape.git Add some items from Peter Selinger's patch to Potrace 1.7 --- diff --git a/src/trace/potrace/curve.h b/src/trace/potrace/curve.h index 9b08f2d37..574433518 100644 --- a/src/trace/potrace/curve.h +++ b/src/trace/potrace/curve.h @@ -18,8 +18,12 @@ struct privcurve_s { int n; /* number of segments */ int *tag; /* tag[n]: POTRACE_CORNER or POTRACE_CURVETO */ - dpoint_t (*c)[3]; /* c[n][i]: control points. + dpoint_t (*c)[3]; /* c[n][i]: control points. c[n][0] is unused for tag[n]=POTRACE_CORNER */ + /* the remainder of this structure is special to privcurve, and is + used in EPS debug output and special EPS "short coding". These + fields are valid only if "alphacurve" is set. */ + int alphacurve; /* have the following fields been initialized? */ dpoint_t *vertex; /* for POTRACE_CORNER, this equals c[1] */ double *alpha; /* only for POTRACE_CURVETO */ double *alpha0; /* "uncropped" alpha parameter - for debug output only */ diff --git a/src/trace/potrace/decompose.cpp b/src/trace/potrace/decompose.cpp index 7c88068f0..512d9f4fe 100644 --- a/src/trace/potrace/decompose.cpp +++ b/src/trace/potrace/decompose.cpp @@ -86,7 +86,7 @@ static void xor_to_ref(potrace_bitmap_t *bm, int x, int y, int xa) { int xhi = x & -BM_WORDBITS; int xlo = x & (BM_WORDBITS-1); /* = x % BM_WORDBITS */ int i; - + if (xhi=size) { size+=100; + //size*=1.3; + size = size * 13 / 10; pt1 = (point_t *)realloc(pt, size * sizeof(point_t)); if (!pt1) { goto error; @@ -197,21 +199,21 @@ static path_t *findpath(potrace_bitmap_t *bm, int x0, int y0, int sign, int turn pt[len].x = x; pt[len].y = y; len++; - + /* move to next point */ x += dirx; y += diry; area += x*diry; - + /* path complete? */ if (x==x0 && y==y0) { break; } - + /* determine next direction */ c = BM_GET(bm, x + (dirx+diry-1)/2, y + (diry-dirx-1)/2); d = BM_GET(bm, x + (dirx-diry-1)/2, y + (diry+dirx-1)/2); - + if (c && !d) { /* ambiguous turn */ if (turnpolicy == POTRACE_TURNPOLICY_RIGHT || (turnpolicy == POTRACE_TURNPOLICY_BLACK && sign == '+') @@ -250,10 +252,10 @@ static path_t *findpath(potrace_bitmap_t *bm, int x0, int y0, int sign, int turn p->sign = sign; return p; - + error: free(pt); - return NULL; + return NULL; } /* Give a tree structure to the given path list, based on "insideness" @@ -279,7 +281,7 @@ static void pathlist_to_tree(path_t *plist, potrace_bitmap_t *bm) { path_t *head; path_t **hook, **hook_in, **hook_out; /* for fast appending to linked list */ bbox_t bbox; - + bm_clear(bm, 0); /* save original "next" pointers */ @@ -287,7 +289,7 @@ static void pathlist_to_tree(path_t *plist, potrace_bitmap_t *bm) { p->sibling = p->next; p->childlist = NULL; } - + heap = plist; /* the heap holds a list of lists of paths. Use "childlist" field @@ -302,7 +304,7 @@ static void pathlist_to_tree(path_t *plist, potrace_bitmap_t *bm) { cur = heap; heap = heap->childlist; cur->childlist = NULL; - + /* unlink first path */ head = cur; cur = cur->next; @@ -345,7 +347,7 @@ static void pathlist_to_tree(path_t *plist, potrace_bitmap_t *bm) { heap = head->childlist; } } - + /* copy sibling structure from "next" to "sibling" component */ p = plist; while (p) { @@ -371,7 +373,7 @@ static void pathlist_to_tree(path_t *plist, potrace_bitmap_t *bm) { /* p is a positive path */ /* append to linked list */ list_insert_beforehook(p, hook); - + /* go through its children */ for (p1=p->childlist; p1; p1=p1->sibling) { /* append to linked list */ @@ -439,7 +441,7 @@ int bm_to_pathlist(const potrace_bitmap_t *bm, path_t **plistp, const potrace_pa /* iterate through components */ y = bm1->h - 1; - while (findnext(bm1, &x, &y) == 0) { + while (findnext(bm1, &x, &y) == 0) { /* calculate the sign by looking at the original */ sign = BM_GET(bm, x, y) ? '+' : '-'; diff --git a/src/trace/potrace/potracelib.cpp b/src/trace/potrace/potracelib.cpp index a51afd81e..c86c0e4c2 100644 --- a/src/trace/potrace/potracelib.cpp +++ b/src/trace/potrace/potracelib.cpp @@ -32,7 +32,7 @@ static const potrace_param_t param_default = { potrace_param_t *potrace_param_default() { potrace_param_t *p; - p = (potrace_param_t *) malloc(sizeof(potrace_param_t)); + p = (potrace_param_t *) malloc(sizeof(potrace_param_t *)); if (!p) { return NULL; } @@ -51,7 +51,7 @@ potrace_state_t *potrace_trace(const potrace_param_t *param, const potrace_bitma potrace_state_t *st; progress_t prog; progress_t subprog; - + /* prepare private progress bar state */ prog.callback = param->progress.callback; prog.data = param->progress.data; @@ -77,6 +77,7 @@ potrace_state_t *potrace_trace(const potrace_param_t *param, const potrace_bitma st->status = POTRACE_STATUS_OK; st->plist = plist; + st->priv = NULL; progress_subrange_end(&prog, &subprog); diff --git a/src/trace/potrace/trace.cpp b/src/trace/potrace/trace.cpp index 78388d3e9..ae9f756b0 100644 --- a/src/trace/potrace/trace.cpp +++ b/src/trace/potrace/trace.cpp @@ -26,7 +26,7 @@ a,n are integers and n>0. */ #define SAFE_MALLOC(var, n, typ) \ - if ((var = (typ *)malloc((n)*sizeof(typ))) == NULL) goto malloc_error + if ((var = (typ *)malloc((n)*sizeof(typ))) == NULL) goto malloc_error /* ---------------------------------------------------------------------- */ /* auxiliary functions */ @@ -35,7 +35,7 @@ but then restricted to one of the major wind directions (n, nw, w, etc) */ static inline point_t dorth_infty(dpoint_t p0, dpoint_t p2) { point_t r; - + r.y = sign(p2.x-p0.x); r.x = -sign(p2.y-p0.y); @@ -100,21 +100,21 @@ static void pointslope(privpath_t *pp, int i, int j, dpoint_t *ctr, dpoint_t *di i+=n; r+=1; } - + x = sums[j+1].x-sums[i].x+r*sums[n].x; y = sums[j+1].y-sums[i].y+r*sums[n].y; x2 = sums[j+1].x2-sums[i].x2+r*sums[n].x2; xy = sums[j+1].xy-sums[i].xy+r*sums[n].xy; y2 = sums[j+1].y2-sums[i].y2+r*sums[n].y2; k = j+1-i+r*n; - + ctr->x = x/k; ctr->y = y/k; a = (x2-(double)x*x/k)/k; b = (xy-(double)x*y/k)/k; c = (y2-(double)y*y/k)/k; - + lambda2 = (a+c+sqrt((a-c)*(a-c)+4*b*b))/2; /* larger e.value */ /* now find e.vector for lambda2 */ @@ -240,7 +240,7 @@ static double tangent(dpoint_t p0, dpoint_t p1, dpoint_t p2, dpoint_t p3, dpoint a = A - 2*B + C; b = -2*A + 2*B; c = A; - + d = b*b - 4*a*c; if (a==0 || d<0) { @@ -286,7 +286,7 @@ static int calc_sums(privpath_t *pp) { pp->sums[i+1].xy = pp->sums[i].xy + x*y; pp->sums[i+1].y2 = pp->sums[i].y2 + y*y; } - return 0; + return 0; malloc_error: return 1; @@ -357,7 +357,7 @@ static int calc_lon(privpath_t *pp) { /* determine pivot points: for each i, let pivk[i] be the furthest k such that all j with i=0; i--) { ct[0] = ct[1] = ct[2] = ct[3] = 0; @@ -374,7 +374,7 @@ static int calc_lon(privpath_t *pp) { k = nc[i]; k1 = i; while (1) { - + dir = (3+3*sign(pt[k].x-pt[k1].x)+sign(pt[k].y-pt[k1].y))/2; ct[dir]++; @@ -406,7 +406,7 @@ static int calc_lon(privpath_t *pp) { if (xprod(constraint[1], off) <= 0) { constraint[1] = off; } - } + } k1 = k; k = nc[k1]; if (!cyclic(k,i,k1)) { @@ -470,7 +470,7 @@ static int calc_lon(privpath_t *pp) { /* ---------------------------------------------------------------------- */ -/* Stage 2: calculate the optimal polygon (Sec. 2.2.2-2.2.4). */ +/* Stage 2: calculate the optimal polygon (Sec. 2.2.2-2.2.4). */ /* Auxiliary function: calculate the penalty of an edge from i to j in the given path. This needs the "lon" and "sum*" data. */ @@ -492,14 +492,14 @@ static double penalty3(privpath_t *pp, int i, int j) { j-=n; r+=1; } - + x = sums[j+1].x-sums[i].x+r*sums[n].x; y = sums[j+1].y-sums[i].y+r*sums[n].y; x2 = sums[j+1].x2-sums[i].x2+r*sums[n].x2; xy = sums[j+1].xy-sums[i].xy+r*sums[n].xy; y2 = sums[j+1].y2-sums[i].y2+r*sums[n].y2; k = j+1-i+r*n; - + px = (pt[i].x+pt[j].x)/2.0-pt[0].x; py = (pt[i].y+pt[j].y)/2.0-pt[0].y; ey = (pt[j].x-pt[i].x); @@ -508,7 +508,7 @@ static double penalty3(privpath_t *pp, int i, int j) { a = ((x2-2*x*px)/k+px*px); b = ((xy-x*py-y*px)/k+px*py); c = ((y2-2*y*py)/k+py*py); - + s = ex*ex*a + 2*ex*ey*b + ey*ey*c; return sqrt(s); @@ -519,7 +519,7 @@ static double penalty3(privpath_t *pp, int i, int j) { is in the polygon. Fixme: ### implement cyclic version. */ static int bestpolygon(privpath_t *pp) { - int i, j, m, k; + int i, j, m, k; int n = pp->len; double *pen = NULL; /* pen[n+1]: penalty vector */ int *prev = NULL; /* prev[n+1]: best path pointer vector */ @@ -613,7 +613,7 @@ static int bestpolygon(privpath_t *pp) free(seg0); free(seg1); return 0; - + malloc_error: free(pen); free(prev); @@ -657,7 +657,7 @@ static int adjust_vertices(privpath_t *pp) { if (r) { goto malloc_error; } - + /* calculate "optimal" point-slope representation for each line segment */ for (i=0; ialpha[j] = alpha; /* store the "cropped" value of alpha */ curve->beta[j] = 0.5; } - + curve->alphacurve = 1; return 0; } @@ -962,8 +962,8 @@ static int opti_penalty(privpath_t *pp, int i, int j, opti_t *res, double opttol A2 = dpara(p0, p1, p3); A3 = dpara(p0, p2, p3); /* A4 = dpara(p1, p2, p3); */ - A4 = A1+A3-A2; - + A4 = A1+A3-A2; + if (A2 == A1) { /* this should never happen */ return 1; } @@ -971,7 +971,7 @@ static int opti_penalty(privpath_t *pp, int i, int j, opti_t *res, double opttol t = A3/(A3-A4); s = A2/(A2-A1); A = A2 * t / 2.0; - + if (A == 0.0) { /* this should never happen */ return 1; } @@ -1160,6 +1160,7 @@ static int opticurve(privpath_t *pp, double opttolerance) { i1 = mod(i+1,om); pp->ocurve.beta[i] = s[i] / (s[i] + t[i1]); } + pp->ocurve.alphacurve = 1; free(pt); free(pen); @@ -1200,7 +1201,7 @@ int process_path(path_t *plist, const potrace_param_t *param, progress_t *progre } cn = 0; } - + /* call downstream function with each path */ list_forall (p, plist) { TRY(calc_sums(p->priv));