region.c 70 KB

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