consolidate image transfer operations in new _mesa_apply_rgba_transfer_ops() function
[mesa.git] / src / mesa / swrast / s_copypix.c
index 74d1164ae38a1ec8466abe08d812e3d5e9e56b2f..9176755e35345bbfab905f493ec59030687d5eee 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: s_copypix.c,v 1.35 2002/04/12 15:39:59 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  4.1
+ * Version:  5.1
  *
- * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2003  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"),
 
 
 #include "glheader.h"
-#include "colormac.h"
 #include "context.h"
 #include "convolve.h"
-#include "feedback.h"
+#include "histogram.h"
 #include "macros.h"
-#include "mem.h"
-#include "mmath.h"
+#include "imports.h"
 #include "pixel.h"
 
 #include "s_context.h"
 #include "s_depth.h"
-#include "s_histogram.h"
 #include "s_pixeltex.h"
 #include "s_span.h"
 #include "s_stencil.h"
@@ -103,25 +98,23 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    GLboolean quick_draw;
    GLint row;
    GLboolean changeBuffer;
-   GLchan *saveReadAlpha;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    const GLuint transferOps = ctx->_ImageTransferState;
    GLfloat *dest, *tmpImage, *convImage;
    struct sw_span span;
 
    INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
-   /*span.arrayMask |= SPAN_RGBA;*/
 
    if (ctx->Depth.Test)
-      _mesa_span_default_z(ctx, &span);
+      _swrast_span_default_z(ctx, &span);
    if (ctx->Fog.Enabled)
-      _mesa_span_default_fog(ctx, &span);
+      _swrast_span_default_fog(ctx, &span);
 
 
    if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
        && !zoom
        && destx >= 0
-       && destx + width <= ctx->DrawBuffer->Width) {
+       && destx + width <= (GLint) ctx->DrawBuffer->Width) {
       quick_draw = GL_TRUE;
    }
    else {
@@ -129,7 +122,6 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    }
 
    /* If read and draw buffer are different we must do buffer switching */
-   saveReadAlpha = ctx->ReadBuffer->Alpha;
    changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
                || ctx->DrawBuffer != ctx->ReadBuffer;
 
@@ -150,16 +142,8 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    dest = tmpImage;
 
    if (changeBuffer) {
-      (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                    ctx->Pixel.DriverReadBuffer );
-      if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
-         ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
-      else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT)
-         ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
-      else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT)
-         ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
-      else
-         ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
+      /* choose the read buffer */
+      _swrast_use_read_buffer(ctx);
    }
 
    /* read source image */
@@ -167,7 +151,7 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    for (row = 0; row < height; row++) {
       GLchan rgba[MAX_WIDTH][4];
       GLint i;
-      _mesa_read_rgba_span(ctx, ctx->ReadBuffer, width, srcx, srcy + row, rgba);
+      _swrast_read_rgba_span(ctx, ctx->ReadBuffer, width, srcx, srcy + row, rgba);
       /* convert GLchan to GLfloat */
       for (i = 0; i < width; i++) {
          *dest++ = (GLfloat) rgba[i][RCOMP] * (1.0F / CHAN_MAXF);
@@ -177,33 +161,17 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       }
    }
 
-   /* read from the draw buffer again (in case of blending) */
    if (changeBuffer) {
-      (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                    ctx->Color.DriverDrawBuffer );
-      ctx->ReadBuffer->Alpha = saveReadAlpha;
+      /* restore default src/dst buffer */
+      _swrast_use_draw_buffer(ctx);
    }
 
-   /* do image transfer ops up until convolution */
+   /* do the image transfer ops which preceed convolution */
    for (row = 0; row < height; row++) {
       GLfloat (*rgba)[4] = (GLfloat (*)[4]) (tmpImage + row * width * 4);
-
-      /* scale & bias */
-      if (transferOps & IMAGE_SCALE_BIAS_BIT) {
-         _mesa_scale_and_bias_rgba(ctx, width, rgba,
-                                   ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
-                                   ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
-                                   ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
-                                   ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
-      }
-      /* color map lookup */
-      if (transferOps & IMAGE_MAP_COLOR_BIT) {
-         _mesa_map_rgba(ctx, width, rgba);
-      }
-      /* GL_COLOR_TABLE lookup */
-      if (transferOps & IMAGE_COLOR_TABLE_BIT) {
-         _mesa_lookup_rgba(&ctx->ColorTable, width, rgba);
-      }
+      _mesa_apply_rgba_transfer_ops(ctx,
+                                    transferOps & IMAGE_PRE_CONVOLUTION_BITS,
+                                    width, rgba);
    }
 
    /* do convolution */
@@ -216,32 +184,15 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    }
    FREE(tmpImage);
 
