1 // FIXME: Use PXLATEOBJ logicalToSystem instead of int *mapping
3 #undef WIN32_LEAN_AND_MEAN
6 #include <ddk/winddi.h>
8 #include <win32k/color.h>
9 #include "../eng/handle.h"
12 #include <win32k/debug1.h>
14 int COLOR_gapStart = 256;
15 int COLOR_gapEnd = -1;
16 int COLOR_gapFilled = 0;
19 static HPALETTE hPrimaryPalette = 0; // used for WM_PALETTECHANGED
20 static HPALETTE hLastRealizedPalette = 0; // UnrealizeObject() needs it
22 const PALETTEENTRY COLOR_sysPalTemplate[NB_RESERVED_COLORS] =
24 // first 10 entries in the system palette
25 // red green blue flags
26 { 0x00, 0x00, 0x00, PC_SYS_USED },
27 { 0x80, 0x00, 0x00, PC_SYS_USED },
28 { 0x00, 0x80, 0x00, PC_SYS_USED },
29 { 0x80, 0x80, 0x00, PC_SYS_USED },
30 { 0x00, 0x00, 0x80, PC_SYS_USED },
31 { 0x80, 0x00, 0x80, PC_SYS_USED },
32 { 0x00, 0x80, 0x80, PC_SYS_USED },
33 { 0xc0, 0xc0, 0xc0, PC_SYS_USED },
34 { 0xc0, 0xdc, 0xc0, PC_SYS_USED },
35 { 0xa6, 0xca, 0xf0, PC_SYS_USED },
37 // ... c_min/2 dynamic colorcells
38 // ... gap (for sparse palettes)
39 // ... c_min/2 dynamic colorcells
41 { 0xff, 0xfb, 0xf0, PC_SYS_USED },
42 { 0xa0, 0xa0, 0xa4, PC_SYS_USED },
43 { 0x80, 0x80, 0x80, PC_SYS_USED },
44 { 0xff, 0x00, 0x00, PC_SYS_USED },
45 { 0x00, 0xff, 0x00, PC_SYS_USED },
46 { 0xff, 0xff, 0x00, PC_SYS_USED },
47 { 0x00, 0x00, 0xff, PC_SYS_USED },
48 { 0xff, 0x00, 0xff, PC_SYS_USED },
49 { 0x00, 0xff, 0xff, PC_SYS_USED },
50 { 0xff, 0xff, 0xff, PC_SYS_USED } // last 10
53 ULONG W32kGetSysColor(int nIndex)
55 PALETTEENTRY *p = COLOR_sysPalTemplate + (nIndex * sizeof(PALETTEENTRY));
56 return RGB(p->peRed, p->peGreen, p->peBlue);
59 HPEN STDCALL W32kGetSysColorPen(int nIndex)
61 return(W32kCreatePen(PS_SOLID, 1, COLOR_sysPalTemplate[nIndex]));
64 HBRUSH STDCALL W32kGetSysColorBrush(int nIndex)
66 return(W32kCreateSolidBrush(COLOR_sysPalTemplate[nIndex]));
69 //forward declarations
70 COLORREF COLOR_LookupNearestColor( PALETTEENTRY* palPalEntry, int size, COLORREF color );
73 const PALETTEENTRY* COLOR_GetSystemPaletteTemplate(void)
75 return (const PALETTEENTRY*)&COLOR_sysPalTemplate;
78 BOOL STDCALL W32kAnimatePalette(HPALETTE hpal,
81 CONST PPALETTEENTRY ppe)
84 if( hPal != W32kGetStockObject(DEFAULT_PALETTE) )
86 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
87 if (!palPtr) return FALSE;
89 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
92 for( u = 0; u < NumEntries; u++ )
93 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
94 PALETTE_Driver->pSetMapping(palPtr, StartIndex, NumEntries, hPal != hPrimaryPalette );
105 HPALETTE STDCALL W32kCreateHalftonePalette(HDC hDC)
110 WORD NumberOfEntries;
111 PALETTEENTRY aEntries[256];
114 Palette.Version = 0x300;
115 Palette.NumberOfEntries = 256;
116 W32kGetSystemPaletteEntries(hDC, 0, 256, Palette.aEntries);
118 for (r = 0; r < 6; r++) {
119 for (g = 0; g < 6; g++) {
120 for (b = 0; b < 6; b++) {
121 i = r + g*6 + b*36 + 10;
122 Palette.aEntries[i].peRed = r * 51;
123 Palette.aEntries[i].peGreen = g * 51;
124 Palette.aEntries[i].peBlue = b * 51;
129 for (i = 216; i < 246; i++) {
130 int v = (i - 216) * 8;
131 Palette.aEntries[i].peRed = v;
132 Palette.aEntries[i].peGreen = v;
133 Palette.aEntries[i].peBlue = v;
136 return W32kCreatePalette((LOGPALETTE *)&Palette);
139 HPALETTE STDCALL W32kCreatePalette(CONST PLOGPALETTE palette)
143 HPALETTE NewPalette = (HPALETTE)EngCreatePalette(PAL_INDEXED, palette->palNumEntries, (PULONG*) palette->palPalEntry, 0, 0, 0);
146 PalObj = (PPALOBJ)AccessUserObject(NewPalette);
148 size = sizeof(LOGPALETTE) + (palette->palNumEntries * sizeof(PALETTEENTRY));
149 PalObj->logpalette = ExAllocatePool(NonPagedPool, size);
150 memcpy(PalObj->logpalette, palette, size);
151 PALETTE_ValidateFlags(PalObj->logpalette->palPalEntry, PalObj->logpalette->palNumEntries);
152 PalObj->logicalToSystem = NULL;
157 BOOL STDCALL W32kGetColorAdjustment(HDC hDC,
158 LPCOLORADJUSTMENT ca)
163 COLORREF STDCALL W32kGetNearestColor(HDC hDC,
166 COLORREF nearest = CLR_INVALID;
170 if( (dc = DC_HandleToPtr(hDC) ) )
172 HPALETTE hpal = (dc->w.hPalette)? dc->w.hPalette : W32kGetStockObject(DEFAULT_PALETTE);
173 palObj = (PPALOBJ)AccessUserObject(hpal);
175 // GDI_ReleaseObj(hdc);
179 nearest = COLOR_LookupNearestColor(palObj->logpalette->palPalEntry,
180 palObj->logpalette->palNumEntries, Color);
181 // FIXME: release hpal!!
182 // GDI_ReleaseObj( hpal );
183 DC_ReleasePtr( hDC );
189 UINT STDCALL W32kGetNearestPaletteIndex(HPALETTE hpal,
192 PPALOBJ palObj = (PPALOBJ)AccessUserObject(hpal);
197 // Return closest match for the given RGB color
198 index = COLOR_PaletteLookupPixel(palObj->logpalette->palPalEntry, palObj->logpalette->palNumEntries, NULL, Color, FALSE);
199 // GDI_ReleaseObj( hpalette );
205 UINT STDCALL W32kGetPaletteEntries(HPALETTE hpal,
213 palPtr = (PPALOBJ)AccessUserObject(hpal);
214 if (!palPtr) return 0;
216 numEntries = palPtr->logpalette->palNumEntries;
217 if (StartIndex + Entries > numEntries) Entries = numEntries - StartIndex;
220 if (StartIndex >= numEntries)
222 // GDI_ReleaseObj( hpalette );
225 memcpy(pe, &palPtr->logpalette->palPalEntry[StartIndex], Entries * sizeof(PALETTEENTRY));
226 for(numEntries = 0; numEntries < Entries ; numEntries++)
227 if (pe[numEntries].peFlags & 0xF0)
228 pe[numEntries].peFlags = 0;
231 // GDI_ReleaseObj( hpalette );
235 UINT STDCALL W32kGetSystemPaletteEntries(HDC hDC,
243 if (!(dc = AccessUserObject(hdc))) return 0;
247 Entries = dc->devCaps->sizePalette;
251 if (StartIndex >= dc->devCaps->sizePalette)
257 if (StartIndex + Entries >= dc->devCaps->sizePalette) Entries = dc->devCaps->sizePalette - StartIndex;
259 for (i = 0; i < Entries; i++)
261 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry(StartIndex + i);
265 // GDI_ReleaseObj(hdc);
269 UINT STDCALL W32kGetSystemPaletteUse(HDC hDC)
275 The RealizePalette function modifies the palette for the device associated with the specified device context. If the device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device context is a display DC, the physical palette for that device is modified.
277 A logical palette is a buffer between color-intensive applications and the system, allowing these applications to use as many colors as needed without interfering with colors displayed by other windows.
279 1= IF DRAWING TO A DEVICE
280 -- If it is a paletted bitmap, and is not an identity palette, then an XLATEOBJ is created between the logical palette and
282 -- If it is an RGB palette, then an XLATEOBJ is created between the RGB values and the system palette.
284 2= IF DRAWING TO A MEMORY DC\BITMAP
285 -- If it is a paletted bitmap, and is not an identity palette, then an XLATEOBJ is created between the logical palette and
287 -- If it is an RGB palette, then an XLATEOBJ is created between the RGB values and the dc palette.
289 UINT STDCALL W32kRealizePalette(HDC hDC)
291 PPALOBJ palPtr, sysPtr;
292 PPALGDI palGDI, sysGDI;
295 HPALETTE systemPalette;
299 dc = DC_HandleToPtr(hDC);
303 palPtr = (PPALOBJ)AccessUserObject(dc->w.hPalette);
304 SurfGDI = (PSURFGDI)AccessInternalObjectFromUserObject(dc->Surface);
305 systemPalette = W32kGetStockObject(STOCK_DEFAULT_PALETTE);
306 sysPtr = (PPALOBJ)AccessInternalObject(systemPalette);
307 palGDI = (PPALGDI)AccessInternalObject(dc->w.hPalette);
308 sysGDI = (PPALGDI)AccessInternalObject(systemPalette);
310 // Step 1: Create mapping of system palette\DC palette
311 realized = PALETTE_SetMapping(palPtr, 0, palPtr->logpalette->palNumEntries,
312 (dc->w.hPalette != hPrimaryPalette) ||
313 (dc->w.hPalette == W32kGetStockObject(DEFAULT_PALETTE)));
316 // The RealizePalette function modifies the palette for the device associated with the specified device context. If the
317 // device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device
318 // context is a display DC, the physical palette for that device is modified.
319 if(dc->w.flags == DC_MEMORY)
322 DbgPrint("win32k: realizepalette unimplemented step 2 for DC_MEMORY");
324 if(SurfGDI->SetPalette)
326 success = SurfGDI->SetPalette(dc->PDev, sysPtr, 0, 0, sysPtr->logpalette->palNumEntries);
330 // Step 3: Create the XLATEOBJ for device managed DCs
331 if(dc->w.flags != DC_MEMORY)
334 palPtr->logicalToSystem = EngCreateXlate(sysGDI->Mode, palGDI->Mode, systemPalette, dc->w.hPalette);
337 // GDI_ReleaseObj(dc->w.hPalette);
338 // GDI_ReleaseObj(hdc);
343 BOOL STDCALL W32kResizePalette(HPALETTE hpal,
346 /* PPALOBJ palPtr = (PPALOBJ)AccessUserObject(hPal);
347 UINT cPrevEnt, prevVer;
348 INT prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
349 PXLATEOBJ XlateObj = NULL;
351 if(!palPtr) return FALSE;
352 cPrevEnt = palPtr->logpalette->palNumEntries;
353 prevVer = palPtr->logpalette->palVersion;
354 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) + sizeof(int*) + sizeof(GDIOBJHDR);
355 size += sizeof(int*) + sizeof(GDIOBJHDR);
356 XlateObj = palPtr->logicalToSystem;
358 if (!(palPtr = GDI_ReallocObject(size, hPal, palPtr))) return FALSE;
362 PXLATEOBJ NewXlateObj = (int*) HeapReAlloc(GetProcessHeap(), 0, XlateObj, cEntries * sizeof(int));
363 if(NewXlateObj == NULL)
365 ERR("Can not resize logicalToSystem -- out of memory!");
366 GDI_ReleaseObj( hPal );
369 palPtr->logicalToSystem = NewXlateObj;
372 if(cEntries > cPrevEnt)
374 if(XlateObj) memset(palPtr->logicalToSystem + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
375 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
376 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), cEntries - cPrevEnt );
378 palPtr->logpalette->palNumEntries = cEntries;
379 palPtr->logpalette->palVersion = prevVer;
380 // GDI_ReleaseObj( hPal );
387 * Select logical palette into device context.
388 * \param hDC handle to the device context
389 * \param hpal handle to the palette
390 * \param ForceBackground If this value is FALSE the logical palette will be copied to the device palette only when the applicatioon
391 * is in the foreground. If this value is TRUE then map the colors in the logical palette to the device
392 * palette colors in the best way.
393 * \return old palette
395 * \todo implement ForceBackground == TRUE
397 HPALETTE STDCALL W32kSelectPalette(HDC hDC,
399 BOOL ForceBackground)
404 // FIXME: mark the palette as a [fore\back]ground pal
405 dc = DC_HandleToPtr(hDC);
407 oldPal = dc->w.hPalette;
408 dc->w.hPalette = hpal;
409 DC_ReleasePtr( hDC );
415 BOOL STDCALL W32kSetColorAdjustment(HDC hDC,
416 CONST LPCOLORADJUSTMENT ca)
421 UINT STDCALL W32kSetPaletteEntries(HPALETTE hpal,
424 CONST LPPALETTEENTRY pe)
429 palPtr = (PPALOBJ)AccessUserObject(hpal);
430 if (!palPtr) return 0;
432 numEntries = palPtr->logpalette->palNumEntries;
433 if (Start >= numEntries)
435 // GDI_ReleaseObj( hpalette );
438 if (Start + Entries > numEntries) Entries = numEntries - Start;
439 memcpy(&palPtr->logpalette->palPalEntry[Start], pe, Entries * sizeof(PALETTEENTRY));
440 PALETTE_ValidateFlags(palPtr->logpalette->palPalEntry, palPtr->logpalette->palNumEntries);
441 ExFreePool(palPtr->logicalToSystem);
442 palPtr->logicalToSystem = NULL;
443 // GDI_ReleaseObj( hpalette );
447 UINT STDCALL W32kSetSystemPaletteUse(HDC hDC,
453 BOOL STDCALL W32kUnrealizeObject(HGDIOBJ hgdiobj)
458 BOOL STDCALL W32kUpdateColors(HDC hDC)
464 if (!(dc = AccessUserObject(hDC))) return 0;
465 size = dc->devCaps->sizePalette;
466 // GDI_ReleaseObj( hDC );
468 if (Callout.WindowFromDC)
470 hWnd = Callout.WindowFromDC( hDC );
472 // Docs say that we have to remap current drawable pixel by pixel
473 // but it would take forever given the speed of XGet/PutPixel.
474 if (hWnd && size) Callout.RedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
479 int COLOR_PaletteLookupPixel(PALETTEENTRY *palPalEntry, int size,
480 PXLATEOBJ XlateObj, COLORREF col, BOOL skipReserved)
482 int i, best = 0, diff = 0x7fffffff;
485 for( i = 0; i < size && diff ; i++ )
487 if(!(palPalEntry[i].peFlags & PC_SYS_USED) || (skipReserved && palPalEntry[i].peFlags & PC_SYS_RESERVED))
490 r = palPalEntry[i].peRed - GetRValue(col);
491 g = palPalEntry[i].peGreen - GetGValue(col);
492 b = palPalEntry[i].peBlue - GetBValue(col);
496 if( r < diff ) { best = i; diff = r; }
498 return (XlateObj->pulXlate) ? XlateObj->pulXlate[best] : best;
501 COLORREF COLOR_LookupNearestColor( PALETTEENTRY* palPalEntry, int size, COLORREF color )
503 unsigned char spec_type = color >> 24;
505 PALETTEENTRY *COLOR_sysPal = (PALETTEENTRY*)ReturnSystemPalette();
507 // we need logical palette for PALETTERGB and PALETTEINDEX colorrefs
509 if( spec_type == 2 ) /* PALETTERGB */
510 color = *(COLORREF*)(palPalEntry + COLOR_PaletteLookupPixel(palPalEntry,size,NULL,color,FALSE));
512 else if( spec_type == 1 ) /* PALETTEINDEX */
514 if( (i = color & 0x0000ffff) >= size )
516 DbgPrint("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color, i);
517 color = *(COLORREF*)palPalEntry;
519 else color = *(COLORREF*)(palPalEntry + i);
523 return (0x00ffffff & *(COLORREF*)(COLOR_sysPal + COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL, color, FALSE)));
526 int COLOR_PaletteLookupExactIndex( PALETTEENTRY* palPalEntry, int size,
530 BYTE r = GetRValue(col), g = GetGValue(col), b = GetBValue(col);
531 for( i = 0; i < size; i++ )
533 if( palPalEntry[i].peFlags & PC_SYS_USED ) /* skips gap */
534 if(palPalEntry[i].peRed == r && palPalEntry[i].peGreen == g && palPalEntry[i].peBlue == b) return i;