drawing color-index image to RGB window was broken
[mesa.git] / src / mesa / main / image.c
index 9869d8cf949fb434734820d5efd522111de4c882..af60908280c7903bc9ca1e0a0de557c932c5153d 100644 (file)
@@ -1,8 +1,8 @@
-/* $Id: image.c,v 1.21 2000/03/21 00:48:53 brianp Exp $ */
+/* $Id: image.c,v 1.43 2000/10/05 16:22:22 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  3.3
+ * Version:  3.5
  * 
  * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
  * 
@@ -31,6 +31,7 @@
 #include "glheader.h"
 #include "context.h"
 #include "image.h"
+#include "imaging.h"
 #include "macros.h"
 #include "mem.h"
 #include "mmath.h"
@@ -47,7 +48,7 @@
  * from within display lists we have to be sure to set the current
  * unpacking params to these values!
  */
-struct gl_pixelstore_attrib _mesa_native_packing = {
+const struct gl_pixelstore_attrib _mesa_native_packing = {
    1,            /* Alignment */
    0,            /* RowLength */
    0,            /* SkipPixels */
@@ -62,13 +63,19 @@ struct gl_pixelstore_attrib _mesa_native_packing = {
 
 /*
  * Flip the 8 bits in each byte of the given array.
+ *
+ * XXX try this trick to flip bytes someday:
+ *  v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
+ *  v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
+ *  v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
  */
-void gl_flip_bytes( GLubyte *p, GLuint n )
+static void
+flip_bytes( GLubyte *p, GLuint n )
 {
    register GLuint i, a, b;
 
    for (i=0;i<n;i++) {
-      b = (GLuint) p[i];
+      b = (GLuint) p[i];        /* words are often faster than bytes */
       a = ((b & 0x01) << 7) |
          ((b & 0x02) << 5) |
          ((b & 0x04) << 3) |
@@ -85,7 +92,8 @@ void gl_flip_bytes( GLubyte *p, GLuint n )
 /*
  * Flip the order of the 2 bytes in each word in the given array.
  */
-void gl_swap2( GLushort *p, GLuint n )
+void
+_mesa_swap2( GLushort *p, GLuint n )
 {
    register GLuint i;
 
@@ -99,7 +107,8 @@ void gl_swap2( GLushort *p, GLuint n )
 /*
  * Flip the order of the 4 bytes in each word in the given array.
  */
-void gl_swap4( GLuint *p, GLuint n )
+void
+_mesa_swap4( GLuint *p, GLuint n )
 {
    register GLuint i, a, b;
 
@@ -121,7 +130,7 @@ void gl_swap4( GLuint *p, GLuint n )
  * Return 0 if GL_BITMAP.
  * Return -1 if invalid type enum.
  */
-GLint gl_sizeof_type( GLenum type )
+GLint _mesa_sizeof_type( GLenum type )
 {
    switch (type) {
       case GL_BITMAP:
@@ -147,10 +156,10 @@ GLint gl_sizeof_type( GLenum type )
 
 
 /*
- * Same as gl_sizeof_packed_type() but we also accept the
+ * Same as _mesa_sizeof_packed_type() but we also accept the
  * packed pixel format datatypes.
  */
-GLint gl_sizeof_packed_type( GLenum type )
+GLint _mesa_sizeof_packed_type( GLenum type )
 {
    switch (type) {
       case GL_BITMAP:
@@ -204,7 +213,7 @@ GLint gl_sizeof_packed_type( GLenum type )
  * Return the number of components in a GL enum pixel type.
  * Return -1 if bad format.
  */
-GLint gl_components_in_format( GLenum format )
+GLint _mesa_components_in_format( GLenum format )
 {
    switch (format) {
       case GL_COLOR_INDEX:
@@ -221,6 +230,7 @@ GLint gl_components_in_format( GLenum format )
       case GL_BLUE:
       case GL_ALPHA:
       case GL_LUMINANCE:
+      case GL_INTENSITY:
          return 1;
       case GL_LUMINANCE_ALPHA:
         return 2;
@@ -244,9 +254,9 @@ GLint gl_components_in_format( GLenum format )
  * Return bytes per pixel for given format and type
  * Return -1 if bad format or type.
  */
-GLint gl_bytes_per_pixel( GLenum format, GLenum type )
+GLint _mesa_bytes_per_pixel( GLenum format, GLenum type )
 {
-   GLint comps = gl_components_in_format( format );
+   GLint comps = _mesa_components_in_format( format );
    if (comps < 0)
       return -1;
 
@@ -302,7 +312,8 @@ GLint gl_bytes_per_pixel( GLenum format, GLenum type )
  * Test if the given pixel format and type are legal.
  * Return GL_TRUE for legal, GL_FALSE for illegal.
  */
-GLboolean gl_is_legal_format_and_type( GLenum format, GLenum type )
+GLboolean
+_mesa_is_legal_format_and_type( GLenum format, GLenum type )
 {
    switch (format) {
       case GL_COLOR_INDEX:
@@ -324,6 +335,7 @@ GLboolean gl_is_legal_format_and_type( GLenum format, GLenum type )
       case GL_GREEN:
       case GL_BLUE:
       case GL_ALPHA:
+      case GL_INTENSITY:
       case GL_LUMINANCE:
       case GL_LUMINANCE_ALPHA:
       case GL_DEPTH_COMPONENT:
@@ -400,10 +412,11 @@ GLboolean gl_is_legal_format_and_type( GLenum format, GLenum type )
  *         row, column - location of pixel in the image
  * Return:  address of pixel at (image,row,column) in image or NULL if error.
  */
-GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
-                                const GLvoid *image, GLsizei width,
-                                GLsizei height, GLenum format, GLenum type,
-                                GLint img, GLint row, GLint column )
+GLvoid *
+_mesa_image_address( const struct gl_pixelstore_attrib *packing,
+                     const GLvoid *image, GLsizei width,
+                     GLsizei height, GLenum format, GLenum type,
+                     GLint img, GLint row, GLint column )
 {
    GLint alignment;        /* 1, 2 or 4 */
    GLint pixels_per_row;
@@ -438,13 +451,13 @@ GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
       GLint bytes_per_image;
 
       /* Compute bytes per component */
-      bytes_per_comp = gl_sizeof_packed_type( type );
+      bytes_per_comp = _mesa_sizeof_packed_type( type );
       if (bytes_per_comp<0) {
          return NULL;
       }
 
       /* Compute number of components per pixel */
-      comp_per_pixel = gl_components_in_format( format );
+      comp_per_pixel = _mesa_components_in_format( format );
       if (comp_per_pixel<0 && type != GL_BITMAP) {
          return NULL;
       }
@@ -463,7 +476,7 @@ GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
       /* Non-BITMAP data */
       GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
 
-      bytes_per_pixel = gl_bytes_per_pixel( format, type );
+      bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
 
       /* The pixel type and format should have been error checked earlier */
       assert(bytes_per_pixel > 0);
@@ -511,17 +524,20 @@ _mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
    }
    else {
       /* Non-BITMAP data */
-      const GLint bytesPerPixel = gl_bytes_per_pixel(format, type);
+      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
+      GLint bytesPerRow, remainder;
       if (bytesPerPixel <= 0)
          return -1;  /* error */
       if (packing->RowLength == 0) {
-         GLint bytes = bytesPerPixel * width;
-         return bytes;
+         bytesPerRow = bytesPerPixel * width;
       }
       else {
-         GLint bytes = bytesPerPixel * packing->RowLength;
-         return bytes;
+         bytesPerRow = bytesPerPixel * packing->RowLength;
       }
+      remainder = bytesPerRow % packing->Alignment;
+      if (remainder > 0)
+         bytesPerRow += (packing->Alignment - remainder);
+      return bytesPerRow;
    }
 }
 
@@ -593,807 +609,945 @@ _mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
  *         type - dest packing datatype
  *         destination - destination packing address
  *         packing - pixel packing parameters
- *         applyTransferOps - apply scale/bias/lookup-table ops?
+ *         transferOps - bitmask of IMAGE_*_BIT operations to apply
  */
-void gl_pack_rgba_span( const GLcontext *ctx,
-                        GLuint n, CONST GLubyte rgba[][4],
-                        GLenum format, GLenum type, GLvoid *destination,
-                        const struct gl_pixelstore_attrib *packing,
-                        GLboolean applyTransferOps )
+void
+_mesa_pack_rgba_span( GLcontext *ctx,
+                      GLuint n, CONST GLubyte srcRgba[][4],
+                      GLenum dstFormat, GLenum dstType,
+                      GLvoid *dstAddr,
+                      const struct gl_pixelstore_attrib *dstPacking,
+                      GLuint transferOps)
 {
-   applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA || ctx->Pixel.MapColorFlag);
+   ASSERT(ctx->ImageTransferState != UPDATE_IMAGE_TRANSFER_STATE);
 
    /* Test for optimized case first */
-   if (!applyTransferOps && format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
+   if (transferOps == 0 && dstFormat == GL_RGBA
+       && dstType == GL_UNSIGNED_BYTE) {
       /* common simple case */
-      MEMCPY( destination, rgba, n * 4 * sizeof(GLubyte) );
+      MEMCPY(dstAddr, srcRgba, n * 4 * sizeof(GLubyte));
    }
-   else if (!applyTransferOps && format == GL_RGB && type == GL_UNSIGNED_BYTE) {
+   else if (transferOps == 0 && dstFormat == GL_RGB
+            && dstType == GL_UNSIGNED_BYTE) {
       /* common simple case */
       GLint i;
-      GLubyte *dest = (GLubyte *) destination;
+      GLubyte *dest = (GLubyte *) dstAddr;
       for (i = 0; i < n; i++) {
-         dest[0] = rgba[i][RCOMP];
-         dest[1] = rgba[i][GCOMP];
-         dest[2] = rgba[i][BCOMP];
+         dest[0] = srcRgba[i][RCOMP];
+         dest[1] = srcRgba[i][GCOMP];
+         dest[2] = srcRgba[i][BCOMP];
          dest += 3;
       }
    }
    else {
       /* general solution */
-      GLfloat red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH];
-      GLfloat alpha[MAX_WIDTH], luminance[MAX_WIDTH];
+      GLfloat rgba[MAX_WIDTH][4];
       const GLfloat rscale = 1.0F / 255.0F;
       const GLfloat gscale = 1.0F / 255.0F;
       const GLfloat bscale = 1.0F / 255.0F;
       const GLfloat ascale = 1.0F / 255.0F;
-      const GLint comps = gl_components_in_format(format);
       GLuint i;
-
       assert(n <= MAX_WIDTH);
-
       /* convert color components to floating point */
       for (i=0;i<n;i++) {
-         red[i]   = rgba[i][RCOMP] * rscale;
-         green[i] = rgba[i][GCOMP] * gscale;
-         blue[i]  = rgba[i][BCOMP] * bscale;
-         alpha[i] = rgba[i][ACOMP] * ascale;
+         rgba[i][RCOMP] = srcRgba[i][RCOMP] * rscale;
+         rgba[i][GCOMP] = srcRgba[i][GCOMP] * gscale;
+         rgba[i][BCOMP] = srcRgba[i][BCOMP] * bscale;
+         rgba[i][ACOMP] = srcRgba[i][ACOMP] * ascale;
       }
+      _mesa_pack_float_rgba_span(ctx, n, (const GLfloat (*)[4]) rgba,
+                                 dstFormat, dstType, dstAddr,
+                                 dstPacking, transferOps);
+   }
+}
 
-      /*
-       * Apply scale, bias and lookup-tables if enabled.
-       */
-      if (applyTransferOps) {
-         if (ctx->Pixel.ScaleOrBiasRGBA) {
-            gl_scale_and_bias_color( ctx, n, red, green, blue, alpha );
-         }
-         if (ctx->Pixel.MapColorFlag) {
-            gl_map_color( ctx, n, red, green, blue, alpha );
-         }
+
+void
+_mesa_pack_float_rgba_span( GLcontext *ctx,
+                            GLuint n, CONST GLfloat rgbaIn[][4],
+                            GLenum dstFormat, GLenum dstType,
+                            GLvoid *dstAddr,
+                            const struct gl_pixelstore_attrib *dstPacking,
+                            GLuint transferOps )
+{
+   const GLint comps = _mesa_components_in_format(dstFormat);
+   GLfloat luminance[MAX_WIDTH];
+   GLfloat (*rgba)[4];
+   GLuint i;
+
+   if (transferOps) {
+      /* make copy of incoming data */
+      GLfloat rgbaCopy[MAX_WIDTH][4];
+      for (i = 0; i < n; i++) {
+         rgbaCopy[i][0] = rgbaIn[i][0];
+         rgbaCopy[i][1] = rgbaIn[i][1];
+         rgbaCopy[i][2] = rgbaIn[i][2];
+         rgbaCopy[i][3] = rgbaIn[i][3];
       }
 
-      if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
-         for (i=0;i<n;i++) {
-            GLfloat sum = red[i] + green[i] + blue[i];
-            luminance[i] = CLAMP( sum, 0.0F, 1.0F );
-         }
+      rgba = (GLfloat (*)[4]) rgbaCopy;
+
+      /* scale & bias */
+      if (transferOps & IMAGE_SCALE_BIAS_BIT) {
+         _mesa_scale_and_bias_rgba( ctx, n, rgba );
+      }
+      /* color map lookup */
+      if (transferOps & IMAGE_MAP_COLOR_BIT) {
+         _mesa_map_rgba( ctx, n, rgba );
+      }
+      /* GL_COLOR_TABLE lookup */
+      if (transferOps & IMAGE_COLOR_TABLE_BIT) {
+         _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
+      }
+      /* convolution */
+      if (transferOps & IMAGE_CONVOLUTION_BIT) {
+         /* this has to be done in the calling code */
+      }
+      /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
+      if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
+         _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
+      }
+      /* color matrix transform */
+      if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
+         _mesa_transform_rgba(ctx, n, rgba);
       }
+      /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
+      if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
+         _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
+      }
+      /* update histogram count */
+      if (transferOps & IMAGE_HISTOGRAM_BIT) {
+         _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
+      }
+      /* min/max here */
+      if (transferOps & IMAGE_MIN_MAX_BIT) {
+         _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
+         if (ctx->MinMax.Sink)
+            return;
+      }
+   }
+   else {
+      /* use incoming data, not a copy */
+      rgba = (GLfloat (*)[4]) rgbaIn;
+   }
 
-      /*
-       * Pack/store the pixels.  Ugh!  Lots of cases!!!
-       */
-      switch (type) {
-         case GL_UNSIGNED_BYTE:
-            {
-               GLubyte *dst = (GLubyte *) destination;
-               switch (format) {
-                  case GL_RED:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UBYTE(red[i]);
-                     break;
-                  case GL_GREEN:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UBYTE(green[i]);
-                     break;
-                  case GL_BLUE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UBYTE(blue[i]);
-                     break;
-                  case GL_ALPHA:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UBYTE(alpha[i]);
-                     break;
-                  case GL_LUMINANCE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UBYTE(luminance[i]);
-                     break;
-                  case GL_LUMINANCE_ALPHA:
-                     for (i=0;i<n;i++) {
-                        dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
-                        dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]);
-                     }
-                     break;
-                  case GL_RGB:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_UBYTE(red[i]);
-                        dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
-                        dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]);
-                     }
-                     break;
-                  case GL_RGBA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_UBYTE(red[i]);
-                        dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
-                        dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]);
-                        dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
-                     }
-                     break;
-                  case GL_BGR:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]);
-                        dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
-                        dst[i*3+2] = FLOAT_TO_UBYTE(red[i]);
-                     }
-                     break;
-                  case GL_BGRA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]);
-                        dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
-                        dst[i*4+2] = FLOAT_TO_UBYTE(red[i]);
-                        dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
-                     }
-                     break;
-                  case GL_ABGR_EXT:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]);
-                        dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]);
-                        dst[i*4+2] = FLOAT_TO_UBYTE(green[i]);
-                        dst[i*4+3] = FLOAT_TO_UBYTE(red[i]);
-                     }
-                     break;
-                  default:
-                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
-               }
-           }
-           break;
-        case GL_BYTE:
-            {
-               GLbyte *dst = (GLbyte *) destination;
-               switch (format) {
-                  case GL_RED:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_BYTE(red[i]);
-                     break;
-                  case GL_GREEN:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_BYTE(green[i]);
-                     break;
-                  case GL_BLUE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_BYTE(blue[i]);
-                     break;
-                  case GL_ALPHA:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_BYTE(alpha[i]);
-                     break;
-                  case GL_LUMINANCE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_BYTE(luminance[i]);
-                     break;
-                  case GL_LUMINANCE_ALPHA:
-                     for (i=0;i<n;i++) {
-                        dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
-                        dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]);
-                     }
-                     break;
-                  case GL_RGB:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_BYTE(red[i]);
-                        dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
-                        dst[i*3+2] = FLOAT_TO_BYTE(blue[i]);
-                     }
-                     break;
-                  case GL_RGBA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_BYTE(red[i]);
-                        dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
-                        dst[i*4+2] = FLOAT_TO_BYTE(blue[i]);
-                        dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
-                     }
-                     break;
-                  case GL_BGR:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_BYTE(blue[i]);
-                        dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
-                        dst[i*3+2] = FLOAT_TO_BYTE(red[i]);
-                     }
-                     break;
-                  case GL_BGRA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_BYTE(blue[i]);
-                        dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
-                        dst[i*4+2] = FLOAT_TO_BYTE(red[i]);
-                        dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
-                     }
-                  case GL_ABGR_EXT:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]);
-                        dst[i*4+1] = FLOAT_TO_BYTE(blue[i]);
-                        dst[i*4+2] = FLOAT_TO_BYTE(green[i]);
-                        dst[i*4+3] = FLOAT_TO_BYTE(red[i]);
-                     }
-                     break;
-                  default:
-                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
-               }
+   /* XXX clamp rgba to [0,1]? */
+
+
+   if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
+      for (i = 0; i < n; i++) {
+         GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
+         luminance[i] = CLAMP(sum, 0.0F, 1.0F);
+      }
+   }
+
+   /*
+    * Pack/store the pixels.  Ugh!  Lots of cases!!!
+    */
+   switch (dstType) {
+      case GL_UNSIGNED_BYTE:
+         {
+            GLubyte *dst = (GLubyte *) dstAddr;
+            switch (dstFormat) {
+               case GL_RED:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
+                  break;
+               case GL_GREEN:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
+                  break;
+               case GL_BLUE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
+                  break;
+               case GL_ALPHA:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
+                  break;
+               case GL_LUMINANCE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
+                  break;
+               case GL_LUMINANCE_ALPHA:
+                  for (i=0;i<n;i++) {
+                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
+                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_RGB:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
+                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
+                  }
+                  break;
+               case GL_RGBA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
+                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
+                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_BGR:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
+                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
+                  }
+                  break;
+               case GL_BGRA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
+                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
+                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_ABGR_EXT:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
+                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
+                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
+                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
+                  }
+                  break;
+               default:
+                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
             }
