blob: 3b13bb2c3e2b86323b124fe80d6a206d0c463f0b [file] [log] [blame]
Alan Hourihane056b3582002-05-02 09:15:22 +00001/* $Id: readpix.c,v 1.6 2002/05/02 09:15:22 alanh Exp $ */
Brian Pauleca1bc92000-03-01 16:23:14 +00002
3/*
4 * glReadPixels and glCopyPixels test
5 *
6 * Brian Paul March 1, 2000 This file is in the public domain.
7 */
8
Brian Pauleca1bc92000-03-01 16:23:14 +00009#include <assert.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <math.h>
13#include <GL/glut.h>
14
15#include "../util/readtex.c" /* a hack, I know */
16
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 Paul1100b4d2000-10-16 21:24:39 +000032#if 0
Brian Pauld3d72802000-03-31 01:01:31 +000033#define ReadFormat ImgFormat
34#define ReadType GL_UNSIGNED_BYTE
35#endif
Brian Paul1100b4d2000-10-16 21:24:39 +000036#if 1
Alan Hourihane056b3582002-05-02 09:15:22 +000037static GLenum ReadFormat = GL_DEPTH_COMPONENT; /*GL_RGBA; */
Brian Pauld3d72802000-03-31 01:01:31 +000038static GLenum ReadType = GL_UNSIGNED_BYTE;
39#endif
40#if 0
41static GLenum ReadFormat = GL_RGB;
42static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5;
43#endif
Brian Paul1100b4d2000-10-16 21:24:39 +000044#if 0
45static GLenum ReadFormat = GL_RGBA;
46static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
47#endif
48#if 0
49static GLenum ReadFormat = GL_BGRA;
50static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1;
51#endif
52#if 0
53static GLenum ReadFormat = GL_BGRA;
54static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV;
55#endif
Brian Pauld3d72802000-03-31 01:01:31 +000056
Brian Pauleca1bc92000-03-01 16:23:14 +000057
58static void
59Reset( void )
60{
61 APosX = 5; APosY = 20;
62 BPosX = APosX + ImgWidth + 5; BPosY = 20;
63 CPosX = BPosX + ImgWidth + 5; CPosY = 20;
64}
65
66
67static void
68PrintString(const char *s)
69{
70 while (*s) {
71 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
72 s++;
73 }
74}
75
76
77static void
78SetupPixelTransfer(GLboolean invert)
79{
80 if (invert) {
81 glPixelTransferf(GL_RED_SCALE, -1.0);
82 glPixelTransferf(GL_RED_BIAS, 1.0);
83 glPixelTransferf(GL_GREEN_SCALE, -1.0);
84 glPixelTransferf(GL_GREEN_BIAS, 1.0);
85 glPixelTransferf(GL_BLUE_SCALE, -1.0);
86 glPixelTransferf(GL_BLUE_BIAS, 1.0);
87 }
88 else {
89 glPixelTransferf(GL_RED_SCALE, 1.0);
90 glPixelTransferf(GL_RED_BIAS, 0.0);
91 glPixelTransferf(GL_GREEN_SCALE, 1.0);
92 glPixelTransferf(GL_GREEN_BIAS, 0.0);
93 glPixelTransferf(GL_BLUE_SCALE, 1.0);
94 glPixelTransferf(GL_BLUE_BIAS, 0.0);
95 }
96}
97
98
99static void
100Display( void )
101{
Brian Paul1100b4d2000-10-16 21:24:39 +0000102 glClearColor(.3, .3, .3, 1);
Brian Pauleca1bc92000-03-01 16:23:14 +0000103 glClear( GL_COLOR_BUFFER_BIT );
104
105 glRasterPos2i(5, ImgHeight+25);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000106 PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
Brian Pauleca1bc92000-03-01 16:23:14 +0000107
108 /* draw original image */
109 glRasterPos2i(APosX, 5);
110 PrintString("Original");
111 glRasterPos2i(APosX, APosY);
112 glEnable(GL_DITHER);
113 SetupPixelTransfer(GL_FALSE);
Alan Hourihane056b3582002-05-02 09:15:22 +0000114 glDrawBuffer(GL_AUX0);
115 glReadBuffer(GL_AUX0);
Brian Pauleca1bc92000-03-01 16:23:14 +0000116 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
117
118 /* do readpixels, drawpixels */
119 glRasterPos2i(BPosX, 5);
120 PrintString("Read/DrawPixels");
121 SetupPixelTransfer(ScaleAndBias);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000122 if (Benchmark) {
123 GLint reads = 0;
124 GLint endTime;
125 GLint startTime = glutGet(GLUT_ELAPSED_TIME);
126 GLdouble seconds, pixelsPerSecond;
127 printf("Benchmarking...\n");
128 do {
129 glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
Brian Pauld3d72802000-03-31 01:01:31 +0000130 ReadFormat, ReadType, TempImage);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000131 reads++;
132 endTime = glutGet(GLUT_ELAPSED_TIME);
133 } while (endTime - startTime < 4000); /* 4 seconds */
134 seconds = (double) (endTime - startTime) / 1000.0;
135 pixelsPerSecond = reads * ImgWidth * ImgHeight / seconds;
136 printf("Result: %d reads in %f seconds = %f pixels/sec\n",
137 reads, seconds, pixelsPerSecond);
138 Benchmark = GL_FALSE;
139 }
140 else {
Brian Paul1100b4d2000-10-16 21:24:39 +0000141 /* clear the temporary image to white (helpful for debugging */
142 memset(TempImage, 255, ImgWidth * ImgHeight * 4);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000143 glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
Brian Pauld3d72802000-03-31 01:01:31 +0000144 ReadFormat, ReadType, TempImage);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000145 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000146 glRasterPos2i(BPosX, BPosY);
147 glDisable(GL_DITHER);
148 SetupPixelTransfer(GL_FALSE);
Brian Pauld3d72802000-03-31 01:01:31 +0000149 glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage);
Brian Pauleca1bc92000-03-01 16:23:14 +0000150
151 /* do copypixels */
152 glRasterPos2i(CPosX, 5);
153 PrintString("CopyPixels");
154 glRasterPos2i(CPosX, CPosY);
155 glDisable(GL_DITHER);
156 SetupPixelTransfer(ScaleAndBias);
Alan Hourihane056b3582002-05-02 09:15:22 +0000157 glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_DEPTH);
Brian Pauleca1bc92000-03-01 16:23:14 +0000158
159 if (!DrawFront)
160 glutSwapBuffers();
Alan Hourihane056b3582002-05-02 09:15:22 +0000161 else
162 glFinish();
Brian Pauleca1bc92000-03-01 16:23:14 +0000163}
164
165
Brian Paul4f6d60e2000-03-23 19:47:25 +0000166static void
167Reshape( int width, int height )
Brian Pauleca1bc92000-03-01 16:23:14 +0000168{
169 glViewport( 0, 0, width, height );
170 glMatrixMode( GL_PROJECTION );
171 glLoadIdentity();
172 glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
173 glMatrixMode( GL_MODELVIEW );
174 glLoadIdentity();
175}
176
177
Brian Paul4f6d60e2000-03-23 19:47:25 +0000178static void
179Key( unsigned char key, int x, int y )
Brian Pauleca1bc92000-03-01 16:23:14 +0000180{
181 (void) x;
182 (void) y;
183 switch (key) {
184 case 'b':
Brian Paul4f6d60e2000-03-23 19:47:25 +0000185 Benchmark = GL_TRUE;
186 break;
187 case 's':
Brian Pauleca1bc92000-03-01 16:23:14 +0000188 ScaleAndBias = !ScaleAndBias;
189 break;
190 case 'f':
191 DrawFront = !DrawFront;
Brian Paul4f6d60e2000-03-23 19:47:25 +0000192 if (DrawFront) {
Alan Hourihane056b3582002-05-02 09:15:22 +0000193 glDrawBuffer(GL_AUX0);
194 glReadBuffer(GL_AUX0);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000195 }
196 else {
Alan Hourihane056b3582002-05-02 09:15:22 +0000197 glDrawBuffer(GL_AUX0);
198 glReadBuffer(GL_AUX0);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000199 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000200 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
201 break;
202 case 27:
203 exit(0);
204 break;
205 }
206 glutPostRedisplay();
207}
208
209
Brian Pauleca1bc92000-03-01 16:23:14 +0000210static void
211Init( GLboolean ciMode )
212{
213 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
214 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
215
216 Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
217 if (!Image) {
218 printf("Couldn't read %s\n", IMAGE_FILE);
219 exit(0);
220 }
221
222 if (ciMode) {
223 /* Convert RGB image to grayscale */
224 GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
225 GLint i;
226 for (i=0; i<ImgWidth*ImgHeight; i++) {
227 int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
228 indexImage[i] = gray / 3;
229 }
230 free(Image);
231 Image = indexImage;
232 ImgFormat = GL_COLOR_INDEX;
233
234 for (i=0;i<255;i++) {
235 float g = i / 255.0;
236 glutSetColor(i, g, g, g);
237 }
238 }
239
240 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
241
242 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
243 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
244 glPixelStorei(GL_PACK_ALIGNMENT, 1);
245 glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
246
247 Reset();
248
249 TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * sizeof(GLubyte));
250 assert(TempImage);
251}
252
253
Brian Paul4f6d60e2000-03-23 19:47:25 +0000254int
255main( int argc, char *argv[] )
Brian Pauleca1bc92000-03-01 16:23:14 +0000256{
257 GLboolean ciMode = GL_FALSE;
Brian Pauleca1bc92000-03-01 16:23:14 +0000258 if (argc > 1 && strcmp(argv[1], "-ci")==0) {
259 ciMode = GL_TRUE;
260 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000261 glutInit( &argc, argv );
262 glutInitWindowPosition( 0, 0 );
263 glutInitWindowSize( 750, 250 );
Brian Pauleca1bc92000-03-01 16:23:14 +0000264 if (ciMode)
265 glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
266 else
267 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
Brian Pauleca1bc92000-03-01 16:23:14 +0000268 glutCreateWindow(argv[0]);
Brian Pauleca1bc92000-03-01 16:23:14 +0000269 Init(ciMode);
Brian Pauleca1bc92000-03-01 16:23:14 +0000270 glutReshapeFunc( Reshape );
271 glutKeyboardFunc( Key );
Brian Pauleca1bc92000-03-01 16:23:14 +0000272 glutDisplayFunc( Display );
Brian Pauleca1bc92000-03-01 16:23:14 +0000273 glutMainLoop();
274 return 0;
275}