The colormap code is now simpler, 15bpp works where it did not before.
[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 <stdlib.h>
28
29 #include <linux/fb.h>
30
31 #include <GL/gl.h>
32 #include <GL/glut.h>
33
34 #include "internal.h"
35
36 #define TOCMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat) ((1<<16) - 1))
37 #define TORMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat)(REVERSECMAPSIZE-1))
38 #define FROMCMAP(x) (GLfloat)x / (GLfloat)((1<<16) - 1)
39
40 static struct fb_cmap ColorMap;
41
42 unsigned short RedColorMap[256], GreenColorMap[256], BlueColorMap[256];
43
44 unsigned char ReverseColorMap[REVERSECMAPSIZE]
45 [REVERSECMAPSIZE]
46 [REVERSECMAPSIZE];
47
48 static void FindReverseMap(int r, int g, int b)
49 {
50 static int count;
51 int i, shift = 16 - REVERSECMAPSIZELOG;
52 unsigned int minv = -1, mini = 0;
53 for(i=0; i<256; i++) {
54 int val = 0;
55 val += abs(r-(RedColorMap[i]>>shift));
56 val += abs(g-(GreenColorMap[i]>>shift));
57 val += abs(b-(BlueColorMap[i]>>shift));
58 if(val < minv) {
59 minv = val;
60 mini = i;
61 }
62 }
63 ReverseColorMap[r][g][b] = mini;
64 }
65
66 static void FillItemReverseColorMap(int r, int g, int b)
67 {
68 FindReverseMap(r, g, b);
69 if(r > 0)
70 FindReverseMap(r-1, g, b);
71 if(r < REVERSECMAPSIZE - 1)
72 FindReverseMap(r+1, g, b);
73 if(g > 0)
74 FindReverseMap(r, g-1, b);
75 if(g < REVERSECMAPSIZE - 1)
76 FindReverseMap(r, g+1, b);
77 if(b > 0)
78 FindReverseMap(r, g, b-1);
79 if(b < REVERSECMAPSIZE - 1)
80 FindReverseMap(r, g, b+1);
81 }
82
83 static void FillReverseColorMap(void)
84 {
85 int r, g, b;
86 for(r = 0; r < REVERSECMAPSIZE; r++)
87 for(g = 0; g < REVERSECMAPSIZE; g++)
88 for(b = 0; b < REVERSECMAPSIZE; b++)
89 FindReverseMap(r, g, b);
90 }
91
92 void RestoreColorMap(void)
93 {
94 if (ioctl(FrameBufferFD, FBIOPUTCMAP, (void *) &ColorMap) < 0)
95 sprintf(exiterror, "ioctl(FBIOPUTCMAP) failed!\n");
96 }
97
98 void LoadColorMap(void)
99 {
100 ColorMap.start = 0;
101 ColorMap.red = RedColorMap;
102 ColorMap.green = GreenColorMap;
103 ColorMap.blue = BlueColorMap;
104 ColorMap.transp = NULL;
105
106 if(DisplayMode & GLUT_INDEX) {
107 ColorMap.len = 256;
108
109 if (ioctl(FrameBufferFD, FBIOGETCMAP, (void *) &ColorMap) < 0)
110 sprintf(exiterror, "ioctl(FBIOGETCMAP) failed!\n");
111
112 FillReverseColorMap();
113 } else {
114 int rcols = 1 << VarInfo.red.length;
115 int gcols = 1 << VarInfo.green.length;
116 int bcols = 1 << VarInfo.blue.length;
117
118 int i;
119
120 ColorMap.len = gcols;
121
122 for (i = 0; i < rcols ; i++)
123 RedColorMap[i] = (65536/(rcols-1)) * i;
124
125 for (i = 0; i < gcols ; i++)
126 GreenColorMap[i] = (65536/(gcols-1)) * i;
127
128 for (i = 0; i < bcols ; i++)
129 BlueColorMap[i] = (65536/(bcols-1)) * i;
130
131 RestoreColorMap();
132 }
133 }
134
135 void glutSetColor(int cell, GLfloat red, GLfloat green, GLfloat blue)
136 {
137 if(cell < 0 || cell >= 256)
138 return;
139
140 RedColorMap[cell] = TOCMAP(red);
141 GreenColorMap[cell] = TOCMAP(green);
142 BlueColorMap[cell] = TOCMAP(blue);
143
144 RestoreColorMap();
145
146 FillItemReverseColorMap(TORMAP(red), TORMAP(green), TORMAP(blue));
147 }
148
149 GLfloat glutGetColor(int cell, int component)
150 {
151 if(!(DisplayMode & GLUT_INDEX))
152 return -1.0;
153
154 if(cell < 0 || cell > 256)
155 return -1.0;
156
157 switch(component) {
158 case GLUT_RED:
159 return FROMCMAP(RedColorMap[cell]);
160 case GLUT_GREEN:
161 return FROMCMAP(GreenColorMap[cell]);
162 case GLUT_BLUE:
163 return FROMCMAP(BlueColorMap[cell]);
164 }
165 return -1.0;
166 }
167
168 void glutCopyColormap(int win)
169 {
170 }