-           break;
-        case GL_UNSIGNED_SHORT:
-            {
-               GLushort *dst = (GLushort *) destination;
-               switch (format) {
-                  case GL_RED:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_USHORT(red[i]);
-                     break;
-                  case GL_GREEN:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_USHORT(green[i]);
-                     break;
-                  case GL_BLUE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_USHORT(blue[i]);
-                     break;
-                  case GL_ALPHA:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_USHORT(alpha[i]);
-                     break;
-                  case GL_LUMINANCE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_USHORT(luminance[i]);
-                     break;
-                  case GL_LUMINANCE_ALPHA:
-                     for (i=0;i<n;i++) {
-                        dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
-                        dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]);
-                     }
-                     break;
-                  case GL_RGB:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_USHORT(red[i]);
-                        dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
-                        dst[i*3+2] = FLOAT_TO_USHORT(blue[i]);
-                     }
-                     break;
-                  case GL_RGBA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_USHORT(red[i]);
-                        dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
-                        dst[i*4+2] = FLOAT_TO_USHORT(blue[i]);
-                        dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
-                     }
-                     break;
-                  case GL_BGR:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_USHORT(blue[i]);
-                        dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
-                        dst[i*3+2] = FLOAT_TO_USHORT(red[i]);
-                     }
-                     break;
-                  case GL_BGRA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_USHORT(blue[i]);
-                        dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
-                        dst[i*4+2] = FLOAT_TO_USHORT(red[i]);
-                        dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
-                     }
-                     break;
-                  case GL_ABGR_EXT:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]);
-                        dst[i*4+1] = FLOAT_TO_USHORT(blue[i]);
-                        dst[i*4+2] = FLOAT_TO_USHORT(green[i]);
-                        dst[i*4+3] = FLOAT_TO_USHORT(red[i]);
-                     }
-                     break;
-                  default:
-                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
-               }
-               if (packing->SwapBytes) {
-                  gl_swap2( (GLushort *) dst, n * comps);
-               }
+         }
+         break;
+      case GL_BYTE:
+         {
+            GLbyte *dst = (GLbyte *) dstAddr;
+            switch (dstFormat) {
+               case GL_RED:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
+                  break;
+               case GL_GREEN:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
+                  break;
+               case GL_BLUE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
+                  break;
+               case GL_ALPHA:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
+                  break;
+               case GL_LUMINANCE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_BYTE(luminance[i]);
+                  break;
+               case GL_LUMINANCE_ALPHA:
+                  for (i=0;i<n;i++) {
+                     dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
+                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_RGB:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
+                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
+                  }
+                  break;
+               case GL_RGBA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
+                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
+                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_BGR:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
+                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
+                  }
+                  break;
+               case GL_BGRA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
+                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
+                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
+                  }
+               case GL_ABGR_EXT:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
+                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
+                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
+                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
+                  }
+                  break;
+               default:
+                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
             }
