blob: b6b067502d5a4454e0373690773b7a5e907cb797 [file] [log] [blame]
Brian Pauleca1bc92000-03-01 16:23:14 +00001/* $Id: readpix.c,v 1.1 2000/03/01 16:23:14 brianp Exp $ */
2
3/*
4 * glReadPixels and glCopyPixels test
5 *
6 * Brian Paul March 1, 2000 This file is in the public domain.
7 */
8
9/*
10 * $Log: readpix.c,v $
11 * Revision 1.1 2000/03/01 16:23:14 brianp
12 * test glDraw/Read/CopyPixels()
13 *
14 */
15
16
17#include <assert.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <math.h>
21#include <GL/glut.h>
22
23#include "../util/readtex.c" /* a hack, I know */
24
25#define IMAGE_FILE "../images/girl.rgb"
26
27static int ImgWidth, ImgHeight;
28static GLenum ImgFormat;
29static GLubyte *Image = NULL;
30
31static int APosX, APosY; /* simple drawpixels */
32static int BPosX, BPosY; /* read/draw pixels */
33static int CPosX, CPosY; /* copypixels */
34
35static GLboolean DrawFront = GL_FALSE;
36static GLboolean ScaleAndBias = GL_FALSE;
37
38static GLubyte *TempImage = NULL;
39
40
41static void
42Reset( void )
43{
44 APosX = 5; APosY = 20;
45 BPosX = APosX + ImgWidth + 5; BPosY = 20;
46 CPosX = BPosX + ImgWidth + 5; CPosY = 20;
47}
48
49
50static void
51PrintString(const char *s)
52{
53 while (*s) {
54 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
55 s++;
56 }
57}
58
59
60static void
61SetupPixelTransfer(GLboolean invert)
62{
63 if (invert) {
64 glPixelTransferf(GL_RED_SCALE, -1.0);
65 glPixelTransferf(GL_RED_BIAS, 1.0);
66 glPixelTransferf(GL_GREEN_SCALE, -1.0);
67 glPixelTransferf(GL_GREEN_BIAS, 1.0);
68 glPixelTransferf(GL_BLUE_SCALE, -1.0);
69 glPixelTransferf(GL_BLUE_BIAS, 1.0);
70 }
71 else {
72 glPixelTransferf(GL_RED_SCALE, 1.0);
73 glPixelTransferf(GL_RED_BIAS, 0.0);
74 glPixelTransferf(GL_GREEN_SCALE, 1.0);
75 glPixelTransferf(GL_GREEN_BIAS, 0.0);
76 glPixelTransferf(GL_BLUE_SCALE, 1.0);
77 glPixelTransferf(GL_BLUE_BIAS, 0.0);
78 }
79}
80
81
82static void
83Display( void )
84{
85 glClear( GL_COLOR_BUFFER_BIT );
86
87 glRasterPos2i(5, ImgHeight+25);
88 PrintString("f = toggle front/back b = toggle scale/bias");
89
90 /* draw original image */
91 glRasterPos2i(APosX, 5);
92 PrintString("Original");
93 glRasterPos2i(APosX, APosY);
94 glEnable(GL_DITHER);
95 SetupPixelTransfer(GL_FALSE);
96 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
97
98 /* do readpixels, drawpixels */
99 glRasterPos2i(BPosX, 5);
100 PrintString("Read/DrawPixels");
101 SetupPixelTransfer(ScaleAndBias);
102 glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
103 ImgFormat, GL_UNSIGNED_BYTE, TempImage);
104 glRasterPos2i(BPosX, BPosY);
105 glDisable(GL_DITHER);
106 SetupPixelTransfer(GL_FALSE);
107 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, TempImage);
108
109 /* do copypixels */
110 glRasterPos2i(CPosX, 5);
111 PrintString("CopyPixels");
112 glRasterPos2i(CPosX, CPosY);
113 glDisable(GL_DITHER);
114 SetupPixelTransfer(ScaleAndBias);
115 glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR);
116
117 if (!DrawFront)
118 glutSwapBuffers();
119}
120
121
122static void Reshape( int width, int height )
123{
124 glViewport( 0, 0, width, height );
125 glMatrixMode( GL_PROJECTION );
126 glLoadIdentity();
127 glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
128 glMatrixMode( GL_MODELVIEW );
129 glLoadIdentity();
130}
131
132
133static void Key( unsigned char key, int x, int y )
134{
135 (void) x;
136 (void) y;
137 switch (key) {
138 case 'b':
139 ScaleAndBias = !ScaleAndBias;
140 break;
141 case 'f':
142 DrawFront = !DrawFront;
143 if (DrawFront)
144 glDrawBuffer(GL_FRONT);
145 else
146 glDrawBuffer(GL_BACK);
147 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
148 break;
149 case 27:
150 exit(0);
151 break;
152 }
153 glutPostRedisplay();
154}
155
156
157static void SpecialKey( int key, int x, int y )
158{
159 (void) x;
160 (void) y;
161#if 0
162 switch (key) {
163 case GLUT_KEY_UP:
164 Ypos += 1;
165 break;
166 case GLUT_KEY_DOWN:
167 Ypos -= 1;
168 break;
169 case GLUT_KEY_LEFT:
170 Xpos -= 1;
171 break;
172 case GLUT_KEY_RIGHT:
173 Xpos += 1;
174 break;
175 }
176#endif
177 glutPostRedisplay();
178}
179
180
181static void
182Init( GLboolean ciMode )
183{
184 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
185 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
186
187 Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
188 if (!Image) {
189 printf("Couldn't read %s\n", IMAGE_FILE);
190 exit(0);
191 }
192
193 if (ciMode) {
194 /* Convert RGB image to grayscale */
195 GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
196 GLint i;
197 for (i=0; i<ImgWidth*ImgHeight; i++) {
198 int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
199 indexImage[i] = gray / 3;
200 }
201 free(Image);
202 Image = indexImage;
203 ImgFormat = GL_COLOR_INDEX;
204
205 for (i=0;i<255;i++) {
206 float g = i / 255.0;
207 glutSetColor(i, g, g, g);
208 }
209 }
210
211 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
212
213 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
214 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
215 glPixelStorei(GL_PACK_ALIGNMENT, 1);
216 glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
217
218 Reset();
219
220 TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * sizeof(GLubyte));
221 assert(TempImage);
222}
223
224
225int main( int argc, char *argv[] )
226{
227 GLboolean ciMode = GL_FALSE;
228
229 if (argc > 1 && strcmp(argv[1], "-ci")==0) {
230 ciMode = GL_TRUE;
231 }
232
233 glutInit( &argc, argv );
234 glutInitWindowPosition( 0, 0 );
235 glutInitWindowSize( 750, 250 );
236
237 if (ciMode)
238 glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
239 else
240 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
241
242 glutCreateWindow(argv[0]);
243
244 Init(ciMode);
245
246 glutReshapeFunc( Reshape );
247 glutKeyboardFunc( Key );
248 glutSpecialFunc( SpecialKey );
249 glutDisplayFunc( Display );
250
251 glutMainLoop();
252 return 0;
253}