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