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