-   /* do remaining image transfer ops */
+   /* do remaining post-convolution image transfer ops */
    for (row = 0; row < height; row++) {
       GLfloat (*rgba)[4] = (GLfloat (*)[4]) (convImage + row * width * 4);
-
-      /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
-      if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
-         _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, width, rgba);
-      }
-      /* color matrix */
-      if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
-         _mesa_transform_rgba(ctx, width, rgba);
-      }
-      /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
-      if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
-         _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, width, rgba);
-      }
-      /* update histogram count */
-      if (transferOps & IMAGE_HISTOGRAM_BIT) {
-         _mesa_update_histogram(ctx, width, (CONST GLfloat (*)[4]) rgba);
-      }
-      /* update min/max */
-      if (transferOps & IMAGE_MIN_MAX_BIT) {
-         _mesa_update_minmax(ctx, width, (CONST GLfloat (*)[4]) rgba);
-      }
+      _mesa_apply_rgba_transfer_ops(ctx,
+                                    transferOps & IMAGE_POST_CONVOLUTION_BITS,
+                                    width, rgba);
    }
 
+   /* write the new image */
    for (row = 0; row < height; row++) {
       const GLfloat *src = convImage + row * width * 4;
       GLint i, dy;
@@ -252,13 +203,13 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
          GLint g = (GLint) (src[i * 4 + GCOMP] * CHAN_MAXF);
          GLint b = (GLint) (src[i * 4 + BCOMP] * CHAN_MAXF);
          GLint a = (GLint) (src[i * 4 + ACOMP] * CHAN_MAXF);
-         span.color.rgba[i][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
-         span.color.rgba[i][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
-         span.color.rgba[i][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
-         span.color.rgba[i][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
+         span.array->rgba[i][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
+         span.array->rgba[i][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
+         span.array->rgba[i][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
+         span.array->rgba[i][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
       }
 
-      if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._ReallyEnabled) {
+      if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._EnabledUnits) {
          span.end = width;
          _swrast_pixel_texture(ctx, &span);
       }
@@ -266,23 +217,23 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       /* write row to framebuffer */
 
       dy = desty + row;
-      if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
+      if (quick_draw && dy >= 0 && dy < (GLint) ctx->DrawBuffer->Height) {
          (*swrast->Driver.WriteRGBASpan)( ctx, width, destx, dy,
-                      (const GLchan (*)[4])span.color.rgba, NULL );
+                      (const GLchan (*)[4])span.array->rgba, NULL );
       }
       else if (zoom) {
          span.x = destx;
          span.y = dy;
          span.end = width;
-         _mesa_write_zoomed_rgba_span(ctx, &span, 
-                                     (CONST GLchan (*)[4])span.color.rgba,
-                                     desty);
+         _swrast_write_zoomed_rgba_span(ctx, &span, 
+                                     (CONST GLchan (*)[4])span.array->rgba,
+                                     desty, 0);
       }
       else {
          span.x = destx;
          span.y = dy;
          span.end = width;
-         _mesa_write_rgba_span(ctx, &span);
+         _swrast_write_rgba_span(ctx, &span);
       }
    }
 
@@ -302,14 +253,12 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    GLboolean quick_draw;
    GLint sy, dy, stepy, j;
    GLboolean changeBuffer;
-   GLchan *saveReadAlpha;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    GLint overlapping;
    const GLuint transferOps = ctx->_ImageTransferState;
    struct sw_span span;
 
    INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
-   /*span.arrayMask |= SPAN_RGBA;*/
 
    if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
       copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
@@ -330,18 +279,23 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   }
+   else {
+      overlapping = GL_FALSE;
+   }
 
    if (ctx->Depth.Test)
-      _mesa_span_default_z(ctx, &span);
+      _swrast_span_default_z(ctx, &span);
    if (ctx->Fog.Enabled)
-      _mesa_span_default_fog(ctx, &span);
+      _swrast_span_default_fog(ctx, &span);
 
    if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
        && !zoom
        && destx >= 0
-       && destx + width <= ctx->DrawBuffer->Width) {
+       && destx + width <= (GLint) ctx->DrawBuffer->Width) {
       quick_draw = GL_TRUE;
    }
    else {
@@ -349,13 +303,9 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    }
 
    /* If read and draw buffer are different we must do buffer switching */
-   saveReadAlpha = ctx->ReadBuffer->Alpha;
    changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
                   || ctx->DrawBuffer != ctx->ReadBuffer;
 
-   (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                 ctx->Pixel.DriverReadBuffer );
-
    if (overlapping) {
       GLint ssy = sy;
       tmpImage = (GLchan *) MALLOC(width * height * sizeof(GLchan) * 4);
@@ -363,25 +313,22 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
          _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
+      /* setup source */
+      if (changeBuffer)
+         _swrast_use_read_buffer(ctx);
+      /* read the source image */
       p = tmpImage;
-      if (changeBuffer) {
-         (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                       ctx->Pixel.DriverReadBuffer );
-         if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
-            ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
-         else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT)
-            ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
-         else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT)
-            ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
-         else
-            ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
-      }
       for (j = 0; j < height; j++, ssy += stepy) {
-         _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, ssy,
+         _swrast_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, ssy,
                             (GLchan (*)[4]) p );
          p += width * 4;
       }
       p = tmpImage;
+      /* restore dest */
+      if (changeBuffer) {
+         _swrast_use_draw_buffer(ctx);
+         changeBuffer = GL_FALSE;
+      }
    }
    else {
       tmpImage = NULL;  /* silence compiler warnings */
@@ -392,35 +339,19 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       /* Get source pixels */
       if (overlapping) {
          /* get from buffered image */
-         MEMCPY(span.color.rgba, p, width * sizeof(GLchan) * 4);
+         ASSERT(width < MAX_WIDTH);
+         MEMCPY(span.array->rgba, p, width * sizeof(GLchan) * 4);
          p += width * 4;
       }
       else {
          /* get from framebuffer */
-         if (changeBuffer) {
-            (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                          ctx->Pixel.DriverReadBuffer );
-            if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT) {
-               ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
-            }
-            else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT) {
-               ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
-            }
-            else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT) {
-               ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
-            }
-            else {
-               ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
-            }
-         }
-         _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, sy, span.color.rgba );
-      }
-
-      if (changeBuffer) {
-         /* read from the draw buffer again (in case of blending) */
-         (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                       ctx->Color.DriverDrawBuffer );
-         ctx->ReadBuffer->Alpha = saveReadAlpha;
+         if (changeBuffer)
+            _swrast_use_read_buffer(ctx);
+         ASSERT(width < MAX_WIDTH);
+         _swrast_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, sy,
+                               span.array->rgba );
+         if (changeBuffer)
+            _swrast_use_draw_buffer(ctx);
       }
 
       if (transferOps) {
@@ -431,116 +362,64 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
          /* convert chan to float */
          for (k = 0; k < width; k++) {
-            rgbaFloat[k][RCOMP] = (GLfloat) span.color.rgba[k][RCOMP] * scale;
-            rgbaFloat[k][GCOMP] = (GLfloat) span.color.rgba[k][GCOMP] * scale;
-            rgbaFloat[k][BCOMP] = (GLfloat) span.color.rgba[k][BCOMP] * scale;
-            rgbaFloat[k][ACOMP] = (GLfloat) span.color.rgba[k][ACOMP] * scale;
-         }
-         /* scale & bias */
-         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
-            _mesa_scale_and_bias_rgba(ctx, width, rgbaFloat,
-                                   ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
-                                   ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
-                                   ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
-                                   ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
-         }
-         /* color map lookup */
-         if (transferOps & IMAGE_MAP_COLOR_BIT) {
-            _mesa_map_rgba(ctx, width, rgbaFloat);
-         }
-         /* GL_COLOR_TABLE lookup */
-         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
-            _mesa_lookup_rgba(&ctx->ColorTable, width, rgbaFloat);
-         }
-         /* convolution */
-         if (transferOps & IMAGE_CONVOLUTION_BIT) {
-            abort();  /* should never get here; caught at top of function */
-         }
-         /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
-         if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
-            _mesa_scale_and_bias_rgba(ctx, width, rgbaFloat,
-                                      ctx->Pixel.PostConvolutionScale[RCOMP],
-                                      ctx->Pixel.PostConvolutionScale[GCOMP],
-                                      ctx->Pixel.PostConvolutionScale[BCOMP],
-                                      ctx->Pixel.PostConvolutionScale[ACOMP],
-                                      ctx->Pixel.PostConvolutionBias[RCOMP],
-                                      ctx->Pixel.PostConvolutionBias[GCOMP],
-                                      ctx->Pixel.PostConvolutionBias[BCOMP],
-                                      ctx->Pixel.PostConvolutionBias[ACOMP]);
-         }
-         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
-         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
-            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, width, rgbaFloat);
-         }
-         /* color matrix */
-         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
-            _mesa_transform_rgba(ctx, width, rgbaFloat);
-         }
-         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
-         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
-            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, width, rgbaFloat);
-         }
-         /* update histogram count */
-         if (transferOps & IMAGE_HISTOGRAM_BIT) {
-            _mesa_update_histogram(ctx, width, (CONST GLfloat (*)[4]) rgbaFloat);
-         }
-         /* update min/max */
-         if (transferOps & IMAGE_MIN_MAX_BIT) {
-            _mesa_update_minmax(ctx, width, (CONST GLfloat (*)[4]) rgbaFloat);
+            rgbaFloat[k][RCOMP] = (GLfloat) span.array->rgba[k][RCOMP] * scale;
+            rgbaFloat[k][GCOMP] = (GLfloat) span.array->rgba[k][GCOMP] * scale;
+            rgbaFloat[k][BCOMP] = (GLfloat) span.array->rgba[k][BCOMP] * scale;
+            rgbaFloat[k][ACOMP] = (GLfloat) span.array->rgba[k][ACOMP] * scale;
          }
