DIB Engine: implement most engine functions
[wine/hacks.git] / dlls / winedib.drv / primitives_color.c
blob3ccc2e0d914ec070f72f5d51d8a1863015460c6a
1 /*
2 * DIB Engine color 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 PutField32(DWORD field, int shift, int len)
32 shift = shift - (8 - len);
33 if (len <= 8)
34 field &= (((1 << len) - 1) << (8 - len));
35 if (shift < 0)
36 field >>= -shift;
37 else
38 field <<= shift;
39 return field;
42 static inline WORD PutField16(WORD field, int shift, int len)
44 shift = shift - (8 - len);
45 if (len <= 8)
46 field &= (((1 << len) - 1) << (8 - len));
47 if (shift < 0)
48 field >>= -shift;
49 else
50 field <<= shift;
51 return field;
54 /* ------------------------------------------------------------*/
55 /* COLOR FUNCTIONS */
56 DWORD _DIBDRV_ColorToPixel32_RGB(const DIBDRVBITMAP *dib, COLORREF color)
58 return ( ((color >> 16) & 0xff) | (color & 0xff00) | ((color << 16) & 0xff0000) );
61 DWORD _DIBDRV_ColorToPixel32_BITFIELDS(const DIBDRVBITMAP *dib, COLORREF color)
63 DWORD r,g,b;
65 r = GetRValue(color);
66 g = GetGValue(color);
67 b = GetBValue(color);
69 return PutField32(r, dib->redShift, dib->redLen) |
70 PutField32(g, dib->greenShift, dib->greenLen) |
71 PutField32(b, dib->blueShift, dib->blueLen);
74 DWORD _DIBDRV_ColorToPixel24(const DIBDRVBITMAP *dib, COLORREF color)
76 return ( ((color >> 16) & 0xff) | (color & 0xff00) | ((color << 16) & 0xff0000) );
79 DWORD _DIBDRV_ColorToPixel16_RGB(const DIBDRVBITMAP *dib, COLORREF color)
81 return ( ((color >> 19) & 0x001f) | ((color >> 6) & 0x03e0) | ((color << 7) & 0x7c00) );
84 DWORD _DIBDRV_ColorToPixel16_BITFIELDS(const DIBDRVBITMAP *dib, COLORREF color)
86 DWORD r,g,b;
88 r = GetRValue(color);
89 g = GetGValue(color);
90 b = GetBValue(color);
92 return PutField16(r, dib->redShift, dib->redLen) |
93 PutField16(g, dib->greenShift, dib->greenLen) |
94 PutField16(b, dib->blueShift, dib->blueLen);
97 DWORD _DIBDRV_ColorToPixelColortable(const DIBDRVBITMAP *dib, COLORREF color)
99 int i, best_index = 0;
100 DWORD r, g, b;
101 DWORD diff, best_diff = 0xffffffff;
103 r = GetRValue(color);
104 g = GetGValue(color);
105 b = GetBValue(color);
107 /* just in case it's being called without color table
108 properly initialized */
109 if(!dib->colorTableGrabbed)
110 return 0;
112 /* for monochrome bitmaps, color is background
113 if not matching foreground */
114 if(dib->colorTableSize == 2)
116 RGBQUAD *fore = dib->colorTable + 1;
117 if(r == fore->rgbRed && g == fore->rgbGreen && b == fore->rgbBlue)
118 return 1;
119 return 0;
122 for(i = 0; i < dib->colorTableSize; i++)
124 RGBQUAD *cur = dib->colorTable + i;
125 diff = (r - cur->rgbRed) * (r - cur->rgbRed)
126 + (g - cur->rgbGreen) * (g - cur->rgbGreen)
127 + (b - cur->rgbBlue) * (b - cur->rgbBlue);
129 if(diff == 0)
131 best_index = i;
132 break;
135 if(diff < best_diff)
137 best_diff = diff;
138 best_index = i;
141 return best_index;