b18248d4762ec59aa5cd5df35db7c0269133994d
[reactos.git] / reactos / subsystems / win32 / win32k / dib / dib32bpp.c
1 /*
2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /* $Id$ */
20
21 #include <w32k.h>
22
23 #define NDEBUG
24 #include <debug.h>
25
26 VOID
27 DIB_32BPP_PutPixel(SURFOBJ *SurfObj, LONG x, LONG y, ULONG c)
28 {
29 PBYTE byteaddr = (PBYTE)SurfObj->pvScan0 + y * SurfObj->lDelta;
30 PDWORD addr = (PDWORD)byteaddr + x;
31
32 *addr = c;
33 }
34
35 ULONG
36 DIB_32BPP_GetPixel(SURFOBJ *SurfObj, LONG x, LONG y)
37 {
38 PBYTE byteaddr = (PBYTE)SurfObj->pvScan0 + y * SurfObj->lDelta;
39 PDWORD addr = (PDWORD)byteaddr + x;
40
41 return (ULONG)(*addr);
42 }
43
44
45 VOID
46 DIB_32BPP_VLine(SURFOBJ *SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
47 {
48
49
50 PBYTE byteaddr = (PBYTE)SurfObj->pvScan0 + y1 * SurfObj->lDelta;
51 PDWORD addr = (PDWORD)byteaddr + x;
52 LONG lDelta = SurfObj->lDelta >> 2; // >> 2 == / sizeof(DWORD)
53
54 byteaddr = (PBYTE)addr;
55 while(y1++ < y2)
56 {
57 *addr = (DWORD)c;
58 addr += lDelta;
59 }
60
61 }
62
63 BOOLEAN
64 DIB_32BPP_BitBltSrcCopy(PBLTINFO BltInfo)
65 {
66 LONG i, j, sx, sy, xColor, f1;
67 PBYTE SourceBits, DestBits, SourceLine, DestLine;
68 PBYTE SourceBits_4BPP, SourceLine_4BPP;
69 PDWORD Source32, Dest32;
70
71 DestBits = (PBYTE)BltInfo->DestSurface->pvScan0 + (BltInfo->DestRect.top * BltInfo->DestSurface->lDelta) + 4 * BltInfo->DestRect.left;
72
73 switch(BltInfo->SourceSurface->iBitmapFormat)
74 {
75 case BMF_1BPP:
76
77 sx = BltInfo->SourcePoint.x;
78 sy = BltInfo->SourcePoint.y;
79
80 for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
81 {
82 sx = BltInfo->SourcePoint.x;
83 for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
84 {
85 if(DIB_1BPP_GetPixel(BltInfo->SourceSurface, sx, sy) == 0)
86 {
87 DIB_32BPP_PutPixel(BltInfo->DestSurface, i, j, XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, 0));
88 } else {
89 DIB_32BPP_PutPixel(BltInfo->DestSurface, i, j, XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, 1));
90 }
91 sx++;
92 }
93 sy++;
94 }
95 break;
96
97 case BMF_4BPP:
98 SourceBits_4BPP = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + (BltInfo->SourcePoint.x >> 1);
99
100 for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
101 {
102 SourceLine_4BPP = SourceBits_4BPP;
103 sx = BltInfo->SourcePoint.x;
104 f1 = sx & 1;
105
106 for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
107 {
108 xColor = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest,
109 (*SourceLine_4BPP & altnotmask[f1]) >> (4 * (1 - f1)));
110 DIB_32BPP_PutPixel(BltInfo->DestSurface, i, j, xColor);
111 if(f1 == 1) { SourceLine_4BPP++; f1 = 0; } else { f1 = 1; }
112 sx++;
113 }
114
115 SourceBits_4BPP += BltInfo->SourceSurface->lDelta;
116 }
117 break;
118
119 case BMF_8BPP:
120 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + BltInfo->SourcePoint.x;
121 DestLine = DestBits;
122
123 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
124 {
125 SourceBits = SourceLine;
126 DestBits = DestLine;
127
128 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
129 {
130 xColor = *SourceBits;
131 *((PDWORD) DestBits) = (DWORD)XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, xColor);
132 SourceBits += 1;
133 DestBits += 4;
134 }
135
136 SourceLine += BltInfo->SourceSurface->lDelta;
137 DestLine += BltInfo->DestSurface->lDelta;
138 }
139 break;
140
141 case BMF_16BPP:
142 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + 2 * BltInfo->SourcePoint.x;
143 DestLine = DestBits;
144
145 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
146 {
147 SourceBits = SourceLine;
148 DestBits = DestLine;
149
150 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
151 {
152 xColor = *((PWORD) SourceBits);
153 *((PDWORD) DestBits) = (DWORD)XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, xColor);
154 SourceBits += 2;
155 DestBits += 4;
156 }
157
158 SourceLine += BltInfo->SourceSurface->lDelta;
159 DestLine += BltInfo->DestSurface->lDelta;
160 }
161 break;
162
163 case BMF_24BPP:
164 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + 3 * BltInfo->SourcePoint.x;
165 DestLine = DestBits;
166
167 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
168 {
169 SourceBits = SourceLine;
170 DestBits = DestLine;
171
172 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
173 {
174 xColor = (*(SourceBits + 2) << 0x10) +
175 (*(SourceBits + 1) << 0x08) +
176 (*(SourceBits));
177 *((PDWORD)DestBits) = (DWORD)XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, xColor);
178 SourceBits += 3;
179 DestBits += 4;
180 }
181
182 SourceLine += BltInfo->SourceSurface->lDelta;
183 DestLine += BltInfo->DestSurface->lDelta;
184 }
185 break;
186
187 case BMF_32BPP:
188 if (NULL == BltInfo->XlateSourceToDest || 0 != (BltInfo->XlateSourceToDest->flXlate & XO_TRIVIAL))
189 {
190 if (BltInfo->DestRect.top < BltInfo->SourcePoint.y)
191 {
192 SourceBits = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + 4 * BltInfo->SourcePoint.x;
193 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
194 {
195 RtlMoveMemory(DestBits, SourceBits, 4 * (BltInfo->DestRect.right - BltInfo->DestRect.left));
196 SourceBits += BltInfo->SourceSurface->lDelta;
197 DestBits += BltInfo->DestSurface->lDelta;
198 }
199 }
200 else
201 {
202 SourceBits = (PBYTE)BltInfo->SourceSurface->pvScan0 + ((BltInfo->SourcePoint.y + BltInfo->DestRect.bottom - BltInfo->DestRect.top - 1) * BltInfo->SourceSurface->lDelta) + 4 * BltInfo->SourcePoint.x;
203 DestBits = (PBYTE)BltInfo->DestSurface->pvScan0 + ((BltInfo->DestRect.bottom - 1) * BltInfo->DestSurface->lDelta) + 4 * BltInfo->DestRect.left;
204 for (j = BltInfo->DestRect.bottom - 1; BltInfo->DestRect.top <= j; j--)
205 {
206 RtlMoveMemory(DestBits, SourceBits, 4 * (BltInfo->DestRect.right - BltInfo->DestRect.left));
207 SourceBits -= BltInfo->SourceSurface->lDelta;
208 DestBits -= BltInfo->DestSurface->lDelta;
209 }
210 }
211 }
212 else
213 {
214 if (BltInfo->DestRect.top < BltInfo->SourcePoint.y)
215 {
216 SourceBits = ((PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + 4 * BltInfo->SourcePoint.x);
217 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
218 {
219 if (BltInfo->DestRect.left < BltInfo->SourcePoint.x)
220 {
221 Dest32 = (DWORD *) DestBits;
222 Source32 = (DWORD *) SourceBits;
223 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
224 {
225 *Dest32++ = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, *Source32++);
226 }
227 }
228 else
229 {
230 Dest32 = (DWORD *) DestBits + (BltInfo->DestRect.right - BltInfo->DestRect.left - 1);
231 Source32 = (DWORD *) SourceBits + (BltInfo->DestRect.right - BltInfo->DestRect.left - 1);
232 for (i = BltInfo->DestRect.right - 1; BltInfo->DestRect.left <= i; i--)
233 {
234 *Dest32-- = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, *Source32--);
235 }
236 }
237 SourceBits += BltInfo->SourceSurface->lDelta;
238 DestBits += BltInfo->DestSurface->lDelta;
239 }
240 }
241 else
242 {
243 SourceBits = (PBYTE)BltInfo->SourceSurface->pvScan0 + ((BltInfo->SourcePoint.y + BltInfo->DestRect.bottom - BltInfo->DestRect.top - 1) * BltInfo->SourceSurface->lDelta) + 4 * BltInfo->SourcePoint.x;
244 DestBits = (PBYTE)BltInfo->DestSurface->pvScan0 + ((BltInfo->DestRect.bottom - 1) * BltInfo->DestSurface->lDelta) + 4 * BltInfo->DestRect.left;
245 for (j = BltInfo->DestRect.bottom - 1; BltInfo->DestRect.top <= j; j--)
246 {
247 if (BltInfo->DestRect.left < BltInfo->SourcePoint.x)
248 {
249 Dest32 = (DWORD *) DestBits;
250 Source32 = (DWORD *) SourceBits;
251 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
252 {
253 *Dest32++ = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, *Source32++);
254 }
255 }
256 else
257 {
258 Dest32 = (DWORD *) DestBits + (BltInfo->DestRect.right - BltInfo->DestRect.left - 1);
259 Source32 = (DWORD *) SourceBits + (BltInfo->DestRect.right - BltInfo->DestRect.left - 1);
260 for (i = BltInfo->DestRect.right; BltInfo->DestRect.left <= i; i--)
261 {
262 *Dest32-- = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, *Source32--);
263 }
264 }
265 SourceBits -= BltInfo->SourceSurface->lDelta;
266 DestBits -= BltInfo->DestSurface->lDelta;
267 }
268 }
269 }
270 break;
271
272 default:
273 DPRINT1("DIB_32BPP_Bitblt: Unhandled Source BPP: %u\n", BitsPerFormat(BltInfo->SourceSurface->iBitmapFormat));
274 return FALSE;
275 }
276
277 return TRUE;
278 }
279
280 BOOLEAN
281 DIB_32BPP_TransparentBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
282 RECTL* DestRect, RECTL *SourceRect,
283 XLATEOBJ *ColorTranslation, ULONG iTransColor)
284 {
285 ULONG X, Y, SourceX, SourceY = 0, Source = 0, wd;
286 ULONG *DestBits;
287
288 LONG DstHeight;
289 LONG DstWidth;
290 LONG SrcHeight;
291 LONG SrcWidth;
292
293 DstHeight = DestRect->bottom - DestRect->top;
294 DstWidth = DestRect->right - DestRect->left;
295 SrcHeight = SourceRect->bottom - SourceRect->top;
296 SrcWidth = SourceRect->right - SourceRect->left;
297
298 DestBits = (ULONG*)((PBYTE)DestSurf->pvScan0 +
299 (DestRect->left << 2) +
300 DestRect->top * DestSurf->lDelta);
301 wd = DestSurf->lDelta - ((DestRect->right - DestRect->left) << 2);
302
303 for(Y = DestRect->top; Y < DestRect->bottom; Y++)
304 {
305 SourceY = SourceRect->top+(Y - DestRect->top) * SrcHeight / DstHeight;
306 for(X = DestRect->left; X < DestRect->right; X++, DestBits++)
307 {
308 SourceX = SourceRect->left+(X - DestRect->left) * SrcWidth / DstWidth;
309 if (SourceX >= 0 && SourceY >= 0 &&
310 SourceSurf->sizlBitmap.cx > SourceX && SourceSurf->sizlBitmap.cy > SourceY)
311 {
312 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
313 if(Source != iTransColor)
314 {
315 *DestBits = XLATEOBJ_iXlate(ColorTranslation, Source);
316 }
317 }
318 }
319
320 DestBits = (ULONG*)((ULONG_PTR)DestBits + wd);
321 }
322
323 return TRUE;
324 }
325
326 typedef union {
327 ULONG ul;
328 struct {
329 UCHAR red;
330 UCHAR green;
331 UCHAR blue;
332 UCHAR alpha;
333 } col;
334 } NICEPIXEL32;
335
336 static __inline UCHAR
337 Clamp8(ULONG val)
338 {
339 return (val > 255) ? 255 : val;
340 }
341
342 BOOLEAN
343 DIB_32BPP_AlphaBlend(SURFOBJ* Dest, SURFOBJ* Source, RECTL* DestRect,
344 RECTL* SourceRect, CLIPOBJ* ClipRegion,
345 XLATEOBJ* ColorTranslation, BLENDOBJ* BlendObj)
346 {
347 INT Rows, Cols, SrcX, SrcY;
348 register PULONG Dst;
349 ULONG DstDelta;
350 BLENDFUNCTION BlendFunc;
351 register NICEPIXEL32 DstPixel, SrcPixel;
352 UCHAR Alpha, SrcBpp;
353
354 DPRINT("DIB_32BPP_AlphaBlend: srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
355 SourceRect->left, SourceRect->top, SourceRect->right, SourceRect->bottom,
356 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
357
358 ASSERT(DestRect->bottom - DestRect->top == SourceRect->bottom - SourceRect->top &&
359 DestRect->right - DestRect->left == SourceRect->right - SourceRect->left);
360
361 BlendFunc = BlendObj->BlendFunction;
362 if (BlendFunc.BlendOp != AC_SRC_OVER)
363 {
364 DPRINT1("BlendOp != AC_SRC_OVER\n");
365 return FALSE;
366 }
367 if (BlendFunc.BlendFlags != 0)
368 {
369 DPRINT1("BlendFlags != 0\n");
370 return FALSE;
371 }
372 if ((BlendFunc.AlphaFormat & ~AC_SRC_ALPHA) != 0)
373 {
374 DPRINT1("Unsupported AlphaFormat (0x%x)\n", BlendFunc.AlphaFormat);
375 return FALSE;
376 }
377 if ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0 &&
378 BitsPerFormat(Source->iBitmapFormat) != 32)
379 {
380 DPRINT1("Source bitmap must be 32bpp when AC_SRC_ALPHA is set\n");
381 return FALSE;
382 }
383
384 Dst = (PULONG)((ULONG_PTR)Dest->pvScan0 + (DestRect->top * Dest->lDelta) +
385 (DestRect->left << 2));
386 DstDelta = Dest->lDelta - ((DestRect->right - DestRect->left) << 2);
387 SrcBpp = BitsPerFormat(Source->iBitmapFormat);
388
389 Rows = DestRect->bottom - DestRect->top;
390 SrcY = SourceRect->top;
391 while (--Rows >= 0)
392 {
393 Cols = DestRect->right - DestRect->left;
394 SrcX = SourceRect->left;
395 while (--Cols >= 0)
396 {
397 SrcPixel.ul = DIB_GetSource(Source, SrcX++, SrcY, ColorTranslation);
398 SrcPixel.col.red = SrcPixel.col.red * BlendFunc.SourceConstantAlpha / 255;
399 SrcPixel.col.green = SrcPixel.col.green * BlendFunc.SourceConstantAlpha / 255;
400 SrcPixel.col.blue = SrcPixel.col.blue * BlendFunc.SourceConstantAlpha / 255;
401 SrcPixel.col.alpha = (SrcBpp == 32) ? (SrcPixel.col.alpha * BlendFunc.SourceConstantAlpha / 255) : BlendFunc.SourceConstantAlpha;
402
403 Alpha = ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0) ?
404 SrcPixel.col.alpha : BlendFunc.SourceConstantAlpha;
405
406 DstPixel.ul = *Dst;
407 DstPixel.col.red = Clamp8(DstPixel.col.red * (255 - Alpha) / 255 + SrcPixel.col.red);
408 DstPixel.col.green = Clamp8(DstPixel.col.green * (255 - Alpha) / 255 + SrcPixel.col.green);
409 DstPixel.col.blue = Clamp8(DstPixel.col.blue * (255 - Alpha) / 255 + SrcPixel.col.blue);
410 DstPixel.col.alpha = Clamp8(DstPixel.col.alpha * (255 - Alpha) / 255 + SrcPixel.col.alpha);
411 *Dst++ = DstPixel.ul;
412 }
413 Dst = (PULONG)((ULONG_PTR)Dst + DstDelta);
414 SrcY++;
415 }
416
417 return TRUE;
418 }
419
420 /* EOF */