fixed pointer arithmetic error in glCopyPixels
[mesa.git] / src / mesa / swrast / s_copypix.c
index f85af995d32323a9055e7efc530d4f544b12aa44..5f8c35daaab20ff9fd34646007ba59337a0641ff 100644 (file)
@@ -1,10 +1,10 @@
-/* $Id: s_copypix.c,v 1.16 2001/03/19 02:25:36 keithw Exp $ */
+/* $Id: s_copypix.c,v 1.34 2002/03/14 22:09:25 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
+ * Version:  4.1
  *
- * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2002  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"),
@@ -66,6 +66,9 @@ regions_overlap(GLint srcx, GLint srcy,
       else if (srcy < dsty) { /* this is OK */
          return GL_FALSE;
       }
+      else if (srcy > dsty + height) {
+         return GL_FALSE;
+      }
       else {
          return GL_TRUE;
       }
@@ -97,7 +100,6 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
                       GLint width, GLint height, GLint destx, GLint desty)
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   GLdepth zspan[MAX_WIDTH];
    GLboolean quick_draw;
    GLint row;
    GLboolean changeBuffer;
@@ -105,16 +107,16 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    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);
+   span.arrayMask |= SPAN_RGBA;
+
+   if (ctx->Depth.Test)
+      _mesa_span_default_z(ctx, &span);
+   if (ctx->Fog.Enabled)
+      _mesa_span_default_fog(ctx, &span);
 
-   if (ctx->Depth.Test || ctx->Fog.Enabled) {
-      /* fill in array of z values */
-      GLdepth z = (GLdepth)
-         (ctx->Current.RasterPos[2] * ctx->DepthMax);
-      GLint i;
-      for (i = 0; i < width; i++) {
-         zspan[i] = z;
-      }
-   }
 
    if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
        && !zoom
@@ -184,7 +186,7 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
    /* do image transfer ops up until convolution */
    for (row = 0; row < height; row++) {
-      GLfloat (*rgba)[4] = (GLfloat (*)[4]) tmpImage + row * width * 4;
+      GLfloat (*rgba)[4] = (GLfloat (*)[4]) (tmpImage + row * width * 4);
 
       /* scale & bias */
       if (transferOps & IMAGE_SCALE_BIAS_BIT) {
@@ -216,7 +218,7 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
    /* do remaining image transfer ops */
    for (row = 0; row < height; row++) {
-      GLfloat (*rgba)[4] = (GLfloat (*)[4]) convImage + row * width * 4;
+      GLfloat (*rgba)[4] = (GLfloat (*)[4]) (convImage + row * width * 4);
 
       /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
       if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
@@ -242,7 +244,6 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
    for (row = 0; row < height; row++) {
       const GLfloat *src = convImage + row * width * 4;
-      GLchan rgba[MAX_WIDTH][4];
       GLint i, dy;
 
       /* clamp to [0,1] and convert float back to chan */
@@ -251,27 +252,15 @@ 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);
-         rgba[i][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
-         rgba[i][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
-         rgba[i][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
-         rgba[i][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
-      }
-
-      if (ctx->Texture._ReallyEnabled && ctx->Pixel.PixelTextureEnabled) {
-         GLfloat s[MAX_WIDTH], t[MAX_WIDTH], r[MAX_WIDTH], q[MAX_WIDTH];
-         GLchan primary_rgba[MAX_WIDTH][4];
-         GLuint unit;
-         /* XXX not sure how multitexture is supposed to work here */
-
-         MEMCPY(primary_rgba, rgba, 4 * width * sizeof(GLchan));
-
-         for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
-            _mesa_pixeltexgen(ctx, width, (const GLchan (*)[4]) rgba,
-                              s, t, r, q);
-            _swrast_texture_fragments(ctx, unit, width, s, t, r, NULL,
-                                      (CONST GLchan (*)[4]) primary_rgba,
-                                      rgba);
-         }
+         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);
+      }
+
+      if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._ReallyEnabled) {
+         span.end = width;
+         _swrast_pixel_texture(ctx, &span);
       }
 
       /* write row to framebuffer */
@@ -279,14 +268,21 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       dy = desty + row;
       if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
          (*swrast->Driver.WriteRGBASpan)( ctx, width, destx, dy,
-                                      (const GLchan (*)[4])rgba, NULL );
+                      (const GLchan (*)[4])span.color.rgba, NULL );
       }
       else if (zoom) {
-         _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, 0,
-                                   (const GLchan (*)[4])rgba, desty);
+         span.x = destx;
+         span.y = dy;
+         span.end = width;
+         _mesa_write_zoomed_rgba_span(ctx, &span, 
+                                     (CONST GLchan (*)[4])span.color.rgba,
+                                     desty);
       }
       else {
-         _mesa_write_rgba_span( ctx, width, destx, dy, zspan, 0, rgba, GL_BITMAP );
+         span.x = destx;
+         span.y = dy;
+         span.end = width;
+         _mesa_write_rgba_span(ctx, &span, GL_BITMAP);
       }
    }
 
