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 GLenum ImgFormat
;
21 static GLubyte
*Image
= NULL
;
23 static int APosX
, APosY
; /* simple drawpixels */
24 static int BPosX
, BPosY
; /* read/draw pixels */
25 static int CPosX
, CPosY
; /* copypixels */
27 static GLboolean DrawFront
= GL_FALSE
;
28 static GLboolean ScaleAndBias
= GL_FALSE
;
29 static GLboolean Benchmark
= GL_FALSE
;
30 static GLubyte
*TempImage
= NULL
;
34 #define ReadFormat ImgFormat
35 #define ReadType GL_UNSIGNED_BYTE
37 static GLenum ReadFormat
= GL_RGBA
;
38 static GLenum ReadType
= GL_UNSIGNED_BYTE
;
40 static GLenum ReadFormat
= GL_RGB
;
41 static GLenum ReadType
= GL_UNSIGNED_BYTE
;
43 static GLenum ReadFormat
= GL_RGB
;
44 static GLenum ReadType
= GL_UNSIGNED_SHORT_5_6_5
;
46 static GLenum ReadFormat
= GL_RGBA
;
47 static GLenum ReadType
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
49 static GLenum ReadFormat
= GL_BGRA
;
50 static GLenum ReadType
= GL_UNSIGNED_SHORT_5_5_5_1
;
52 static GLenum ReadFormat
= GL_BGRA
;
53 static GLenum ReadType
= GL_UNSIGNED_SHORT_4_4_4_4_REV
;
55 static GLenum ReadFormat
= GL_RGBA
;
56 static GLenum ReadType
= GL_HALF_FLOAT_ARB
;
57 #undef GL_OES_read_format
64 APosX
= 5; APosY
= 20;
65 BPosX
= APosX
+ ImgWidth
+ 5; BPosY
= 20;
66 CPosX
= BPosX
+ ImgWidth
+ 5; CPosY
= 20;
71 PrintString(const char *s
)
74 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
81 SetupPixelTransfer(GLboolean invert
)
84 glPixelTransferf(GL_RED_SCALE
, -1.0);
85 glPixelTransferf(GL_RED_BIAS
, 1.0);
86 glPixelTransferf(GL_GREEN_SCALE
, -1.0);
87 glPixelTransferf(GL_GREEN_BIAS
, 1.0);
88 glPixelTransferf(GL_BLUE_SCALE
, -1.0);
89 glPixelTransferf(GL_BLUE_BIAS
, 1.0);
92 glPixelTransferf(GL_RED_SCALE
, 1.0);
93 glPixelTransferf(GL_RED_BIAS
, 0.0);
94 glPixelTransferf(GL_GREEN_SCALE
, 1.0);
95 glPixelTransferf(GL_GREEN_BIAS
, 0.0);
96 glPixelTransferf(GL_BLUE_SCALE
, 1.0);
97 glPixelTransferf(GL_BLUE_BIAS
, 0.0);
103 * Exercise Pixel Pack parameters by reading the image in four pieces.
106 ComplexReadPixels(GLint x
, GLint y
, GLsizei width
, GLsizei height
,
107 GLenum format
, GLenum type
, GLvoid
*pixels
)
109 const GLsizei width0
= width
/ 2;
110 const GLsizei width1
= width
- width0
;
111 const GLsizei height0
= height
/ 2;
112 const GLsizei height1
= height
- height0
;
114 glPixelStorei(GL_PACK_ROW_LENGTH
, width
);
116 /* lower-left quadrant */
117 glReadPixels(x
, y
, width0
, height0
, format
, type
, pixels
);
119 /* lower-right quadrant */
120 glPixelStorei(GL_PACK_SKIP_PIXELS
, width0
);
121 glReadPixels(x
+ width0
, y
, width1
, height0
, format
, type
, pixels
);
123 /* upper-left quadrant */
124 glPixelStorei(GL_PACK_SKIP_PIXELS
, 0);
125 glPixelStorei(GL_PACK_SKIP_ROWS
, height0
);
126 glReadPixels(x
, y
+ height0
, width0
, height1
, format
, type
, pixels
);
128 /* upper-right quadrant */
129 glPixelStorei(GL_PACK_SKIP_PIXELS
, width0
);
130 glPixelStorei(GL_PACK_SKIP_ROWS
, height0
);
131 glReadPixels(x
+ width0
, y
+ height0
, width1
, height1
, format
, type
, pixels
);
133 /* restore defaults */
134 glPixelStorei(GL_PACK_SKIP_PIXELS
, 0);
135 glPixelStorei(GL_PACK_SKIP_ROWS
, 0);
136 glPixelStorei(GL_PACK_ROW_LENGTH
, ImgWidth
);
144 glClearColor(.3, .3, .3, 1);
145 glClear( GL_COLOR_BUFFER_BIT
);
147 glRasterPos2i(5, ImgHeight
+25);
148 PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
150 /* draw original image */
151 glRasterPos2i(APosX
, 5);
152 PrintString("Original");
153 glRasterPos2i(APosX
, APosY
);
155 SetupPixelTransfer(GL_FALSE
);
156 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
157 glDrawPixels(ImgWidth
, ImgHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
159 /* might try alignment=4 here for testing */
160 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
161 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
163 /* do readpixels, drawpixels */
164 glRasterPos2i(BPosX
, 5);
165 PrintString("Read/DrawPixels");
166 SetupPixelTransfer(ScaleAndBias
);
170 GLint startTime
= glutGet(GLUT_ELAPSED_TIME
);
171 GLdouble seconds
, pixelsPerSecond
;
172 printf("Benchmarking...\n");
174 glReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
175 ReadFormat
, ReadType
, TempImage
);
177 endTime
= glutGet(GLUT_ELAPSED_TIME
);
178 } while (endTime
- startTime
< 4000); /* 4 seconds */
179 seconds
= (double) (endTime
- startTime
) / 1000.0;
180 pixelsPerSecond
= reads
* ImgWidth
* ImgHeight
/ seconds
;
181 printf("Result: %d reads in %f seconds = %f pixels/sec\n",
182 reads
, seconds
, pixelsPerSecond
);
183 Benchmark
= GL_FALSE
;
186 /* clear the temporary image to white (helpful for debugging */
187 memset(TempImage
, 255, ImgWidth
* ImgHeight
* 4);
189 glReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
190 ReadFormat
, ReadType
, TempImage
);
191 (void) ComplexReadPixels
;
193 /* you might use this when debugging */
194 ComplexReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
195 ReadFormat
, ReadType
, TempImage
);
198 glRasterPos2i(BPosX
, BPosY
);
199 glDisable(GL_DITHER
);
200 SetupPixelTransfer(GL_FALSE
);
201 glDrawPixels(ImgWidth
, ImgHeight
, ReadFormat
, ReadType
, TempImage
);
204 glRasterPos2i(CPosX
, 5);
205 PrintString("CopyPixels");
206 glRasterPos2i(CPosX
, CPosY
);
207 glDisable(GL_DITHER
);
208 SetupPixelTransfer(ScaleAndBias
);
209 glCopyPixels(APosX
, APosY
, ImgWidth
, ImgHeight
, GL_COLOR
);
219 Reshape( int width
, int height
)
221 glViewport( 0, 0, width
, height
);
222 glMatrixMode( GL_PROJECTION
);
224 glOrtho( 0.0, width
, 0.0, height
, -1.0, 1.0 );
225 glMatrixMode( GL_MODELVIEW
);
231 Key( unsigned char key
, int x
, int y
)
240 ScaleAndBias
= !ScaleAndBias
;
243 DrawFront
= !DrawFront
;
245 glDrawBuffer(GL_FRONT
);
246 glReadBuffer(GL_FRONT
);
249 glDrawBuffer(GL_BACK
);
250 glReadBuffer(GL_BACK
);
252 printf("glDrawBuffer(%s)\n", DrawFront
? "GL_FRONT" : "GL_BACK");
263 Init( GLboolean ciMode
)
265 GLboolean have_read_format
= GL_FALSE
;
267 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
268 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
270 Image
= LoadRGBImage( IMAGE_FILE
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
272 printf("Couldn't read %s\n", IMAGE_FILE
);
277 /* Convert RGB image to grayscale */
278 GLubyte
*indexImage
= (GLubyte
*) malloc( ImgWidth
* ImgHeight
);
280 for (i
=0; i
<ImgWidth
*ImgHeight
; i
++) {
281 int gray
= Image
[i
*3] + Image
[i
*3+1] + Image
[i
*3+2];
282 indexImage
[i
] = gray
/ 3;
286 ImgFormat
= GL_COLOR_INDEX
;
288 for (i
=0;i
<255;i
++) {
290 glutSetColor(i
, g
, g
, g
);
294 #ifdef GL_OES_read_format
295 if ( glutExtensionSupported( "GL_OES_read_format" ) ) {
296 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES
, (GLint
*) &ReadType
);
297 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES
, (GLint
*) &ReadFormat
);
299 have_read_format
= GL_TRUE
;
303 printf( "GL_OES_read_format %ssupported. "
304 "Using type / format = 0x%04x / 0x%04x\n",
305 (have_read_format
) ? "" : "not ",
306 ReadType
, ReadFormat
);
308 printf("Loaded %d by %d image\n", ImgWidth
, ImgHeight
);
310 glPixelStorei(GL_UNPACK_ROW_LENGTH
, ImgWidth
);
311 glPixelStorei(GL_PACK_ROW_LENGTH
, ImgWidth
);
315 /* allocate large TempImage to store and image data type, plus an
316 * extra 1KB in case we're tinkering with pack alignment.
318 TempImage
= (GLubyte
*) malloc(ImgWidth
* ImgHeight
* 4 * 4
325 main( int argc
, char *argv
[] )
327 GLboolean ciMode
= GL_FALSE
;
328 if (argc
> 1 && strcmp(argv
[1], "-ci")==0) {
331 glutInit( &argc
, argv
);
332 glutInitWindowPosition( 0, 0 );
333 glutInitWindowSize( 750, 250 );
335 glutInitDisplayMode( GLUT_INDEX
| GLUT_DOUBLE
);
337 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
338 glutCreateWindow(argv
[0]);
340 glutReshapeFunc( Reshape
);
341 glutKeyboardFunc( Key
);
342 glutDisplayFunc( Display
);