blob: c0aac2272f7598e6aacebc2e11edb2d0fe1e3587 [file] [log] [blame]
Brian Pauleca1bc92000-03-01 16:23:14 +00001
2/*
3 * glReadPixels and glCopyPixels test
4 *
5 * Brian Paul March 1, 2000 This file is in the public domain.
6 */
7
Brian Pauleca1bc92000-03-01 16:23:14 +00008#include <assert.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <math.h>
Brian Paul2d84ed82005-01-09 17:39:06 +000012#include <string.h>
Brian Pauleca1bc92000-03-01 16:23:14 +000013#include <GL/glut.h>
14
Brian Paul0fe7f402005-01-09 17:06:22 +000015#include "readtex.h"
Brian Pauleca1bc92000-03-01 16:23:14 +000016
17#define IMAGE_FILE "../images/girl.rgb"
18
19static int ImgWidth, ImgHeight;
20static GLenum ImgFormat;
21static GLubyte *Image = NULL;
22
23static int APosX, APosY; /* simple drawpixels */
24static int BPosX, BPosY; /* read/draw pixels */
25static int CPosX, CPosY; /* copypixels */
26
27static GLboolean DrawFront = GL_FALSE;
28static GLboolean ScaleAndBias = GL_FALSE;
Brian Paul4f6d60e2000-03-23 19:47:25 +000029static GLboolean Benchmark = GL_FALSE;
Brian Pauleca1bc92000-03-01 16:23:14 +000030static GLubyte *TempImage = NULL;
31
Brian Paul1ad12872006-09-23 16:09:26 +000032#define COMBO 1
33#if COMBO == 0
Brian Pauld3d72802000-03-31 01:01:31 +000034#define ReadFormat ImgFormat
35#define ReadType GL_UNSIGNED_BYTE
Brian Paul1ad12872006-09-23 16:09:26 +000036#elif COMBO == 1
Alan Hourihanea5cdf992002-05-02 09:17:59 +000037static GLenum ReadFormat = GL_RGBA;
Brian Pauld3d72802000-03-31 01:01:31 +000038static GLenum ReadType = GL_UNSIGNED_BYTE;
Brian Paul1ad12872006-09-23 16:09:26 +000039#elif COMBO == 2
Brian Pauld3d72802000-03-31 01:01:31 +000040static GLenum ReadFormat = GL_RGB;
Brian Paul8818eae2005-05-18 15:44:13 +000041static GLenum ReadType = GL_UNSIGNED_BYTE;
Brian Paul1ad12872006-09-23 16:09:26 +000042#elif COMBO == 3
Brian Paul8818eae2005-05-18 15:44:13 +000043static GLenum ReadFormat = GL_RGB;
Brian Pauld3d72802000-03-31 01:01:31 +000044static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5;
Brian Paul1ad12872006-09-23 16:09:26 +000045#elif COMBO == 4
Brian Paul1100b4d2000-10-16 21:24:39 +000046static GLenum ReadFormat = GL_RGBA;
47static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
Brian Paul1ad12872006-09-23 16:09:26 +000048#elif COMBO == 5
Brian Paul1100b4d2000-10-16 21:24:39 +000049static GLenum ReadFormat = GL_BGRA;
50static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1;
Brian Paul1ad12872006-09-23 16:09:26 +000051#elif COMBO == 6
Brian Paul1100b4d2000-10-16 21:24:39 +000052static GLenum ReadFormat = GL_BGRA;
53static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV;
Brian Paul1ad12872006-09-23 16:09:26 +000054#elif COMBO == 7
55static GLenum ReadFormat = GL_RGBA;
56static GLenum ReadType = GL_HALF_FLOAT_ARB;
57#undef GL_OES_read_format
Brian Paul1100b4d2000-10-16 21:24:39 +000058#endif
Brian Pauld3d72802000-03-31 01:01:31 +000059
Brian Pauleca1bc92000-03-01 16:23:14 +000060
61static void
62Reset( void )
63{
64 APosX = 5; APosY = 20;
65 BPosX = APosX + ImgWidth + 5; BPosY = 20;
66 CPosX = BPosX + ImgWidth + 5; CPosY = 20;
67}
68
69
70static void
71PrintString(const char *s)
72{
73 while (*s) {
74 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
75 s++;
76 }
77}
78
79
80static void
81SetupPixelTransfer(GLboolean invert)
82{
83 if (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);
90 }
91 else {
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);
98 }
99}
100
101
Brian Paul8818eae2005-05-18 15:44:13 +0000102/**
103 * Exercise Pixel Pack parameters by reading the image in four pieces.
104 */
105static void
106ComplexReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
107 GLenum format, GLenum type, GLvoid *pixels)
108{
109 const GLsizei width0 = width / 2;
110 const GLsizei width1 = width - width0;
111 const GLsizei height0 = height / 2;
112 const GLsizei height1 = height - height0;
113
114 glPixelStorei(GL_PACK_ROW_LENGTH, width);
115
116 /* lower-left quadrant */
117 glReadPixels(x, y, width0, height0, format, type, pixels);
118
119 /* lower-right quadrant */
120 glPixelStorei(GL_PACK_SKIP_PIXELS, width0);
121 glReadPixels(x + width0, y, width1, height0, format, type, pixels);
122
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);
127
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);
132
133 /* restore defaults */
134 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
135 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
136 glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
137}
138
139
140
Brian Pauleca1bc92000-03-01 16:23:14 +0000141static void
142Display( void )
143{
Brian Paul1100b4d2000-10-16 21:24:39 +0000144 glClearColor(.3, .3, .3, 1);
Brian Pauleca1bc92000-03-01 16:23:14 +0000145 glClear( GL_COLOR_BUFFER_BIT );
146
147 glRasterPos2i(5, ImgHeight+25);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000148 PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
Brian Pauleca1bc92000-03-01 16:23:14 +0000149
150 /* draw original image */
151 glRasterPos2i(APosX, 5);
152 PrintString("Original");
153 glRasterPos2i(APosX, APosY);
154 glEnable(GL_DITHER);
155 SetupPixelTransfer(GL_FALSE);
Brian Paul8818eae2005-05-18 15:44:13 +0000156 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Brian Pauleca1bc92000-03-01 16:23:14 +0000157 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
158
Brian Paul8818eae2005-05-18 15:44:13 +0000159 /* might try alignment=4 here for testing */
160 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
161 glPixelStorei(GL_PACK_ALIGNMENT, 1);
162
Brian Pauleca1bc92000-03-01 16:23:14 +0000163 /* do readpixels, drawpixels */
164 glRasterPos2i(BPosX, 5);
165 PrintString("Read/DrawPixels");
166 SetupPixelTransfer(ScaleAndBias);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000167 if (Benchmark) {
168 GLint reads = 0;
169 GLint endTime;
170 GLint startTime = glutGet(GLUT_ELAPSED_TIME);
171 GLdouble seconds, pixelsPerSecond;
172 printf("Benchmarking...\n");
173 do {
174 glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
Brian Pauld3d72802000-03-31 01:01:31 +0000175 ReadFormat, ReadType, TempImage);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000176 reads++;
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;
184 }
185 else {
Brian Paul1100b4d2000-10-16 21:24:39 +0000186 /* clear the temporary image to white (helpful for debugging */
187 memset(TempImage, 255, ImgWidth * ImgHeight * 4);
Brian Paul713635a2005-08-25 22:09:43 +0000188#if 1
Brian Paul4f6d60e2000-03-23 19:47:25 +0000189 glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
Brian Pauld3d72802000-03-31 01:01:31 +0000190 ReadFormat, ReadType, TempImage);
Brian Paul713635a2005-08-25 22:09:43 +0000191 (void) ComplexReadPixels;
Brian Paul8818eae2005-05-18 15:44:13 +0000192#else
Brian Paul713635a2005-08-25 22:09:43 +0000193 /* you might use this when debugging */
Brian Paul8818eae2005-05-18 15:44:13 +0000194 ComplexReadPixels(APosX, APosY, ImgWidth, ImgHeight,
195 ReadFormat, ReadType, TempImage);
196#endif
Brian Paul4f6d60e2000-03-23 19:47:25 +0000197 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000198 glRasterPos2i(BPosX, BPosY);
199 glDisable(GL_DITHER);
200 SetupPixelTransfer(GL_FALSE);
Brian Pauld3d72802000-03-31 01:01:31 +0000201 glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage);
Brian Pauleca1bc92000-03-01 16:23:14 +0000202
203 /* do copypixels */
204 glRasterPos2i(CPosX, 5);
205 PrintString("CopyPixels");
206 glRasterPos2i(CPosX, CPosY);
207 glDisable(GL_DITHER);
208 SetupPixelTransfer(ScaleAndBias);
Alan Hourihanea5cdf992002-05-02 09:17:59 +0000209 glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR);
Brian Pauleca1bc92000-03-01 16:23:14 +0000210
211 if (!DrawFront)
212 glutSwapBuffers();
Alan Hourihane056b3582002-05-02 09:15:22 +0000213 else
214 glFinish();
Brian Pauleca1bc92000-03-01 16:23:14 +0000215}
216
217
Brian Paul4f6d60e2000-03-23 19:47:25 +0000218static void
219Reshape( int width, int height )
Brian Pauleca1bc92000-03-01 16:23:14 +0000220{
221 glViewport( 0, 0, width, height );
222 glMatrixMode( GL_PROJECTION );
223 glLoadIdentity();
224 glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
225 glMatrixMode( GL_MODELVIEW );
226 glLoadIdentity();
227}
228
229
Brian Paul4f6d60e2000-03-23 19:47:25 +0000230static void
231Key( unsigned char key, int x, int y )
Brian Pauleca1bc92000-03-01 16:23:14 +0000232{
233 (void) x;
234 (void) y;
235 switch (key) {
236 case 'b':
Brian Paul4f6d60e2000-03-23 19:47:25 +0000237 Benchmark = GL_TRUE;
238 break;
239 case 's':
Brian Pauleca1bc92000-03-01 16:23:14 +0000240 ScaleAndBias = !ScaleAndBias;
241 break;
242 case 'f':
243 DrawFront = !DrawFront;
Brian Paul4f6d60e2000-03-23 19:47:25 +0000244 if (DrawFront) {
Alan Hourihanea5cdf992002-05-02 09:17:59 +0000245 glDrawBuffer(GL_FRONT);
246 glReadBuffer(GL_FRONT);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000247 }
248 else {
Alan Hourihanea5cdf992002-05-02 09:17:59 +0000249 glDrawBuffer(GL_BACK);
250 glReadBuffer(GL_BACK);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000251 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000252 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
253 break;
254 case 27:
255 exit(0);
256 break;
257 }
258 glutPostRedisplay();
259}
260
261
Brian Pauleca1bc92000-03-01 16:23:14 +0000262static void
263Init( GLboolean ciMode )
264{
Ian Romanick33899b72004-10-16 01:16:54 +0000265 GLboolean have_read_format = GL_FALSE;
266
Brian Pauleca1bc92000-03-01 16:23:14 +0000267 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
268 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
269
270 Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
271 if (!Image) {
272 printf("Couldn't read %s\n", IMAGE_FILE);
273 exit(0);
274 }
275
276 if (ciMode) {
277 /* Convert RGB image to grayscale */
Brian Paulf02a5f62002-07-12 15:54:01 +0000278 GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
Brian Pauleca1bc92000-03-01 16:23:14 +0000279 GLint i;
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;
283 }
284 free(Image);
285 Image = indexImage;
286 ImgFormat = GL_COLOR_INDEX;
287
288 for (i=0;i<255;i++) {
289 float g = i / 255.0;
290 glutSetColor(i, g, g, g);
291 }
292 }
293
Ian Romanick33899b72004-10-16 01:16:54 +0000294#ifdef GL_OES_read_format
295 if ( glutExtensionSupported( "GL_OES_read_format" ) ) {
Brian Paul4fe34f32004-11-26 13:43:17 +0000296 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES, (GLint *) &ReadType);
297 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, (GLint *) &ReadFormat);
Ian Romanick33899b72004-10-16 01:16:54 +0000298
299 have_read_format = GL_TRUE;
300 }
301#endif
302
303 printf( "GL_OES_read_format %ssupported. "
304 "Using type / format = 0x%04x / 0x%04x\n",
305 (have_read_format) ? "" : "not ",
306 ReadType, ReadFormat );
307
Brian Pauleca1bc92000-03-01 16:23:14 +0000308 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
309
Brian Pauleca1bc92000-03-01 16:23:14 +0000310 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
Brian Pauleca1bc92000-03-01 16:23:14 +0000311 glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
312
313 Reset();
314
Brian Paul1ad12872006-09-23 16:09:26 +0000315 /* allocate large TempImage to store and image data type, plus an
316 * extra 1KB in case we're tinkering with pack alignment.
317 */
318 TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * 4
Brian Paul8818eae2005-05-18 15:44:13 +0000319 + 1000);
Brian Pauleca1bc92000-03-01 16:23:14 +0000320 assert(TempImage);
321}
322
323
Brian Paul4f6d60e2000-03-23 19:47:25 +0000324int
325main( int argc, char *argv[] )
Brian Pauleca1bc92000-03-01 16:23:14 +0000326{
327 GLboolean ciMode = GL_FALSE;
Brian Pauleca1bc92000-03-01 16:23:14 +0000328 if (argc > 1 && strcmp(argv[1], "-ci")==0) {
329 ciMode = GL_TRUE;
330 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000331 glutInit( &argc, argv );
332 glutInitWindowPosition( 0, 0 );
333 glutInitWindowSize( 750, 250 );
Brian Pauleca1bc92000-03-01 16:23:14 +0000334 if (ciMode)
335 glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
336 else
337 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
Brian Pauleca1bc92000-03-01 16:23:14 +0000338 glutCreateWindow(argv[0]);
Brian Pauleca1bc92000-03-01 16:23:14 +0000339 Init(ciMode);
Brian Pauleca1bc92000-03-01 16:23:14 +0000340 glutReshapeFunc( Reshape );
341 glutKeyboardFunc( Key );
Brian Pauleca1bc92000-03-01 16:23:14 +0000342 glutDisplayFunc( Display );
Brian Pauleca1bc92000-03-01 16:23:14 +0000343 glutMainLoop();
344 return 0;
345}