+
+         _mesa_apply_rgba_transfer_ops(ctx, transferOps, width, rgbaFloat);
+
          /* clamp to [0,1] and convert float back to chan */
          for (k = 0; k < width; k++) {
             GLint r = (GLint) (rgbaFloat[k][RCOMP] * CHAN_MAXF);
             GLint g = (GLint) (rgbaFloat[k][GCOMP] * CHAN_MAXF);
             GLint b = (GLint) (rgbaFloat[k][BCOMP] * CHAN_MAXF);
             GLint a = (GLint) (rgbaFloat[k][ACOMP] * CHAN_MAXF);
-            span.color.rgba[k][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
-            span.color.rgba[k][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
-            span.color.rgba[k][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
-            span.color.rgba[k][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
+            span.array->rgba[k][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
+            span.array->rgba[k][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
+            span.array->rgba[k][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
+            span.array->rgba[k][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
          }
          UNDEFARRAY(rgbaFloat);  /* mac 32k limitation */
       }
 
-      if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._ReallyEnabled) {
+      if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._EnabledUnits) {
          span.end = width;
          _swrast_pixel_texture(ctx, &span);
       }
 
-      if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
+      /* Write color span */
+      if (quick_draw && dy >= 0 && dy < (GLint) ctx->DrawBuffer->Height) {
          (*swrast->Driver.WriteRGBASpan)( ctx, width, destx, dy,
-                                      (const GLchan (*)[4])span.color.rgba, NULL );
+                                 (const GLchan (*)[4])span.array->rgba, NULL );
       }
       else if (zoom) {
          span.x = destx;
          span.y = dy;
          span.end = width;
-         _mesa_write_zoomed_rgba_span(ctx, &span,
-                                     (CONST GLchan (*)[4]) span.color.rgba,
-                                     desty);
+         _swrast_write_zoomed_rgba_span(ctx, &span,
+                                     (CONST GLchan (*)[4]) span.array->rgba,
+                                     desty, 0);
       }
       else {
          span.x = destx;
          span.y = dy;
          span.end = width;
-         _mesa_write_rgba_span(ctx, &span);
+         _swrast_write_rgba_span(ctx, &span);
       }
    }
 
-   /* Restore pixel source to be the draw buffer (for blending, etc) */
-   (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                 ctx->Color.DriverDrawBuffer );
-
    if (overlapping)
       FREE(tmpImage);
 }
 
 
-static void copy_ci_pixels( GLcontext *ctx,
-                            GLint srcx, GLint srcy, GLint width, GLint height,
-                            GLint destx, GLint desty )
+static void
+copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
+                GLint width, GLint height,
+                GLint destx, GLint desty )
 {
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    GLuint *tmpImage,*p;
    GLint sy, dy, stepy;
    GLint j;
@@ -551,7 +430,6 @@ static void copy_ci_pixels( GLcontext *ctx,
    struct sw_span span;
 
    INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_INDEX);
-   /*span.arrayMask |= SPAN_INDEX;*/
 
    /* Determine if copy should be bottom-to-top or top-to-bottom */
    if (srcy<desty) {
@@ -567,21 +445,23 @@ static void copy_ci_pixels( GLcontext *ctx,
       stepy = 1;
    }
 
-   overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   }
+   else {
+      overlapping = GL_FALSE;
+   }
 
    if (ctx->Depth.Test)
-      _mesa_span_default_z(ctx, &span);
+      _swrast_span_default_z(ctx, &span);
    if (ctx->Fog.Enabled)
-      _mesa_span_default_fog(ctx, &span);
+      _swrast_span_default_fog(ctx, &span);
 
    /* If read and draw buffer are different we must do buffer switching */
    changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
                || ctx->DrawBuffer != ctx->ReadBuffer;
 
-   (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                 ctx->Pixel.DriverReadBuffer );
-
    if (overlapping) {
       GLint ssy = sy;
       tmpImage = (GLuint *) MALLOC(width * height * sizeof(GLuint));
@@ -589,16 +469,21 @@ static void copy_ci_pixels( GLcontext *ctx,
          _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
+      /* setup source */
+      if (changeBuffer)
+         _swrast_use_read_buffer(ctx);
+      /* read the image */
       p = tmpImage;
-      if (changeBuffer) {
-         (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                       ctx->Pixel.DriverReadBuffer );
-      }
       for (j = 0; j < height; j++, ssy += stepy) {
-         _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, ssy, p );
+         _swrast_read_index_span( ctx, ctx->ReadBuffer, width, srcx, ssy, p );
          p += width;
       }
       p = tmpImage;
+      /* restore to draw buffer */
+      if (changeBuffer) {
+         _swrast_use_draw_buffer(ctx);
+         changeBuffer = GL_FALSE;
+      }
    }
    else {
       tmpImage = NULL;  /* silence compiler warning */
@@ -606,45 +491,38 @@ static void copy_ci_pixels( GLcontext *ctx,
    }
 
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
+      /* Get color indexes */
       if (overlapping) {
-         MEMCPY(span.color.index, p, width * sizeof(GLuint));
+         MEMCPY(span.array->index, p, width * sizeof(GLuint));
          p += width;
       }
       else {
-         if (changeBuffer) {
-            (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                          ctx->Pixel.DriverReadBuffer );
-         }
-         _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy,
-                                span.color.index );
-      }
-
-      if (changeBuffer) {
-         /* set read buffer back to draw buffer (in case of logicops) */
-         (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                       ctx->Color.DriverDrawBuffer );
+         if (changeBuffer)
+            _swrast_use_read_buffer(ctx);
+         _swrast_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy,
+                                span.array->index );
+         if (changeBuffer)
+            _swrast_use_draw_buffer(ctx);
       }
 
+      /* Apply shift, offset, look-up table */
       if (shift_or_offset) {
-         _mesa_shift_and_offset_ci( ctx, width, span.color.index );
+         _mesa_shift_and_offset_ci( ctx, width, span.array->index );
       }
       if (ctx->Pixel.MapColorFlag) {
-         _mesa_map_ci( ctx, width, span.color.index );
+         _mesa_map_ci( ctx, width, span.array->index );
       }
 
+      /* write color indexes */
       span.x = destx;
       span.y = dy;
       span.end = width;
       if (zoom)
-         _mesa_write_zoomed_index_span(ctx, &span, desty);
+         _swrast_write_zoomed_index_span(ctx, &span, desty, 0);
       else
-         _mesa_write_index_span(ctx, &span);
+         _swrast_write_index_span(ctx, &span);
    }
 
-   /* Restore pixel source to be the draw buffer (for blending, etc) */
-   (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                 ctx->Color.DriverDrawBuffer );
-
    if (overlapping)
       FREE(tmpImage);
 }
