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.

302 lines
8.0 KiB

  1. /* Copyright (C) 2001-2015 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. #ifdef HAVE_CONFIG_H
  5. #include <config.h>
  6. #endif
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <string.h>
  11. #include "render.h"
  12. #include "greymap.h"
  13. #include "auxiliary.h"
  14. /* ---------------------------------------------------------------------- */
  15. /* routines for anti-aliased rendering of curves */
  16. /* we use the following method. Given a point (x,y) (with real-valued
  17. * coordinates) in the plane, let (xi,yi) be the integer part of the
  18. * coordinates, i.e., xi=floor(x), yi=floor(y). Define a path from
  19. * (x,y) to infinity as follows: path(x,y) =
  20. * (x,y)--(xi+1,y)--(xi+1,yi)--(+infty,yi). Now as the point (x,y)
  21. * moves smoothly across the plane, the path path(x,y) sweeps
  22. * (non-smoothly) across a certain area. We proportionately blacken
  23. * the area as the path moves "downward", and we whiten the area as
  24. * the path moves "upward". This way, after the point has traversed a
  25. * closed curve, the interior of the curve has been darkened
  26. * (counterclockwise movement) or lightened (clockwise movement). (The
  27. * "grey shift" is actually proportional to the winding number). By
  28. * choosing the above path with mostly integer coordinates, we achieve
  29. * that only pixels close to (x,y) receive grey values and are subject
  30. * to round-off errors. The grey value of pixels far away from (x,y)
  31. * is always in "integer" (where 0=black, 1=white). As a special
  32. * trick, we keep an accumulator rm->a1, which holds a double value to
  33. * be added to the grey value to be added to the current pixel
  34. * (xi,yi). Only when changing "current" pixels, we convert this
  35. * double value to an integer. This way we avoid round-off errors at
  36. * the meeting points of line segments. Another speedup measure is
  37. * that we sometimes use the rm->incrow_buf array to postpone
  38. * incrementing or decrementing an entire row. If incrow_buf[y]=x+1!=0,
  39. * then all the pixels (x,y),(x+1,y),(x+2,y),... are scheduled to be
  40. * incremented/decremented (which one is the case will be clear from
  41. * context). This keeps the greymap operations reasonably local. */
  42. /* allocate a new rendering state */
  43. render_t* render_new( greymap_t* gm )
  44. {
  45. render_t* rm;
  46. rm = (render_t*) malloc( sizeof(render_t) );
  47. if( !rm )
  48. {
  49. return NULL;
  50. }
  51. memset( rm, 0, sizeof(render_t) );
  52. rm->gm = gm;
  53. rm->incrow_buf = (int*) calloc( gm->h, sizeof(int) );
  54. if( !rm->incrow_buf )
  55. {
  56. free( rm );
  57. return NULL;
  58. }
  59. memset( rm->incrow_buf, 0, gm->h * sizeof(int) );
  60. return rm;
  61. }
  62. /* free a given rendering state. Note: this does not free the
  63. * underlying greymap. */
  64. void render_free( render_t* rm )
  65. {
  66. free( rm->incrow_buf );
  67. free( rm );
  68. }
  69. /* close path */
  70. void render_close( render_t* rm )
  71. {
  72. if( rm->x0 != rm->x1 || rm->y0 != rm->y1 )
  73. {
  74. render_lineto( rm, rm->x0, rm->y0 );
  75. }
  76. GM_INC( rm->gm, rm->x0i, rm->y0i, (rm->a0 + rm->a1) * 255 );
  77. /* assert (rm->x0i != rm->x1i || rm->y0i != rm->y1i); */
  78. /* the persistent state is now undefined */
  79. }
  80. /* move point */
  81. void render_moveto( render_t* rm, double x, double y )
  82. {
  83. /* close the previous path */
  84. render_close( rm );
  85. rm->x0 = rm->x1 = x;
  86. rm->y0 = rm->y1 = y;
  87. rm->x0i = (int) floor( rm->x0 );
  88. rm->x1i = (int) floor( rm->x1 );
  89. rm->y0i = (int) floor( rm->y0 );
  90. rm->y1i = (int) floor( rm->y1 );
  91. rm->a0 = rm->a1 = 0;
  92. }
  93. /* add b to pixels (x,y) and all pixels to the right of it. However,
  94. * use rm->incrow_buf as a buffer to economize on multiple calls */
  95. static void incrow( render_t* rm, int x, int y, int b )
  96. {
  97. int i, x0;
  98. if( y < 0 || y >= rm->gm->h )
  99. {
  100. return;
  101. }
  102. if( x < 0 )
  103. {
  104. x = 0;
  105. }
  106. else if( x > rm->gm->w )
  107. {
  108. x = rm->gm->w;
  109. }
  110. if( rm->incrow_buf[y] == 0 )
  111. {
  112. rm->incrow_buf[y] = x + 1; /* store x+1 so that we can use 0 for "vacant" */
  113. return;
  114. }
  115. x0 = rm->incrow_buf[y] - 1;
  116. rm->incrow_buf[y] = 0;
  117. if( x0 < x )
  118. {
  119. for( i = x0; i<x; i++ )
  120. {
  121. GM_INC( rm->gm, i, y, -b );
  122. }
  123. }
  124. else
  125. {
  126. for( i = x; i<x0; i++ )
  127. {
  128. GM_INC( rm->gm, i, y, b );
  129. }
  130. }
  131. }
  132. /* render a straight line */
  133. void render_lineto( render_t* rm, double x2, double y2 )
  134. {
  135. int x2i, y2i;
  136. double t0 = 2, s0 = 2;
  137. int sn, tn;
  138. double ss = 2, ts = 2;
  139. double r0, r1;
  140. int i, j;
  141. int rxi, ryi;
  142. int s;
  143. x2i = (int) floor( x2 );
  144. y2i = (int) floor( y2 );
  145. sn = abs( x2i - rm->x1i );
  146. tn = abs( y2i - rm->y1i );
  147. if( sn )
  148. {
  149. s0 = ( (x2>rm->x1 ? rm->x1i + 1 : rm->x1i) - rm->x1 ) / (x2 - rm->x1);
  150. ss = fabs( 1.0 / (x2 - rm->x1) );
  151. }
  152. if( tn )
  153. {
  154. t0 = ( (y2>rm->y1 ? rm->y1i + 1 : rm->y1i) - rm->y1 ) / (y2 - rm->y1);
  155. ts = fabs( 1.0 / (y2 - rm->y1) );
  156. }
  157. r0 = 0;
  158. i = 0;
  159. j = 0;
  160. rxi = rm->x1i;
  161. ryi = rm->y1i;
  162. while( i<sn || j<tn )
  163. {
  164. if( j>=tn || (i<sn && s0 + i * ss < t0 + j * ts) )
  165. {
  166. r1 = s0 + i * ss;
  167. i++;
  168. s = 1;
  169. }
  170. else
  171. {
  172. r1 = t0 + j * ts;
  173. j++;
  174. s = 0;
  175. }
  176. /* render line from r0 to r1 segment of (rm->x1,rm->y1)..(x2,y2) */
  177. /* move point to r1 */
  178. rm->a1 += (r1 - r0) * (y2 - rm->y1) *
  179. ( rxi + 1 - ( (r0 + r1) / 2.0 * (x2 - rm->x1) + rm->x1 ) );
  180. /* move point across pixel boundary */
  181. if( s && x2>rm->x1 )
  182. {
  183. GM_INC( rm->gm, rxi, ryi, rm->a1 * 255 );
  184. rm->a1 = 0;
  185. rxi++;
  186. rm->a1 += rm->y1 + r1 * (y2 - rm->y1) - ryi;
  187. }
  188. else if( !s && y2>rm->y1 )
  189. {
  190. GM_INC( rm->gm, rxi, ryi, rm->a1 * 255 );
  191. rm->a1 = 0;
  192. incrow( rm, rxi + 1, ryi, 255 );
  193. ryi++;
  194. }
  195. else if( s && x2<=rm->x1 )
  196. {
  197. rm->a1 -= rm->y1 + r1 * (y2 - rm->y1) - ryi;
  198. GM_INC( rm->gm, rxi, ryi, rm->a1 * 255 );
  199. rm->a1 = 0;
  200. rxi--;
  201. }
  202. else if( !s && y2<=rm->y1 )
  203. {
  204. GM_INC( rm->gm, rxi, ryi, rm->a1 * 255 );
  205. rm->a1 = 0;
  206. ryi--;
  207. incrow( rm, rxi + 1, ryi, -255 );
  208. }
  209. r0 = r1;
  210. }
  211. /* move point to (x2,y2) */
  212. r1 = 1;
  213. rm->a1 += (r1 - r0) * (y2 - rm->y1) *
  214. ( rxi + 1 - ( (r0 + r1) / 2.0 * (x2 - rm->x1) + rm->x1 ) );
  215. rm->x1i = x2i;
  216. rm->y1i = y2i;
  217. rm->x1 = x2;
  218. rm->y1 = y2;
  219. /* assert (rxi != rm->x1i || ryi != rm->y1i); */
  220. }
  221. /* render a Bezier curve. */
  222. void render_curveto( render_t* rm, double x2, double y2, double x3, double y3, double x4,
  223. double y4 )
  224. {
  225. double x1, y1, dd0, dd1, dd, delta, e2, epsilon, t;
  226. x1 = rm->x1; /* starting point */
  227. y1 = rm->y1;
  228. /* we approximate the curve by small line segments. The interval
  229. * size, epsilon, is determined on the fly so that the distance
  230. * between the true curve and its approximation does not exceed the
  231. * desired accuracy delta. */
  232. delta = .1; /* desired accuracy, in pixels */
  233. /* let dd = maximal value of 2nd derivative over curve - this must
  234. * occur at an endpoint. */
  235. dd0 = sq( x1 - 2 * x2 + x3 ) + sq( y1 - 2 * y2 + y3 );
  236. dd1 = sq( x2 - 2 * x3 + x4 ) + sq( y2 - 2 * y3 + y4 );
  237. dd = 6 * sqrt( max( dd0, dd1 ) );
  238. e2 = 8 * delta <= dd ? 8 * delta / dd : 1;
  239. epsilon = sqrt( e2 ); /* necessary interval size */
  240. for( t = epsilon; t<1; t += epsilon )
  241. {
  242. render_lineto( rm, x1 * cu( 1 - t ) + 3 * x2 * sq( 1 - t ) * t + 3 * x3 * (1 - t) * sq(
  243. t ) + x4 * cu( t ),
  244. y1 * cu( 1 - t ) + 3 * y2 * sq( 1 - t ) * t + 3 * y3 * (1 - t) * sq( t ) + y4 *
  245. cu( t ) );
  246. }
  247. render_lineto( rm, x4, y4 );
  248. }