home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src / colortab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  8.1 KB  |  322 lines

  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_TEXTURE_3D_EXT:
  148.          texObj = ctx->Texture.Current3D;
  149.          break;
  150.       case GL_PROXY_TEXTURE_1D:
  151.          texObj = ctx->Texture.Proxy1D;
  152.          proxy = GL_TRUE;
  153.          break;
  154.       case GL_PROXY_TEXTURE_2D:
  155.          texObj = ctx->Texture.Proxy2D;
  156.          proxy = GL_TRUE;
  157.          break;
  158.       case GL_PROXY_TEXTURE_3D_EXT:
  159.          texObj = ctx->Texture.Proxy3D;
  160.          proxy = GL_TRUE;
  161.          break;
  162.       case GL_SHARED_TEXTURE_PALETTE_EXT:
  163.          texObj = NULL;
  164.          break;
  165.       default:
  166.          gl_error(ctx, GL_INVALID_ENUM, "glColorTableEXT(target)");
  167.          return;
  168.    }
  169.  
  170.    /* internalformat = just like glTexImage */
  171.  
  172.    if (table->Width < 1 || table->Width > MAX_TEXTURE_PALETTE_SIZE
  173.        || !power_of_two(table->Width)) {
  174.       gl_error(ctx, GL_INVALID_VALUE, "glColorTableEXT(width)");
  175.       if (proxy) {
  176.          texObj->PaletteSize = 0;
  177.          texObj->PaletteIntFormat = 0;
  178.          texObj->PaletteFormat = 0;
  179.       }
  180.       return;
  181.    }
  182.  
  183.    if (texObj) {
  184.       /* per-texture object palette */
  185.       texObj->PaletteSize = table->Width;
  186.       texObj->PaletteIntFormat = internalFormat;
  187.       texObj->PaletteFormat = decode_internal_format(internalFormat);
  188.       if (!proxy) {
  189.          MEMCPY(texObj->Palette, table->Data, table->Width*table->Components);
  190.          if (ctx->Driver.UpdateTexturePalette) {
  191.             (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
  192.          }
  193.       }
  194.    }
  195.    else {
  196.       /* shared texture palette */
  197.       ctx->Texture.PaletteSize = table->Width;
  198.       ctx->Texture.PaletteIntFormat = internalFormat;
  199.       ctx->Texture.PaletteFormat = decode_internal_format(internalFormat);
  200.       MEMCPY(ctx->Texture.Palette, table->Data, table->Width*table->Components);
  201.       if (ctx->Driver.UpdateTexturePalette) {
  202.          (*ctx->Driver.UpdateTexturePalette)( ctx, NULL );
  203.       }
  204.    }
  205. }
  206.  
  207.  
  208.  
  209. void gl_ColorSubTable( GLcontext *ctx, GLenum target,
  210.                        GLsizei start, struct gl_image *data )
  211. {
  212.    /* XXX TODO */
  213.    gl_problem(ctx, "glColorSubTableEXT not implemented");
  214. }
  215.  
  216.  
  217.  
  218. void gl_GetColorTable( GLcontext *ctx, GLenum target, GLenum format,
  219.                        GLenum type, GLvoid *table )
  220. {
  221.    if (INSIDE_BEGIN_END(ctx)) {
  222.       gl_error( ctx, GL_INVALID_OPERATION, "glGetBooleanv" );
  223.       return;
  224.    }
  225.  
  226.    switch (target) {
  227.       case GL_TEXTURE_1D:
  228.          break;
  229.       case GL_TEXTURE_2D:
  230.          break;
  231.       case GL_TEXTURE_3D_EXT:
  232.          break;
  233.       case GL_SHARED_TEXTURE_PALETTE_EXT:
  234.          break;
  235.       default:
  236.          gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableEXT(target)");
  237.          return;
  238.    }
  239.  
  240.    gl_problem(ctx, "glGetColorTableEXT not implemented!");
  241. }
  242.  
  243.  
  244.  
  245. void gl_GetColorTableParameterfv( GLcontext *ctx, GLenum target,
  246.                                   GLenum pname, GLfloat *params )
  247. {
  248.    GLint iparams[10];
  249.  
  250.    gl_GetColorTableParameteriv( ctx, target, pname, iparams );
  251.    *params = (GLfloat) iparams[0];
  252. }
  253.  
  254.  
  255.  
  256. void gl_GetColorTableParameteriv( GLcontext *ctx, GLenum target,
  257.                                   GLenum pname, GLint *params )
  258. {
  259.    struct gl_texture_object *texObj;
  260.  
  261.    if (INSIDE_BEGIN_END(ctx)) {
  262.       gl_error( ctx, GL_INVALID_OPERATION, "glGetColorTableParameter" );
  263.       return;
  264.    }
  265.  
  266.    switch (target) {
  267.       case GL_TEXTURE_1D:
  268.          texObj = ctx->Texture.Current1D;
  269.          break;
  270.       case GL_TEXTURE_2D:
  271.          texObj = ctx->Texture.Current2D;
  272.          break;
  273.       case GL_TEXTURE_3D_EXT:
  274.          texObj = ctx->Texture.Current3D;
  275.          break;
  276.       case GL_SHARED_TEXTURE_PALETTE_EXT:
  277.          texObj = NULL;
  278.          break;
  279.       default:
  280.          gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter(target)");
  281.          return;
  282.    }
  283.  
  284.    switch (pname) {
  285.       case GL_COLOR_TABLE_FORMAT_EXT:
  286.          if (texObj)
  287.             *params = texObj->PaletteIntFormat;
  288.          else
  289.             *params = ctx->Texture.PaletteIntFormat;
  290.          break;
  291.       case GL_COLOR_TABLE_WIDTH_EXT:
  292.          if (texObj)
  293.             *params = texObj->PaletteSize;
  294.          else
  295.             *params = ctx->Texture.PaletteSize;
  296.          break;
  297.       case GL_COLOR_TABLE_RED_SIZE_EXT:
  298.          *params = 8;
  299.          break;
  300.       case GL_COLOR_TABLE_GREEN_SIZE_EXT:
  301.          *params = 8;
  302.          break;
  303.       case GL_COLOR_TABLE_BLUE_SIZE_EXT:
  304.          *params = 8;
  305.          break;
  306.       case GL_COLOR_TABLE_ALPHA_SIZE_EXT:
  307.          *params = 8;
  308.          break;
  309.       case GL_COLOR_TABLE_LUMINANCE_SIZE_EXT:
  310.          *params = 8;
  311.          break;
  312.       case GL_COLOR_TABLE_INTENSITY_SIZE_EXT:
  313.          *params = 8;
  314.          break;
  315.       default:
  316.          gl_error(ctx, GL_INVALID_ENUM, "glGetColorTableParameter" );
  317.          return;
  318.    }
  319. }
  320.  
  321.  
  322.