assorted changes for supporting GLfloat color channels (not done)
[mesa.git] / src / mesa / main / pixel.c
index 6bbbbdfe3ec511b8c3232a57951992616ac428b4..e0acfd5b93aa0c02559a0de40eedbe6597ce618a 100644 (file)
@@ -1,10 +1,10 @@
-/* $Id: pixel.c,v 1.22 2000/12/26 05:09:29 keithw Exp $ */
+/* $Id: pixel.c,v 1.30 2001/07/13 20:07:37 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
  * Version:  3.5
  *
- * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
+ * 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"),
@@ -75,20 +75,20 @@ _mesa_PixelStorei( GLenum pname, GLint param )
 
    switch (pname) {
       case GL_PACK_SWAP_BYTES:
-        if (param == (GLint)ctx->Pack.SwapBytes) 
+        if (param == (GLint)ctx->Pack.SwapBytes)
            return;
         FLUSH_VERTICES(ctx, _NEW_PACKUNPACK);
          ctx->Pack.SwapBytes = param ? GL_TRUE : GL_FALSE;
         break;
       case GL_PACK_LSB_FIRST:
-        if (param == (GLint)ctx->Pack.LsbFirst) 
+        if (param == (GLint)ctx->Pack.LsbFirst)
            return;
         FLUSH_VERTICES(ctx, _NEW_PACKUNPACK);
          ctx->Pack.LsbFirst = param ? GL_TRUE : GL_FALSE;
         break;
       case GL_PACK_ROW_LENGTH:
         if (param<0) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Pack.RowLength == param)
@@ -98,7 +98,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         break;
       case GL_PACK_IMAGE_HEIGHT:
          if (param<0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+            _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Pack.ImageHeight == param)
@@ -108,7 +108,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
          break;
       case GL_PACK_SKIP_PIXELS:
         if (param<0) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Pack.SkipPixels == param)
@@ -118,7 +118,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         break;
       case GL_PACK_SKIP_ROWS:
         if (param<0) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Pack.SkipRows == param)
@@ -128,7 +128,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         break;
       case GL_PACK_SKIP_IMAGES:
         if (param<0) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Pack.SkipImages == param)
@@ -138,7 +138,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         break;
       case GL_PACK_ALIGNMENT:
          if (param!=1 && param!=2 && param!=4 && param!=8) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Pack.Alignment == param)
@@ -147,7 +147,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         ctx->Pack.Alignment = param;
         break;
       case GL_UNPACK_SWAP_BYTES:
-        if (param == (GLint)ctx->Unpack.SwapBytes) 
+        if (param == (GLint)ctx->Unpack.SwapBytes)
            return;
         if ((GLint)ctx->Unpack.SwapBytes == param)
            return;
@@ -155,7 +155,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         ctx->Unpack.SwapBytes = param ? GL_TRUE : GL_FALSE;
          break;
       case GL_UNPACK_LSB_FIRST:
-        if (param == (GLint)ctx->Unpack.LsbFirst) 
+        if (param == (GLint)ctx->Unpack.LsbFirst)
            return;
         if ((GLint)ctx->Unpack.LsbFirst == param)
            return;
@@ -164,7 +164,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         break;
       case GL_UNPACK_ROW_LENGTH:
         if (param<0) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Unpack.RowLength == param)
@@ -174,7 +174,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         break;
       case GL_UNPACK_IMAGE_HEIGHT:
          if (param<0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+            _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Unpack.ImageHeight == param)
@@ -185,7 +185,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
          break;
       case GL_UNPACK_SKIP_PIXELS:
         if (param<0) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Unpack.SkipPixels == param)
@@ -195,7 +195,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         break;
       case GL_UNPACK_SKIP_ROWS:
         if (param<0) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Unpack.SkipRows == param)
@@ -205,7 +205,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         break;
       case GL_UNPACK_SKIP_IMAGES:
         if (param < 0) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
            return;
         }
         if (ctx->Unpack.SkipImages == param)
@@ -215,7 +215,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         break;
       case GL_UNPACK_ALIGNMENT:
          if (param!=1 && param!=2 && param!=4 && param!=8) {
-           gl_error( ctx, GL_INVALID_VALUE, "glPixelStore" );
+           _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore" );
            return;
         }
         if (ctx->Unpack.Alignment == param)
@@ -224,7 +224,7 @@ _mesa_PixelStorei( GLenum pname, GLint param )
         ctx->Unpack.Alignment = param;
         break;
       default:
-        gl_error( ctx, GL_INVALID_ENUM, "glPixelStore" );
+        _mesa_error( ctx, GL_INVALID_ENUM, "glPixelStore" );
         return;
    }
 }
@@ -252,7 +252,7 @@ _mesa_PixelMapfv( GLenum map, GLint mapsize, const GLfloat *values )
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (mapsize<0 || mapsize>MAX_PIXEL_MAP_TABLE) {
-      gl_error( ctx, GL_INVALID_VALUE, "glPixelMapfv(mapsize)" );
+      _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapfv(mapsize)" );
       return;
    }
 
@@ -267,7 +267,7 @@ _mesa_PixelMapfv( GLenum map, GLint mapsize, const GLfloat *values )
         }
       }
       if (!ok) {
-        gl_error( ctx, GL_INVALID_VALUE, "glPixelMapfv(mapsize)" );
+        _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapfv(mapsize)" );
          return;
       }
    }
@@ -344,7 +344,7 @@ _mesa_PixelMapfv( GLenum map, GLint mapsize, const GLfloat *values )
         }
         break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glPixelMapfv(map)" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glPixelMapfv(map)" );
    }
 }
 
@@ -433,7 +433,7 @@ _mesa_GetPixelMapfv( GLenum map, GLfloat *values )
          MEMCPY(values,ctx->Pixel.MapAtoA,ctx->Pixel.MapAtoAsize*sizeof(GLfloat));
         break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glGetPixelMapfv" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glGetPixelMapfv" );
    }
 }
 
@@ -493,7 +493,7 @@ _mesa_GetPixelMapuiv( GLenum map, GLuint *values )
         }
         break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glGetPixelMapfv" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glGetPixelMapfv" );
    }
 }
 
@@ -557,7 +557,7 @@ _mesa_GetPixelMapusv( GLenum map, GLushort *values )
         }
         break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glGetPixelMapfv" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glGetPixelMapfv" );
    }
 }
 
@@ -580,13 +580,13 @@ _mesa_PixelTransferf( GLenum pname, GLfloat param )
 
    switch (pname) {
       case GL_MAP_COLOR:
-         if (ctx->Pixel.MapColorFlag == param ? GL_TRUE : GL_FALSE)
+         if (ctx->Pixel.MapColorFlag == (param ? GL_TRUE : GL_FALSE))
            return;
         FLUSH_VERTICES(ctx, _NEW_PIXEL);
          ctx->Pixel.MapColorFlag = param ? GL_TRUE : GL_FALSE;
         break;
       case GL_MAP_STENCIL:
-         if (ctx->Pixel.MapStencilFlag == param ? GL_TRUE : GL_FALSE)
+         if (ctx->Pixel.MapStencilFlag == (param ? GL_TRUE : GL_FALSE))
            return;
         FLUSH_VERTICES(ctx, _NEW_PIXEL);
          ctx->Pixel.MapStencilFlag = param ? GL_TRUE : GL_FALSE;
@@ -760,7 +760,7 @@ _mesa_PixelTransferf( GLenum pname, GLfloat param )
          ctx->Pixel.PostConvolutionBias[2] = param;
         break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glPixelTransfer(pname)" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glPixelTransfer(pname)" );
          return;
    }
 }
@@ -832,10 +832,14 @@ _mesa_map_rgba( const GLcontext *ctx, GLuint n, GLfloat rgba[][4] )
    const GLfloat *aMap = ctx->Pixel.MapAtoA;
    GLuint i;
    for (i=0;i<n;i++) {
-      rgba[i][RCOMP] = rMap[(GLint) (rgba[i][RCOMP] * rscale + 0.5F)];
-      rgba[i][GCOMP] = gMap[(GLint) (rgba[i][GCOMP] * gscale + 0.5F)];
-      rgba[i][BCOMP] = bMap[(GLint) (rgba[i][BCOMP] * bscale + 0.5F)];
-      rgba[i][ACOMP] = aMap[(GLint) (rgba[i][ACOMP] * ascale + 0.5F)];
+      GLfloat r = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
+      GLfloat g = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
+      GLfloat b = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
+      GLfloat a = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
+      rgba[i][RCOMP] = rMap[IROUND(r * rscale)];
+      rgba[i][GCOMP] = gMap[IROUND(g * gscale)];
+      rgba[i][BCOMP] = bMap[IROUND(b * bscale)];
+      rgba[i][ACOMP] = aMap[IROUND(a * ascale)];
    }
 }
 
@@ -877,31 +881,33 @@ _mesa_lookup_rgba(const struct gl_color_table *table,
                   GLuint n, GLfloat rgba[][4])
 {
    ASSERT(table->FloatTable);
-   if (!table->Table)
+   if (!table->Table || table->Size == 0)
       return;
 
    switch (table->Format) {
       case GL_INTENSITY:
          /* replace RGBA with I */
          if (!table->FloatTable) {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLchan *lut = (const GLchan *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint j = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLfloat c = CHAN_TO_FLOAT(lut[j]);
+               GLint j = IROUND(rgba[i][RCOMP] * scale);
+               GLfloat c = CHAN_TO_FLOAT(lut[CLAMP(j, 0, 1)]);
                rgba[i][RCOMP] = rgba[i][GCOMP] =
                   rgba[i][BCOMP] = rgba[i][ACOMP] = c;
             }
 
          }
          else {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLfloat *lut = (const GLfloat *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint j = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLfloat c = lut[j];
+               GLint j = IROUND(rgba[i][RCOMP] * scale);
+               GLfloat c = lut[CLAMP(j, 0, max)];
                rgba[i][RCOMP] = rgba[i][GCOMP] =
                   rgba[i][BCOMP] = rgba[i][ACOMP] = c;
             }
@@ -910,22 +916,24 @@ _mesa_lookup_rgba(const struct gl_color_table *table,
       case GL_LUMINANCE:
          /* replace RGB with L */
          if (!table->FloatTable) {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLchan *lut = (const GLchan *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint j = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLfloat c = CHAN_TO_FLOAT(lut[j]);
+               GLint j = IROUND(rgba[i][RCOMP] * scale);
+               GLfloat c = CHAN_TO_FLOAT(lut[CLAMP(j, 0, max)]);
                rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = c;
             }
          }
          else {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLfloat *lut = (const GLfloat *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint j = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLfloat c = lut[j];
+               GLint j = IROUND(rgba[i][RCOMP] * scale);
+               GLfloat c = lut[CLAMP(j, 0, max)];
                rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = c;
             }
          }
@@ -933,48 +941,58 @@ _mesa_lookup_rgba(const struct gl_color_table *table,
       case GL_ALPHA:
          /* replace A with A */
          if (!table->FloatTable) {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLchan *lut = (const GLchan *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint j = (GLint) (rgba[i][ACOMP] * scale + 0.5F);
-               rgba[i][ACOMP] = CHAN_TO_FLOAT(lut[j]);
+               GLint j = IROUND(rgba[i][ACOMP] * scale);
+               rgba[i][ACOMP] = CHAN_TO_FLOAT(lut[CLAMP(j, 0, max)]);
             }
          }
          else  {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLfloat *lut = (const GLfloat *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint j = (GLint) (rgba[i][ACOMP] * scale + 0.5F);
-               rgba[i][ACOMP] = lut[j];
+               GLint j = IROUND(rgba[i][ACOMP] * scale);
+               rgba[i][ACOMP] = lut[CLAMP(j, 0, max)];
             }
          }
          break;
       case GL_LUMINANCE_ALPHA:
          /* replace RGBA with LLLA */
          if (!table->FloatTable) {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLchan *lut = (const GLchan *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint jL = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLint jA = (GLint) (rgba[i][ACOMP] * scale + 0.5F);
-               GLfloat luminance = CHAN_TO_FLOAT(lut[jL * 2 + 0]);
-               GLfloat alpha     = CHAN_TO_FLOAT(lut[jA * 2 + 1]);
+               GLint jL = IROUND(rgba[i][RCOMP] * scale);
+               GLint jA = IROUND(rgba[i][ACOMP] * scale);
+               GLfloat luminance, alpha;
+               jL = CLAMP(jL, 0, max);
+               jA = CLAMP(jA, 0, max);
+               luminance = CHAN_TO_FLOAT(lut[jL * 2 + 0]);
+               alpha     = CHAN_TO_FLOAT(lut[jA * 2 + 1]);
                rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = luminance;
                rgba[i][ACOMP] = alpha;;
             }
          }
          else {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLfloat *lut = (const GLfloat *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint jL = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLint jA = (GLint) (rgba[i][ACOMP] * scale + 0.5F);
-               GLfloat luminance = lut[jL * 2 + 0];
-               GLfloat alpha     = lut[jA * 2 + 1];
+               GLint jL = IROUND(rgba[i][RCOMP] * scale);
+               GLint jA = IROUND(rgba[i][ACOMP] * scale);
+               GLfloat luminance, alpha;
+               jL = CLAMP(jL, 0, max);
+               jA = CLAMP(jA, 0, max);
+               luminance = lut[jL * 2 + 0];
+               alpha     = lut[jA * 2 + 1];
                rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = luminance;
                rgba[i][ACOMP] = alpha;;
             }
@@ -983,26 +1001,34 @@ _mesa_lookup_rgba(const struct gl_color_table *table,
       case GL_RGB:
          /* replace RGB with RGB */
          if (!table->FloatTable) {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLchan *lut = (const GLchan *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint jR = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLint jG = (GLint) (rgba[i][GCOMP] * scale + 0.5F);
-               GLint jB = (GLint) (rgba[i][BCOMP] * scale + 0.5F);
+               GLint jR = IROUND(rgba[i][RCOMP] * scale);
+               GLint jG = IROUND(rgba[i][GCOMP] * scale);
+               GLint jB = IROUND(rgba[i][BCOMP] * scale);
+               jR = CLAMP(jR, 0, max);
+               jG = CLAMP(jG, 0, max);
+               jB = CLAMP(jB, 0, max);
                rgba[i][RCOMP] = CHAN_TO_FLOAT(lut[jR * 3 + 0]);
                rgba[i][GCOMP] = CHAN_TO_FLOAT(lut[jG * 3 + 1]);
                rgba[i][BCOMP] = CHAN_TO_FLOAT(lut[jB * 3 + 2]);
             }
          }
          else {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLfloat *lut = (const GLfloat *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint jR = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLint jG = (GLint) (rgba[i][GCOMP] * scale + 0.5F);
-               GLint jB = (GLint) (rgba[i][BCOMP] * scale + 0.5F);
+               GLint jR = IROUND(rgba[i][RCOMP] * scale);
+               GLint jG = IROUND(rgba[i][GCOMP] * scale);
+               GLint jB = IROUND(rgba[i][BCOMP] * scale);
+               jR = CLAMP(jR, 0, max);
+               jG = CLAMP(jG, 0, max);
+               jB = CLAMP(jB, 0, max);
                rgba[i][RCOMP] = lut[jR * 3 + 0];
                rgba[i][GCOMP] = lut[jG * 3 + 1];
                rgba[i][BCOMP] = lut[jB * 3 + 2];
@@ -1012,14 +1038,19 @@ _mesa_lookup_rgba(const struct gl_color_table *table,
       case GL_RGBA:
          /* replace RGBA with RGBA */
          if (!table->FloatTable) {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLchan *lut = (const GLchan *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint jR = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLint jG = (GLint) (rgba[i][GCOMP] * scale + 0.5F);
-               GLint jB = (GLint) (rgba[i][BCOMP] * scale + 0.5F);
-               GLint jA = (GLint) (rgba[i][ACOMP] * scale + 0.5F);
+               GLint jR = IROUND(rgba[i][RCOMP] * scale);
+               GLint jG = IROUND(rgba[i][GCOMP] * scale);
+               GLint jB = IROUND(rgba[i][BCOMP] * scale);
+               GLint jA = IROUND(rgba[i][ACOMP] * scale);
+               jR = CLAMP(jR, 0, max);
+               jG = CLAMP(jG, 0, max);
+               jB = CLAMP(jB, 0, max);
+               jA = CLAMP(jA, 0, max);
                rgba[i][RCOMP] = CHAN_TO_FLOAT(lut[jR * 4 + 0]);
                rgba[i][GCOMP] = CHAN_TO_FLOAT(lut[jG * 4 + 1]);
                rgba[i][BCOMP] = CHAN_TO_FLOAT(lut[jB * 4 + 2]);
@@ -1027,14 +1058,19 @@ _mesa_lookup_rgba(const struct gl_color_table *table,
             }
          }
          else {
-            const GLfloat scale = (GLfloat) (table->Size - 1);
+            const GLint max = table->Size - 1;
+            const GLfloat scale = (GLfloat) max;
             const GLfloat *lut = (const GLfloat *) table->Table;
             GLuint i;
             for (i = 0; i < n; i++) {
-               GLint jR = (GLint) (rgba[i][RCOMP] * scale + 0.5F);
-               GLint jG = (GLint) (rgba[i][GCOMP] * scale + 0.5F);
-               GLint jB = (GLint) (rgba[i][BCOMP] * scale + 0.5F);
-               GLint jA = (GLint) (rgba[i][ACOMP] * scale + 0.5F);
+               GLint jR = IROUND(rgba[i][RCOMP] * scale);
+               GLint jG = IROUND(rgba[i][GCOMP] * scale);
+               GLint jB = IROUND(rgba[i][BCOMP] * scale);
+               GLint jA = IROUND(rgba[i][ACOMP] * scale);
+               jR = CLAMP(jR, 0, max);
+               jG = CLAMP(jG, 0, max);
+               jB = CLAMP(jB, 0, max);
+               jA = CLAMP(jA, 0, max);
                rgba[i][RCOMP] = lut[jR * 4 + 0];
                rgba[i][GCOMP] = lut[jG * 4 + 1];
                rgba[i][BCOMP] = lut[jB * 4 + 2];
@@ -1043,7 +1079,7 @@ _mesa_lookup_rgba(const struct gl_color_table *table,
          }
          break;
       default:
-         gl_problem(NULL, "Bad format in _mesa_lookup_rgba");
+         _mesa_problem(NULL, "Bad format in _mesa_lookup_rgba");
          return;
    }
 }
@@ -1126,10 +1162,10 @@ _mesa_map_ci_to_rgba_chan( const GLcontext *ctx, GLuint n,
    const GLfloat *aMap = ctx->Pixel.MapItoA;
    GLuint i;
    for (i=0;i<n;i++) {
-      rgba[i][RCOMP] = FLOAT_TO_CHAN(rMap[index[i] & rmask]);
-      rgba[i][GCOMP] = FLOAT_TO_CHAN(gMap[index[i] & gmask]);
-      rgba[i][BCOMP] = FLOAT_TO_CHAN(bMap[index[i] & bmask]);
-      rgba[i][ACOMP] = FLOAT_TO_CHAN(aMap[index[i] & amask]);
+      CLAMPED_FLOAT_TO_CHAN(rgba[i][RCOMP], rMap[index[i] & rmask]);
+      CLAMPED_FLOAT_TO_CHAN(rgba[i][GCOMP], gMap[index[i] & gmask]);
+      CLAMPED_FLOAT_TO_CHAN(rgba[i][BCOMP], bMap[index[i] & bmask]);
+      CLAMPED_FLOAT_TO_CHAN(rgba[i][ACOMP], aMap[index[i] & amask]);
    }
 #endif
 }
@@ -1194,10 +1230,10 @@ _mesa_map_ci8_to_rgba( const GLcontext *ctx, GLuint n, const GLubyte index[],
    const GLfloat *aMap = ctx->Pixel.MapItoA;
    GLuint i;
    for (i=0;i<n;i++) {
-      rgba[i][RCOMP] = FLOAT_TO_CHAN(rMap[index[i] & rmask]);
-      rgba[i][GCOMP] = FLOAT_TO_CHAN(gMap[index[i] & gmask]);
-      rgba[i][BCOMP] = FLOAT_TO_CHAN(bMap[index[i] & bmask]);
-      rgba[i][ACOMP] = FLOAT_TO_CHAN(aMap[index[i] & amask]);
+      CLAMPED_FLOAT_TO_CHAN(rgba[i][RCOMP], rMap[index[i] & rmask]);
+      CLAMPED_FLOAT_TO_CHAN(rgba[i][GCOMP], gMap[index[i] & gmask]);
+      CLAMPED_FLOAT_TO_CHAN(rgba[i][BCOMP], bMap[index[i] & bmask]);
+      CLAMPED_FLOAT_TO_CHAN(rgba[i][ACOMP], aMap[index[i] & amask]);
    }
 #endif
 }
@@ -1254,19 +1290,22 @@ void
 _mesa_chan_to_float_span(const GLcontext *ctx, GLuint n,
                          CONST GLchan rgba[][4], GLfloat rgbaf[][4])
 {
-   const GLuint rShift = CHAN_BITS - ctx->Visual.RedBits;
-   const GLuint gShift = CHAN_BITS - ctx->Visual.GreenBits;
-   const GLuint bShift = CHAN_BITS - ctx->Visual.BlueBits;
+#if CHAN_TYPE == GL_FLOAT
+   MEMCPY(rgbaf, rgba, n * 4 * sizeof(GLfloat));
+#else
+   const GLuint rShift = CHAN_BITS - ctx->Visual.redBits;
+   const GLuint gShift = CHAN_BITS - ctx->Visual.greenBits;
+   const GLuint bShift = CHAN_BITS - ctx->Visual.blueBits;
    GLuint aShift;
-   const GLfloat rScale = 1.0 / (GLfloat) ((1 << ctx->Visual.RedBits  ) - 1);
-   const GLfloat gScale = 1.0 / (GLfloat) ((1 << ctx->Visual.GreenBits) - 1);
-   const GLfloat bScale = 1.0 / (GLfloat) ((1 << ctx->Visual.BlueBits ) - 1);
+   const GLfloat rScale = 1.0 / (GLfloat) ((1 << ctx->Visual.redBits  ) - 1);
+   const GLfloat gScale = 1.0 / (GLfloat) ((1 << ctx->Visual.greenBits) - 1);
+   const GLfloat bScale = 1.0 / (GLfloat) ((1 << ctx->Visual.blueBits ) - 1);
    GLfloat aScale;
    GLuint i;
 
-   if (ctx->Visual.AlphaBits > 0) {
-      aShift = CHAN_BITS - ctx->Visual.AlphaBits;
-      aScale = 1.0 / (GLfloat) ((1 << ctx->Visual.AlphaBits) - 1);
+   if (ctx->Visual.alphaBits > 0) {
+      aShift = CHAN_BITS - ctx->Visual.alphaBits;
+      aScale = 1.0 / (GLfloat) ((1 << ctx->Visual.alphaBits) - 1);
    }
    else {
       aShift = 0;
@@ -1283,4 +1322,5 @@ _mesa_chan_to_float_span(const GLcontext *ctx, GLuint n,
       rgbaf[i][BCOMP] = (GLfloat) b * bScale;
       rgbaf[i][ACOMP] = (GLfloat) a * aScale;
    }
+#endif
 }