xref: /reactos/dll/opengl/mesa/colortab.c (revision 19f6fc25)
1 /* $Id: colortab.c,v 1.6 1997/10/16 02:26:18 brianp Exp $ */
2 
3 /*
4  * Mesa 3-D graphics library
5  * Version:  2.5
6  * Copyright (C) 1995-1997  Brian Paul
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 
23 
24 /*
25  * $Log: colortab.c,v $
26  * Revision 1.6  1997/10/16 02:26:18  brianp
27  * call Driver.UpdateTexturePalette(ctx,NULL) when shared palette changed
28  *
29  * Revision 1.5  1997/10/16 01:59:08  brianp
30  * added GL_EXT_shared_texture_palette extension
31  *
32  * Revision 1.4  1997/10/15 23:37:05  brianp
33  * removed dirty flag stuff
34  *
35  * Revision 1.3  1997/10/13 23:50:03  brianp
36  * added call to Driver.UpdateTexturePalette
37  *
38  * Revision 1.2  1997/09/29 23:27:44  brianp
39  * updated for new device driver texture functions
40  *
41  * Revision 1.1  1997/09/27 00:12:46  brianp
42  * Initial revision
43  *
44  */
45 
46 
47 #ifdef PC_HEADER
48 #include "all.h"
49 #else
50 #include "colortab.h"
51 #include "context.h"
52 #include "macros.h"
53 #endif
54 
55 
56 
57 /*
58  * Return GL_TRUE if k is a power of two, else return GL_FALSE.
59  */
60 static GLboolean power_of_two( GLint k )
61 {
62    GLint i, m = 1;
63    for (i=0; i<32; i++) {
64       if (k == m)
65          return GL_TRUE;
66       m = m << 1;
67    }
68    return GL_FALSE;
69 }
70 
71 
72 static GLint decode_internal_format( GLint format )
73 {
74    switch (format) {
75       case GL_ALPHA:
76       case GL_ALPHA4:
77       case GL_ALPHA8:
78       case GL_ALPHA12:
79       case GL_ALPHA16:
80          return GL_ALPHA;
81       case 1:
82       case GL_LUMINANCE:
83       case GL_LUMINANCE4:
84       case GL_LUMINANCE8:
85       case GL_LUMINANCE12:
86       case GL_LUMINANCE16:
87          return GL_LUMINANCE;
88       case 2:
89       case GL_LUMINANCE_ALPHA:
90       case GL_LUMINANCE4_ALPHA4:
91       case GL_LUMINANCE6_ALPHA2:
92       case GL_LUMINANCE8_ALPHA8:
93       case GL_LUMINANCE12_ALPHA4:
94       case GL_LUMINANCE12_ALPHA12:
95       case GL_LUMINANCE16_ALPHA16:
96          return GL_LUMINANCE_ALPHA;
97       case GL_INTENSITY:
98       case GL_INTENSITY4:
99       case GL_INTENSITY8:
100       case GL_INTENSITY12:
101       case GL_INTENSITY16:
102          return GL_INTENSITY;
103       case 3:
104       case GL_RGB:
105       case GL_R3_G3_B2:
106       case GL_RGB4:
107       case GL_RGB5:
108       case GL_RGB8:
109       case GL_RGB10:
110       case GL_RGB12:
111       case GL_RGB16:
112          return GL_RGB;
113       case 4:
114       case GL_RGBA:
115       case GL_RGBA2:
116       case GL_RGBA4:
117       case GL_RGB5_A1:
118       case GL_RGBA8:
119       case GL_RGB10_A2:
120       case GL_RGBA12:
121       case GL_RGBA16:
122          return GL_RGBA;
123       default:
124          return -1;  /* error */
125    }
126 }
127 
128 
129 void gl_ColorTable( GLcontext *ctx, GLenum target,
130                     GLenum internalFormat, struct gl_image *table )
131 {
132    struct gl_texture_object *texObj;
133    GLboolean proxy = GL_FALSE;
134 
135    if (INSIDE_BEGIN_END(ctx)) {
136       gl_error( ctx, GL_INVALID_OPERATION, "glGetBooleanv" );
137       return;
138    }
139 
140    switch (target) {
141       case GL_TEXTURE_1D:
142          texObj = ctx->Texture.Current1D;
143          break;
144       case GL_TEXTURE_2D:
145          texObj = ctx->Texture.Current2D;
146          break;
147       case GL_PROXY_TEXTURE_1D:
148          texObj = ctx->Texture.Proxy1D;
149          proxy = GL_TRUE;
150          break;
151       case GL_PROXY_TEXTURE_2D:
152          texObj = ctx->Texture.Proxy2D;
153          proxy = GL_TRUE;
154          break;
155       default:
156          gl_error(ctx, GL_INVALID_ENUM, "glColorTableEXT(target)");
157          return;
158    }
159 
160    /* internalformat = just like glTexImage */
161 
162    if (table->Width < 1 || table->Width > MAX_TEXTURE_PALETTE_SIZE
163        || !power_of_two(table->Width)) {
164       gl_error(ctx, GL_INVALID_VALUE, "glColorTableEXT(width)");
165       if (proxy) {
166          texObj->PaletteSize = 0;
167          texObj->PaletteIntFormat = 0;
168          texObj->PaletteFormat = 0;
169       }
170       return;
171    }
172 
173   /* per-texture object palette */
174   texObj->PaletteSize = table->Width;
175   texObj->PaletteIntFormat = internalFormat;
176   texObj->PaletteFormat = decode_internal_format(internalFormat);
177   if (!proxy) {
178      MEMCPY(texObj->Palette, table->Data, table->Width*table->Components);
179      if (ctx->Driver.UpdateTexturePalette) {
180         (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
181      }
182    }
183 }
184 
185 
186 
187 void gl_ColorSubTable( GLcontext *ctx, GLenum target,
188                        GLsizei start, struct gl_image *data )
189 {
190    /* XXX TODO */
191    gl_problem(ctx, "glColorSubTableEXT not implemented");
192 }
193 
194 
195 
196 void gl_GetColorTable( GLcontext *ctx, GLenum target, GLenum format,
197                        GLenum type, GLvoid *table )
198 {
199    if (INSIDE_BEGIN_END(ctx)) {
200       gl_error( ctx, GL_INVALID_OPERATION, "glGetBooleanv" );
201       return;
202    }
203 
204    switch (target) {
205       case GL_TEXTURE_1D:
206          break;
207       case GL_TEXTURE_2D:
208          break;
209       case GL_TEXTURE_3D_EXT:
210          break;
211       default:
212          gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableEXT(target)");
213          return;
214    }
215 
216    gl_problem(ctx, "glGetColorTableEXT not implemented!");
217 }
218 
219 
220 
221 void gl_GetColorTableParameterfv( GLcontext *ctx, GLenum target,
222                                   GLenum pname, GLfloat *params )
223 {
224    GLint iparams[10];
225 
226    gl_GetColorTableParameteriv( ctx, target, pname, iparams );
227    *params = (GLfloat) iparams[0];
228 }
229 
230 
231 
232 void gl_GetColorTableParameteriv( GLcontext *ctx, GLenum target,
233                                   GLenum pname, GLint *params )
234 {
235    struct gl_texture_object *texObj;
236 
237    if (INSIDE_BEGIN_END(ctx)) {
238       gl_error( ctx, GL_INVALID_OPERATION, "glGetColorTableParameter" );
239       return;
240    }
241 
242    switch (target) {
243       case GL_TEXTURE_1D:
244          texObj = ctx->Texture.Current1D;
245          break;
246       case GL_TEXTURE_2D:
247          texObj = ctx->Texture.Current2D;
248          break;
249       default:
250          gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
251          return;
252    }
253 
254    switch (pname) {
255       case GL_COLOR_TABLE_FORMAT_EXT:
256          if (texObj)
257             *params = texObj->PaletteIntFormat;
258          break;
259       case GL_COLOR_TABLE_WIDTH_EXT:
260          if (texObj)
261             *params = texObj->PaletteSize;
262          break;
263       case GL_COLOR_TABLE_RED_SIZE_EXT:
264          *params = 8;
265          break;
266       case GL_COLOR_TABLE_GREEN_SIZE_EXT:
267          *params = 8;
268          break;
269       case GL_COLOR_TABLE_BLUE_SIZE_EXT:
270          *params = 8;
271          break;
272       case GL_COLOR_TABLE_ALPHA_SIZE_EXT:
273          *params = 8;
274          break;
275       case GL_COLOR_TABLE_LUMINANCE_SIZE_EXT:
276          *params = 8;
277          break;
278       case GL_COLOR_TABLE_INTENSITY_SIZE_EXT:
279          *params = 8;
280          break;
281       default:
282          gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter" );
283          return;
284    }
285 }
286 
287 
288