@@ -302,17 +298,18 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
                  GLint width, GLint height, GLint destx, GLint desty)
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   GLdepth zspan[MAX_WIDTH];
-   GLchan rgba[MAX_WIDTH][4];
    GLchan *tmpImage,*p;
    GLboolean quick_draw;
-   GLint sy, dy, stepy;
-   GLint i, j;
+   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);
+   span.arrayMask |= SPAN_RGBA;
 
    if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
       copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
@@ -336,13 +333,10 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
                                  ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
 
-   if (ctx->Depth.Test || ctx->Fog.Enabled) {
-      /* fill in array of z values */
-      GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->DepthMax);
-      for (i=0;i<width;i++) {
-         zspan[i] = z;
-      }
-   }
+   if (ctx->Depth.Test)
+      _mesa_span_default_z(ctx, &span);
+   if (ctx->Fog.Enabled)
+      _mesa_span_default_fog(ctx, &span);
 
    if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
        && !zoom
@@ -385,7 +379,7 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       for (j = 0; j < height; j++, ssy += stepy) {
          _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, ssy,
                             (GLchan (*)[4]) p );
-         p += (width * sizeof(GLchan) * 4);
+         p += width * 4;
       }
       p = tmpImage;
    }
@@ -398,8 +392,8 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       /* Get source pixels */
       if (overlapping) {
          /* get from buffered image */
-         MEMCPY(rgba, p, width * sizeof(GLchan) * 4);
-         p += (width * sizeof(GLchan) * 4);
+         MEMCPY(span.color.rgba, p, width * sizeof(GLchan) * 4);
+         p += width * 4;
       }
       else {
          /* get from framebuffer */
@@ -419,7 +413,7 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
                ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
             }
          }
