Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
232 lines (190 loc) · 6.89 KB

SDL_x11gamma.c

File metadata and controls

232 lines (190 loc) · 6.89 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Jan 24, 2010
Jan 24, 2010
3
Copyright (C) 1997-2010 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#include "../SDL_sysvideo.h"
#include "SDL_x11video.h"
Jan 15, 2009
Jan 15, 2009
26
27
28
/* The size of *all* SDL gamma ramps */
#define SDL_GammaRampSize (3 * 256 * sizeof(Uint16))
Jul 24, 2007
Jul 24, 2007
29
30
31
32
33
34
static int numCmaps = 0;
typedef struct
{
Display *display;
int scrNum;
Jul 25, 2007
Jul 25, 2007
35
Colormap colormap;
Jul 24, 2007
Jul 24, 2007
36
Visual visual;
Jan 9, 2009
Jan 9, 2009
37
Uint16 *ramp;
Jul 24, 2007
Jul 24, 2007
38
39
40
41
} cmapTableEntry;
cmapTableEntry *cmapTable = NULL;
Jul 25, 2007
Jul 25, 2007
42
43
44
45
46
47
48
49
50
51
52
53
54
/* To reduce the overhead as much as possible lets do as little as
possible. When we do have to create a colormap keep track of it and
reuse it. We're going to do this for both DirectColor and
PseudoColor colormaps. */
Colormap
X11_LookupColormap(Display * display, int scrNum, VisualID vid)
{
int i;
for (i = 0; i < numCmaps; i++) {
if (cmapTable[i].display == display &&
cmapTable[i].scrNum == scrNum &&
Jan 9, 2009
Jan 9, 2009
55
56
cmapTable[i].visual.visualid == vid) {
return cmapTable[i].colormap;
Jul 25, 2007
Jul 25, 2007
57
58
59
60
61
62
63
}
}
return 0;
}
Jul 24, 2007
Jul 24, 2007
64
void
Jul 25, 2007
Jul 25, 2007
65
X11_TrackColormap(Display * display, int scrNum, Colormap colormap,
Jan 9, 2009
Jan 9, 2009
66
Visual * visual, XColor * ramp)
Jul 24, 2007
Jul 24, 2007
67
68
{
int i;
Jan 9, 2009
Jan 9, 2009
69
70
Uint16 *newramp;
int ncolors;
Jul 24, 2007
Jul 24, 2007
71
Jul 25, 2007
Jul 25, 2007
72
73
74
/* search the table to find out if we already have this one. We
only want one entry for each display, screen number, visualid,
and colormap combination */
Jul 24, 2007
Jul 24, 2007
75
76
77
for (i = 0; i < numCmaps; i++) {
if (cmapTable[i].display == display &&
cmapTable[i].scrNum == scrNum &&
Jan 9, 2009
Jan 9, 2009
78
79
cmapTable[i].visual.visualid == visual->visualid &&
cmapTable[i].colormap == colormap) {
Jul 24, 2007
Jul 24, 2007
80
81
82
83
84
85
return;
}
}
/* increase the table by one entry. If the table is NULL create the
first entrty */
Aug 11, 2007
Aug 11, 2007
86
87
cmapTable =
SDL_realloc(cmapTable, (numCmaps + 1) * sizeof(cmapTableEntry));
Jul 26, 2007
Jul 26, 2007
88
if (NULL == cmapTable) {
Jul 24, 2007
Jul 24, 2007
89
90
91
92
93
94
SDL_SetError("Out of memory in X11_TrackColormap()");
return;
}
cmapTable[numCmaps].display = display;
cmapTable[numCmaps].scrNum = scrNum;
Jul 25, 2007
Jul 25, 2007
95
cmapTable[numCmaps].colormap = colormap;
Jul 24, 2007
Jul 24, 2007
96
SDL_memcpy(&cmapTable[numCmaps].visual, visual, sizeof(Visual));
Jan 9, 2009
Jan 9, 2009
97
98
cmapTable[numCmaps].ramp = NULL;
Jan 15, 2009
Jan 15, 2009
99
if (ramp != NULL) {
Jan 30, 2009
Jan 30, 2009
100
101
102
103
104
105
106
newramp = SDL_malloc(SDL_GammaRampSize);
if (NULL == newramp) {
SDL_SetError("Out of memory in X11_TrackColormap()");
return;
}
SDL_memset(newramp, 0, SDL_GammaRampSize);
cmapTable[numCmaps].ramp = newramp;
Jan 9, 2009
Jan 9, 2009
107
Jan 30, 2009
Jan 30, 2009
108
ncolors = cmapTable[numCmaps].visual.map_entries;
Jan 9, 2009
Jan 9, 2009
109
Jan 30, 2009
Jan 30, 2009
110
111
112
113
114
for (i = 0; i < ncolors; i++) {
newramp[(0 * 256) + i] = ramp[i].red;
newramp[(1 * 256) + i] = ramp[i].green;
newramp[(2 * 256) + i] = ramp[i].blue;
}
Jan 9, 2009
Jan 9, 2009
115
}
Jul 24, 2007
Jul 24, 2007
116
117
118
119
numCmaps++;
}
Jul 25, 2007
Jul 25, 2007
120
121
122
123
124
125
/* The problem is that you have to have at least one DirectColor
colormap before you can set the gamma ramps or read the gamma
ramps. If the application has created a DirectColor window then the
cmapTable will have at least one colormap in it and everything is
cool. If not, then we just fail */
Dec 1, 2009
Dec 1, 2009
127
X11_SetDisplayGammaRamp(_THIS, SDL_VideoDisplay * sdl_display, Uint16 * ramp)
Jan 9, 2009
Jan 9, 2009
129
Visual *visual;
Jul 25, 2007
Jul 25, 2007
130
131
132
133
Display *display;
Colormap colormap;
XColor *colorcells;
int ncolors;
Jan 9, 2009
Jan 9, 2009
134
135
int rmask, gmask, bmask;
int rshift, gshift, bshift;
Jul 25, 2007
Jul 25, 2007
136
137
138
139
140
141
142
143
int i;
int j;
for (j = 0; j < numCmaps; j++) {
if (cmapTable[j].visual.class == DirectColor) {
display = cmapTable[j].display;
colormap = cmapTable[j].colormap;
ncolors = cmapTable[j].visual.map_entries;
Jan 9, 2009
Jan 9, 2009
144
visual = &cmapTable[j].visual;
Jul 25, 2007
Jul 25, 2007
145
146
147
148
149
150
colorcells = SDL_malloc(ncolors * sizeof(XColor));
if (NULL == colorcells) {
SDL_SetError("out of memory in X11_SetDisplayGammaRamp");
return -1;
}
Jan 9, 2009
Jan 9, 2009
151
/* remember the new ramp */
Jan 30, 2009
Jan 30, 2009
152
153
154
155
156
157
158
159
if (cmapTable[j].ramp == NULL) {
Uint16 *newramp = SDL_malloc(SDL_GammaRampSize);
if (NULL == newramp) {
SDL_SetError("Out of memory in X11_TrackColormap()");
return -1;
}
cmapTable[j].ramp = newramp;
}
Jan 15, 2009
Jan 15, 2009
160
SDL_memcpy(cmapTable[j].ramp, ramp, SDL_GammaRampSize);
Jan 9, 2009
Jan 9, 2009
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
rshift = 0;
rmask = visual->red_mask;
while (0 == (rmask & 1)) {
rshift++;
rmask >>= 1;
}
/* printf("rmask = %4x rshift = %4d\n", rmask, rshift); */
gshift = 0;
gmask = visual->green_mask;
while (0 == (gmask & 1)) {
gshift++;
gmask >>= 1;
}
/* printf("gmask = %4x gshift = %4d\n", gmask, gshift); */
Jul 25, 2007
Jul 25, 2007
179
Jan 9, 2009
Jan 9, 2009
180
181
182
183
184
185
bshift = 0;
bmask = visual->blue_mask;
while (0 == (bmask & 1)) {
bshift++;
bmask >>= 1;
}
Jul 25, 2007
Jul 25, 2007
186
Jan 9, 2009
Jan 9, 2009
187
/* printf("bmask = %4x bshift = %4d\n", bmask, bshift); */
Jul 25, 2007
Jul 25, 2007
188
189
190
/* build the color table pixel values */
for (i = 0; i < ncolors; i++) {
Jan 9, 2009
Jan 9, 2009
191
192
193
194
195
196
197
198
Uint32 rbits = (rmask * i) / (ncolors - 1);
Uint32 gbits = (gmask * i) / (ncolors - 1);
Uint32 bbits = (bmask * i) / (ncolors - 1);
Uint32 pix =
(rbits << rshift) | (gbits << gshift) | (bbits << bshift);
colorcells[i].pixel = pix;
Jul 25, 2007
Jul 25, 2007
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
colorcells[i].flags = DoRed | DoGreen | DoBlue;
colorcells[i].red = ramp[(0 * 256) + i];
colorcells[i].green = ramp[(1 * 256) + i];
colorcells[i].blue = ramp[(2 * 256) + i];
}
XStoreColors(display, colormap, colorcells, ncolors);
XFlush(display);
SDL_free(colorcells);
}
}
return 0;
Dec 1, 2009
Dec 1, 2009
217
X11_GetDisplayGammaRamp(_THIS, SDL_VideoDisplay * display, Uint16 * ramp)
Jul 25, 2007
Jul 25, 2007
219
220
221
222
223
224
225
int i;
/* find the first DirectColor colormap and use it to get the gamma
ramp */
for (i = 0; i < numCmaps; i++) {
if (cmapTable[i].visual.class == DirectColor) {
Jan 15, 2009
Jan 15, 2009
226
SDL_memcpy(ramp, cmapTable[i].ramp, SDL_GammaRampSize);
Jan 9, 2009
Jan 9, 2009
227
return 0;
Jul 25, 2007
Jul 25, 2007
228
229
230
}
}
Jan 9, 2009
Jan 9, 2009
231
return -1;