Release 980913
[wine] / include / region.h
1 /*
2  * GDI region definitions
3  * Mainly taken from the X11 distribution.
4  * Modifications: Copyright 1998 Huw Davies
5  */
6
7 /************************************************************************
8
9 Copyright (c) 1987  X Consortium
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
24 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 Except as contained in this notice, the name of the X Consortium shall not be
29 used in advertising or otherwise to promote the sale, use or other dealings
30 in this Software without prior written authorization from the X Consortium.
31
32
33 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
34
35                         All Rights Reserved
36
37 Permission to use, copy, modify, and distribute this software and its 
38 documentation for any purpose and without fee is hereby granted, 
39 provided that the above copyright notice appear in all copies and that
40 both that copyright notice and this permission notice appear in 
41 supporting documentation, and that the name of Digital not be
42 used in advertising or publicity pertaining to distribution of the
43 software without specific, written prior permission.  
44
45 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
46 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
47 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
48 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
49 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
50 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 SOFTWARE.
52
53 ************************************************************************/
54 #ifndef __WINE_REGION_H
55 #define __WINE_REGION_H
56
57 #include "windows.h"
58 #include "gdi.h"
59
60
61 typedef struct {
62     INT32 size;
63     INT32 numRects;
64     INT32 type; /* NULL, SIMPLE or COMPLEX */
65     RECT32 *rects;
66     RECT32 extents;
67 } WINEREGION;
68
69   /* GDI logical region object */
70 typedef struct
71 {
72     GDIOBJHDR   header;
73     WINEREGION  *rgn;
74 } RGNOBJ;
75
76 /*  1 if two RECTs overlap.
77  *  0 if two RECTs do not overlap.
78  */
79 #define EXTENTCHECK(r1, r2) \
80         ((r1)->right > (r2)->left && \
81          (r1)->left < (r2)->right && \
82          (r1)->bottom > (r2)->top && \
83          (r1)->top < (r2)->bottom)
84
85 /*
86  *   Check to see if there is enough memory in the present region.
87  */
88 #define MEMCHECK(reg, rect, firstrect){\
89         if ((reg)->numRects >= ((reg)->size - 1)){\
90           (firstrect) = HeapReAlloc( SystemHeap, 0, \
91            (firstrect), (2 * (sizeof(RECT32)) * ((reg)->size)));\
92           if ((firstrect) == 0)\
93             return;\
94           (reg)->size *= 2;\
95           (rect) = &(firstrect)[(reg)->numRects];\
96          }\
97        }
98
99 #define EMPTY_REGION(pReg) { \
100     (pReg)->numRects = 0; \
101     (pReg)->extents.left = (pReg)->extents.top = 0; \
102     (pReg)->extents.right = (pReg)->extents.bottom = 0; \
103     (pReg)->type = NULLREGION; \
104  }
105
106 #define REGION_NOT_EMPTY(pReg) pReg->numRects
107
108 #define INRECT(r, x, y) \
109       ( ( ((r).right >  x)) && \
110         ( ((r).left <= x)) && \
111         ( ((r).bottom >  y)) && \
112         ( ((r).top <= y)) )
113
114
115 /*
116  * number of points to buffer before sending them off
117  * to scanlines() :  Must be an even number
118  */
119 #define NUMPTSTOBUFFER 200
120
121 /*
122  * used to allocate buffers for points and link
123  * the buffers together
124  */
125
126 typedef struct _POINTBLOCK {
127     POINT32 pts[NUMPTSTOBUFFER];
128     struct _POINTBLOCK *next;
129 } POINTBLOCK;
130
131
132
133 /*
134  *     This file contains a few macros to help track
135  *     the edge of a filled object.  The object is assumed
136  *     to be filled in scanline order, and thus the
137  *     algorithm used is an extension of Bresenham's line
138  *     drawing algorithm which assumes that y is always the
139  *     major axis.
140  *     Since these pieces of code are the same for any filled shape,
141  *     it is more convenient to gather the library in one
142  *     place, but since these pieces of code are also in
143  *     the inner loops of output primitives, procedure call
144  *     overhead is out of the question.
145  *     See the author for a derivation if needed.
146  */
147
148
149 /*
150  *  In scan converting polygons, we want to choose those pixels
151  *  which are inside the polygon.  Thus, we add .5 to the starting
152  *  x coordinate for both left and right edges.  Now we choose the
153  *  first pixel which is inside the pgon for the left edge and the
154  *  first pixel which is outside the pgon for the right edge.
155  *  Draw the left pixel, but not the right.
156  *
157  *  How to add .5 to the starting x coordinate:
158  *      If the edge is moving to the right, then subtract dy from the
159  *  error term from the general form of the algorithm.
160  *      If the edge is moving to the left, then add dy to the error term.
161  *
162  *  The reason for the difference between edges moving to the left
163  *  and edges moving to the right is simple:  If an edge is moving
164  *  to the right, then we want the algorithm to flip immediately.
165  *  If it is moving to the left, then we don't want it to flip until
166  *  we traverse an entire pixel.
167  */
168 #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
169     int dx;      /* local storage */ \
170 \
171     /* \
172      *  if the edge is horizontal, then it is ignored \
173      *  and assumed not to be processed.  Otherwise, do this stuff. \
174      */ \
175     if ((dy) != 0) { \
176         xStart = (x1); \
177         dx = (x2) - xStart; \
178         if (dx < 0) { \
179             m = dx / (dy); \
180             m1 = m - 1; \
181             incr1 = -2 * dx + 2 * (dy) * m1; \
182             incr2 = -2 * dx + 2 * (dy) * m; \
183             d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
184         } else { \
185             m = dx / (dy); \
186             m1 = m + 1; \
187             incr1 = 2 * dx - 2 * (dy) * m1; \
188             incr2 = 2 * dx - 2 * (dy) * m; \
189             d = -2 * m * (dy) + 2 * dx; \
190         } \
191     } \
192 }
193
194 #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
195     if (m1 > 0) { \
196         if (d > 0) { \
197             minval += m1; \
198             d += incr1; \
199         } \
200         else { \
201             minval += m; \
202             d += incr2; \
203         } \
204     } else {\
205         if (d >= 0) { \
206             minval += m1; \
207             d += incr1; \
208         } \
209         else { \
210             minval += m; \
211             d += incr2; \
212         } \
213     } \
214 }
215
216 /*
217  *     This structure contains all of the information needed
218  *     to run the bresenham algorithm.
219  *     The variables may be hardcoded into the declarations
220  *     instead of using this structure to make use of
221  *     register declarations.
222  */
223 typedef struct {
224     INT32 minor_axis;   /* minor axis        */
225     INT32 d;            /* decision variable */
226     INT32 m, m1;        /* slope and slope+1 */
227     INT32 incr1, incr2; /* error increments */
228 } BRESINFO;
229
230
231 #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
232         BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
233                      bres.m, bres.m1, bres.incr1, bres.incr2)
234
235 #define BRESINCRPGONSTRUCT(bres) \
236         BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
237
238
239
240 /*
241  *     These are the data structures needed to scan
242  *     convert regions.  Two different scan conversion
243  *     methods are available -- the even-odd method, and
244  *     the winding number method.
245  *     The even-odd rule states that a point is inside
246  *     the polygon if a ray drawn from that point in any
247  *     direction will pass through an odd number of
248  *     path segments.
249  *     By the winding number rule, a point is decided
250  *     to be inside the polygon if a ray drawn from that
251  *     point in any direction passes through a different
252  *     number of clockwise and counter-clockwise path
253  *     segments.
254  *
255  *     These data structures are adapted somewhat from
256  *     the algorithm in (Foley/Van Dam) for scan converting
257  *     polygons.
258  *     The basic algorithm is to start at the top (smallest y)
259  *     of the polygon, stepping down to the bottom of
260  *     the polygon by incrementing the y coordinate.  We
261  *     keep a list of edges which the current scanline crosses,
262  *     sorted by x.  This list is called the Active Edge Table (AET)
263  *     As we change the y-coordinate, we update each entry in 
264  *     in the active edge table to reflect the edges new xcoord.
265  *     This list must be sorted at each scanline in case
266  *     two edges intersect.
267  *     We also keep a data structure known as the Edge Table (ET),
268  *     which keeps track of all the edges which the current
269  *     scanline has not yet reached.  The ET is basically a
270  *     list of ScanLineList structures containing a list of
271  *     edges which are entered at a given scanline.  There is one
272  *     ScanLineList per scanline at which an edge is entered.
273  *     When we enter a new edge, we move it from the ET to the AET.
274  *
275  *     From the AET, we can implement the even-odd rule as in
276  *     (Foley/Van Dam).
277  *     The winding number rule is a little trickier.  We also
278  *     keep the EdgeTableEntries in the AET linked by the
279  *     nextWETE (winding EdgeTableEntry) link.  This allows
280  *     the edges to be linked just as before for updating
281  *     purposes, but only uses the edges linked by the nextWETE
282  *     link as edges representing spans of the polygon to
283  *     drawn (as with the even-odd rule).
284  */
285
286 /*
287  * for the winding number rule
288  */
289 #define CLOCKWISE          1
290 #define COUNTERCLOCKWISE  -1 
291
292 typedef struct _EdgeTableEntry {
293      INT32 ymax;           /* ycoord at which we exit this edge. */
294      BRESINFO bres;        /* Bresenham info to run the edge     */
295      struct _EdgeTableEntry *next;       /* next in the list     */
296      struct _EdgeTableEntry *back;       /* for insertion sort   */
297      struct _EdgeTableEntry *nextWETE;   /* for winding num rule */
298      int ClockWise;        /* flag for winding number rule       */
299 } EdgeTableEntry;
300
301
302 typedef struct _ScanLineList{
303      INT32 scanline;            /* the scanline represented */
304      EdgeTableEntry *edgelist;  /* header node              */
305      struct _ScanLineList *next;  /* next in the list       */
306 } ScanLineList;
307
308
309 typedef struct {
310      INT32 ymax;               /* ymax for the polygon     */
311      INT32 ymin;               /* ymin for the polygon     */
312      ScanLineList scanlines;   /* header node              */
313 } EdgeTable;
314
315
316 /*
317  * Here is a struct to help with storage allocation
318  * so we can allocate a big chunk at a time, and then take
319  * pieces from this heap when we need to.
320  */
321 #define SLLSPERBLOCK 25
322
323 typedef struct _ScanLineListBlock {
324      ScanLineList SLLs[SLLSPERBLOCK];
325      struct _ScanLineListBlock *next;
326 } ScanLineListBlock;
327
328
329 /*
330  *
331  *     a few macros for the inner loops of the fill code where
332  *     performance considerations don't allow a procedure call.
333  *
334  *     Evaluate the given edge at the given scanline.
335  *     If the edge has expired, then we leave it and fix up
336  *     the active edge table; otherwise, we increment the
337  *     x value to be ready for the next scanline.
338  *     The winding number rule is in effect, so we must notify
339  *     the caller when the edge has been removed so he
340  *     can reorder the Winding Active Edge Table.
341  */
342 #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
343    if (pAET->ymax == y) {          /* leaving this edge */ \
344       pPrevAET->next = pAET->next; \
345       pAET = pPrevAET->next; \
346       fixWAET = 1; \
347       if (pAET) \
348          pAET->back = pPrevAET; \
349    } \
350    else { \
351       BRESINCRPGONSTRUCT(pAET->bres); \
352       pPrevAET = pAET; \
353       pAET = pAET->next; \
354    } \
355 }
356
357
358 /*
359  *     Evaluate the given edge at the given scanline.
360  *     If the edge has expired, then we leave it and fix up
361  *     the active edge table; otherwise, we increment the
362  *     x value to be ready for the next scanline.
363  *     The even-odd rule is in effect.
364  */
365 #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
366    if (pAET->ymax == y) {          /* leaving this edge */ \
367       pPrevAET->next = pAET->next; \
368       pAET = pPrevAET->next; \
369       if (pAET) \
370          pAET->back = pPrevAET; \
371    } \
372    else { \
373       BRESINCRPGONSTRUCT(pAET->bres); \
374       pPrevAET = pAET; \
375       pAET = pAET->next; \
376    } \
377 }
378
379 extern BOOL32 REGION_DeleteObject( HRGN32 hrgn, RGNOBJ * obj );
380 extern BOOL32 REGION_UnionRectWithRgn( HRGN32 hrgn, const RECT32 *lpRect );
381 extern BOOL32 REGION_FrameRgn( HRGN32 dest, HRGN32 src, INT32 x, INT32 y );
382 extern BOOL32 REGION_LPTODP( HDC32 hdc, HRGN32 hDest, HRGN32 hSrc );
383
384 #endif  /* __WINE_REGION_H */
385
386