-         _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, sy, rgba );
+         _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, sy, span.color.rgba );
       }
 
       if (changeBuffer) {
@@ -431,14 +425,16 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
       if (transferOps) {
          const GLfloat scale = (1.0F / CHAN_MAXF);
-         GLfloat rgbaFloat[MAX_WIDTH][4];
          GLint k;
+         DEFMARRAY(GLfloat, rgbaFloat, MAX_WIDTH, 4);  /* mac 32k limitation */
+         CHECKARRAY(rgbaFloat, return);
+
          /* convert chan to float */
          for (k = 0; k < width; k++) {
-            rgbaFloat[k][RCOMP] = (GLfloat) rgba[k][RCOMP] * scale;
-            rgbaFloat[k][GCOMP] = (GLfloat) rgba[k][GCOMP] * scale;
-            rgbaFloat[k][BCOMP] = (GLfloat) rgba[k][BCOMP] * scale;
-            rgbaFloat[k][ACOMP] = (GLfloat) rgba[k][ACOMP] * scale;
+            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) {
@@ -458,7 +454,7 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
          }
          /* convolution */
          if (transferOps & IMAGE_CONVOLUTION_BIT) {
-            /* XXX to do */
+            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) {
@@ -498,40 +494,36 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
             GLint g = (GLint) (rgbaFloat[k][GCOMP] * CHAN_MAXF);
             GLint b = (GLint) (rgbaFloat[k][BCOMP] * CHAN_MAXF);
             GLint a = (GLint) (rgbaFloat[k][ACOMP] * CHAN_MAXF);
-            rgba[k][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
-            rgba[k][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
-            rgba[k][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
-            rgba[k][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
+            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);
          }
+         UNDEFARRAY(rgbaFloat);  /* mac 32k limitation */
       }
 
-      if (ctx->Texture._ReallyEnabled && ctx->Pixel.PixelTextureEnabled) {
-         GLfloat s[MAX_WIDTH], t[MAX_WIDTH], r[MAX_WIDTH], q[MAX_WIDTH];
-         GLchan primary_rgba[MAX_WIDTH][4];
-         GLuint unit;
-         /* XXX not sure how multitexture is supposed to work here */
-
-         MEMCPY(primary_rgba, rgba, 4 * width * sizeof(GLchan));
-
-         for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
-            _mesa_pixeltexgen(ctx, width, (const GLchan (*)[4]) rgba,
-                              s, t, r, q);
-            _swrast_texture_fragments(ctx, unit, width, s, t, r, NULL,
-                                      (CONST GLchan (*)[4]) primary_rgba,
-                                      rgba);
-         }
+      if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._ReallyEnabled) {
+         span.end = width;
+         _swrast_pixel_texture(ctx, &span);
       }
 
       if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
          (*swrast->Driver.WriteRGBASpan)( ctx, width, destx, dy,
-                                      (const GLchan (*)[4])rgba, NULL );
+                                      (const GLchan (*)[4])span.color.rgba, NULL );
       }
       else if (zoom) {
-         _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, 0,
-                                   (const GLchan (*)[4])rgba, desty);
+         span.x = destx;
+         span.y = dy;
+         span.end = width;
+         _mesa_write_zoomed_rgba_span(ctx, &span,
+                                     (CONST GLchan (*)[4]) span.color.rgba,
+                                     desty);
       }
       else {
-         _mesa_write_rgba_span( ctx, width, destx, dy, zspan, 0, rgba, GL_BITMAP );
+         span.x = destx;
+         span.y = dy;
+         span.end = width;
+         _mesa_write_rgba_span(ctx, &span, GL_BITMAP);
       }
    }
 
@@ -549,14 +541,17 @@ static void copy_ci_pixels( GLcontext *ctx,
                             GLint destx, GLint desty )
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   GLdepth zspan[MAX_WIDTH];
    GLuint *tmpImage,*p;
    GLint sy, dy, stepy;
-   GLint i, j;
+   GLint j;
    GLboolean changeBuffer;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
    GLint overlapping;
+   struct sw_span span;
+
+   INIT_SPAN(span);
+   span.arrayMask |= SPAN_INDEX;
 
    /* Determine if copy should be bottom-to-top or top-to-bottom */
    if (srcy<desty) {
@@ -575,13 +570,10 @@ static void copy_ci_pixels( GLcontext *ctx,
    overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
                                  ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
 
-   if (ctx->Depth.Test || ctx->Fog.Enabled) {
-      /* fill in array of z values */
-      GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->DepthMax);
-      for (i=0;i<width;i++) {
-         zspan[i] = z;
-      }
-   }
+   if (ctx->Depth.Test)
+      _mesa_span_default_z(ctx, &span);
+   if (ctx->Fog.Enabled)
+      _mesa_span_default_fog(ctx, &span);
 
    /* If read and draw buffer are different we must do buffer switching */
    changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
@@ -614,9 +606,8 @@ static void copy_ci_pixels( GLcontext *ctx,
    }
 
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
-      GLuint indexes[MAX_WIDTH];
       if (overlapping) {
-         MEMCPY(indexes, p, width * sizeof(GLuint));
+         MEMCPY(span.color.index, p, width * sizeof(GLuint));
          p += width;
       }
       else {
@@ -624,7 +615,8 @@ static void copy_ci_pixels( GLcontext *ctx,
             (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
                                           ctx->Pixel.DriverReadBuffer );
          }
-         _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy, indexes );
+         _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy,
+                                span.color.index );
       }
 
       if (changeBuffer) {
@@ -634,18 +626,19 @@ static void copy_ci_pixels( GLcontext *ctx,
       }
 
       if (shift_or_offset) {
-         _mesa_shift_and_offset_ci( ctx, width, indexes );
+         _mesa_shift_and_offset_ci( ctx, width, span.color.index );
       }
       if (ctx->Pixel.MapColorFlag) {
-         _mesa_map_ci( ctx, width, indexes );
+         _mesa_map_ci( ctx, width, span.color.index );
       }
 
-      if (zoom) {
-         _mesa_write_zoomed_index_span( ctx, width, destx, dy, zspan, 0, indexes, desty );
-      }
-      else {
-         _mesa_write_index_span(ctx, width, destx, dy, zspan, 0, indexes, GL_BITMAP);
-      }
+      span.x = destx;
+      span.y = dy;
+      span.end = width;
+      if (zoom)
+         _mesa_write_zoomed_index_span(ctx, &span, desty);
+      else
+         _mesa_write_index_span(ctx, &span, GL_BITMAP);
    }
 
    /* Restore pixel source to be the draw buffer (for blending, etc) */
