r300: move some more function to generic
[mesa.git] / src / mesa / swrast / s_imaging.c
index 672d163d8cf33a7dd778fae34616e7ea22a4a8cd..591857c3423e4f29d0c26b78dd530c152b27e510 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: s_imaging.c,v 1.1 2000/10/31 18:00:04 keithw Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.4
+ * Version:  6.5
  *
- * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2005  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"),
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
-
-/*
- * Histogram, Min/max and convolution for GL_ARB_imaging subset
- *
+/* KW:  Moved these here to remove knowledge of swrast from core mesa.
+ * Should probably pull the entire software implementation of these
+ * extensions into either swrast or a sister module.  
  */
 
+#include "main/glheader.h"
+#include "main/colortab.h"
+#include "main/convolve.h"
+#include "s_context.h"
+#include "s_span.h"
 
-#include "glheader.h"
-#include "colormac.h"
-#include "image.h"
-#include "mmath.h"
 
-#include "s_imaging.h"
-#include "s_span.h"
+void
+_swrast_CopyColorTable( GLcontext *ctx, 
+                       GLenum target, GLenum internalformat,
+                       GLint x, GLint y, GLsizei width)
+{
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   GLchan data[MAX_WIDTH][4];
+   struct gl_buffer_object *bufferSave;
 
+   if (!ctx->ReadBuffer->_ColorReadBuffer) {
+      /* no readbuffer - OK */
+      return;
+   }
+
+   if (width > MAX_WIDTH)
+      width = MAX_WIDTH;
+
+   RENDER_START( swrast, ctx );
+
+   /* read the data from framebuffer */
+   _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                           width, x, y, CHAN_TYPE, data );
+
+   RENDER_FINISH(swrast,ctx);
+
+   /* save PBO binding */
+   bufferSave = ctx->Unpack.BufferObj;
+   ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
+
+   _mesa_ColorTable(target, internalformat, width, GL_RGBA, CHAN_TYPE, data);
+
+   /* restore PBO binding */
+   ctx->Unpack.BufferObj = bufferSave;
+}
 
 
-/*
- * Update the min/max values from an array of fragment colors.
- */
 void
