home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / src / colortab.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  8KB  |  314 lines

  1. /* $Id: colortab.c,v 1.3 1999/11/08 07:36:43 brianp 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. #endif
  42.  
  43.  
  44.  
  45. /*
  46.  * Return GL_TRUE if k is a power of two, else return GL_FALSE.
  47.  */
  48. static GLboolean power_of_two( GLint k )
  49. {
  50.    GLint i, m = 1;
  51.    for (i=0; i<32; i++) {
  52.       if (k == m)
  53.          return GL_TRUE;
  54.       m = m << 1;
  55.    }
  56.    return GL_FALSE;
  57. }
  58.  
  59.  
  60. static GLint decode_internal_format( GLint format )
  61. {
  62.    switch (format) {
  63.       case GL_ALPHA:
  64.       case GL_ALPHA4:
  65.       case GL_ALPHA8:
  66.       case GL_ALPHA12:
  67.       case GL_ALPHA16:
  68.          return GL_ALPHA;
  69.       case 1:
  70.       case GL_LUMINANCE:
  71.       case GL_LUMINANCE4:
  72.       case GL_LUMINANCE8:
  73.       case GL_LUMINANCE12:
  74.       case GL_LUMINANCE16:
  75.          return GL_LUMINANCE;
  76.       case 2:
  77.       case GL_LUMINANCE_ALPHA:
  78.       case GL_LUMINANCE4_ALPHA4:
  79.       case GL_LUMINANCE6_ALPHA2:
  80.       case GL_LUMINANCE8_ALPHA8:
  81.       case GL_LUMINANCE12_ALPHA4:
  82.       case GL_LUMINANCE12_ALPHA12:
  83.       case GL_LUMINANCE16_ALPHA16:
  84.          return GL_LUMINANCE_ALPHA;
  85.       case GL_INTENSITY:
  86.       case GL_INTENSITY4:
  87.       case GL_INTENSITY8:
  88.       case GL_INTENSITY12:
  89.       case GL_INTENSITY16:
  90.          return GL_INTENSITY;
  91.       case 3:
  92.       case GL_RGB:
  93.       case GL_R3_G3_B2:
  94.       case GL_RGB4:
  95.       case GL_RGB5:
  96.       case GL_RGB8:
  97.       case GL_RGB10:
  98.       case GL_RGB12:
  99.       case GL_RGB16:
  100.          return GL_RGB;
  101.       case 4:
  102.       case GL_RGBA:
  103.       case GL_RGBA2:
  104.       case GL_RGBA4:
  105.       case GL_RGB5_A1:
  106.       case GL_RGBA8:
  107.       case GL_RGB10_A2:
  108.       case GL_RGBA12:
  109.       case GL_RGBA16:
  110.          return GL_RGBA;
  111.       default:
  112.          return -1;  /* error */
  113.    }
  114. }
  115.  
  116.  
  117. void gl_ColorTable( GLcontext *ctx, GLenum target,
  118.                     GLenum internalFormat, struct gl_image *table )
  119. {
  120.    struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
  121.    struct gl_texture_object *texObj;
  122.    GLboolean proxy = GL_FALSE;
  123.  
  124.    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glColorTable");
  125.  
  126.    if (decode_internal_format(internalFormat) < 0) {
  127.       gl_error( ctx, GL_INVALID_ENUM, "glColorTable(internalFormat)" );
  128.       return;
  129.    }
  130.  
  131.    switch (target) {
  132.       case GL_TEXTURE_1D:
  133.          texObj = texUnit->CurrentD[1];
  134.          break;
  135.       case GL_TEXTURE_2D:
  136.          texObj = texUnit->CurrentD[2];
  137.          break;
  138.       case GL_TEXTURE_3D_EXT:
  139.          texObj = texUnit->CurrentD[3];
  140.          break;
  141.       case GL_PROXY_TEXTURE_1D:
  142.          texObj = ctx->Texture.Proxy1D;
  143.          proxy = GL_TRUE;
  144.          break;
  145.       case GL_PROXY_TEXTURE_2D:
  146.          texObj = ctx->Texture.Proxy2D;
  147.          proxy = GL_TRUE;
  148.          break;
  149.       case GL_PROXY_TEXTURE_3D_EXT:
  150.          texObj = ctx->Texture.Proxy3D;
  151.          proxy = GL_TRUE;
  152.          break;
  153.       case GL_SHARED_TEXTURE_PALETTE_EXT:
  154.          texObj = NULL;
  155.          break;
  156.       default:
  157.          gl_error(ctx, GL_INVALID_ENUM, "glColorTableEXT(target)");
  158.          return;
  159.    }
  160.  
  161.    /* internalformat = just like glTexImage */
  162.  
  163.    if (table->Width < 1 || table->Width > MAX_TEXTURE_PALETTE_SIZE
  164.        || !power_of_two(table->Width)) {
  165.       gl_error(ctx, GL_INVALID_VALUE, "glColorTableEXT(width)");
  166.       if (proxy) {
  167.          texObj->PaletteSize = 0;
  168.          texObj->PaletteIntFormat = (GLenum) 0;
  169.          texObj->PaletteFormat = (GLenum) 0;
  170.       }
  171.       return;
  172.    }
  173.  
  174.    if (texObj) {
  175.       /* per-texture object palette */
  176.       texObj->PaletteSize = table->Width;
  177.       texObj->PaletteIntFormat = internalFormat;
  178.       texObj->PaletteFormat = (GLenum) decode_internal_format(internalFormat);
  179.       if (!proxy) {
  180.          MEMCPY(texObj->Palette, table->Data, table->Width*table->Components);
  181.          if (ctx->Driver.UpdateTexturePalette) {
  182.             (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
  183.          }
  184.       }
  185.    }
  186.    else {
  187.       /* shared texture palette */
  188.       ctx->Texture.PaletteSize = table->Width;
  189.       ctx->Texture.PaletteIntFormat = internalFormat;
  190.       ctx->Texture.PaletteFormat = (GLenum) decode_internal_format(internalFormat);
  191.       MEMCPY(ctx->Texture.Palette, table->Data, table->Width*table->Components);
  192.       if (ctx->Driver.UpdateTexturePalette) {
  193.          (*ctx->Driver.UpdateTexturePalette)( ctx, NULL );
  194.       }
  195.    }
  196. }
  197.  
  198.  
  199.  
  200. void gl_ColorSubTable( GLcontext *ctx, GLenum target,
  201.                        GLsizei start, struct gl_image *data )
  202. {
  203.    /* XXX TODO */
  204.    gl_problem(ctx, "glColorSubTableEXT not implemented");
  205.    (void) target;
  206.    (void) start;
  207.    (void) data;
  208. }
  209.  
  210.  
  211.  
  212. void gl_GetColorTable( GLcontext *ctx, GLenum target, GLenum format,
  213.                        GLenum type, GLvoid *table )
  214. {
  215.    ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetBooleanv");
  216.  
  217.    switch (target) {
  218.       case GL_TEXTURE_1D:
  219.          break;
  220.       case GL_TEXTURE_2D:
  221.          break;
  222.       case GL_TEXTURE_3D_EXT:
  223.          break;
  224.       case GL_SHARED_TEXTURE_PALETTE_EXT:
  225.          break;
  226.       default:
  227.          gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableEXT(target)");
  228.          return;
  229.    }
  230.  
  231.    gl_problem(ctx, "glGetColorTableEXT not implemented!");
  232.    (void) format;
  233.    (void) type;
  234.    (void) table;
  235. }
  236.  
  237.  
  238.  
  239. void gl_GetColorTableParameterfv( GLcontext *ctx, GLenum target,
  240.                                   GLenum pname, GLfloat *params )
  241. {
  242.    GLint iparams[10];
  243.  
  244.    gl_GetColorTableParameteriv( ctx, target, pname, iparams );
  245.    *params = (GLfloat) iparams[0];
  246. }
  247.  
  248.  
  249.  
  250. void gl_GetColorTableParameteriv( GLcontext *ctx, GLenum target,
  251.                                   GLenum pname, GLint *params )
  252. {
  253.    struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
  254.    struct gl_texture_object *texObj;
  255.  
  256.    ASSERT_OUTSIDE_BEGIN_END(ctx, "glGetColorTableParameter");
  257.  
  258.    switch (target) {
  259.       case GL_TEXTURE_1D:
  260.          texObj = texUnit->CurrentD[1];
  261.          break;
  262.       case GL_TEXTURE_2D:
  263.          texObj = texUnit->CurrentD[2];
  264.          break;
  265.       case GL_TEXTURE_3D_EXT:
  266.          texObj = texUnit->CurrentD[3];
  267.          break;
  268.       case GL_SHARED_TEXTURE_PALETTE_EXT:
  269.          texObj = NULL;
  270.          break;
  271.       default:
  272.          gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
  273.          return;
  274.    }
  275.  
  276.    switch (pname) {
  277.       case GL_COLOR_TABLE_FORMAT_EXT:
  278.          if (texObj)
  279.             *params = texObj->PaletteIntFormat;
  280.          else
  281.             *params = ctx->Texture.PaletteIntFormat;
  282.          break;
  283.       case GL_COLOR_TABLE_WIDTH_EXT:
  284.          if (texObj)
  285.             *params = texObj->PaletteSize;
  286.          else
  287.             *params = ctx->Texture.PaletteSize;
  288.          break;
  289.       case GL_COLOR_TABLE_RED_SIZE_EXT:
  290.          *params = 8;
  291.          break;
  292.       case GL_COLOR_TABLE_GREEN_SIZE_EXT:
  293.          *params = 8;
  294.          break;
  295.       case GL_COLOR_TABLE_BLUE_SIZE_EXT:
  296.          *params = 8;
  297.          break;
  298.       case GL_COLOR_TABLE_ALPHA_SIZE_EXT:
  299.          *params = 8;
  300.          break;
  301.       case GL_COLOR_TABLE_LUMINANCE_SIZE_EXT:
  302.          *params = 8;
  303.          break;
  304.       case GL_COLOR_TABLE_INTENSITY_SIZE_EXT:
  305.          *params = 8;
  306.          break;
  307.       default:
  308.          gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter" );
  309.          return;
  310.    }
  311. }
  312.  
  313.  
  314.