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