Merge branch 'master' into drm-gem
[mesa.git] / src / glut / fbdev / colormap.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 * Copyright (C) 1995-2006 Brian Paul
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22 * Library for glut using mesa fbdev driver
23 *
24 * Written by Sean D'Epagnier (c) 2006
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <linux/fb.h>
31
32 #include <GL/gl.h>
33 #include <GL/glut.h>
34
35 #include "internal.h"
36
37 #define TOCMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat) ((1<<16) - 1))
38 #define TORMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat)(REVERSECMAPSIZE-1))
39 #define FROMCMAP(x) (GLfloat)x / (GLfloat)((1<<16) - 1)
40
41 static struct fb_cmap ColorMap, OriginalColorMap;
42
43 unsigned short RedColorMap[256], GreenColorMap[256], BlueColorMap[256];
44
45 unsigned char ReverseColorMap[REVERSECMAPSIZE]
46 [REVERSECMAPSIZE]
47 [REVERSECMAPSIZE];
48
49 static void FindReverseMap(int r, int g, int b)
50 {
51 static int count;
52 int i, shift = 16 - REVERSECMAPSIZELOG;
53 unsigned int minv = -1, mini = 0;
54 for(i=0; i<256; i++) {
55 int val = 0;
56 val += abs(r-(RedColorMap[i]>>shift));
57 val += abs(g-(GreenColorMap[i]>>shift));
58 val += abs(b-(BlueColorMap[i]>>shift));
59 if(val < minv) {
60 minv = val;
61 mini = i;
62 }
63 }
64 ReverseColorMap[r][g][b] = mini;
65 }
66
67 static void FillItemReverseColorMap(int r, int g, int b)
68 {
69 FindReverseMap(r, g, b);
70 if(r > 0)
71 FindReverseMap(r-1, g, b);
72 if(r < REVERSECMAPSIZE - 1)
73 FindReverseMap(r+1, g, b);
74 if(g > 0)
75 FindReverseMap(r, g-1, b);
76 if(g < REVERSECMAPSIZE - 1)
77 FindReverseMap(r, g+1, b);
78 if(b > 0)
79 FindReverseMap(r, g, b-1);
80 if(b < REVERSECMAPSIZE - 1)
81 FindReverseMap(r, g, b+1);
82 }
83
84 static void FillReverseColorMap(void)
85 {
86 int r, g, b;
87 for(r = 0; r < REVERSECMAPSIZE; r++)
88 for(g = 0; g < REVERSECMAPSIZE; g++)
89 for(b = 0; b < REVERSECMAPSIZE; b++)
90 FindReverseMap(r, g, b);
91 }
92
93 void RestoreColorMap(void)
94 {
95 if(FixedInfo.visual == FB_VISUAL_TRUECOLOR)
96 return;
97
98 if (ioctl(FrameBufferFD, FBIOPUTCMAP, (void *) &ColorMap) < 0)
99 sprintf(exiterror, "ioctl(FBIOPUTCMAP) failed!\n");
100 }
101
102 void LoadColorMap(void)
103 {
104 if(FixedInfo.visual == FB_VISUAL_TRUECOLOR)
105 return;
106
107 ColorMap.start = 0;
108 ColorMap.red = RedColorMap;
109 ColorMap.green = GreenColorMap;
110 ColorMap.blue = BlueColorMap;
111 ColorMap.transp = NULL;
112
113 if(DisplayMode & GLUT_INDEX) {
114 ColorMap.len = 256;
115
116 if (ioctl(FrameBufferFD, FBIOGETCMAP, (void *) &ColorMap) < 0)
117 sprintf(exiterror, "ioctl(FBIOGETCMAP) failed!\n");
118
119 FillReverseColorMap();
120 } else {
121 int rcols = 1 << VarInfo.red.length;
122 int gcols = 1 << VarInfo.green.length;
123 int bcols = 1 << VarInfo.blue.length;
124
125 int i;
126
127 ColorMap.len = gcols;
128
129 for (i = 0; i < rcols ; i++)
130 RedColorMap[i] = (65536/(rcols-1)) * i;
131
132 for (i = 0; i < gcols ; i++)
133 GreenColorMap[i] = (65536/(gcols-1)) * i;
134
135 for (i = 0; i < bcols ; i++)
136 BlueColorMap[i] = (65536/(bcols-1)) * i;
137
138 RestoreColorMap();
139 }
140 }
141
142 void glutSetColor(int cell, GLfloat red, GLfloat green, GLfloat blue)
143 {
144 if(cell < 0 || cell >= 256)
145 return;
146
147 RedColorMap[cell] = TOCMAP(red);
148 GreenColorMap[cell] = TOCMAP(green);
149 BlueColorMap[cell] = TOCMAP(blue);
150
151 RestoreColorMap();
152
153 FillItemReverseColorMap(TORMAP(red), TORMAP(green), TORMAP(blue));
154 }
155
156 GLfloat glutGetColor(int cell, int component)
157 {
158 if(!(DisplayMode & GLUT_INDEX))
159 return -1.0;
160
161 if(cell < 0 || cell > 256)
162 return -1.0;
163
164 switch(component) {
165 case GLUT_RED:
166 return FROMCMAP(RedColorMap[cell]);
167 case GLUT_GREEN:
168 return FROMCMAP(GreenColorMap[cell]);
169 case GLUT_BLUE:
170 return FROMCMAP(BlueColorMap[cell]);
171 }
172 return -1.0;
173 }
174
175 void glutCopyColormap(int win)
176 {
177 }