region.c 70 KB

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