DIB Engine: fixes against wine tests
[wine/hacks.git] / dlls / winedib.drv / primitives_pixel.c
blob78e68c7b42280feeadbb6c7b42045b5e68590d7b
1 /*
2 * DIB Engine pixel Primitives
4 * Copyright 2009 Massimo Del Fedele
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include "dibdrv.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(dibdrv);
28 /* ------------------------------------------------------------*/
29 /* BITFIELD HELPERS */
30 static inline DWORD GetField32 (DWORD pixel, int shift, int len)
32 pixel = pixel & (((1 << (len)) - 1) << shift);
33 pixel = pixel << (32 - (shift + len)) >> 24;
34 return pixel;
37 /* ------------------------------------------------------------*/
38 /* PIXEL POINTER READING */
39 void *_DIBDRV_GetPixelPointer32(const DIBDRVBITMAP *dib, int x, int y)
41 BYTE *ptr = dib->bits;
43 ptr += (y * dib->stride);
45 ptr += x * 4;
46 return ptr;
49 void *_DIBDRV_GetPixelPointer24(const DIBDRVBITMAP *dib, int x, int y)
51 BYTE *ptr = dib->bits;
53 ptr += (y * dib->stride);
55 ptr += x * 3;
56 return ptr;
59 void *_DIBDRV_GetPixelPointer16(const DIBDRVBITMAP *dib, int x, int y)
61 BYTE *ptr = dib->bits;
63 ptr += (y * dib->stride);
65 ptr += x * 2;
66 return ptr;
69 void *_DIBDRV_GetPixelPointer8(const DIBDRVBITMAP *dib, int x, int y)
71 BYTE *ptr = dib->bits;
73 ptr += (y * dib->stride);
75 ptr += x;
76 return ptr;
79 void *_DIBDRV_GetPixelPointer4(const DIBDRVBITMAP *dib, int x, int y)
81 BYTE *ptr = dib->bits;
83 ptr += (y * dib->stride);
85 ptr += x / 2;
86 return ptr;
89 void *_DIBDRV_GetPixelPointer1(const DIBDRVBITMAP *dib, int x, int y)
91 BYTE *ptr = dib->bits;
93 ptr += (y * dib->stride);
95 ptr += x / 8;
96 return ptr;
99 /* ------------------------------------------------------------*/
100 /* PIXEL WRITING */
101 void _DIBDRV_SetPixel32(DIBDRVBITMAP *dib, int x, int y, DWORD and, DWORD xor)
103 DWORD *ptr = dib->funcs->GetPixelPointer(dib, x, y);
104 _DIBDRV_rop32(ptr, and, xor);
107 void _DIBDRV_SetPixel24(DIBDRVBITMAP *dib, int x, int y, DWORD and, DWORD xor)
109 BYTE *ptr = dib->funcs->GetPixelPointer(dib, x, y);
110 _DIBDRV_rop8(ptr, and & 0xff, xor & 0xff);
111 _DIBDRV_rop8(ptr + 1, (and >> 8) & 0xff, (xor >> 8) & 0xff);
112 _DIBDRV_rop8(ptr + 2, (and >> 16) & 0xff, (xor >> 16) & 0xff);
115 void _DIBDRV_SetPixel16(DIBDRVBITMAP *dib, int x, int y, DWORD and, DWORD xor)
117 WORD *ptr = dib->funcs->GetPixelPointer(dib, x, y);
118 _DIBDRV_rop16(ptr, and, xor);
121 void _DIBDRV_SetPixel8(DIBDRVBITMAP *dib, int x, int y, DWORD and, DWORD xor)
123 BYTE *ptr = dib->funcs->GetPixelPointer(dib, x, y);
124 _DIBDRV_rop8(ptr, and, xor);
127 void _DIBDRV_SetPixel4(DIBDRVBITMAP *dib, int x, int y, DWORD and, DWORD xor)
129 BYTE *ptr = dib->funcs->GetPixelPointer(dib, x, y);
130 BYTE byte_and, byte_xor;
132 if(x & 1) /* upper nibble untouched */
134 byte_and = (and & 0xf) | 0xf0;
135 byte_xor = (xor & 0xf);
137 else
139 byte_and = ((and << 4) & 0xf0) | 0x0f;
140 byte_xor = ((xor << 4) & 0xf0);
143 _DIBDRV_rop8(ptr, byte_and, byte_xor);
146 void _DIBDRV_SetPixel1(DIBDRVBITMAP *dib, int x, int y, DWORD and, DWORD xor)
148 BYTE *ptr;
149 BYTE byte_and = 0, byte_xor = 0, mask;
151 if(and & 1) byte_and = 0xff;
152 if(xor & 1) byte_xor = 0xff;
154 mask = 1 << (7 - (x & 7));
156 byte_and |= ~mask;
157 byte_xor &= mask;
159 ptr = dib->funcs->GetPixelPointer(dib, x, y);
161 _DIBDRV_rop8(ptr, byte_and, byte_xor);
164 /* ------------------------------------------------------------*/
165 /* PIXEL READING */
166 DWORD _DIBDRV_GetPixel32_RGB(const DIBDRVBITMAP *dib, int x, int y)
168 DWORD c = *(DWORD *)dib->funcs->GetPixelPointer(dib, x, y);
169 return
170 ((c & 0x000000ff) << 16) |
171 ((c & 0x0000ff00) ) |
172 ((c & 0x00ff0000) >> 16) |
173 ( c & 0xff000000 ); /* last one for alpha channel */
176 DWORD _DIBDRV_GetPixel32_BITFIELDS(const DIBDRVBITMAP *dib, int x, int y)
178 DWORD *ptr = dib->funcs->GetPixelPointer(dib, x, y);
180 return GetField32(*ptr, dib->redShift, dib->redLen) |
181 GetField32(*ptr, dib->greenShift, dib->greenLen) << 8 |
182 GetField32(*ptr, dib->blueShift, dib->blueLen) << 16;
185 DWORD _DIBDRV_GetPixel24(const DIBDRVBITMAP *dib, int x, int y)
187 BYTE *ptr = dib->funcs->GetPixelPointer(dib, x, y);
188 return ((WORD)ptr[0] << 16) | ((WORD)ptr[1] << 8) | (WORD)ptr[2];
191 DWORD _DIBDRV_GetPixel16_RGB(const DIBDRVBITMAP *dib, int x, int y)
193 WORD c = *(WORD *)dib->funcs->GetPixelPointer(dib, x, y);
194 /* 0RRR|RRGG|GGGB|BBBB */
195 return ((c & 0x7c00) >> 7) | ((c & 0x03e0) << 6) | ((c & 0x001f) << 19);
198 DWORD _DIBDRV_GetPixel16_BITFIELDS(const DIBDRVBITMAP *dib, int x, int y)
200 WORD c = *(WORD *)dib->funcs->GetPixelPointer(dib, x, y);
202 return (((c & dib->blueMask ) >> dib->blueShift ) << (24 - dib->blueLen )) |
203 (((c & dib->greenMask) >> dib->greenShift) << (16 - dib->greenLen)) |
204 (((c & dib->redMask ) >> dib->redShift ) << ( 8 - dib->redLen ));
207 DWORD _DIBDRV_GetPixel8(const DIBDRVBITMAP *dib, int x, int y)
209 BYTE *ptr = dib->funcs->GetPixelPointer(dib, x, y);
210 RGBQUAD *color = dib->colorTable + *ptr;
211 return (color->rgbRed) | (color->rgbGreen << 8) | (color->rgbBlue << 16);
214 DWORD _DIBDRV_GetPixel4(const DIBDRVBITMAP *dib, int x, int y)
216 BYTE *ptr = dib->funcs->GetPixelPointer(dib, x, y), pix;
217 RGBQUAD *color;
219 if(x & 1)
220 pix = *ptr & 0x0f;
221 else
222 pix = *ptr >> 4;
224 color = dib->colorTable + pix;
225 return (color->rgbRed) | (color->rgbGreen << 8) | (color->rgbBlue << 16);
228 DWORD _DIBDRV_GetPixel1(const DIBDRVBITMAP *dib, int x, int y)
230 BYTE *ptr = dib->funcs->GetPixelPointer(dib, x, y), pix;
231 RGBQUAD *color;
233 pix = *ptr;
235 pix >>= (7 - (x & 7));
236 pix &= 1;
238 color = dib->colorTable + pix;
239 return (color->rgbRed) | (color->rgbGreen << 8) | (color->rgbBlue << 16);