@@ -666,14 +659,15 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
                                GLint destx, GLint desty )
 {
    GLfloat depth[MAX_WIDTH];
-   GLdepth zspan[MAX_WIDTH];
    GLfloat *p, *tmpImage;
-   GLuint indexes[MAX_WIDTH];
-   GLchan rgba[MAX_WIDTH][4];
    GLint sy, dy, stepy;
    GLint i, j;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    GLint overlapping;
+   struct sw_span span;
+
+   INIT_SPAN(span);
+   span.arrayMask |= SPAN_Z;
 
    if (!ctx->Visual.depthBits) {
       _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
@@ -697,19 +691,9 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
    overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
                                  ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
 
-   /* setup colors or indexes */
-   if (ctx->Visual.rgbMode) {
-      GLuint *rgba32 = (GLuint *) rgba;
-      GLuint color = *(GLuint*)( ctx->Current.Color );
-      for (i = 0; i < width; i++) {
-         rgba32[i] = color;
-      }
-   }
-   else {
-      for (i = 0; i < width; i++) {
-         indexes[i] = ctx->Current.Index;
-      }
-   }
+   _mesa_span_default_color(ctx, &span);
+   if (ctx->Fog.Enabled)
+      _mesa_span_default_fog(ctx, &span);
 
    if (overlapping) {
       GLint ssy = sy;
@@ -741,33 +725,30 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
 
       for (i = 0; i < width; i++) {
          GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
-         zspan[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * ctx->DepthMax);
+         span.zArray[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * ctx->DepthMax);
       }
 
+      span.x = destx;
+      span.y = dy;
+      span.end = width;
       if (ctx->Visual.rgbMode) {
-         if (zoom) {
-            _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, 0,
-                                      (const GLchan (*)[4])rgba, desty );
-         }
-         else {
-            _mesa_write_rgba_span( ctx, width, destx, dy, zspan, 0,
-                               rgba, GL_BITMAP);
-         }
+         if (zoom)
+            _mesa_write_zoomed_rgba_span( ctx, &span, 
+                                          (const GLchan (*)[4])span.color.rgba,
+                                          desty );
+         else
+            _mesa_write_rgba_span(ctx, &span, GL_BITMAP);
       }
       else {
-         if (zoom) {
-            _mesa_write_zoomed_index_span( ctx, width, destx, dy,
-                                        zspan, 0, indexes, desty );
-         }
-         else {
-            _mesa_write_index_span( ctx, width, destx, dy,
-                                 zspan, 0, indexes, GL_BITMAP );
-         }
+         if (zoom)
+            _mesa_write_zoomed_index_span( ctx, &span, desty );
+         else
+            _mesa_write_index_span(ctx, &span, GL_BITMAP);
       }
    }
 
-  if (overlapping)
-     FREE(tmpImage);
+   if (overlapping)
+      FREE(tmpImage);
 }