region.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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_move(&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_move(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_move);
  1909. void rtgui_rect_move_to_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_move_to_point);
  1920. void rtgui_rect_move_to_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_move_to_point(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 center horizontal */
  1942. else if (align & RTGUI_ALIGN_CENTER_HORIZONTAL)
  1943. {
  1944. to->x1 += dw >> 1;
  1945. to->x2 += dw >> 1;
  1946. }
  1947. /* align to bottom */
  1948. if (align & RTGUI_ALIGN_BOTTOM)
  1949. {
  1950. to->y1 += dh;
  1951. to->y2 += dh;
  1952. }
  1953. /* align to center vertical */
  1954. else if (align & RTGUI_ALIGN_CENTER_VERTICAL)
  1955. {
  1956. to->y1 += dh >> 1;
  1957. to->y2 += dh >> 1;
  1958. }
  1959. }
  1960. RTM_EXPORT(rtgui_rect_move_to_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);