Committing in .
[mesa.git] / src / mesa / swrast / s_readpix.c
index ac91dadcf3a45861301b038d7e4969c5262f3cc7..b5ab06e4ba6e5ec3919c85b17f30ca8b791349a0 100644 (file)
@@ -1,21 +1,19 @@
-/* $Id: s_readpix.c,v 1.3 2000/11/13 20:02:57 keithw Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
- * 
- * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
- * 
+ * Version:  6.1
+ *
+ * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
@@ -32,7 +30,7 @@
 #include "feedback.h"
 #include "image.h"
 #include "macros.h"
-#include "mem.h"
+#include "imports.h"
 #include "pixel.h"
 
 #include "s_alphabuf.h"
 /*
  * Read a block of color index pixels.
  */
-static void read_index_pixels( GLcontext *ctx,
-                               GLint x, GLint y,
-                              GLsizei width, GLsizei height,
-                              GLenum type, GLvoid *pixels,
-                               const struct gl_pixelstore_attrib *packing )
+static void
+read_index_pixels( GLcontext *ctx,
+                   GLint x, GLint y,
+                   GLsizei width, GLsizei height,
+                   GLenum type, GLvoid *pixels,
+                   const struct gl_pixelstore_attrib *packing )
 {
-   GLint i, j, readWidth;
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   GLint i, readWidth;
 
    /* error checking */
-   if (ctx->Visual.RGBAflag) {
-      gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
+   if (ctx->Visual.rgbMode) {
+      _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
+      return;
+   }
+
+   if (type != GL_BYTE &&
+       type != GL_UNSIGNED_BYTE &&
+       type != GL_SHORT &&
+       type != GL_UNSIGNED_SHORT &&
+       type != GL_INT &&
+       type != GL_UNSIGNED_INT &&
+       type != GL_FLOAT) {
+      _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(index type)");
       return;
    }
 
-   ASSERT(ctx->Driver.SetReadBuffer);
-   (*ctx->Driver.SetReadBuffer)(ctx, ctx->ReadBuffer, ctx->Pixel.DriverReadBuffer);
+   _swrast_use_read_buffer(ctx);
 
    readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
 
    /* process image row by row */
-   for (j=0;j<height;j++,y++) {
+   for (i = 0; i < height; i++) {
       GLuint index[MAX_WIDTH];
       GLvoid *dest;
 
-      (*ctx->Driver.ReadCI32Span)( ctx, readWidth, x, y, index );
+      (*swrast->Driver.ReadCI32Span)(ctx, readWidth, x, y + i, index);
 
-      if (ctx->Pixel.IndexShift!=0 || ctx->Pixel.IndexOffset!=0) {
-         _mesa_shift_and_offset_ci( ctx, readWidth, index);
-      }
-
-      if (ctx->Pixel.MapColorFlag) {
-         _mesa_map_ci(ctx, readWidth, index);
-      }
+      dest = _mesa_image_address(packing, pixels, width, height,
+                                 GL_COLOR_INDEX, type, 0, i, 0);
 
-      dest = _mesa_image_address(packing, pixels,
-                         width, height, GL_COLOR_INDEX, type, 0, j, 0);
-
-      switch (type) {
-        case GL_UNSIGNED_BYTE:
-           {
-               GLubyte *dst = (GLubyte *) dest;
-              for (i=0;i<readWidth;i++) {
-                 *dst++ = (GLubyte) index[i];
-              }
-           }
-           break;
-        case GL_BYTE:
-           {
-               GLbyte *dst = (GLbyte *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLbyte) index[i];
-              }
-           }
-           break;
-        case GL_UNSIGNED_SHORT:
-           {
-               GLushort *dst = (GLushort *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLushort) index[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap2( (GLushort *) dst, readWidth );
-              }
-           }
-           break;
-        case GL_SHORT:
-           {
-               GLshort *dst = (GLshort *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLshort) index[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap2( (GLushort *) dst, readWidth );
-              }
-           }
-           break;
-        case GL_UNSIGNED_INT:
-           {
-               GLuint *dst = (GLuint *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLuint) index[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap4( (GLuint *) dst, readWidth );
-              }
-           }
-           break;
-        case GL_INT:
-           {
-               GLint *dst = (GLint *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLint) index[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap4( (GLuint *) dst, readWidth );
-              }
-           }
-           break;
-        case GL_FLOAT:
-           {
-               GLfloat *dst = (GLfloat *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLfloat) index[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap4( (GLuint *) dst, readWidth );
-              }
-           }
-           break;
-         default:
-            gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
-            j = height + 1; /* exit loop */
-      }
+      _mesa_pack_index_span(ctx, readWidth, type, dest, index,
+                            &ctx->Pack, ctx->_ImageTransferState);
    }
 
-   (*ctx->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer, ctx->Color.DriverDrawBuffer);
+   _swrast_use_draw_buffer(ctx);
 }
 
 
 
-static void read_depth_pixels( GLcontext *ctx,
-                               GLint x, GLint y,
-                              GLsizei width, GLsizei height,
-                              GLenum type, GLvoid *pixels,
-                               const struct gl_pixelstore_attrib *packing )
+static void
+read_depth_pixels( GLcontext *ctx,
+                   GLint x, GLint y,
+                   GLsizei width, GLsizei height,
+                   GLenum type, GLvoid *pixels,
+                   const struct gl_pixelstore_attrib *packing )
 {
-   GLint i, j, readWidth;
+   GLint readWidth;
    GLboolean bias_or_scale;
 
    /* Error checking */
-   if (ctx->Visual.DepthBits <= 0) {
+   if (ctx->Visual.depthBits <= 0) {
       /* No depth buffer */
-      gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
+      _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
       return;
    }
 
@@ -191,142 +120,64 @@ static void read_depth_pixels( GLcontext *ctx,
        type != GL_INT &&
        type != GL_UNSIGNED_INT &&
        type != GL_FLOAT) {
-      gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels(depth type)");
+      _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(depth type)");
       return;
    }
 
    bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
 
-   if (type==GL_UNSIGNED_SHORT && ctx->Visual.DepthBits == 16
+   if (type==GL_UNSIGNED_SHORT && ctx->Visual.depthBits == 16
        && !bias_or_scale && !packing->SwapBytes) {
       /* Special case: directly read 16-bit unsigned depth values. */
+      GLint j;
       for (j=0;j<height;j++,y++) {
          GLdepth depth[MAX_WIDTH];
          GLushort *dst = (GLushort*) _mesa_image_address( packing, pixels,
                          width, height, GL_DEPTH_COMPONENT, type, 0, j, 0 );
          GLint i;
-         _mesa_read_depth_span(ctx, width, x, y, depth);
+         _swrast_read_depth_span(ctx, width, x, y, depth);
          for (i = 0; i < width; i++)
             dst[i] = depth[i];
       }
    }
-   else if (type==GL_UNSIGNED_INT && ctx->Visual.DepthBits == 32
+   else if (type==GL_UNSIGNED_INT && ctx->Visual.depthBits == 32
             && !bias_or_scale && !packing->SwapBytes) {
       /* Special case: directly read 32-bit unsigned depth values. */
+      GLint j;
       for (j=0;j<height;j++,y++) {
          GLdepth *dst = (GLdepth *) _mesa_image_address( packing, pixels,
                          width, height, GL_DEPTH_COMPONENT, type, 0, j, 0 );
-         _mesa_read_depth_span(ctx, width, x, y, dst);
+         _swrast_read_depth_span(ctx, width, x, y, dst);
       }
    }
    else {
       /* General case (slower) */
+      GLint j;
       for (j=0;j<height;j++,y++) {
          GLfloat depth[MAX_WIDTH];
          GLvoid *dest;
 
-         _mesa_read_depth_span_float(ctx, readWidth, x, y, depth);
+         _swrast_read_depth_span_float(ctx, readWidth, x, y, depth);
 
-         if (bias_or_scale) {
-            for (i=0;i<readWidth;i++) {
-               GLfloat d;
-               d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
-               depth[i] = CLAMP( d, 0.0F, 1.0F );
-            }
-         }
+         dest = _mesa_image_address(packing, pixels, width, height,
+                                    GL_DEPTH_COMPONENT, type, 0, j, 0);
 
-         dest = _mesa_image_address(packing, pixels,
-                         width, height, GL_DEPTH_COMPONENT, type, 0, j, 0);
-
-         switch (type) {
-            case GL_UNSIGNED_BYTE:
-               {
-                  GLubyte *dst = (GLubyte *) dest;
-                  for (i=0;i<readWidth;i++) {
-                     dst[i] = FLOAT_TO_UBYTE( depth[i] );
-                  }
-               }
-               break;
-            case GL_BYTE:
-               {
-                  GLbyte *dst = (GLbyte *) dest;
-                  for (i=0;i<readWidth;i++) {
-                     dst[i] = FLOAT_TO_BYTE( depth[i] );
-                  }
-               }
-               break;
-            case GL_UNSIGNED_SHORT:
-               {
-                  GLushort *dst = (GLushort *) dest;
-                  for (i=0;i<readWidth;i++) {
-                     dst[i] = FLOAT_TO_USHORT( depth[i] );
-                  }
-                  if (packing->SwapBytes) {
-                     _mesa_swap2( (GLushort *) dst, readWidth );
-                  }
-               }
-               break;
-            case GL_SHORT:
-               {
-                  GLshort *dst = (GLshort *) dest;
-                  for (i=0;i<readWidth;i++) {
-                     dst[i] = FLOAT_TO_SHORT( depth[i] );
-                  }
-                  if (packing->SwapBytes) {
-                     _mesa_swap2( (GLushort *) dst, readWidth );
-                  }
-               }
-               break;
-            case GL_UNSIGNED_INT:
-               {
-                  GLuint *dst = (GLuint *) dest;
-                  for (i=0;i<readWidth;i++) {
-                     dst[i] = FLOAT_TO_UINT( depth[i] );
-                  }
-                  if (packing->SwapBytes) {
-                     _mesa_swap4( (GLuint *) dst, readWidth );
-                  }
-               }
-               break;
-            case GL_INT:
-               {
-                  GLint *dst = (GLint *) dest;
-                  for (i=0;i<readWidth;i++) {
-                     dst[i] = FLOAT_TO_INT( depth[i] );
-                  }
-                  if (packing->SwapBytes) {
-                     _mesa_swap4( (GLuint *) dst, readWidth );
-                  }
-               }
-               break;
-            case GL_FLOAT:
-               {
-                  GLfloat *dst = (GLfloat *) dest;
-                  for (i=0;i<readWidth;i++) {
-                     dst[i] = depth[i];
-                  }
-                  if (packing->SwapBytes) {
-                     _mesa_swap4( (GLuint *) dst, readWidth );
-                  }
-               }
-               break;
-            default:
-               gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
-         }
+         _mesa_pack_depth_span(ctx, readWidth, (GLdepth *) dest, type,
+                               depth, &ctx->Pack);
       }
    }
 }
 
 
 
-static void read_stencil_pixels( GLcontext *ctx,
-                                 GLint x, GLint y,
-                                GLsizei width, GLsizei height,
-                                GLenum type, GLvoid *pixels,
-                                 const struct gl_pixelstore_attrib *packing )
+static void
+read_stencil_pixels( GLcontext *ctx,
+                     GLint x, GLint y,
+                     GLsizei width, GLsizei height,
+                     GLenum type, GLvoid *pixels,
+                     const struct gl_pixelstore_attrib *packing )
 {
-   GLboolean shift_or_offset;
-   GLint i, j, readWidth;
+   GLint j, readWidth;
 
    if (type != GL_BYTE &&
        type != GL_UNSIGNED_BYTE &&
@@ -336,149 +187,29 @@ static void read_stencil_pixels( GLcontext *ctx,
        type != GL_UNSIGNED_INT &&
        type != GL_FLOAT &&
        type != GL_BITMAP) {
-      gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels(stencil type)");
+      _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(stencil type)");
       return;
    }
 
    readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
 
-   if (ctx->Visual.StencilBits<=0) {
+   if (ctx->Visual.stencilBits <= 0) {
       /* No stencil buffer */
-      gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
+      _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
       return;
    }
 
-   shift_or_offset = ctx->Pixel.IndexShift!=0 || ctx->Pixel.IndexOffset!=0;
-
    /* process image row by row */
    for (j=0;j<height;j++,y++) {
       GLvoid *dest;
       GLstencil stencil[MAX_WIDTH];
 
-      _mesa_read_stencil_span( ctx, readWidth, x, y, stencil );
-
-      if (shift_or_offset) {
-         _mesa_shift_and_offset_stencil( ctx, readWidth, stencil );
-      }
-
-      if (ctx->Pixel.MapStencilFlag) {
-         _mesa_map_stencil( ctx, readWidth, stencil );
-      }
+      _swrast_read_stencil_span(ctx, readWidth, x, y, stencil);
 
-      dest = _mesa_image_address( packing, pixels,
-                          width, height, GL_STENCIL_INDEX, type, 0, j, 0 );
+      dest = _mesa_image_address(packing, pixels, width, height,
+                                 GL_STENCIL_INDEX, type, 0, j, 0);
 
-      switch (type) {
-        case GL_UNSIGNED_BYTE:
-            if (sizeof(GLstencil) == 8) {
-               MEMCPY( dest, stencil, readWidth );
-            }
-            else {
-               GLubyte *dst = (GLubyte *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLubyte) stencil[i];
-              }
-            }
-           break;
-        case GL_BYTE:
-            if (sizeof(GLstencil) == 8) {
-               MEMCPY( dest, stencil, readWidth );
-            }
-            else {
-               GLbyte *dst = (GLbyte *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLbyte) stencil[i];
-              }
-            }
-           break;
-        case GL_UNSIGNED_SHORT:
-           {
-               GLushort *dst = (GLushort *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLushort) stencil[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap2( (GLushort *) dst, readWidth );
-              }
-           }
-           break;
-        case GL_SHORT:
-           {
-               GLshort *dst = (GLshort *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLshort) stencil[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap2( (GLushort *) dst, readWidth );
-              }
-           }
-           break;
-        case GL_UNSIGNED_INT:
-           {
-               GLuint *dst = (GLuint *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLuint) stencil[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap4( (GLuint *) dst, readWidth );
-              }
-           }
-           break;
-        case GL_INT:
-           {
-               GLint *dst = (GLint *) dest;
-              for (i=0;i<readWidth;i++) {
-                 *dst++ = (GLint) stencil[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap4( (GLuint *) dst, readWidth );
-              }
-           }
-           break;
-        case GL_FLOAT:
-           {
-               GLfloat *dst = (GLfloat *) dest;
-              for (i=0;i<readWidth;i++) {
-                 dst[i] = (GLfloat) stencil[i];
-              }
-              if (packing->SwapBytes) {
-                 _mesa_swap4( (GLuint *) dst, readWidth );
-              }
-           }
-           break;
-         case GL_BITMAP:
-            if (packing->LsbFirst) {
-               GLubyte *dst = (GLubyte*) dest;
-               GLint shift = 0;
-               for (i = 0; i < readWidth; i++) {
-                  if (shift == 0)
-                     *dst = 0;
-                  *dst |= ((stencil != 0) << shift);
-                  shift++;
-                  if (shift == 8) {
-                     shift = 0;
-                     dst++;
-                  }
-               }
-            }
-            else {
-               GLubyte *dst = (GLubyte*) dest;
-               GLint shift = 7;
-               for (i = 0; i < readWidth; i++) {
-                  if (shift == 7)
-                     *dst = 0;
-                  *dst |= ((stencil != 0) << shift);
-                  shift--;
-                  if (shift < 0) {
-                     shift = 7;
-                     dst++;
-                  }
-               }
-            }
-            break;
-         default:
-            gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
-      }
+      _mesa_pack_stencil_span(ctx, readWidth, type, dest, stencil, &ctx->Pack);
    }
 }
 
@@ -497,6 +228,7 @@ read_fast_rgba_pixels( GLcontext *ctx,
                        GLvoid *pixels,
                        const struct gl_pixelstore_attrib *packing )
 {
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    /* can't do scale, bias, mapping, etc */
    if (ctx->_ImageTransferState)
        return GL_FALSE;
@@ -520,24 +252,24 @@ read_fast_rgba_pixels( GLcontext *ctx,
          rowLength = width;
 
       /* horizontal clipping */
-      if (srcX < ctx->ReadBuffer->_Xmin) {
-         skipPixels += (ctx->ReadBuffer->_Xmin - srcX);
-         readWidth  -= (ctx->ReadBuffer->_Xmin - srcX);
-         srcX = ctx->ReadBuffer->_Xmin;
+      if (srcX < 0) {
+         skipPixels -= srcX;
+         readWidth += srcX;
+         srcX = 0;
       }
-      if (srcX + readWidth > ctx->ReadBuffer->_Xmax)
-         readWidth -= (srcX + readWidth - ctx->ReadBuffer->_Xmax);
+      if (srcX + readWidth > (GLint) ctx->ReadBuffer->Width)
+         readWidth -= (srcX + readWidth - (GLint) ctx->ReadBuffer->Width);
       if (readWidth <= 0)
          return GL_TRUE;
 
       /* vertical clipping */
-      if (srcY < ctx->ReadBuffer->_Ymin) {
-         skipRows   += (ctx->ReadBuffer->_Ymin - srcY);
-         readHeight -= (ctx->ReadBuffer->_Ymin - srcY);
-         srcY = ctx->ReadBuffer->_Ymin;
+      if (srcY < 0) {
+         skipRows -= srcY;
+         readHeight += srcY;
+         srcY = 0;
       }
-      if (srcY + readHeight > ctx->ReadBuffer->_Ymax)
-         readHeight -= (srcY + readHeight - ctx->ReadBuffer->_Ymax);
+      if (srcY + readHeight > (GLint) ctx->ReadBuffer->Height)
+         readHeight -= (srcY + readHeight - (GLint) ctx->ReadBuffer->Height);
       if (readHeight <= 0)
          return GL_TRUE;
 
@@ -556,13 +288,20 @@ read_fast_rgba_pixels( GLcontext *ctx,
       if (0) {
 #endif
          GLchan *dest = (GLchan *) pixels
-                         + (skipRows * rowLength + skipPixels) * 4;
+                      + (skipRows * rowLength + skipPixels) * 4;
          GLint row;
+
+         if (packing->Invert) {
+            /* start at top and go down */
+            dest += (readHeight - 1) * rowLength * 4;
+            rowLength = -rowLength;
+         }
+
          for (row=0; row<readHeight; row++) {
-            (*ctx->Driver.ReadRGBASpan)(ctx, readWidth, srcX, srcY,
+            (*swrast->Driver.ReadRGBASpan)(ctx, readWidth, srcX, srcY,
                                         (GLchan (*)[4]) dest);
             if (ctx->DrawBuffer->UseSoftwareAlphaBuffers) {
-               _mesa_read_alpha_span(ctx, readWidth, srcX, srcY,
+               _swrast_read_alpha_span(ctx, readWidth, srcX, srcY,
                                      (GLchan (*)[4]) dest);
             }
             dest += rowLength * 4;
@@ -582,21 +321,23 @@ read_fast_rgba_pixels( GLcontext *ctx,
 /*
  * Read R, G, B, A, RGB, L, or LA pixels.
  */
-static void read_rgba_pixels( GLcontext *ctx,
-                              GLint x, GLint y,
-                              GLsizei width, GLsizei height,
-                              GLenum format, GLenum type, GLvoid *pixels,
-                              const struct gl_pixelstore_attrib *packing )
+static void
+read_rgba_pixels( GLcontext *ctx,
+                  GLint x, GLint y,
+                  GLsizei width, GLsizei height,
+                  GLenum format, GLenum type, GLvoid *pixels,
+                  const struct gl_pixelstore_attrib *packing )
 {
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    GLint readWidth;
 
-   (*ctx->Driver.SetReadBuffer)(ctx, ctx->ReadBuffer, ctx->Pixel.DriverReadBuffer);
+   _swrast_use_read_buffer(ctx);
 
    /* Try optimized path first */
    if (read_fast_rgba_pixels( ctx, x, y, width, height,
                               format, type, pixels, packing )) {
 
-      (*ctx->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer, ctx->Color.DriverDrawBuffer);
+      _swrast_use_draw_buffer(ctx);
       return; /* done! */
    }
 
@@ -626,13 +367,13 @@ static void read_rgba_pixels( GLcontext *ctx,
          /* valid pixel type */
          break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
          return;
    }
 
    if (!_mesa_is_legal_format_and_type(format, type) ||
        format == GL_INTENSITY) {
-      gl_error(ctx, GL_INVALID_OPERATION, "glReadPixels(format or type)");
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(format or type)");
       return;
    }
 
@@ -641,15 +382,15 @@ static void read_rgba_pixels( GLcontext *ctx,
       GLfloat *dest, *src, *tmpImage, *convImage;
       GLint row;
 
-      tmpImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
+      tmpImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
       if (!tmpImage) {
-         gl_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
          return;
       }
-      convImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
+      convImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
       if (!convImage) {
-         FREE(tmpImage);
-         gl_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
+         _mesa_free(tmpImage);
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
          return;
       }
 
@@ -657,19 +398,19 @@ static void read_rgba_pixels( GLcontext *ctx,
       dest = tmpImage;
       for (row = 0; row < height; row++, y++) {
          GLchan rgba[MAX_WIDTH][4];
-         if (ctx->Visual.RGBAflag) {
-            gl_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
+         if (ctx->Visual.rgbMode) {
+            _swrast_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
          }
          else {
             GLuint index[MAX_WIDTH];
-            (*ctx->Driver.ReadCI32Span)(ctx, readWidth, x, y, index);
+            (*swrast->Driver.ReadCI32Span)(ctx, readWidth, x, y, index);
             if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset !=0 ) {
                _mesa_map_ci(ctx, readWidth, index);
             }
             _mesa_map_ci_to_rgba_chan(ctx, readWidth, index, rgba);
          }
-         _mesa_pack_rgba_span(ctx, readWidth, (const GLchan (*)[4]) rgba,
-                              GL_RGBA, GL_FLOAT, dest, &_mesa_native_packing,
+         _mesa_pack_rgba_span_chan(ctx, readWidth, (const GLchan (*)[4]) rgba,
+                              GL_RGBA, GL_FLOAT, dest, &ctx->DefaultPacking,
                               transferOps & IMAGE_PRE_CONVOLUTION_BITS);
          dest += width * 4;
       }
@@ -682,15 +423,15 @@ static void read_rgba_pixels( GLcontext *ctx,
          ASSERT(ctx->Pixel.Separable2DEnabled);
          _mesa_convolve_sep_image(ctx, &readWidth, &height, tmpImage, convImage);
       }
-      FREE(tmpImage);
+      _mesa_free(tmpImage);
 
       /* finish transfer ops and pack the resulting image */
       src = convImage;
       for (row = 0; row < height; row++) {
          GLvoid *dest;
-         dest = _mesa_image_address(packing, pixels, width, height,
+         dest = _mesa_image_address(packing, pixels, readWidth, height,
                                     format, type, 0, row, 0);
-         _mesa_pack_float_rgba_span(ctx, readWidth,
+         _mesa_pack_rgba_span_float(ctx, readWidth,
                                     (const GLfloat (*)[4]) src,
                                     format, type, dest, packing,
                                     transferOps & IMAGE_POST_CONVOLUTION_BITS);
@@ -703,12 +444,12 @@ static void read_rgba_pixels( GLcontext *ctx,
       for (row = 0; row < height; row++, y++) {
          GLchan rgba[MAX_WIDTH][4];
          GLvoid *dst;
-         if (ctx->Visual.RGBAflag) {
-            gl_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
+         if (ctx->Visual.rgbMode) {
+            _swrast_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
          }
          else {
             GLuint index[MAX_WIDTH];
-            (*ctx->Driver.ReadCI32Span)(ctx, readWidth, x, y, index);
+            (*swrast->Driver.ReadCI32Span)(ctx, readWidth, x, y, index);
             if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
                _mesa_map_ci(ctx, readWidth, index);
             }
@@ -716,13 +457,33 @@ static void read_rgba_pixels( GLcontext *ctx,
          }
          dst = _mesa_image_address(packing, pixels, width, height,
                                    format, type, 0, row, 0);
-         _mesa_pack_rgba_span(ctx, readWidth, (const GLchan (*)[4]) rgba,
-                              format, type, dst, packing,
-                              ctx->_ImageTransferState);
+         if (ctx->Visual.redBits < CHAN_BITS ||
+             ctx->Visual.greenBits < CHAN_BITS ||
+             ctx->Visual.blueBits < CHAN_BITS) {
+            /* Requantize the color values into floating point and go from
+             * there.  This fixes conformance failures with 16-bit color
+             * buffers, for example.
+             */
+            DEFMARRAY(GLfloat, rgbaf, MAX_WIDTH, 4);  /* mac 32k limitation */
+            CHECKARRAY(rgbaf, return);  /* mac 32k limitation */
+            _mesa_chan_to_float_span(ctx, readWidth,
+                                     (CONST GLchan (*)[4]) rgba, rgbaf);
+            _mesa_pack_rgba_span_float(ctx, readWidth,
+                                       (CONST GLfloat (*)[4]) rgbaf,
+                                       format, type, dst, packing,
+                                       ctx->_ImageTransferState);
+            UNDEFARRAY(rgbaf);  /* mac 32k limitation */
+         }
+         else {
+            /* GLubytes are fine */
+            _mesa_pack_rgba_span_chan(ctx, readWidth, (CONST GLchan (*)[4]) rgba,
+                                 format, type, dst, packing,
+                                 ctx->_ImageTransferState);
+         }
       }
    }
 
-   (*ctx->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer, ctx->Color.DriverDrawBuffer);
+   _swrast_use_draw_buffer(ctx);
 }
 
 
@@ -730,15 +491,26 @@ static void read_rgba_pixels( GLcontext *ctx,
 void
 _swrast_ReadPixels( GLcontext *ctx,
                    GLint x, GLint y, GLsizei width, GLsizei height,
-                   GLenum format, GLenum type, 
+                   GLenum format, GLenum type,
                    const struct gl_pixelstore_attrib *pack,
                    GLvoid *pixels )
 {
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    (void) pack;
 
-   if (SWRAST_CONTEXT(ctx)->NewState)
+   if (swrast->NewState)
       _swrast_validate_derived( ctx );
 
+   pixels = _swrast_validate_pbo_access(pack, width, height, 1,
+                                        format, type, (GLvoid *) pixels);
+
+   if (!pixels) {
+      _mesa_error( ctx, GL_INVALID_VALUE, "glReadPixels(pixels)" );
+      return;
+   }
+
+   RENDER_START(swrast,ctx);
+
    switch (format) {
       case GL_COLOR_INDEX:
          read_index_pixels(ctx, x, y, width, height, type, pixels, &ctx->Pack);
@@ -764,6 +536,8 @@ _swrast_ReadPixels( GLcontext *ctx,
                           format, type, pixels, &ctx->Pack);
         break;
       default:
-        gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(format)" );
+        _mesa_error( ctx, GL_INVALID_ENUM, "glReadPixels(format)" );
    }
+
+   RENDER_FINISH(swrast,ctx);
 }