Consolidate texObj->Pos/Neg/X/Y/Z and texObj->Image into a single
[mesa.git] / src / mesa / swrast / s_readpix.c
index 5fc03da27d72759457c939af3aeba9d7d6d84d6f..f2b91e0528259f9954f865179d4627e9bee4223d 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: s_readpix.c,v 1.12 2001/05/16 20:27:12 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
+ * Version:  5.1
  *
- * Copyright (C) 1999-2001  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"),
@@ -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"
@@ -62,9 +60,18 @@ read_index_pixels( GLcontext *ctx,
       return;
    }
 
-   ASSERT(swrast->Driver.SetReadBuffer);
-   (*swrast->Driver.SetReadBuffer)(ctx, ctx->ReadBuffer,
-                                ctx->Pixel.DriverReadBuffer);
+   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;
+   }
+
+   _swrast_use_read_buffer(ctx);
 
    readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
 
@@ -82,8 +89,7 @@ read_index_pixels( GLcontext *ctx,
                             &ctx->Pack, ctx->_ImageTransferState);
    }
 
-   (*swrast->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer,
-                                ctx->Color.DriverDrawBuffer);
+   _swrast_use_draw_buffer(ctx);
 }
 
 
@@ -129,7 +135,7 @@ read_depth_pixels( GLcontext *ctx,
          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];
       }
@@ -141,7 +147,7 @@ read_depth_pixels( GLcontext *ctx,
       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 {
@@ -151,7 +157,7 @@ read_depth_pixels( GLcontext *ctx,
          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);
 
          dest = _mesa_image_address(packing, pixels, width, height,
                                     GL_DEPTH_COMPONENT, type, 0, j, 0);
@@ -198,7 +204,7 @@ read_stencil_pixels( GLcontext *ctx,
       GLvoid *dest;
       GLstencil stencil[MAX_WIDTH];
 
-      _mesa_read_stencil_span(ctx, readWidth, x, y, 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);
@@ -246,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;
 
@@ -282,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++) {
             (*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;
@@ -318,13 +331,13 @@ read_rgba_pixels( GLcontext *ctx,
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
    GLint readWidth;
 
-   (*swrast->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 )) {
 
-      (*swrast->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer, ctx->Color.DriverDrawBuffer);
+      _swrast_use_draw_buffer(ctx);
       return; /* done! */
    }
 
@@ -386,7 +399,7 @@ read_rgba_pixels( GLcontext *ctx,
       for (row = 0; row < height; row++, y++) {
          GLchan rgba[MAX_WIDTH][4];
          if (ctx->Visual.rgbMode) {
-            _mesa_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
+            _swrast_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
          }
          else {
             GLuint index[MAX_WIDTH];
@@ -432,7 +445,7 @@ read_rgba_pixels( GLcontext *ctx,
          GLchan rgba[MAX_WIDTH][4];
          GLvoid *dst;
          if (ctx->Visual.rgbMode) {
-            _mesa_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
+            _swrast_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
          }
          else {
             GLuint index[MAX_WIDTH];
@@ -470,7 +483,7 @@ read_rgba_pixels( GLcontext *ctx,
       }
    }
 
-   (*swrast->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer, ctx->Color.DriverDrawBuffer);
+   _swrast_use_draw_buffer(ctx);
 }