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