update for HEAD-2003050101
[reactos.git] / subsys / win32k / dib / dib4bpp.c
1 #undef WIN32_LEAN_AND_MEAN
2 #include <windows.h>
3 #include <stdlib.h>
4 #include <win32k/bitmaps.h>
5 #include <win32k/debug.h>
6 #include <debug.h>
7 #include <ddk/winddi.h>
8 #include "../eng/objects.h"
9 #include "dib.h"
10
11 VOID DIB_4BPP_PutPixel(PSURFOBJ SurfObj, LONG x, LONG y, ULONG c)
12 {
13   PBYTE addr = SurfObj->pvScan0;
14
15   addr += (x>>1) + y * SurfObj->lDelta;
16   *addr = (*addr & notmask[x&1]) | (c << ((1-(x&1))<<2));
17 }
18
19 ULONG DIB_4BPP_GetPixel(PSURFOBJ SurfObj, LONG x, LONG y)
20 {
21   PBYTE addr = SurfObj->pvScan0;
22
23   return (addr[(x>>1) + y * SurfObj->lDelta] >> ((1-(x&1))<<2) ) & 0x0f;
24 }
25
26 VOID DIB_4BPP_HLine(PSURFOBJ SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
27 {
28   PBYTE  addr = SurfObj->pvScan0 + (x1>>1) + y * SurfObj->lDelta;
29   LONG  cx = x1;
30
31   while(cx < x2) {
32     *addr = (*addr & notmask[x1&1]) | (c << ((1-(x1&1))<<2));
33     if((++x1 & 1) == 0)
34       ++addr;
35     ++cx;
36   }
37 }
38
39 VOID DIB_4BPP_VLine(PSURFOBJ SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
40 {
41   PBYTE  addr = SurfObj->pvScan0;
42   int  lDelta = SurfObj->lDelta;
43
44   addr += (x>>1) + y1 * lDelta;
45   while(y1++ < y2) {
46     *addr = (*addr & notmask[x&1]) | (c << ((1-(x&1))<<2));
47     addr += lDelta;
48   }
49 }
50
51 BOOLEAN DIB_4BPP_BitBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
52                         SURFGDI *DestGDI,  SURFGDI *SourceGDI,
53                         PRECTL  DestRect,  POINTL  *SourcePoint,
54                         XLATEOBJ *ColorTranslation)
55 {
56   LONG     i, j, sx, sy, f1, f2, xColor;
57   PBYTE    SourceBits_24BPP, SourceLine_24BPP;
58   PBYTE    DestBits, DestLine, SourceBits_8BPP, SourceLine_8BPP;
59   PBYTE    SourceBits, SourceLine;
60
61   DestBits = DestSurf->pvScan0 + (DestRect->left>>1) + DestRect->top * DestSurf->lDelta;
62
63   switch(SourceGDI->BitsPerPixel)
64   {
65     case 1:
66       sx = SourcePoint->x;
67       sy = SourcePoint->y;
68
69       for (j=DestRect->top; j<DestRect->bottom; j++)
70       {
71         sx = SourcePoint->x;
72         for (i=DestRect->left; i<DestRect->right; i++)
73         {
74           if(DIB_1BPP_GetPixel(SourceSurf, sx, sy) == 0)
75           {
76             DIB_4BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 0));
77           } else {
78             DIB_4BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 1));
79           }
80           sx++;
81         }
82         sy++;
83       }
84       break;
85
86     case 4:
87       sy = SourcePoint->y;
88
89       for (j=DestRect->top; j<DestRect->bottom; j++)
90       {
91         sx = SourcePoint->x;
92
93         for (i=DestRect->left; i<DestRect->right; i++)
94         {
95           DIB_4BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, DIB_4BPP_GetPixel(SourceSurf, sx, sy)));
96           sx++;
97         }
98         sy++;
99       }
100       break;
101
102     case 8:
103       SourceBits_8BPP = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + SourcePoint->x;
104
105       for (j=DestRect->top; j<DestRect->bottom; j++)
106       {
107         SourceLine_8BPP = SourceBits_8BPP;
108         DestLine = DestBits;
109         f2 = DestRect->left & 1;
110
111         for (i=DestRect->left; i<DestRect->right; i++)
112         {
113           *DestLine = (*DestLine & notmask[i&1]) |
114             ((XLATEOBJ_iXlate(ColorTranslation, *SourceLine_8BPP)) << ((4 * (1-(i & 1)))));
115           if(f2 == 1) { DestLine++; f2 = 0; } else { f2 = 1; }
116           SourceLine_8BPP++;
117         }
118
119         SourceBits_8BPP += SourceSurf->lDelta;
120         DestBits += DestSurf->lDelta;
121       }
122       break;
123
124     case 16:
125       SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 2 * SourcePoint->x;
126       DestLine = DestBits;
127
128       for (j = DestRect->top; j < DestRect->bottom; j++)
129       {
130         SourceBits = SourceLine;
131         DestBits = DestLine;
132         f2 = DestRect->left & 1;
133
134         for (i = DestRect->left; i < DestRect->right; i++)
135         {
136           xColor = *((PWORD) SourceBits);
137           *DestBits = (*DestBits & notmask[i&1]) |
138             ((XLATEOBJ_iXlate(ColorTranslation, xColor)) << ((4 * (1-(i & 1)))));
139           if(f2 == 1) { DestBits++; f2 = 0; } else { f2 = 1; }
140           SourceBits += 2;
141         }
142
143         SourceLine += SourceSurf->lDelta;
144         DestLine += DestSurf->lDelta;
145       }
146       break;
147
148     case 24:
149       SourceBits_24BPP = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + SourcePoint->x * 3;
150
151       for (j=DestRect->top; j<DestRect->bottom; j++)
152       {
153         SourceLine_24BPP = SourceBits_24BPP;
154         DestLine = DestBits;
155         f2 = DestRect->left & 1;
156
157         for (i=DestRect->left; i<DestRect->right; i++)
158         {
159           xColor = (*(SourceLine_24BPP + 2) << 0x10) +
160              (*(SourceLine_24BPP + 1) << 0x08) +
161              (*(SourceLine_24BPP));
162           *DestLine = (*DestLine & notmask[i&1]) |
163             ((XLATEOBJ_iXlate(ColorTranslation, xColor)) << ((4 * (1-(i & 1)))));
164           if(f2 == 1) { DestLine++; f2 = 0; } else { f2 = 1; }
165           SourceLine_24BPP+=3;
166         }
167
168         SourceBits_24BPP += SourceSurf->lDelta;
169         DestBits += DestSurf->lDelta;
170       }
171       break;
172
173     case 32:
174       SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 4 * SourcePoint->x;
175       DestLine = DestBits;
176
177       for (j = DestRect->top; j < DestRect->bottom; j++)
178       {
179         SourceBits = SourceLine;
180         DestBits = DestLine;
181         f2 = DestRect->left & 1;
182
183         for (i = DestRect->left; i < DestRect->right; i++)
184         {
185           xColor = *((PDWORD) SourceBits);
186           *DestBits = (*DestBits & notmask[i&1]) |
187             ((XLATEOBJ_iXlate(ColorTranslation, xColor)) << ((4 * (1-(i & 1)))));
188           if(f2 == 1) { DestBits++; f2 = 0; } else { f2 = 1; }
189           SourceBits += 4;
190         }
191
192         SourceLine += SourceSurf->lDelta;
193         DestLine += DestSurf->lDelta;
194       }
195       break;
196
197     default:
198       DbgPrint("DIB_4BPP_Bitblt: Unhandled Source BPP: %u\n", SourceGDI->BitsPerPixel);
199       return FALSE;
200   }
201
202   return TRUE;
203 }