remove unused var
[mesa.git] / progs / demos / readpix.c
index 0277a46a037fe0614cce80d5b747dd2ed3be395a..c0aac2272f7598e6aacebc2e11edb2d0fe1e3587 100644 (file)
@@ -1,4 +1,3 @@
-/* $Id: readpix.c,v 1.7 2002/05/02 09:17:59 alanh Exp $ */
 
 /*
  * glReadPixels and glCopyPixels test
@@ -10,9 +9,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
+#include <string.h>
 #include <GL/glut.h>
 
-#include "../util/readtex.c"  /* a hack, I know */
+#include "readtex.h"
 
 #define IMAGE_FILE "../images/girl.rgb"
 
@@ -29,29 +29,32 @@ static GLboolean ScaleAndBias = GL_FALSE;
 static GLboolean Benchmark = GL_FALSE;
 static GLubyte *TempImage = NULL;
 
-#if 0
+#define COMBO 1
+#if COMBO == 0
 #define ReadFormat ImgFormat
 #define ReadType GL_UNSIGNED_BYTE
-#endif
-#if 1
+#elif COMBO == 1
 static GLenum ReadFormat = GL_RGBA;
 static GLenum ReadType = GL_UNSIGNED_BYTE;
-#endif
-#if 0
+#elif COMBO == 2
+static GLenum ReadFormat = GL_RGB;
+static GLenum ReadType = GL_UNSIGNED_BYTE;
+#elif COMBO == 3
 static GLenum ReadFormat = GL_RGB;
 static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5;
-#endif
-#if 0
+#elif COMBO == 4
 static GLenum ReadFormat = GL_RGBA;
 static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
-#endif
-#if 0
+#elif COMBO == 5
 static GLenum ReadFormat = GL_BGRA;
 static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1;
-#endif
-#if 0
+#elif COMBO == 6
 static GLenum ReadFormat = GL_BGRA;
 static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV;
+#elif COMBO == 7
+static GLenum ReadFormat = GL_RGBA;
+static GLenum ReadType = GL_HALF_FLOAT_ARB;
+#undef GL_OES_read_format
 #endif
 
 
@@ -96,6 +99,45 @@ SetupPixelTransfer(GLboolean invert)
 }
 
 
+/**
+ * Exercise Pixel Pack parameters by reading the image in four pieces.
+ */
+static void
+ComplexReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
+                  GLenum format, GLenum type, GLvoid *pixels)
+{
+   const GLsizei width0 = width / 2;
+   const GLsizei width1 = width - width0;
+   const GLsizei height0 = height / 2;
+   const GLsizei height1 = height - height0;
+
+   glPixelStorei(GL_PACK_ROW_LENGTH, width);
+
+   /* lower-left quadrant */
+   glReadPixels(x, y, width0, height0, format, type, pixels);
+
+   /* lower-right quadrant */
+   glPixelStorei(GL_PACK_SKIP_PIXELS, width0);
+   glReadPixels(x + width0, y, width1, height0, format, type, pixels);
+
+   /* upper-left quadrant */
+   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
+   glPixelStorei(GL_PACK_SKIP_ROWS, height0);
+   glReadPixels(x, y + height0, width0, height1, format, type, pixels);
+
+   /* upper-right quadrant */
+   glPixelStorei(GL_PACK_SKIP_PIXELS, width0);
+   glPixelStorei(GL_PACK_SKIP_ROWS, height0);
+   glReadPixels(x + width0, y + height0, width1, height1, format, type, pixels);
+
+   /* restore defaults */
+   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
+   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
+   glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
+}
+
+
+
 static void
 Display( void )
 {
@@ -111,8 +153,13 @@ Display( void )
    glRasterPos2i(APosX, APosY);
    glEnable(GL_DITHER);
    SetupPixelTransfer(GL_FALSE);
+   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
 
+   /* might try alignment=4 here for testing */
+   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+   glPixelStorei(GL_PACK_ALIGNMENT, 1);
+
    /* do readpixels, drawpixels */
    glRasterPos2i(BPosX, 5);
    PrintString("Read/DrawPixels");
@@ -138,8 +185,15 @@ Display( void )
    else {
       /* clear the temporary image to white (helpful for debugging */
       memset(TempImage, 255, ImgWidth * ImgHeight * 4);
+#if 1
       glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
                    ReadFormat, ReadType, TempImage);
+      (void) ComplexReadPixels;
+#else
+      /* you might use this when debugging */
+      ComplexReadPixels(APosX, APosY, ImgWidth, ImgHeight,
+                        ReadFormat, ReadType, TempImage);
+#endif
    }
    glRasterPos2i(BPosX, BPosY);
    glDisable(GL_DITHER);
@@ -208,6 +262,8 @@ Key( unsigned char key, int x, int y )
 static void
 Init( GLboolean ciMode )
 {
+   GLboolean have_read_format = GL_FALSE;
+
    printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
    printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
 
@@ -219,7 +275,7 @@ Init( GLboolean ciMode )
 
    if (ciMode) {
       /* Convert RGB image to grayscale */
-      GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
+      GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
       GLint i;
       for (i=0; i<ImgWidth*ImgHeight; i++) {
          int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
@@ -235,16 +291,32 @@ Init( GLboolean ciMode )
       }
    }
 
+#ifdef GL_OES_read_format
+   if ( glutExtensionSupported( "GL_OES_read_format" ) ) {
+      glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES,   (GLint *) &ReadType);
+      glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, (GLint *) &ReadFormat);
+
+      have_read_format = GL_TRUE;
+   }
+#endif
+
+   printf( "GL_OES_read_format %ssupported.  "
+          "Using type / format = 0x%04x / 0x%04x\n",
+          (have_read_format) ? "" : "not ",
+          ReadType, ReadFormat );
+
    printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
 
-   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
-   glPixelStorei(GL_PACK_ALIGNMENT, 1);
    glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
 
    Reset();
 
-   TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * sizeof(GLubyte));
+   /* allocate large TempImage to store and image data type, plus an
+    * extra 1KB in case we're tinkering with pack alignment.
+    */
+   TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * 4
+                                  + 1000);
    assert(TempImage);
 }