@@ -654,11 +532,11 @@ static void copy_ci_pixels( GLcontext *ctx,
 /*
  * TODO: Optimize!!!!
  */
-static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
-                               GLint width, GLint height,
-                               GLint destx, GLint desty )
+static void
+copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
+                   GLint width, GLint height,
+                   GLint destx, GLint desty )
 {
-   GLfloat depth[MAX_WIDTH];
    GLfloat *p, *tmpImage;
    GLint sy, dy, stepy;
    GLint i, j;
@@ -667,7 +545,6 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
    struct sw_span span;
 
    INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_Z);
-   /*span.arrayMask |= SPAN_Z;*/
 
    if (!ctx->Visual.depthBits) {
       _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
@@ -688,12 +565,17 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   }
+   else {
+      overlapping = GL_FALSE;
+   }
 
-   _mesa_span_default_color(ctx, &span);
+   _swrast_span_default_color(ctx, &span);
    if (ctx->Fog.Enabled)
-      _mesa_span_default_fog(ctx, &span);
+      _swrast_span_default_fog(ctx, &span);
 
    if (overlapping) {
       GLint ssy = sy;
@@ -704,7 +586,7 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       }
       p = tmpImage;
       for (j = 0; j < height; j++, ssy += stepy) {
-         _mesa_read_depth_span_float(ctx, width, srcx, ssy, p);
+         _swrast_read_depth_span_float(ctx, width, srcx, ssy, p);
          p += width;
       }
       p = tmpImage;
