2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998 - 2004 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: gradient.c,v 1.9 2004/05/10 17:07:17 weiden Exp $
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * PURPOSE: GDI Driver Gradient Functions
24 * FILE: subsys/win32k/eng/gradient.c
25 * PROGRAMER: Thomas Weidenmueller
31 /* MACROS *********************************************************************/
33 const LONG LINC
[2] = {-1, 1};
35 #define VERTEX(n) (pVertex + gt->n)
36 #define COMPAREVERTEX(a, b) ((a)->x == (b)->x && (a)->y == (b)->y)
38 #define VCMPCLR(a,b,c,color) (a->color != b->color || a->color != c->color)
39 #define VCMPCLRS(a,b,c) \
40 !(!VCMPCLR(a,b,c,Red) || !VCMPCLR(a,b,c,Green) || !VCMPCLR(a,b,c,Blue))
42 #define MOVERECT(r,x,y) \
43 r.left += x; r.right += x; \
44 r.top += y; r.bottom += y
47 /* Horizontal/Vertical gradients */
48 #define HVINITCOL(Col, id) \
49 c[id] = v1->Col >> 8; \
50 dc[id] = abs((v2->Col >> 8) - c[id]); \
51 ec[id] = -(dy >> 1); \
52 ic[id] = LINC[(v2->Col >> 8) > c[id]]
53 #define HVSTEPCOL(id) \
61 /* FUNCTIONS ******************************************************************/
64 IntEngGradientFillRect(
68 IN TRIVERTEX
*pVertex
,
70 IN PGRADIENT_RECT gRect
,
71 IN RECTL
*prclExtents
,
72 IN POINTL
*pptlDitherOrg
,
78 RECTL rcGradient
, rcSG
;
83 INTENG_ENTER_LEAVE EnterLeave
;
84 LONG y
, dy
, c
[3], dc
[3], ec
[3], ic
[3];
86 v1
= (pVertex
+ gRect
->UpperLeft
);
87 v2
= (pVertex
+ gRect
->LowerRight
);
89 rcGradient
.left
= min(v1
->x
, v2
->x
);
90 rcGradient
.right
= max(v1
->x
, v2
->x
);
91 rcGradient
.top
= min(v1
->y
, v2
->y
);
92 rcGradient
.bottom
= max(v1
->y
, v2
->y
);
94 MOVERECT(rcSG
, pptlDitherOrg
->x
, pptlDitherOrg
->y
);
98 dy
= abs(rcGradient
.right
- rcGradient
.left
);
102 dy
= abs(rcGradient
.bottom
- rcGradient
.top
);
105 if(!IntEngEnter(&EnterLeave
, psoDest
, &rcSG
, FALSE
, &Translate
, &OutputObj
))
109 OutputGDI
= AccessInternalObjectFromUserObject(OutputObj
);
111 if((v1
->Red
!= v2
->Red
|| v1
->Green
!= v2
->Green
|| v1
->Blue
!= v2
->Blue
) && dy
> 1)
113 CLIPOBJ_cEnumStart(pco
, FALSE
, CT_RECTANGLES
, CD_RIGHTDOWN
, 0);
121 EnumMore
= CLIPOBJ_bEnum(pco
, (ULONG
) sizeof(RectEnum
), (PVOID
) &RectEnum
);
122 for (i
= 0; i
< RectEnum
.c
&& RectEnum
.arcl
[i
].top
<= rcSG
.bottom
; i
++)
124 if(NtGdiIntersectRect(&FillRect
, (PRECT
)&RectEnum
.arcl
[i
], (PRECT
)&rcSG
))
130 for(y
= rcSG
.left
; y
< FillRect
.right
; y
++)
132 if(y
>= FillRect
.left
)
134 Color
= XLATEOBJ_iXlate(pxlo
, RGB(c
[0], c
[1], c
[2]));
135 OutputGDI
->DIB_VLine(OutputObj
, y
, FillRect
.top
, FillRect
.bottom
, Color
);
148 EnumMore
= CLIPOBJ_bEnum(pco
, (ULONG
) sizeof(RectEnum
), (PVOID
) &RectEnum
);
149 for (i
= 0; i
< RectEnum
.c
&& RectEnum
.arcl
[i
].top
<= rcSG
.bottom
; i
++)
151 if(NtGdiIntersectRect(&FillRect
, (PRECT
)&RectEnum
.arcl
[i
], (PRECT
)&rcSG
))
157 for(y
= rcSG
.top
; y
< FillRect
.bottom
; y
++)
159 if(y
>= FillRect
.top
)
161 Color
= XLATEOBJ_iXlate(pxlo
, RGB(c
[0], c
[1], c
[2]));
162 OutputGDI
->DIB_HLine(OutputObj
, FillRect
.left
, FillRect
.right
, y
, Color
);
173 return IntEngLeave(&EnterLeave
);
176 /* rectangle has only one color, no calculation required */
177 CLIPOBJ_cEnumStart(pco
, FALSE
, CT_RECTANGLES
, CD_RIGHTDOWN
, 0);
181 ULONG Color
= XLATEOBJ_iXlate(pxlo
, RGB(v1
->Red
, v1
->Green
, v1
->Blue
));
183 EnumMore
= CLIPOBJ_bEnum(pco
, (ULONG
) sizeof(RectEnum
), (PVOID
) &RectEnum
);
184 for (i
= 0; i
< RectEnum
.c
&& RectEnum
.arcl
[i
].top
<= rcSG
.bottom
; i
++)
186 if(NtGdiIntersectRect(&FillRect
, (PRECT
)&RectEnum
.arcl
[i
], (PRECT
)&rcSG
))
188 for(; FillRect
.top
< FillRect
.bottom
; FillRect
.top
++)
190 OutputGDI
->DIB_HLine(OutputObj
, FillRect
.left
, FillRect
.right
, FillRect
.top
, Color
);
196 return IntEngLeave(&EnterLeave
);
199 /* Fill triangle with solid color */
200 #define S_FILLLINE(linefrom,lineto) \
201 if(sx[lineto] < sx[linefrom]) \
202 OutputGDI->DIB_HLine(OutputObj, max(sx[lineto], FillRect.left), min(sx[linefrom], FillRect.right), sy, Color); \
204 OutputGDI->DIB_HLine(OutputObj, max(sx[linefrom], FillRect.left), min(sx[lineto], FillRect.right), sy, Color);
205 #define S_DOLINE(a,b,line) \
206 ex[line] += dx[line]; \
207 while(ex[line] > 0 && x[line] != destx[line]) \
209 x[line] += incx[line]; \
210 sx[line] += incx[line]; \
211 ex[line] -= dy[line]; \
213 #define S_GOLINE(a,b,line) \
214 if(y >= a->y && y <= b->y) \
216 #define S_ENDLINE(a,b,line) \
218 #define S_INITLINE(a,b,line) \
220 sx[line] = a->x + pptlDitherOrg->x; \
221 dx[line] = abs(b->x - a->x); \
222 dy[line] = abs(b->y - a->y); \
223 incx[line] = LINC[b->x > a->x]; \
224 ex[line] = -(dy[line]>>1); \
227 /* Fill triangle with gradient */
228 #define INITCOL(a,b,line,col,id) \
229 c[line][id] = a->col >> 8; \
230 dc[line][id] = abs((b->col >> 8) - c[line][id]); \
231 ec[line][id] = -(dy[line]>>1); \
232 ic[line][id] = LINC[(b->col >> 8) > c[line][id]]
233 #define STEPCOL(a,b,line,col,id) \
234 ec[line][id] += dc[line][id]; \
235 while(ec[line][id] > 0) \
237 c[line][id] += ic[line][id]; \
238 ec[line][id] -= dy[line]; \
240 #define FINITCOL(linefrom,lineto,colid) \
241 gc[colid] = c[linefrom][colid]; \
242 gd[colid] = abs(c[lineto][colid] - gc[colid]); \
243 ge[colid] = -(gx >> 1); \
244 gi[colid] = LINC[c[lineto][colid] > gc[colid]]
245 #define FDOCOL(linefrom,lineto,colid) \
246 ge[colid] += gd[colid]; \
247 while(ge[colid] > 0) \
249 gc[colid] += gi[colid]; \
252 #define FILLLINE(linefrom,lineto) \
253 gx = abs(sx[lineto] - sx[linefrom]); \
254 gxi = LINC[sx[linefrom] < sx[lineto]]; \
255 FINITCOL(linefrom, lineto, 0); \
256 FINITCOL(linefrom, lineto, 1); \
257 FINITCOL(linefrom, lineto, 2); \
258 for(g = sx[linefrom]; g != sx[lineto]; g += gxi) \
260 if(InY && g >= FillRect.left && g < FillRect.right) \
262 Color = XLATEOBJ_iXlate(pxlo, RGB(gc[0], gc[1], gc[2])); \
263 OutputGDI->DIB_PutPixel(OutputObj, g, sy, Color); \
265 FDOCOL(linefrom, lineto, 0); \
266 FDOCOL(linefrom, lineto, 1); \
267 FDOCOL(linefrom, lineto, 2); \
269 #define DOLINE(a,b,line) \
270 STEPCOL(a, b, line, Red, 0); \
271 STEPCOL(a, b, line, Green, 1); \
272 STEPCOL(a, b, line, Blue, 2); \
273 ex[line] += dx[line]; \
274 while(ex[line] > 0 && x[line] != destx[line]) \
276 x[line] += incx[line]; \
277 sx[line] += incx[line]; \
278 ex[line] -= dy[line]; \
280 #define GOLINE(a,b,line) \
281 if(y >= a->y && y <= b->y) \
283 #define ENDLINE(a,b,line) \
285 #define INITLINE(a,b,line) \
287 sx[line] = a->x + pptlDitherOrg->x; \
288 dx[line] = abs(b->x - a->x); \
289 dy[line] = abs(b->y - a->y); \
290 incx[line] = LINC[b->x > a->x]; \
291 ex[line] = -(dy[line]>>1); \
293 #define DOINIT(a, b, line) \
294 INITLINE(a, b, line); \
295 INITCOL(a, b, line, Red, 0); \
296 INITCOL(a, b, line, Green, 1); \
297 INITCOL(a, b, line, Blue, 2);
298 #define SMALLER(a,b) (a->y < b->y) || (a->y == b->y && a->x < b->x)
299 #define SWAP(a,b,c) c = a;\
304 IntEngGradientFillTriangle(
308 IN TRIVERTEX
*pVertex
,
310 IN PGRADIENT_TRIANGLE gTriangle
,
311 IN RECTL
*prclExtents
,
312 IN POINTL
*pptlDitherOrg
)
316 PTRIVERTEX v1
, v2
, v3
;
321 INTENG_ENTER_LEAVE EnterLeave
;
326 LONG x
[NLINES
], dx
[NLINES
], dy
[NLINES
], incx
[NLINES
], ex
[NLINES
], destx
[NLINES
];
327 LONG c
[NLINES
][3], dc
[NLINES
][3], ec
[NLINES
][3], ic
[NLINES
][3]; /* colors on lines */
328 LONG g
, gx
, gxi
, gc
[3], gd
[3], ge
[3], gi
[3]; /* colors in triangle */
331 v1
= (pVertex
+ gTriangle
->Vertex1
);
332 v2
= (pVertex
+ gTriangle
->Vertex2
);
333 v3
= (pVertex
+ gTriangle
->Vertex3
);
351 DbgPrint("Triangle: (%i,%i) (%i,%i) (%i,%i)\n", v1
->x
, v1
->y
, v2
->x
, v2
->y
, v3
->x
, v3
->y
);
353 if(!IntEngEnter(&EnterLeave
, psoDest
, &FillRect
, FALSE
, &Translate
, &OutputObj
))
357 OutputGDI
= AccessInternalObjectFromUserObject(OutputObj
);
359 if(VCMPCLRS(v1
, v2
, v3
))
361 CLIPOBJ_cEnumStart(pco
, FALSE
, CT_RECTANGLES
, CD_RIGHTDOWN
, 0);
364 EnumMore
= CLIPOBJ_bEnum(pco
, (ULONG
) sizeof(RectEnum
), (PVOID
) &RectEnum
);
365 for (i
= 0; i
< RectEnum
.c
&& RectEnum
.arcl
[i
].top
<= prclExtents
->bottom
; i
++)
367 if(NtGdiIntersectRect((PRECT
)&FillRect
, (PRECT
)&RectEnum
.arcl
[i
], (PRECT
)prclExtents
))
376 sy
= v1
->y
+ pptlDitherOrg
->y
;
377 bt
= min(v3
->y
+ pptlDitherOrg
->y
, FillRect
.bottom
);
381 InY
= !(sy
< FillRect
.top
|| sy
>= FillRect
.bottom
);
403 return IntEngLeave(&EnterLeave
);
406 /* fill triangle with one solid color */
408 Color
= XLATEOBJ_iXlate(pxlo
, RGB(v1
->Red
>> 8, v1
->Green
>> 8, v1
->Blue
>> 8));
409 CLIPOBJ_cEnumStart(pco
, FALSE
, CT_RECTANGLES
, CD_RIGHTDOWN
, 0);
412 EnumMore
= CLIPOBJ_bEnum(pco
, (ULONG
) sizeof(RectEnum
), (PVOID
) &RectEnum
);
413 for (i
= 0; i
< RectEnum
.c
&& RectEnum
.arcl
[i
].top
<= prclExtents
->bottom
; i
++)
415 if(NtGdiIntersectRect((PRECT
)&FillRect
, (PRECT
)&RectEnum
.arcl
[i
], (PRECT
)prclExtents
))
417 S_INITLINE(v1
, v3
, 0);
418 S_INITLINE(v1
, v2
, 1);
419 S_INITLINE(v2
, v3
, 2);
422 sy
= v1
->y
+ pptlDitherOrg
->y
;
423 bt
= min(v3
->y
+ pptlDitherOrg
->y
, FillRect
.bottom
);
429 S_ENDLINE(v1
, v3
, 0);
434 S_ENDLINE(v1
, v2
, 1);
439 S_ENDLINE(23, v3
, 2);
448 return IntEngLeave(&EnterLeave
);
453 IntEngIsNULLTriangle(TRIVERTEX
*pVertex
, GRADIENT_TRIANGLE
*gt
)
455 if(COMPAREVERTEX(VERTEX(Vertex1
), VERTEX(Vertex2
)))
457 if(COMPAREVERTEX(VERTEX(Vertex1
), VERTEX(Vertex3
)))
459 if(COMPAREVERTEX(VERTEX(Vertex2
), VERTEX(Vertex3
)))
470 IN TRIVERTEX
*pVertex
,
474 IN RECTL
*prclExtents
,
475 IN POINTL
*pptlDitherOrg
,
482 case GRADIENT_FILL_RECT_H
:
483 case GRADIENT_FILL_RECT_V
:
485 PGRADIENT_RECT gr
= (PGRADIENT_RECT
)pMesh
;
486 for(i
= 0; i
< nMesh
; i
++, gr
++)
488 if(!IntEngGradientFillRect(psoDest
, pco
, pxlo
, pVertex
, nVertex
, gr
, prclExtents
,
489 pptlDitherOrg
, (ulMode
== GRADIENT_FILL_RECT_H
)))
496 case GRADIENT_FILL_TRIANGLE
:
498 PGRADIENT_TRIANGLE gt
= (PGRADIENT_TRIANGLE
)pMesh
;
499 for(i
= 0; i
< nMesh
; i
++, gt
++)
501 if(IntEngIsNULLTriangle(pVertex
, gt
))
503 /* skip empty triangles */
506 if(!IntEngGradientFillTriangle(psoDest
, pco
, pxlo
, pVertex
, nVertex
, gt
, prclExtents
,
523 IN TRIVERTEX
*pVertex
,
527 IN RECTL
*prclExtents
,
528 IN POINTL
*pptlDitherOrg
,
534 SurfGDI
= (SURFGDI
*)AccessInternalObjectFromUserObject(psoDest
);
535 MouseSafetyOnDrawStart(psoDest
, SurfGDI
, pco
->rclBounds
.left
, pco
->rclBounds
.top
,
536 pco
->rclBounds
.right
, pco
->rclBounds
.bottom
);
537 if((psoDest
->iType
!= STYPE_BITMAP
) && SurfGDI
->GradientFill
)
539 IntLockGDIDriver(SurfGDI
);
540 Ret
= SurfGDI
->GradientFill(psoDest
, pco
, pxlo
, pVertex
, nVertex
, pMesh
, nMesh
,
541 prclExtents
, pptlDitherOrg
, ulMode
);
542 IntUnLockGDIDriver(SurfGDI
);
543 MouseSafetyOnDrawEnd(psoDest
, SurfGDI
);
546 Ret
= EngGradientFill(psoDest
, pco
, pxlo
, pVertex
, nVertex
, pMesh
, nMesh
, prclExtents
,
547 pptlDitherOrg
, ulMode
);
550 /* Dummy BitBlt to let driver know that something has changed.
551 0x00AA0029 is the Rop for D (no-op) */
554 IntLockGDIDriver(SurfGDI
);
555 SurfGDI
->BitBlt(psoDest
, NULL
, NULL
, pco
, pxlo
,
556 prclExtents
, pptlDitherOrg
, NULL
, NULL
, NULL
, ROP_NOOP
);
557 IntUnLockGDIDriver(SurfGDI
);
558 MouseSafetyOnDrawEnd(psoDest
, SurfGDI
);
561 EngBitBlt(psoDest
, NULL
, NULL
, pco
, pxlo
,
562 prclExtents
, pptlDitherOrg
, NULL
, NULL
, NULL
, ROP_NOOP
);
564 MouseSafetyOnDrawEnd(psoDest
, SurfGDI
);