Added initial multisampling support to glfbdev driver.
[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 /* we're assuming 256 entries here */
101 int i;
102
103 ColorMap.start = 0;
104 ColorMap.len = 256;
105 ColorMap.red = RedColorMap;
106 ColorMap.green = GreenColorMap;
107 ColorMap.blue = BlueColorMap;
108 ColorMap.transp = NULL;
109
110 if (ioctl(FrameBufferFD, FBIOGETCMAP, (void *) &ColorMap) < 0)
111 sprintf(exiterror, "ioctl(FBIOGETCMAP) failed!\n");
112
113 switch(VarInfo.bits_per_pixel) {
114 case 15:
115 for(i=0; i<32; i++)
116 RedColorMap[i] = GreenColorMap[i] = BlueColorMap[i] = i*65535/31;
117 break;
118 case 16:
119 for(i=0; i<32; i++)
120 RedColorMap[i] = BlueColorMap[i] = i*65535/31;
121 for(i=0; i<64; i++)
122 GreenColorMap[i] = i*65535/63;
123 break;
124 case 24:
125 case 32:
126 for(i=0; i<256; i++)
127 RedColorMap[i] = GreenColorMap[i] = BlueColorMap[i] = i*257;
128 break;
129 }
130
131 RestoreColorMap();
132
133 if(DisplayMode & GLUT_INDEX)
134 FillReverseColorMap();
135 }
136
137 void glutSetColor(int cell, GLfloat red, GLfloat green, GLfloat blue)
138 {
139 if(cell < 0 || cell >= 256)
140 return;
141
142 RedColorMap[cell] = TOCMAP(red);
143 GreenColorMap[cell] = TOCMAP(green);
144 BlueColorMap[cell] = TOCMAP(blue);
145
146 RestoreColorMap();
147
148 FillItemReverseColorMap(TORMAP(red), TORMAP(green), TORMAP(blue));
149 }
150
151 GLfloat glutGetColor(int cell, int component)
152 {
153 if(!(DisplayMode & GLUT_INDEX))
154 return -1.0;
155
156 if(cell < 0 || cell > 256)
157 return -1.0;
158
159 switch(component) {
160 case GLUT_RED:
161 return FROMCMAP(RedColorMap[cell]);
162 case GLUT_GREEN:
163 return FROMCMAP(GreenColorMap[cell]);
164 case GLUT_BLUE:
165 return FROMCMAP(BlueColorMap[cell]);
166 }
167 return -1.0;
168 }
169
170 void glutCopyColormap(int win)
171 {
172 }