3 * glReadPixels and glCopyPixels test
5 * Brian Paul March 1, 2000 This file is in the public domain.
17 #define IMAGE_FILE "../images/girl.rgb"
19 static int ImgWidth
, ImgHeight
;
20 static int WinWidth
, WinHeight
;
21 static GLenum ImgFormat
;
22 static GLubyte
*Image
= NULL
;
24 static int APosX
, APosY
; /* simple drawpixels */
25 static int BPosX
, BPosY
; /* read/draw pixels */
26 static int CPosX
, CPosY
; /* copypixels */
28 static GLboolean DrawFront
= GL_FALSE
;
29 static GLboolean ScaleAndBias
= GL_FALSE
;
30 static GLboolean Benchmark
= GL_FALSE
;
31 static GLboolean Triangle
= GL_FALSE
;
32 static GLubyte
*TempImage
= NULL
;
36 #define ReadFormat ImgFormat
37 #define ReadType GL_UNSIGNED_BYTE
39 static GLenum ReadFormat
= GL_RGBA
;
40 static GLenum ReadType
= GL_UNSIGNED_BYTE
;
42 static GLenum ReadFormat
= GL_RGB
;
43 static GLenum ReadType
= GL_UNSIGNED_BYTE
;
45 static GLenum ReadFormat
= GL_RGB
;
46 static GLenum ReadType
= GL_UNSIGNED_SHORT_5_6_5
;
48 static GLenum ReadFormat
= GL_RGBA
;
49 static GLenum ReadType
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
51 static GLenum ReadFormat
= GL_BGRA
;
52 static GLenum ReadType
= GL_UNSIGNED_SHORT_5_5_5_1
;
54 static GLenum ReadFormat
= GL_BGRA
;
55 static GLenum ReadType
= GL_UNSIGNED_SHORT_4_4_4_4_REV
;
57 static GLenum ReadFormat
= GL_RGBA
;
58 static GLenum ReadType
= GL_HALF_FLOAT_ARB
;
59 #undef GL_OES_read_format
66 APosX
= 5; APosY
= 20;
67 BPosX
= APosX
+ ImgWidth
+ 5; BPosY
= 20;
68 CPosX
= BPosX
+ ImgWidth
+ 5; CPosY
= 20;
73 PrintString(const char *s
)
76 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
83 SetupPixelTransfer(GLboolean 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);
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);
105 * Exercise Pixel Pack parameters by reading the image in four pieces.
108 ComplexReadPixels(GLint x
, GLint y
, GLsizei width
, GLsizei height
,
109 GLenum format
, GLenum type
, GLvoid
*pixels
)
111 const GLsizei width0
= width
/ 2;
112 const GLsizei width1
= width
- width0
;
113 const GLsizei height0
= height
/ 2;
114 const GLsizei height1
= height
- height0
;
116 glPixelStorei(GL_PACK_ROW_LENGTH
, width
);
118 /* lower-left quadrant */
119 glReadPixels(x
, y
, width0
, height0
, format
, type
, pixels
);
121 /* lower-right quadrant */
122 glPixelStorei(GL_PACK_SKIP_PIXELS
, width0
);
123 glReadPixels(x
+ width0
, y
, width1
, height0
, format
, type
, pixels
);
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
);
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
);
135 /* restore defaults */
136 glPixelStorei(GL_PACK_SKIP_PIXELS
, 0);
137 glPixelStorei(GL_PACK_SKIP_ROWS
, 0);
138 glPixelStorei(GL_PACK_ROW_LENGTH
, ImgWidth
);
146 glClearColor(.3, .3, .3, 1);
147 glClear( GL_COLOR_BUFFER_BIT
);
149 glRasterPos2i(5, ImgHeight
+25);
150 PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
152 /* draw original image */
153 glRasterPos2i(APosX
, 5);
154 PrintString("Original");
156 glRasterPos2i(APosX
, APosY
);
158 SetupPixelTransfer(GL_FALSE
);
159 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
160 glDrawPixels(ImgWidth
, ImgHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
165 glViewport(APosX
, APosY
, ImgWidth
, ImgHeight
);
166 glMatrixMode( GL_PROJECTION
);
168 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
169 glDisable(GL_CULL_FACE
);
171 /* Red should never be seen
175 glVertex3f(-2, -2, z
);
176 glVertex3f(-2, 2, z
);
178 glVertex3f(2, -2, z
);
185 glVertex3f(-1, -1, z
);
186 glVertex3f(-1, 1, z
);
188 glVertex3f(1, -1, z
);
193 glBegin(GL_TRIANGLES
);
195 glVertex3f(-0.9, -0.9, z
);
197 glVertex3f( 0.9, -0.9, z
);
199 glVertex3f( 0.0, 0.9, z
);
204 glViewport( 0, 0, WinWidth
, WinHeight
);
205 glMatrixMode( GL_PROJECTION
);
207 glOrtho( 0.0, WinWidth
, 0.0, WinHeight
, -1.0, 1.0 );
210 /* might try alignment=4 here for testing */
211 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
212 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
214 /* do readpixels, drawpixels */
215 glRasterPos2i(BPosX
, 5);
216 PrintString("Read/DrawPixels");
217 SetupPixelTransfer(ScaleAndBias
);
221 GLint startTime
= glutGet(GLUT_ELAPSED_TIME
);
222 GLdouble seconds
, pixelsPerSecond
;
223 printf("Benchmarking...\n");
225 glReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
226 ReadFormat
, ReadType
, TempImage
);
228 endTime
= glutGet(GLUT_ELAPSED_TIME
);
229 } while (endTime
- startTime
< 4000); /* 4 seconds */
230 seconds
= (double) (endTime
- startTime
) / 1000.0;
231 pixelsPerSecond
= reads
* ImgWidth
* ImgHeight
/ seconds
;
232 printf("Result: %d reads in %f seconds = %f pixels/sec\n",
233 reads
, seconds
, pixelsPerSecond
);
234 Benchmark
= GL_FALSE
;
237 /* clear the temporary image to white (helpful for debugging */
238 memset(TempImage
, 255, ImgWidth
* ImgHeight
* 4);
240 glReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
241 ReadFormat
, ReadType
, TempImage
);
242 (void) ComplexReadPixels
;
244 /* you might use this when debugging */
245 ComplexReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
246 ReadFormat
, ReadType
, TempImage
);
249 glRasterPos2i(BPosX
, BPosY
);
250 glDisable(GL_DITHER
);
251 SetupPixelTransfer(GL_FALSE
);
252 glDrawPixels(ImgWidth
, ImgHeight
, ReadFormat
, ReadType
, TempImage
);
255 glRasterPos2i(CPosX
, 5);
256 PrintString("CopyPixels");
257 glRasterPos2i(CPosX
, CPosY
);
258 glDisable(GL_DITHER
);
259 SetupPixelTransfer(ScaleAndBias
);
260 glCopyPixels(APosX
, APosY
, ImgWidth
, ImgHeight
, GL_COLOR
);
270 Reshape( int width
, int height
)
275 glViewport( 0, 0, width
, height
);
276 glMatrixMode( GL_PROJECTION
);
278 glOrtho( 0.0, width
, 0.0, height
, -1.0, 1.0 );
279 glMatrixMode( GL_MODELVIEW
);
285 Key( unsigned char key
, int x
, int y
)
294 Triangle
= !Triangle
;
297 ScaleAndBias
= !ScaleAndBias
;
300 DrawFront
= !DrawFront
;
302 glDrawBuffer(GL_FRONT
);
303 glReadBuffer(GL_FRONT
);
306 glDrawBuffer(GL_BACK
);
307 glReadBuffer(GL_BACK
);
309 printf("glDrawBuffer(%s)\n", DrawFront
? "GL_FRONT" : "GL_BACK");
320 Init( GLboolean ciMode
)
322 GLboolean have_read_format
= GL_FALSE
;
324 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
325 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
327 Image
= LoadRGBImage( IMAGE_FILE
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
329 printf("Couldn't read %s\n", IMAGE_FILE
);
334 /* Convert RGB image to grayscale */
335 GLubyte
*indexImage
= (GLubyte
*) malloc( ImgWidth
* ImgHeight
);
337 for (i
=0; i
<ImgWidth
*ImgHeight
; i
++) {
338 int gray
= Image
[i
*3] + Image
[i
*3+1] + Image
[i
*3+2];
339 indexImage
[i
] = gray
/ 3;
343 ImgFormat
= GL_COLOR_INDEX
;
345 for (i
=0;i
<255;i
++) {
347 glutSetColor(i
, g
, g
, g
);
351 #ifdef GL_OES_read_format
352 if ( glutExtensionSupported( "GL_OES_read_format" ) ) {
353 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES
, (GLint
*) &ReadType
);
354 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES
, (GLint
*) &ReadFormat
);
356 have_read_format
= GL_TRUE
;
360 printf( "GL_OES_read_format %ssupported. "
361 "Using type / format = 0x%04x / 0x%04x\n",
362 (have_read_format
) ? "" : "not ",
363 ReadType
, ReadFormat
);
365 printf("Loaded %d by %d image\n", ImgWidth
, ImgHeight
);
367 glPixelStorei(GL_UNPACK_ROW_LENGTH
, ImgWidth
);
368 glPixelStorei(GL_PACK_ROW_LENGTH
, ImgWidth
);
372 /* allocate large TempImage to store and image data type, plus an
373 * extra 1KB in case we're tinkering with pack alignment.
375 TempImage
= (GLubyte
*) malloc(ImgWidth
* ImgHeight
* 4 * 4
382 main( int argc
, char *argv
[] )
384 GLboolean ciMode
= GL_FALSE
;
385 if (argc
> 1 && strcmp(argv
[1], "-ci")==0) {
388 glutInit( &argc
, argv
);
389 glutInitWindowPosition( 0, 0 );
390 glutInitWindowSize( 750, 250 );
392 glutInitDisplayMode( GLUT_INDEX
| GLUT_DOUBLE
);
394 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
395 glutCreateWindow(argv
[0]);
397 glutReshapeFunc( Reshape
);
398 glutKeyboardFunc( Key
);
399 glutDisplayFunc( Display
);