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