You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1456 lines
38 KiB

  1. /* Copyright (C) 2001-2007 Peter Selinger.
  2. * This file is part of Potrace. It is free software and it is covered
  3. * by the GNU General Public License. See the file COPYING for details. */
  4. /* $Id: trace.c 147 2007-04-09 00:44:09Z selinger $ */
  5. /* transform jaggy paths into smooth curves */
  6. #include <stdio.h>
  7. #include <cmath>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <potracelib.h>
  11. #include <curve.h>
  12. #include <lists.h>
  13. #include <auxiliary.h>
  14. #include <trace.h>
  15. #include <progress.h>
  16. #define INFTY 10000000 /* it suffices that this is longer than any
  17. * path; it need not be really infinite */
  18. #define COS179 -0.999847695156 /* the cosine of 179 degrees */
  19. /* ---------------------------------------------------------------------- */
  20. #define SAFE_MALLOC( var, n, typ ) \
  21. if( ( var = (typ*) malloc( (n) * sizeof(typ) ) ) == NULL ) \
  22. goto malloc_error
  23. /* ---------------------------------------------------------------------- */
  24. /* auxiliary functions */
  25. /* return a direction that is 90 degrees counterclockwise from p2-p0,
  26. * but then restricted to one of the major wind directions (n, nw, w, etc) */
  27. static inline point_t dorth_infty( dpoint_t p0, dpoint_t p2 )
  28. {
  29. point_t r;
  30. r.y = sign( p2.x - p0.x );
  31. r.x = -sign( p2.y - p0.y );
  32. return r;
  33. }
  34. /* return (p1-p0)x(p2-p0), the area of the parallelogram */
  35. static inline double dpara( dpoint_t p0, dpoint_t p1, dpoint_t p2 )
  36. {
  37. double x1, y1, x2, y2;
  38. x1 = p1.x - p0.x;
  39. y1 = p1.y - p0.y;
  40. x2 = p2.x - p0.x;
  41. y2 = p2.y - p0.y;
  42. return x1 * y2 - x2 * y1;
  43. }
  44. /* ddenom/dpara have the property that the square of radius 1 centered
  45. * at p1 intersects the line p0p2 iff |dpara(p0,p1,p2)| <= ddenom(p0,p2) */
  46. static inline double ddenom( dpoint_t p0, dpoint_t p2 )
  47. {
  48. point_t r = dorth_infty( p0, p2 );
  49. return r.y * (p2.x - p0.x) - r.x * (p2.y - p0.y);
  50. }
  51. /* return 1 if a <= b < c < a, in a cyclic sense (mod n) */
  52. static inline int cyclic( int a, int b, int c )
  53. {
  54. if( a<=c )
  55. {
  56. return a<=b && b<c;
  57. }
  58. else
  59. {
  60. return a<=b || b<c;
  61. }
  62. }
  63. /* determine the center and slope of the line i..j. Assume i<j. Needs
  64. * "sum" components of p to be set. */
  65. static void pointslope( privpath_t* pp, int i, int j, dpoint_t* ctr, dpoint_t* dir )
  66. {
  67. /* assume i<j */
  68. int n = pp->len;
  69. sums_t* sums = pp->sums;
  70. double x, y, x2, xy, y2;
  71. double k;
  72. double a, b, c, lambda2, l;
  73. int r = 0; /* rotations from i to j */
  74. while( j>=n )
  75. {
  76. j -= n;
  77. r += 1;
  78. }
  79. while( i>=n )
  80. {
  81. i -= n;
  82. r -= 1;
  83. }
  84. while( j<0 )
  85. {
  86. j += n;
  87. r -= 1;
  88. }
  89. while( i<0 )
  90. {
  91. i += n;
  92. r += 1;
  93. }
  94. x = sums[j + 1].x - sums[i].x + r * sums[n].x;
  95. y = sums[j + 1].y - sums[i].y + r * sums[n].y;
  96. x2 = sums[j + 1].x2 - sums[i].x2 + r * sums[n].x2;
  97. xy = sums[j + 1].xy - sums[i].xy + r * sums[n].xy;
  98. y2 = sums[j + 1].y2 - sums[i].y2 + r * sums[n].y2;
  99. k = j + 1 - i + r * n;
  100. ctr->x = x / k;
  101. ctr->y = y / k;
  102. a = (x2 - (double) x * x / k) / k;
  103. b = (xy - (double) x * y / k) / k;
  104. c = (y2 - (double) y * y / k) / k;
  105. lambda2 = ( a + c + sqrt( (a - c) * (a - c) + 4 * b * b ) ) / 2; /* larger e.value */
  106. /* now find e.vector for lambda2 */
  107. a -= lambda2;
  108. c -= lambda2;
  109. if( fabs( a ) >= fabs( c ) )
  110. {
  111. l = sqrt( a * a + b * b );
  112. if( l!=0 )
  113. {
  114. dir->x = -b / l;
  115. dir->y = a / l;
  116. }
  117. }
  118. else
  119. {
  120. l = sqrt( c * c + b * b );
  121. if( l!=0 )
  122. {
  123. dir->x = -c / l;
  124. dir->y = b / l;
  125. }
  126. }
  127. if( l==0 )
  128. {
  129. dir->x = dir->y = 0; /* sometimes this can happen when k=4:
  130. * the two eigenvalues coincide */
  131. }
  132. }
  133. /* the type of (affine) quadratic forms, represented as symmetric 3x3
  134. * matrices. The value of the quadratic form at a vector (x,y) is v^t
  135. * Q v, where v = (x,y,1)^t. */
  136. typedef double quadform_t[3][3];
  137. /* Apply quadratic form Q to vector w = (w.x,w.y) */
  138. static inline double quadform( quadform_t Q, dpoint_t w )
  139. {
  140. double v[3];
  141. int i, j;
  142. double sum;
  143. v[0] = w.x;
  144. v[1] = w.y;
  145. v[2] = 1;
  146. sum = 0.0;
  147. for( i = 0; i<3; i++ )
  148. {
  149. for( j = 0; j<3; j++ )
  150. {
  151. sum += v[i] *Q[i][j] *v[j];
  152. }
  153. }
  154. return sum;
  155. }
  156. /* calculate p1 x p2 */
  157. static inline int xprod( point_t p1, point_t p2 )
  158. {
  159. return p1.x * p2.y - p1.y * p2.x;
  160. }
  161. /* calculate (p1-p0)x(p3-p2) */
  162. static inline double cprod( dpoint_t p0, dpoint_t p1, dpoint_t p2, dpoint_t p3 )
  163. {
  164. double x1, y1, x2, y2;
  165. x1 = p1.x - p0.x;
  166. y1 = p1.y - p0.y;
  167. x2 = p3.x - p2.x;
  168. y2 = p3.y - p2.y;
  169. return x1 * y2 - x2 * y1;
  170. }
  171. /* calculate (p1-p0)*(p2-p0) */
  172. static inline double iprod( dpoint_t p0, dpoint_t p1, dpoint_t p2 )
  173. {
  174. double x1, y1, x2, y2;
  175. x1 = p1.x - p0.x;
  176. y1 = p1.y - p0.y;
  177. x2 = p2.x - p0.x;
  178. y2 = p2.y - p0.y;
  179. return x1 * x2 + y1 * y2;
  180. }
  181. /* calculate (p1-p0)*(p3-p2) */
  182. static inline double iprod1( dpoint_t p0, dpoint_t p1, dpoint_t p2, dpoint_t p3 )
  183. {
  184. double x1, y1, x2, y2;
  185. x1 = p1.x - p0.x;
  186. y1 = p1.y - p0.y;
  187. x2 = p3.x - p2.x;
  188. y2 = p3.y - p2.y;
  189. return x1 * x2 + y1 * y2;
  190. }
  191. /* calculate distance between two points */
  192. static inline double ddist( dpoint_t p, dpoint_t q )
  193. {
  194. return sqrt( sq( p.x - q.x ) + sq( p.y - q.y ) );
  195. }
  196. /* calculate point of a bezier curve */
  197. static inline dpoint_t bezier( double t, dpoint_t p0, dpoint_t p1, dpoint_t p2, dpoint_t p3 )
  198. {
  199. double s = 1 - t;
  200. dpoint_t res;
  201. /* Note: a good optimizing compiler (such as gcc-3) reduces the
  202. * following to 16 multiplications, using common subexpression
  203. * elimination. */
  204. res.x = s * s * s * p0.x + 3 * (s * s * t) * p1.x + 3 * (t * t * s) * p2.x + t * t * t * p3.x;
  205. res.y = s * s * s * p0.y + 3 * (s * s * t) * p1.y + 3 * (t * t * s) * p2.y + t * t * t * p3.y;
  206. return res;
  207. }
  208. /* calculate the point t in [0..1] on the (convex) bezier curve
  209. * (p0,p1,p2,p3) which is tangent to q1-q0. Return -1.0 if there is no
  210. * solution in [0..1]. */
  211. static double tangent( dpoint_t p0,
  212. dpoint_t p1,
  213. dpoint_t p2,
  214. dpoint_t p3,
  215. dpoint_t q0,
  216. dpoint_t q1 )
  217. {
  218. double A, B, C; /* (1-t)^2 A + 2(1-t)t B + t^2 C = 0 */
  219. double a, b, c; /* a t^2 + b t + c = 0 */
  220. double d, s, r1, r2;
  221. A = cprod( p0, p1, q0, q1 );
  222. B = cprod( p1, p2, q0, q1 );
  223. C = cprod( p2, p3, q0, q1 );
  224. a = A - 2 * B + C;
  225. b = -2 * A + 2 * B;
  226. c = A;
  227. d = b * b - 4 * a * c;
  228. if( a==0 || d<0 )
  229. {
  230. return -1.0;
  231. }
  232. s = sqrt( d );
  233. r1 = (-b + s) / (2 * a);
  234. r2 = (-b - s) / (2 * a);
  235. if( r1 >= 0 && r1 <= 1 )
  236. {
  237. return r1;
  238. }
  239. else if( r2 >= 0 && r2 <= 1 )
  240. {
  241. return r2;
  242. }
  243. else
  244. {
  245. return -1.0;
  246. }
  247. }
  248. /* ---------------------------------------------------------------------- */
  249. /* Preparation: fill in the sum* fields of a path (used for later
  250. * rapid summing). Return 0 on success, 1 with errno set on
  251. * failure. */
  252. static int calc_sums( privpath_t* pp )
  253. {
  254. int i, x, y;
  255. int n = pp->len;
  256. SAFE_MALLOC( pp->sums, pp->len + 1, sums_t );
  257. /* origin */
  258. pp->x0 = pp->pt[0].x;
  259. pp->y0 = pp->pt[0].y;
  260. /* preparatory computation for later fast summing */
  261. pp->sums[0].x2 = pp->sums[0].xy = pp->sums[0].y2 = pp->sums[0].x = pp->sums[0].y = 0;
  262. for( i = 0; i<n; i++ )
  263. {
  264. x = pp->pt[i].x - pp->x0;
  265. y = pp->pt[i].y - pp->y0;
  266. pp->sums[i + 1].x = pp->sums[i].x + x;
  267. pp->sums[i + 1].y = pp->sums[i].y + y;
  268. pp->sums[i + 1].x2 = pp->sums[i].x2 + x * x;
  269. pp->sums[i + 1].xy = pp->sums[i].xy + x * y;
  270. pp->sums[i + 1].y2 = pp->sums[i].y2 + y * y;
  271. }
  272. return 0;
  273. malloc_error:
  274. return 1;
  275. }
  276. /* ---------------------------------------------------------------------- */
  277. /* Stage 1: determine the straight subpaths (Sec. 2.2.1). Fill in the
  278. * "lon" component of a path object (based on pt/len). For each i,
  279. * lon[i] is the furthest index such that a straight line can be drawn
  280. * from i to lon[i]. Return 1 on error with errno set, else 0. */
  281. /* this algorithm depends on the fact that the existence of straight
  282. * subpaths is a triplewise property. I.e., there exists a straight
  283. * line through squares i0,...,in iff there exists a straight line
  284. * through i,j,k, for all i0<=i<j<k<=in. (Proof?) */
  285. /* this implementation of calc_lon is O(n^2). It replaces an older
  286. * O(n^3) version. A "constraint" means that future points must
  287. * satisfy xprod(constraint[0], cur) >= 0 and xprod(constraint[1],
  288. * cur) <= 0. */
  289. /* Remark for Potrace 1.1: the current implementation of calc_lon is
  290. * more complex than the implementation found in Potrace 1.0, but it
  291. * is considerably faster. The introduction of the "nc" data structure
  292. * means that we only have to test the constraints for "corner"
  293. * points. On a typical input file, this speeds up the calc_lon
  294. * function by a factor of 31.2, thereby decreasing its time share
  295. * within the overall Potrace algorithm from 72.6% to 7.82%, and
  296. * speeding up the overall algorithm by a factor of 3.36. On another
  297. * input file, calc_lon was sped up by a factor of 6.7, decreasing its
  298. * time share from 51.4% to 13.61%, and speeding up the overall
  299. * algorithm by a factor of 1.78. In any case, the savings are
  300. * substantial. */
  301. /* returns 0 on success, 1 on error with errno set */
  302. static int calc_lon( privpath_t* pp )
  303. {
  304. point_t* pt = pp->pt;
  305. int n = pp->len;
  306. int i, j, k, k1;
  307. int ct[4], dir;
  308. point_t constraint[2];
  309. point_t cur;
  310. point_t off;
  311. int* pivk = NULL; /* pivk[n] */
  312. int* nc = NULL; /* nc[n]: next corner */
  313. point_t dk; /* direction of k-k1 */
  314. int a, b, c, d;
  315. SAFE_MALLOC( pivk, n, int );
  316. SAFE_MALLOC( nc, n, int );
  317. /* initialize the nc data structure. Point from each point to the
  318. * furthest future point to which it is connected by a vertical or
  319. * horizontal segment. We take advantage of the fact that there is
  320. * always a direction change at 0 (due to the path decomposition
  321. * algorithm). But even if this were not so, there is no harm, as
  322. * in practice, correctness does not depend on the word "furthest"
  323. * above. */
  324. k = 0;
  325. for( i = n - 1; i>=0; i-- )
  326. {
  327. if( pt[i].x != pt[k].x && pt[i].y != pt[k].y )
  328. {
  329. k = i + 1; /* necessarily i<n-1 in this case */
  330. }
  331. nc[i] = k;
  332. }
  333. SAFE_MALLOC( pp->lon, n, int );
  334. /* determine pivot points: for each i, let pivk[i] be the furthest k
  335. * such that all j with i<j<k lie on a line connecting i,k. */
  336. for( i = n - 1; i>=0; i-- )
  337. {
  338. ct[0] = ct[1] = ct[2] = ct[3] = 0;
  339. /* keep track of "directions" that have occurred */
  340. dir =
  341. ( 3 + 3 * (pt[mod( i + 1, n )].x - pt[i].x) + (pt[mod( i + 1, n )].y - pt[i].y) ) / 2;
  342. ct[dir]++;
  343. constraint[0].x = 0;
  344. constraint[0].y = 0;
  345. constraint[1].x = 0;
  346. constraint[1].y = 0;
  347. /* find the next k such that no straight line from i to k */
  348. k = nc[i];
  349. k1 = i;
  350. while( 1 )
  351. {
  352. dir = ( 3 + 3 * sign( pt[k].x - pt[k1].x ) + sign( pt[k].y - pt[k1].y ) ) / 2;
  353. ct[dir]++;
  354. /* if all four "directions" have occurred, cut this path */
  355. if( ct[0] && ct[1] && ct[2] && ct[3] )
  356. {
  357. pivk[i] = k1;
  358. goto foundk;
  359. }
  360. cur.x = pt[k].x - pt[i].x;
  361. cur.y = pt[k].y - pt[i].y;
  362. /* see if current constraint is violated */
  363. if( xprod( constraint[0], cur ) < 0 || xprod( constraint[1], cur ) > 0 )
  364. {
  365. goto constraint_viol;
  366. }
  367. /* else, update constraint */
  368. if( abs( cur.x ) <= 1 && abs( cur.y ) <= 1 )
  369. {
  370. /* no constraint */
  371. }
  372. else
  373. {
  374. off.x = cur.x + ( ( cur.y>=0 && (cur.y>0 || cur.x<0) ) ? 1 : -1 );
  375. off.y = cur.y + ( ( cur.x<=0 && (cur.x<0 || cur.y<0) ) ? 1 : -1 );
  376. if( xprod( constraint[0], off ) >= 0 )
  377. {
  378. constraint[0] = off;
  379. }
  380. off.x = cur.x + ( ( cur.y<=0 && (cur.y<0 || cur.x<0) ) ? 1 : -1 );
  381. off.y = cur.y + ( ( cur.x>=0 && (cur.x>0 || cur.y<0) ) ? 1 : -1 );
  382. if( xprod( constraint[1], off ) <= 0 )
  383. {
  384. constraint[1] = off;
  385. }
  386. }
  387. k1 = k;
  388. k = nc[k1];
  389. if( !cyclic( k, i, k1 ) )
  390. {
  391. break;
  392. }
  393. }
  394. constraint_viol:
  395. /* k1 was the last "corner" satisfying the current constraint, and
  396. * k is the first one violating it. We now need to find the last
  397. * point along k1..k which satisfied the constraint. */
  398. dk.x = sign( pt[k].x - pt[k1].x );
  399. dk.y = sign( pt[k].y - pt[k1].y );
  400. cur.x = pt[k1].x - pt[i].x;
  401. cur.y = pt[k1].y - pt[i].y;
  402. /* find largest integer j such that xprod(constraint[0], cur+j*dk)
  403. * >= 0 and xprod(constraint[1], cur+j*dk) <= 0. Use bilinearity
  404. * of xprod. */
  405. a = xprod( constraint[0], cur );
  406. b = xprod( constraint[0], dk );
  407. c = xprod( constraint[1], cur );
  408. d = xprod( constraint[1], dk );
  409. /* find largest integer j such that a+j*b>=0 and c+j*d<=0. This
  410. * can be solved with integer arithmetic. */
  411. j = INFTY;
  412. if( b<0 )
  413. {
  414. j = floordiv( a, -b );
  415. }
  416. if( d>0 )
  417. {
  418. j = min( j, floordiv( -c, d ) );
  419. }
  420. pivk[i] = mod( k1 + j, n );
  421. foundk:
  422. ;
  423. } /* for i */
  424. /* clean up: for each i, let lon[i] be the largest k such that for
  425. * all i' with i<=i'<k, i'<k<=pivk[i']. */
  426. j = pivk[n - 1];
  427. pp->lon[n - 1] = j;
  428. for( i = n - 2; i>=0; i-- )
  429. {
  430. if( cyclic( i + 1, pivk[i], j ) )
  431. {
  432. j = pivk[i];
  433. }
  434. pp->lon[i] = j;
  435. }
  436. for( i = n - 1; cyclic( mod( i + 1, n ), j, pp->lon[i] ); i-- )
  437. {
  438. pp->lon[i] = j;
  439. }
  440. free( pivk );
  441. free( nc );
  442. return 0;
  443. malloc_error:
  444. free( pivk );
  445. free( nc );
  446. return 1;
  447. }
  448. /* ---------------------------------------------------------------------- */
  449. /* Stage 2: calculate the optimal polygon (Sec. 2.2.2-2.2.4). */
  450. /* Auxiliary function: calculate the penalty of an edge from i to j in
  451. * the given path. This needs the "lon" and "sum*" data. */
  452. static double penalty3( privpath_t* pp, int i, int j )
  453. {
  454. int n = pp->len;
  455. point_t* pt = pp->pt;
  456. sums_t* sums = pp->sums;
  457. /* assume 0<=i<j<=n */
  458. double x, y, x2, xy, y2;
  459. double k;
  460. double a, b, c, s;
  461. double px, py, ex, ey;
  462. int r = 0; /* rotations from i to j */
  463. if( j>=n )
  464. {
  465. j -= n;
  466. r += 1;
  467. }
  468. x = sums[j + 1].x - sums[i].x + r * sums[n].x;
  469. y = sums[j + 1].y - sums[i].y + r * sums[n].y;
  470. x2 = sums[j + 1].x2 - sums[i].x2 + r * sums[n].x2;
  471. xy = sums[j + 1].xy - sums[i].xy + r * sums[n].xy;
  472. y2 = sums[j + 1].y2 - sums[i].y2 + r * sums[n].y2;
  473. k = j + 1 - i + r * n;
  474. px = (pt[i].x + pt[j].x) / 2.0 - pt[0].x;
  475. py = (pt[i].y + pt[j].y) / 2.0 - pt[0].y;
  476. ey = (pt[j].x - pt[i].x);
  477. ex = -(pt[j].y - pt[i].y);
  478. a = ( (x2 - 2 * x * px) / k + px * px );
  479. b = ( (xy - x * py - y * px) / k + px * py );
  480. c = ( (y2 - 2 * y * py) / k + py * py );
  481. s = ex * ex * a + 2 * ex * ey * b + ey * ey * c;
  482. return sqrt( s );
  483. }
  484. /* find the optimal polygon. Fill in the m and po components. Return 1
  485. * on failure with errno set, else 0. Non-cyclic version: assumes i=0
  486. * is in the polygon. Fixme: ### implement cyclic version. */
  487. static int bestpolygon( privpath_t* pp )
  488. {
  489. int i, j, m, k;
  490. int n = pp->len;
  491. double* pen = NULL; /* pen[n+1]: penalty vector */
  492. int* prev = NULL; /* prev[n+1]: best path pointer vector */
  493. int* clip0 = NULL; /* clip0[n]: longest segment pointer, non-cyclic */
  494. int* clip1 = NULL; /* clip1[n+1]: backwards segment pointer, non-cyclic */
  495. int* seg0 = NULL; /* seg0[m+1]: forward segment bounds, m<=n */
  496. int* seg1 = NULL; /* seg1[m+1]: backward segment bounds, m<=n */
  497. double thispen;
  498. double best;
  499. int c;
  500. SAFE_MALLOC( pen, n + 1, double );
  501. SAFE_MALLOC( prev, n + 1, int );
  502. SAFE_MALLOC( clip0, n, int );
  503. SAFE_MALLOC( clip1, n + 1, int );
  504. SAFE_MALLOC( seg0, n + 1, int );
  505. SAFE_MALLOC( seg1, n + 1, int );
  506. /* calculate clipped paths */
  507. for( i = 0; i<n; i++ )
  508. {
  509. c = mod( pp->lon[mod( i - 1, n )] - 1, n );
  510. if( c == i )
  511. {
  512. c = mod( i + 1, n );
  513. }
  514. if( c < i )
  515. {
  516. clip0[i] = n;
  517. }
  518. else
  519. {
  520. clip0[i] = c;
  521. }
  522. }
  523. /* calculate backwards path clipping, non-cyclic. j <= clip0[i] iff
  524. * clip1[j] <= i, for i,j=0..n. */
  525. j = 1;
  526. for( i = 0; i<n; i++ )
  527. {
  528. while( j <= clip0[i] )
  529. {
  530. clip1[j] = i;
  531. j++;
  532. }
  533. }
  534. /* calculate seg0[j] = longest path from 0 with j segments */
  535. i = 0;
  536. for( j = 0; i<n; j++ )
  537. {
  538. seg0[j] = i;
  539. i = clip0[i];
  540. }
  541. seg0[j] = n;
  542. m = j;
  543. /* calculate seg1[j] = longest path to n with m-j segments */
  544. i = n;
  545. for( j = m; j>0; j-- )
  546. {
  547. seg1[j] = i;
  548. i = clip1[i];
  549. }
  550. seg1[0] = 0;
  551. /* now find the shortest path with m segments, based on penalty3 */
  552. /* note: the outer 2 loops jointly have at most n interations, thus
  553. * the worst-case behavior here is quadratic. In practice, it is
  554. * close to linear since the inner loop tends to be short. */
  555. pen[0] = 0;
  556. for( j = 1; j<=m; j++ )
  557. {
  558. for( i = seg1[j]; i<=seg0[j]; i++ )
  559. {
  560. best = -1;
  561. for( k = seg0[j - 1]; k>=clip1[i]; k-- )
  562. {
  563. thispen = penalty3( pp, k, i ) + pen[k];
  564. if( best < 0 || thispen < best )
  565. {
  566. prev[i] = k;
  567. best = thispen;
  568. }
  569. }
  570. pen[i] = best;
  571. }
  572. }
  573. pp->m = m;
  574. SAFE_MALLOC( pp->po, m, int );
  575. /* read off shortest path */
  576. for( i = n, j = m - 1; i>0; j-- )
  577. {
  578. i = prev[i];
  579. pp->po[j] = i;
  580. }
  581. free( pen );
  582. free( prev );
  583. free( clip0 );
  584. free( clip1 );
  585. free( seg0 );
  586. free( seg1 );
  587. return 0;
  588. malloc_error:
  589. free( pen );
  590. free( prev );
  591. free( clip0 );
  592. free( clip1 );
  593. free( seg0 );
  594. free( seg1 );
  595. return 1;
  596. }
  597. /* ---------------------------------------------------------------------- */
  598. /* Stage 3: vertex adjustment (Sec. 2.3.1). */
  599. /* Adjust vertices of optimal polygon: calculate the intersection of
  600. * the two "optimal" line segments, then move it into the unit square
  601. * if it lies outside. Return 1 with errno set on error; 0 on
  602. * success. */
  603. static int adjust_vertices( privpath_t* pp )
  604. {
  605. int m = pp->m;
  606. int* po = pp->po;
  607. int n = pp->len;
  608. point_t* pt = pp->pt;
  609. int x0 = pp->x0;
  610. int y0 = pp->y0;
  611. dpoint_t* ctr = NULL; /* ctr[m] */
  612. dpoint_t* dir = NULL; /* dir[m] */
  613. quadform_t* q = NULL; /* q[m] */
  614. double v[3];
  615. double d;
  616. int i, j, k, l;
  617. dpoint_t s;
  618. int r;
  619. SAFE_MALLOC( ctr, m, dpoint_t );
  620. SAFE_MALLOC( dir, m, dpoint_t );
  621. SAFE_MALLOC( q, m, quadform_t );
  622. r = privcurve_init( &pp->curve, m );
  623. if( r )
  624. {
  625. goto malloc_error;
  626. }
  627. /* calculate "optimal" point-slope representation for each line
  628. * segment */
  629. for( i = 0; i<m; i++ )
  630. {
  631. j = po[mod( i + 1, m )];
  632. j = mod( j - po[i], n ) + po[i];
  633. pointslope( pp, po[i], j, &ctr[i], &dir[i] );
  634. }
  635. /* represent each line segment as a singular quadratic form; the
  636. * distance of a point (x,y) from the line segment will be
  637. * (x,y,1)Q(x,y,1)^t, where Q=q[i]. */
  638. for( i = 0; i<m; i++ )
  639. {
  640. d = sq( dir[i].x ) + sq( dir[i].y );
  641. if( d == 0.0 )
  642. {
  643. for( j = 0; j<3; j++ )
  644. {
  645. for( k = 0; k<3; k++ )
  646. {
  647. q[i][j][k] = 0;
  648. }
  649. }
  650. }
  651. else
  652. {
  653. v[0] = dir[i].y;
  654. v[1] = -dir[i].x;
  655. v[2] = -v[1] *ctr[i].y - v[0] *ctr[i].x;
  656. for( l = 0; l<3; l++ )
  657. {
  658. for( k = 0; k<3; k++ )
  659. {
  660. q[i][l][k] = v[l] *v[k] / d;
  661. }
  662. }
  663. }
  664. }
  665. /* now calculate the "intersections" of consecutive segments.
  666. * Instead of using the actual intersection, we find the point
  667. * within a given unit square which minimizes the square distance to
  668. * the two lines. */
  669. for( i = 0; i<m; i++ )
  670. {
  671. quadform_t Q;
  672. dpoint_t w;
  673. double dx, dy;
  674. double det;
  675. double min, cand; /* minimum and candidate for minimum of quad. form */
  676. double xmin, ymin; /* coordinates of minimum */
  677. int z;
  678. /* let s be the vertex, in coordinates relative to x0/y0 */
  679. s.x = pt[po[i]].x - x0;
  680. s.y = pt[po[i]].y - y0;
  681. /* intersect segments i-1 and i */
  682. j = mod( i - 1, m );
  683. /* add quadratic forms */
  684. for( l = 0; l<3; l++ )
  685. {
  686. for( k = 0; k<3; k++ )
  687. {
  688. Q[l][k] = q[j][l][k] + q[i][l][k];
  689. }
  690. }
  691. while( 1 )
  692. {
  693. /* minimize the quadratic form Q on the unit square */
  694. /* find intersection */
  695. #ifdef HAVE_GCC_LOOP_BUG
  696. /* work around gcc bug #12243 */
  697. free( NULL );
  698. #endif
  699. det = Q[0][0] *Q[1][1] - Q[0][1] *Q[1][0];
  700. if( det != 0.0 )
  701. {
  702. w.x = (-Q[0][2] *Q[1][1] + Q[1][2] *Q[0][1]) / det;
  703. w.y = ( Q[0][2] *Q[1][0] - Q[1][2] *Q[0][0]) / det;
  704. break;
  705. }
  706. /* matrix is singular - lines are parallel. Add another,
  707. * orthogonal axis, through the center of the unit square */
  708. if( Q[0][0]>Q[1][1] )
  709. {
  710. v[0] = -Q[0][1];
  711. v[1] = Q[0][0];
  712. }
  713. else if( Q[1][1] )
  714. {
  715. v[0] = -Q[1][1];
  716. v[1] = Q[1][0];
  717. }
  718. else
  719. {
  720. v[0] = 1;
  721. v[1] = 0;
  722. }
  723. d = sq( v[0] ) + sq( v[1] );
  724. v[2] = -v[1] *s.y - v[0] *s.x;
  725. for( l = 0; l<3; l++ )
  726. {
  727. for( k = 0; k<3; k++ )
  728. {
  729. Q[l][k] += v[l] *v[k] / d;
  730. }
  731. }
  732. }
  733. dx = fabs( w.x - s.x );
  734. dy = fabs( w.y - s.y );
  735. if( dx <= .5 && dy <= .5 )
  736. {
  737. pp->curve.vertex[i].x = w.x + x0;
  738. pp->curve.vertex[i].y = w.y + y0;
  739. continue;
  740. }
  741. /* the minimum was not in the unit square; now minimize quadratic
  742. * on boundary of square */
  743. min = quadform( Q, s );
  744. xmin = s.x;
  745. ymin = s.y;
  746. if( Q[0][0] == 0.0 )
  747. {
  748. goto fixx;
  749. }
  750. for( z = 0; z<2; z++ ) /* value of the y-coordinate */
  751. {
  752. w.y = s.y - 0.5 + z;
  753. w.x = -(Q[0][1] *w.y + Q[0][2]) / Q[0][0];
  754. dx = fabs( w.x - s.x );
  755. cand = quadform( Q, w );
  756. if( dx <= .5 && cand < min )
  757. {
  758. min = cand;
  759. xmin = w.x;
  760. ymin = w.y;
  761. }
  762. }
  763. fixx:
  764. if( Q[1][1] == 0.0 )
  765. {
  766. goto corners;
  767. }
  768. for( z = 0; z<2; z++ ) /* value of the x-coordinate */
  769. {
  770. w.x = s.x - 0.5 + z;
  771. w.y = -(Q[1][0] *w.x + Q[1][2]) / Q[1][1];
  772. dy = fabs( w.y - s.y );
  773. cand = quadform( Q, w );
  774. if( dy <= .5 && cand < min )
  775. {
  776. min = cand;
  777. xmin = w.x;
  778. ymin = w.y;
  779. }
  780. }
  781. corners:
  782. /* check four corners */
  783. for( l = 0; l<2; l++ )
  784. {
  785. for( k = 0; k<2; k++ )
  786. {
  787. w.x = s.x - 0.5 + l;
  788. w.y = s.y - 0.5 + k;
  789. cand = quadform( Q, w );
  790. if( cand < min )
  791. {
  792. min = cand;
  793. xmin = w.x;
  794. ymin = w.y;
  795. }
  796. }
  797. }
  798. pp->curve.vertex[i].x = xmin + x0;
  799. pp->curve.vertex[i].y = ymin + y0;
  800. continue;
  801. }
  802. free( ctr );
  803. free( dir );
  804. free( q );
  805. return 0;
  806. malloc_error:
  807. free( ctr );
  808. free( dir );
  809. free( q );
  810. return 1;
  811. }
  812. /* ---------------------------------------------------------------------- */
  813. /* Stage 4: smoothing and corner analysis (Sec. 2.3.3) */
  814. /* Always succeeds and returns 0 */
  815. static int smooth( privcurve_t* curve, int sign, double alphamax )
  816. {
  817. int m = curve->n;
  818. int i, j, k;
  819. double dd, denom, alpha;
  820. dpoint_t p2, p3, p4;
  821. if( sign == '-' )
  822. {
  823. /* reverse orientation of negative paths */
  824. for( i = 0, j = m - 1; i<j; i++, j-- )
  825. {
  826. dpoint_t tmp;
  827. tmp = curve->vertex[i];
  828. curve->vertex[i] = curve->vertex[j];
  829. curve->vertex[j] = tmp;
  830. }
  831. }
  832. /* examine each vertex and find its best fit */
  833. for( i = 0; i<m; i++ )
  834. {
  835. j = mod( i + 1, m );
  836. k = mod( i + 2, m );
  837. p4 = interval( 1 / 2.0, curve->vertex[k], curve->vertex[j] );
  838. denom = ddenom( curve->vertex[i], curve->vertex[k] );
  839. if( denom != 0.0 )
  840. {
  841. dd = dpara( curve->vertex[i], curve->vertex[j], curve->vertex[k] ) / denom;
  842. dd = fabs( dd );
  843. alpha = dd>1 ? (1 - 1.0 / dd) : 0;
  844. alpha = alpha / 0.75;
  845. }
  846. else
  847. {
  848. alpha = 4 / 3.0;
  849. }
  850. curve->alpha0[j] = alpha; /* remember "original" value of alpha */
  851. if( alpha > alphamax ) /* pointed corner */
  852. {
  853. curve->tag[j] = POTRACE_CORNER;
  854. curve->c[j][1] = curve->vertex[j];
  855. curve->c[j][2] = p4;
  856. }
  857. else
  858. {
  859. if( alpha < 0.55 )
  860. {
  861. alpha = 0.55;
  862. }
  863. else if( alpha > 1 )
  864. {
  865. alpha = 1;
  866. }
  867. p2 = interval( .5 + .5 * alpha, curve->vertex[i], curve->vertex[j] );
  868. p3 = interval( .5 + .5 * alpha, curve->vertex[k], curve->vertex[j] );
  869. curve->tag[j] = POTRACE_CURVETO;
  870. curve->c[j][0] = p2;
  871. curve->c[j][1] = p3;
  872. curve->c[j][2] = p4;
  873. }
  874. curve->alpha[j] = alpha; /* store the "cropped" value of alpha */
  875. curve->beta[j] = 0.5;
  876. }
  877. curve->alphacurve = 1;
  878. return 0;
  879. }
  880. /* ---------------------------------------------------------------------- */
  881. /* Stage 5: Curve optimization (Sec. 2.4) */
  882. /* a private type for the result of opti_penalty */
  883. struct opti_s
  884. {
  885. double pen; /* penalty */
  886. dpoint_t c[2]; /* curve parameters */
  887. double t, s; /* curve parameters */
  888. double alpha; /* curve parameter */
  889. };
  890. typedef struct opti_s opti_t;
  891. /* calculate best fit from i+.5 to j+.5. Assume i<j (cyclically).
  892. * Return 0 and set badness and parameters (alpha, beta), if
  893. * possible. Return 1 if impossible. */
  894. static int opti_penalty( privpath_t* pp,
  895. int i,
  896. int j,
  897. opti_t* res,
  898. double opttolerance,
  899. int* convc,
  900. double* areac )
  901. {
  902. int m = pp->curve.n;
  903. int k, k1, k2, conv, i1;
  904. double area, alpha, d, d1, d2;
  905. dpoint_t p0, p1, p2, p3, pt;
  906. double A, R, A1, A2, A3, A4;
  907. double s, t;
  908. /* check convexity, corner-freeness, and maximum bend < 179 degrees */
  909. if( i==j ) /* sanity - a full loop can never be an opticurve */
  910. {
  911. return 1;
  912. }
  913. k = i;
  914. i1 = mod( i + 1, m );
  915. k1 = mod( k + 1, m );
  916. conv = convc[k1];
  917. if( conv == 0 )
  918. {
  919. return 1;
  920. }
  921. d = ddist( pp->curve.vertex[i], pp->curve.vertex[i1] );
  922. for( k = k1; k!=j; k = k1 )
  923. {
  924. k1 = mod( k + 1, m );
  925. k2 = mod( k + 2, m );
  926. if( convc[k1] != conv )
  927. {
  928. return 1;
  929. }
  930. if( sign( cprod( pp->curve.vertex[i], pp->curve.vertex[i1], pp->curve.vertex[k1],
  931. pp->curve.vertex[k2] ) ) != conv )
  932. {
  933. return 1;
  934. }
  935. if( iprod1( pp->curve.vertex[i], pp->curve.vertex[i1], pp->curve.vertex[k1],
  936. pp->curve.vertex[k2] ) < d *
  937. ddist( pp->curve.vertex[k1], pp->curve.vertex[k2] ) * COS179 )
  938. {
  939. return 1;
  940. }
  941. }
  942. /* the curve we're working in: */
  943. p0 = pp->curve.c[mod( i, m )][2];
  944. p1 = pp->curve.vertex[mod( i + 1, m )];
  945. p2 = pp->curve.vertex[mod( j, m )];
  946. p3 = pp->curve.c[mod( j, m )][2];
  947. /* determine its area */
  948. area = areac[j] - areac[i];
  949. area -= dpara( pp->curve.vertex[0], pp->curve.c[i][2], pp->curve.c[j][2] ) / 2;
  950. if( i>=j )
  951. {
  952. area += areac[m];
  953. }
  954. /* find intersection o of p0p1 and p2p3. Let t,s such that o =
  955. * interval(t,p0,p1) = interval(s,p3,p2). Let A be the area of the
  956. * triangle (p0,o,p3). */
  957. A1 = dpara( p0, p1, p2 );
  958. A2 = dpara( p0, p1, p3 );
  959. A3 = dpara( p0, p2, p3 );
  960. /* A4 = dpara(p1, p2, p3); */
  961. A4 = A1 + A3 - A2;
  962. if( A2 == A1 ) /* this should never happen */
  963. {
  964. return 1;
  965. }
  966. t = A3 / (A3 - A4);
  967. s = A2 / (A2 - A1);
  968. A = A2 * t / 2.0;
  969. if( A == 0.0 ) /* this should never happen */
  970. {
  971. return 1;
  972. }
  973. R = area / A; /* relative area */
  974. alpha = 2 - sqrt( 4 - R / 0.3 ); /* overall alpha for p0-o-p3 curve */
  975. res->c[0] = interval( t * alpha, p0, p1 );
  976. res->c[1] = interval( s * alpha, p3, p2 );
  977. res->alpha = alpha;
  978. res->t = t;
  979. res->s = s;
  980. p1 = res->c[0];
  981. p2 = res->c[1]; /* the proposed curve is now (p0,p1,p2,p3) */
  982. res->pen = 0;
  983. /* calculate penalty */
  984. /* check tangency with edges */
  985. for( k = mod( i + 1, m ); k!=j; k = k1 )
  986. {
  987. k1 = mod( k + 1, m );
  988. t = tangent( p0, p1, p2, p3, pp->curve.vertex[k], pp->curve.vertex[k1] );
  989. if( t<-.5 )
  990. {
  991. return 1;
  992. }
  993. pt = bezier( t, p0, p1, p2, p3 );
  994. d = ddist( pp->curve.vertex[k], pp->curve.vertex[k1] );
  995. if( d == 0.0 ) /* this should never happen */
  996. {
  997. return 1;
  998. }
  999. d1 = dpara( pp->curve.vertex[k], pp->curve.vertex[k1], pt ) / d;
  1000. if( fabs( d1 ) > opttolerance )
  1001. {
  1002. return 1;
  1003. }
  1004. if( iprod( pp->curve.vertex[k], pp->curve.vertex[k1],
  1005. pt ) < 0 || iprod( pp->curve.vertex[k1], pp->curve.vertex[k], pt ) < 0 )
  1006. {
  1007. return 1;
  1008. }
  1009. res->pen += sq( d1 );
  1010. }
  1011. /* check corners */
  1012. for( k = i; k!=j; k = k1 )
  1013. {
  1014. k1 = mod( k + 1, m );
  1015. t = tangent( p0, p1, p2, p3, pp->curve.c[k][2], pp->curve.c[k1][2] );
  1016. if( t<-.5 )
  1017. {
  1018. return 1;
  1019. }
  1020. pt = bezier( t, p0, p1, p2, p3 );
  1021. d = ddist( pp->curve.c[k][2], pp->curve.c[k1][2] );
  1022. if( d == 0.0 ) /* this should never happen */
  1023. {
  1024. return 1;
  1025. }
  1026. d1 = dpara( pp->curve.c[k][2], pp->curve.c[k1][2], pt ) / d;
  1027. d2 = dpara( pp->curve.c[k][2], pp->curve.c[k1][2], pp->curve.vertex[k1] ) / d;
  1028. d2 *= 0.75 * pp->curve.alpha[k1];
  1029. if( d2 < 0 )
  1030. {
  1031. d1 = -d1;
  1032. d2 = -d2;
  1033. }
  1034. if( d1 < d2 - opttolerance )
  1035. {
  1036. return 1;
  1037. }
  1038. if( d1 < d2 )
  1039. {
  1040. res->pen += sq( d1 - d2 );
  1041. }
  1042. }
  1043. return 0;
  1044. }
  1045. /* optimize the path p, replacing sequences of Bezier segments by a
  1046. * single segment when possible. Return 0 on success, 1 with errno set
  1047. * on failure. */
  1048. static int opticurve( privpath_t* pp, double opttolerance )
  1049. {
  1050. int m = pp->curve.n;
  1051. int* pt = NULL; /* pt[m+1] */
  1052. double* pen = NULL; /* pen[m+1] */
  1053. int* len = NULL; /* len[m+1] */
  1054. opti_t* opt = NULL; /* opt[m+1] */
  1055. int om;
  1056. int i, j, r;
  1057. opti_t o;
  1058. dpoint_t p0;
  1059. int i1;
  1060. double area;
  1061. double alpha;
  1062. double* s = NULL;
  1063. double* t = NULL;
  1064. int* convc = NULL; /* conv[m]: pre-computed convexities */
  1065. double* areac = NULL; /* cumarea[m+1]: cache for fast area computation */
  1066. SAFE_MALLOC( pt, m + 1, int );
  1067. SAFE_MALLOC( pen, m + 1, double );
  1068. SAFE_MALLOC( len, m + 1, int );
  1069. SAFE_MALLOC( opt, m + 1, opti_t );
  1070. SAFE_MALLOC( convc, m, int );
  1071. SAFE_MALLOC( areac, m + 1, double );
  1072. /* pre-calculate convexity: +1 = right turn, -1 = left turn, 0 = corner */
  1073. for( i = 0; i<m; i++ )
  1074. {
  1075. if( pp->curve.tag[i] == POTRACE_CURVETO )
  1076. {
  1077. convc[i] =
  1078. sign( dpara( pp->curve.vertex[mod( i - 1,
  1079. m )], pp->curve.vertex[i],
  1080. pp->curve.vertex[mod( i + 1, m )] ) );
  1081. }
  1082. else
  1083. {
  1084. convc[i] = 0;
  1085. }
  1086. }
  1087. /* pre-calculate areas */
  1088. area = 0.0;
  1089. areac[0] = 0.0;
  1090. p0 = pp->curve.vertex[0];
  1091. for( i = 0; i<m; i++ )
  1092. {
  1093. i1 = mod( i + 1, m );
  1094. if( pp->curve.tag[i1] == POTRACE_CURVETO )
  1095. {
  1096. alpha = pp->curve.alpha[i1];
  1097. area += 0.3 * alpha * (4 - alpha) * dpara( pp->curve.c[i][2],
  1098. pp->curve.vertex[i1],
  1099. pp->curve.c[i1][2] ) / 2;
  1100. area += dpara( p0, pp->curve.c[i][2], pp->curve.c[i1][2] ) / 2;
  1101. }
  1102. areac[i + 1] = area;
  1103. }
  1104. pt[0] = -1;
  1105. pen[0] = 0;
  1106. len[0] = 0;
  1107. /* Fixme: we always start from a fixed point -- should find the best
  1108. * curve cyclically ### */
  1109. for( j = 1; j<=m; j++ )
  1110. {
  1111. /* calculate best path from 0 to j */
  1112. pt[j] = j - 1;
  1113. pen[j] = pen[j - 1];
  1114. len[j] = len[j - 1] + 1;
  1115. for( i = j - 2; i>=0; i-- )
  1116. {
  1117. r = opti_penalty( pp, i, mod( j, m ), &o, opttolerance, convc, areac );
  1118. if( r )
  1119. {
  1120. break;
  1121. }
  1122. if( len[j] > len[i] + 1 || (len[j] == len[i] + 1 && pen[j] > pen[i] + o.pen) )
  1123. {
  1124. pt[j] = i;
  1125. pen[j] = pen[i] + o.pen;
  1126. len[j] = len[i] + 1;
  1127. opt[j] = o;
  1128. }
  1129. }
  1130. }
  1131. om = len[m];
  1132. r = privcurve_init( &pp->ocurve, om );
  1133. if( r )
  1134. {
  1135. goto malloc_error;
  1136. }
  1137. SAFE_MALLOC( s, om, double );
  1138. SAFE_MALLOC( t, om, double );
  1139. j = m;
  1140. for( i = om - 1; i>=0; i-- )
  1141. {
  1142. if( pt[j]==j - 1 )
  1143. {
  1144. pp->ocurve.tag[i] = pp->curve.tag[mod( j, m )];
  1145. pp->ocurve.c[i][0] = pp->curve.c[mod( j, m )][0];
  1146. pp->ocurve.c[i][1] = pp->curve.c[mod( j, m )][1];
  1147. pp->ocurve.c[i][2] = pp->curve.c[mod( j, m )][2];
  1148. pp->ocurve.vertex[i] = pp->curve.vertex[mod( j, m )];
  1149. pp->ocurve.alpha[i] = pp->curve.alpha[mod( j, m )];
  1150. pp->ocurve.alpha0[i] = pp->curve.alpha0[mod( j, m )];
  1151. pp->ocurve.beta[i] = pp->curve.beta[mod( j, m )];
  1152. s[i] = t[i] = 1.0;
  1153. }
  1154. else
  1155. {
  1156. pp->ocurve.tag[i] = POTRACE_CURVETO;
  1157. pp->ocurve.c[i][0] = opt[j].c[0];
  1158. pp->ocurve.c[i][1] = opt[j].c[1];
  1159. pp->ocurve.c[i][2] = pp->curve.c[mod( j, m )][2];
  1160. pp->ocurve.vertex[i] = interval( opt[j].s, pp->curve.c[mod( j,
  1161. m )][2],
  1162. pp->curve.vertex[mod( j, m )] );
  1163. pp->ocurve.alpha[i] = opt[j].alpha;
  1164. pp->ocurve.alpha0[i] = opt[j].alpha;
  1165. s[i] = opt[j].s;
  1166. t[i] = opt[j].t;
  1167. }
  1168. j = pt[j];
  1169. }
  1170. /* calculate beta parameters */
  1171. for( i = 0; i<om; i++ )
  1172. {
  1173. i1 = mod( i + 1, om );
  1174. pp->ocurve.beta[i] = s[i] / (s[i] + t[i1]);
  1175. }
  1176. pp->ocurve.alphacurve = 1;
  1177. free( pt );
  1178. free( pen );
  1179. free( len );
  1180. free( opt );
  1181. free( s );
  1182. free( t );
  1183. free( convc );
  1184. free( areac );
  1185. return 0;
  1186. malloc_error:
  1187. free( pt );
  1188. free( pen );
  1189. free( len );
  1190. free( opt );
  1191. free( s );
  1192. free( t );
  1193. free( convc );
  1194. free( areac );
  1195. return 1;
  1196. }
  1197. /* ---------------------------------------------------------------------- */
  1198. #define TRY( x ) if( x ) \
  1199. goto try_error
  1200. /* return 0 on success, 1 on error with errno set. */
  1201. int process_path( path_t* plist, const potrace_param_t* param, progress_t* progress )
  1202. {
  1203. path_t* p;
  1204. double nn = 0, cn = 0;
  1205. if( progress->callback )
  1206. {
  1207. /* precompute task size for progress estimates */
  1208. nn = 0;
  1209. list_forall( p, plist ) {
  1210. nn += p->priv->len;
  1211. }
  1212. cn = 0;
  1213. }
  1214. /* call downstream function with each path */
  1215. list_forall( p, plist ) {
  1216. TRY( calc_sums( p->priv ) );
  1217. TRY( calc_lon( p->priv ) );
  1218. TRY( bestpolygon( p->priv ) );
  1219. TRY( adjust_vertices( p->priv ) );
  1220. TRY( smooth( &p->priv->curve, p->sign, param->alphamax ) );
  1221. if( param->opticurve )
  1222. {
  1223. TRY( opticurve( p->priv, param->opttolerance ) );
  1224. p->priv->fcurve = &p->priv->ocurve;
  1225. }
  1226. else
  1227. {
  1228. p->priv->fcurve = &p->priv->curve;
  1229. }
  1230. privcurve_to_curve( p->priv->fcurve, &p->curve );
  1231. if( progress->callback )
  1232. {
  1233. cn += p->priv->len;
  1234. progress_update( cn / nn, progress );
  1235. }
  1236. }
  1237. progress_update( 1.0, progress );
  1238. return 0;
  1239. try_error:
  1240. return 1;
  1241. }