-           break;
-        case GL_SHORT:
-            {
-               GLshort *dst = (GLshort *) destination;
-               switch (format) {
-                  case GL_RED:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_SHORT(red[i]);
-                     break;
-                  case GL_GREEN:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_SHORT(green[i]);
-                     break;
-                  case GL_BLUE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_SHORT(blue[i]);
-                     break;
-                  case GL_ALPHA:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_SHORT(alpha[i]);
-                     break;
-                  case GL_LUMINANCE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_SHORT(luminance[i]);
-                     break;
-                  case GL_LUMINANCE_ALPHA:
-                     for (i=0;i<n;i++) {
-                        dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
-                        dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]);
-                     }
-                     break;
-                  case GL_RGB:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_SHORT(red[i]);
-                        dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
-                        dst[i*3+2] = FLOAT_TO_SHORT(blue[i]);
-                     }
-                     break;
-                  case GL_RGBA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_SHORT(red[i]);
-                        dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
-                        dst[i*4+2] = FLOAT_TO_SHORT(blue[i]);
-                        dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
-                     }
-                     break;
-                  case GL_BGR:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_SHORT(blue[i]);
-                        dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
-                        dst[i*3+2] = FLOAT_TO_SHORT(red[i]);
-                     }
-                     break;
-                  case GL_BGRA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_SHORT(blue[i]);
-                        dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
-                        dst[i*4+2] = FLOAT_TO_SHORT(red[i]);
-                        dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
-                     }
-                  case GL_ABGR_EXT:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]);
-                        dst[i*4+1] = FLOAT_TO_SHORT(blue[i]);
-                        dst[i*4+2] = FLOAT_TO_SHORT(green[i]);
-                        dst[i*4+3] = FLOAT_TO_SHORT(red[i]);
-                     }
-                     break;
-                  default:
-                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
-               }
-               if (packing->SwapBytes) {
-                  gl_swap2( (GLushort *) dst, n * comps );
-               }
+         }
+         break;
+      case GL_UNSIGNED_SHORT:
+         {
+            GLushort *dst = (GLushort *) dstAddr;
+            switch (dstFormat) {
+               case GL_RED:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
+                  break;
+               case GL_GREEN:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
+                  break;
+               case GL_BLUE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
+                  break;
+               case GL_ALPHA:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
+                  break;
+               case GL_LUMINANCE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_USHORT(luminance[i]);
+                  break;
+               case GL_LUMINANCE_ALPHA:
+                  for (i=0;i<n;i++) {
+                     dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
+                     dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_RGB:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
+                     dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
+                  }
+                  break;
+               case GL_RGBA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
+                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
+                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_BGR:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
+                     dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
+                  }
+                  break;
+               case GL_BGRA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
+                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
+                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_ABGR_EXT:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
+                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
+                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
+                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
+                  }
+                  break;
+               default:
+                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
             }
-           break;
-        case GL_UNSIGNED_INT:
-            {
-               GLuint *dst = (GLuint *) destination;
-               switch (format) {
-                  case GL_RED:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UINT(red[i]);
-                     break;
-                  case GL_GREEN:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UINT(green[i]);
-                     break;
-                  case GL_BLUE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UINT(blue[i]);
-                     break;
-                  case GL_ALPHA:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UINT(alpha[i]);
-                     break;
-                  case GL_LUMINANCE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_UINT(luminance[i]);
-                     break;
-                  case GL_LUMINANCE_ALPHA:
-                     for (i=0;i<n;i++) {
-                        dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
-                        dst[i*2+1] = FLOAT_TO_UINT(alpha[i]);
-                     }
-                     break;
-                  case GL_RGB:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_UINT(red[i]);
-                        dst[i*3+1] = FLOAT_TO_UINT(green[i]);
-                        dst[i*3+2] = FLOAT_TO_UINT(blue[i]);
-                     }
-                     break;
-                  case GL_RGBA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_UINT(red[i]);
-                        dst[i*4+1] = FLOAT_TO_UINT(green[i]);
-                        dst[i*4+2] = FLOAT_TO_UINT(blue[i]);
-                        dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
-                     }
-                     break;
-                  case GL_BGR:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_UINT(blue[i]);
-                        dst[i*3+1] = FLOAT_TO_UINT(green[i]);
-                        dst[i*3+2] = FLOAT_TO_UINT(red[i]);
-                     }
-                     break;
-                  case GL_BGRA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_UINT(blue[i]);
-                        dst[i*4+1] = FLOAT_TO_UINT(green[i]);
-                        dst[i*4+2] = FLOAT_TO_UINT(red[i]);
-                        dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
-                     }
-                     break;
-                  case GL_ABGR_EXT:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_UINT(alpha[i]);
-                        dst[i*4+1] = FLOAT_TO_UINT(blue[i]);
-                        dst[i*4+2] = FLOAT_TO_UINT(green[i]);
-                        dst[i*4+3] = FLOAT_TO_UINT(red[i]);
-                     }
-                     break;
-                  default:
-                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
-               }
-               if (packing->SwapBytes) {
-                  gl_swap4( (GLuint *) dst, n * comps );
-               }
+            if (dstPacking->SwapBytes) {
+               _mesa_swap2( (GLushort *) dst, n * comps);
             }
