bug fixes, added missing state query cases
[mesa.git] / src / mesa / main / colortab.c
index f9693c6ba78cf033c1f699eefdc4eb8662465797..61f93be2aadaad6fd986212503b2e9eeed107c25 100644 (file)
@@ -1,21 +1,21 @@
-/* $Id: colortab.c,v 1.35 2001/03/03 20:33:27 brianp Exp $ */
+/* $Id: colortab.c,v 1.39 2001/04/10 15:25:45 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
  * Version:  3.5
- * 
+ *
  * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
@@ -184,7 +184,7 @@ set_component_sizes( struct gl_color_table *table )
 
 
 
-void 
+void
 _mesa_ColorTable( GLenum target, GLenum internalFormat,
                   GLsizei width, GLenum format, GLenum type,
                   const GLvoid *data )
@@ -314,7 +314,7 @@ _mesa_ColorTable( GLenum target, GLenum internalFormat,
       return;
    }
 
-   if (width > ctx->Const.MaxColorTableSize) {
+   if (width > (GLsizei) ctx->Const.MaxColorTableSize) {
       if (proxy) {
          table->Size = 0;
          table->IntFormat = (GLenum) 0;
@@ -344,7 +344,7 @@ _mesa_ColorTable( GLenum target, GLenum internalFormat,
          if (floatTable) {
             GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
             GLfloat *tableF;
-            GLuint i;
+            GLint i;
 
             _mesa_unpack_float_color_span(ctx, width, table->Format,
                                           tempTab,  /* dest */
@@ -411,7 +411,7 @@ _mesa_ColorTable( GLenum target, GLenum internalFormat,
                return;
             }
             _mesa_unpack_chan_color_span(ctx, width, table->Format,
-                                         table->Table,  /* dest */
+                                         (GLchan *) table->Table,  /* dest */
                                          format, type, data,
                                          &ctx->Unpack, 0);
          } /* floatTable */
@@ -424,7 +424,7 @@ _mesa_ColorTable( GLenum target, GLenum internalFormat,
          (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
       }
    }
-   
+
    ctx->NewState |= _NEW_PIXEL;
 }
 
@@ -514,7 +514,7 @@ _mesa_ColorSubTable( GLenum target, GLsizei start,
    comps = _mesa_components_in_format(table->Format);
    assert(comps > 0);  /* error should have been caught sooner */
 
-   if (start + count > table->Size) {
+   if (start + count > (GLint) table->Size) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glColorSubTable(count)");
       return;
    }
@@ -532,7 +532,7 @@ _mesa_ColorSubTable( GLenum target, GLsizei start,
    else {
       GLfloat tempTab[MAX_COLOR_TABLE_SIZE * 4];
       GLfloat *tableF;
-      GLuint i;
+      GLint i;
 
       ASSERT(table->FloatTable);
 
@@ -598,7 +598,7 @@ _mesa_ColorSubTable( GLenum target, GLsizei start,
          (*ctx->Driver.UpdateTexturePalette)( ctx, texObj );
       }
    }
-   
+
    ctx->NewState |= _NEW_PIXEL;
 }
 
@@ -609,28 +609,11 @@ void
 _mesa_CopyColorTable(GLenum target, GLenum internalformat,
                      GLint x, GLint y, GLsizei width)
 {
-   GLchan data[MAX_WIDTH][4];
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    /* Select buffer to read from */
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                 ctx->Pixel.DriverReadBuffer );
-
-   if (width > MAX_WIDTH)
-      width = MAX_WIDTH;
-
-   /* read the data from framebuffer */
-   RENDER_START(ctx);
-   _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y, data );
-   RENDER_FINISH(ctx);
-
-   /* Restore reading from draw buffer (the default) */
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                 ctx->Color.DriverDrawBuffer );
-
-   _mesa_ColorTable(target, internalformat, width,
-                    GL_RGBA, GL_UNSIGNED_BYTE, data);
+   ctx->Driver.CopyColorTable( ctx, target, internalformat, x, y, width );
 }
 
 
