1 /* $Id: readpix.c,v 1.8 2002/07/12 15:54:02 brianp Exp $ */
4 * glReadPixels and glCopyPixels test
6 * Brian Paul March 1, 2000 This file is in the public domain.
15 #include "../util/readtex.c" /* a hack, I know */
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
;
33 #define ReadFormat ImgFormat
34 #define ReadType GL_UNSIGNED_BYTE
37 static GLenum ReadFormat
= GL_RGBA
;
38 static GLenum ReadType
= GL_UNSIGNED_BYTE
;
41 static GLenum ReadFormat
= GL_RGB
;
42 static GLenum ReadType
= GL_UNSIGNED_SHORT_5_6_5
;
45 static GLenum ReadFormat
= GL_RGBA
;
46 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
;
53 static GLenum ReadFormat
= GL_BGRA
;
54 static GLenum ReadType
= GL_UNSIGNED_SHORT_4_4_4_4_REV
;
61 APosX
= 5; APosY
= 20;
62 BPosX
= APosX
+ ImgWidth
+ 5; BPosY
= 20;
63 CPosX
= BPosX
+ ImgWidth
+ 5; CPosY
= 20;
68 PrintString(const char *s
)
71 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
78 SetupPixelTransfer(GLboolean 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);
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);
102 glClearColor(.3, .3, .3, 1);
103 glClear( GL_COLOR_BUFFER_BIT
);
105 glRasterPos2i(5, ImgHeight
+25);
106 PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
108 /* draw original image */
109 glRasterPos2i(APosX
, 5);
110 PrintString("Original");
111 glRasterPos2i(APosX
, APosY
);
113 SetupPixelTransfer(GL_FALSE
);
114 glDrawPixels(ImgWidth
, ImgHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
116 /* do readpixels, drawpixels */
117 glRasterPos2i(BPosX
, 5);
118 PrintString("Read/DrawPixels");
119 SetupPixelTransfer(ScaleAndBias
);
123 GLint startTime
= glutGet(GLUT_ELAPSED_TIME
);
124 GLdouble seconds
, pixelsPerSecond
;
125 printf("Benchmarking...\n");
127 glReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
128 ReadFormat
, ReadType
, TempImage
);
130 endTime
= glutGet(GLUT_ELAPSED_TIME
);
131 } while (endTime
- startTime
< 4000); /* 4 seconds */
132 seconds
= (double) (endTime
- startTime
) / 1000.0;
133 pixelsPerSecond
= reads
* ImgWidth
* ImgHeight
/ seconds
;
134 printf("Result: %d reads in %f seconds = %f pixels/sec\n",
135 reads
, seconds
, pixelsPerSecond
);
136 Benchmark
= GL_FALSE
;
139 /* clear the temporary image to white (helpful for debugging */
140 memset(TempImage
, 255, ImgWidth
* ImgHeight
* 4);
141 glReadPixels(APosX
, APosY
, ImgWidth
, ImgHeight
,
142 ReadFormat
, ReadType
, TempImage
);
144 glRasterPos2i(BPosX
, BPosY
);
145 glDisable(GL_DITHER
);
146 SetupPixelTransfer(GL_FALSE
);
147 glDrawPixels(ImgWidth
, ImgHeight
, ReadFormat
, ReadType
, TempImage
);
150 glRasterPos2i(CPosX
, 5);
151 PrintString("CopyPixels");
152 glRasterPos2i(CPosX
, CPosY
);
153 glDisable(GL_DITHER
);
154 SetupPixelTransfer(ScaleAndBias
);
155 glCopyPixels(APosX
, APosY
, ImgWidth
, ImgHeight
, GL_COLOR
);
165 Reshape( int width
, int height
)
167 glViewport( 0, 0, width
, height
);
168 glMatrixMode( GL_PROJECTION
);
170 glOrtho( 0.0, width
, 0.0, height
, -1.0, 1.0 );
171 glMatrixMode( GL_MODELVIEW
);
177 Key( unsigned char key
, int x
, int y
)
186 ScaleAndBias
= !ScaleAndBias
;
189 DrawFront
= !DrawFront
;
191 glDrawBuffer(GL_FRONT
);
192 glReadBuffer(GL_FRONT
);
195 glDrawBuffer(GL_BACK
);
196 glReadBuffer(GL_BACK
);
198 printf("glDrawBuffer(%s)\n", DrawFront
? "GL_FRONT" : "GL_BACK");
209 Init( GLboolean ciMode
)
211 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
212 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
214 Image
= LoadRGBImage( IMAGE_FILE
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
216 printf("Couldn't read %s\n", IMAGE_FILE
);
221 /* Convert RGB image to grayscale */
222 GLubyte
*indexImage
= (GLubyte
*) malloc( ImgWidth
* ImgHeight
);
224 for (i
=0; i
<ImgWidth
*ImgHeight
; i
++) {
225 int gray
= Image
[i
*3] + Image
[i
*3+1] + Image
[i
*3+2];
226 indexImage
[i
] = gray
/ 3;
230 ImgFormat
= GL_COLOR_INDEX
;
232 for (i
=0;i
<255;i
++) {
234 glutSetColor(i
, g
, g
, g
);
238 printf("Loaded %d by %d image\n", ImgWidth
, ImgHeight
);
240 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
241 glPixelStorei(GL_UNPACK_ROW_LENGTH
, ImgWidth
);
242 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
243 glPixelStorei(GL_PACK_ROW_LENGTH
, ImgWidth
);
247 TempImage
= (GLubyte
*) malloc(ImgWidth
* ImgHeight
* 4 * sizeof(GLubyte
));
253 main( int argc
, char *argv
[] )
255 GLboolean ciMode
= GL_FALSE
;
256 if (argc
> 1 && strcmp(argv
[1], "-ci")==0) {
259 glutInit( &argc
, argv
);
260 glutInitWindowPosition( 0, 0 );
261 glutInitWindowSize( 750, 250 );
263 glutInitDisplayMode( GLUT_INDEX
| GLUT_DOUBLE
);
265 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
266 glutCreateWindow(argv
[0]);
268 glutReshapeFunc( Reshape
);
269 glutKeyboardFunc( Key
);
270 glutDisplayFunc( Display
);