-           break;
-        case GL_INT:
-           {
-               GLint *dst = (GLint *) destination;
-               switch (format) {
-                  case GL_RED:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_INT(red[i]);
-                     break;
-                  case GL_GREEN:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_INT(green[i]);
-                     break;
-                  case GL_BLUE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_INT(blue[i]);
-                     break;
-                  case GL_ALPHA:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_INT(alpha[i]);
-                     break;
-                  case GL_LUMINANCE:
-                     for (i=0;i<n;i++)
-                        dst[i] = FLOAT_TO_INT(luminance[i]);
-                     break;
-                  case GL_LUMINANCE_ALPHA:
-                     for (i=0;i<n;i++) {
-                        dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
-                        dst[i*2+1] = FLOAT_TO_INT(alpha[i]);
-                     }
-                     break;
-                  case GL_RGB:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_INT(red[i]);
-                        dst[i*3+1] = FLOAT_TO_INT(green[i]);
-                        dst[i*3+2] = FLOAT_TO_INT(blue[i]);
-                     }
-                     break;
-                  case GL_RGBA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_INT(red[i]);
-                        dst[i*4+1] = FLOAT_TO_INT(green[i]);
-                        dst[i*4+2] = FLOAT_TO_INT(blue[i]);
-                        dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
-                     }
-                     break;
-                  case GL_BGR:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = FLOAT_TO_INT(blue[i]);
-                        dst[i*3+1] = FLOAT_TO_INT(green[i]);
-                        dst[i*3+2] = FLOAT_TO_INT(red[i]);
-                     }
-                     break;
-                  case GL_BGRA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_INT(blue[i]);
-                        dst[i*4+1] = FLOAT_TO_INT(green[i]);
-                        dst[i*4+2] = FLOAT_TO_INT(red[i]);
-                        dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
-                     }
-                     break;
-                  case GL_ABGR_EXT:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = FLOAT_TO_INT(alpha[i]);
-                        dst[i*4+1] = FLOAT_TO_INT(blue[i]);
-                        dst[i*4+2] = FLOAT_TO_INT(green[i]);
-                        dst[i*4+3] = FLOAT_TO_INT(red[i]);
-                     }
-                     break;
-                  default:
-                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
-               }
-              if (packing->SwapBytes) {
-                 gl_swap4( (GLuint *) dst, n * comps );
-              }
-           }
-           break;
-        case GL_FLOAT:
-           {
-               GLfloat *dst = (GLfloat *) destination;
-               switch (format) {
-                  case GL_RED:
-                     for (i=0;i<n;i++)
-                        dst[i] = red[i];
-                     break;
-                  case GL_GREEN:
-                     for (i=0;i<n;i++)
-                        dst[i] = green[i];
-                     break;
-                  case GL_BLUE:
-                     for (i=0;i<n;i++)
-                        dst[i] = blue[i];
-                     break;
-                  case GL_ALPHA:
-                     for (i=0;i<n;i++)
-                        dst[i] = alpha[i];
-                     break;
-                  case GL_LUMINANCE:
-                     for (i=0;i<n;i++)
-                        dst[i] = luminance[i];
-                     break;
-                  case GL_LUMINANCE_ALPHA:
-                     for (i=0;i<n;i++) {
-                        dst[i*2+0] = luminance[i];
-                        dst[i*2+1] = alpha[i];
-                     }
-                     break;
-                  case GL_RGB:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = red[i];
-                        dst[i*3+1] = green[i];
-                        dst[i*3+2] = blue[i];
-                     }
-                     break;
-                  case GL_RGBA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = red[i];
-                        dst[i*4+1] = green[i];
-                        dst[i*4+2] = blue[i];
-                        dst[i*4+3] = alpha[i];
-                     }
-                     break;
-                  case GL_BGR:
-                     for (i=0;i<n;i++) {
-                        dst[i*3+0] = blue[i];
-                        dst[i*3+1] = green[i];
-                        dst[i*3+2] = red[i];
-                     }
-                     break;
-                  case GL_BGRA:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = blue[i];
-                        dst[i*4+1] = green[i];
-                        dst[i*4+2] = red[i];
-                        dst[i*4+3] = alpha[i];
-                     }
-                     break;
-                  case GL_ABGR_EXT:
-                     for (i=0;i<n;i++) {
-                        dst[i*4+0] = alpha[i];
-                        dst[i*4+1] = blue[i];
-                        dst[i*4+2] = green[i];
-                        dst[i*4+3] = red[i];
-                     }
-                     break;
-                  default:
-                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
-               }
-              if (packing->SwapBytes) {
-                 gl_swap4( (GLuint *) dst, n * comps );
-              }
-           }
-           break;
-         case GL_UNSIGNED_BYTE_3_3_2:
-            if (format == GL_RGB) {
-               GLubyte *dst = (GLubyte *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLint) (red[i]   * 7.0F)) << 5)
-                         | (((GLint) (green[i] * 7.0F)) << 2)
-                         | (((GLint) (blue[i]  * 3.0F))     );
-               }
+         }
+         break;
+      case GL_SHORT:
+         {
+            GLshort *dst = (GLshort *) dstAddr;
+            switch (dstFormat) {
+               case GL_RED:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
+                  break;
+               case GL_GREEN:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
+                  break;
+               case GL_BLUE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
+                  break;
+               case GL_ALPHA:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
+                  break;
+               case GL_LUMINANCE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_SHORT(luminance[i]);
+                  break;
+               case GL_LUMINANCE_ALPHA:
+                  for (i=0;i<n;i++) {
+                     dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
+                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_RGB:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
+                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
+                  }
+                  break;
+               case GL_RGBA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
+                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
+                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_BGR:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
+                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
+                  }
+                  break;
+               case GL_BGRA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
+                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
+                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
+                  }
+               case GL_ABGR_EXT:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
+                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
+                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
+                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
+                  }
+                  break;
+               default:
+                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
             }
-            break;
-         case GL_UNSIGNED_BYTE_2_3_3_REV:
-            if (format == GL_RGB) {
-               GLubyte *dst = (GLubyte *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLint) (red[i]   * 7.0F))     )
-                         | (((GLint) (green[i] * 7.0F)) << 3)
-                         | (((GLint) (blue[i]  * 3.0F)) << 5);
-               }
+            if (dstPacking->SwapBytes) {
+               _mesa_swap2( (GLushort *) dst, n * comps );
             }
-            break;
-         case GL_UNSIGNED_SHORT_5_6_5:
-            if (format == GL_RGB) {
-               GLushort *dst = (GLushort *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
-                         | (((GLint) (green[i] * 63.0F)) <<  5)
-                         | (((GLint) (blue[i]  * 31.0F))      );
-               }
+         }
+         break;
+      case GL_UNSIGNED_INT:
+         {
+            GLuint *dst = (GLuint *) dstAddr;
+            switch (dstFormat) {
+               case GL_RED:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
+                  break;
+               case GL_GREEN:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
+                  break;
+               case GL_BLUE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
+                  break;
+               case GL_ALPHA:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
+                  break;
+               case GL_LUMINANCE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_UINT(luminance[i]);
+                  break;
+               case GL_LUMINANCE_ALPHA:
+                  for (i=0;i<n;i++) {
+                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
+                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_RGB:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
+                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
+                  }
+                  break;
+               case GL_RGBA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
+                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
+                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_BGR:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
+                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
+                  }
+                  break;
+               case GL_BGRA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
+                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
+                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_ABGR_EXT:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
+                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
+                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
+                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
+                  }
+                  break;
+               default:
+                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
             }
-            break;
-         case GL_UNSIGNED_SHORT_5_6_5_REV:
-            if (format == GL_RGB) {
-               GLushort *dst = (GLushort *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLint) (red[i]   * 31.0F))      )
-                         | (((GLint) (green[i] * 63.0F)) <<  5)
-                         | (((GLint) (blue[i]  * 31.0F)) << 11);
-               }
+            if (dstPacking->SwapBytes) {
+               _mesa_swap4( (GLuint *) dst, n * comps );
             }
-            break;
-         case GL_UNSIGNED_SHORT_4_4_4_4:
-            if (format == GL_RGB) {
-               GLushort *dst = (GLushort *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLint) (red[i]   * 15.0F)) << 12)
-                         | (((GLint) (green[i] * 15.0F)) <<  8)
-                         | (((GLint) (blue[i]  * 15.0F)) <<  4)
-                         | (((GLint) (alpha[i] * 15.0F))      );
-               }
+         }
+         break;
+      case GL_INT:
+         {
+            GLint *dst = (GLint *) dstAddr;
+            switch (dstFormat) {
+               case GL_RED:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
+                  break;
+               case GL_GREEN:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
+                  break;
+               case GL_BLUE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
+                  break;
+               case GL_ALPHA:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
+                  break;
+               case GL_LUMINANCE:
+                  for (i=0;i<n;i++)
+                     dst[i] = FLOAT_TO_INT(luminance[i]);
+                  break;
+               case GL_LUMINANCE_ALPHA:
+                  for (i=0;i<n;i++) {
+                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
+                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_RGB:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
+                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
+                  }
+                  break;
+               case GL_RGBA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
+                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
+                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_BGR:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
+                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
+                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
+                  }
+                  break;
+               case GL_BGRA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
+                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
+                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
+                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
+                  }
+                  break;
+               case GL_ABGR_EXT:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
+                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
+                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
+                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
+                  }
+                  break;
+               default:
+                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
             }
