[YAROTOWS] Reintegrate the branch. For a brighter future.
[reactos.git] / reactos / subsystems / win32 / win32k / dib / dib8bpp.c
1 /*
2 * PROJECT: Win32 subsystem
3 * LICENSE: See COPYING in the top level directory
4 * FILE: subsystems/win32/win32k/dib/dib8bpp.c
5 * PURPOSE: Device Independant Bitmap functions, 8bpp
6 * PROGRAMMERS: Jason Filby
7 * Thomas Bluemel
8 * Gregor Anich
9 */
10
11 #include <win32k.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16 VOID
17 DIB_8BPP_PutPixel(SURFOBJ *SurfObj, LONG x, LONG y, ULONG c)
18 {
19 PBYTE byteaddr = (PBYTE)SurfObj->pvScan0 + y * SurfObj->lDelta + x;
20
21 *byteaddr = c;
22 }
23
24 ULONG
25 DIB_8BPP_GetPixel(SURFOBJ *SurfObj, LONG x, LONG y)
26 {
27 PBYTE byteaddr = (PBYTE)SurfObj->pvScan0 + y * SurfObj->lDelta + x;
28
29 return (ULONG)(*byteaddr);
30 }
31
32 VOID
33 DIB_8BPP_HLine(SURFOBJ *SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
34 {
35 memset((PBYTE)SurfObj->pvScan0 + y * SurfObj->lDelta + x1, (BYTE) c, x2 - x1);
36 }
37
38 VOID
39 DIB_8BPP_VLine(SURFOBJ *SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
40 {
41 PBYTE byteaddr = (PBYTE)SurfObj->pvScan0 + y1 * SurfObj->lDelta;
42 PBYTE addr = byteaddr + x;
43 LONG lDelta = SurfObj->lDelta;
44
45 byteaddr = addr;
46 while(y1++ < y2)
47 {
48 *addr = c;
49
50 addr += lDelta;
51 }
52 }
53
54 BOOLEAN
55 DIB_8BPP_BitBltSrcCopy(PBLTINFO BltInfo)
56 {
57 LONG i, j, sx, sy, xColor, f1;
58 PBYTE SourceBits, DestBits, SourceLine, DestLine;
59 PBYTE SourceBits_4BPP, SourceLine_4BPP;
60
61 DestBits = (PBYTE)BltInfo->DestSurface->pvScan0 + (BltInfo->DestRect.top * BltInfo->DestSurface->lDelta) + BltInfo->DestRect.left;
62
63 switch(BltInfo->SourceSurface->iBitmapFormat)
64 {
65 case BMF_1BPP:
66 sx = BltInfo->SourcePoint.x;
67 sy = BltInfo->SourcePoint.y;
68
69 for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
70 {
71 sx = BltInfo->SourcePoint.x;
72 for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
73 {
74 if(DIB_1BPP_GetPixel(BltInfo->SourceSurface, sx, sy) == 0)
75 {
76 DIB_8BPP_PutPixel(BltInfo->DestSurface, i, j, XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, 0));
77 }
78 else
79 {
80 DIB_8BPP_PutPixel(BltInfo->DestSurface, i, j, XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, 1));
81 }
82 sx++;
83 }
84 sy++;
85 }
86 break;
87
88 case BMF_4BPP:
89 SourceBits_4BPP = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + (BltInfo->SourcePoint.x >> 1);
90
91 for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
92 {
93 SourceLine_4BPP = SourceBits_4BPP;
94 sx = BltInfo->SourcePoint.x;
95 f1 = sx & 1;
96
97 for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
98 {
99 xColor = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest,
100 (*SourceLine_4BPP & altnotmask[f1]) >> (4 * (1 - f1)));
101 DIB_8BPP_PutPixel(BltInfo->DestSurface, i, j, xColor);
102 if(f1 == 1) { SourceLine_4BPP++; f1 = 0; } else { f1 = 1; }
103 sx++;
104 }
105
106 SourceBits_4BPP += BltInfo->SourceSurface->lDelta;
107 }
108 break;
109
110 case BMF_8BPP:
111 if (NULL == BltInfo->XlateSourceToDest || 0 != (BltInfo->XlateSourceToDest->flXlate & XO_TRIVIAL))
112 {
113 if (BltInfo->DestRect.top < BltInfo->SourcePoint.y)
114 {
115 SourceBits = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + BltInfo->SourcePoint.x;
116 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
117 {
118 RtlMoveMemory(DestBits, SourceBits, BltInfo->DestRect.right - BltInfo->DestRect.left);
119 SourceBits += BltInfo->SourceSurface->lDelta;
120 DestBits += BltInfo->DestSurface->lDelta;
121 }
122 }
123 else
124 {
125 SourceBits = (PBYTE)BltInfo->SourceSurface->pvScan0 + ((BltInfo->SourcePoint.y + BltInfo->DestRect.bottom - BltInfo->DestRect.top - 1) * BltInfo->SourceSurface->lDelta) + BltInfo->SourcePoint.x;
126 DestBits = (PBYTE)BltInfo->DestSurface->pvScan0 + ((BltInfo->DestRect.bottom - 1) * BltInfo->DestSurface->lDelta) + BltInfo->DestRect.left;
127 for (j = BltInfo->DestRect.bottom - 1; BltInfo->DestRect.top <= j; j--)
128 {
129 RtlMoveMemory(DestBits, SourceBits, BltInfo->DestRect.right - BltInfo->DestRect.left);
130 SourceBits -= BltInfo->SourceSurface->lDelta;
131 DestBits -= BltInfo->DestSurface->lDelta;
132 }
133 }
134 }
135 else
136 {
137 if (BltInfo->DestRect.top < BltInfo->SourcePoint.y)
138 {
139 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + BltInfo->SourcePoint.x;
140 DestLine = DestBits;
141 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
142 {
143 SourceBits = SourceLine;
144 DestBits = DestLine;
145 for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
146 {
147 *DestBits++ = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, *SourceBits++);
148 }
149 SourceLine += BltInfo->SourceSurface->lDelta;
150 DestLine += BltInfo->DestSurface->lDelta;
151 }
152 }
153 else
154 {
155 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + ((BltInfo->SourcePoint.y + BltInfo->DestRect.bottom - BltInfo->DestRect.top - 1) * BltInfo->SourceSurface->lDelta) + BltInfo->SourcePoint.x;
156 DestLine = (PBYTE)BltInfo->DestSurface->pvScan0 + ((BltInfo->DestRect.bottom - 1) * BltInfo->DestSurface->lDelta) + BltInfo->DestRect.left;
157 for (j = BltInfo->DestRect.bottom - 1; BltInfo->DestRect.top <= j; j--)
158 {
159 SourceBits = SourceLine;
160 DestBits = DestLine;
161 for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
162 {
163 *DestBits++ = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, *SourceBits++);
164 }
165 SourceLine -= BltInfo->SourceSurface->lDelta;
166 DestLine -= BltInfo->DestSurface->lDelta;
167 }
168 }
169 }
170 break;
171
172 case BMF_16BPP:
173 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + 2 * BltInfo->SourcePoint.x;
174 DestLine = DestBits;
175
176 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
177 {
178 SourceBits = SourceLine;
179 DestBits = DestLine;
180
181 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
182 {
183 xColor = *((PWORD) SourceBits);
184 *DestBits = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, xColor);
185 SourceBits += 2;
186 DestBits += 1;
187 }
188
189 SourceLine += BltInfo->SourceSurface->lDelta;
190 DestLine += BltInfo->DestSurface->lDelta;
191 }
192 break;
193
194 case BMF_24BPP:
195 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + 3 * BltInfo->SourcePoint.x;
196 DestLine = DestBits;
197
198 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
199 {
200 SourceBits = SourceLine;
201 DestBits = DestLine;
202
203 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
204 {
205 xColor = (*(SourceBits + 2) << 0x10) +
206 (*(SourceBits + 1) << 0x08) +
207 (*(SourceBits));
208 *DestBits = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, xColor);
209 SourceBits += 3;
210 DestBits += 1;
211 }
212
213 SourceLine += BltInfo->SourceSurface->lDelta;
214 DestLine += BltInfo->DestSurface->lDelta;
215 }
216 break;
217
218 case BMF_32BPP:
219 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 + (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) + 4 * BltInfo->SourcePoint.x;
220 DestLine = DestBits;
221
222 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
223 {
224 SourceBits = SourceLine;
225 DestBits = DestLine;
226
227 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
228 {
229 xColor = *((PDWORD) SourceBits);
230 *DestBits = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, xColor);
231 SourceBits += 4;
232 DestBits += 1;
233 }
234
235 SourceLine += BltInfo->SourceSurface->lDelta;
236 DestLine += BltInfo->DestSurface->lDelta;
237 }
238 break;
239
240 default:
241 DPRINT1("DIB_8BPP_Bitblt: Unhandled Source BPP: %u\n", BitsPerFormat(BltInfo->SourceSurface->iBitmapFormat));
242 return FALSE;
243 }
244
245 return TRUE;
246 }
247
248 /* BitBlt Optimize */
249 BOOLEAN
250 DIB_8BPP_ColorFill(SURFOBJ* DestSurface, RECTL* DestRect, ULONG color)
251 {
252 ULONG DestY;
253 for (DestY = DestRect->top; DestY< DestRect->bottom; DestY++)
254 {
255 DIB_8BPP_HLine(DestSurface, DestRect->left, DestRect->right, DestY, color);
256 }
257 return TRUE;
258 }
259
260
261 BOOLEAN
262 DIB_8BPP_TransparentBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
263 RECTL* DestRect, RECTL *SourceRect,
264 XLATEOBJ *ColorTranslation, ULONG iTransColor)
265 {
266 ULONG RoundedRight, X, Y, SourceX = 0, SourceY = 0, Source, Dest;
267 ULONG *DestBits;
268
269 LONG DstHeight;
270 LONG DstWidth;
271 LONG SrcHeight;
272 LONG SrcWidth;
273
274 DstHeight = DestRect->bottom - DestRect->top;
275 DstWidth = DestRect->right - DestRect->left;
276 SrcHeight = SourceRect->bottom - SourceRect->top;
277 SrcWidth = SourceRect->right - SourceRect->left;
278
279 RoundedRight = DestRect->right - ((DestRect->right - DestRect->left) & 0x3);
280 DestBits = (ULONG*)((PBYTE)DestSurf->pvScan0 + DestRect->left +
281 (DestRect->top * DestSurf->lDelta));
282
283 for(Y = DestRect->top; Y < DestRect->bottom; Y++)
284 {
285 DestBits = (ULONG*)((PBYTE)DestSurf->pvScan0 + DestRect->left +
286 (Y * DestSurf->lDelta));
287 SourceY = SourceRect->top+(Y - DestRect->top) * SrcHeight / DstHeight;
288 for (X = DestRect->left; X < RoundedRight; X += 4, DestBits++)
289 {
290 Dest = *DestBits;
291
292 SourceX = SourceRect->left+(X - DestRect->left) * SrcWidth / DstWidth;
293 if (SourceX >= 0 && SourceY >= 0 &&
294 SourceSurf->sizlBitmap.cx > SourceX && SourceSurf->sizlBitmap.cy > SourceY)
295 {
296 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
297 if(Source != iTransColor)
298 {
299 Dest &= 0xFFFFFF00;
300 Dest |= (XLATEOBJ_iXlate(ColorTranslation, Source) & 0xFF);
301 }
302 }
303
304 SourceX = SourceRect->left+(X+1 - DestRect->left) * SrcWidth / DstWidth;
305 if (SourceX >= 0 && SourceY >= 0 &&
306 SourceSurf->sizlBitmap.cx > SourceX && SourceSurf->sizlBitmap.cy > SourceY)
307 {
308 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
309 if(Source != iTransColor)
310 {
311 Dest &= 0xFFFF00FF;
312 Dest |= ((XLATEOBJ_iXlate(ColorTranslation, Source) << 8) & 0xFF00);
313 }
314 }
315
316 SourceX = SourceRect->left+(X+2 - DestRect->left) * SrcWidth / DstWidth;
317 if (SourceX >= 0 && SourceY >= 0 &&
318 SourceSurf->sizlBitmap.cx > SourceX && SourceSurf->sizlBitmap.cy > SourceY)
319 {
320 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
321 if(Source != iTransColor)
322 {
323 Dest &= 0xFF00FFFF;
324 Dest |= ((XLATEOBJ_iXlate(ColorTranslation, Source) << 16) & 0xFF0000);
325 }
326 }
327
328 SourceX = SourceRect->left+(X+3 - DestRect->left) * SrcWidth / DstWidth;
329 if (SourceX >= 0 && SourceY >= 0 &&
330 SourceSurf->sizlBitmap.cx > SourceX && SourceSurf->sizlBitmap.cy > SourceY)
331 {
332 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
333 if(Source != iTransColor)
334 {
335 Dest &= 0x00FFFFFF;
336 Dest |= ((XLATEOBJ_iXlate(ColorTranslation, Source) << 24) & 0xFF000000);
337 }
338 }
339
340 *DestBits = Dest;
341 }
342
343 if(X < DestRect->right)
344 {
345 for (; X < DestRect->right; X++)
346 {
347 SourceX = SourceRect->left+(X - DestRect->left) * SrcWidth / DstWidth;
348 if (SourceX >= 0 && SourceY >= 0 &&
349 SourceSurf->sizlBitmap.cx > SourceX && SourceSurf->sizlBitmap.cy > SourceY)
350 {
351 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
352 if(Source != iTransColor)
353 {
354 *((BYTE*)DestBits) = (BYTE)(XLATEOBJ_iXlate(ColorTranslation, Source) & 0xFF);
355 }
356 }
357 DestBits = (PULONG)((ULONG_PTR)DestBits + 1);
358 }
359 }
360 }
361
362 return TRUE;
363 }
364
365 /* EOF */