e24df9146f22b7fdc65bf684efdf032d35fe76fd
[reactos.git] / subsys / win32k / dib / dib24bpp.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_24BPP_PutPixel(PSURFOBJ SurfObj, LONG x, LONG y, ULONG c)
12 {
13   PBYTE byteaddr = SurfObj->pvScan0 + y * SurfObj->lDelta;
14   PRGBTRIPLE addr = (PRGBTRIPLE)byteaddr + x;
15
16   *(PULONG)(addr) = c;
17 }
18
19 ULONG DIB_24BPP_GetPixel(PSURFOBJ SurfObj, LONG x, LONG y)
20 {
21   PBYTE byteaddr = SurfObj->pvScan0 + y * SurfObj->lDelta;
22   PRGBTRIPLE addr = (PRGBTRIPLE)byteaddr + x;
23
24   return *(PULONG)(addr) & 0x00ffffff;
25 }
26
27 VOID DIB_24BPP_HLine(PSURFOBJ SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
28 {
29   PBYTE byteaddr = SurfObj->pvScan0 + y * SurfObj->lDelta;
30   PRGBTRIPLE addr = (PRGBTRIPLE)byteaddr + x1;
31   LONG cx = x1;
32
33   while(cx < x2) {
34     *(PULONG)(addr) = c;
35     ++addr;
36     ++cx;
37   }
38 }
39
40 VOID DIB_24BPP_VLine(PSURFOBJ SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
41 {
42   PBYTE byteaddr = SurfObj->pvScan0 + y1 * SurfObj->lDelta;
43   PRGBTRIPLE addr = (PRGBTRIPLE)byteaddr + x;
44   LONG lDelta = SurfObj->lDelta;
45
46   byteaddr = (PBYTE)addr;
47   while(y1++ < y2) {
48     *(PULONG)(addr) = c;
49
50     byteaddr += lDelta;
51     addr = (PRGBTRIPLE)byteaddr;
52   }
53 }
54
55 BOOLEAN DIB_24BPP_BitBlt(  SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
56         SURFGDI *DestGDI,  SURFGDI *SourceGDI,
57         PRECTL  DestRect,  POINTL  *SourcePoint,
58         XLATEOBJ *ColorTranslation)
59 {
60   LONG     i, j, sx, sy, xColor, f1;
61   PBYTE    SourceBits, DestBits, SourceLine, DestLine;
62   PBYTE    SourceBits_4BPP, SourceLine_4BPP;
63   PWORD    SourceBits_16BPP, SourceLine_16BPP;
64
65   DestBits = DestSurf->pvScan0 + (DestRect->top * DestSurf->lDelta) + DestRect->left * 3;
66
67   switch(SourceGDI->BitsPerPixel)
68   {
69     case 1:
70       sx = SourcePoint->x;
71       sy = SourcePoint->y;
72
73       for (j=DestRect->top; j<DestRect->bottom; j++)
74       {
75         sx = SourcePoint->x;
76         for (i=DestRect->left; i<DestRect->right; i++)
77         {
78           if(DIB_1BPP_GetPixel(SourceSurf, sx, sy) == 0)
79           {
80             DIB_24BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 0));
81           } else {
82             DIB_24BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 1));
83           }
84           sx++;
85         }
86         sy++;
87       }
88       break;
89
90     case 4:
91       SourceBits_4BPP = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + (SourcePoint->x >> 1);
92
93       for (j=DestRect->top; j<DestRect->bottom; j++)
94       {
95         SourceLine_4BPP = SourceBits_4BPP;
96         DestLine = DestBits;
97         sx = SourcePoint->x;
98         f1 = sx & 1;
99
100         for (i=DestRect->left; i<DestRect->right; i++)
101         {
102           xColor = XLATEOBJ_iXlate(ColorTranslation,
103               (*SourceLine_4BPP & altnotmask[sx&1]) >> (4 * (1-(sx & 1))));
104           *DestLine++ = xColor & 0xff;
105           *(PWORD)DestLine = xColor >> 8;
106           DestLine += 2;
107           if(f1 == 1) { SourceLine_4BPP++; f1 = 0; } else { f1 = 1; }
108           sx++;
109         }
110
111         SourceBits_4BPP += SourceSurf->lDelta;
112         DestBits += DestSurf->lDelta;
113       }
114       break;
115
116     case 8:
117       SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + SourcePoint->x;
118       DestLine = DestBits;
119
120       for (j = DestRect->top; j < DestRect->bottom; j++)
121       {
122         SourceBits = SourceLine;
123         DestBits = DestLine;
124
125         for (i = DestRect->left; i < DestRect->right; i++)
126         {
127           xColor = XLATEOBJ_iXlate(ColorTranslation, *SourceBits);
128           *DestBits = xColor & 0xff;
129           *(PWORD)(DestBits + 1) = xColor >> 8;
130           SourceBits += 1;
131           DestBits += 3;
132         }
133
134         SourceLine += SourceSurf->lDelta;
135         DestLine += DestSurf->lDelta;
136       }
137       break;
138
139     case 16:
140       SourceBits_16BPP = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 2 * SourcePoint->x;
141
142       for (j=DestRect->top; j<DestRect->bottom; j++)
143       {
144         SourceLine_16BPP = SourceBits_16BPP;
145         DestLine = DestBits;
146
147         for (i=DestRect->left; i<DestRect->right; i++)
148         {
149           xColor = XLATEOBJ_iXlate(ColorTranslation, *SourceLine_16BPP);
150           *DestLine++ = xColor & 0xff;
151           *(PWORD)DestLine = xColor >> 8;
152           DestLine += 2;
153           SourceLine_16BPP++;
154         }
155
156         SourceBits_16BPP = (PWORD)((PBYTE)SourceBits_16BPP + SourceSurf->lDelta);
157         DestBits += DestSurf->lDelta;
158       }
159       break;
160
161     case 24:
162       if (NULL == ColorTranslation || 0 != (ColorTranslation->flXlate & XO_TRIVIAL))
163       {
164         SourceBits = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 3 * SourcePoint->x;
165         for (j = DestRect->top; j < DestRect->bottom; j++)
166         {
167           RtlCopyMemory(DestBits, SourceBits, 3 * (DestRect->right - DestRect->left));
168           SourceBits += SourceSurf->lDelta;
169           DestBits += DestSurf->lDelta;
170         }
171       }
172       else
173       {
174         /* FIXME */
175         DPRINT1("DIB_24BPP_Bitblt: Unhandled ColorTranslation for 16 -> 16 copy");
176         return FALSE;
177       }
178       break;
179
180     case 32:
181       SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 4 * SourcePoint->x;
182       DestLine = DestBits;
183
184       for (j = DestRect->top; j < DestRect->bottom; j++)
185       {
186         SourceBits = SourceLine;
187         DestBits = DestLine;
188
189         for (i = DestRect->left; i < DestRect->right; i++)
190         {
191           xColor = XLATEOBJ_iXlate(ColorTranslation, *((PDWORD) SourceBits));
192           *DestBits = xColor & 0xff;
193           *(PWORD)(DestBits + 1) = xColor >> 8;
194           SourceBits += 4;
195           DestBits += 3;
196         }
197
198         SourceLine += SourceSurf->lDelta;
199         DestLine += DestSurf->lDelta;
200       }
201       break;
202
203     default:
204       DbgPrint("DIB_24BPP_Bitblt: Unhandled Source BPP: %u\n", SourceGDI->BitsPerPixel);
205       return FALSE;
206   }
207
208   return TRUE;
209 }