-            break;
-         case GL_UNSIGNED_SHORT_4_4_4_4_REV:
-            if (format == GL_RGB) {
-               GLushort *dst = (GLushort *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLint) (red[i]   * 15.0F))      )
-                         | (((GLint) (green[i] * 15.0F)) <<  4)
-                         | (((GLint) (blue[i]  * 15.0F)) <<  8)
-                         | (((GLint) (alpha[i] * 15.0F)) << 12);
-               }
+            if (dstPacking->SwapBytes) {
+               _mesa_swap4( (GLuint *) dst, n * comps );
             }
-            break;
-         case GL_UNSIGNED_SHORT_5_5_5_1:
-            if (format == GL_RGB) {
-               GLushort *dst = (GLushort *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
-                         | (((GLint) (green[i] * 31.0F)) <<  6)
-                         | (((GLint) (blue[i]  * 31.0F)) <<  1)
-                         | (((GLint) (alpha[i] *  1.0F))      );
-               }
+         }
+         break;
+      case GL_FLOAT:
+         {
+            GLfloat *dst = (GLfloat *) dstAddr;
+            switch (dstFormat) {
+               case GL_RED:
+                  for (i=0;i<n;i++)
+                     dst[i] = rgba[i][RCOMP];
+                  break;
+               case GL_GREEN:
+                  for (i=0;i<n;i++)
+                     dst[i] = rgba[i][GCOMP];
+                  break;
+               case GL_BLUE:
+                  for (i=0;i<n;i++)
+                     dst[i] = rgba[i][BCOMP];
+                  break;
+               case GL_ALPHA:
+                  for (i=0;i<n;i++)
+                     dst[i] = rgba[i][ACOMP];
+                  break;
+               case GL_LUMINANCE:
+                  for (i=0;i<n;i++)
+                     dst[i] = luminance[i];
+                  break;
+               case GL_LUMINANCE_ALPHA:
+                  for (i=0;i<n;i++) {
+                     dst[i*2+0] = luminance[i];
+                     dst[i*2+1] = rgba[i][ACOMP];
+                  }
+                  break;
+               case GL_RGB:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = rgba[i][RCOMP];
+                     dst[i*3+1] = rgba[i][GCOMP];
+                     dst[i*3+2] = rgba[i][BCOMP];
+                  }
+                  break;
+               case GL_RGBA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = rgba[i][RCOMP];
+                     dst[i*4+1] = rgba[i][GCOMP];
+                     dst[i*4+2] = rgba[i][BCOMP];
+                     dst[i*4+3] = rgba[i][ACOMP];
+                  }
+                  break;
+               case GL_BGR:
+                  for (i=0;i<n;i++) {
+                     dst[i*3+0] = rgba[i][BCOMP];
+                     dst[i*3+1] = rgba[i][GCOMP];
+                     dst[i*3+2] = rgba[i][RCOMP];
+                  }
+                  break;
+               case GL_BGRA:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = rgba[i][BCOMP];
+                     dst[i*4+1] = rgba[i][GCOMP];
+                     dst[i*4+2] = rgba[i][RCOMP];
+                     dst[i*4+3] = rgba[i][ACOMP];
+                  }
+                  break;
+               case GL_ABGR_EXT:
+                  for (i=0;i<n;i++) {
+                     dst[i*4+0] = rgba[i][ACOMP];
+                     dst[i*4+1] = rgba[i][BCOMP];
+                     dst[i*4+2] = rgba[i][GCOMP];
+                     dst[i*4+3] = rgba[i][RCOMP];
+                  }
+                  break;
+               default:
+                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
             }
-            break;
-         case GL_UNSIGNED_SHORT_1_5_5_5_REV:
-            if (format == GL_RGB) {
-               GLushort *dst = (GLushort *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLint) (red[i]   * 31.0F))      )
-                         | (((GLint) (green[i] * 31.0F)) <<  5)
-                         | (((GLint) (blue[i]  * 31.0F)) << 10)
-                         | (((GLint) (alpha[i] *  1.0F)) << 15);
-               }
+            if (dstPacking->SwapBytes) {
+               _mesa_swap4( (GLuint *) dst, n * comps );
             }
-            break;
-         case GL_UNSIGNED_INT_8_8_8_8:
-            if (format == GL_RGBA) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (red[i]   * 255.0F)) << 24)
-                         | (((GLuint) (green[i] * 255.0F)) << 16)
-                         | (((GLuint) (blue[i]  * 255.0F)) <<  8)
-                         | (((GLuint) (alpha[i] * 255.0F))      );
-               }
+         }
+         break;
+      case GL_UNSIGNED_BYTE_3_3_2:
+         if (dstFormat == GL_RGB) {
+            GLubyte *dst = (GLubyte *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F)) << 5)
+                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 2)
+                      | (((GLint) (rgba[i][BCOMP] * 3.0F))     );
             }
-            else if (format == GL_BGRA) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (blue[i]  * 255.0F)) << 24)
-                         | (((GLuint) (green[i] * 255.0F)) << 16)
-                         | (((GLuint) (red[i]   * 255.0F)) <<  8)
-                         | (((GLuint) (alpha[i] * 255.0F))      );
-               }
+         }
+         break;
+      case GL_UNSIGNED_BYTE_2_3_3_REV:
+         if (dstFormat == GL_RGB) {
+            GLubyte *dst = (GLubyte *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F))     )
+                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 3)
+                      | (((GLint) (rgba[i][BCOMP] * 3.0F)) << 5);
             }
-            else if (format == GL_ABGR_EXT) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (alpha[i] * 255.0F)) << 24)
-                         | (((GLuint) (blue[i]  * 255.0F)) << 16)
-                         | (((GLuint) (green[i] * 255.0F)) <<  8)
-                         | (((GLuint) (red[i]   * 255.0F))      );
-               }
+         }
+         break;
+      case GL_UNSIGNED_SHORT_5_6_5:
+         if (dstFormat == GL_RGB) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
+                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
+                      | (((GLint) (rgba[i][BCOMP] * 31.0F))      );
             }
-            break;
-         case GL_UNSIGNED_INT_8_8_8_8_REV:
-            if (format == GL_RGBA) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (red[i]   * 255.0F))      )
-                         | (((GLuint) (green[i] * 255.0F)) <<  8)
-                         | (((GLuint) (blue[i]  * 255.0F)) << 16)
-                         | (((GLuint) (alpha[i] * 255.0F)) << 24);
-               }
+         }
+         break;
+      case GL_UNSIGNED_SHORT_5_6_5_REV:
+         if (dstFormat == GL_RGB) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
+                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
+                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11);
             }
-            else if (format == GL_BGRA) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (blue[i]  * 255.0F))      )
-                         | (((GLuint) (green[i] * 255.0F)) <<  8)
-                         | (((GLuint) (red[i]   * 255.0F)) << 16)
-                         | (((GLuint) (alpha[i] * 255.0F)) << 24);
-               }
+         }
+         break;
+      case GL_UNSIGNED_SHORT_4_4_4_4:
+         if (dstFormat == GL_RGBA) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12)
+                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
+                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
+                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
             }
-            else if (format == GL_ABGR_EXT) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (alpha[i] * 255.0F))      )
-                         | (((GLuint) (blue[i]  * 255.0F)) <<  8)
-                         | (((GLuint) (green[i] * 255.0F)) << 16)
-                         | (((GLuint) (red[i]   * 255.0F)) << 24);
-               }
+         }
+         else if (dstFormat == GL_BGRA) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F)) << 12)
+                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
+                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  4)
+                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
             }
-            break;
-         case GL_UNSIGNED_INT_10_10_10_2:
-            if (format == GL_RGBA) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (red[i]   * 1023.0F)) << 22)
-                         | (((GLuint) (green[i] * 1023.0F)) << 12)
-                         | (((GLuint) (blue[i]  * 1023.0F)) <<  2)
-                         | (((GLuint) (alpha[i] *    3.0F))      );
-               }
+         }
+         else if (dstFormat == GL_ABGR_EXT) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F)) <<  4)
+                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
+                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) << 12)
+                      | (((GLint) (rgba[i][RCOMP] * 15.0F))      );
             }
-            else if (format == GL_BGRA) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (blue[i]  * 1023.0F)) << 22)
-                         | (((GLuint) (green[i] * 1023.0F)) << 12)
-                         | (((GLuint) (red[i]   * 1023.0F)) <<  2)
-                         | (((GLuint) (alpha[i] *    3.0F))      );
-               }
+         }
+         break;
+      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
+         if (dstFormat == GL_RGBA) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F))      )
+                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
+                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
+                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
             }
-            else if (format == GL_ABGR_EXT) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (alpha[i] * 1023.0F)) << 22)
-                         | (((GLuint) (blue[i]  * 1023.0F)) << 12)
-                         | (((GLuint) (green[i] * 1023.0F)) <<  2)
-                         | (((GLuint) (red[i]   *    3.0F))      );
-               }
+         }
+         else if (dstFormat == GL_BGRA) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F))      )
+                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
+                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  8)
+                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
             }
-            break;
-         case GL_UNSIGNED_INT_2_10_10_10_REV:
-            if (format == GL_RGBA) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (red[i]   * 1023.0F))      )
-                         | (((GLuint) (green[i] * 1023.0F)) << 10)
-                         | (((GLuint) (blue[i]  * 1023.0F)) << 20)
-                         | (((GLuint) (alpha[i] *    3.0F)) << 30);
-               }
+         }
+         else if (dstFormat == GL_ABGR_EXT) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F))      )
+                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
+                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
+                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12);
             }
