summaryrefslogtreecommitdiff
path: root/src/glut/fbdev/colormap.c
blob: 89556ef4a5c75ba73ac009f73d7982d595e1880e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Mesa 3-D graphics library
 * Version:  6.5
 * Copyright (C) 1995-2006  Brian Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Library for glut using mesa fbdev driver
 *
 * Written by Sean D'Epagnier (c) 2006
 */

#include <stdlib.h>

#include <linux/fb.h>

#include <GL/gl.h>
#include <GL/glut.h>

#include "internal.h"

#define TOCMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat) ((1<<16) - 1))
#define TORMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat)(REVERSECMAPSIZE-1))
#define FROMCMAP(x) (GLfloat)x / (GLfloat)((1<<16) - 1)

static struct fb_cmap ColorMap;

unsigned short RedColorMap[256], GreenColorMap[256], BlueColorMap[256];

unsigned char ReverseColorMap[REVERSECMAPSIZE]
                             [REVERSECMAPSIZE]
                             [REVERSECMAPSIZE];

static void FindReverseMap(int r, int g, int b)
{
   static int count;
   int i, shift = 16 - REVERSECMAPSIZELOG;
   unsigned int minv = -1, mini = 0;
   for(i=0; i<256; i++) {
      int val = 0;
      val += abs(r-(RedColorMap[i]>>shift));
      val += abs(g-(GreenColorMap[i]>>shift));
      val += abs(b-(BlueColorMap[i]>>shift));
      if(val < minv) {
	 minv = val;
	 mini = i;
      }
   }
   ReverseColorMap[r][g][b] = mini;
}

static void FillItemReverseColorMap(int r, int g, int b)
{
   FindReverseMap(r, g, b);
   if(r > 0)
      FindReverseMap(r-1, g, b);
   if(r < REVERSECMAPSIZE - 1)
      FindReverseMap(r+1, g, b);
   if(g > 0)
      FindReverseMap(r, g-1, b);
   if(g < REVERSECMAPSIZE - 1)
      FindReverseMap(r, g+1, b);
   if(b > 0)
      FindReverseMap(r, g, b-1);
   if(b < REVERSECMAPSIZE - 1)
      FindReverseMap(r, g, b+1);
}

static void FillReverseColorMap(void)
{
   int r, g, b;
   for(r = 0; r < REVERSECMAPSIZE; r++)
      for(g = 0; g < REVERSECMAPSIZE; g++)
	 for(b = 0; b < REVERSECMAPSIZE; b++)
	    FindReverseMap(r, g, b);
}

void RestoreColorMap(void)
{
   if (ioctl(FrameBufferFD, FBIOPUTCMAP, (void *) &ColorMap) < 0)
      sprintf(exiterror, "ioctl(FBIOPUTCMAP) failed!\n");
}

void LoadColorMap(void)
{
   ColorMap.start = 0;
   ColorMap.red   = RedColorMap;
   ColorMap.green = GreenColorMap;
   ColorMap.blue  = BlueColorMap;
   ColorMap.transp = NULL;

   if(DisplayMode & GLUT_INDEX) {
      ColorMap.len = 256;

      if (ioctl(FrameBufferFD, FBIOGETCMAP, (void *) &ColorMap) < 0)
	 sprintf(exiterror, "ioctl(FBIOGETCMAP) failed!\n");

      FillReverseColorMap();
   } else {
      int rcols = 1 << VarInfo.red.length;
      int gcols = 1 << VarInfo.green.length;
      int bcols = 1 << VarInfo.blue.length;

      int i;

      ColorMap.len = gcols;

      for (i = 0; i < rcols ; i++) 
	 RedColorMap[i] = (65536/(rcols-1)) * i;
      
      for (i = 0; i < gcols ; i++) 
	 GreenColorMap[i] = (65536/(gcols-1)) * i;
      
      for (i = 0; i < bcols ; i++) 
	 BlueColorMap[i] = (65536/(bcols-1)) * i;  

      RestoreColorMap();
   }
}

void glutSetColor(int cell, GLfloat red, GLfloat green, GLfloat blue)
{
   if(cell < 0 || cell >= 256)
      return;

   RedColorMap[cell] = TOCMAP(red);
   GreenColorMap[cell] = TOCMAP(green); 
   BlueColorMap[cell] = TOCMAP(blue);
    
   RestoreColorMap();

   FillItemReverseColorMap(TORMAP(red), TORMAP(green), TORMAP(blue));
}

GLfloat glutGetColor(int cell, int component)
{
   if(!(DisplayMode & GLUT_INDEX))
      return -1.0;

   if(cell < 0 || cell > 256)
      return -1.0;

   switch(component) {
   case GLUT_RED:
      return FROMCMAP(RedColorMap[cell]);
   case GLUT_GREEN:
      return FROMCMAP(GreenColorMap[cell]);
   case GLUT_BLUE:
      return FROMCMAP(BlueColorMap[cell]);
   }
   return -1.0;
}

void glutCopyColormap(int win)
{
}