Release 951124
[wine/multimedia.git] / objects / palette.c
blobf49f066b674548ab6378d9b191b7c489ebc53d7c
1 /*
2 * GDI palette objects
4 * Copyright 1993,1994 Alexandre Julliard
6 static char Copyright[] = "Copyright Alexandre Julliard, 1993,1994";
7 */
8 #include <stdlib.h>
9 #include <string.h>
10 #include <limits.h>
12 #if !defined (MAXINT)
13 #include <limits.h>
14 #define MAXINT INT_MAX
15 #endif
17 #include <X11/Xlib.h>
18 #include "color.h"
19 #include "palette.h"
20 #include "stddebug.h"
21 /* #define DEBUG_PALETTE */
22 #include "debug.h"
24 static WORD SystemPaletteUse = SYSPAL_STATIC; /* currently not considered */
27 /***********************************************************************
28 * PALETTE_GetNearestIndexAndColor
30 static WORD PALETTE_GetNearestIndexAndColor(HPALETTE hpalette, COLORREF *color)
32 int i, minDist, dist;
33 WORD index = 0;
34 BYTE r, g, b;
35 PALETTEENTRY * entry;
36 PALETTEOBJ * palPtr;
38 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
39 if (!palPtr) return 0;
41 if ((COLOR_WinColormap != DefaultColormapOfScreen(screen)) &&
42 (hpalette == STOCK_DEFAULT_PALETTE))
44 if ((*color & 0xffffff) == 0) return 0; /* Entry 0 is black */
45 if ((*color & 0xffffff) == 0xffffff) /* Max entry is white */
46 return palPtr->logpalette.palNumEntries - 1;
49 r = GetRValue(*color);
50 g = GetGValue(*color);
51 b = GetBValue(*color);
53 entry = palPtr->logpalette.palPalEntry;
54 for (i = 0, minDist = MAXINT; minDist !=0 &&
55 i < palPtr->logpalette.palNumEntries ; i++)
57 if (entry->peFlags != 0xff)
59 dist = (r - entry->peRed) * (r - entry->peRed) +
60 (g - entry->peGreen) * (g - entry->peGreen) +
61 (b - entry->peBlue) * (b - entry->peBlue);
62 if (dist < minDist)
64 minDist = dist;
65 index = i;
68 entry++;
70 entry = &palPtr->logpalette.palPalEntry[index];
71 *color = RGB( entry->peRed, entry->peGreen, entry->peBlue );
72 return index;
76 /***********************************************************************
77 * CreatePalette (GDI.360)
79 HPALETTE CreatePalette( LOGPALETTE * palette )
81 PALETTEOBJ * palettePtr;
82 HPALETTE hpalette;
83 int size;
85 size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
86 hpalette = GDI_AllocObject( sizeof(GDIOBJHDR) + size, PALETTE_MAGIC );
87 if (!hpalette) return 0;
88 palettePtr = (PALETTEOBJ *) GDI_HEAP_LIN_ADDR( hpalette );
89 memcpy( &palettePtr->logpalette, palette, size );
90 return hpalette;
94 /***********************************************************************
95 * GetPaletteEntries (GDI.363)
97 WORD GetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
98 LPPALETTEENTRY entries )
100 PALETTEOBJ * palPtr;
101 int numEntries;
103 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
104 if (!palPtr) return 0;
105 numEntries = palPtr->logpalette.palNumEntries;
106 if (start >= numEntries) return 0;
107 if (start+count > numEntries) count = numEntries - start;
108 memcpy( entries, &palPtr->logpalette.palPalEntry[start],
109 count * sizeof(PALETTEENTRY) );
110 return count;
114 /***********************************************************************
115 * SetPaletteEntries (GDI.364)
117 WORD SetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
118 LPPALETTEENTRY entries )
120 PALETTEOBJ * palPtr;
121 int numEntries;
123 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
124 if (!palPtr) return 0;
125 numEntries = palPtr->logpalette.palNumEntries;
126 if (start >= numEntries) return 0;
127 if (start+count > numEntries) count = numEntries - start;
128 memcpy( &palPtr->logpalette.palPalEntry[start], entries,
129 count * sizeof(PALETTEENTRY) );
130 return count;
133 /***********************************************************************
134 * ResizePalette (GDI.368)
136 BOOL ResizePalette(HPALETTE hPal, UINT cEntries)
138 fprintf(stdnimp,"ResizePalette: empty stub! \n");
139 return FALSE;
142 /***********************************************************************
143 * AnimatePalette (GDI.367)
145 void AnimatePalette(HPALETTE hPal, UINT StartIndex, UINT NumEntries,
146 LPPALETTEENTRY PaletteColors)
148 fprintf(stdnimp,"AnimatePalette: empty stub! \n");
151 /***********************************************************************
152 * SetSystemPaletteUse (GDI.373)
153 * Should this be per DC rather than system wide?
154 * Currently, it does not matter as the use is only set and returned,
155 * but not taken into account
157 WORD SetSystemPaletteUse( HDC hdc, WORD use)
159 WORD old=SystemPaletteUse;
160 printf("SetSystemPaletteUse("NPFMT",%04X) // empty stub !!!\n", hdc, use);
161 SystemPaletteUse=use;
162 return old;
165 /***********************************************************************
166 * GetSystemPaletteUse (GDI.374)
168 WORD GetSystemPaletteUse( HDC hdc )
170 printf("GetSystemPaletteUse("NPFMT") // empty stub !!!\n", hdc);
171 return SystemPaletteUse;
175 /***********************************************************************
176 * GetSystemPaletteEntries (GDI.375)
178 WORD GetSystemPaletteEntries( HDC hdc, WORD start, WORD count,
179 LPPALETTEENTRY entries )
181 WORD i;
182 DC *dc;
183 XColor color;
185 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
186 if (start >= dc->w.devCaps->sizePalette) return 0;
187 if (start+count >= dc->w.devCaps->sizePalette)
188 count = dc->w.devCaps->sizePalette - start;
189 for (i = 0; i < count; i++)
191 color.pixel = start + i;
192 XQueryColor( display, COLOR_WinColormap, &color );
193 entries[i].peRed = color.red >> 8;
194 entries[i].peGreen = color.green >> 8;
195 entries[i].peBlue = color.blue >> 8;
196 entries[i].peFlags = 0;
198 return count;
202 /***********************************************************************
203 * GetNearestPaletteIndex (GDI.370)
205 WORD GetNearestPaletteIndex( HPALETTE hpalette, COLORREF color )
207 WORD index = PALETTE_GetNearestIndexAndColor( hpalette, &color );
208 dprintf_palette(stddeb,"GetNearestPaletteIndex("NPFMT",%06lx): returning %d\n",
209 hpalette, color, index );
210 return index;
214 /***********************************************************************
215 * GetNearestColor (GDI.154)
217 COLORREF GetNearestColor( HDC hdc, COLORREF color )
219 COLORREF nearest = color;
220 DC *dc;
222 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
223 PALETTE_GetNearestIndexAndColor( dc->w.hPalette, &nearest );
224 dprintf_palette(stddeb,"GetNearestColor(%06lx): returning %06lx\n",
225 color, nearest );
226 return nearest;
230 /***********************************************************************
231 * PALETTE_GetObject
233 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
235 if (count > sizeof(WORD)) count = sizeof(WORD);
236 memcpy( buffer, &palette->logpalette.palNumEntries, count );
237 return count;
241 /***********************************************************************
242 * GDISelectPalette (GDI.361)
244 HPALETTE GDISelectPalette( HDC hdc, HPALETTE hpal )
246 HPALETTE prev;
247 DC *dc;
249 dprintf_palette(stddeb, "GDISelectPalette: "NPFMT" "NPFMT"\n", hdc, hpal );
250 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
251 prev = dc->w.hPalette;
252 dc->w.hPalette = hpal;
253 if (hpal != STOCK_DEFAULT_PALETTE) COLOR_SetMapping( dc, 0, 0, 0 );
254 else RealizeDefaultPalette( hdc ); /* Always realize default palette */
255 return prev;
259 /***********************************************************************
260 * GDIRealizePalette (GDI.362)
262 UINT GDIRealizePalette( HDC hdc )
264 dprintf_palette(stdnimp, "GDIRealizePalette: "NPFMT"\n", hdc );
265 return 0;
269 /***********************************************************************
270 * SelectPalette (USER.282)
272 HPALETTE SelectPalette(HDC hDC, HPALETTE hPal, BOOL bForceBackground)
274 return GDISelectPalette( hDC, hPal );
278 /***********************************************************************
279 * RealizePalette (USER.283)
281 UINT RealizePalette(HDC hDC)
283 return GDIRealizePalette( hDC );