-            else if (format == GL_BGRA) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (blue[i]  * 1023.0F))      )
-                         | (((GLuint) (green[i] * 1023.0F)) << 10)
-                         | (((GLuint) (red[i]   * 1023.0F)) << 20)
-                         | (((GLuint) (alpha[i] *    3.0F)) << 30);
-               }
+         }
+         break;
+      case GL_UNSIGNED_SHORT_5_5_5_1:
+         if (dstFormat == GL_RGBA) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
+                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
+                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  1)
+                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
             }
-            else if (format == GL_ABGR_EXT) {
-               GLuint *dst = (GLuint *) destination;
-               for (i=0;i<n;i++) {
-                  dst[i] = (((GLuint) (alpha[i] * 1023.0F))      )
-                         | (((GLuint) (blue[i]  * 1023.0F)) << 10)
-                         | (((GLuint) (green[i] * 1023.0F)) << 20)
-                         | (((GLuint) (red[i]   *    3.0F)) << 30);
-               }
+         }
+         else if (dstFormat == GL_BGRA) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11)
+                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
+                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) <<  1)
+                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
             }
-            break;
-         default:
-            gl_problem( ctx, "bad type in gl_pack_rgba_span" );
-      }
+         }
+         else if (dstFormat == GL_ABGR_EXT) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F)) << 11)
+                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  6)
+                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  1)
+                      | (((GLint) (rgba[i][RCOMP] *  1.0F))      );
+            }
+         }
+         break;
+      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
+         if (dstFormat == GL_RGBA) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
+                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
+                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 10)
+                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
+            }
+         }
+         else if (dstFormat == GL_BGRA) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F))      )
+                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
+                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) << 10)
+                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
+            }
+         }
+         else if (dstFormat == GL_ABGR_EXT) {
+            GLushort *dst = (GLushort *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F))      )
+                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  5)
+                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) << 10)
+                      | (((GLint) (rgba[i][RCOMP] *  1.0F)) << 15);
+            }
+         }
+         break;
+      case GL_UNSIGNED_INT_8_8_8_8:
+         if (dstFormat == GL_RGBA) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24)
+                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
+                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
+                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
+            }
+         }
+         else if (dstFormat == GL_BGRA) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 24)
+                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
+                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) <<  8)
+                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
+            }
+         }
+         else if (dstFormat == GL_ABGR_EXT) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24)
+                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
+                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
+                      | (((GLuint) (rgba[i][RCOMP] * 255.0F))      );
+            }
+         }
+         break;
+      case GL_UNSIGNED_INT_8_8_8_8_REV:
+         if (dstFormat == GL_RGBA) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F))      )
+                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
+                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
+                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
+            }
+         }
+         else if (dstFormat == GL_BGRA) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F))      )
+                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
+                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 16)
+                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
+            }
+         }
+         else if (dstFormat == GL_ABGR_EXT) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F))      )
+                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
+                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
+                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24);
+            }
+         }
+         break;
+      case GL_UNSIGNED_INT_10_10_10_2:
+         if (dstFormat == GL_RGBA) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 22)
+                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
+                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) <<  2)
+                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
+            }
+         }
+         else if (dstFormat == GL_BGRA) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 22)
+                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
+                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) <<  2)
+                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
+            }
+         }
+         else if (dstFormat == GL_ABGR_EXT) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F)) << 22)
+                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 12)
+                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) <<  2)
+                      | (((GLuint) (rgba[i][RCOMP] *    3.0F))      );
+            }
+         }
+         break;
+      case GL_UNSIGNED_INT_2_10_10_10_REV:
+         if (dstFormat == GL_RGBA) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F))      )
+                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
+                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 20)
+                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
+            }
+         }
+         else if (dstFormat == GL_BGRA) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F))      )
+                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
+                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 20)
+                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
+            }
+         }
+         else if (dstFormat == GL_ABGR_EXT) {
+            GLuint *dst = (GLuint *) dstAddr;
+            for (i=0;i<n;i++) {
+               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F))      )
+                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 10)
+                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 20)
+                      | (((GLuint) (rgba[i][RCOMP] *    3.0F)) << 30);
+            }
+         }
+         break;
+      default:
+         gl_problem(ctx, "bad type in _mesa_pack_float_rgba_span");
    }
 }
 
 
+
 #define SWAP2BYTE(VALUE)                       \
    {                                           \
       GLubyte *bytes = (GLubyte *) &(VALUE);   \
@@ -2080,8 +2234,7 @@ extract_float_rgba(GLuint n, GLfloat rgba[][4],
 
 /*
  * Unpack a row of color image data from a client buffer according to
- * the pixel unpacking parameters.  Apply any enabled pixel transfer
- * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
+ * the pixel unpacking parameters.
  * Return GLubyte values in the specified dest image format.
  * This is (or will be) used by glDrawPixels and glTexImage?D().
  * Input:  ctx - the context
@@ -2091,18 +2244,18 @@ extract_float_rgba(GLuint n, GLfloat rgba[][4],
  *         srcFormat - source image format
  *         srcType - source image  datatype
  *         source - source image pointer
- *         unpacking - pixel unpacking parameters
- *         applyTransferOps - apply scale/bias/lookup-table ops?
+ *         srcPacking - pixel unpacking parameters
+ *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
  *
  * XXX perhaps expand this to process whole images someday.
  */
 void
-_mesa_unpack_ubyte_color_span( const GLcontext *ctx,
+_mesa_unpack_ubyte_color_span( GLcontext *ctx,
                                GLuint n, GLenum dstFormat, GLubyte dest[],
                                GLenum srcFormat, GLenum srcType,
                                const GLvoid *source,
-                               const struct gl_pixelstore_attrib *unpacking,
-                               GLboolean applyTransferOps )
+                               const struct gl_pixelstore_attrib *srcPacking,
+                               GLuint transferOps )
 {
    ASSERT(dstFormat == GL_ALPHA ||
           dstFormat == GL_LUMINANCE || 
@@ -2147,15 +2300,11 @@ _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
           srcType == GL_UNSIGNED_INT_10_10_10_2 ||
           srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
 
-   /* this is intended for RGBA mode */
-   assert(ctx->Visual->RGBAflag);
-
-   applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA ||
-                        ctx->Pixel.MapColorFlag ||
-                        ctx->Pixel.MapColorFlag);
+   /* this is intended for RGBA mode only */
+   assert(ctx->Visual.RGBAflag);
 
    /* Try simple cases first */
-   if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE) {
+   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE) {
       if (dstFormat == GL_RGBA) {
          if (srcFormat == GL_RGBA) {
             MEMCPY( dest, source, n * 4 * sizeof(GLubyte) );
@@ -2196,7 +2345,7 @@ _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
          }
       }
       else if (dstFormat == srcFormat) {
-         GLint comps = gl_components_in_format(srcFormat);
+         GLint comps = _mesa_components_in_format(srcFormat);
          assert(comps > 0);
          MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
          return;
@@ -2204,14 +2353,14 @@ _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
    }
 
 
+   /* general solution begins here */
    {
-      /* general solution */
       GLfloat rgba[MAX_WIDTH][4];
       GLint dstComponents;
       GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
       GLint dstLuminanceIndex, dstIntensityIndex;
 
-      dstComponents = gl_components_in_format( dstFormat );
+      dstComponents = _mesa_components_in_format( dstFormat );
       /* source & dest image formats should have been error checked by now */
       assert(dstComponents > 0);
 
@@ -2222,59 +2371,73 @@ _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
       if (srcFormat == GL_COLOR_INDEX) {
          GLuint indexes[MAX_WIDTH];
          extract_uint_indexes(n, indexes, srcFormat, srcType, source,
-                              unpacking);
+                              srcPacking);
 
-         /* shift and offset indexes */
-         gl_shift_and_offset_ci(ctx, n, indexes);
+         if (dstFormat == GL_COLOR_INDEX
+             && (transferOps & IMAGE_MAP_COLOR_BIT)) {
+            _mesa_map_ci(ctx, n, indexes);
+         }
+         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
+            _mesa_shift_and_offset_ci(ctx, n, indexes);
+         }
 
          if (dstFormat == GL_COLOR_INDEX) {
-            if (applyTransferOps) {
-               if (ctx->Pixel.MapColorFlag) {
-                  /* Apply lookup table */
-                  gl_map_ci(ctx, n, indexes);
-               }
-
-               if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
-
-               }
-            }
-
             /* convert to GLubyte and return */
-            {
-               GLuint i;
-               for (i = 0; i < n; i++) {
-                  dest[i] = (GLubyte) (indexes[i] & 0xff);
-               }
+            GLuint i;
+            for (i = 0; i < n; i++) {
+               dest[i] = (GLubyte) (indexes[i] & 0xff);
             }
+            return;
          }
          else {
             /* Convert indexes to RGBA */
-            gl_map_ci_to_rgba_float(ctx, n, indexes, rgba);
+            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
          }
       }
       else {
          extract_float_rgba(n, rgba, srcFormat, srcType, source,
-                            unpacking->SwapBytes);
+                            srcPacking->SwapBytes);
 
-         if (applyTransferOps) {
-            /* scale and bias colors */
-            gl_scale_and_bias_rgba_float(ctx, n, rgba);
-
-            /* color table lookup */
-            if (ctx->Pixel.MapColorFlag) {
-               gl_map_rgba_float(ctx, n, rgba);
-            }
+         /* scale and bias colors */
+         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
+            _mesa_scale_and_bias_rgba(ctx, n, rgba);
+         }
+         /* color map lookup */
+         if (transferOps & IMAGE_MAP_COLOR_BIT) {
+            _mesa_map_rgba(ctx, n, rgba);
          }
       }
 
-
-      /*
-       * XXX This is where more color table lookups, convolution,
-       * histograms, minmax, color matrix, etc would take place if
-       * implemented.
-       * See figure 3.7 in the OpenGL 1.2 specification for more info.
-       */
-
+      if (transferOps) {
+         /* GL_COLOR_TABLE lookup */
+         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
+            _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
+         }
+         /* convolution */
+         if (transferOps & IMAGE_CONVOLUTION_BIT) {
+            /* this has to be done in the calling code */
+         }
+         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
+         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
+            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
+         }
+         /* color matrix transform */
+         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
+            _mesa_transform_rgba(ctx, n, rgba);
+         }
+         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
+         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
+            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
+         }
+         /* update histogram count */
+         if (transferOps & IMAGE_HISTOGRAM_BIT) {
+            _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
+         }
+         /* min/max here */
+         if (transferOps & IMAGE_MIN_MAX_BIT) {
+            _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
+         }
+      }
 
       /* clamp to [0,1] */
       {
@@ -2394,11 +2557,267 @@ _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
 }
 
 