@@ -715,35 +597,39 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
    }
 
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
+      GLfloat depth[MAX_WIDTH];
+
+      /* get depth values */
       if (overlapping) {
          MEMCPY(depth, p, width * sizeof(GLfloat));
          p += width;
       }
       else {
-         _mesa_read_depth_span_float(ctx, width, srcx, sy, depth);
+         _swrast_read_depth_span_float(ctx, width, srcx, sy, depth);
       }
 
+      /* apply scale and bias */
       for (i = 0; i < width; i++) {
          GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
-         span.zArray[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * ctx->DepthMax);
+         span.array->z[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * ctx->DepthMax);
       }
 
+      /* write depth values */
       span.x = destx;
       span.y = dy;
       span.end = width;
       if (ctx->Visual.rgbMode) {
          if (zoom)
-            _mesa_write_zoomed_rgba_span( ctx, &span, 
-                                          (const GLchan (*)[4])span.color.rgba,
-                                          desty );
+            _swrast_write_zoomed_rgba_span( ctx, &span, 
+                            (const GLchan (*)[4])span.array->rgba, desty, 0 );
          else
-            _mesa_write_rgba_span(ctx, &span);
+            _swrast_write_rgba_span(ctx, &span);
       }
       else {
          if (zoom)
-            _mesa_write_zoomed_index_span( ctx, &span, desty );
+            _swrast_write_zoomed_index_span( ctx, &span, desty, 0 );
          else
-            _mesa_write_index_span(ctx, &span);
+            _swrast_write_index_span(ctx, &span);
       }
    }
 
