region.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  1. /*
  2. * File : region.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtgui/region.h>
  15. #include <rtgui/rtgui_system.h>
  16. /* #define good(reg) RT_ASSERT(rtgui_region16_valid(reg)) */
  17. #define good(reg)
  18. #define RTGUI_MIN(a,b) ((a) < (b) ? (a) : (b))
  19. #define RTGUI_MAX(a,b) ((a) > (b) ? (a) : (b))
  20. #define RTGUI_SHRT_MIN (-32767-1)
  21. #define RTGUI_SHRT_MAX 32767
  22. #define PIXREGION_NIL(reg) ((reg)->data && !(reg)->data->numRects)
  23. /* not a region */
  24. #define PIXREGION_NAR(reg) ((reg)->data == &rtgui_brokendata)
  25. #define PIXREGION_NUM_RECTS(reg) ((reg)->data ? (reg)->data->numRects : 1)
  26. #define PIXREGION_SIZE(reg) ((reg)->data ? (reg)->data->size : 0)
  27. #define PIXREGION_RECTS(reg) ((reg)->data ? (rtgui_rect_t *)((reg)->data + 1) \
  28. : &(reg)->extents)
  29. #define PIXREGION_BOXPTR(reg) ((rtgui_rect_t *)((reg)->data + 1))
  30. #define PIXREGION_BOX(reg,i) (&PIXREGION_BOXPTR(reg)[i])
  31. #define PIXREGION_TOP(reg) PIXREGION_BOX(reg, (reg)->data->numRects)
  32. #define PIXREGION_END(reg) PIXREGION_BOX(reg, (reg)->data->numRects - 1)
  33. #define PIXREGION_SZOF(n) (sizeof(rtgui_region_data_t) + ((n) * sizeof(rtgui_rect_t)))
  34. rtgui_rect_t rtgui_empty_rect = {0, 0, 0, 0};
  35. rtgui_point_t rtgui_empty_point = {0, 0};
  36. static rtgui_region_data_t rtgui_region_emptydata = {0, 0};
  37. static rtgui_region_data_t rtgui_brokendata = {0, 0};
  38. static rtgui_region_status_t rtgui_break(rtgui_region_t *pReg);
  39. /*
  40. * The functions in this file implement the Region abstraction used extensively
  41. * throughout the X11 sample server. A Region is simply a set of disjoint
  42. * (non-overlapping) rectangles, plus an "extent" rectangle which is the
  43. * smallest single rectangle that contains all the non-overlapping rectangles.
  44. *
  45. * A Region is implemented as a "y-x-banded" array of rectangles. This array
  46. * imposes two degrees of order. First, all rectangles are sorted by top side
  47. * y coordinate first (y1), and then by left side x coordinate (x1).
  48. *
  49. * Furthermore, the rectangles are grouped into "bands". Each rectangle in a
  50. * band has the same top y coordinate (y1), and each has the same bottom y
  51. * coordinate (y2). Thus all rectangles in a band differ only in their left
  52. * and right side (x1 and x2). Bands are implicit in the array of rectangles:
  53. * there is no separate list of band start pointers.
  54. *
  55. * The y-x band representation does not RTGUI_MINimize rectangles. In particular,
  56. * if a rectangle vertically crosses a band (the rectangle has scanlines in
  57. * the y1 to y2 area spanned by the band), then the rectangle may be broken
  58. * down into two or more smaller rectangles stacked one atop the other.
  59. *
  60. * ----------- -----------
  61. * | | | | band 0
  62. * | | -------- ----------- --------
  63. * | | | | in y-x banded | | | | band 1
  64. * | | | | form is | | | |
  65. * ----------- | | ----------- --------
  66. * | | | | band 2
  67. * -------- --------
  68. *
  69. * An added constraint on the rectangles is that they must cover as much
  70. * horizontal area as possible: no two rectangles within a band are allowed
  71. * to touch.
  72. *
  73. * Whenever possible, bands will be merged together to cover a greater vertical
  74. * distance (and thus reduce the number of rectangles). Two bands can be merged
  75. * only if the bottom of one touches the top of the other and they have
  76. * rectangles in the same places (of the same width, of course).
  77. *
  78. * Adam de Boor wrote most of the original region code. Joel McCormack
  79. * substantially modified or rewrote most of the core arithmetic routines, and
  80. * added rtgui_region_validate in order to support several speed improvements to
  81. * rtgui_region_validateTree. Bob Scheifler changed the representation to be more
  82. * compact when empty or a single rectangle, and did a bunch of gratuitous
  83. * reformatting. Carl Worth did further gratuitous reformatting while re-merging
  84. * the server and client region code into libpixregion.
  85. */
  86. /* true iff two Boxes overlap */
  87. #define EXTENTCHECK(r1,r2) \
  88. (!( ((r1)->x2 <= (r2)->x1) || \
  89. ((r1)->x1 >= (r2)->x2) || \
  90. ((r1)->y2 <= (r2)->y1) || \
  91. ((r1)->y1 >= (r2)->y2) ) )
  92. /* true iff (x,y) is in Box */
  93. #define INBOX(r,x,y) \
  94. ( ((r)->x2 > (x)) && \
  95. ((r)->x1 <= (x)) && \
  96. ((r)->y2 > (y)) && \
  97. ((r)->y1 <= (y)) )
  98. /* true iff Box r1 contains Box r2 */
  99. #define SUBSUMES(r1,r2) \
  100. ( ((r1)->x1 <= (r2)->x1) && \
  101. ((r1)->x2 >= (r2)->x2) && \
  102. ((r1)->y1 <= (r2)->y1) && \
  103. ((r1)->y2 >= (r2)->y2) )
  104. #define allocData(n) rtgui_malloc(PIXREGION_SZOF(n))
  105. #define freeData(reg) if ((reg)->data && (reg)->data->size) rtgui_free((reg)->data)
  106. #define RECTALLOC_BAIL(pReg,n,bail) \
  107. if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
  108. if (!rtgui_rect_alloc(pReg, n)) { goto bail; }
  109. #define RECTALLOC(pReg,n) \
  110. if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
  111. if (!rtgui_rect_alloc(pReg, n)) { return RTGUI_REGION_STATUS_FAILURE; }
  112. #define ADDRECT(pNextRect,nx1,ny1,nx2,ny2) \
  113. { \
  114. pNextRect->x1 = nx1; \
  115. pNextRect->y1 = ny1; \
  116. pNextRect->x2 = nx2; \
  117. pNextRect->y2 = ny2; \
  118. pNextRect++; \
  119. }
  120. #define NEWRECT(pReg,pNextRect,nx1,ny1,nx2,ny2) \
  121. { \
  122. if (!(pReg)->data || ((pReg)->data->numRects == (pReg)->data->size))\
  123. { \
  124. if (!rtgui_rect_alloc(pReg, 1)) \
  125. return RTGUI_REGION_STATUS_FAILURE; \
  126. pNextRect = PIXREGION_TOP(pReg); \
  127. } \
  128. ADDRECT(pNextRect,nx1,ny1,nx2,ny2); \
  129. pReg->data->numRects++; \
  130. RT_ASSERT(pReg->data->numRects<=pReg->data->size); \
  131. }
  132. #define DOWNSIZE(reg,numRects) \
  133. if (((numRects) < ((reg)->data->size >> 1)) && ((reg)->data->size > 50)) \
  134. { \
  135. rtgui_region_data_t * NewData; \
  136. NewData = (rtgui_region_data_t *)rt_realloc((reg)->data, PIXREGION_SZOF(numRects)); \
  137. if (NewData) \
  138. { \
  139. NewData->size = (numRects); \
  140. (reg)->data = NewData; \
  141. } \
  142. }
  143. void rtgui_region_init(rtgui_region_t *region)
  144. {
  145. region->extents = rtgui_empty_rect;
  146. region->data = &rtgui_region_emptydata;
  147. }
  148. void rtgui_region_init_rect(rtgui_region_t *region,
  149. int x, int y, unsigned int width, unsigned int height)
  150. {
  151. region->extents.x1 = x;
  152. region->extents.y1 = y;
  153. region->extents.x2 = x + width;
  154. region->extents.y2 = y + height;
  155. region->data = RT_NULL;
  156. }
  157. void rtgui_region_init_with_extents(rtgui_region_t *region, rtgui_rect_t *extents)
  158. {
  159. region->extents = *extents;
  160. region->data = RT_NULL;
  161. }
  162. void rtgui_region_fini (rtgui_region_t *region)
  163. {
  164. good (region);
  165. freeData (region);
  166. }
  167. int rtgui_region_num_rects (rtgui_region_t *region)
  168. {
  169. return PIXREGION_NUM_RECTS (region);
  170. }
  171. rtgui_rect_t *rtgui_region_rects (rtgui_region_t *region)
  172. {
  173. return PIXREGION_RECTS (region);
  174. }
  175. static rtgui_region_status_t rtgui_break (rtgui_region_t *region)
  176. {
  177. freeData (region);
  178. region->extents = rtgui_empty_rect;
  179. region->data = &rtgui_brokendata;
  180. return RTGUI_REGION_STATUS_FAILURE;
  181. }
  182. static rtgui_region_status_t rtgui_rect_alloc(rtgui_region_t * region, int n)
  183. {
  184. rtgui_region_data_t *data;
  185. if (!region->data)
  186. {
  187. n++;
  188. region->data = allocData(n);
  189. if (!region->data) return rtgui_break (region);
  190. region->data->numRects = 1;
  191. *PIXREGION_BOXPTR(region) = region->extents;
  192. }
  193. else if (!region->data->size)
  194. {
  195. region->data = allocData(n);
  196. if (!region->data) return rtgui_break (region);
  197. region->data->numRects = 0;
  198. }
  199. else
  200. {
  201. if (n == 1)
  202. {
  203. n = region->data->numRects;
  204. if (n > 500) /* XXX pick numbers out of a hat */
  205. n = 250;
  206. }
  207. n += region->data->numRects;
  208. data = (rtgui_region_data_t *)rt_realloc(region->data, PIXREGION_SZOF(n));
  209. if (!data) return rtgui_break (region);
  210. region->data = data;
  211. }
  212. region->data->size = n;
  213. return RTGUI_REGION_STATUS_SUCCESS;
  214. }
  215. rtgui_region_status_t rtgui_region_copy(rtgui_region_t *dst, rtgui_region_t *src)
  216. {
  217. good(dst);
  218. good(src);
  219. if (dst == src)
  220. return RTGUI_REGION_STATUS_SUCCESS;
  221. dst->extents = src->extents;
  222. if (!src->data || !src->data->size)
  223. {
  224. freeData(dst);
  225. dst->data = src->data;
  226. return RTGUI_REGION_STATUS_SUCCESS;
  227. }
  228. if (!dst->data || (dst->data->size < src->data->numRects))
  229. {
  230. freeData(dst);
  231. dst->data = allocData(src->data->numRects);
  232. if (!dst->data) return rtgui_break (dst);
  233. dst->data->size = src->data->numRects;
  234. }
  235. dst->data->numRects = src->data->numRects;
  236. rt_memmove((char *)PIXREGION_BOXPTR(dst),(char *)PIXREGION_BOXPTR(src),
  237. dst->data->numRects * sizeof(rtgui_rect_t));
  238. return RTGUI_REGION_STATUS_SUCCESS;
  239. }
  240. /*======================================================================
  241. * Generic Region Operator
  242. *====================================================================*/
  243. /*-
  244. *-----------------------------------------------------------------------
  245. * rtgui_coalesce --
  246. * Attempt to merge the boxes in the current band with those in the
  247. * previous one. We are guaranteed that the current band extends to
  248. * the end of the rects array. Used only by rtgui_op.
  249. *
  250. * Results:
  251. * The new index for the previous band.
  252. *
  253. * Side Effects:
  254. * If coalescing takes place:
  255. * - rectangles in the previous band will have their y2 fields
  256. * altered.
  257. * - region->data->numRects will be decreased.
  258. *
  259. *-----------------------------------------------------------------------
  260. */
  261. rt_inline int
  262. rtgui_coalesce (
  263. rtgui_region_t *region, /* Region to coalesce */
  264. int prevStart, /* Index of start of previous band */
  265. int curStart) /* Index of start of current band */
  266. {
  267. rtgui_rect_t * pPrevBox; /* Current box in previous band */
  268. rtgui_rect_t * pCurBox; /* Current box in current band */
  269. int numRects; /* Number rectangles in both bands */
  270. int y2; /* Bottom of current band */
  271. /*
  272. * Figure out how many rectangles are in the band.
  273. */
  274. numRects = curStart - prevStart;
  275. RT_ASSERT(numRects == region->data->numRects - curStart);
  276. if (!numRects) return curStart;
  277. /*
  278. * The bands may only be coalesced if the bottom of the previous
  279. * matches the top scanline of the current.
  280. */
  281. pPrevBox = PIXREGION_BOX(region, prevStart);
  282. pCurBox = PIXREGION_BOX(region, curStart);
  283. if (pPrevBox->y2 != pCurBox->y1) return curStart;
  284. /*
  285. * Make sure the bands have boxes in the same places. This
  286. * assumes that boxes have been added in such a way that they
  287. * cover the most area possible. I.e. two boxes in a band must
  288. * have some horizontal space between them.
  289. */
  290. y2 = pCurBox->y2;
  291. do
  292. {
  293. if ((pPrevBox->x1 != pCurBox->x1) || (pPrevBox->x2 != pCurBox->x2))
  294. {
  295. return (curStart);
  296. }
  297. pPrevBox++;
  298. pCurBox++;
  299. numRects--;
  300. }
  301. while (numRects);
  302. /*
  303. * The bands may be merged, so set the bottom y of each box
  304. * in the previous band to the bottom y of the current band.
  305. */
  306. numRects = curStart - prevStart;
  307. region->data->numRects -= numRects;
  308. do
  309. {
  310. pPrevBox--;
  311. pPrevBox->y2 = y2;
  312. numRects--;
  313. }
  314. while (numRects);
  315. return prevStart;
  316. }
  317. /* Quicky macro to avoid trivial reject procedure calls to rtgui_coalesce */
  318. #define Coalesce(newReg, prevBand, curBand) \
  319. if (curBand - prevBand == newReg->data->numRects - curBand) { \
  320. prevBand = rtgui_coalesce(newReg, prevBand, curBand); \
  321. } else { \
  322. prevBand = curBand; \
  323. }
  324. /*-
  325. *-----------------------------------------------------------------------
  326. * rtgui_region_appendNonO --
  327. * Handle a non-overlapping band for the union and subtract operations.
  328. * Just adds the (top/bottom-clipped) rectangles into the region.
  329. * Doesn't have to check for subsumption or anything.
  330. *
  331. * Results:
  332. * None.
  333. *
  334. * Side Effects:
  335. * region->data->numRects is incremented and the rectangles overwritten
  336. * with the rectangles we're passed.
  337. *
  338. *-----------------------------------------------------------------------
  339. */
  340. rt_inline rtgui_region_status_t
  341. rtgui_region_appendNonO (
  342. rtgui_region_t *region,
  343. rtgui_rect_t *r,
  344. rtgui_rect_t *rEnd,
  345. int y1,
  346. int y2)
  347. {
  348. rtgui_rect_t * pNextRect;
  349. int newRects;
  350. newRects = rEnd - r;
  351. RT_ASSERT(y1 < y2);
  352. RT_ASSERT(newRects != 0);
  353. /* Make sure we have enough space for all rectangles to be added */
  354. RECTALLOC(region, newRects);
  355. pNextRect = PIXREGION_TOP(region);
  356. region->data->numRects += newRects;
  357. do
  358. {
  359. RT_ASSERT(r->x1 < r->x2);
  360. ADDRECT(pNextRect, r->x1, y1, r->x2, y2);
  361. r++;
  362. }
  363. while (r != rEnd);
  364. return RTGUI_REGION_STATUS_SUCCESS;
  365. }
  366. #define FindBand(r, rBandEnd, rEnd, ry1) \
  367. { \
  368. ry1 = r->y1; \
  369. rBandEnd = r+1; \
  370. while ((rBandEnd != rEnd) && (rBandEnd->y1 == ry1)) { \
  371. rBandEnd++; \
  372. } \
  373. }
  374. #define AppendRegions(newReg, r, rEnd) \
  375. { \
  376. int newRects = rEnd - r; \
  377. if (newRects) { \
  378. RECTALLOC(newReg, newRects); \
  379. rt_memmove((char *)PIXREGION_TOP(newReg),(char *)r, \
  380. newRects * sizeof(rtgui_rect_t)); \
  381. newReg->data->numRects += newRects; \
  382. } \
  383. }
  384. /*-
  385. *-----------------------------------------------------------------------
  386. * rtgui_op --
  387. * Apply an operation to two regions. Called by rtgui_region_union, rtgui_region_inverse,
  388. * rtgui_region_subtract, rtgui_region_intersect.... Both regions MUST have at least one
  389. * rectangle, and cannot be the same object.
  390. *
  391. * Results:
  392. * RTGUI_REGION_STATUS_SUCCESS if successful.
  393. *
  394. * Side Effects:
  395. * The new region is overwritten.
  396. * pOverlap set to RTGUI_REGION_STATUS_SUCCESS if overlapFunc ever returns RTGUI_REGION_STATUS_SUCCESS.
  397. *
  398. * Notes:
  399. * The idea behind this function is to view the two regions as sets.
  400. * Together they cover a rectangle of area that this function divides
  401. * into horizontal bands where points are covered only by one region
  402. * or by both. For the first case, the nonOverlapFunc is called with
  403. * each the band and the band's upper and lower extents. For the
  404. * second, the overlapFunc is called to process the entire band. It
  405. * is responsible for clipping the rectangles in the band, though
  406. * this function provides the boundaries.
  407. * At the end of each band, the new region is coalesced, if possible,
  408. * to reduce the number of rectangles in the region.
  409. *
  410. *-----------------------------------------------------------------------
  411. */
  412. typedef rtgui_region_status_t (*OverlapProcPtr)(
  413. rtgui_region_t *region,
  414. rtgui_rect_t *r1,
  415. rtgui_rect_t *r1End,
  416. rtgui_rect_t *r2,
  417. rtgui_rect_t *r2End,
  418. short y1,
  419. short y2,
  420. int *pOverlap);
  421. static rtgui_region_status_t
  422. rtgui_op(
  423. rtgui_region_t *newReg, /* Place to store result */
  424. rtgui_region_t *reg1, /* First region in operation */
  425. rtgui_region_t *reg2, /* 2d region in operation */
  426. OverlapProcPtr overlapFunc, /* Function to call for over-
  427. * lapping bands */
  428. int appendNon1, /* Append non-overlapping bands */
  429. /* in region 1 ? */
  430. int appendNon2, /* Append non-overlapping bands */
  431. /* in region 2 ? */
  432. int *pOverlap)
  433. {
  434. rtgui_rect_t * r1; /* Pointer into first region */
  435. rtgui_rect_t * r2; /* Pointer into 2d region */
  436. rtgui_rect_t * r1End; /* End of 1st region */
  437. rtgui_rect_t * r2End; /* End of 2d region */
  438. short ybot; /* Bottom of intersection */
  439. short ytop; /* Top of intersection */
  440. rtgui_region_data_t * oldData; /* Old data for newReg */
  441. int prevBand; /* Index of start of
  442. * previous band in newReg */
  443. int curBand; /* Index of start of current
  444. * band in newReg */
  445. rtgui_rect_t * r1BandEnd; /* End of current band in r1 */
  446. rtgui_rect_t * r2BandEnd; /* End of current band in r2 */
  447. short top; /* Top of non-overlapping band */
  448. short bot; /* Bottom of non-overlapping band*/
  449. int r1y1; /* Temps for r1->y1 and r2->y1 */
  450. int r2y1;
  451. int newSize;
  452. int numRects;
  453. /*
  454. * Break any region computed from a broken region
  455. */
  456. if (PIXREGION_NAR (reg1) || PIXREGION_NAR(reg2))
  457. return rtgui_break (newReg);
  458. /*
  459. * Initialization:
  460. * set r1, r2, r1End and r2End appropriately, save the rectangles
  461. * of the destination region until the end in case it's one of
  462. * the two source regions, then mark the "new" region empty, allocating
  463. * another array of rectangles for it to use.
  464. */
  465. r1 = PIXREGION_RECTS(reg1);
  466. newSize = PIXREGION_NUM_RECTS(reg1);
  467. r1End = r1 + newSize;
  468. numRects = PIXREGION_NUM_RECTS(reg2);
  469. r2 = PIXREGION_RECTS(reg2);
  470. r2End = r2 + numRects;
  471. RT_ASSERT(r1 != r1End);
  472. RT_ASSERT(r2 != r2End);
  473. oldData = (rtgui_region_data_t *)RT_NULL;
  474. if (((newReg == reg1) && (newSize > 1)) ||
  475. ((newReg == reg2) && (numRects > 1)))
  476. {
  477. oldData = newReg->data;
  478. newReg->data = &rtgui_region_emptydata;
  479. }
  480. /* guess at new size */
  481. if (numRects > newSize)
  482. newSize = numRects;
  483. newSize <<= 1;
  484. if (!newReg->data)
  485. newReg->data = &rtgui_region_emptydata;
  486. else if (newReg->data->size)
  487. newReg->data->numRects = 0;
  488. if (newSize > newReg->data->size)
  489. if (!rtgui_rect_alloc(newReg, newSize))
  490. return RTGUI_REGION_STATUS_FAILURE;
  491. /*
  492. * Initialize ybot.
  493. * In the upcoRTGUI_MINg loop, ybot and ytop serve different functions depending
  494. * on whether the band being handled is an overlapping or non-overlapping
  495. * band.
  496. * In the case of a non-overlapping band (only one of the regions
  497. * has points in the band), ybot is the bottom of the most recent
  498. * intersection and thus clips the top of the rectangles in that band.
  499. * ytop is the top of the next intersection between the two regions and
  500. * serves to clip the bottom of the rectangles in the current band.
  501. * For an overlapping band (where the two regions intersect), ytop clips
  502. * the top of the rectangles of both regions and ybot clips the bottoms.
  503. */
  504. ybot = RTGUI_MIN(r1->y1, r2->y1);
  505. /*
  506. * prevBand serves to mark the start of the previous band so rectangles
  507. * can be coalesced into larger rectangles. qv. rtgui_coalesce, above.
  508. * In the beginning, there is no previous band, so prevBand == curBand
  509. * (curBand is set later on, of course, but the first band will always
  510. * start at index 0). prevBand and curBand must be indices because of
  511. * the possible expansion, and resultant moving, of the new region's
  512. * array of rectangles.
  513. */
  514. prevBand = 0;
  515. do
  516. {
  517. /*
  518. * This algorithm proceeds one source-band (as opposed to a
  519. * destination band, which is deterRTGUI_MINed by where the two regions
  520. * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
  521. * rectangle after the last one in the current band for their
  522. * respective regions.
  523. */
  524. RT_ASSERT(r1 != r1End);
  525. RT_ASSERT(r2 != r2End);
  526. FindBand(r1, r1BandEnd, r1End, r1y1);
  527. FindBand(r2, r2BandEnd, r2End, r2y1);
  528. /*
  529. * First handle the band that doesn't intersect, if any.
  530. *
  531. * Note that attention is restricted to one band in the
  532. * non-intersecting region at once, so if a region has n
  533. * bands between the current position and the next place it overlaps
  534. * the other, this entire loop will be passed through n times.
  535. */
  536. if (r1y1 < r2y1)
  537. {
  538. if (appendNon1)
  539. {
  540. top = RTGUI_MAX(r1y1, ybot);
  541. bot = RTGUI_MIN(r1->y2, r2y1);
  542. if (top != bot)
  543. {
  544. curBand = newReg->data->numRects;
  545. rtgui_region_appendNonO(newReg, r1, r1BandEnd, top, bot);
  546. Coalesce(newReg, prevBand, curBand);
  547. }
  548. }
  549. ytop = r2y1;
  550. }
  551. else if (r2y1 < r1y1)
  552. {
  553. if (appendNon2)
  554. {
  555. top = RTGUI_MAX(r2y1, ybot);
  556. bot = RTGUI_MIN(r2->y2, r1y1);
  557. if (top != bot)
  558. {
  559. curBand = newReg->data->numRects;
  560. rtgui_region_appendNonO(newReg, r2, r2BandEnd, top, bot);
  561. Coalesce(newReg, prevBand, curBand);
  562. }
  563. }
  564. ytop = r1y1;
  565. }
  566. else
  567. {
  568. ytop = r1y1;
  569. }
  570. /*
  571. * Now see if we've hit an intersecting band. The two bands only
  572. * intersect if ybot > ytop
  573. */
  574. ybot = RTGUI_MIN(r1->y2, r2->y2);
  575. if (ybot > ytop)
  576. {
  577. curBand = newReg->data->numRects;
  578. (* overlapFunc)(newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot,
  579. pOverlap);
  580. Coalesce(newReg, prevBand, curBand);
  581. }
  582. /*
  583. * If we've finished with a band (y2 == ybot) we skip forward
  584. * in the region to the next band.
  585. */
  586. if (r1->y2 == ybot) r1 = r1BandEnd;
  587. if (r2->y2 == ybot) r2 = r2BandEnd;
  588. }
  589. while (r1 != r1End && r2 != r2End);
  590. /*
  591. * Deal with whichever region (if any) still has rectangles left.
  592. *
  593. * We only need to worry about banding and coalescing for the very first
  594. * band left. After that, we can just group all remaining boxes,
  595. * regardless of how many bands, into one final append to the list.
  596. */
  597. if ((r1 != r1End) && appendNon1)
  598. {
  599. /* Do first nonOverlap1Func call, which may be able to coalesce */
  600. FindBand(r1, r1BandEnd, r1End, r1y1);
  601. curBand = newReg->data->numRects;
  602. rtgui_region_appendNonO(newReg, r1, r1BandEnd, RTGUI_MAX(r1y1, ybot), r1->y2);
  603. Coalesce(newReg, prevBand, curBand);
  604. /* Just append the rest of the boxes */
  605. AppendRegions(newReg, r1BandEnd, r1End);
  606. }
  607. else if ((r2 != r2End) && appendNon2)
  608. {
  609. /* Do first nonOverlap2Func call, which may be able to coalesce */
  610. FindBand(r2, r2BandEnd, r2End, r2y1);
  611. curBand = newReg->data->numRects;
  612. rtgui_region_appendNonO(newReg, r2, r2BandEnd, RTGUI_MAX(r2y1, ybot), r2->y2);
  613. Coalesce(newReg, prevBand, curBand);
  614. /* Append rest of boxes */
  615. AppendRegions(newReg, r2BandEnd, r2End);
  616. }
  617. if (oldData)
  618. rtgui_free(oldData);
  619. numRects = newReg->data->numRects;
  620. if (!numRects)
  621. {
  622. freeData(newReg);
  623. newReg->data = &rtgui_region_emptydata;
  624. }
  625. else if (numRects == 1)
  626. {
  627. newReg->extents = *PIXREGION_BOXPTR(newReg);
  628. freeData(newReg);
  629. newReg->data = (rtgui_region_data_t *)RT_NULL;
  630. }
  631. else
  632. {
  633. DOWNSIZE(newReg, numRects);
  634. }
  635. return RTGUI_REGION_STATUS_SUCCESS;
  636. }
  637. /*-
  638. *-----------------------------------------------------------------------
  639. * rtgui_set_extents --
  640. * Reset the extents of a region to what they should be. Called by
  641. * rtgui_region_subtract and rtgui_region_intersect as they can't figure it out along the
  642. * way or do so easily, as rtgui_region_union can.
  643. *
  644. * Results:
  645. * None.
  646. *
  647. * Side Effects:
  648. * The region's 'extents' structure is overwritten.
  649. *
  650. *-----------------------------------------------------------------------
  651. */
  652. static void
  653. rtgui_set_extents (rtgui_region_t *region)
  654. {
  655. rtgui_rect_t *box, *boxEnd;
  656. if (!region->data)
  657. return;
  658. if (!region->data->size)
  659. {
  660. region->extents.x2 = region->extents.x1;
  661. region->extents.y2 = region->extents.y1;
  662. return;
  663. }
  664. box = PIXREGION_BOXPTR(region);
  665. boxEnd = PIXREGION_END(region);
  666. /*
  667. * Since box is the first rectangle in the region, it must have the
  668. * smallest y1 and since boxEnd is the last rectangle in the region,
  669. * it must have the largest y2, because of banding. Initialize x1 and
  670. * x2 from box and boxEnd, resp., as good things to initialize them
  671. * to...
  672. */
  673. region->extents.x1 = box->x1;
  674. region->extents.y1 = box->y1;
  675. region->extents.x2 = boxEnd->x2;
  676. region->extents.y2 = boxEnd->y2;
  677. RT_ASSERT(region->extents.y1 < region->extents.y2);
  678. while (box <= boxEnd)
  679. {
  680. if (box->x1 < region->extents.x1)
  681. region->extents.x1 = box->x1;
  682. if (box->x2 > region->extents.x2)
  683. region->extents.x2 = box->x2;
  684. box++;
  685. };
  686. RT_ASSERT(region->extents.x1 < region->extents.x2);
  687. }
  688. /*======================================================================
  689. * Region Intersection
  690. *====================================================================*/
  691. /*-
  692. *-----------------------------------------------------------------------
  693. * rtgui_region_intersectO --
  694. * Handle an overlapping band for rtgui_region_intersect.
  695. *
  696. * Results:
  697. * RTGUI_REGION_STATUS_SUCCESS if successful.
  698. *
  699. * Side Effects:
  700. * Rectangles may be added to the region.
  701. *
  702. *-----------------------------------------------------------------------
  703. */
  704. /*ARGSUSED*/
  705. static rtgui_region_status_t
  706. rtgui_region_intersectO (
  707. rtgui_region_t *region,
  708. rtgui_rect_t *r1,
  709. rtgui_rect_t *r1End,
  710. rtgui_rect_t *r2,
  711. rtgui_rect_t *r2End,
  712. short y1,
  713. short y2,
  714. int *pOverlap)
  715. {
  716. int x1;
  717. int x2;
  718. rtgui_rect_t * pNextRect;
  719. pNextRect = PIXREGION_TOP(region);
  720. RT_ASSERT(y1 < y2);
  721. RT_ASSERT(r1 != r1End && r2 != r2End);
  722. do
  723. {
  724. x1 = RTGUI_MAX(r1->x1, r2->x1);
  725. x2 = RTGUI_MIN(r1->x2, r2->x2);
  726. /*
  727. * If there's any overlap between the two rectangles, add that
  728. * overlap to the new region.
  729. */
  730. if (x1 < x2)
  731. NEWRECT(region, pNextRect, x1, y1, x2, y2);
  732. /*
  733. * Advance the pointer(s) with the leftmost right side, since the next
  734. * rectangle on that list may still overlap the other region's
  735. * current rectangle.
  736. */
  737. if (r1->x2 == x2)
  738. {
  739. r1++;
  740. }
  741. if (r2->x2 == x2)
  742. {
  743. r2++;
  744. }
  745. }
  746. while ((r1 != r1End) && (r2 != r2End));
  747. return RTGUI_REGION_STATUS_SUCCESS;
  748. }
  749. rtgui_region_status_t
  750. rtgui_region_intersect(rtgui_region_t *newReg,
  751. rtgui_region_t *reg1,
  752. rtgui_region_t *reg2)
  753. {
  754. good(reg1);
  755. good(reg2);
  756. good(newReg);
  757. /* check for trivial reject */
  758. if (PIXREGION_NIL(reg1) || PIXREGION_NIL(reg2) ||
  759. !EXTENTCHECK(&reg1->extents, &reg2->extents))
  760. {
  761. /* Covers about 20% of all cases */
  762. freeData(newReg);
  763. newReg->extents.x2 = newReg->extents.x1;
  764. newReg->extents.y2 = newReg->extents.y1;
  765. if (PIXREGION_NAR(reg1) || PIXREGION_NAR(reg2))
  766. {
  767. newReg->data = &rtgui_brokendata;
  768. return RTGUI_REGION_STATUS_FAILURE;
  769. }
  770. else
  771. newReg->data = &rtgui_region_emptydata;
  772. }
  773. else if (!reg1->data && !reg2->data)
  774. {
  775. /* Covers about 80% of cases that aren't trivially rejected */
  776. newReg->extents.x1 = RTGUI_MAX(reg1->extents.x1, reg2->extents.x1);
  777. newReg->extents.y1 = RTGUI_MAX(reg1->extents.y1, reg2->extents.y1);
  778. newReg->extents.x2 = RTGUI_MIN(reg1->extents.x2, reg2->extents.x2);
  779. newReg->extents.y2 = RTGUI_MIN(reg1->extents.y2, reg2->extents.y2);
  780. freeData(newReg);
  781. newReg->data = (rtgui_region_data_t *)RT_NULL;
  782. }
  783. else if (!reg2->data && SUBSUMES(&reg2->extents, &reg1->extents))
  784. {
  785. return rtgui_region_copy(newReg, reg1);
  786. }
  787. else if (!reg1->data && SUBSUMES(&reg1->extents, &reg2->extents))
  788. {
  789. return rtgui_region_copy(newReg, reg2);
  790. }
  791. else if (reg1 == reg2)
  792. {
  793. return rtgui_region_copy(newReg, reg1);
  794. }
  795. else
  796. {
  797. /* General purpose intersection */
  798. int overlap; /* result ignored */
  799. if (!rtgui_op(newReg, reg1, reg2, rtgui_region_intersectO, RTGUI_REGION_STATUS_FAILURE, RTGUI_REGION_STATUS_FAILURE,
  800. &overlap))
  801. return RTGUI_REGION_STATUS_FAILURE;
  802. rtgui_set_extents(newReg);
  803. }
  804. good(newReg);
  805. return(RTGUI_REGION_STATUS_SUCCESS);
  806. }
  807. rtgui_region_status_t
  808. rtgui_region_intersect_rect(rtgui_region_t *newReg,
  809. rtgui_region_t *reg1,
  810. rtgui_rect_t *rect)
  811. {
  812. rtgui_region_t region;
  813. region.data = RT_NULL;
  814. region.extents.x1 = rect->x1;
  815. region.extents.y1 = rect->y1;
  816. region.extents.x2 = rect->x2;
  817. region.extents.y2 = rect->y2;
  818. return rtgui_region_intersect(newReg, reg1, &region);
  819. }
  820. #define MERGERECT(r) \
  821. { \
  822. if (r->x1 <= x2) { \
  823. /* Merge with current rectangle */ \
  824. if (r->x1 < x2) *pOverlap = RTGUI_REGION_STATUS_SUCCESS; \
  825. if (x2 < r->x2) x2 = r->x2; \
  826. } else { \
  827. /* Add current rectangle, start new one */ \
  828. NEWRECT(region, pNextRect, x1, y1, x2, y2); \
  829. x1 = r->x1; \
  830. x2 = r->x2; \
  831. } \
  832. r++; \
  833. }
  834. /*======================================================================
  835. * Region Union
  836. *====================================================================*/
  837. /*-
  838. *-----------------------------------------------------------------------
  839. * rtgui_region_unionO --
  840. * Handle an overlapping band for the union operation. Picks the
  841. * left-most rectangle each time and merges it into the region.
  842. *
  843. * Results:
  844. * RTGUI_REGION_STATUS_SUCCESS if successful.
  845. *
  846. * Side Effects:
  847. * region is overwritten.
  848. * pOverlap is set to RTGUI_REGION_STATUS_SUCCESS if any boxes overlap.
  849. *
  850. *-----------------------------------------------------------------------
  851. */
  852. static rtgui_region_status_t
  853. rtgui_region_unionO (
  854. rtgui_region_t *region,
  855. rtgui_rect_t *r1,
  856. rtgui_rect_t *r1End,
  857. rtgui_rect_t *r2,
  858. rtgui_rect_t *r2End,
  859. short y1,
  860. short y2,
  861. int *pOverlap)
  862. {
  863. rtgui_rect_t * pNextRect;
  864. int x1; /* left and right side of current union */
  865. int x2;
  866. RT_ASSERT (y1 < y2);
  867. RT_ASSERT(r1 != r1End && r2 != r2End);
  868. pNextRect = PIXREGION_TOP(region);
  869. /* Start off current rectangle */
  870. if (r1->x1 < r2->x1)
  871. {
  872. x1 = r1->x1;
  873. x2 = r1->x2;
  874. r1++;
  875. }
  876. else
  877. {
  878. x1 = r2->x1;
  879. x2 = r2->x2;
  880. r2++;
  881. }
  882. while (r1 != r1End && r2 != r2End)
  883. {
  884. if (r1->x1 < r2->x1) MERGERECT(r1) else MERGERECT(r2);
  885. }
  886. /* Finish off whoever (if any) is left */
  887. if (r1 != r1End)
  888. {
  889. do
  890. {
  891. MERGERECT(r1);
  892. }
  893. while (r1 != r1End);
  894. }
  895. else if (r2 != r2End)
  896. {
  897. do
  898. {
  899. MERGERECT(r2);
  900. }
  901. while (r2 != r2End);
  902. }
  903. /* Add current rectangle */
  904. NEWRECT(region, pNextRect, x1, y1, x2, y2);
  905. return RTGUI_REGION_STATUS_SUCCESS;
  906. }
  907. /* Convenience function for perforRTGUI_MINg union of region with a single rectangle */
  908. rtgui_region_status_t
  909. rtgui_region_union_rect(rtgui_region_t *dest, rtgui_region_t *source, rtgui_rect_t* rect)
  910. {
  911. rtgui_region_t region;
  912. region.data = RT_NULL;
  913. region.extents.x1 = rect->x1;
  914. region.extents.y1 = rect->y1;
  915. region.extents.x2 = rect->x2;
  916. region.extents.y2 = rect->y2;
  917. return rtgui_region_union (dest, source, &region);
  918. }
  919. rtgui_region_status_t
  920. rtgui_region_union(rtgui_region_t *newReg, rtgui_region_t *reg1, rtgui_region_t *reg2)
  921. {
  922. int overlap; /* result ignored */
  923. /* Return RTGUI_REGION_STATUS_SUCCESS if some overlap between reg1, reg2 */
  924. good(reg1);
  925. good(reg2);
  926. good(newReg);
  927. /* checks all the simple cases */
  928. /*
  929. * Region 1 and 2 are the same
  930. */
  931. if (reg1 == reg2)
  932. {
  933. return rtgui_region_copy(newReg, reg1);
  934. }
  935. /*
  936. * Region 1 is empty
  937. */
  938. if (PIXREGION_NIL(reg1))
  939. {
  940. if (PIXREGION_NAR(reg1)) return rtgui_break (newReg);
  941. if (newReg != reg2)
  942. return rtgui_region_copy(newReg, reg2);
  943. return RTGUI_REGION_STATUS_SUCCESS;
  944. }
  945. /*
  946. * Region 2 is empty
  947. */
  948. if (PIXREGION_NIL(reg2))
  949. {
  950. if (PIXREGION_NAR(reg2)) return rtgui_break (newReg);
  951. if (newReg != reg1)
  952. return rtgui_region_copy(newReg, reg1);
  953. return RTGUI_REGION_STATUS_SUCCESS;
  954. }
  955. /*
  956. * Region 1 completely subsumes region 2
  957. */
  958. if (!reg1->data && SUBSUMES(&reg1->extents, &reg2->extents))
  959. {
  960. if (newReg != reg1)
  961. return rtgui_region_copy(newReg, reg1);
  962. return RTGUI_REGION_STATUS_SUCCESS;
  963. }
  964. /*
  965. * Region 2 completely subsumes region 1
  966. */
  967. if (!reg2->data && SUBSUMES(&reg2->extents, &reg1->extents))
  968. {
  969. if (newReg != reg2)
  970. return rtgui_region_copy(newReg, reg2);
  971. return RTGUI_REGION_STATUS_SUCCESS;
  972. }
  973. if (!rtgui_op(newReg, reg1, reg2, rtgui_region_unionO, RTGUI_REGION_STATUS_SUCCESS, RTGUI_REGION_STATUS_SUCCESS, &overlap))
  974. return RTGUI_REGION_STATUS_FAILURE;
  975. newReg->extents.x1 = RTGUI_MIN(reg1->extents.x1, reg2->extents.x1);
  976. newReg->extents.y1 = RTGUI_MIN(reg1->extents.y1, reg2->extents.y1);
  977. newReg->extents.x2 = RTGUI_MAX(reg1->extents.x2, reg2->extents.x2);
  978. newReg->extents.y2 = RTGUI_MAX(reg1->extents.y2, reg2->extents.y2);
  979. good(newReg);
  980. return RTGUI_REGION_STATUS_SUCCESS;
  981. }
  982. /*======================================================================
  983. * Batch Rectangle Union
  984. *====================================================================*/
  985. /*-
  986. *-----------------------------------------------------------------------
  987. * rtgui_region_append --
  988. *
  989. * "Append" the rgn rectangles onto the end of dstrgn, maintaining
  990. * knowledge of YX-banding when it's easy. Otherwise, dstrgn just
  991. * becomes a non-y-x-banded random collection of rectangles, and not
  992. * yet a true region. After a sequence of appends, the caller must
  993. * call rtgui_region_validate to ensure that a valid region is constructed.
  994. *
  995. * Results:
  996. * RTGUI_REGION_STATUS_SUCCESS if successful.
  997. *
  998. * Side Effects:
  999. * dstrgn is modified if rgn has rectangles.
  1000. *
  1001. */
  1002. rtgui_region_status_t
  1003. rtgui_region_append(rtgui_region_t *dstrgn,
  1004. rtgui_region_t *rgn)
  1005. {
  1006. int numRects, dnumRects, size;
  1007. rtgui_rect_t *new, *old;
  1008. int prepend;
  1009. if (PIXREGION_NAR(rgn)) return rtgui_break (dstrgn);
  1010. if (!rgn->data && (dstrgn->data == &rtgui_region_emptydata))
  1011. {
  1012. dstrgn->extents = rgn->extents;
  1013. dstrgn->data = (rtgui_region_data_t *)RT_NULL;
  1014. return RTGUI_REGION_STATUS_SUCCESS;
  1015. }
  1016. numRects = PIXREGION_NUM_RECTS(rgn);
  1017. if (!numRects)
  1018. return RTGUI_REGION_STATUS_SUCCESS;
  1019. prepend = RTGUI_REGION_STATUS_FAILURE;
  1020. size = numRects;
  1021. dnumRects = PIXREGION_NUM_RECTS(dstrgn);
  1022. if (!dnumRects && (size < 200))
  1023. size = 200; /* XXX pick numbers out of a hat */
  1024. RECTALLOC(dstrgn, size);
  1025. old = PIXREGION_RECTS(rgn);
  1026. if (!dnumRects)
  1027. dstrgn->extents = rgn->extents;
  1028. else if (dstrgn->extents.x2 > dstrgn->extents.x1)
  1029. {
  1030. rtgui_rect_t *first, *last;
  1031. first = old;
  1032. last = PIXREGION_BOXPTR(dstrgn) + (dnumRects - 1);
  1033. if ((first->y1 > last->y2) ||
  1034. ((first->y1 == last->y1) && (first->y2 == last->y2) &&
  1035. (first->x1 > last->x2)))
  1036. {
  1037. if (rgn->extents.x1 < dstrgn->extents.x1)
  1038. dstrgn->extents.x1 = rgn->extents.x1;
  1039. if (rgn->extents.x2 > dstrgn->extents.x2)
  1040. dstrgn->extents.x2 = rgn->extents.x2;
  1041. dstrgn->extents.y2 = rgn->extents.y2;
  1042. }
  1043. else
  1044. {
  1045. first = PIXREGION_BOXPTR(dstrgn);
  1046. last = old + (numRects - 1);
  1047. if ((first->y1 > last->y2) ||
  1048. ((first->y1 == last->y1) && (first->y2 == last->y2) &&
  1049. (first->x1 > last->x2)))
  1050. {
  1051. prepend = RTGUI_REGION_STATUS_SUCCESS;
  1052. if (rgn->extents.x1 < dstrgn->extents.x1)
  1053. dstrgn->extents.x1 = rgn->extents.x1;
  1054. if (rgn->extents.x2 > dstrgn->extents.x2)
  1055. dstrgn->extents.x2 = rgn->extents.x2;
  1056. dstrgn->extents.y1 = rgn->extents.y1;
  1057. }
  1058. else
  1059. dstrgn->extents.x2 = dstrgn->extents.x1;
  1060. }
  1061. }
  1062. if (prepend)
  1063. {
  1064. new = PIXREGION_BOX(dstrgn, numRects);
  1065. if (dnumRects == 1)
  1066. *new = *PIXREGION_BOXPTR(dstrgn);
  1067. else
  1068. rt_memmove((char *)new,(char *)PIXREGION_BOXPTR(dstrgn),
  1069. dnumRects * sizeof(rtgui_rect_t));
  1070. new = PIXREGION_BOXPTR(dstrgn);
  1071. }
  1072. else
  1073. new = PIXREGION_BOXPTR(dstrgn) + dnumRects;
  1074. if (numRects == 1)
  1075. *new = *old;
  1076. else
  1077. rt_memmove((char *)new, (char *)old, numRects * sizeof(rtgui_rect_t));
  1078. dstrgn->data->numRects += numRects;
  1079. return RTGUI_REGION_STATUS_SUCCESS;
  1080. }
  1081. #define ExchangeRects(a, b) \
  1082. { \
  1083. rtgui_rect_t t; \
  1084. t = rects[a]; \
  1085. rects[a] = rects[b]; \
  1086. rects[b] = t; \
  1087. }
  1088. static void QuickSortRects(rtgui_rect_t rects[], int numRects)
  1089. {
  1090. int y1;
  1091. int x1;
  1092. int i, j;
  1093. rtgui_rect_t *r;
  1094. /* Always called with numRects > 1 */
  1095. do
  1096. {
  1097. if (numRects == 2)
  1098. {
  1099. if (rects[0].y1 > rects[1].y1 ||
  1100. (rects[0].y1 == rects[1].y1 && rects[0].x1 > rects[1].x1))
  1101. ExchangeRects(0, 1);
  1102. return;
  1103. }
  1104. /* Choose partition element, stick in location 0 */
  1105. ExchangeRects(0, numRects >> 1);
  1106. y1 = rects[0].y1;
  1107. x1 = rects[0].x1;
  1108. /* Partition array */
  1109. i = 0;
  1110. j = numRects;
  1111. do
  1112. {
  1113. r = &(rects[i]);
  1114. do
  1115. {
  1116. r++;
  1117. i++;
  1118. }
  1119. while (i != numRects &&
  1120. (r->y1 < y1 || (r->y1 == y1 && r->x1 < x1)));
  1121. r = &(rects[j]);
  1122. do
  1123. {
  1124. r--;
  1125. j--;
  1126. }
  1127. while (y1 < r->y1 || (y1 == r->y1 && x1 < r->x1));
  1128. if (i < j)
  1129. ExchangeRects(i, j);
  1130. }
  1131. while (i < j);
  1132. /* Move partition element back to middle */
  1133. ExchangeRects(0, j);
  1134. /* Recurse */
  1135. if (numRects-j-1 > 1)
  1136. QuickSortRects(&rects[j+1], numRects-j-1);
  1137. numRects = j;
  1138. }
  1139. while (numRects > 1);
  1140. }
  1141. /*-
  1142. *-----------------------------------------------------------------------
  1143. * rtgui_region_validate --
  1144. *
  1145. * Take a ``region'' which is a non-y-x-banded random collection of
  1146. * rectangles, and compute a nice region which is the union of all the
  1147. * rectangles.
  1148. *
  1149. * Results:
  1150. * RTGUI_REGION_STATUS_SUCCESS if successful.
  1151. *
  1152. * Side Effects:
  1153. * The passed-in ``region'' may be modified.
  1154. * pOverlap set to RTGUI_REGION_STATUS_SUCCESS if any retangles overlapped, else RTGUI_REGION_STATUS_FAILURE;
  1155. *
  1156. * Strategy:
  1157. * Step 1. Sort the rectangles into ascending order with primary key y1
  1158. * and secondary key x1.
  1159. *
  1160. * Step 2. Split the rectangles into the RTGUI_MINimum number of proper y-x
  1161. * banded regions. This may require horizontally merging
  1162. * rectangles, and vertically coalescing bands. With any luck,
  1163. * this step in an identity transformation (ala the Box widget),
  1164. * or a coalescing into 1 box (ala Menus).
  1165. *
  1166. * Step 3. Merge the separate regions down to a single region by calling
  1167. * rtgui_region_union. Maximize the work each rtgui_region_union call does by using
  1168. * a binary merge.
  1169. *
  1170. *-----------------------------------------------------------------------
  1171. */
  1172. rtgui_region_status_t rtgui_region_validate(rtgui_region_t * badreg,
  1173. int *pOverlap)
  1174. {
  1175. /* Descriptor for regions under construction in Step 2. */
  1176. typedef struct
  1177. {
  1178. rtgui_region_t reg;
  1179. int prevBand;
  1180. int curBand;
  1181. }
  1182. RegionInfo;
  1183. int numRects; /* Original numRects for badreg */
  1184. RegionInfo *ri; /* Array of current regions */
  1185. int numRI; /* Number of entries used in ri */
  1186. int sizeRI; /* Number of entries available in ri */
  1187. int i; /* Index into rects */
  1188. int j; /* Index into ri */
  1189. RegionInfo *rit; /* &ri[j] */
  1190. rtgui_region_t * reg; /* ri[j].reg */
  1191. rtgui_rect_t * box; /* Current box in rects */
  1192. rtgui_rect_t * riBox; /* Last box in ri[j].reg */
  1193. rtgui_region_t * hreg; /* ri[j_half].reg */
  1194. rtgui_region_status_t ret = RTGUI_REGION_STATUS_SUCCESS;
  1195. *pOverlap = RTGUI_REGION_STATUS_FAILURE;
  1196. if (!badreg->data)
  1197. {
  1198. good(badreg);
  1199. return RTGUI_REGION_STATUS_SUCCESS;
  1200. }
  1201. numRects = badreg->data->numRects;
  1202. if (!numRects)
  1203. {
  1204. if (PIXREGION_NAR(badreg))
  1205. return RTGUI_REGION_STATUS_FAILURE;
  1206. good(badreg);
  1207. return RTGUI_REGION_STATUS_SUCCESS;
  1208. }
  1209. if (badreg->extents.x1 < badreg->extents.x2)
  1210. {
  1211. if ((numRects) == 1)
  1212. {
  1213. freeData(badreg);
  1214. badreg->data = (rtgui_region_data_t *) RT_NULL;
  1215. }
  1216. else
  1217. {
  1218. DOWNSIZE(badreg, numRects);
  1219. }
  1220. good(badreg);
  1221. return RTGUI_REGION_STATUS_SUCCESS;
  1222. }
  1223. /* Step 1: Sort the rects array into ascending (y1, x1) order */
  1224. QuickSortRects(PIXREGION_BOXPTR(badreg), numRects);
  1225. /* Step 2: Scatter the sorted array into the RTGUI_MINimum number of regions */
  1226. /* Set up the first region to be the first rectangle in badreg */
  1227. /* Note that step 2 code will never overflow the ri[0].reg rects array */
  1228. ri = (RegionInfo *) rtgui_malloc(4 * sizeof(RegionInfo));
  1229. if (!ri) return rtgui_break (badreg);
  1230. sizeRI = 4;
  1231. numRI = 1;
  1232. ri[0].prevBand = 0;
  1233. ri[0].curBand = 0;
  1234. ri[0].reg = *badreg;
  1235. box = PIXREGION_BOXPTR(&ri[0].reg);
  1236. ri[0].reg.extents = *box;
  1237. ri[0].reg.data->numRects = 1;
  1238. /* Now scatter rectangles into the RTGUI_MINimum set of valid regions. If the
  1239. next rectangle to be added to a region would force an existing rectangle
  1240. in the region to be split up in order to maintain y-x banding, just
  1241. forget it. Try the next region. If it doesn't fit cleanly into any
  1242. region, make a new one. */
  1243. for (i = numRects; --i > 0;)
  1244. {
  1245. box++;
  1246. /* Look for a region to append box to */
  1247. for (j = numRI, rit = ri; --j >= 0; rit++)
  1248. {
  1249. reg = &rit->reg;
  1250. riBox = PIXREGION_END(reg);
  1251. if (box->y1 == riBox->y1 && box->y2 == riBox->y2)
  1252. {
  1253. /* box is in same band as riBox. Merge or append it */
  1254. if (box->x1 <= riBox->x2)
  1255. {
  1256. /* Merge it with riBox */
  1257. if (box->x1 < riBox->x2) *pOverlap = RTGUI_REGION_STATUS_SUCCESS;
  1258. if (box->x2 > riBox->x2) riBox->x2 = box->x2;
  1259. }
  1260. else
  1261. {
  1262. RECTALLOC_BAIL(reg, 1, bail);
  1263. *PIXREGION_TOP(reg) = *box;
  1264. reg->data->numRects++;
  1265. }
  1266. goto NextRect; /* So sue me */
  1267. }
  1268. else if (box->y1 >= riBox->y2)
  1269. {
  1270. /* Put box into new band */
  1271. if (reg->extents.x2 < riBox->x2) reg->extents.x2 = riBox->x2;
  1272. if (reg->extents.x1 > box->x1) reg->extents.x1 = box->x1;
  1273. Coalesce(reg, rit->prevBand, rit->curBand);
  1274. rit->curBand = reg->data->numRects;
  1275. RECTALLOC_BAIL(reg, 1, bail);
  1276. *PIXREGION_TOP(reg) = *box;
  1277. reg->data->numRects++;
  1278. goto NextRect;
  1279. }
  1280. /* Well, this region was inappropriate. Try the next one. */
  1281. } /* for j */
  1282. /* Uh-oh. No regions were appropriate. Create a new one. */
  1283. if (sizeRI == numRI)
  1284. {
  1285. /* Oops, allocate space for new region information */
  1286. sizeRI <<= 1;
  1287. rit = (RegionInfo *) rt_realloc(ri, sizeRI * sizeof(RegionInfo));
  1288. if (!rit)
  1289. goto bail;
  1290. ri = rit;
  1291. rit = &ri[numRI];
  1292. }
  1293. numRI++;
  1294. rit->prevBand = 0;
  1295. rit->curBand = 0;
  1296. rit->reg.extents = *box;
  1297. rit->reg.data = (rtgui_region_data_t *)RT_NULL;
  1298. if (!rtgui_rect_alloc(&rit->reg, (i+numRI) / numRI)) /* MUST force allocation */
  1299. goto bail;
  1300. NextRect:
  1301. ;
  1302. } /* for i */
  1303. /* Make a final pass over each region in order to Coalesce and set
  1304. extents.x2 and extents.y2 */
  1305. for (j = numRI, rit = ri; --j >= 0; rit++)
  1306. {
  1307. reg = &rit->reg;
  1308. riBox = PIXREGION_END(reg);
  1309. reg->extents.y2 = riBox->y2;
  1310. if (reg->extents.x2 < riBox->x2) reg->extents.x2 = riBox->x2;
  1311. Coalesce(reg, rit->prevBand, rit->curBand);
  1312. if (reg->data->numRects == 1) /* keep unions happy below */
  1313. {
  1314. freeData(reg);
  1315. reg->data = (rtgui_region_data_t *)RT_NULL;
  1316. }
  1317. }
  1318. /* Step 3: Union all regions into a single region */
  1319. while (numRI > 1)
  1320. {
  1321. int half = numRI/2;
  1322. for (j = numRI & 1; j < (half + (numRI & 1)); j++)
  1323. {
  1324. reg = &ri[j].reg;
  1325. hreg = &ri[j+half].reg;
  1326. if (!rtgui_op(reg, reg, hreg, rtgui_region_unionO, RTGUI_REGION_STATUS_SUCCESS, RTGUI_REGION_STATUS_SUCCESS, pOverlap))
  1327. ret = RTGUI_REGION_STATUS_FAILURE;
  1328. if (hreg->extents.x1 < reg->extents.x1)
  1329. reg->extents.x1 = hreg->extents.x1;
  1330. if (hreg->extents.y1 < reg->extents.y1)
  1331. reg->extents.y1 = hreg->extents.y1;
  1332. if (hreg->extents.x2 > reg->extents.x2)
  1333. reg->extents.x2 = hreg->extents.x2;
  1334. if (hreg->extents.y2 > reg->extents.y2)
  1335. reg->extents.y2 = hreg->extents.y2;
  1336. freeData(hreg);
  1337. }
  1338. numRI -= half;
  1339. }
  1340. *badreg = ri[0].reg;
  1341. rtgui_free(ri);
  1342. good(badreg);
  1343. return ret;
  1344. bail:
  1345. for (i = 0; i < numRI; i++)
  1346. freeData(&ri[i].reg);
  1347. rtgui_free (ri);
  1348. return rtgui_break (badreg);
  1349. }
  1350. /*======================================================================
  1351. * Region Subtraction
  1352. *====================================================================*/
  1353. /*-
  1354. *-----------------------------------------------------------------------
  1355. * rtgui_region_subtractO --
  1356. * Overlapping band subtraction. x1 is the left-most point not yet
  1357. * checked.
  1358. *
  1359. * Results:
  1360. * RTGUI_REGION_STATUS_SUCCESS if successful.
  1361. *
  1362. * Side Effects:
  1363. * region may have rectangles added to it.
  1364. *
  1365. *-----------------------------------------------------------------------
  1366. */
  1367. /*ARGSUSED*/
  1368. static rtgui_region_status_t
  1369. rtgui_region_subtractO (
  1370. rtgui_region_t *region,
  1371. rtgui_rect_t *r1,
  1372. rtgui_rect_t *r1End,
  1373. rtgui_rect_t *r2,
  1374. rtgui_rect_t *r2End,
  1375. short y1,
  1376. short y2,
  1377. int *pOverlap)
  1378. {
  1379. rtgui_rect_t * pNextRect;
  1380. int x1;
  1381. x1 = r1->x1;
  1382. RT_ASSERT(y1<y2);
  1383. RT_ASSERT(r1 != r1End && r2 != r2End);
  1384. pNextRect = PIXREGION_TOP(region);
  1385. do
  1386. {
  1387. if (r2->x2 <= x1)
  1388. {
  1389. /*
  1390. * Subtrahend entirely to left of minuend: go to next subtrahend.
  1391. */
  1392. r2++;
  1393. }
  1394. else if (r2->x1 <= x1)
  1395. {
  1396. /*
  1397. * Subtrahend preceeds RTGUI_MINuend: nuke left edge of minuend.
  1398. */
  1399. x1 = r2->x2;
  1400. if (x1 >= r1->x2)
  1401. {
  1402. /*
  1403. * Minuend completely covered: advance to next minuend and
  1404. * reset left fence to edge of new RTGUI_MINuend.
  1405. */
  1406. r1++;
  1407. if (r1 != r1End)
  1408. x1 = r1->x1;
  1409. }
  1410. else
  1411. {
  1412. /*
  1413. * Subtrahend now used up since it doesn't extend beyond
  1414. * minuend
  1415. */
  1416. r2++;
  1417. }
  1418. }
  1419. else if (r2->x1 < r1->x2)
  1420. {
  1421. /*
  1422. * Left part of subtrahend covers part of minuend: add uncovered
  1423. * part of minuend to region and skip to next subtrahend.
  1424. */
  1425. RT_ASSERT(x1<r2->x1);
  1426. NEWRECT(region, pNextRect, x1, y1, r2->x1, y2);
  1427. x1 = r2->x2;
  1428. if (x1 >= r1->x2)
  1429. {
  1430. /*
  1431. * Minuend used up: advance to new...
  1432. */
  1433. r1++;
  1434. if (r1 != r1End)
  1435. x1 = r1->x1;
  1436. }
  1437. else
  1438. {
  1439. /*
  1440. * Subtrahend used up
  1441. */
  1442. r2++;
  1443. }
  1444. }
  1445. else
  1446. {
  1447. /*
  1448. * Minuend used up: add any remaining piece before advancing.
  1449. */
  1450. if (r1->x2 > x1)
  1451. NEWRECT(region, pNextRect, x1, y1, r1->x2, y2);
  1452. r1++;
  1453. if (r1 != r1End)
  1454. x1 = r1->x1;
  1455. }
  1456. }
  1457. while ((r1 != r1End) && (r2 != r2End));
  1458. /*
  1459. * Add remaining minuend rectangles to region.
  1460. */
  1461. while (r1 != r1End)
  1462. {
  1463. RT_ASSERT(x1<r1->x2);
  1464. NEWRECT(region, pNextRect, x1, y1, r1->x2, y2);
  1465. r1++;
  1466. if (r1 != r1End)
  1467. x1 = r1->x1;
  1468. }
  1469. return RTGUI_REGION_STATUS_SUCCESS;
  1470. }
  1471. /*-
  1472. *-----------------------------------------------------------------------
  1473. * rtgui_region_subtract --
  1474. * Subtract regS from regM and leave the result in regD.
  1475. * S stands for subtrahend, M for minuend and D for difference.
  1476. *
  1477. * Results:
  1478. * RTGUI_REGION_STATUS_SUCCESS if successful.
  1479. *
  1480. * Side Effects:
  1481. * regD is overwritten.
  1482. *
  1483. *-----------------------------------------------------------------------
  1484. */
  1485. rtgui_region_status_t
  1486. rtgui_region_subtract(rtgui_region_t *regD,
  1487. rtgui_region_t *regM,
  1488. rtgui_region_t *regS)
  1489. {
  1490. int overlap; /* result ignored */
  1491. good(regM);
  1492. good(regS);
  1493. good(regD);
  1494. /* check for trivial rejects */
  1495. if (PIXREGION_NIL(regM) || PIXREGION_NIL(regS) ||
  1496. !EXTENTCHECK(&regM->extents, &regS->extents))
  1497. {
  1498. if (PIXREGION_NAR (regS)) return rtgui_break (regD);
  1499. return rtgui_region_copy(regD, regM);
  1500. }
  1501. else if (regM == regS)
  1502. {
  1503. freeData(regD);
  1504. regD->extents.x2 = regD->extents.x1;
  1505. regD->extents.y2 = regD->extents.y1;
  1506. regD->data = &rtgui_region_emptydata;
  1507. return RTGUI_REGION_STATUS_SUCCESS;
  1508. }
  1509. /* Add those rectangles in region 1 that aren't in region 2,
  1510. do yucky substraction for overlaps, and
  1511. just throw away rectangles in region 2 that aren't in region 1 */
  1512. if (!rtgui_op(regD, regM, regS, rtgui_region_subtractO, RTGUI_REGION_STATUS_SUCCESS, RTGUI_REGION_STATUS_FAILURE, &overlap))
  1513. return RTGUI_REGION_STATUS_FAILURE;
  1514. /*
  1515. * Can't alter RegD's extents before we call rtgui_op because
  1516. * it might be one of the source regions and rtgui_op depends
  1517. * on the extents of those regions being unaltered. Besides, this
  1518. * way there's no checking against rectangles that will be nuked
  1519. * due to coalescing, so we have to exaRTGUI_MINe fewer rectangles.
  1520. */
  1521. rtgui_set_extents(regD);
  1522. good(regD);
  1523. return RTGUI_REGION_STATUS_SUCCESS;
  1524. }
  1525. rtgui_region_status_t
  1526. rtgui_region_subtract_rect (rtgui_region_t *regD, rtgui_region_t *regM, rtgui_rect_t* rect)
  1527. {
  1528. rtgui_region_t region;
  1529. region.data = RT_NULL;
  1530. region.extents.x1 = rect->x1;
  1531. region.extents.y1 = rect->y1;
  1532. region.extents.x2 = rect->x2;
  1533. region.extents.y2 = rect->y2;
  1534. return rtgui_region_subtract(regD, regM, &region);
  1535. }
  1536. /*======================================================================
  1537. * Region Inversion
  1538. *====================================================================*/
  1539. /*-
  1540. *-----------------------------------------------------------------------
  1541. * rtgui_region_inverse --
  1542. * Take a region and a box and return a region that is everything
  1543. * in the box but not in the region. The careful reader will note
  1544. * that this is the same as subtracting the region from the box...
  1545. *
  1546. * Results:
  1547. * RTGUI_REGION_STATUS_SUCCESS.
  1548. *
  1549. * Side Effects:
  1550. * newReg is overwritten.
  1551. *
  1552. *-----------------------------------------------------------------------
  1553. */
  1554. rtgui_region_status_t
  1555. rtgui_region_inverse(rtgui_region_t *newReg, /* Destination region */
  1556. rtgui_region_t *reg1, /* Region to invert */
  1557. rtgui_rect_t *invRect) /* Bounding box for inversion */
  1558. {
  1559. rtgui_region_t invReg; /* Quick and dirty region made from the
  1560. * bounding box */
  1561. int overlap; /* result ignored */
  1562. good(reg1);
  1563. good(newReg);
  1564. /* check for trivial rejects */
  1565. if (PIXREGION_NIL(reg1) || !EXTENTCHECK(invRect, &reg1->extents))
  1566. {
  1567. if (PIXREGION_NAR(reg1)) return rtgui_break (newReg);
  1568. newReg->extents = *invRect;
  1569. freeData(newReg);
  1570. newReg->data = (rtgui_region_data_t *)RT_NULL;
  1571. return RTGUI_REGION_STATUS_SUCCESS;
  1572. }
  1573. /* Add those rectangles in region 1 that aren't in region 2,
  1574. do yucky substraction for overlaps, and
  1575. just throw away rectangles in region 2 that aren't in region 1 */
  1576. invReg.extents = *invRect;
  1577. invReg.data = (rtgui_region_data_t *)RT_NULL;
  1578. if (!rtgui_op(newReg, &invReg, reg1, rtgui_region_subtractO, RTGUI_REGION_STATUS_SUCCESS, RTGUI_REGION_STATUS_FAILURE, &overlap))
  1579. return RTGUI_REGION_STATUS_FAILURE;
  1580. /*
  1581. * Can't alter newReg's extents before we call rtgui_op because
  1582. * it might be one of the source regions and rtgui_op depends
  1583. * on the extents of those regions being unaltered. Besides, this
  1584. * way there's no checking against rectangles that will be nuked
  1585. * due to coalescing, so we have to exaRTGUI_MINe fewer rectangles.
  1586. */
  1587. rtgui_set_extents(newReg);
  1588. good(newReg);
  1589. return RTGUI_REGION_STATUS_SUCCESS;
  1590. }
  1591. /*
  1592. * RectIn(region, rect)
  1593. * This routine takes a pointer to a region and a pointer to a box
  1594. * and deterRTGUI_MINes if the box is outside/inside/partly inside the region.
  1595. *
  1596. * The idea is to travel through the list of rectangles trying to cover the
  1597. * passed box with them. Anytime a piece of the rectangle isn't covered
  1598. * by a band of rectangles, partOut is set RTGUI_REGION_STATUS_SUCCESS. Any time a rectangle in
  1599. * the region covers part of the box, partIn is set RTGUI_REGION_STATUS_SUCCESS. The process ends
  1600. * when either the box has been completely covered (we reached a band that
  1601. * doesn't overlap the box, partIn is RTGUI_REGION_STATUS_SUCCESS and partOut is false), the
  1602. * box has been partially covered (partIn == partOut == RTGUI_REGION_STATUS_SUCCESS -- because of
  1603. * the banding, the first time this is true we know the box is only
  1604. * partially in the region) or is outside the region (we reached a band
  1605. * that doesn't overlap the box at all and partIn is false)
  1606. */
  1607. int rtgui_region_contains_rectangle(rtgui_region_t *region, rtgui_rect_t *prect)
  1608. {
  1609. int x;
  1610. int y;
  1611. rtgui_rect_t * pbox;
  1612. rtgui_rect_t * pboxEnd;
  1613. int partIn, partOut;
  1614. int numRects;
  1615. good(region);
  1616. numRects = PIXREGION_NUM_RECTS(region);
  1617. /* useful optimization */
  1618. if (!numRects || !EXTENTCHECK(&region->extents, prect))
  1619. return(RTGUI_REGION_OUT);
  1620. if (numRects == 1)
  1621. {
  1622. /* We know that it must be rgnIN or rgnPART */
  1623. if (SUBSUMES(&region->extents, prect))
  1624. return(RTGUI_REGION_IN);
  1625. else
  1626. return(RTGUI_REGION_PART);
  1627. }
  1628. partOut = RTGUI_REGION_STATUS_FAILURE;
  1629. partIn = RTGUI_REGION_STATUS_FAILURE;
  1630. /* (x,y) starts at upper left of rect, moving to the right and down */
  1631. x = prect->x1;
  1632. y = prect->y1;
  1633. /* can stop when both partOut and partIn are RTGUI_REGION_STATUS_SUCCESS, or we reach prect->y2 */
  1634. for (pbox = PIXREGION_BOXPTR(region), pboxEnd = pbox + numRects;
  1635. pbox != pboxEnd;
  1636. pbox++)
  1637. {
  1638. if (pbox->y2 <= y)
  1639. continue; /* getting up to speed or skipping remainder of band */
  1640. if (pbox->y1 > y)
  1641. {
  1642. partOut = RTGUI_REGION_STATUS_SUCCESS; /* missed part of rectangle above */
  1643. if (partIn || (pbox->y1 >= prect->y2))
  1644. break;
  1645. y = pbox->y1; /* x guaranteed to be == prect->x1 */
  1646. }
  1647. if (pbox->x2 <= x)
  1648. continue; /* not far enough over yet */
  1649. if (pbox->x1 > x)
  1650. {
  1651. partOut = RTGUI_REGION_STATUS_SUCCESS; /* missed part of rectangle to left */
  1652. if (partIn)
  1653. break;
  1654. }
  1655. if (pbox->x1 < prect->x2)
  1656. {
  1657. partIn = RTGUI_REGION_STATUS_SUCCESS; /* definitely overlap */
  1658. if (partOut)
  1659. break;
  1660. }
  1661. if (pbox->x2 >= prect->x2)
  1662. {
  1663. y = pbox->y2; /* finished with this band */
  1664. if (y >= prect->y2)
  1665. break;
  1666. x = prect->x1; /* reset x out to left again */
  1667. }
  1668. else
  1669. {
  1670. /*
  1671. * Because boxes in a band are maximal width, if the first box
  1672. * to overlap the rectangle doesn't completely cover it in that
  1673. * band, the rectangle must be partially out, since some of it
  1674. * will be uncovered in that band. partIn will have been set true
  1675. * by now...
  1676. */
  1677. partOut = RTGUI_REGION_STATUS_SUCCESS;
  1678. break;
  1679. }
  1680. }
  1681. return(partIn ? ((y < prect->y2) ? RTGUI_REGION_PART : RTGUI_REGION_IN) : RTGUI_REGION_OUT);
  1682. }
  1683. /* rtgui_region_translate (region, x, y)
  1684. translates in place
  1685. */
  1686. void rtgui_region_translate (rtgui_region_t *region, int x, int y)
  1687. {
  1688. int x1, x2, y1, y2;
  1689. int nbox;
  1690. rtgui_rect_t * pbox;
  1691. good(region);
  1692. region->extents.x1 = x1 = region->extents.x1 + x;
  1693. region->extents.y1 = y1 = region->extents.y1 + y;
  1694. region->extents.x2 = x2 = region->extents.x2 + x;
  1695. region->extents.y2 = y2 = region->extents.y2 + y;
  1696. if (((x1 - RTGUI_SHRT_MIN)|(y1 - RTGUI_SHRT_MIN)|(RTGUI_SHRT_MAX - x2)|(RTGUI_SHRT_MAX - y2)) >= 0)
  1697. {
  1698. nbox = region->data->numRects;
  1699. if (region->data && nbox)
  1700. {
  1701. for (pbox = PIXREGION_BOXPTR(region); nbox--; pbox++)
  1702. {
  1703. pbox->x1 += x;
  1704. pbox->y1 += y;
  1705. pbox->x2 += x;
  1706. pbox->y2 += y;
  1707. }
  1708. }
  1709. return;
  1710. }
  1711. if (((x2 - RTGUI_SHRT_MIN)|(y2 - RTGUI_SHRT_MIN)|(RTGUI_SHRT_MAX - x1)|(RTGUI_SHRT_MAX - y1)) <= 0)
  1712. {
  1713. region->extents.x2 = region->extents.x1;
  1714. region->extents.y2 = region->extents.y1;
  1715. freeData(region);
  1716. region->data = &rtgui_region_emptydata;
  1717. return;
  1718. }
  1719. if (x1 < RTGUI_SHRT_MIN)
  1720. region->extents.x1 = RTGUI_SHRT_MIN;
  1721. else if (x2 > RTGUI_SHRT_MAX)
  1722. region->extents.x2 = RTGUI_SHRT_MAX;
  1723. if (y1 < RTGUI_SHRT_MIN)
  1724. region->extents.y1 = RTGUI_SHRT_MIN;
  1725. else if (y2 > RTGUI_SHRT_MAX)
  1726. region->extents.y2 = RTGUI_SHRT_MAX;
  1727. nbox = region->data->numRects;
  1728. if (region->data && nbox)
  1729. {
  1730. rtgui_rect_t * pboxout;
  1731. for (pboxout = pbox = PIXREGION_BOXPTR(region); nbox--; pbox++)
  1732. {
  1733. pboxout->x1 = x1 = pbox->x1 + x;
  1734. pboxout->y1 = y1 = pbox->y1 + y;
  1735. pboxout->x2 = x2 = pbox->x2 + x;
  1736. pboxout->y2 = y2 = pbox->y2 + y;
  1737. if (((x2 - RTGUI_SHRT_MIN)|(y2 - RTGUI_SHRT_MIN)|
  1738. (RTGUI_SHRT_MAX - x1)|(RTGUI_SHRT_MAX - y1)) <= 0)
  1739. {
  1740. region->data->numRects--;
  1741. continue;
  1742. }
  1743. if (x1 < RTGUI_SHRT_MIN)
  1744. pboxout->x1 = RTGUI_SHRT_MIN;
  1745. else if (x2 > RTGUI_SHRT_MAX)
  1746. pboxout->x2 = RTGUI_SHRT_MAX;
  1747. if (y1 < RTGUI_SHRT_MIN)
  1748. pboxout->y1 = RTGUI_SHRT_MIN;
  1749. else if (y2 > RTGUI_SHRT_MAX)
  1750. pboxout->y2 = RTGUI_SHRT_MAX;
  1751. pboxout++;
  1752. }
  1753. if (pboxout != pbox)
  1754. {
  1755. if (region->data->numRects == 1)
  1756. {
  1757. region->extents = *PIXREGION_BOXPTR(region);
  1758. freeData(region);
  1759. region->data = (rtgui_region_data_t *)RT_NULL;
  1760. }
  1761. else
  1762. rtgui_set_extents(region);
  1763. }
  1764. }
  1765. }
  1766. void rtgui_region_reset(rtgui_region_t *region, rtgui_rect_t* rect)
  1767. {
  1768. good(region);
  1769. freeData(region);
  1770. rtgui_region_init_with_extents(region, rect);
  1771. }
  1772. /* box is "return" value */
  1773. int rtgui_region_contains_point(rtgui_region_t * region,
  1774. int x, int y,
  1775. rtgui_rect_t * box)
  1776. {
  1777. rtgui_rect_t *pbox, *pboxEnd;
  1778. int numRects;
  1779. good(region);
  1780. numRects = PIXREGION_NUM_RECTS(region);
  1781. if (!numRects || !INBOX(&region->extents, x, y))
  1782. return -RT_ERROR;
  1783. if (numRects == 1)
  1784. {
  1785. *box = region->extents;
  1786. return RT_EOK;
  1787. }
  1788. for (pbox = PIXREGION_BOXPTR(region), pboxEnd = pbox + numRects;
  1789. pbox != pboxEnd;
  1790. pbox++)
  1791. {
  1792. if (y >= pbox->y2)
  1793. continue; /* not there yet */
  1794. if ((y < pbox->y1) || (x < pbox->x1))
  1795. break; /* missed it */
  1796. if (x >= pbox->x2)
  1797. continue; /* not there yet */
  1798. *box = *pbox;
  1799. return RT_EOK;
  1800. }
  1801. return -RT_ERROR;
  1802. }
  1803. int rtgui_region_not_empty(rtgui_region_t * region)
  1804. {
  1805. good(region);
  1806. return(!PIXREGION_NIL(region));
  1807. }
  1808. void rtgui_region_empty(rtgui_region_t * region)
  1809. {
  1810. good(region);
  1811. freeData(region);
  1812. region->extents = rtgui_empty_rect;
  1813. region->data = &rtgui_region_emptydata;
  1814. }
  1815. rtgui_rect_t *rtgui_region_extents(rtgui_region_t * region)
  1816. {
  1817. good(region);
  1818. return(&region->extents);
  1819. }
  1820. #define ExchangeSpans(a, b) \
  1821. { \
  1822. rtgui_point_t tpt; \
  1823. int tw; \
  1824. \
  1825. tpt = spans[a]; spans[a] = spans[b]; spans[b] = tpt; \
  1826. tw = widths[a]; widths[a] = widths[b]; widths[b] = tw; \
  1827. }
  1828. /* ||| I should apply the merge sort code to rectangle sorting above, and see
  1829. if mapping time can be improved. But right now I've been at work 12 hours,
  1830. so forget it.
  1831. */
  1832. static void QuickSortSpans(
  1833. rtgui_point_t spans[],
  1834. int widths[],
  1835. int numSpans)
  1836. {
  1837. int y;
  1838. int i, j, m;
  1839. rtgui_point_t *r;
  1840. /* Always called with numSpans > 1 */
  1841. /* Sorts only by y, doesn't bother to sort by x */
  1842. do
  1843. {
  1844. if (numSpans < 9)
  1845. {
  1846. /* Do insertion sort */
  1847. int yprev;
  1848. yprev = spans[0].y;
  1849. i = 1;
  1850. do
  1851. { /* while i != numSpans */
  1852. y = spans[i].y;
  1853. if (yprev > y)
  1854. {
  1855. /* spans[i] is out of order. Move into proper location. */
  1856. rtgui_point_t tpt;
  1857. int tw, k;
  1858. for (j = 0; y >= spans[j].y; j++)
  1859. {}
  1860. tpt = spans[i];
  1861. tw = widths[i];
  1862. for (k = i; k != j; k--)
  1863. {
  1864. spans[k] = spans[k-1];
  1865. widths[k] = widths[k-1];
  1866. }
  1867. spans[j] = tpt;
  1868. widths[j] = tw;
  1869. y = spans[i].y;
  1870. } /* if out of order */
  1871. yprev = y;
  1872. i++;
  1873. }
  1874. while (i != numSpans);
  1875. return;
  1876. }
  1877. /* Choose partition element, stick in location 0 */
  1878. m = numSpans / 2;
  1879. if (spans[m].y > spans[0].y) ExchangeSpans(m, 0);
  1880. if (spans[m].y > spans[numSpans-1].y) ExchangeSpans(m, numSpans-1);
  1881. if (spans[m].y > spans[0].y) ExchangeSpans(m, 0);
  1882. y = spans[0].y;
  1883. /* Partition array */
  1884. i = 0;
  1885. j = numSpans;
  1886. do
  1887. {
  1888. r = &(spans[i]);
  1889. do
  1890. {
  1891. r++;
  1892. i++;
  1893. }
  1894. while (i != numSpans && r->y < y);
  1895. r = &(spans[j]);
  1896. do
  1897. {
  1898. r--;
  1899. j--;
  1900. }
  1901. while (y < r->y);
  1902. if (i < j)
  1903. ExchangeSpans(i, j);
  1904. }
  1905. while (i < j);
  1906. /* Move partition element back to middle */
  1907. ExchangeSpans(0, j);
  1908. /* Recurse */
  1909. if (numSpans-j-1 > 1)
  1910. QuickSortSpans(&spans[j+1], &widths[j+1], numSpans-j-1);
  1911. numSpans = j;
  1912. }
  1913. while (numSpans > 1);
  1914. }
  1915. void rtgui_region_dump(rtgui_region_t* region)
  1916. {
  1917. int num;
  1918. int i;
  1919. rtgui_rect_t * rects;
  1920. num = PIXREGION_NUM_RECTS(region);
  1921. rects = PIXREGION_RECTS(region);
  1922. rt_kprintf("extents: (%d,%d) (%d,%d)\n",
  1923. region->extents.x1, region->extents.y1,
  1924. region->extents.x2, region->extents.y2);
  1925. for (i = 0; i < num; i++)
  1926. {
  1927. rt_kprintf("box[%d]: (%d,%d) (%d,%d)\n", i,
  1928. rects[i].x1, rects[i].y1,
  1929. rects[i].x2, rects[i].y2);
  1930. }
  1931. }
  1932. void rtgui_rect_moveto(rtgui_rect_t *rect, int x, int y)
  1933. {
  1934. rect->x1 += x;
  1935. rect->x2 += x;
  1936. rect->y1 += y;
  1937. rect->y2 += y;
  1938. }
  1939. void rtgui_rect_moveto_align(rtgui_rect_t *rect, rtgui_rect_t *to, int align)
  1940. {
  1941. int dw, dh;
  1942. dw = 0;
  1943. dh = 0;
  1944. /* get delta width and height */
  1945. dw = rtgui_rect_width(*rect) - rtgui_rect_width(*to);
  1946. dh = rtgui_rect_height(*rect) - rtgui_rect_height(*to);
  1947. if (dw < 0) dw = 0;
  1948. if (dh < 0) dh = 0;
  1949. /* move to insider of rect */
  1950. rtgui_rect_moveto(to, rect->x1, rect->y1);
  1951. /* limited the destination rect to source rect */
  1952. if (dw == 0) to->x2 = rect->x2;
  1953. if (dh == 0) to->y2 = rect->y2;
  1954. /* align to right */
  1955. if (align & RTGUI_ALIGN_RIGHT)
  1956. {
  1957. to->x1 += dw;
  1958. to->x2 += dw;
  1959. }
  1960. /* align to bottom */
  1961. if (align & RTGUI_ALIGN_BOTTOM)
  1962. {
  1963. to->y1 += dh;
  1964. to->y2 += dh;
  1965. }
  1966. /* align to center horizontal */
  1967. if (align & RTGUI_ALIGN_CENTER_HORIZONTAL)
  1968. {
  1969. to->x1 += dw >> 1;
  1970. to->x2 += dw >> 1;
  1971. }
  1972. /* align to center vertical */
  1973. if (align & RTGUI_ALIGN_CENTER_VERTICAL)
  1974. {
  1975. to->y1 += dh >> 1;
  1976. to->y2 += dh >> 1;
  1977. }
  1978. }
  1979. void rtgui_rect_inflate(rtgui_rect_t *rect, int d)
  1980. {
  1981. rect->x1 -= d;
  1982. rect->x2 += d;
  1983. rect->y1 -= d;
  1984. rect->y2 += d;
  1985. }
  1986. /* put the intersect of src rect and dest rect to dest */
  1987. void rtgui_rect_intersect(rtgui_rect_t *src, rtgui_rect_t *dest)
  1988. {
  1989. if (dest->x1 < src->x1) dest->x1 = src->x1;
  1990. if (dest->y1 < src->y1) dest->y1 = src->y1;
  1991. if (dest->x2 > src->x2) dest->x2 = src->x2;
  1992. if (dest->y2 > src->y2) dest->y2 = src->y2;
  1993. }
  1994. int rtgui_rect_contains_point(const rtgui_rect_t *rect, int x, int y)
  1995. {
  1996. if (INBOX(rect, x, y)) return RT_EOK;
  1997. return -RT_ERROR;
  1998. }
  1999. int rtgui_rect_is_intersect(const rtgui_rect_t *rect1, const rtgui_rect_t *rect2)
  2000. {
  2001. if (INBOX(rect1, rect2->x1, rect2->y1) ||
  2002. INBOX(rect1, rect2->x1, rect2->y2) ||
  2003. INBOX(rect1, rect2->x2, rect2->y1) ||
  2004. INBOX(rect1, rect2->x2, rect2->y2))
  2005. {
  2006. return RT_EOK;
  2007. }
  2008. else if (INBOX(rect2, rect1->x1, rect1->y1) ||
  2009. INBOX(rect2, rect1->x1, rect1->y2) ||
  2010. INBOX(rect2, rect1->x2, rect1->y1) ||
  2011. INBOX(rect2, rect1->x2, rect1->y2))
  2012. {
  2013. return RT_EOK;
  2014. }
  2015. return -RT_ERROR;
  2016. }