blob: d0c46a271430a47f534d3f9d3c2ac3657b00b1fb [file] [log] [blame]
Brian Paulfdd631a2002-04-22 16:03:37 +00001/* $Id: readpix.c,v 1.5 2002/04/22 16:03:37 brianp 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
Brian Pauld3d72802000-03-31 01:01:31 +000037static GLenum ReadFormat = GL_RGBA;
38static 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);
114 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
115
116 /* do readpixels, drawpixels */
117 glRasterPos2i(BPosX, 5);
118 PrintString("Read/DrawPixels");
119 SetupPixelTransfer(ScaleAndBias);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000120 if (Benchmark) {
121 GLint reads = 0;
122 GLint endTime;
123 GLint startTime = glutGet(GLUT_ELAPSED_TIME);
124 GLdouble seconds, pixelsPerSecond;
125 printf("Benchmarking...\n");
126 do {
127 glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
Brian Pauld3d72802000-03-31 01:01:31 +0000128 ReadFormat, ReadType, TempImage);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000129 reads++;
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;
137 }
138 else {
Brian Paul1100b4d2000-10-16 21:24:39 +0000139 /* clear the temporary image to white (helpful for debugging */
140 memset(TempImage, 255, ImgWidth * ImgHeight * 4);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000141 glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
Brian Pauld3d72802000-03-31 01:01:31 +0000142 ReadFormat, ReadType, TempImage);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000143 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000144 glRasterPos2i(BPosX, BPosY);
145 glDisable(GL_DITHER);
146 SetupPixelTransfer(GL_FALSE);
Brian Pauld3d72802000-03-31 01:01:31 +0000147 glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage);
Brian Pauleca1bc92000-03-01 16:23:14 +0000148
149 /* do copypixels */
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);
156
157 if (!DrawFront)
158 glutSwapBuffers();
159}
160
161
Brian Paul4f6d60e2000-03-23 19:47:25 +0000162static void
163Reshape( int width, int height )
Brian Pauleca1bc92000-03-01 16:23:14 +0000164{
165 glViewport( 0, 0, width, height );
166 glMatrixMode( GL_PROJECTION );
167 glLoadIdentity();
168 glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
169 glMatrixMode( GL_MODELVIEW );
170 glLoadIdentity();
171}
172
173
Brian Paul4f6d60e2000-03-23 19:47:25 +0000174static void
175Key( unsigned char key, int x, int y )
Brian Pauleca1bc92000-03-01 16:23:14 +0000176{
177 (void) x;
178 (void) y;
179 switch (key) {
180 case 'b':
Brian Paul4f6d60e2000-03-23 19:47:25 +0000181 Benchmark = GL_TRUE;
182 break;
183 case 's':
Brian Pauleca1bc92000-03-01 16:23:14 +0000184 ScaleAndBias = !ScaleAndBias;
185 break;
186 case 'f':
187 DrawFront = !DrawFront;
Brian Paul4f6d60e2000-03-23 19:47:25 +0000188 if (DrawFront) {
Brian Pauleca1bc92000-03-01 16:23:14 +0000189 glDrawBuffer(GL_FRONT);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000190 glReadBuffer(GL_FRONT);
191 }
192 else {
Brian Pauleca1bc92000-03-01 16:23:14 +0000193 glDrawBuffer(GL_BACK);
Brian Paul4f6d60e2000-03-23 19:47:25 +0000194 glReadBuffer(GL_BACK);
195 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000196 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
197 break;
198 case 27:
199 exit(0);
200 break;
201 }
202 glutPostRedisplay();
203}
204
205
Brian Pauleca1bc92000-03-01 16:23:14 +0000206static void
207Init( GLboolean ciMode )
208{
209 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
210 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
211
212 Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
213 if (!Image) {
214 printf("Couldn't read %s\n", IMAGE_FILE);
215 exit(0);
216 }
217
218 if (ciMode) {
219 /* Convert RGB image to grayscale */
220 GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
221 GLint i;
222 for (i=0; i<ImgWidth*ImgHeight; i++) {
223 int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
224 indexImage[i] = gray / 3;
225 }
226 free(Image);
227 Image = indexImage;
228 ImgFormat = GL_COLOR_INDEX;
229
230 for (i=0;i<255;i++) {
231 float g = i / 255.0;
232 glutSetColor(i, g, g, g);
233 }
234 }
235
236 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
237
238 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
239 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
240 glPixelStorei(GL_PACK_ALIGNMENT, 1);
241 glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
242
243 Reset();
244
245 TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * sizeof(GLubyte));
246 assert(TempImage);
247}
248
249
Brian Paul4f6d60e2000-03-23 19:47:25 +0000250int
251main( int argc, char *argv[] )
Brian Pauleca1bc92000-03-01 16:23:14 +0000252{
253 GLboolean ciMode = GL_FALSE;
Brian Pauleca1bc92000-03-01 16:23:14 +0000254 if (argc > 1 && strcmp(argv[1], "-ci")==0) {
255 ciMode = GL_TRUE;
256 }
Brian Pauleca1bc92000-03-01 16:23:14 +0000257 glutInit( &argc, argv );
258 glutInitWindowPosition( 0, 0 );
259 glutInitWindowSize( 750, 250 );
Brian Pauleca1bc92000-03-01 16:23:14 +0000260 if (ciMode)
261 glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
262 else
263 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
Brian Pauleca1bc92000-03-01 16:23:14 +0000264 glutCreateWindow(argv[0]);
Brian Pauleca1bc92000-03-01 16:23:14 +0000265 Init(ciMode);
Brian Pauleca1bc92000-03-01 16:23:14 +0000266 glutReshapeFunc( Reshape );
267 glutKeyboardFunc( Key );
Brian Pauleca1bc92000-03-01 16:23:14 +0000268 glutDisplayFunc( Display );
Brian Pauleca1bc92000-03-01 16:23:14 +0000269 glutMainLoop();
270 return 0;
271}