+void
+_mesa_unpack_float_color_span( GLcontext *ctx,
+                               GLuint n, GLenum dstFormat, GLfloat dest[],
+                               GLenum srcFormat, GLenum srcType,
+                               const GLvoid *source,
+                               const struct gl_pixelstore_attrib *srcPacking,
+                               GLuint transferOps, GLboolean clamp )
+{
+   ASSERT(dstFormat == GL_ALPHA ||
+          dstFormat == GL_LUMINANCE || 
+          dstFormat == GL_LUMINANCE_ALPHA ||
+          dstFormat == GL_INTENSITY ||
+          dstFormat == GL_RGB ||
+          dstFormat == GL_RGBA ||
+          dstFormat == GL_COLOR_INDEX);
+
+   ASSERT(srcFormat == GL_RED ||
+          srcFormat == GL_GREEN ||
+          srcFormat == GL_BLUE ||
+          srcFormat == GL_ALPHA ||
+          srcFormat == GL_LUMINANCE ||
+          srcFormat == GL_LUMINANCE_ALPHA ||
+          srcFormat == GL_INTENSITY ||
+          srcFormat == GL_RGB ||
+          srcFormat == GL_BGR ||
+          srcFormat == GL_RGBA ||
+          srcFormat == GL_BGRA ||
+          srcFormat == GL_ABGR_EXT ||
+          srcFormat == GL_COLOR_INDEX);
+
+   ASSERT(srcType == GL_BITMAP ||
+          srcType == GL_UNSIGNED_BYTE ||
+          srcType == GL_BYTE ||
+          srcType == GL_UNSIGNED_SHORT ||
+          srcType == GL_SHORT ||
+          srcType == GL_UNSIGNED_INT ||
+          srcType == GL_INT ||
+          srcType == GL_FLOAT ||
+          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
+          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
+          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
+          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
+          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
+          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
+          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
+          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
+          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
+          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
+          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
+          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
+
+   /* this is intended for RGBA mode only */
+   assert(ctx->Visual.RGBAflag);
+
+   /* general solution, no special cases, yet */
+   {
+      GLfloat rgba[MAX_WIDTH][4];
+      GLint dstComponents;
+      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
+      GLint dstLuminanceIndex, dstIntensityIndex;
+
+      dstComponents = _mesa_components_in_format( dstFormat );
+      /* source & dest image formats should have been error checked by now */
+      assert(dstComponents > 0);
+
+      /*
+       * Extract image data and convert to RGBA floats
+       */
+      assert(n <= MAX_WIDTH);
+      if (srcFormat == GL_COLOR_INDEX) {
+         GLuint indexes[MAX_WIDTH];
+         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
+                              srcPacking);
+
+         if (dstFormat == GL_COLOR_INDEX
+             && (transferOps & IMAGE_MAP_COLOR_BIT)) {
+            _mesa_map_ci(ctx, n, indexes);
+         }
+         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
+            _mesa_shift_and_offset_ci(ctx, n, indexes);
+         }
+
+         if (dstFormat == GL_COLOR_INDEX) {
+            /* convert to GLubyte and return */
+            GLuint i;
+            for (i = 0; i < n; i++) {
+               dest[i] = (GLubyte) (indexes[i] & 0xff);
+            }
+            return;
+         }
+         else {
+            /* Convert indexes to RGBA */
+            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
+         }
+      }
+      else {
+         extract_float_rgba(n, rgba, srcFormat, srcType, source,
+                            srcPacking->SwapBytes);
+
+         /* scale and bias colors */
+         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
+            _mesa_scale_and_bias_rgba(ctx, n, rgba);
+         }
+         /* color map lookup */
+         if (transferOps & IMAGE_MAP_COLOR_BIT) {
+            _mesa_map_rgba(ctx, n, rgba);
+         }
+      }
+
+      if (transferOps) {
+         /* GL_COLOR_TABLE lookup */
+         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
+            _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
+         }
+         /* convolution */
+         if (transferOps & IMAGE_CONVOLUTION_BIT) {
+            /* XXX to do */
+         }
+         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
+         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
+            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
+         }
+         /* color matrix transform */
+         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
+            _mesa_transform_rgba(ctx, n, rgba);
+         }
+         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
+         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
+            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
+         }
+         /* update histogram count */
+         if (transferOps & IMAGE_HISTOGRAM_BIT) {
+            _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
+         }
+         /* min/max here */
+         if (transferOps & IMAGE_MIN_MAX_BIT) {
+            _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
+         }
+      }
+
+      /* clamp to [0,1] */
+      if (clamp) {
+         GLuint i;
+         for (i = 0; i < n; i++) {
+            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
+            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
+            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
+            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
+         }
+      }
+
+      /* Now determine which color channels we need to produce.
+       * And determine the dest index (offset) within each color tuple.
+       */
+      switch (dstFormat) {
+         case GL_ALPHA:
+            dstAlphaIndex = 0;
+            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
+            dstLuminanceIndex = dstIntensityIndex = -1;
+            break;
+         case GL_LUMINANCE: 
+            dstLuminanceIndex = 0;
+            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
+            dstIntensityIndex = -1;
+            break;
+         case GL_LUMINANCE_ALPHA:
+            dstLuminanceIndex = 0;
+            dstAlphaIndex = 1;
+            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
+            dstIntensityIndex = -1;
+            break;
+         case GL_INTENSITY:
+            dstIntensityIndex = 0;
+            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
+            dstLuminanceIndex = -1;
+            break;
+         case GL_RGB:
+            dstRedIndex = 0;
+            dstGreenIndex = 1;
+            dstBlueIndex = 2;
+            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
+            break;
+         case GL_RGBA:
+            dstRedIndex = 0;
+            dstGreenIndex = 1;
+            dstBlueIndex = 2;
+            dstAlphaIndex = 3;
+            dstLuminanceIndex = dstIntensityIndex = -1;
+            break;
+         default:
+            gl_problem(ctx, "bad dstFormat in _mesa_unpack_float_color_span()");
+            return;
+      }
+
+      /* Now pack results in the requested dstFormat */
+      if (dstRedIndex >= 0) {
+         GLfloat *dst = dest;
+         GLuint i;
+         for (i = 0; i < n; i++) {
+            dst[dstRedIndex] = rgba[i][RCOMP];
+            dst += dstComponents;
+         }
+      }
+
+      if (dstGreenIndex >= 0) {
+         GLfloat *dst = dest;
+         GLuint i;
+         for (i = 0; i < n; i++) {
+            dst[dstGreenIndex] = rgba[i][GCOMP];
+            dst += dstComponents;
+         }
+      }
+
+      if (dstBlueIndex >= 0) {
+         GLfloat *dst = dest;
+         GLuint i;
+         for (i = 0; i < n; i++) {
+            dst[dstBlueIndex] = rgba[i][BCOMP];
+            dst += dstComponents;
+         }
+      }
+
+      if (dstAlphaIndex >= 0) {
+         GLfloat *dst = dest;
+         GLuint i;
+         for (i = 0; i < n; i++) {
+            dst[dstAlphaIndex] = rgba[i][ACOMP];
+            dst += dstComponents;
+         }
+      }
+
+      if (dstIntensityIndex >= 0) {
+         GLfloat *dst = dest;
+         GLuint i;
+         assert(dstIntensityIndex == 0);
+         assert(dstComponents == 1);
+         for (i = 0; i < n; i++) {
+            /* Intensity comes from red channel */
+            dst[i] = rgba[i][RCOMP];
+         }
+      }
+
+      if (dstLuminanceIndex >= 0) {
+         GLfloat *dst = dest;
+         GLuint i;
+         assert(dstLuminanceIndex == 0);
+         for (i = 0; i < n; i++) {
+            /* Luminance comes from red channel */
+            dst[0] = rgba[i][RCOMP];
+            dst += dstComponents;
+         }
+      }
+   }
+}
+
+
+
 
 /*
  * Unpack a row of color index data from a client buffer according to
- * the pixel unpacking parameters.  Apply pixel transfer ops if enabled
- * and applyTransferOps is true.
+ * the pixel unpacking parameters.
  * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
  *
  * Args:  ctx - the context
@@ -2407,15 +2826,15 @@ _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
  *        dest - destination array
  *        srcType - source pixel type
  *        source - source data pointer
- *        unpacking - pixel unpacking parameters
- *        applyTransferOps - apply offset/bias/lookup ops?
+ *        srcPacking - pixel unpacking parameters
+ *        transferOps - the pixel transfer operations to apply
  */
 void
 _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
                          GLenum dstType, GLvoid *dest,
                          GLenum srcType, const GLvoid *source,