@@ -640,27 +623,10 @@ void
 _mesa_CopyColorSubTable(GLenum target, GLsizei start,
                         GLint x, GLint y, GLsizei width)
 {
-   GLchan data[MAX_WIDTH][4];
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
-   /* Select buffer to read from */
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                 ctx->Pixel.DriverReadBuffer );
-
-   if (width > MAX_WIDTH)
-      width = MAX_WIDTH;
-
-   /* read the data from framebuffer */
-   RENDER_START(ctx);
-   _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y, data );
-   RENDER_FINISH(ctx);
-
-   /* Restore reading from draw buffer (the default) */
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                 ctx->Color.DriverDrawBuffer );
-
-   _mesa_ColorSubTable(target, start, width, GL_RGBA, GL_UNSIGNED_BYTE, data);
+   ctx->Driver.CopyColorSubTable( ctx, target, start, x, y, width );
 }
 
 
@@ -673,7 +639,6 @@ _mesa_GetColorTable( GLenum target, GLenum format,
    struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
    struct gl_color_table *table = NULL;
    GLchan rgba[MAX_COLOR_TABLE_SIZE][4];
-   GLint i;
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
 
    if (ctx->NewState) {
@@ -713,15 +678,17 @@ _mesa_GetColorTable( GLenum target, GLenum format,
       case GL_ALPHA:
          if (table->FloatTable) {
             const GLfloat *tableF = (const GLfloat *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
                rgba[i][RCOMP] = 0;
                rgba[i][GCOMP] = 0;
                rgba[i][BCOMP] = 0;
-               rgba[i][ACOMP] = (GLint) (tableF[i] * CHAN_MAXF);
+               rgba[i][ACOMP] = IROUND_POS(tableF[i] * CHAN_MAXF);
             }
          }
          else {
             const GLchan *tableUB = (const GLchan *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
                rgba[i][RCOMP] = 0;
                rgba[i][GCOMP] = 0;
@@ -733,15 +700,17 @@ _mesa_GetColorTable( GLenum target, GLenum format,
       case GL_LUMINANCE:
          if (table->FloatTable) {
             const GLfloat *tableF = (const GLfloat *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
-               rgba[i][RCOMP] = (GLint) (tableF[i] * CHAN_MAXF);
-               rgba[i][GCOMP] = (GLint) (tableF[i] * CHAN_MAXF);
-               rgba[i][BCOMP] = (GLint) (tableF[i] * CHAN_MAXF);
+               rgba[i][RCOMP] = IROUND_POS(tableF[i] * CHAN_MAXF);
+               rgba[i][GCOMP] = IROUND_POS(tableF[i] * CHAN_MAXF);
+               rgba[i][BCOMP] = IROUND_POS(tableF[i] * CHAN_MAXF);
                rgba[i][ACOMP] = CHAN_MAX;
             }
          }
          else {
             const GLchan *tableUB = (const GLchan *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
                rgba[i][RCOMP] = tableUB[i];
                rgba[i][GCOMP] = tableUB[i];
@@ -753,15 +722,17 @@ _mesa_GetColorTable( GLenum target, GLenum format,
       case GL_LUMINANCE_ALPHA:
          if (table->FloatTable) {
             const GLfloat *tableF = (const GLfloat *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
-               rgba[i][RCOMP] = (GLint) (tableF[i*2+0] * CHAN_MAXF);
-               rgba[i][GCOMP] = (GLint) (tableF[i*2+0] * CHAN_MAXF);
-               rgba[i][BCOMP] = (GLint) (tableF[i*2+0] * CHAN_MAXF);
-               rgba[i][ACOMP] = (GLint) (tableF[i*2+1] * CHAN_MAXF);
+               rgba[i][RCOMP] = IROUND_POS(tableF[i*2+0] * CHAN_MAXF);
+               rgba[i][GCOMP] = IROUND_POS(tableF[i*2+0] * CHAN_MAXF);
+               rgba[i][BCOMP] = IROUND_POS(tableF[i*2+0] * CHAN_MAXF);
+               rgba[i][ACOMP] = IROUND_POS(tableF[i*2+1] * CHAN_MAXF);
             }
          }
          else {
             const GLchan *tableUB = (const GLchan *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
                rgba[i][RCOMP] = tableUB[i*2+0];
                rgba[i][GCOMP] = tableUB[i*2+0];
@@ -773,15 +744,17 @@ _mesa_GetColorTable( GLenum target, GLenum format,
       case GL_INTENSITY:
          if (table->FloatTable) {
             const GLfloat *tableF = (const GLfloat *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
-               rgba[i][RCOMP] = (GLint) (tableF[i] * CHAN_MAXF);
-               rgba[i][GCOMP] = (GLint) (tableF[i] * CHAN_MAXF);
-               rgba[i][BCOMP] = (GLint) (tableF[i] * CHAN_MAXF);
-               rgba[i][ACOMP] = (GLint) (tableF[i] * CHAN_MAXF);
+               rgba[i][RCOMP] = IROUND_POS(tableF[i] * CHAN_MAXF);
+               rgba[i][GCOMP] = IROUND_POS(tableF[i] * CHAN_MAXF);
+               rgba[i][BCOMP] = IROUND_POS(tableF[i] * CHAN_MAXF);
+               rgba[i][ACOMP] = IROUND_POS(tableF[i] * CHAN_MAXF);
             }
          }
          else {
             const GLchan *tableUB = (const GLchan *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
                rgba[i][RCOMP] = tableUB[i];
                rgba[i][GCOMP] = tableUB[i];
@@ -793,15 +766,17 @@ _mesa_GetColorTable( GLenum target, GLenum format,
       case GL_RGB:
          if (table->FloatTable) {
             const GLfloat *tableF = (const GLfloat *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
-               rgba[i][RCOMP] = (GLint) (tableF[i*3+0] * CHAN_MAXF);
-               rgba[i][GCOMP] = (GLint) (tableF[i*3+1] * CHAN_MAXF);
-               rgba[i][BCOMP] = (GLint) (tableF[i*3+2] * CHAN_MAXF);
+               rgba[i][RCOMP] = IROUND_POS(tableF[i*3+0] * CHAN_MAXF);
+               rgba[i][GCOMP] = IROUND_POS(tableF[i*3+1] * CHAN_MAXF);
+               rgba[i][BCOMP] = IROUND_POS(tableF[i*3+2] * CHAN_MAXF);
                rgba[i][ACOMP] = CHAN_MAX;
             }
          }
          else {
             const GLchan *tableUB = (const GLchan *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
                rgba[i][RCOMP] = tableUB[i*3+0];
                rgba[i][GCOMP] = tableUB[i*3+1];
@@ -813,15 +788,17 @@ _mesa_GetColorTable( GLenum target, GLenum format,
       case GL_RGBA:
          if (table->FloatTable) {
             const GLfloat *tableF = (const GLfloat *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
-               rgba[i][RCOMP] = (GLint) (tableF[i*4+0] * CHAN_MAXF + 0.5F);
-               rgba[i][GCOMP] = (GLint) (tableF[i*4+1] * CHAN_MAXF + 0.5F);
-               rgba[i][BCOMP] = (GLint) (tableF[i*4+2] * CHAN_MAXF + 0.5F);
-               rgba[i][ACOMP] = (GLint) (tableF[i*4+3] * CHAN_MAXF + 0.5F);
+               rgba[i][RCOMP] = IROUND_POS(tableF[i*4+0] * CHAN_MAXF);
+               rgba[i][GCOMP] = IROUND_POS(tableF[i*4+1] * CHAN_MAXF);
+               rgba[i][BCOMP] = IROUND_POS(tableF[i*4+2] * CHAN_MAXF);
+               rgba[i][ACOMP] = IROUND_POS(tableF[i*4+3] * CHAN_MAXF);
             }
          }
          else {
             const GLchan *tableUB = (const GLchan *) table->Table;
+            GLuint i;
             for (i = 0; i < table->Size; i++) {
                rgba[i][RCOMP] = tableUB[i*4+0];
                rgba[i][GCOMP] = tableUB[i*4+1];