Release 950727
[wine] / objects / palette.c
1 /*
2  * GDI palette objects
3  *
4  * Copyright 1993,1994 Alexandre Julliard
5  *
6 static char Copyright[] = "Copyright  Alexandre Julliard, 1993,1994";
7 */
8 #include <stdlib.h>
9 #include <string.h>
10 #include <limits.h>
11
12 #if !defined  (MAXINT)
13 #include <limits.h>
14 #define MAXINT INT_MAX
15 #endif
16
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"
23
24 static WORD SystemPaletteUse = SYSPAL_STATIC;   /* currently not considered */
25
26 /***********************************************************************
27  *           CreatePalette    (GDI.360)
28  */
29 HPALETTE CreatePalette( LOGPALETTE * palette )
30 {
31     PALETTEOBJ * palettePtr;
32     HPALETTE hpalette;
33     int size;
34
35     size = sizeof(LOGPALETTE) + (palette->palNumEntries - 1) * sizeof(PALETTEENTRY);
36     hpalette = GDI_AllocObject( sizeof(GDIOBJHDR) + size, PALETTE_MAGIC );
37     if (!hpalette) return 0;
38     palettePtr = (PALETTEOBJ *) GDI_HEAP_LIN_ADDR( hpalette );
39     memcpy( &palettePtr->logpalette, palette, size );
40     return hpalette;
41 }
42
43
44 /***********************************************************************
45  *           GetPaletteEntries    (GDI.363)
46  */
47 WORD GetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
48                         LPPALETTEENTRY entries )
49 {
50     PALETTEOBJ * palPtr;
51     int numEntries;
52         
53     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
54     if (!palPtr) return 0;
55     numEntries = palPtr->logpalette.palNumEntries;
56     if (start >= numEntries) return 0;
57     if (start+count > numEntries) count = numEntries - start;
58     memcpy( entries, &palPtr->logpalette.palPalEntry[start],
59             count * sizeof(PALETTEENTRY) );
60     return count;
61 }
62
63
64 /***********************************************************************
65  *           SetPaletteEntries    (GDI.364)
66  */
67 WORD SetPaletteEntries( HPALETTE hpalette, WORD start, WORD count,
68                         LPPALETTEENTRY entries )
69 {
70     PALETTEOBJ * palPtr;
71     int numEntries;
72         
73     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
74     if (!palPtr) return 0;
75     numEntries = palPtr->logpalette.palNumEntries;
76     if (start >= numEntries) return 0;
77     if (start+count > numEntries) count = numEntries - start;
78     memcpy( &palPtr->logpalette.palPalEntry[start], entries,
79             count * sizeof(PALETTEENTRY) );
80     return count;
81 }
82
83
84 /***********************************************************************
85  *           SetSystemPaletteUse    (GDI.373)
86  *      Should this be per DC rather than system wide?
87  *  Currently, it does not matter as the use is only set and returned,
88  *  but not taken into account
89  */
90 WORD SetSystemPaletteUse( HDC hdc, WORD use)
91 {
92          WORD old=SystemPaletteUse;
93          printf("SetSystemPaletteUse(%04X,%04X) // empty stub !!!\n", hdc, use);
94          SystemPaletteUse=use;
95          return old;
96 }
97
98 /***********************************************************************
99  *           GetSystemPaletteUse    (GDI.374)
100  */
101 WORD GetSystemPaletteUse( HDC hdc )
102 {
103         printf("GetSystemPaletteUse(%04X) // empty stub !!!\n", hdc);
104         return SystemPaletteUse;
105 }
106
107
108 /***********************************************************************
109  *           GetSystemPaletteEntries    (GDI.375)
110  */
111 WORD GetSystemPaletteEntries( HDC hdc, WORD start, WORD count,
112                               LPPALETTEENTRY entries )
113 {
114     WORD i;
115     DC *dc;
116     XColor color;
117
118     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
119     if (start >= dc->w.devCaps->sizePalette) return 0;
120     if (start+count >= dc->w.devCaps->sizePalette)
121         count = dc->w.devCaps->sizePalette - start;
122     for (i = 0; i < count; i++)
123     {
124         color.pixel = start + i;
125         XQueryColor( display, COLOR_WinColormap, &color );
126         entries[i].peRed   = color.red >> 8;
127         entries[i].peGreen = color.green >> 8;
128         entries[i].peBlue  = color.blue >> 8;
129         entries[i].peFlags = 0; 
130     }
131     return count;
132 }
133
134
135 /***********************************************************************
136  *           GetNearestPaletteIndex    (GDI.370)
137  */
138 WORD GetNearestPaletteIndex( HPALETTE hpalette, COLORREF color )
139 {
140     int i, minDist, dist;
141     WORD index = 0;
142     BYTE r, g, b;
143     PALETTEENTRY * entry;
144     PALETTEOBJ * palPtr;
145     
146     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
147     if (!palPtr) return 0;
148
149     if ((COLOR_WinColormap != DefaultColormapOfScreen(screen)) &&
150         (hpalette == STOCK_DEFAULT_PALETTE))
151     {
152         if ((color & 0xffffff) == 0) return 0;  /* Entry 0 is black */
153         if ((color & 0xffffff) == 0xffffff)     /* Max entry is white */
154             return palPtr->logpalette.palNumEntries - 1;
155     }
156     
157     r = GetRValue(color);
158     g = GetGValue(color);
159     b = GetBValue(color);
160
161     entry = palPtr->logpalette.palPalEntry;
162     for (i = 0, minDist = MAXINT; minDist !=0 &&
163          i < palPtr->logpalette.palNumEntries ; i++)
164     {
165         if (entry->peFlags != 0xff)
166         {
167             dist = (r - entry->peRed) * (r - entry->peRed) +
168                    (g - entry->peGreen) * (g - entry->peGreen) +
169                    (b - entry->peBlue) * (b - entry->peBlue);   
170             if (dist < minDist)
171             {
172                 minDist = dist;
173                 index = i;
174             }
175         }
176         entry++;
177     }
178     dprintf_palette(stddeb,"GetNearestPaletteIndex(%x,%06lx): returning %d\n", 
179              hpalette, color, index );
180     return index;
181 }
182
183
184 /***********************************************************************
185  *           PALETTE_GetObject
186  */
187 int PALETTE_GetObject( PALETTEOBJ * palette, int count, LPSTR buffer )
188 {
189     if (count > sizeof(WORD)) count = sizeof(WORD);
190     memcpy( buffer, &palette->logpalette.palNumEntries, count );
191     return count;
192 }
193
194
195 /***********************************************************************
196  *           GDISelectPalette    (GDI.361)
197  */
198 HPALETTE GDISelectPalette( HDC hdc, HPALETTE hpal )
199 {
200     HPALETTE prev;
201     DC *dc;
202
203     dprintf_palette(stddeb, "GDISelectPalette: %d %d\n", hdc, hpal );
204     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
205     prev = dc->w.hPalette;
206     dc->w.hPalette = hpal;
207     if (hpal != STOCK_DEFAULT_PALETTE) COLOR_SetMapping( dc, 0, 0, 0 );
208     else RealizeDefaultPalette( hdc );  /* Always realize default palette */
209     return prev;
210 }
211
212
213 /***********************************************************************
214  *           GDIRealizePalette    (GDI.362)
215  */
216 UINT GDIRealizePalette( HDC hdc )
217 {
218     dprintf_palette(stdnimp, "GDIRealizePalette: %d\n", hdc );
219     return 0;
220 }
221
222
223 /***********************************************************************
224  *           SelectPalette    (USER.282)
225  */
226 HPALETTE SelectPalette(HDC hDC, HPALETTE hPal, BOOL bForceBackground)
227 {
228     return GDISelectPalette( hDC, hPal );
229 }
230
231
232 /***********************************************************************
233  *           RealizePalette    (USER.283)
234  */
235 UINT RealizePalette(HDC hDC)
236 {
237     return GDIRealizePalette( hDC );
238 }
239