-                         const struct gl_pixelstore_attrib *unpacking,
-                         GLboolean applyTransferOps )
+                         const struct gl_pixelstore_attrib *srcPacking,
+                         GLuint transferOps )
 {
    ASSERT(srcType == GL_BITMAP ||
           srcType == GL_UNSIGNED_BYTE ||
@@ -2430,17 +2849,18 @@ _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
           dstType == GL_UNSIGNED_SHORT ||
           dstType == GL_UNSIGNED_INT);
 
-   applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
+
+   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
 
    /*
     * Try simple cases first
     */
-   if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
+   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
        && dstType == GL_UNSIGNED_BYTE) {
       MEMCPY(dest, source, n * sizeof(GLubyte));
    }
-   else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
-            && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
+   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
+            && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
       MEMCPY(dest, source, n * sizeof(GLuint));
    }
    else {
@@ -2451,18 +2871,15 @@ _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
       assert(n <= MAX_WIDTH);
 
       extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
-                           unpacking);
+                           srcPacking);
 
-      if (applyTransferOps) {
-         if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
-            /* shift and offset indexes */
-            gl_shift_and_offset_ci(ctx, n, indexes);
-         }
-
-         if (ctx->Pixel.MapColorFlag) {
-            /* Apply lookup table */
-            gl_map_ci(ctx, n, indexes);
-         }
+      if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
+         /* shift and offset indexes */
+         _mesa_shift_and_offset_ci(ctx, n, indexes);
+      }
+      if (transferOps & IMAGE_MAP_COLOR_BIT) {
+         /* Apply lookup table */
+         _mesa_map_ci(ctx, n, indexes);
       }
 
       /* convert to dest type */
@@ -2497,8 +2914,7 @@ _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
 
 /*
  * Unpack a row of stencil data from a client buffer according to
- * the pixel unpacking parameters.  Apply pixel transfer ops if enabled
- * and applyTransferOps is true.
+ * the pixel unpacking parameters.
  * This is (or will be) used by glDrawPixels
  *
  * Args:  ctx - the context
@@ -2507,15 +2923,15 @@ _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
  *        dest - destination array
  *        srcType - source pixel type
  *        source - source data pointer
- *        unpacking - pixel unpacking parameters
- *        applyTransferOps - apply offset/bias/lookup ops?
+ *        srcPacking - pixel unpacking parameters
+ *        transferOps - apply offset/bias/lookup ops?
  */
 void
 _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
                            GLenum dstType, GLvoid *dest,
                            GLenum srcType, const GLvoid *source,
-                           const struct gl_pixelstore_attrib *unpacking,
-                           GLboolean applyTransferOps )
+                           const struct gl_pixelstore_attrib *srcPacking,
+                           GLuint transferOps )
 {
    ASSERT(srcType == GL_BITMAP ||
           srcType == GL_UNSIGNED_BYTE ||
@@ -2530,17 +2946,21 @@ _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
           dstType == GL_UNSIGNED_SHORT ||
           dstType == GL_UNSIGNED_INT);
 
-   applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
+   /* only shift and offset apply to stencil */
+   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
 
    /*
     * Try simple cases first
     */
-   if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
-       && dstType == GL_UNSIGNED_BYTE) {
+   if (transferOps == 0 &&
+       srcType == GL_UNSIGNED_BYTE &&
+       dstType == GL_UNSIGNED_BYTE) {
       MEMCPY(dest, source, n * sizeof(GLubyte));
    }
-   else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
-            && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
+   else if (transferOps == 0 &&
+            srcType == GL_UNSIGNED_INT &&
+            dstType == GL_UNSIGNED_INT &&
+            !srcPacking->SwapBytes) {
       MEMCPY(dest, source, n * sizeof(GLuint));
    }
    else {
@@ -2551,12 +2971,12 @@ _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
       assert(n <= MAX_WIDTH);
 
       extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
-                           unpacking);
+                           srcPacking);
 
-      if (applyTransferOps) {
-         if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
+      if (transferOps) {
+         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
             /* shift and offset indexes */
-            gl_shift_and_offset_ci(ctx, n, indexes);
+            _mesa_shift_and_offset_ci(ctx, n, indexes);
          }
 
          if (ctx->Pixel.MapStencilFlag) {
@@ -2603,8 +3023,8 @@ _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
 void
 _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
                          GLenum srcType, const GLvoid *source,
-                         const struct gl_pixelstore_attrib *unpacking,
-                         GLboolean applyTransferOps )
+                         const struct gl_pixelstore_attrib *srcPacking,
+                         GLuint transferOps )
 {
    GLfloat *depth = MALLOC(n * sizeof(GLfloat));
    if (!depth)
@@ -2670,6 +3090,7 @@ _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
          break;
       default:
          gl_problem(NULL, "bad type in _mesa_unpack_depth_span()");
+         FREE(depth);
          return;
    }
 
@@ -2684,7 +3105,7 @@ _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
 
    /* clamp depth values to [0,1] and convert from floats to integers */
    {
-      const GLfloat zs = ctx->Visual->DepthMaxF;
+      const GLfloat zs = ctx->Visual.DepthMaxF;
       GLuint i;
       for (i = 0; i < n; i++) {
          dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * zs);
@@ -2721,8 +3142,8 @@ _mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
       compsPerRow = 0;
    }
    else {
-      const GLint bytesPerPixel = gl_bytes_per_pixel(format, type);
-      const GLint components = gl_components_in_format(format);
+      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
+      const GLint components = _mesa_components_in_format(format);
       GLint bytesPerComp;
       if (bytesPerPixel <= 0 || components <= 0)
          return NULL;   /* bad format or type.  generate error later */
@@ -2745,18 +3166,18 @@ _mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
       dst = destBuffer;
       for (img = 0; img < depth; img++) {
          for (row = 0; row < height; row++) {
-            const GLvoid *src = gl_pixel_addr_in_image(unpack, pixels,
+            const GLvoid *src = _mesa_image_address(unpack, pixels,
                                width, height, format, type, img, row, 0);
             MEMCPY(dst, src, bytesPerRow);
             /* byte flipping/swapping */
             if (flipBytes) {
-               gl_flip_bytes((GLubyte *) dst, bytesPerRow);
+               flip_bytes((GLubyte *) dst, bytesPerRow);
             }
             else if (swap2) {
-               gl_swap2((GLushort*) dst, compsPerRow);
+               _mesa_swap2((GLushort*) dst, compsPerRow);
             }
             else if (swap4) {
-               gl_swap4((GLuint*) dst, compsPerRow);
+               _mesa_swap4((GLuint*) dst, compsPerRow);
             }
             dst += bytesPerRow;
          }
@@ -2790,9 +3211,9 @@ _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
    width_in_bytes = CEILING( width, 8 );
    dst = buffer;
    for (row = 0; row < height; row++) {
-      GLubyte *src = gl_pixel_addr_in_image( packing, pixels, width, height,
-                                             GL_COLOR_INDEX, GL_BITMAP,
-                                             0, row, 0 );
+      GLubyte *src = _mesa_image_address( packing, pixels, width, height,
+                                          GL_COLOR_INDEX, GL_BITMAP,
+                                          0, row, 0 );
       if (!src) {
          FREE(buffer);
          return NULL;
@@ -2801,7 +3222,7 @@ _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
       if (packing->SkipPixels == 0) {
          MEMCPY( dst, src, width_in_bytes );
          if (packing->LsbFirst) {
-            gl_flip_bytes( dst, width_in_bytes );
+            flip_bytes( dst, width_in_bytes );
          }
       }
       else {
@@ -2885,16 +3306,16 @@ _mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
    width_in_bytes = CEILING( width, 8 );
    src = source;
    for (row = 0; row < height; row++) {
-      GLubyte *dst = gl_pixel_addr_in_image( packing, dest, width, height,
-                                             GL_COLOR_INDEX, GL_BITMAP,
-                                             0, row, 0 );
+      GLubyte *dst = _mesa_image_address( packing, dest, width, height,
+                                          GL_COLOR_INDEX, GL_BITMAP,
+                                          0, row, 0 );
       if (!dst)
          return;
 
       if (packing->SkipPixels == 0) {
          MEMCPY( dst, src, width_in_bytes );
          if (packing->LsbFirst) {
-            gl_flip_bytes( dst, width_in_bytes );
+            flip_bytes( dst, width_in_bytes );
          }
       }
       else {