blob: 05f89bb53ea9efcd5d4f2c80ffdc7513e1117e8c [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;
Keith Whitwelleb5d9692009-04-22 15:19:44 +010020static int WinWidth, WinHeight;
Brian Pauleca1bc92000-03-01 16:23:14 +000021static GLenum ImgFormat;
22static GLubyte *Image = NULL;
23
24static int APosX, APosY; /* simple drawpixels */
25static int BPosX, BPosY; /* read/draw pixels */
26static int CPosX, CPosY; /* copypixels */
27
28static GLboolean DrawFront = GL_FALSE;
29static GLboolean ScaleAndBias = GL_FALSE;
Brian Paul4f6d60e2000-03-23 19:47:25 +000030static GLboolean Benchmark = GL_FALSE;
Keith Whitwelleb5d9692009-04-22 15:19:44 +010031static GLboolean Triangle = GL_FALSE;
Brian Pauleca1bc92000-03-01 16:23:14 +000032static GLubyte *TempImage = NULL;
33
Brian Paul1ad12872006-09-23 16:09:26 +000034#define COMBO 1
35#if COMBO == 0
Brian Pauld3d72802000-03-31 01:01:31 +000036#define ReadFormat ImgFormat
37#define ReadType GL_UNSIGNED_BYTE
Brian Paul1ad12872006-09-23 16:09:26 +000038#elif COMBO == 1
Alan Hourihanea5cdf992002-05-02 09:17:59 +000039static GLenum ReadFormat = GL_RGBA;
Brian Pauld3d72802000-03-31 01:01:31 +000040static GLenum ReadType = GL_UNSIGNED_BYTE;
Brian Paul1ad12872006-09-23 16:09:26 +000041#elif COMBO == 2
Brian Pauld3d72802000-03-31 01:01:31 +000042static GLenum ReadFormat = GL_RGB;
Brian Paul8818eae2005-05-18 15:44:13 +000043static GLenum ReadType = GL_UNSIGNED_BYTE;
Brian Paul1ad12872006-09-23 16:09:26 +000044#elif COMBO == 3
Brian Paul8818eae2005-05-18 15:44:13 +000045static GLenum ReadFormat = GL_RGB;
Brian Pauld3d72802000-03-31 01:01:31 +000046static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5;
Brian Paul1ad12872006-09-23 16:09:26 +000047#elif COMBO == 4
Brian Paul1100b4d2000-10-16 21:24:39 +000048static GLenum ReadFormat = GL_RGBA;
49static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
Brian Paul1ad12872006-09-23 16:09:26 +000050#elif COMBO == 5
Brian Paul1100b4d2000-10-16 21:24:39 +000051static GLenum ReadFormat = GL_BGRA;
52static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1;
Brian Paul1ad12872006-09-23 16:09:26 +000053#elif COMBO == 6
Brian Paul1100b4d2000-10-16 21:24:39 +000054static GLenum ReadFormat = GL_BGRA;
55static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV;
Brian Paul1ad12872006-09-23 16:09:26 +000056#elif COMBO == 7
57static GLenum ReadFormat = GL_RGBA;
58static GLenum ReadType = GL_HALF_FLOAT_ARB;
59#undef GL_OES_read_format
Brian Paul1100b4d2000-10-16 21:24:39 +000060#endif
Brian Pauld3d72802000-03-31 01:01:31 +000061
Brian Pauleca1bc92000-03-01 16:23:14 +000062
63static void
64Reset( void )
65{
66 APosX = 5; APosY = 20;
67 BPosX = APosX + ImgWidth + 5; BPosY = 20;
68 CPosX = BPosX + ImgWidth + 5; CPosY = 20;
69}
70
71
72static void
73PrintString(const char *s)
74{
75 while (*s) {
76 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
77 s++;
78 }
79}
80
81
82static void
83SetupPixelTransfer(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 Paul8818eae2005-05-18 15:44:13 +0000104/**
105 * Exercise Pixel Pack parameters by reading the image in four pieces.
106 */
107static void
108ComplexReadPixels(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 Pauleca1bc92000-03-01 16:23:14 +0000143static void
144Display( void )
145{
Brian Paul1100b4d2000-10-16 21:24:39 +0000146 glClearColor(.3, .3, .3, 1);
Brian Pauleca1bc92000-03-01 16:23:14 +0000147 glClear( GL_COLOR_BUFFER_BIT );
148
149 glRasterPos2i(5, ImgHeight+25);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000150 PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
Brian Pauleca1bc92000-03-01 16:23:14 +0000151
152 /* draw original image */
153 glRasterPos2i(APosX, 5);
154 PrintString("Original");
Keith Whitwelleb5d9692009-04-22 15:19:44 +0100155 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 Pauleca1bc92000-03-01 16:23:14 +0000209
Brian Paul8818eae2005-05-18 15:44:13 +0000210 /* might try alignment=4 here for testing */
211 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
212 glPixelStorei(GL_PACK_ALIGNMENT, 1);
213
Brian Pauleca1bc92000-03-01 16:23:14 +0000214 /* do readpixels, drawpixels */
215 glRasterPos2i(BPosX, 5);
216 PrintString("Read/DrawPixels");
217 SetupPixelTransfer(ScaleAndBias);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000218 if (Benchmark) {
219 GLint reads = 0;
220 GLint endTime;
221 GLint startTime = glutGet(GLUT_ELAPSED_TIME);
222 GLdouble seconds, pixelsPerSecond;
223 printf("Benchmarking...\n");
224 do {
225 glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
Brian Pauld3d72802000-03-31 01:01:31 +0000226 ReadFormat, ReadType, TempImage);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000227 reads++;
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;
235 }
236 else {
Brian Paul1100b4d2000-10-16 21:24:39 +0000237 /* clear the temporary image to white (helpful for debugging */
238 memset(TempImage, 255, ImgWidth * ImgHeight * 4);
Brian Paul713635a2005-08-25 22:09:43 +0000239#if 1
Brian Paul4f6d60e2000-03-23 19:47:25 +0000240 glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
Brian Pauld3d72802000-03-31 01:01:31 +0000241 ReadFormat, ReadType, TempImage);
Brian Paul713635a2005-08-25 22:09:43 +0000242 (void) ComplexReadPixels;
Brian Paul8818eae2005-05-18 15:44:13 +0000243#else
Brian Paul713635a2005-08-25 22:09:43 +0000244 /* you might use this when debugging */
Brian Paul8818eae2005-05-18 15:44:13 +0000245 ComplexReadPixels(APosX, APosY, ImgWidth, ImgHeight,
246 ReadFormat, ReadType, TempImage);
247#endif
Brian Paul4f6d60e2000-03-23 19:47:25 +0000248 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000249 glRasterPos2i(BPosX, BPosY);
250 glDisable(GL_DITHER);
251 SetupPixelTransfer(GL_FALSE);
Brian Pauld3d72802000-03-31 01:01:31 +0000252 glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage);
Brian Pauleca1bc92000-03-01 16:23:14 +0000253
254 /* do copypixels */
255 glRasterPos2i(CPosX, 5);
256 PrintString("CopyPixels");
257 glRasterPos2i(CPosX, CPosY);
258 glDisable(GL_DITHER);
259 SetupPixelTransfer(ScaleAndBias);
Alan Hourihanea5cdf992002-05-02 09:17:59 +0000260 glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR);
Brian Pauleca1bc92000-03-01 16:23:14 +0000261
262 if (!DrawFront)
263 glutSwapBuffers();
Alan Hourihane056b3582002-05-02 09:15:22 +0000264 else
265 glFinish();
Brian Pauleca1bc92000-03-01 16:23:14 +0000266}
267
268
Brian Paul4f6d60e2000-03-23 19:47:25 +0000269static void
270Reshape( int width, int height )
Brian Pauleca1bc92000-03-01 16:23:14 +0000271{
Keith Whitwelleb5d9692009-04-22 15:19:44 +0100272 WinWidth = width;
273 WinHeight = height;
274
Brian Pauleca1bc92000-03-01 16:23:14 +0000275 glViewport( 0, 0, width, height );
276 glMatrixMode( GL_PROJECTION );
277 glLoadIdentity();
278 glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
279 glMatrixMode( GL_MODELVIEW );
280 glLoadIdentity();
281}
282
283
Brian Paul4f6d60e2000-03-23 19:47:25 +0000284static void
285Key( unsigned char key, int x, int y )
Brian Pauleca1bc92000-03-01 16:23:14 +0000286{
287 (void) x;
288 (void) y;
289 switch (key) {
290 case 'b':
Brian Paul4f6d60e2000-03-23 19:47:25 +0000291 Benchmark = GL_TRUE;
292 break;
Keith Whitwelleb5d9692009-04-22 15:19:44 +0100293 case 't':
294 Triangle = !Triangle;
295 break;
Brian Paul4f6d60e2000-03-23 19:47:25 +0000296 case 's':
Brian Pauleca1bc92000-03-01 16:23:14 +0000297 ScaleAndBias = !ScaleAndBias;
298 break;
299 case 'f':
300 DrawFront = !DrawFront;
Brian Paul4f6d60e2000-03-23 19:47:25 +0000301 if (DrawFront) {
Alan Hourihanea5cdf992002-05-02 09:17:59 +0000302 glDrawBuffer(GL_FRONT);
303 glReadBuffer(GL_FRONT);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000304 }
305 else {
Alan Hourihanea5cdf992002-05-02 09:17:59 +0000306 glDrawBuffer(GL_BACK);
307 glReadBuffer(GL_BACK);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000308 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000309 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
310 break;
311 case 27:
312 exit(0);
313 break;
314 }
315 glutPostRedisplay();
316}
317
318
Brian Pauleca1bc92000-03-01 16:23:14 +0000319static void
320Init( GLboolean ciMode )
321{
Ian Romanick33899b72004-10-16 01:16:54 +0000322 GLboolean have_read_format = GL_FALSE;
323
Brian Pauleca1bc92000-03-01 16:23:14 +0000324 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
325 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
326
327 Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
328 if (!Image) {
329 printf("Couldn't read %s\n", IMAGE_FILE);
330 exit(0);
331 }
332
333 if (ciMode) {
334 /* Convert RGB image to grayscale */
Brian Paulf02a5f62002-07-12 15:54:01 +0000335 GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
Brian Pauleca1bc92000-03-01 16:23:14 +0000336 GLint i;
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;
340 }
341 free(Image);
342 Image = indexImage;
343 ImgFormat = GL_COLOR_INDEX;
344
345 for (i=0;i<255;i++) {
346 float g = i / 255.0;
347 glutSetColor(i, g, g, g);
348 }
349 }
350
Ian Romanick33899b72004-10-16 01:16:54 +0000351#ifdef GL_OES_read_format
352 if ( glutExtensionSupported( "GL_OES_read_format" ) ) {
Brian Paul4fe34f32004-11-26 13:43:17 +0000353 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES, (GLint *) &ReadType);
354 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, (GLint *) &ReadFormat);
Ian Romanick33899b72004-10-16 01:16:54 +0000355
356 have_read_format = GL_TRUE;
357 }
358#endif
359
360 printf( "GL_OES_read_format %ssupported. "
361 "Using type / format = 0x%04x / 0x%04x\n",
362 (have_read_format) ? "" : "not ",
363 ReadType, ReadFormat );
364
Brian Pauleca1bc92000-03-01 16:23:14 +0000365 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
366
Brian Pauleca1bc92000-03-01 16:23:14 +0000367 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
Brian Pauleca1bc92000-03-01 16:23:14 +0000368 glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
369
370 Reset();
371
Brian Paul1ad12872006-09-23 16:09:26 +0000372 /* allocate large TempImage to store and image data type, plus an
373 * extra 1KB in case we're tinkering with pack alignment.
374 */
375 TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * 4
Brian Paul8818eae2005-05-18 15:44:13 +0000376 + 1000);
Brian Pauleca1bc92000-03-01 16:23:14 +0000377 assert(TempImage);
378}
379
380
Brian Paul4f6d60e2000-03-23 19:47:25 +0000381int
382main( int argc, char *argv[] )
Brian Pauleca1bc92000-03-01 16:23:14 +0000383{
384 GLboolean ciMode = GL_FALSE;
Brian Paul263f4322009-12-18 08:12:55 -0700385 glutInitWindowSize( 750, 250 );
386 glutInit( &argc, argv );
Brian Pauleca1bc92000-03-01 16:23:14 +0000387 if (argc > 1 && strcmp(argv[1], "-ci")==0) {
388 ciMode = GL_TRUE;
389 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000390 if (ciMode)
391 glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
392 else
393 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
Brian Pauleca1bc92000-03-01 16:23:14 +0000394 glutCreateWindow(argv[0]);
Brian Pauleca1bc92000-03-01 16:23:14 +0000395 Init(ciMode);
Brian Pauleca1bc92000-03-01 16:23:14 +0000396 glutReshapeFunc( Reshape );
397 glutKeyboardFunc( Key );
Brian Pauleca1bc92000-03-01 16:23:14 +0000398 glutDisplayFunc( Display );
Brian Pauleca1bc92000-03-01 16:23:14 +0000399 glutMainLoop();
400 return 0;
401}