-_mesa_update_minmax(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
+_swrast_CopyColorSubTable( GLcontext *ctx,GLenum target, GLsizei start,
+                          GLint x, GLint y, GLsizei width)
 {
-   GLuint i;
-   for (i = 0; i < n; i++) {
-      /* update mins */
-      if (rgba[i][RCOMP] < ctx->MinMax.Min[RCOMP])
-         ctx->MinMax.Min[RCOMP] = rgba[i][RCOMP];
-      if (rgba[i][GCOMP] < ctx->MinMax.Min[GCOMP])
-         ctx->MinMax.Min[GCOMP] = rgba[i][GCOMP];
-      if (rgba[i][BCOMP] < ctx->MinMax.Min[BCOMP])
-         ctx->MinMax.Min[BCOMP] = rgba[i][BCOMP];
-      if (rgba[i][ACOMP] < ctx->MinMax.Min[ACOMP])
-         ctx->MinMax.Min[ACOMP] = rgba[i][ACOMP];
-
-      /* update maxs */
-      if (rgba[i][RCOMP] > ctx->MinMax.Max[RCOMP])
-         ctx->MinMax.Max[RCOMP] = rgba[i][RCOMP];
-      if (rgba[i][GCOMP] > ctx->MinMax.Max[GCOMP])
-         ctx->MinMax.Max[GCOMP] = rgba[i][GCOMP];
-      if (rgba[i][BCOMP] > ctx->MinMax.Max[BCOMP])
-         ctx->MinMax.Max[BCOMP] = rgba[i][BCOMP];
-      if (rgba[i][ACOMP] > ctx->MinMax.Max[ACOMP])
-         ctx->MinMax.Max[ACOMP] = rgba[i][ACOMP];
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   GLchan data[MAX_WIDTH][4];
+   struct gl_buffer_object *bufferSave;
+
+   if (!ctx->ReadBuffer->_ColorReadBuffer) {
+      /* no readbuffer - OK */
+      return;
    }
+
+   if (width > MAX_WIDTH)
+      width = MAX_WIDTH;
+
+   RENDER_START( swrast, ctx );
+
+   /* read the data from framebuffer */
+   _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                           width, x, y, CHAN_TYPE, data );
+
+   RENDER_FINISH(swrast,ctx);
+
+   /* save PBO binding */
+   bufferSave = ctx->Unpack.BufferObj;
+   ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
+
+   _mesa_ColorSubTable(target, start, width, GL_RGBA, CHAN_TYPE, data);
+
+   /* restore PBO binding */
+   ctx->Unpack.BufferObj = bufferSave;
 }
 
 
-/*
- * Update the histogram values from an array of fragment colors.
- */
 void
-_mesa_update_histogram(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
+_swrast_CopyConvolutionFilter1D(GLcontext *ctx, GLenum target, 
+                               GLenum internalFormat, 
+                               GLint x, GLint y, GLsizei width)
 {
-   const GLint max = ctx->Histogram.Width - 1;
-   GLfloat w = (GLfloat) max;
-   GLuint i;
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   GLchan rgba[MAX_CONVOLUTION_WIDTH][4];
+   struct gl_buffer_object *bufferSave;
 
-   if (ctx->Histogram.Width == 0)
+   if (!ctx->ReadBuffer->_ColorReadBuffer) {
+      /* no readbuffer - OK */
       return;
-   
-   for (i = 0; i < n; i++) {
-      GLint ri = (GLint) (rgba[i][RCOMP] * w + 0.5F);
-      GLint gi = (GLint) (rgba[i][GCOMP] * w + 0.5F);
-      GLint bi = (GLint) (rgba[i][BCOMP] * w + 0.5F);
-      GLint ai = (GLint) (rgba[i][ACOMP] * w + 0.5F);
-      ri = CLAMP(ri, 0, max);
-      gi = CLAMP(gi, 0, max);
-      bi = CLAMP(bi, 0, max);
-      ai = CLAMP(ai, 0, max);
-      ctx->Histogram.Count[ri][RCOMP]++;
-      ctx->Histogram.Count[gi][GCOMP]++;
-      ctx->Histogram.Count[bi][BCOMP]++;
-      ctx->Histogram.Count[ai][ACOMP]++;
    }
+
+   RENDER_START( swrast, ctx );
+
+   /* read the data from framebuffer */
+   _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                           width, x, y, CHAN_TYPE, rgba );
+   
+   RENDER_FINISH( swrast, ctx );
+
+   /* save PBO binding */
+   bufferSave = ctx->Unpack.BufferObj;
+   ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
+
+   /* store as convolution filter */
+   _mesa_ConvolutionFilter1D(target, internalFormat, width,
+                             GL_RGBA, CHAN_TYPE, rgba);
+
+   /* restore PBO binding */
+   ctx->Unpack.BufferObj = bufferSave;
 }
 
 
+void
+_swrast_CopyConvolutionFilter2D(GLcontext *ctx, GLenum target, 
+                               GLenum internalFormat, 
+                               GLint x, GLint y, GLsizei width, GLsizei height)
+{
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   struct gl_pixelstore_attrib packSave;
+   GLchan rgba[MAX_CONVOLUTION_HEIGHT][MAX_CONVOLUTION_WIDTH][4];
+   GLint i;
+   struct gl_buffer_object *bufferSave;
+
+   if (!ctx->ReadBuffer->_ColorReadBuffer) {
+      /* no readbuffer - OK */
+      return;
+   }
+
+   RENDER_START(swrast,ctx);
+   
+   /* read pixels from framebuffer */
+   for (i = 0; i < height; i++) {
+      _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                              width, x, y + i, CHAN_TYPE, rgba[i] );
+   }
+
+   RENDER_FINISH(swrast,ctx);
+
+   /*
+    * HACK: save & restore context state so we can store this as a
+    * convolution filter via the GL api.  Doesn't call any callbacks
+    * hanging off ctx->Unpack statechanges.
+    */
 
+   packSave = ctx->Unpack;  /* save pixel packing params */
+
+   ctx->Unpack.Alignment = 1;
+   ctx->Unpack.RowLength = MAX_CONVOLUTION_WIDTH;
+   ctx->Unpack.SkipPixels = 0;
+   ctx->Unpack.SkipRows = 0;
+   ctx->Unpack.ImageHeight = 0;
+   ctx->Unpack.SkipImages = 0;
+   ctx->Unpack.SwapBytes = GL_FALSE;
+   ctx->Unpack.LsbFirst = GL_FALSE;
+   ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
+   ctx->NewState |= _NEW_PACKUNPACK;
+
+   /* save PBO binding */
+   bufferSave = ctx->Unpack.BufferObj;
+   ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
+
+   _mesa_ConvolutionFilter2D(target, internalFormat, width, height,
+                             GL_RGBA, CHAN_TYPE, rgba);
+
+   /* restore PBO binding */
+   ctx->Unpack.BufferObj = bufferSave;
+
+   ctx->Unpack = packSave;  /* restore pixel packing params */
+   ctx->NewState |= _NEW_PACKUNPACK; 
+}