decompose.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* Copyright (C) 2001-2017 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 <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <limits.h>
  12. #include "potracelib.h"
  13. #include "curve.h"
  14. #include "lists.h"
  15. #include "bitmap.h"
  16. #include "decompose.h"
  17. #include "progress.h"
  18. /* ---------------------------------------------------------------------- */
  19. /* deterministically and efficiently hash (x,y) into a pseudo-random bit */
  20. static inline int detrand(int x, int y) {
  21. unsigned int z;
  22. static const unsigned char t[256] = {
  23. /* non-linear sequence: constant term of inverse in GF(8),
  24. mod x^8+x^4+x^3+x+1 */
  25. 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1,
  26. 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0,
  27. 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  28. 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1,
  29. 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0,
  30. 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0,
  31. 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0,
  32. 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
  33. 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
  34. 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1,
  35. 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
  36. };
  37. /* 0x04b3e375 and 0x05a8ef93 are chosen to contain every possible
  38. 5-bit sequence */
  39. z = ((0x04b3e375 * x) ^ y) * 0x05a8ef93;
  40. z = t[z & 0xff] ^ t[(z>>8) & 0xff] ^ t[(z>>16) & 0xff] ^ t[(z>>24) & 0xff];
  41. return z;
  42. }
  43. /* ---------------------------------------------------------------------- */
  44. /* auxiliary bitmap manipulations */
  45. /* set the excess padding to 0 */
  46. static void bm_clearexcess(potrace_bitmap_t *bm) {
  47. potrace_word mask;
  48. int y;
  49. if (bm->w % BM_WORDBITS != 0) {
  50. mask = BM_ALLBITS << (BM_WORDBITS - (bm->w % BM_WORDBITS));
  51. for (y=0; y<bm->h; y++) {
  52. *bm_index(bm, bm->w, y) &= mask;
  53. }
  54. }
  55. }
  56. struct bbox_s {
  57. int x0, x1, y0, y1; /* bounding box */
  58. };
  59. typedef struct bbox_s bbox_t;
  60. /* clear the bm, assuming the bounding box is set correctly (faster
  61. than clearing the whole bitmap) */
  62. static void clear_bm_with_bbox(potrace_bitmap_t *bm, bbox_t *bbox) {
  63. int imin = (bbox->x0 / BM_WORDBITS);
  64. int imax = ((bbox->x1 + BM_WORDBITS-1) / BM_WORDBITS);
  65. int i, y;
  66. for (y=bbox->y0; y<bbox->y1; y++) {
  67. for (i=imin; i<imax; i++) {
  68. bm_scanline(bm, y)[i] = 0;
  69. }
  70. }
  71. }
  72. /* ---------------------------------------------------------------------- */
  73. /* auxiliary functions */
  74. /* return the "majority" value of bitmap bm at intersection (x,y). We
  75. assume that the bitmap is balanced at "radius" 1. */
  76. static int majority(potrace_bitmap_t *bm, int x, int y) {
  77. int i, a, ct;
  78. for (i=2; i<5; i++) { /* check at "radius" i */
  79. ct = 0;
  80. for (a=-i+1; a<=i-1; a++) {
  81. ct += BM_GET(bm, x+a, y+i-1) ? 1 : -1;
  82. ct += BM_GET(bm, x+i-1, y+a-1) ? 1 : -1;
  83. ct += BM_GET(bm, x+a-1, y-i) ? 1 : -1;
  84. ct += BM_GET(bm, x-i, y+a) ? 1 : -1;
  85. }
  86. if (ct>0) {
  87. return 1;
  88. } else if (ct<0) {
  89. return 0;
  90. }
  91. }
  92. return 0;
  93. }
  94. /* ---------------------------------------------------------------------- */
  95. /* decompose image into paths */
  96. /* efficiently invert bits [x,infty) and [xa,infty) in line y. Here xa
  97. must be a multiple of BM_WORDBITS. */
  98. static void xor_to_ref(potrace_bitmap_t *bm, int x, int y, int xa) {
  99. int xhi = x & -BM_WORDBITS;
  100. int xlo = x & (BM_WORDBITS-1); /* = x % BM_WORDBITS */
  101. int i;
  102. if (xhi<xa) {
  103. for (i = xhi; i < xa; i+=BM_WORDBITS) {
  104. *bm_index(bm, i, y) ^= BM_ALLBITS;
  105. }
  106. } else {
  107. for (i = xa; i < xhi; i+=BM_WORDBITS) {
  108. *bm_index(bm, i, y) ^= BM_ALLBITS;
  109. }
  110. }
  111. /* note: the following "if" is needed because x86 treats a<<b as
  112. a<<(b&31). I spent hours looking for this bug. */
  113. if (xlo) {
  114. *bm_index(bm, xhi, y) ^= (BM_ALLBITS << (BM_WORDBITS - xlo));
  115. }
  116. }
  117. /* a path is represented as an array of points, which are thought to
  118. lie on the corners of pixels (not on their centers). The path point
  119. (x,y) is the lower left corner of the pixel (x,y). Paths are
  120. represented by the len/pt components of a path_t object (which
  121. also stores other information about the path) */
  122. /* xor the given pixmap with the interior of the given path. Note: the
  123. path must be within the dimensions of the pixmap. */
  124. static void xor_path(potrace_bitmap_t *bm, path_t *p) {
  125. int xa, x, y, k, y1;
  126. if (p->priv->len <= 0) { /* a path of length 0 is silly, but legal */
  127. return;
  128. }
  129. y1 = p->priv->pt[p->priv->len-1].y;
  130. xa = p->priv->pt[0].x & -BM_WORDBITS;
  131. for (k=0; k<p->priv->len; k++) {
  132. x = p->priv->pt[k].x;
  133. y = p->priv->pt[k].y;
  134. if (y != y1) {
  135. /* efficiently invert the rectangle [x,xa] x [y,y1] */
  136. xor_to_ref(bm, x, min(y,y1), xa);
  137. y1 = y;
  138. }
  139. }
  140. }
  141. /* Find the bounding box of a given path. Path is assumed to be of
  142. non-zero length. */
  143. static void setbbox_path(bbox_t *bbox, path_t *p) {
  144. int x, y;
  145. int k;
  146. bbox->y0 = INT_MAX;
  147. bbox->y1 = 0;
  148. bbox->x0 = INT_MAX;
  149. bbox->x1 = 0;
  150. for (k=0; k<p->priv->len; k++) {
  151. x = p->priv->pt[k].x;
  152. y = p->priv->pt[k].y;
  153. if (x < bbox->x0) {
  154. bbox->x0 = x;
  155. }
  156. if (x > bbox->x1) {
  157. bbox->x1 = x;
  158. }
  159. if (y < bbox->y0) {
  160. bbox->y0 = y;
  161. }
  162. if (y > bbox->y1) {
  163. bbox->y1 = y;
  164. }
  165. }
  166. }
  167. /* compute a path in the given pixmap, separating black from white.
  168. Start path at the point (x0,x1), which must be an upper left corner
  169. of the path. Also compute the area enclosed by the path. Return a
  170. new path_t object, or NULL on error (note that a legitimate path
  171. cannot have length 0). Sign is required for correct interpretation
  172. of turnpolicies. */
  173. static path_t *findpath(potrace_bitmap_t *bm, int x0, int y0, int sign, int turnpolicy) {
  174. int x, y, dirx, diry, len, size;
  175. uint64_t area;
  176. int c, d, tmp;
  177. point_t *pt, *pt1;
  178. path_t *p = NULL;
  179. x = x0;
  180. y = y0;
  181. dirx = 0;
  182. diry = -1;
  183. len = size = 0;
  184. pt = NULL;
  185. area = 0;
  186. while (1) {
  187. /* add point to path */
  188. if (len>=size) {
  189. size += 100;
  190. size = (int)(1.3 * size);
  191. pt1 = (point_t *)realloc(pt, size * sizeof(point_t));
  192. if (!pt1) {
  193. goto error;
  194. }
  195. pt = pt1;
  196. }
  197. pt[len].x = x;
  198. pt[len].y = y;
  199. len++;
  200. /* move to next point */
  201. x += dirx;
  202. y += diry;
  203. area += x*diry;
  204. /* path complete? */
  205. if (x==x0 && y==y0) {
  206. break;
  207. }
  208. /* determine next direction */
  209. c = BM_GET(bm, x + (dirx+diry-1)/2, y + (diry-dirx-1)/2);
  210. d = BM_GET(bm, x + (dirx-diry-1)/2, y + (diry+dirx-1)/2);
  211. if (c && !d) { /* ambiguous turn */
  212. if (turnpolicy == POTRACE_TURNPOLICY_RIGHT
  213. || (turnpolicy == POTRACE_TURNPOLICY_BLACK && sign == '+')
  214. || (turnpolicy == POTRACE_TURNPOLICY_WHITE && sign == '-')
  215. || (turnpolicy == POTRACE_TURNPOLICY_RANDOM && detrand(x,y))
  216. || (turnpolicy == POTRACE_TURNPOLICY_MAJORITY && majority(bm, x, y))
  217. || (turnpolicy == POTRACE_TURNPOLICY_MINORITY && !majority(bm, x, y))) {
  218. tmp = dirx; /* right turn */
  219. dirx = diry;
  220. diry = -tmp;
  221. } else {
  222. tmp = dirx; /* left turn */
  223. dirx = -diry;
  224. diry = tmp;
  225. }
  226. } else if (c) { /* right turn */
  227. tmp = dirx;
  228. dirx = diry;
  229. diry = -tmp;
  230. } else if (!d) { /* left turn */
  231. tmp = dirx;
  232. dirx = -diry;
  233. diry = tmp;
  234. }
  235. } /* while this path */
  236. /* allocate new path object */
  237. p = path_new();
  238. if (!p) {
  239. goto error;
  240. }
  241. p->priv->pt = pt;
  242. p->priv->len = len;
  243. p->area = area <= INT_MAX ? area : INT_MAX; /* avoid overflow */
  244. p->sign = sign;
  245. return p;
  246. error:
  247. free(pt);
  248. return NULL;
  249. }
  250. /* Give a tree structure to the given path list, based on "insideness"
  251. testing. I.e., path A is considered "below" path B if it is inside
  252. path B. The input pathlist is assumed to be ordered so that "outer"
  253. paths occur before "inner" paths. The tree structure is stored in
  254. the "childlist" and "sibling" components of the path_t
  255. structure. The linked list structure is also changed so that
  256. negative path components are listed immediately after their
  257. positive parent. Note: some backends may ignore the tree
  258. structure, others may use it e.g. to group path components. We
  259. assume that in the input, point 0 of each path is an "upper left"
  260. corner of the path, as returned by bm_to_pathlist. This makes it
  261. easy to find an "interior" point. The bm argument should be a
  262. bitmap of the correct size (large enough to hold all the paths),
  263. and will be used as scratch space. Return 0 on success or -1 on
  264. error with errno set. */
  265. static void pathlist_to_tree(path_t *plist, potrace_bitmap_t *bm) {
  266. path_t *p, *p1;
  267. path_t *heap, *heap1;
  268. path_t *cur;
  269. path_t *head;
  270. path_t **plist_hook; /* for fast appending to linked list */
  271. path_t **hook_in, **hook_out; /* for fast appending to linked list */
  272. bbox_t bbox;
  273. bm_clear(bm, 0);
  274. /* save original "next" pointers */
  275. list_forall(p, plist) {
  276. p->sibling = p->next;
  277. p->childlist = NULL;
  278. }
  279. heap = plist;
  280. /* the heap holds a list of lists of paths. Use "childlist" field
  281. for outer list, "next" field for inner list. Each of the sublists
  282. is to be turned into a tree. This code is messy, but it is
  283. actually fast. Each path is rendered exactly once. We use the
  284. heap to get a tail recursive algorithm: the heap holds a list of
  285. pathlists which still need to be transformed. */
  286. while (heap) {
  287. /* unlink first sublist */
  288. cur = heap;
  289. heap = heap->childlist;
  290. cur->childlist = NULL;
  291. /* unlink first path */
  292. head = cur;
  293. cur = cur->next;
  294. head->next = NULL;
  295. /* render path */
  296. xor_path(bm, head);
  297. setbbox_path(&bbox, head);
  298. /* now do insideness test for each element of cur; append it to
  299. head->childlist if it's inside head, else append it to
  300. head->next. */
  301. hook_in=&head->childlist;
  302. hook_out=&head->next;
  303. list_forall_unlink(p, cur) {
  304. if (p->priv->pt[0].y <= bbox.y0) {
  305. list_insert_beforehook(p, hook_out);
  306. /* append the remainder of the list to hook_out */
  307. *hook_out = cur;
  308. break;
  309. }
  310. if (BM_GET(bm, p->priv->pt[0].x, p->priv->pt[0].y-1)) {
  311. list_insert_beforehook(p, hook_in);
  312. } else {
  313. list_insert_beforehook(p, hook_out);
  314. }
  315. }
  316. /* clear bm */
  317. clear_bm_with_bbox(bm, &bbox);
  318. /* now schedule head->childlist and head->next for further
  319. processing */
  320. if (head->next) {
  321. head->next->childlist = heap;
  322. heap = head->next;
  323. }
  324. if (head->childlist) {
  325. head->childlist->childlist = heap;
  326. heap = head->childlist;
  327. }
  328. }
  329. /* copy sibling structure from "next" to "sibling" component */
  330. p = plist;
  331. while (p) {
  332. p1 = p->sibling;
  333. p->sibling = p->next;
  334. p = p1;
  335. }
  336. /* reconstruct a new linked list ("next") structure from tree
  337. ("childlist", "sibling") structure. This code is slightly messy,
  338. because we use a heap to make it tail recursive: the heap
  339. contains a list of childlists which still need to be
  340. processed. */
  341. heap = plist;
  342. if (heap) {
  343. heap->next = NULL; /* heap is a linked list of childlists */
  344. }
  345. plist = NULL;
  346. plist_hook = &plist;
  347. while (heap) {
  348. heap1 = heap->next;
  349. for (p=heap; p; p=p->sibling) {
  350. /* p is a positive path */
  351. /* append to linked list */
  352. list_insert_beforehook(p, plist_hook);
  353. /* go through its children */
  354. for (p1=p->childlist; p1; p1=p1->sibling) {
  355. /* append to linked list */
  356. list_insert_beforehook(p1, plist_hook);
  357. /* append its childlist to heap, if non-empty */
  358. if (p1->childlist) {
  359. list_append(path_t, heap1, p1->childlist);
  360. }
  361. }
  362. }
  363. heap = heap1;
  364. }
  365. return;
  366. }
  367. /* find the next set pixel in a row <= y. Pixels are searched first
  368. left-to-right, then top-down. In other words, (x,y)<(x',y') if y>y'
  369. or y=y' and x<x'. If found, return 0 and store pixel in
  370. (*xp,*yp). Else return 1. Note that this function assumes that
  371. excess bytes have been cleared with bm_clearexcess. */
  372. static int findnext(potrace_bitmap_t *bm, int *xp, int *yp) {
  373. int x;
  374. int y;
  375. int x0;
  376. x0 = (*xp) & ~(BM_WORDBITS-1);
  377. for (y=*yp; y>=0; y--) {
  378. for (x=x0; x<bm->w && x>=0; x+=(unsigned)BM_WORDBITS) {
  379. if (*bm_index(bm, x, y)) {
  380. while (!BM_GET(bm, x, y)) {
  381. x++;
  382. }
  383. /* found */
  384. *xp = x;
  385. *yp = y;
  386. return 0;
  387. }
  388. }
  389. x0 = 0;
  390. }
  391. /* not found */
  392. return 1;
  393. }
  394. /* Decompose the given bitmap into paths. Returns a linked list of
  395. path_t objects with the fields len, pt, area, sign filled
  396. in. Returns 0 on success with plistp set, or -1 on error with errno
  397. set. */
  398. int bm_to_pathlist(const potrace_bitmap_t *bm, path_t **plistp, const potrace_param_t *param, progress_t *progress) {
  399. int x;
  400. int y;
  401. path_t *p;
  402. path_t *plist = NULL; /* linked list of path objects */
  403. path_t **plist_hook = &plist; /* used to speed up appending to linked list */
  404. potrace_bitmap_t *bm1 = NULL;
  405. int sign;
  406. bm1 = bm_dup(bm);
  407. if (!bm1) {
  408. goto error;
  409. }
  410. /* be sure the byte padding on the right is set to 0, as the fast
  411. pixel search below relies on it */
  412. bm_clearexcess(bm1);
  413. /* iterate through components */
  414. x = 0;
  415. y = bm1->h - 1;
  416. while (findnext(bm1, &x, &y) == 0) {
  417. /* calculate the sign by looking at the original */
  418. sign = BM_GET(bm, x, y) ? '+' : '-';
  419. /* calculate the path */
  420. p = findpath(bm1, x, y+1, sign, param->turnpolicy);
  421. if (p==NULL) {
  422. goto error;
  423. }
  424. /* update buffered image */
  425. xor_path(bm1, p);
  426. /* if it's a turd, eliminate it, else append it to the list */
  427. if (p->area <= param->turdsize) {
  428. path_free(p);
  429. } else {
  430. list_insert_beforehook(p, plist_hook);
  431. }
  432. if (bm1->h > 0) { /* to be sure */
  433. progress_update(1-y/(double)bm1->h, progress);
  434. }
  435. }
  436. pathlist_to_tree(plist, bm1);
  437. bm_free(bm1);
  438. *plistp = plist;
  439. progress_update(1.0, progress);
  440. return 0;
  441. error:
  442. bm_free(bm1);
  443. list_forall_unlink(p, plist) {
  444. path_free(p);
  445. }
  446. return -1;
  447. }