@@ -753,9 +639,10 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
 
 
 
-static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
-                                 GLint width, GLint height,
-                                 GLint destx, GLint desty )
+static void
+copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
+                     GLint width, GLint height,
+                     GLint destx, GLint desty )
 {
    GLint sy, dy, stepy;
    GLint j;
@@ -783,8 +670,13 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   }
+   else {
+      overlapping = GL_FALSE;
+   }
 
    if (overlapping) {
       GLint ssy = sy;
@@ -795,7 +687,7 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       }
       p = tmpImage;
       for (j = 0; j < height; j++, ssy += stepy) {
-         _mesa_read_stencil_span( ctx, width, srcx, ssy, p );
+         _swrast_read_stencil_span( ctx, width, srcx, ssy, p );
          p += width;
       }
       p = tmpImage;
@@ -808,14 +700,16 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
       GLstencil stencil[MAX_WIDTH];
 
+      /* Get stencil values */
       if (overlapping) {
          MEMCPY(stencil, p, width * sizeof(GLstencil));
          p += width;
       }
       else {
-         _mesa_read_stencil_span( ctx, width, srcx, sy, stencil );
+         _swrast_read_stencil_span( ctx, width, srcx, sy, stencil );
       }
 
+      /* Apply shift, offset, look-up table */
       if (shift_or_offset) {
          _mesa_shift_and_offset_stencil( ctx, width, stencil );
       }
@@ -823,11 +717,13 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
          _mesa_map_stencil( ctx, width, stencil );
       }
 
+      /* Write stencil values */
       if (zoom) {
-         _mesa_write_zoomed_stencil_span( ctx, width, destx, dy, stencil, desty );
+         _swrast_write_zoomed_stencil_span( ctx, width, destx, dy,
+                                          stencil, desty, 0 );
       }
       else {
-         _mesa_write_stencil_span( ctx, width, destx, dy, stencil );
+         _swrast_write_stencil_span( ctx, width, destx, dy, stencil );
       }
    }
 
@@ -837,7 +733,6 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
 
 
 
-
 void
 _swrast_CopyPixels( GLcontext *ctx,
                    GLint srcx, GLint srcy, GLsizei width, GLsizei height,