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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
35 /* MACROS *********************************************************************/
37 const LONG LINC
[2] = {-1, 1};
39 #define VERTEX(n) (pVertex + gt->n)
40 #define COMPAREVERTEX(a, b) ((a)->x == (b)->x && (a)->y == (b)->y)
42 #define VCMPCLR(a,b,c,color) (a->color != b->color || a->color != c->color)
43 #define VCMPCLRS(a,b,c) \
44 !(!VCMPCLR(a,b,c,Red) || !VCMPCLR(a,b,c,Green) || !VCMPCLR(a,b,c,Blue))
46 /* Horizontal/Vertical gradients */
47 #define HVINITCOL(Col, id) \
48 c[id] = v1->Col >> 8; \
49 dc[id] = abs((v2->Col >> 8) - c[id]); \
50 ec[id] = -(dy >> 1); \
51 ic[id] = LINC[(v2->Col >> 8) > c[id]]
52 #define HVSTEPCOL(id) \
60 /* FUNCTIONS ******************************************************************/
63 IntEngGradientFillRect(
67 IN TRIVERTEX
*pVertex
,
69 IN PGRADIENT_RECT gRect
,
70 IN RECTL
*prclExtents
,
71 IN POINTL
*pptlDitherOrg
,
76 RECTL rcGradient
, rcSG
;
81 INTENG_ENTER_LEAVE EnterLeave
;
82 LONG y
, dy
, c
[3], dc
[3], ec
[3], ic
[3];
84 v1
= (pVertex
+ gRect
->UpperLeft
);
85 v2
= (pVertex
+ gRect
->LowerRight
);
87 rcGradient
.left
= min(v1
->x
, v2
->x
);
88 rcGradient
.right
= max(v1
->x
, v2
->x
);
89 rcGradient
.top
= min(v1
->y
, v2
->y
);
90 rcGradient
.bottom
= max(v1
->y
, v2
->y
);
92 RECTL_vOffsetRect(&rcSG
, pptlDitherOrg
->x
, pptlDitherOrg
->y
);
96 dy
= abs(rcGradient
.right
- rcGradient
.left
);
100 dy
= abs(rcGradient
.bottom
- rcGradient
.top
);
103 if(!IntEngEnter(&EnterLeave
, psoDest
, &rcSG
, FALSE
, &Translate
, &psoOutput
))
108 if((v1
->Red
!= v2
->Red
|| v1
->Green
!= v2
->Green
|| v1
->Blue
!= v2
->Blue
) && dy
> 1)
110 CLIPOBJ_cEnumStart(pco
, FALSE
, CT_RECTANGLES
, CD_RIGHTDOWN
, 0);
118 EnumMore
= CLIPOBJ_bEnum(pco
, (ULONG
) sizeof(RectEnum
), (PVOID
) &RectEnum
);
119 for (i
= 0; i
< RectEnum
.c
&& RectEnum
.arcl
[i
].top
<= rcSG
.bottom
; i
++)
121 if(RECTL_bIntersectRect(&FillRect
, &RectEnum
.arcl
[i
], &rcSG
))
127 for(y
= rcSG
.left
; y
< FillRect
.right
; y
++)
129 if(y
>= FillRect
.left
)
131 Color
= XLATEOBJ_iXlate(pxlo
, RGB(c
[0], c
[1], c
[2]));
132 DibFunctionsForBitmapFormat
[psoOutput
->iBitmapFormat
].DIB_VLine(
133 psoOutput
, y
, FillRect
.top
, FillRect
.bottom
, Color
);
146 EnumMore
= CLIPOBJ_bEnum(pco
, (ULONG
) sizeof(RectEnum
), (PVOID
) &RectEnum
);
147 for (i
= 0; i
< RectEnum
.c
&& RectEnum
.arcl
[i
].top
<= rcSG
.bottom
; i
++)
149 if(RECTL_bIntersectRect(&FillRect
, &RectEnum
.arcl
[i
], &rcSG
))
155 for(y
= rcSG
.top
; y
< FillRect
.bottom
; y
++)
157 if(y
>= FillRect
.top
)
159 Color
= XLATEOBJ_iXlate(pxlo
, RGB(c
[0], c
[1], c
[2]));
160 DibFunctionsForBitmapFormat
[psoOutput
->iBitmapFormat
].DIB_HLine(
161 psoOutput
, FillRect
.left
, FillRect
.right
, y
, Color
);
172 return IntEngLeave(&EnterLeave
);
175 /* rectangle has only one color, no calculation required */
176 CLIPOBJ_cEnumStart(pco
, FALSE
, CT_RECTANGLES
, CD_RIGHTDOWN
, 0);
180 ULONG Color
= XLATEOBJ_iXlate(pxlo
, RGB(v1
->Red
, v1
->Green
, v1
->Blue
));
182 EnumMore
= CLIPOBJ_bEnum(pco
, (ULONG
) sizeof(RectEnum
), (PVOID
) &RectEnum
);
183 for (i
= 0; i
< RectEnum
.c
&& RectEnum
.arcl
[i
].top
<= rcSG
.bottom
; i
++)
185 if(RECTL_bIntersectRect(&FillRect
, &RectEnum
.arcl
[i
], &rcSG
))
187 for(; FillRect
.top
< FillRect
.bottom
; FillRect
.top
++)
189 DibFunctionsForBitmapFormat
[psoOutput
->iBitmapFormat
].DIB_HLine(
190 psoOutput
, 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 DibFunctionsForBitmapFormat[psoOutput->iBitmapFormat].DIB_HLine(psoOutput, max(sx[lineto], FillRect.left), min(sx[linefrom], FillRect.right), sy, Color); \
204 DibFunctionsForBitmapFormat[psoOutput->iBitmapFormat].DIB_HLine(psoOutput, 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 DibFunctionsForBitmapFormat[psoOutput->iBitmapFormat].DIB_PutPixel(psoOutput, 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
)
315 PTRIVERTEX v1
, v2
, v3
;
316 //RECT_ENUM RectEnum;
320 INTENG_ENTER_LEAVE EnterLeave
;
325 //LONG x[NLINES], dx[NLINES], dy[NLINES], incx[NLINES], ex[NLINES], destx[NLINES];
326 //LONG c[NLINES][3], dc[NLINES][3], ec[NLINES][3], ic[NLINES][3]; /* colors on lines */
327 //LONG g, gx, gxi, gc[3], gd[3], ge[3], gi[3]; /* colors in triangle */
330 v1
= (pVertex
+ gTriangle
->Vertex1
);
331 v2
= (pVertex
+ gTriangle
->Vertex2
);
332 v3
= (pVertex
+ gTriangle
->Vertex3
);
350 DPRINT1("Triangle: (%i,%i) (%i,%i) (%i,%i)\n", v1
->x
, v1
->y
, v2
->x
, v2
->y
, v3
->x
, v3
->y
);
351 /* FIXME: commented out because of an endless loop - fix triangles first */
352 DPRINT1("FIXME: IntEngGradientFillTriangle is broken\n");
354 if(!IntEngEnter(&EnterLeave
, psoDest
, &FillRect
, FALSE
, &Translate
, &psoOutput
))
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(RECTL_bIntersectRect(&FillRect, &RectEnum.arcl[i], prclExtents))
371 // DOINIT(v1, v3, 0);
372 // DOINIT(v1, v2, 1);
373 // DOINIT(v2, v3, 2);
376 // sy = v1->y + pptlDitherOrg->y;
377 // bt = min(v3->y + pptlDitherOrg->y, FillRect.bottom);
381 // InY = !(sy < FillRect.top || sy >= FillRect.bottom);
382 // GOLINE(v1, v3, 0);
383 // DOLINE(v1, v3, 0);
384 // ENDLINE(v1, v3, 0);
386 // GOLINE(v1, v2, 1);
387 // DOLINE(v1, v2, 1);
389 // ENDLINE(v1, v2, 1);
391 // GOLINE(v2, v3, 2);
392 // DOLINE(v2, v3, 2);
394 // ENDLINE(23, v3, 2);
401 // } while(EnumMore);
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(RECTL_bIntersectRect(&FillRect, &RectEnum.arcl[i], 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);
427 // S_GOLINE(v1, v3, 0);
428 // S_DOLINE(v1, v3, 0);
429 // S_ENDLINE(v1, v3, 0);
431 // S_GOLINE(v1, v2, 1);
432 // S_DOLINE(v1, v2, 1);
434 // S_ENDLINE(v1, v2, 1);
436 // S_GOLINE(v2, v3, 2);
437 // S_DOLINE(v2, v3, 2);
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
,
483 pco
= IntEngCreateClipRegion(0, 0, prclExtents
);
492 case GRADIENT_FILL_RECT_H
:
493 case GRADIENT_FILL_RECT_V
:
495 PGRADIENT_RECT gr
= (PGRADIENT_RECT
)pMesh
;
496 for(i
= 0; i
< nMesh
; i
++, gr
++)
498 if(!IntEngGradientFillRect(psoDest
, pco
, pxlo
, pVertex
, nVertex
, gr
, prclExtents
,
499 pptlDitherOrg
, (ulMode
== GRADIENT_FILL_RECT_H
)))
507 case GRADIENT_FILL_TRIANGLE
:
509 PGRADIENT_TRIANGLE gt
= (PGRADIENT_TRIANGLE
)pMesh
;
510 for(i
= 0; i
< nMesh
; i
++, gt
++)
512 if(IntEngIsNULLTriangle(pVertex
, gt
))
514 /* skip empty triangles */
517 if(!IntEngGradientFillTriangle(psoDest
, pco
, pxlo
, pVertex
, nVertex
, gt
, prclExtents
,
536 IN TRIVERTEX
*pVertex
,
540 IN RECTL
*prclExtents
,
541 IN POINTL
*pptlDitherOrg
,
548 psurf
= CONTAINING_RECORD(psoDest
, SURFACE
, SurfObj
);
551 if(psurf
->flHooks
& HOOK_GRADIENTFILL
)
553 Ret
= GDIDEVFUNCS(psoDest
).GradientFill(
554 psoDest
, pco
, pxlo
, pVertex
, nVertex
, pMesh
, nMesh
,
555 prclExtents
, pptlDitherOrg
, ulMode
);
559 Ret
= EngGradientFill(psoDest
, pco
, pxlo
, pVertex
, nVertex
, pMesh
, nMesh
, prclExtents
,
560 pptlDitherOrg
, ulMode
);