87016dab6519e336e2a5d228b57bd508ddddaa48
[wine/hacks.git] / dlls / winedib.drv / palette.c
blob87016dab6519e336e2a5d228b57bd508ddddaa48
1 /*
2 * DIBDRV palette objects
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 /* maps a colorref to actual color */
29 COLORREF _DIBDRV_MapColor(DIBDRVPHYSDEV *physDev, COLORREF color)
31 WORD index;
32 RGBQUAD *palColor;
33 HPALETTE hPal;
34 PALETTEENTRY paletteEntry;
36 switch(color >> 24)
38 case 0x10 : /* DIBINDEX */
39 MAYBE(TRACE("DIBINDEX Color is %08x\n", color));
40 index = color & 0xffff;
41 if(index >= physDev->physBitmap.colorTableSize)
43 WARN("DIBINDEX color out of range\n");
44 return 0;
46 palColor = physDev->physBitmap.colorTable + index;
47 MAYBE(TRACE("Returning color %08x\n", RGB(palColor->rgbRed, palColor->rgbGreen, palColor->rgbBlue)));
48 return RGB(palColor->rgbRed, palColor->rgbGreen, palColor->rgbBlue);
50 case 0x01: /* PALETTEINDEX */
51 MAYBE(TRACE("PALETTEINDEX Color is %08x\n", color));
52 index = color & 0xffff;
53 if(!(hPal = GetCurrentObject(physDev->hdc, OBJ_PAL)))
55 ERR("Couldn't get palette\n");
56 return 0;
58 if (!GetPaletteEntries(hPal, index, 1, &paletteEntry))
60 WARN("PALETTEINDEX(%x) : index %d is out of bounds, assuming black\n", color, index);
61 return 0;
63 MAYBE(TRACE("Returning color %08x\n", RGB(paletteEntry.peRed, paletteEntry.peGreen, paletteEntry.peBlue)));
64 return RGB(paletteEntry.peRed, paletteEntry.peGreen, paletteEntry.peBlue);
66 case 0x02: /* PALETTERGB */
67 return _DIBDRV_GetNearestColor(&physDev->physBitmap, color & 0xffffff);
69 default:
70 /* RGB color -- we must process special case for monochrome bitmaps */
71 if(physDev->physBitmap.bitCount == 1)
73 RGBQUAD *back = physDev->physBitmap.colorTable;
74 RGBQUAD *fore = back+1;
75 if(fore->rgbRed * fore->rgbRed + fore->rgbGreen * fore->rgbGreen + fore->rgbBlue * fore->rgbBlue <
76 back->rgbRed * back->rgbRed + back->rgbGreen * back->rgbGreen + back->rgbBlue * back->rgbBlue)
78 fore = back;
79 back = fore + 1;
81 if ( ((color >> 16) & 0xff) + ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2)
82 return RGB(fore->rgbRed, fore->rgbGreen, fore->rgbBlue);
83 else
84 return RGB(back->rgbRed, back->rgbGreen, back->rgbBlue);
86 else
87 return color;
91 /***********************************************************************
92 * DIBDRV_RealizePalette
94 UINT DIBDRV_RealizePalette( DIBDRVPHYSDEV *physDev, HPALETTE hpal, BOOL primary )
96 UINT res = 0;
98 MAYBE(TRACE("physDev:%p, hpal:%p, primary:%s\n", physDev, hpal, (primary ? "TRUE" : "FALSE")));
100 if(physDev && physDev->hasDIB)
102 /* DIB section selected in, additional (if needed) engine code */
103 ONCE(FIXME("STUB\n"));
104 res = 0;
106 else
108 /* we should in any case call X11 function, as UnrealizePalette() doesn't
109 * take a physDev parameter */
110 res = _DIBDRV_GetDisplayDriver()->pRealizePalette(physDev ? physDev->X11PhysDev : NULL, hpal, primary);
114 return res;
117 /***********************************************************************
118 * DIBDRV_UnrealizePalette
120 BOOL DIBDRV_UnrealizePalette( HPALETTE hpal )
122 BOOL res;
124 MAYBE(TRACE("hpal:%p\n", hpal));
126 /* we should in any case call X11 function, as UnrealizePalette() doesn't
127 * take a physDev parameter */
128 res = _DIBDRV_GetDisplayDriver()->pUnrealizePalette(hpal);
130 /* additional Engine code here, if needed */
131 ONCE(FIXME("STUB\n"));
133 return res;
136 /***********************************************************************
137 * DIBDRV_GetSystemPaletteEntries
139 UINT DIBDRV_GetSystemPaletteEntries( DIBDRVPHYSDEV *physDev, UINT start, UINT count,
140 LPPALETTEENTRY entries )
142 UINT res;
144 MAYBE(TRACE("physDev:%p, start:%d, count:%d, entries:%p\n", physDev, start, count, entries));
146 if(physDev->hasDIB)
148 /* DIB section selected in, use DIB Engine */
149 ONCE(FIXME("STUB\n"));
150 res = 0;
152 else
154 /* DDB selected in, use X11 driver */
155 res = _DIBDRV_GetDisplayDriver()->pGetSystemPaletteEntries(physDev->X11PhysDev, start, count, entries);
157 return res;
160 /***********************************************************************
161 * DIBDRV_GetNearestColor
163 COLORREF DIBDRV_GetNearestColor( DIBDRVPHYSDEV *physDev, COLORREF color )
165 COLORREF res;
167 MAYBE(TRACE("physDev:%p, color:%x\n", physDev, color));
169 if(physDev->hasDIB)
171 /* DIB section selected in, use DIB Engine */
172 ONCE(FIXME("STUB\n"));
173 res = 0;
175 else
177 /* DDB selected in, use X11 driver */
178 res = _DIBDRV_GetDisplayDriver()->pGetNearestColor(physDev->X11PhysDev, color);
180 return res;
183 /***********************************************************************
184 * DIBDRV_RealizeDefaultPalette
186 UINT DIBDRV_RealizeDefaultPalette( DIBDRVPHYSDEV *physDev )
188 UINT res;
189 #ifdef DIBDRV_ENABLE_MAYBE
190 int i;
191 RGBQUAD *q;
192 #endif
194 MAYBE(TRACE("physDev:%p\n", physDev));
196 if(physDev->hasDIB)
198 /* DIB section selected in, use DIB Engine */
199 ONCE(FIXME("STUB\n"));
200 /* HACK - we can't get the dib color table during SelectBitmap since it hasn't
201 been initialized yet. This is called from DC_InitDC so it's a convenient place
202 to grab the color table. */
203 MAYBE(TRACE("Color table size = %d, Color table = %p\n", physDev->physBitmap.colorTableSize, physDev->physBitmap.colorTable));
204 if(!physDev->physBitmap.colorTableGrabbed)
206 MAYBE(TRACE("Grabbing palette\n"));
207 physDev->physBitmap.colorTable = HeapAlloc(GetProcessHeap(), 0, sizeof(physDev->physBitmap.colorTable[0]) * physDev->physBitmap.colorTableSize);
208 GetDIBColorTable(physDev->hdc, 0, physDev->physBitmap.colorTableSize, physDev->physBitmap.colorTable);
209 #ifdef DIBDRV_ENABLE_MAYBE
210 for(i = 0; i < physDev->physBitmap.colorTableSize; i++)
212 q = physDev->physBitmap.colorTable + i;
213 TRACE(" %03d : R%03d G%03d B%03d\n", i, q->rgbRed, q->rgbGreen, q->rgbBlue);
215 #endif
216 physDev->physBitmap.colorTableGrabbed = TRUE;
218 res = 0;
220 else
222 /* DDB selected in, use X11 driver */
223 res = _DIBDRV_GetDisplayDriver()->pRealizeDefaultPalette(physDev->X11PhysDev);
225 return res;
228 BOOL DIBDRV_GetICMProfile(DIBDRVPHYSDEV *physDev, LPDWORD lpcbName, LPWSTR lpszFilename)
230 BOOL res;
232 MAYBE(TRACE("physDev:%p, lpcpName:%p, lpszFilename:%p\n", physDev, lpcbName, lpszFilename));
234 if(physDev->hasDIB)
236 /* DIB section selected in, use DIB Engine */
237 ONCE(FIXME("STUB\n"));
239 res = 0;
241 else
243 /* DDB selected in, use X11 driver */
244 res = _DIBDRV_GetDisplayDriver()->pGetICMProfile(physDev->X11PhysDev, lpcbName, lpszFilename);
246 return res;