update for HEAD-2003091401
[reactos.git] / 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$ */
20 #undef WIN32_LEAN_AND_MEAN
21 #include <windows.h>
22 #include <stdlib.h>
23 #include <win32k/bitmaps.h>
24 #include <win32k/debug.h>
25 #include <debug.h>
26 #include <ddk/winddi.h>
27 #include "../eng/objects.h"
28 #include "dib.h"
29
30 VOID
31 DIB_4BPP_PutPixel(PSURFOBJ SurfObj, LONG x, LONG y, ULONG c)
32 {
33   PBYTE addr = SurfObj->pvScan0;
34
35   addr += (x>>1) + y * SurfObj->lDelta;
36   *addr = (*addr & notmask[x&1]) | (c << ((1-(x&1))<<2));
37 }
38
39 ULONG
40 DIB_4BPP_GetPixel(PSURFOBJ SurfObj, LONG x, LONG y)
41 {
42   PBYTE addr = SurfObj->pvScan0;
43
44   return (addr[(x>>1) + y * SurfObj->lDelta] >> ((1-(x&1))<<2) ) & 0x0f;
45 }
46
47 VOID
48 DIB_4BPP_HLine(PSURFOBJ SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
49 {
50   PBYTE  addr = SurfObj->pvScan0 + (x1>>1) + y * SurfObj->lDelta;
51   LONG  cx = x1;
52
53   while(cx < x2) {
54     *addr = (*addr & notmask[x1&1]) | (c << ((1-(x1&1))<<2));
55     if((++x1 & 1) == 0)
56       ++addr;
57     ++cx;
58   }
59 }
60
61 VOID
62 DIB_4BPP_VLine(PSURFOBJ SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
63 {
64   PBYTE  addr = SurfObj->pvScan0;
65   int  lDelta = SurfObj->lDelta;
66
67   addr += (x>>1) + y1 * lDelta;
68   while(y1++ < y2) {
69     *addr = (*addr & notmask[x&1]) | (c << ((1-(x&1))<<2));
70     addr += lDelta;
71   }
72 }
73
74 BOOLEAN STATIC
75 DIB_4BPP_BitBltSrcCopy(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
76                        SURFGDI *DestGDI,  SURFGDI *SourceGDI,
77                        PRECTL  DestRect,  POINTL  *SourcePoint,
78                        XLATEOBJ* ColorTranslation)
79 {
80   LONG     i, j, sx, sy, f2, xColor;
81   PBYTE    SourceBits_24BPP, SourceLine_24BPP;
82   PBYTE    DestBits, DestLine, SourceBits_8BPP, SourceLine_8BPP;
83   PBYTE    SourceBits, SourceLine;
84
85   DestBits = DestSurf->pvScan0 + (DestRect->left>>1) + DestRect->top * DestSurf->lDelta;
86
87   switch(SourceGDI->BitsPerPixel)
88   {
89     case 1:
90       sx = SourcePoint->x;
91       sy = SourcePoint->y;
92
93       for (j=DestRect->top; j<DestRect->bottom; j++)
94       {
95         sx = SourcePoint->x;
96         for (i=DestRect->left; i<DestRect->right; i++)
97         {
98           if(DIB_1BPP_GetPixel(SourceSurf, sx, sy) == 0)
99           {
100             DIB_4BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 0));
101           } else {
102             DIB_4BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 1));
103           }
104           sx++;
105         }
106         sy++;
107       }
108       break;
109
110     case 4:
111       sy = SourcePoint->y;
112
113       for (j=DestRect->top; j<DestRect->bottom; j++)
114       {
115         sx = SourcePoint->x;
116
117         for (i=DestRect->left; i<DestRect->right; i++)
118         {
119           if (NULL != ColorTranslation)
120           {
121             DIB_4BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, DIB_4BPP_GetPixel(SourceSurf, sx, sy)));
122           }
123           else
124           {
125             DIB_4BPP_PutPixel(DestSurf, i, j, DIB_4BPP_GetPixel(SourceSurf, sx, sy));
126           }
127           sx++;
128         }
129         sy++;
130       }
131       break;
132
133     case 8:
134       SourceBits_8BPP = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + SourcePoint->x;
135
136       for (j=DestRect->top; j<DestRect->bottom; j++)
137       {
138         SourceLine_8BPP = SourceBits_8BPP;
139         DestLine = DestBits;
140         f2 = DestRect->left & 1;
141
142         for (i=DestRect->left; i<DestRect->right; i++)
143         {
144           *DestLine = (*DestLine & notmask[i&1]) |
145             ((XLATEOBJ_iXlate(ColorTranslation, *SourceLine_8BPP)) << ((4 * (1-(i & 1)))));
146           if(f2 == 1) { DestLine++; f2 = 0; } else { f2 = 1; }
147           SourceLine_8BPP++;
148         }
149
150         SourceBits_8BPP += SourceSurf->lDelta;
151         DestBits += DestSurf->lDelta;
152       }
153       break;
154
155     case 16:
156       SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 2 * SourcePoint->x;
157       DestLine = DestBits;
158
159       for (j = DestRect->top; j < DestRect->bottom; j++)
160       {
161         SourceBits = SourceLine;
162         DestBits = DestLine;
163         f2 = DestRect->left & 1;
164
165         for (i = DestRect->left; i < DestRect->right; i++)
166         {
167           xColor = *((PWORD) SourceBits);
168           *DestBits = (*DestBits & notmask[i&1]) |
169             ((XLATEOBJ_iXlate(ColorTranslation, xColor)) << ((4 * (1-(i & 1)))));
170           if(f2 == 1) { DestBits++; f2 = 0; } else { f2 = 1; }
171           SourceBits += 2;
172         }
173
174         SourceLine += SourceSurf->lDelta;
175         DestLine += DestSurf->lDelta;
176       }
177       break;
178
179     case 24:
180       SourceBits_24BPP = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + SourcePoint->x * 3;
181
182       for (j=DestRect->top; j<DestRect->bottom; j++)
183       {
184         SourceLine_24BPP = SourceBits_24BPP;
185         DestLine = DestBits;
186         f2 = DestRect->left & 1;
187
188         for (i=DestRect->left; i<DestRect->right; i++)
189         {
190           xColor = (*(SourceLine_24BPP + 2) << 0x10) +
191              (*(SourceLine_24BPP + 1) << 0x08) +
192              (*(SourceLine_24BPP));
193           *DestLine = (*DestLine & notmask[i&1]) |
194             ((XLATEOBJ_iXlate(ColorTranslation, xColor)) << ((4 * (1-(i & 1)))));
195           if(f2 == 1) { DestLine++; f2 = 0; } else { f2 = 1; }
196           SourceLine_24BPP+=3;
197         }
198
199         SourceBits_24BPP += SourceSurf->lDelta;
200         DestBits += DestSurf->lDelta;
201       }
202       break;
203
204     case 32:
205       SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 4 * SourcePoint->x;
206       DestLine = DestBits;
207
208       for (j = DestRect->top; j < DestRect->bottom; j++)
209       {
210         SourceBits = SourceLine;
211         DestBits = DestLine;
212         f2 = DestRect->left & 1;
213
214         for (i = DestRect->left; i < DestRect->right; i++)
215         {
216           xColor = *((PDWORD) SourceBits);
217           *DestBits = (*DestBits & notmask[i&1]) |
218             ((XLATEOBJ_iXlate(ColorTranslation, xColor)) << ((4 * (1-(i & 1)))));
219           if(f2 == 1) { DestBits++; f2 = 0; } else { f2 = 1; }
220           SourceBits += 4;
221         }
222
223         SourceLine += SourceSurf->lDelta;
224         DestLine += DestSurf->lDelta;
225       }
226       break;
227
228     default:
229       DbgPrint("DIB_4BPP_Bitblt: Unhandled Source BPP: %u\n", SourceGDI->BitsPerPixel);
230       return FALSE;
231   }
232   return(TRUE);
233 }
234
235 BOOLEAN
236 DIB_4BPP_BitBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
237                 SURFGDI *DestGDI,  SURFGDI *SourceGDI,
238                 PRECTL  DestRect,  POINTL  *SourcePoint,
239                 PBRUSHOBJ Brush, PPOINTL BrushOrigin,
240                 XLATEOBJ *ColorTranslation, ULONG Rop4)
241 {
242   LONG     i, j, k, sx, sy;
243   ULONG    Dest, Source, Pattern;
244   PULONG   DestBits;
245   BOOL     UsesSource = ((Rop4 & 0xCC0000) >> 2) != (Rop4 & 0x330000);
246   BOOL     UsesPattern = ((Rop4 & 0xF00000) >> 4) != (Rop4 & 0x0F0000);  
247   LONG    RoundedRight = DestRect->right - (DestRect->right & 0x7);
248   static const ULONG ExpandSolidColor[16] = 
249     {
250       0x00000000 /* 0 */,
251       0x11111111 /* 1 */,
252       0x22222222 /* 2 */,
253       0x33333333 /* 3 */,
254       0x44444444 /* 4 */,
255       0x55555555 /* 5 */,
256       0x66666666 /* 6 */,
257       0x77777777 /* 7 */,
258       0x88888888 /* 8 */,
259       0x99999999 /* 9 */,
260       0xAAAAAAAA /* 10 */,
261       0xBBBBBBBB /* 11 */,
262       0xCCCCCCCC /* 12 */,
263       0xDDDDDDDD /* 13 */,
264       0xEEEEEEEE /* 14 */,
265       0xFFFFFFFF /* 15 */,
266     };
267
268   if (Rop4 == SRCCOPY)
269     {
270       return(DIB_4BPP_BitBltSrcCopy(DestSurf, SourceSurf, DestGDI, SourceGDI, DestRect, SourcePoint, ColorTranslation));
271     }
272   else
273     {
274       sy = SourcePoint->y;
275
276       for (j=DestRect->top; j<DestRect->bottom; j++)
277       {
278         sx = SourcePoint->x;
279         DestBits = (PULONG)(DestSurf->pvScan0 + (DestRect->left>>1) + j * DestSurf->lDelta);
280         for (i=DestRect->left; i<RoundedRight; i+=8, DestBits++)
281           {
282             Dest = *DestBits;
283             if (UsesSource)
284               {
285                 Source = 0;
286                 for (k = 0; k < 8; k++)
287                   {
288                     Source |= (DIB_GetSource(SourceSurf, SourceGDI, sx + i + k, sy, ColorTranslation) << (k * 4));
289                   }
290               }
291             if (UsesPattern)
292               {
293                 /* FIXME: No support for pattern brushes. */
294                 Pattern = ExpandSolidColor[Brush->iSolidColor & 0xF];
295               }
296             *DestBits = DIB_DoRop(Rop4, Dest, Source, Pattern);     
297           }
298         if (i < DestRect->right)
299           {
300             Dest = *DestBits;
301             for (; i < DestRect->right; i++)
302               {
303                 if (UsesSource)
304                   {
305                     Source = DIB_GetSource(SourceSurf, SourceGDI, sx + i, sy, ColorTranslation);
306                   }
307                 if (UsesPattern)
308                   {
309                     /* FIXME: No support for pattern brushes. */
310                     Pattern = ExpandSolidColor[Brush->iSolidColor & 0xF];
311                   }                             
312                 DIB_4BPP_PutPixel(DestSurf, i, j, DIB_DoRop(Rop4, Dest, Source, Pattern) & 0xF);
313                 Dest >>= 4;
314               }  
315           }
316       }
317     }
318   return TRUE;
319 }
320 /* EOF */