colortab.c revision 485f04074151686fa24d40e3eeb83029d3d8c425
1/* $Id: colortab.c,v 1.2 1999/10/08 09:27:10 keithw Exp $ */ 2 3/* 4 * Mesa 3-D graphics library 5 * Version: 3.1 6 * 7 * Copyright (C) 1999 Brian Paul All Rights Reserved. 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a 10 * copy of this software and associated documentation files (the "Software"), 11 * to deal in the Software without restriction, including without limitation 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 * and/or sell copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included 17 * in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26/* $XFree86: xc/lib/GL/mesa/src/colortab.c,v 1.2 1999/04/04 00:20:21 dawes Exp $ */ 27 28 29 30 31 32#ifdef PC_HEADER 33#include "all.h" 34#else 35#ifdef XFree86Server 36#include "GL/xf86glx.h" 37#endif 38#include "colortab.h" 39#include "context.h" 40#include "macros.h" 41#ifdef XFree86Server 42#include "GL/xf86glx.h" 43#endif 44#endif 45 46 47 48/* 49 * Return GL_TRUE if k is a power of two, else return GL_FALSE. 50 */ 51static GLboolean power_of_two( GLint k ) 52{ 53 GLint i, m = 1; 54 for (i=0; i<32; i++) { 55 if (k == m) 56 return GL_TRUE; 57 m = m << 1; 58 } 59 return GL_FALSE; 60} 61 62 63static GLint decode_internal_format( GLint format ) 64{ 65 switch (format) { 66 case GL_ALPHA: 67 case GL_ALPHA4: 68 case GL_ALPHA8: 69 case GL_ALPHA12: 70 case GL_ALPHA16: 71 return GL_ALPHA; 72 case 1: 73 case GL_LUMINANCE: 74 case GL_LUMINANCE4: 75 case GL_LUMINANCE8: 76 case GL_LUMINANCE12: 77 case GL_LUMINANCE16: 78 return GL_LUMINANCE; 79 case 2: 80 case GL_LUMINANCE_ALPHA: 81 case GL_LUMINANCE4_ALPHA4: 82 case GL_LUMINANCE6_ALPHA2: 83 case GL_LUMINANCE8_ALPHA8: 84 case GL_LUMINANCE12_ALPHA4: 85 case GL_LUMINANCE12_ALPHA12: 86 case GL_LUMINANCE16_ALPHA16: 87 return GL_LUMINANCE_ALPHA; 88 case GL_INTENSITY: 89 case GL_INTENSITY4: 90 case GL_INTENSITY8: 91 case GL_INTENSITY12: 92 case GL_INTENSITY16: 93 return GL_INTENSITY; 94 case 3: 95 case GL_RGB: 96 case GL_R3_G3_B2: 97 case GL_RGB4: 98 case GL_RGB5: 99 case GL_RGB8: 100 case GL_RGB10: 101 case GL_RGB12: 102 case GL_RGB16: 103 return GL_RGB; 104 case 4: 105 case GL_RGBA: 106 case GL_RGBA2: 107 case GL_RGBA4: 108 case GL_RGB5_A1: 109 case GL_RGBA8: 110 case GL_RGB10_A2: 111 case GL_RGBA12: 112 case GL_RGBA16: 113 return GL_RGBA; 114 default: 115 return -1; /* error */ 116 } 117} 118 119 120void gl_ColorTable( GLcontext *ctx, GLenum target, 121 GLenum internalFormat, struct gl_image *table ) 122{ 123 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 124 struct gl_texture_object *texObj; 125 GLboolean proxy = GL_FALSE; 126 127 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glColorTable"); 128 129 if (decode_internal_format(internalFormat) < 0) { 130 gl_error( ctx, GL_INVALID_ENUM, "glColorTable(internalFormat)" ); 131 return; 132 } 133 134 switch (target) { 135 case GL_TEXTURE_1D: 136 texObj = texUnit->CurrentD[1]; 137 break; 138 case GL_TEXTURE_2D: 139 texObj = texUnit->CurrentD[2]; 140 break; 141 case GL_TEXTURE_3D_EXT: 142 texObj = texUnit->CurrentD[3]; 143 break; 144 case GL_PROXY_TEXTURE_1D: 145 texObj = ctx->Texture.Proxy1D; 146 proxy = GL_TRUE; 147 break; 148 case GL_PROXY_TEXTURE_2D: 149 texObj = ctx->Texture.Proxy2D; 150 proxy = GL_TRUE; 151 break; 152 case GL_PROXY_TEXTURE_3D_EXT: 153 texObj = ctx->Texture.Proxy3D; 154 proxy = GL_TRUE; 155 break; 156 case GL_SHARED_TEXTURE_PALETTE_EXT: 157 texObj = NULL; 158 break; 159 default: 160 gl_error(ctx, GL_INVALID_ENUM, "glColorTableEXT(target)"); 161 return; 162 } 163 164 /* internalformat = just like glTexImage */ 165 166 if (table->Width < 1 || table->Width > MAX_TEXTURE_PALETTE_SIZE 167 || !power_of_two(table->Width)) { 168 gl_error(ctx, GL_INVALID_VALUE, "glColorTableEXT(width)"); 169 if (proxy) { 170 texObj->PaletteSize = 0; 171 texObj->PaletteIntFormat = (GLenum) 0; 172 texObj->PaletteFormat = (GLenum) 0; 173 } 174 return; 175 } 176 177 if (texObj) { 178 /* per-texture object palette */ 179 texObj->PaletteSize = table->Width; 180 texObj->PaletteIntFormat = internalFormat; 181 texObj->PaletteFormat = (GLenum) decode_internal_format(internalFormat); 182 if (!proxy) { 183 MEMCPY(texObj->Palette, table->Data, table->Width*table->Components); 184 if (ctx->Driver.UpdateTexturePalette) { 185 (*ctx->Driver.UpdateTexturePalette)( ctx, texObj ); 186 } 187 } 188 } 189 else { 190 /* shared texture palette */ 191 ctx->Texture.PaletteSize = table->Width; 192 ctx->Texture.PaletteIntFormat = internalFormat; 193 ctx->Texture.PaletteFormat = (GLenum) decode_internal_format(internalFormat); 194 MEMCPY(ctx->Texture.Palette, table->Data, table->Width*table->Components); 195 if (ctx->Driver.UpdateTexturePalette) { 196 (*ctx->Driver.UpdateTexturePalette)( ctx, NULL ); 197 } 198 } 199} 200 201 202 203void gl_ColorSubTable( GLcontext *ctx, GLenum target, 204 GLsizei start, struct gl_image *data ) 205{ 206 /* XXX TODO */ 207 gl_problem(ctx, "glColorSubTableEXT not implemented"); 208 (void) target; 209 (void) start; 210 (void) data; 211} 212 213 214 215void gl_GetColorTable( GLcontext *ctx, GLenum target, GLenum format, 216 GLenum type, GLvoid *table ) 217{ 218 ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetBooleanv"); 219 220 switch (target) { 221 case GL_TEXTURE_1D: 222 break; 223 case GL_TEXTURE_2D: 224 break; 225 case GL_TEXTURE_3D_EXT: 226 break; 227 case GL_SHARED_TEXTURE_PALETTE_EXT: 228 break; 229 default: 230 gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableEXT(target)"); 231 return; 232 } 233 234 gl_problem(ctx, "glGetColorTableEXT not implemented!"); 235 (void) format; 236 (void) type; 237 (void) table; 238} 239 240 241 242void gl_GetColorTableParameterfv( GLcontext *ctx, GLenum target, 243 GLenum pname, GLfloat *params ) 244{ 245 GLint iparams[10]; 246 247 gl_GetColorTableParameteriv( ctx, target, pname, iparams ); 248 *params = (GLfloat) iparams[0]; 249} 250 251 252 253void gl_GetColorTableParameteriv( GLcontext *ctx, GLenum target, 254 GLenum pname, GLint *params ) 255{ 256 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 257 struct gl_texture_object *texObj; 258 259 ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetColorTableParameter"); 260 261 switch (target) { 262 case GL_TEXTURE_1D: 263 texObj = texUnit->CurrentD[1]; 264 break; 265 case GL_TEXTURE_2D: 266 texObj = texUnit->CurrentD[2]; 267 break; 268 case GL_TEXTURE_3D_EXT: 269 texObj = texUnit->CurrentD[3]; 270 break; 271 case GL_SHARED_TEXTURE_PALETTE_EXT: 272 texObj = NULL; 273 break; 274 default: 275 gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)"); 276 return; 277 } 278 279 switch (pname) { 280 case GL_COLOR_TABLE_FORMAT_EXT: 281 if (texObj) 282 *params = texObj->PaletteIntFormat; 283 else 284 *params = ctx->Texture.PaletteIntFormat; 285 break; 286 case GL_COLOR_TABLE_WIDTH_EXT: 287 if (texObj) 288 *params = texObj->PaletteSize; 289 else 290 *params = ctx->Texture.PaletteSize; 291 break; 292 case GL_COLOR_TABLE_RED_SIZE_EXT: 293 *params = 8; 294 break; 295 case GL_COLOR_TABLE_GREEN_SIZE_EXT: 296 *params = 8; 297 break; 298 case GL_COLOR_TABLE_BLUE_SIZE_EXT: 299 *params = 8; 300 break; 301 case GL_COLOR_TABLE_ALPHA_SIZE_EXT: 302 *params = 8; 303 break; 304 case GL_COLOR_TABLE_LUMINANCE_SIZE_EXT: 305 *params = 8; 306 break; 307 case GL_COLOR_TABLE_INTENSITY_SIZE_EXT: 308 *params = 8; 309 break; 310 default: 311 gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter" ); 312 return; 313 } 314} 315 316 317