Fixed several off-by-one errors and some compiler warnings
[reactos.git] / reactos / subsys / win32k / dib / dib16bpp.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_16BPP_PutPixel(PSURFOBJ SurfObj, LONG x, LONG y, ULONG c)
12 {
13 PBYTE byteaddr = SurfObj->pvBits + y * SurfObj->lDelta;
14 PWORD addr = (PWORD)byteaddr + x;
15
16 *addr = (WORD)c;
17 }
18
19 ULONG DIB_16BPP_GetPixel(PSURFOBJ SurfObj, LONG x, LONG y)
20 {
21 PBYTE byteaddr = SurfObj->pvBits + y * SurfObj->lDelta;
22 PWORD addr = (PWORD)byteaddr + x;
23
24 return (ULONG)(*addr);
25 }
26
27 VOID DIB_16BPP_HLine(PSURFOBJ SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
28 {
29 PBYTE byteaddr = SurfObj->pvBits + y * SurfObj->lDelta;
30 PWORD addr = (PWORD)byteaddr + x1;
31 LONG cx = x1;
32
33 while(cx < x2) {
34 *addr = (WORD)c;
35 ++addr;
36 ++cx;
37 }
38 }
39
40 VOID DIB_16BPP_VLine(PSURFOBJ SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
41 {
42 PBYTE byteaddr = SurfObj->pvBits + y1 * SurfObj->lDelta;
43 PWORD addr = (PWORD)byteaddr + x;
44 LONG lDelta = SurfObj->lDelta;
45
46 byteaddr = (PBYTE)addr;
47 while(y1++ < y2) {
48 *addr = (WORD)c;
49
50 byteaddr += lDelta;
51 addr = (PWORD)byteaddr;
52 }
53 }
54
55 BOOL DIB_To_16BPP_Bitblt( SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
56 SURFGDI *DestGDI, SURFGDI *SourceGDI,
57 PRECTL DestRect, POINTL *SourcePoint,
58 LONG Delta, XLATEOBJ *ColorTranslation)
59 {
60 LONG i, j, sx, sy, xColor, f1;
61 PBYTE SourceBits, DestBits, SourceLine, DestLine;
62 PBYTE SourceBits_4BPP, SourceLine_4BPP;
63 DestBits = DestSurf->pvBits + (DestRect->top * DestSurf->lDelta) + 2 * DestRect->left;
64
65 switch(SourceGDI->BitsPerPixel)
66 {
67 case 1:
68 sx = SourcePoint->x;
69 sy = SourcePoint->y;
70
71 for (j=DestRect->top; j<DestRect->bottom; j++)
72 {
73 sx = SourcePoint->x;
74 for (i=DestRect->left; i<DestRect->right; i++)
75 {
76 if(DIB_1BPP_GetPixel(SourceSurf, sx, sy) == 0)
77 {
78 DIB_16BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 0));
79 } else {
80 DIB_16BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 1));
81 }
82 sx++;
83 }
84 sy++;
85 }
86 break;
87
88 case 4:
89 SourceBits_4BPP = SourceSurf->pvBits + (SourcePoint->y * SourceSurf->lDelta) + SourcePoint->x;
90
91 for (j=DestRect->top; j<DestRect->bottom; j++)
92 {
93 SourceLine_4BPP = SourceBits_4BPP;
94 sx = SourcePoint->x;
95 f1 = sx & 1;
96
97 for (i=DestRect->left; i<DestRect->right; i++)
98 {
99 xColor = XLATEOBJ_iXlate(ColorTranslation,
100 (*SourceLine_4BPP & altnotmask[sx&1]) >> (4 * (1-(sx & 1))));
101 DIB_16BPP_PutPixel(DestSurf, i, j, xColor);
102 if(f1 == 1) { SourceLine_4BPP++; f1 = 0; } else { f1 = 1; }
103 sx++;
104 }
105
106 SourceBits_4BPP += SourceSurf->lDelta;
107 }
108 break;
109
110 case 16:
111 if (NULL == ColorTranslation || 0 != (ColorTranslation->flXlate & XO_TRIVIAL))
112 {
113 SourceBits = SourceSurf->pvBits + (SourcePoint->y * SourceSurf->lDelta) + 2 * SourcePoint->x;
114 for (j = DestRect->top; j < DestRect->bottom; j++)
115 {
116 RtlCopyMemory(DestBits, SourceBits, 2 * (DestRect->right - DestRect->left));
117 SourceBits += SourceSurf->lDelta;
118 DestBits += DestSurf->lDelta;
119 }
120 }
121 else
122 {
123 /* FIXME */
124 DPRINT1("DIB_16BPP_Bitblt: Unhandled ColorTranslation for 16 -> 16 copy");
125 return FALSE;
126 }
127 break;
128
129 case 24:
130 SourceLine = SourceSurf->pvBits + (SourcePoint->y * SourceSurf->lDelta) + 3 * SourcePoint->x;
131 DestLine = DestBits;
132
133 for (j = DestRect->top; j < DestRect->bottom; j++)
134 {
135 SourceBits = SourceLine;
136 DestBits = DestLine;
137
138 for (i = DestRect->left; i < DestRect->right; i++)
139 {
140 xColor = (*(SourceBits + 2) << 0x10) +
141 (*(SourceBits + 1) << 0x08) +
142 (*(SourceBits));
143 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(ColorTranslation, xColor);
144 SourceBits += 3;
145 DestBits += 2;
146 }
147
148 SourceLine += SourceSurf->lDelta;
149 DestLine += DestSurf->lDelta;
150 }
151 break;
152
153 default:
154 DbgPrint("DIB_16BPP_Bitblt: Unhandled Source BPP: %u\n", SourceGDI->BitsPerPixel);
155 return FALSE;
156 }
157
158 return TRUE;
159 }