Alan Hourihane | a5cdf99 | 2002-05-02 09:17:59 +0000 | [diff] [blame] | 1 | /* $Id: readpix.c,v 1.7 2002/05/02 09:17:59 alanh Exp $ */ |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * glReadPixels and glCopyPixels test |
| 5 | * |
| 6 | * Brian Paul March 1, 2000 This file is in the public domain. |
| 7 | */ |
| 8 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <math.h> |
| 13 | #include <GL/glut.h> |
| 14 | |
| 15 | #include "../util/readtex.c" /* a hack, I know */ |
| 16 | |
| 17 | #define IMAGE_FILE "../images/girl.rgb" |
| 18 | |
| 19 | static int ImgWidth, ImgHeight; |
| 20 | static GLenum ImgFormat; |
| 21 | static GLubyte *Image = NULL; |
| 22 | |
| 23 | static int APosX, APosY; /* simple drawpixels */ |
| 24 | static int BPosX, BPosY; /* read/draw pixels */ |
| 25 | static int CPosX, CPosY; /* copypixels */ |
| 26 | |
| 27 | static GLboolean DrawFront = GL_FALSE; |
| 28 | static GLboolean ScaleAndBias = GL_FALSE; |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 29 | static GLboolean Benchmark = GL_FALSE; |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 30 | static GLubyte *TempImage = NULL; |
| 31 | |
Brian Paul | 1100b4d | 2000-10-16 21:24:39 +0000 | [diff] [blame] | 32 | #if 0 |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 33 | #define ReadFormat ImgFormat |
| 34 | #define ReadType GL_UNSIGNED_BYTE |
| 35 | #endif |
Brian Paul | 1100b4d | 2000-10-16 21:24:39 +0000 | [diff] [blame] | 36 | #if 1 |
Alan Hourihane | a5cdf99 | 2002-05-02 09:17:59 +0000 | [diff] [blame] | 37 | static GLenum ReadFormat = GL_RGBA; |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 38 | static GLenum ReadType = GL_UNSIGNED_BYTE; |
| 39 | #endif |
| 40 | #if 0 |
| 41 | static GLenum ReadFormat = GL_RGB; |
| 42 | static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5; |
| 43 | #endif |
Brian Paul | 1100b4d | 2000-10-16 21:24:39 +0000 | [diff] [blame] | 44 | #if 0 |
| 45 | static GLenum ReadFormat = GL_RGBA; |
| 46 | static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV; |
| 47 | #endif |
| 48 | #if 0 |
| 49 | static GLenum ReadFormat = GL_BGRA; |
| 50 | static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1; |
| 51 | #endif |
| 52 | #if 0 |
| 53 | static GLenum ReadFormat = GL_BGRA; |
| 54 | static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV; |
| 55 | #endif |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 56 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 57 | |
| 58 | static void |
| 59 | Reset( void ) |
| 60 | { |
| 61 | APosX = 5; APosY = 20; |
| 62 | BPosX = APosX + ImgWidth + 5; BPosY = 20; |
| 63 | CPosX = BPosX + ImgWidth + 5; CPosY = 20; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | static void |
| 68 | PrintString(const char *s) |
| 69 | { |
| 70 | while (*s) { |
| 71 | glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s); |
| 72 | s++; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static void |
| 78 | SetupPixelTransfer(GLboolean invert) |
| 79 | { |
| 80 | if (invert) { |
| 81 | glPixelTransferf(GL_RED_SCALE, -1.0); |
| 82 | glPixelTransferf(GL_RED_BIAS, 1.0); |
| 83 | glPixelTransferf(GL_GREEN_SCALE, -1.0); |
| 84 | glPixelTransferf(GL_GREEN_BIAS, 1.0); |
| 85 | glPixelTransferf(GL_BLUE_SCALE, -1.0); |
| 86 | glPixelTransferf(GL_BLUE_BIAS, 1.0); |
| 87 | } |
| 88 | else { |
| 89 | glPixelTransferf(GL_RED_SCALE, 1.0); |
| 90 | glPixelTransferf(GL_RED_BIAS, 0.0); |
| 91 | glPixelTransferf(GL_GREEN_SCALE, 1.0); |
| 92 | glPixelTransferf(GL_GREEN_BIAS, 0.0); |
| 93 | glPixelTransferf(GL_BLUE_SCALE, 1.0); |
| 94 | glPixelTransferf(GL_BLUE_BIAS, 0.0); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static void |
| 100 | Display( void ) |
| 101 | { |
Brian Paul | 1100b4d | 2000-10-16 21:24:39 +0000 | [diff] [blame] | 102 | glClearColor(.3, .3, .3, 1); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 103 | glClear( GL_COLOR_BUFFER_BIT ); |
| 104 | |
| 105 | glRasterPos2i(5, ImgHeight+25); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 106 | PrintString("f = toggle front/back s = toggle scale/bias b = benchmark"); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 107 | |
| 108 | /* draw original image */ |
| 109 | glRasterPos2i(APosX, 5); |
| 110 | PrintString("Original"); |
| 111 | glRasterPos2i(APosX, APosY); |
| 112 | glEnable(GL_DITHER); |
| 113 | SetupPixelTransfer(GL_FALSE); |
| 114 | glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image); |
| 115 | |
| 116 | /* do readpixels, drawpixels */ |
| 117 | glRasterPos2i(BPosX, 5); |
| 118 | PrintString("Read/DrawPixels"); |
| 119 | SetupPixelTransfer(ScaleAndBias); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 120 | if (Benchmark) { |
| 121 | GLint reads = 0; |
| 122 | GLint endTime; |
| 123 | GLint startTime = glutGet(GLUT_ELAPSED_TIME); |
| 124 | GLdouble seconds, pixelsPerSecond; |
| 125 | printf("Benchmarking...\n"); |
| 126 | do { |
| 127 | glReadPixels(APosX, APosY, ImgWidth, ImgHeight, |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 128 | ReadFormat, ReadType, TempImage); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 129 | reads++; |
| 130 | endTime = glutGet(GLUT_ELAPSED_TIME); |
| 131 | } while (endTime - startTime < 4000); /* 4 seconds */ |
| 132 | seconds = (double) (endTime - startTime) / 1000.0; |
| 133 | pixelsPerSecond = reads * ImgWidth * ImgHeight / seconds; |
| 134 | printf("Result: %d reads in %f seconds = %f pixels/sec\n", |
| 135 | reads, seconds, pixelsPerSecond); |
| 136 | Benchmark = GL_FALSE; |
| 137 | } |
| 138 | else { |
Brian Paul | 1100b4d | 2000-10-16 21:24:39 +0000 | [diff] [blame] | 139 | /* clear the temporary image to white (helpful for debugging */ |
| 140 | memset(TempImage, 255, ImgWidth * ImgHeight * 4); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 141 | glReadPixels(APosX, APosY, ImgWidth, ImgHeight, |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 142 | ReadFormat, ReadType, TempImage); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 143 | } |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 144 | glRasterPos2i(BPosX, BPosY); |
| 145 | glDisable(GL_DITHER); |
| 146 | SetupPixelTransfer(GL_FALSE); |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 147 | glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 148 | |
| 149 | /* do copypixels */ |
| 150 | glRasterPos2i(CPosX, 5); |
| 151 | PrintString("CopyPixels"); |
| 152 | glRasterPos2i(CPosX, CPosY); |
| 153 | glDisable(GL_DITHER); |
| 154 | SetupPixelTransfer(ScaleAndBias); |
Alan Hourihane | a5cdf99 | 2002-05-02 09:17:59 +0000 | [diff] [blame] | 155 | glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 156 | |
| 157 | if (!DrawFront) |
| 158 | glutSwapBuffers(); |
Alan Hourihane | 056b358 | 2002-05-02 09:15:22 +0000 | [diff] [blame] | 159 | else |
| 160 | glFinish(); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 164 | static void |
| 165 | Reshape( int width, int height ) |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 166 | { |
| 167 | glViewport( 0, 0, width, height ); |
| 168 | glMatrixMode( GL_PROJECTION ); |
| 169 | glLoadIdentity(); |
| 170 | glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 ); |
| 171 | glMatrixMode( GL_MODELVIEW ); |
| 172 | glLoadIdentity(); |
| 173 | } |
| 174 | |
| 175 | |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 176 | static void |
| 177 | Key( unsigned char key, int x, int y ) |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 178 | { |
| 179 | (void) x; |
| 180 | (void) y; |
| 181 | switch (key) { |
| 182 | case 'b': |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 183 | Benchmark = GL_TRUE; |
| 184 | break; |
| 185 | case 's': |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 186 | ScaleAndBias = !ScaleAndBias; |
| 187 | break; |
| 188 | case 'f': |
| 189 | DrawFront = !DrawFront; |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 190 | if (DrawFront) { |
Alan Hourihane | a5cdf99 | 2002-05-02 09:17:59 +0000 | [diff] [blame] | 191 | glDrawBuffer(GL_FRONT); |
| 192 | glReadBuffer(GL_FRONT); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 193 | } |
| 194 | else { |
Alan Hourihane | a5cdf99 | 2002-05-02 09:17:59 +0000 | [diff] [blame] | 195 | glDrawBuffer(GL_BACK); |
| 196 | glReadBuffer(GL_BACK); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 197 | } |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 198 | printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK"); |
| 199 | break; |
| 200 | case 27: |
| 201 | exit(0); |
| 202 | break; |
| 203 | } |
| 204 | glutPostRedisplay(); |
| 205 | } |
| 206 | |
| 207 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 208 | static void |
| 209 | Init( GLboolean ciMode ) |
| 210 | { |
| 211 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 212 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 213 | |
| 214 | Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat ); |
| 215 | if (!Image) { |
| 216 | printf("Couldn't read %s\n", IMAGE_FILE); |
| 217 | exit(0); |
| 218 | } |
| 219 | |
| 220 | if (ciMode) { |
| 221 | /* Convert RGB image to grayscale */ |
| 222 | GLubyte *indexImage = malloc( ImgWidth * ImgHeight ); |
| 223 | GLint i; |
| 224 | for (i=0; i<ImgWidth*ImgHeight; i++) { |
| 225 | int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2]; |
| 226 | indexImage[i] = gray / 3; |
| 227 | } |
| 228 | free(Image); |
| 229 | Image = indexImage; |
| 230 | ImgFormat = GL_COLOR_INDEX; |
| 231 | |
| 232 | for (i=0;i<255;i++) { |
| 233 | float g = i / 255.0; |
| 234 | glutSetColor(i, g, g, g); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | printf("Loaded %d by %d image\n", ImgWidth, ImgHeight ); |
| 239 | |
| 240 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 241 | glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth); |
| 242 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 243 | glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth); |
| 244 | |
| 245 | Reset(); |
| 246 | |
| 247 | TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * sizeof(GLubyte)); |
| 248 | assert(TempImage); |
| 249 | } |
| 250 | |
| 251 | |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 252 | int |
| 253 | main( int argc, char *argv[] ) |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 254 | { |
| 255 | GLboolean ciMode = GL_FALSE; |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 256 | if (argc > 1 && strcmp(argv[1], "-ci")==0) { |
| 257 | ciMode = GL_TRUE; |
| 258 | } |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 259 | glutInit( &argc, argv ); |
| 260 | glutInitWindowPosition( 0, 0 ); |
| 261 | glutInitWindowSize( 750, 250 ); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 262 | if (ciMode) |
| 263 | glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE ); |
| 264 | else |
| 265 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 266 | glutCreateWindow(argv[0]); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 267 | Init(ciMode); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 268 | glutReshapeFunc( Reshape ); |
| 269 | glutKeyboardFunc( Key ); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 270 | glutDisplayFunc( Display ); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 271 | glutMainLoop(); |
| 272 | return 0; |
| 273 | } |