Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * glReadPixels and glCopyPixels test |
| 4 | * |
| 5 | * Brian Paul March 1, 2000 This file is in the public domain. |
| 6 | */ |
| 7 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 8 | #include <assert.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <math.h> |
Brian Paul | 2d84ed8 | 2005-01-09 17:39:06 +0000 | [diff] [blame] | 12 | #include <string.h> |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 13 | #include <GL/glut.h> |
| 14 | |
Brian Paul | 0fe7f40 | 2005-01-09 17:06:22 +0000 | [diff] [blame] | 15 | #include "readtex.h" |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 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; |
Brian Paul | 8818eae | 2005-05-18 15:44:13 +0000 | [diff] [blame] | 42 | static GLenum ReadType = GL_UNSIGNED_BYTE; |
| 43 | #endif |
| 44 | #if 0 |
| 45 | static GLenum ReadFormat = GL_RGB; |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 46 | static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5; |
| 47 | #endif |
Brian Paul | 1100b4d | 2000-10-16 21:24:39 +0000 | [diff] [blame] | 48 | #if 0 |
| 49 | static GLenum ReadFormat = GL_RGBA; |
| 50 | static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV; |
| 51 | #endif |
| 52 | #if 0 |
| 53 | static GLenum ReadFormat = GL_BGRA; |
| 54 | static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1; |
| 55 | #endif |
| 56 | #if 0 |
| 57 | static GLenum ReadFormat = GL_BGRA; |
| 58 | static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV; |
| 59 | #endif |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 60 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 61 | |
| 62 | static void |
| 63 | Reset( void ) |
| 64 | { |
| 65 | APosX = 5; APosY = 20; |
| 66 | BPosX = APosX + ImgWidth + 5; BPosY = 20; |
| 67 | CPosX = BPosX + ImgWidth + 5; CPosY = 20; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | static void |
| 72 | PrintString(const char *s) |
| 73 | { |
| 74 | while (*s) { |
| 75 | glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s); |
| 76 | s++; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static void |
| 82 | SetupPixelTransfer(GLboolean invert) |
| 83 | { |
| 84 | if (invert) { |
| 85 | glPixelTransferf(GL_RED_SCALE, -1.0); |
| 86 | glPixelTransferf(GL_RED_BIAS, 1.0); |
| 87 | glPixelTransferf(GL_GREEN_SCALE, -1.0); |
| 88 | glPixelTransferf(GL_GREEN_BIAS, 1.0); |
| 89 | glPixelTransferf(GL_BLUE_SCALE, -1.0); |
| 90 | glPixelTransferf(GL_BLUE_BIAS, 1.0); |
| 91 | } |
| 92 | else { |
| 93 | glPixelTransferf(GL_RED_SCALE, 1.0); |
| 94 | glPixelTransferf(GL_RED_BIAS, 0.0); |
| 95 | glPixelTransferf(GL_GREEN_SCALE, 1.0); |
| 96 | glPixelTransferf(GL_GREEN_BIAS, 0.0); |
| 97 | glPixelTransferf(GL_BLUE_SCALE, 1.0); |
| 98 | glPixelTransferf(GL_BLUE_BIAS, 0.0); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
Brian Paul | 8818eae | 2005-05-18 15:44:13 +0000 | [diff] [blame] | 103 | /** |
| 104 | * Exercise Pixel Pack parameters by reading the image in four pieces. |
| 105 | */ |
| 106 | static void |
| 107 | ComplexReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, |
| 108 | GLenum format, GLenum type, GLvoid *pixels) |
| 109 | { |
| 110 | const GLsizei width0 = width / 2; |
| 111 | const GLsizei width1 = width - width0; |
| 112 | const GLsizei height0 = height / 2; |
| 113 | const GLsizei height1 = height - height0; |
| 114 | |
| 115 | glPixelStorei(GL_PACK_ROW_LENGTH, width); |
| 116 | |
| 117 | /* lower-left quadrant */ |
| 118 | glReadPixels(x, y, width0, height0, format, type, pixels); |
| 119 | |
| 120 | /* lower-right quadrant */ |
| 121 | glPixelStorei(GL_PACK_SKIP_PIXELS, width0); |
| 122 | glReadPixels(x + width0, y, width1, height0, format, type, pixels); |
| 123 | |
| 124 | /* upper-left quadrant */ |
| 125 | glPixelStorei(GL_PACK_SKIP_PIXELS, 0); |
| 126 | glPixelStorei(GL_PACK_SKIP_ROWS, height0); |
| 127 | glReadPixels(x, y + height0, width0, height1, format, type, pixels); |
| 128 | |
| 129 | /* upper-right quadrant */ |
| 130 | glPixelStorei(GL_PACK_SKIP_PIXELS, width0); |
| 131 | glPixelStorei(GL_PACK_SKIP_ROWS, height0); |
| 132 | glReadPixels(x + width0, y + height0, width1, height1, format, type, pixels); |
| 133 | |
| 134 | /* restore defaults */ |
| 135 | glPixelStorei(GL_PACK_SKIP_PIXELS, 0); |
| 136 | glPixelStorei(GL_PACK_SKIP_ROWS, 0); |
| 137 | glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 142 | static void |
| 143 | Display( void ) |
| 144 | { |
Brian Paul | 1100b4d | 2000-10-16 21:24:39 +0000 | [diff] [blame] | 145 | glClearColor(.3, .3, .3, 1); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 146 | glClear( GL_COLOR_BUFFER_BIT ); |
| 147 | |
| 148 | glRasterPos2i(5, ImgHeight+25); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 149 | PrintString("f = toggle front/back s = toggle scale/bias b = benchmark"); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 150 | |
| 151 | /* draw original image */ |
| 152 | glRasterPos2i(APosX, 5); |
| 153 | PrintString("Original"); |
| 154 | glRasterPos2i(APosX, APosY); |
| 155 | glEnable(GL_DITHER); |
| 156 | SetupPixelTransfer(GL_FALSE); |
Brian Paul | 8818eae | 2005-05-18 15:44:13 +0000 | [diff] [blame] | 157 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 158 | glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image); |
| 159 | |
Brian Paul | 8818eae | 2005-05-18 15:44:13 +0000 | [diff] [blame] | 160 | /* might try alignment=4 here for testing */ |
| 161 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 162 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 163 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 164 | /* do readpixels, drawpixels */ |
| 165 | glRasterPos2i(BPosX, 5); |
| 166 | PrintString("Read/DrawPixels"); |
| 167 | SetupPixelTransfer(ScaleAndBias); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 168 | if (Benchmark) { |
| 169 | GLint reads = 0; |
| 170 | GLint endTime; |
| 171 | GLint startTime = glutGet(GLUT_ELAPSED_TIME); |
| 172 | GLdouble seconds, pixelsPerSecond; |
| 173 | printf("Benchmarking...\n"); |
| 174 | do { |
| 175 | glReadPixels(APosX, APosY, ImgWidth, ImgHeight, |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 176 | ReadFormat, ReadType, TempImage); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 177 | reads++; |
| 178 | endTime = glutGet(GLUT_ELAPSED_TIME); |
| 179 | } while (endTime - startTime < 4000); /* 4 seconds */ |
| 180 | seconds = (double) (endTime - startTime) / 1000.0; |
| 181 | pixelsPerSecond = reads * ImgWidth * ImgHeight / seconds; |
| 182 | printf("Result: %d reads in %f seconds = %f pixels/sec\n", |
| 183 | reads, seconds, pixelsPerSecond); |
| 184 | Benchmark = GL_FALSE; |
| 185 | } |
| 186 | else { |
Brian Paul | 1100b4d | 2000-10-16 21:24:39 +0000 | [diff] [blame] | 187 | /* clear the temporary image to white (helpful for debugging */ |
| 188 | memset(TempImage, 255, ImgWidth * ImgHeight * 4); |
Brian Paul | 713635a | 2005-08-25 22:09:43 +0000 | [diff] [blame^] | 189 | #if 1 |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 190 | glReadPixels(APosX, APosY, ImgWidth, ImgHeight, |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 191 | ReadFormat, ReadType, TempImage); |
Brian Paul | 713635a | 2005-08-25 22:09:43 +0000 | [diff] [blame^] | 192 | (void) ComplexReadPixels; |
Brian Paul | 8818eae | 2005-05-18 15:44:13 +0000 | [diff] [blame] | 193 | #else |
Brian Paul | 713635a | 2005-08-25 22:09:43 +0000 | [diff] [blame^] | 194 | /* you might use this when debugging */ |
Brian Paul | 8818eae | 2005-05-18 15:44:13 +0000 | [diff] [blame] | 195 | ComplexReadPixels(APosX, APosY, ImgWidth, ImgHeight, |
| 196 | ReadFormat, ReadType, TempImage); |
| 197 | #endif |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 198 | } |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 199 | glRasterPos2i(BPosX, BPosY); |
| 200 | glDisable(GL_DITHER); |
| 201 | SetupPixelTransfer(GL_FALSE); |
Brian Paul | d3d7280 | 2000-03-31 01:01:31 +0000 | [diff] [blame] | 202 | glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 203 | |
| 204 | /* do copypixels */ |
| 205 | glRasterPos2i(CPosX, 5); |
| 206 | PrintString("CopyPixels"); |
| 207 | glRasterPos2i(CPosX, CPosY); |
| 208 | glDisable(GL_DITHER); |
| 209 | SetupPixelTransfer(ScaleAndBias); |
Alan Hourihane | a5cdf99 | 2002-05-02 09:17:59 +0000 | [diff] [blame] | 210 | glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 211 | |
| 212 | if (!DrawFront) |
| 213 | glutSwapBuffers(); |
Alan Hourihane | 056b358 | 2002-05-02 09:15:22 +0000 | [diff] [blame] | 214 | else |
| 215 | glFinish(); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 219 | static void |
| 220 | Reshape( int width, int height ) |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 221 | { |
| 222 | glViewport( 0, 0, width, height ); |
| 223 | glMatrixMode( GL_PROJECTION ); |
| 224 | glLoadIdentity(); |
| 225 | glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 ); |
| 226 | glMatrixMode( GL_MODELVIEW ); |
| 227 | glLoadIdentity(); |
| 228 | } |
| 229 | |
| 230 | |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 231 | static void |
| 232 | Key( unsigned char key, int x, int y ) |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 233 | { |
| 234 | (void) x; |
| 235 | (void) y; |
| 236 | switch (key) { |
| 237 | case 'b': |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 238 | Benchmark = GL_TRUE; |
| 239 | break; |
| 240 | case 's': |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 241 | ScaleAndBias = !ScaleAndBias; |
| 242 | break; |
| 243 | case 'f': |
| 244 | DrawFront = !DrawFront; |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 245 | if (DrawFront) { |
Alan Hourihane | a5cdf99 | 2002-05-02 09:17:59 +0000 | [diff] [blame] | 246 | glDrawBuffer(GL_FRONT); |
| 247 | glReadBuffer(GL_FRONT); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 248 | } |
| 249 | else { |
Alan Hourihane | a5cdf99 | 2002-05-02 09:17:59 +0000 | [diff] [blame] | 250 | glDrawBuffer(GL_BACK); |
| 251 | glReadBuffer(GL_BACK); |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 252 | } |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 253 | printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK"); |
| 254 | break; |
| 255 | case 27: |
| 256 | exit(0); |
| 257 | break; |
| 258 | } |
| 259 | glutPostRedisplay(); |
| 260 | } |
| 261 | |
| 262 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 263 | static void |
| 264 | Init( GLboolean ciMode ) |
| 265 | { |
Ian Romanick | 33899b7 | 2004-10-16 01:16:54 +0000 | [diff] [blame] | 266 | GLboolean have_read_format = GL_FALSE; |
| 267 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 268 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 269 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 270 | |
| 271 | Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat ); |
| 272 | if (!Image) { |
| 273 | printf("Couldn't read %s\n", IMAGE_FILE); |
| 274 | exit(0); |
| 275 | } |
| 276 | |
| 277 | if (ciMode) { |
| 278 | /* Convert RGB image to grayscale */ |
Brian Paul | f02a5f6 | 2002-07-12 15:54:01 +0000 | [diff] [blame] | 279 | GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight ); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 280 | GLint i; |
| 281 | for (i=0; i<ImgWidth*ImgHeight; i++) { |
| 282 | int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2]; |
| 283 | indexImage[i] = gray / 3; |
| 284 | } |
| 285 | free(Image); |
| 286 | Image = indexImage; |
| 287 | ImgFormat = GL_COLOR_INDEX; |
| 288 | |
| 289 | for (i=0;i<255;i++) { |
| 290 | float g = i / 255.0; |
| 291 | glutSetColor(i, g, g, g); |
| 292 | } |
| 293 | } |
| 294 | |
Ian Romanick | 33899b7 | 2004-10-16 01:16:54 +0000 | [diff] [blame] | 295 | #ifdef GL_OES_read_format |
| 296 | if ( glutExtensionSupported( "GL_OES_read_format" ) ) { |
Brian Paul | 4fe34f3 | 2004-11-26 13:43:17 +0000 | [diff] [blame] | 297 | glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES, (GLint *) &ReadType); |
| 298 | glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, (GLint *) &ReadFormat); |
Ian Romanick | 33899b7 | 2004-10-16 01:16:54 +0000 | [diff] [blame] | 299 | |
| 300 | have_read_format = GL_TRUE; |
| 301 | } |
| 302 | #endif |
| 303 | |
| 304 | printf( "GL_OES_read_format %ssupported. " |
| 305 | "Using type / format = 0x%04x / 0x%04x\n", |
| 306 | (have_read_format) ? "" : "not ", |
| 307 | ReadType, ReadFormat ); |
| 308 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 309 | printf("Loaded %d by %d image\n", ImgWidth, ImgHeight ); |
| 310 | |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 311 | glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 312 | glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth); |
| 313 | |
| 314 | Reset(); |
| 315 | |
Brian Paul | 8818eae | 2005-05-18 15:44:13 +0000 | [diff] [blame] | 316 | /* allocate an extra 1KB in case we're tinkering with pack alignment */ |
| 317 | TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * sizeof(GLubyte) |
| 318 | + 1000); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 319 | assert(TempImage); |
| 320 | } |
| 321 | |
| 322 | |
Brian Paul | 4f6d60e | 2000-03-23 19:47:25 +0000 | [diff] [blame] | 323 | int |
| 324 | main( int argc, char *argv[] ) |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 325 | { |
| 326 | GLboolean ciMode = GL_FALSE; |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 327 | if (argc > 1 && strcmp(argv[1], "-ci")==0) { |
| 328 | ciMode = GL_TRUE; |
| 329 | } |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 330 | glutInit( &argc, argv ); |
| 331 | glutInitWindowPosition( 0, 0 ); |
| 332 | glutInitWindowSize( 750, 250 ); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 333 | if (ciMode) |
| 334 | glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE ); |
| 335 | else |
| 336 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 337 | glutCreateWindow(argv[0]); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 338 | Init(ciMode); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 339 | glutReshapeFunc( Reshape ); |
| 340 | glutKeyboardFunc( Key ); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 341 | glutDisplayFunc( Display ); |
Brian Paul | eca1bc9 | 2000-03-01 16:23:14 +0000 | [diff] [blame] | 342 | glutMainLoop(); |
| 343 | return 0; |
| 344 | } |