boundary.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* The GIMP -- an image manipulation program
  2. * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "config.h"
  19. #include <string.h>
  20. #include <glib.h>
  21. #include "apptypes.h"
  22. #include "appenv.h"
  23. #include "errors.h"
  24. #include "boundary.h"
  25. #include "tile.h"
  26. /* half intensity for mask */
  27. #define HALF_WAY 127
  28. /* BoundSeg array growth parameter */
  29. #define MAX_SEGS_INC 2048
  30. /* The array of vertical segments */
  31. static int * vert_segs = NULL;
  32. /* The array of segments */
  33. static BoundSeg * tmp_segs = NULL;
  34. static int num_segs = 0;
  35. static int max_segs = 0;
  36. /* static empty segment arrays */
  37. static int * empty_segs_n = NULL;
  38. static int num_empty_n = 0;
  39. static int * empty_segs_c = NULL;
  40. static int num_empty_c = 0;
  41. static int * empty_segs_l = NULL;
  42. static int num_empty_l = 0;
  43. static int max_empty_segs = 0;
  44. /* global state variables--improve parameter efficiency */
  45. static PixelRegion * cur_PR;
  46. /* local function prototypes */
  47. static void find_empty_segs (PixelRegion *, int, int *, int, int *,
  48. BoundaryType, int, int, int, int);
  49. static void make_seg (int, int, int, int, int);
  50. static void allocate_vert_segs (void);
  51. static void allocate_empty_segs (void);
  52. static void process_horiz_seg (int, int, int, int, int);
  53. static void make_horiz_segs (int, int, int, int *, int, int);
  54. static void generate_boundary (BoundaryType, int, int, int, int);
  55. /* Function definitions */
  56. static void
  57. find_empty_segs (PixelRegion *maskPR,
  58. int scanline,
  59. int empty_segs[],
  60. int max_empty,
  61. int *num_empty,
  62. BoundaryType type,
  63. int x1,
  64. int y1,
  65. int x2,
  66. int y2)
  67. {
  68. unsigned char *data;
  69. int x;
  70. int start, end;
  71. int val, last;
  72. int tilex;
  73. Tile *tile = NULL;
  74. int endx, l_num_empty, dstep = 0;
  75. data = NULL;
  76. start = 0;
  77. end = 0;
  78. *num_empty = 0;
  79. if (scanline < maskPR->y || scanline >= (maskPR->y + maskPR->h))
  80. {
  81. empty_segs[(*num_empty)++] = 0;
  82. empty_segs[(*num_empty)++] = G_MAXINT;
  83. return;
  84. }
  85. if (type == WithinBounds)
  86. {
  87. if (scanline < y1 || scanline >= y2)
  88. {
  89. empty_segs[(*num_empty)++] = 0;
  90. empty_segs[(*num_empty)++] = G_MAXINT;
  91. return;
  92. }
  93. start = x1;
  94. end = x2;
  95. }
  96. else if (type == IgnoreBounds)
  97. {
  98. start = maskPR->x;
  99. end = maskPR->x + maskPR->w;
  100. if (scanline < y1 || scanline >= y2)
  101. x2 = -1;
  102. }
  103. tilex = -1;
  104. empty_segs[(*num_empty)++] = 0;
  105. last = -1;
  106. l_num_empty = *num_empty;
  107. for (x = start; x < end;)
  108. {
  109. /* Check to see if we must advance to next tile */
  110. if ((x / TILE_WIDTH) != tilex)
  111. {
  112. if (tile)
  113. tile_release (tile, FALSE);
  114. tile = tile_manager_get_tile (maskPR->tiles, x, scanline, TRUE, FALSE);
  115. data = (unsigned char*)tile_data_pointer (tile, x % TILE_WIDTH, scanline % TILE_HEIGHT) + (tile_bpp(tile) - 1);
  116. tilex = x / TILE_WIDTH;
  117. dstep = tile_bpp(tile);
  118. }
  119. endx = x + (TILE_WIDTH - (x%TILE_WIDTH));
  120. endx = MIN (end, endx);
  121. if (type == IgnoreBounds && (endx > x1 || x < x2))
  122. for (; x < endx; x++)
  123. {
  124. if (*data > HALF_WAY)
  125. if (x >= x1 && x < x2)
  126. val = -1;
  127. else
  128. val = 1;
  129. else
  130. val = -1;
  131. data += dstep;
  132. if (last != val)
  133. empty_segs[l_num_empty++] = x;
  134. last = val;
  135. }
  136. else
  137. for (; x < endx; x++)
  138. {
  139. if (*data > HALF_WAY)
  140. val = 1;
  141. else
  142. val = -1;
  143. data += dstep;
  144. if (last != val)
  145. empty_segs[l_num_empty++] = x;
  146. last = val;
  147. }
  148. }
  149. *num_empty = l_num_empty;
  150. if (last > 0)
  151. empty_segs[(*num_empty)++] = x;
  152. empty_segs[(*num_empty)++] = G_MAXINT;
  153. if (tile)
  154. tile_release (tile, FALSE);
  155. }
  156. static void
  157. make_seg (int x1,
  158. int y1,
  159. int x2,
  160. int y2,
  161. int open)
  162. {
  163. if (num_segs >= max_segs)
  164. {
  165. max_segs += MAX_SEGS_INC;
  166. tmp_segs = (BoundSeg *) g_realloc ((void *) tmp_segs,
  167. sizeof (BoundSeg) * max_segs);
  168. if (!tmp_segs)
  169. gimp_fatal_error ("make_seg(): Unable to reallocate segments array for mask boundary.");
  170. }
  171. tmp_segs[num_segs].x1 = x1;
  172. tmp_segs[num_segs].y1 = y1;
  173. tmp_segs[num_segs].x2 = x2;
  174. tmp_segs[num_segs].y2 = y2;
  175. tmp_segs[num_segs].open = open;
  176. num_segs ++;
  177. }
  178. static void
  179. allocate_vert_segs (void)
  180. {
  181. int i;
  182. /* allocate and initialize the vert_segs array */
  183. vert_segs = (int *) g_realloc ((void *) vert_segs, (cur_PR->w + cur_PR->x + 1) * sizeof (int));
  184. for (i = 0; i <= (cur_PR->w + cur_PR->x); i++)
  185. vert_segs[i] = -1;
  186. }
  187. static void
  188. allocate_empty_segs (void)
  189. {
  190. int need_num_segs;
  191. /* find the maximum possible number of empty segments given the current mask */
  192. need_num_segs = cur_PR->w + 3;
  193. if (need_num_segs > max_empty_segs)
  194. {
  195. max_empty_segs = need_num_segs;
  196. empty_segs_n = (int *) g_realloc (empty_segs_n, sizeof (int) * max_empty_segs);
  197. empty_segs_c = (int *) g_realloc (empty_segs_c, sizeof (int) * max_empty_segs);
  198. empty_segs_l = (int *) g_realloc (empty_segs_l, sizeof (int) * max_empty_segs);
  199. if (!empty_segs_n || !empty_segs_l || !empty_segs_c)
  200. gimp_fatal_error ("allocate_empty_segs(): Unable to reallocate empty segments array for mask boundary.");
  201. }
  202. }
  203. static void
  204. process_horiz_seg (int x1,
  205. int y1,
  206. int x2,
  207. int y2,
  208. int open)
  209. {
  210. /* This procedure accounts for any vertical segments that must be
  211. drawn to close in the horizontal segments. */
  212. if (vert_segs[x1] >= 0)
  213. {
  214. make_seg (x1, vert_segs[x1], x1, y1, !open);
  215. vert_segs[x1] = -1;
  216. }
  217. else
  218. vert_segs[x1] = y1;
  219. if (vert_segs[x2] >= 0)
  220. {
  221. make_seg (x2, vert_segs[x2], x2, y2, open);
  222. vert_segs[x2] = -1;
  223. }
  224. else
  225. vert_segs[x2] = y2;
  226. make_seg (x1, y1, x2, y2, open);
  227. }
  228. static void
  229. make_horiz_segs (int start,
  230. int end,
  231. int scanline,
  232. int empty[],
  233. int num_empty,
  234. int top)
  235. {
  236. int empty_index;
  237. int e_s, e_e; /* empty segment start and end values */
  238. for (empty_index = 0; empty_index < num_empty; empty_index += 2)
  239. {
  240. e_s = *empty++;
  241. e_e = *empty++;
  242. if (e_s <= start && e_e >= end)
  243. process_horiz_seg (start, scanline, end, scanline, top);
  244. else if ((e_s > start && e_s < end) ||
  245. (e_e < end && e_e > start))
  246. process_horiz_seg (MAX (e_s, start), scanline,
  247. MIN (e_e, end), scanline, top);
  248. }
  249. }
  250. static void
  251. generate_boundary (BoundaryType type,
  252. int x1,
  253. int y1,
  254. int x2,
  255. int y2)
  256. {
  257. int scanline;
  258. int i;
  259. int start, end;
  260. int * tmp_segs;
  261. start = 0;
  262. end = 0;
  263. /* array for determining the vertical line segments which must be drawn */
  264. allocate_vert_segs ();
  265. /* make sure there is enough space for the empty segment array */
  266. allocate_empty_segs ();
  267. num_segs = 0;
  268. if (type == WithinBounds)
  269. {
  270. start = y1;
  271. end = y2;
  272. }
  273. else if (type == IgnoreBounds)
  274. {
  275. start = cur_PR->y;
  276. end = cur_PR->y + cur_PR->h;
  277. }
  278. /* Find the empty segments for the previous and current scanlines */
  279. find_empty_segs (cur_PR, start - 1, empty_segs_l,
  280. max_empty_segs, &num_empty_l,
  281. type, x1, y1, x2, y2);
  282. find_empty_segs (cur_PR, start, empty_segs_c,
  283. max_empty_segs, &num_empty_c,
  284. type, x1, y1, x2, y2);
  285. for (scanline = start; scanline < end; scanline++)
  286. {
  287. /* find the empty segment list for the next scanline */
  288. find_empty_segs (cur_PR, scanline + 1, empty_segs_n,
  289. max_empty_segs, &num_empty_n,
  290. type, x1, y1, x2, y2);
  291. /* process the segments on the current scanline */
  292. for (i = 1; i < num_empty_c - 1; i += 2)
  293. {
  294. make_horiz_segs (empty_segs_c [i], empty_segs_c [i+1],
  295. scanline, empty_segs_l, num_empty_l, 1);
  296. make_horiz_segs (empty_segs_c [i], empty_segs_c [i+1],
  297. scanline+1, empty_segs_n, num_empty_n, 0);
  298. }
  299. /* get the next scanline of empty segments, swap others */
  300. tmp_segs = empty_segs_l;
  301. empty_segs_l = empty_segs_c;
  302. num_empty_l = num_empty_c;
  303. empty_segs_c = empty_segs_n;
  304. num_empty_c = num_empty_n;
  305. empty_segs_n = tmp_segs;
  306. }
  307. }
  308. BoundSeg *
  309. find_mask_boundary (PixelRegion *maskPR,
  310. int *num_elems,
  311. BoundaryType type,
  312. int x1,
  313. int y1,
  314. int x2,
  315. int y2)
  316. {
  317. BoundSeg * new_segs = NULL;
  318. /* The mask paramater can be any PixelRegion. If the region
  319. * has more than 1 bytes/pixel, the last byte of each pixel is
  320. * used to determine the boundary outline.
  321. */
  322. cur_PR = maskPR;
  323. /* Calculate the boundary */
  324. generate_boundary (type, x1, y1, x2, y2);
  325. /* Set the number of X segments */
  326. *num_elems = num_segs;
  327. /* Make a copy of the boundary */
  328. if (num_segs)
  329. {
  330. new_segs = (BoundSeg *) g_malloc (sizeof (BoundSeg) * num_segs);
  331. memcpy (new_segs, tmp_segs, (sizeof (BoundSeg) * num_segs));
  332. }
  333. /* Return the new boundary */
  334. return new_segs;
  335. }
  336. /************************/
  337. /* Sorting a Boundary */
  338. static int find_segment (BoundSeg *, int, int, int);
  339. static int
  340. find_segment (BoundSeg *segs,
  341. int ns,
  342. int x,
  343. int y)
  344. {
  345. int index;
  346. for (index = 0; index < ns; index++)
  347. if (((segs[index].x1 == x && segs[index].y1 == y) || (segs[index].x2 == x && segs[index].y2 == y)) &&
  348. segs[index].visited == FALSE)
  349. return index;
  350. return -1;
  351. }
  352. BoundSeg *
  353. sort_boundary (BoundSeg *segs,
  354. int ns,
  355. int *num_groups)
  356. {
  357. int i;
  358. int index;
  359. int x, y;
  360. int startx, starty;
  361. int empty = (num_segs == 0);
  362. BoundSeg *new_segs;
  363. index = 0;
  364. new_segs = NULL;
  365. for (i = 0; i < ns; i++)
  366. segs[i].visited = FALSE;
  367. num_segs = 0;
  368. *num_groups = 0;
  369. while (! empty)
  370. {
  371. empty = TRUE;
  372. /* find the index of a non-visited segment to start a group */
  373. for (i = 0; i < ns; i++)
  374. if (segs[i].visited == FALSE)
  375. {
  376. index = i;
  377. empty = FALSE;
  378. i = ns;
  379. }
  380. if (! empty)
  381. {
  382. make_seg (segs[index].x1, segs[index].y1,
  383. segs[index].x2, segs[index].y2,
  384. segs[index].open);
  385. segs[index].visited = TRUE;
  386. startx = segs[index].x1;
  387. starty = segs[index].y1;
  388. x = segs[index].x2;
  389. y = segs[index].y2;
  390. while ((index = find_segment (segs, ns, x, y)) != -1)
  391. {
  392. /* make sure ordering is correct */
  393. if (x == segs[index].x1 && y == segs[index].y1)
  394. {
  395. make_seg (segs[index].x1, segs[index].y1,
  396. segs[index].x2, segs[index].y2,
  397. segs[index].open);
  398. x = segs[index].x2;
  399. y = segs[index].y2;
  400. }
  401. else
  402. {
  403. make_seg (segs[index].x2, segs[index].y2,
  404. segs[index].x1, segs[index].y1,
  405. segs[index].open);
  406. x = segs[index].x1;
  407. y = segs[index].y1;
  408. }
  409. segs[index].visited = TRUE;
  410. }
  411. if (x != startx || y != starty)
  412. g_message ("sort_boundary(): Unconnected boundary group!");
  413. /* Mark the end of a group */
  414. *num_groups = *num_groups + 1;
  415. make_seg (-1, -1, -1, -1, 0);
  416. }
  417. }
  418. /* Make a copy of the boundary */
  419. if (num_segs)
  420. {
  421. new_segs = (BoundSeg *) g_malloc (sizeof (BoundSeg) * num_segs);
  422. memcpy (new_segs, tmp_segs, (sizeof (BoundSeg) * num_segs));
  423. }
  424. /* Return the new boundary */
  425. return new_segs;
  426. }