demos: remove OSMESA stuff from Makefile
[mesa.git] / progs / demos / copypix.c
1 /**
2 * glCopyPixels test
3 *
4 * Brian Paul
5 * 14 Sep 2007
6 */
7
8
9 #define GL_GLEXT_PROTOTYPES
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <GL/glut.h>
15
16 #include "readtex.h"
17
18 #define IMAGE_FILE "../images/arch.rgb"
19
20 static int ImgWidth, ImgHeight;
21 static GLenum ImgFormat;
22 static GLubyte *Image = NULL;
23
24 static int WinWidth = 800, WinHeight = 800;
25 static int Xpos, Ypos;
26 static int Scissor = 0;
27 static float Xzoom, Yzoom;
28 static GLboolean DrawFront = GL_FALSE;
29 static GLboolean Dither = GL_TRUE;
30
31
32 static void Reset( void )
33 {
34 Xpos = Ypos = 20;
35 Scissor = 0;
36 Xzoom = Yzoom = 1.0;
37 }
38
39
40 static void Display( void )
41 {
42 const int dx = (WinWidth - ImgWidth) / 2;
43 const int dy = (WinHeight - ImgHeight) / 2;
44
45 if (DrawFront) {
46 glDrawBuffer(GL_FRONT);
47 glReadBuffer(GL_FRONT);
48 }
49 else {
50 glDrawBuffer(GL_BACK);
51 glReadBuffer(GL_BACK);
52 }
53
54 glClear( GL_COLOR_BUFFER_BIT );
55
56 /* draw original image */
57 glWindowPos2iARB(dx, dy);
58 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
59
60 if (Scissor)
61 glEnable(GL_SCISSOR_TEST);
62
63 /* draw copy */
64 glPixelZoom(Xzoom, Yzoom);
65 glWindowPos2iARB(Xpos, Ypos);
66 glCopyPixels(dx, dy, ImgWidth, ImgHeight, GL_COLOR);
67 glPixelZoom(1, 1);
68
69 glDisable(GL_SCISSOR_TEST);
70
71 if (DrawFront)
72 glFinish();
73 else
74 glutSwapBuffers();
75 }
76
77
78 static void Reshape( int width, int height )
79 {
80 WinWidth = width;
81 WinHeight = height;
82
83 glViewport( 0, 0, width, height );
84 glMatrixMode( GL_PROJECTION );
85 glLoadIdentity();
86 glOrtho( 0.0, width, 0.0, height, 0.0, 2.0 );
87 glMatrixMode( GL_MODELVIEW );
88 glLoadIdentity();
89
90 glScissor(width/4, height/4, width/2, height/2);
91 }
92
93
94 static void Key( unsigned char key, int x, int y )
95 {
96 (void) x;
97 (void) y;
98 switch (key) {
99 case ' ':
100 Reset();
101 break;
102 case 'd':
103 Dither = !Dither;
104 if (Dither)
105 glEnable(GL_DITHER);
106 else
107 glDisable(GL_DITHER);
108 break;
109 case 's':
110 Scissor = !Scissor;
111 break;
112 case 'x':
113 Xzoom -= 0.1;
114 break;
115 case 'X':
116 Xzoom += 0.1;
117 break;
118 case 'y':
119 Yzoom -= 0.1;
120 break;
121 case 'Y':
122 Yzoom += 0.1;
123 break;
124 case 'f':
125 DrawFront = !DrawFront;
126 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
127 break;
128 case 27:
129 exit(0);
130 break;
131 }
132 glutPostRedisplay();
133 }
134
135
136 static void SpecialKey( int key, int x, int y )
137 {
138 const int step = (glutGetModifiers() & GLUT_ACTIVE_SHIFT) ? 10 : 1;
139 (void) x;
140 (void) y;
141 switch (key) {
142 case GLUT_KEY_UP:
143 Ypos += step;
144 break;
145 case GLUT_KEY_DOWN:
146 Ypos -= step;
147 break;
148 case GLUT_KEY_LEFT:
149 Xpos -= step;
150 break;
151 case GLUT_KEY_RIGHT:
152 Xpos += step;
153 break;
154 }
155 glutPostRedisplay();
156 }
157
158
159 static void Init( GLboolean ciMode, const char *filename )
160 {
161 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
162 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
163
164 Image = LoadRGBImage( filename, &ImgWidth, &ImgHeight, &ImgFormat );
165 if (!Image) {
166 printf("Couldn't read %s\n", filename);
167 exit(0);
168 }
169
170 if (ciMode) {
171 /* Convert RGB image to grayscale */
172 GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
173 GLint i;
174 for (i=0; i<ImgWidth*ImgHeight; i++) {
175 int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
176 indexImage[i] = gray / 3;
177 }
178 free(Image);
179 Image = indexImage;
180 ImgFormat = GL_COLOR_INDEX;
181
182 for (i=0;i<255;i++) {
183 float g = i / 255.0;
184 glutSetColor(i, g, g, g);
185 }
186 }
187
188 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
189
190 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
191 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
192
193 Reset();
194 }
195
196
197 static void Usage(void)
198 {
199 printf("Keys:\n");
200 printf(" SPACE Reset Parameters\n");
201 printf(" Up/Down Move image up/down (SHIFT for large step)\n");
202 printf(" Left/Right Move image left/right (SHIFT for large step)\n");
203 printf(" x Decrease X-axis PixelZoom\n");
204 printf(" X Increase X-axis PixelZoom\n");
205 printf(" y Decrease Y-axis PixelZoom\n");
206 printf(" Y Increase Y-axis PixelZoom\n");
207 printf(" s Toggle GL_SCISSOR_TEST\n");
208 printf(" f Toggle front/back buffer drawing\n");
209 printf(" ESC Exit\n");
210 }
211
212
213 int main( int argc, char *argv[] )
214 {
215 GLboolean ciMode = GL_FALSE;
216 const char *filename = IMAGE_FILE;
217 int i = 1;
218
219 if (argc > i && strcmp(argv[i], "-ci")==0) {
220 ciMode = GL_TRUE;
221 i++;
222 }
223 if (argc > i) {
224 filename = argv[i];
225 }
226
227 glutInit( &argc, argv );
228 glutInitWindowPosition( 0, 0 );
229 glutInitWindowSize( WinWidth, WinHeight );
230
231 if (ciMode)
232 glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
233 else
234 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
235
236 glutCreateWindow(argv[0]);
237
238 Init(ciMode, filename);
239 Usage();
240
241 glutReshapeFunc( Reshape );
242 glutKeyboardFunc( Key );
243 glutSpecialFunc( SpecialKey );
244 glutDisplayFunc( Display );
245
246 glutMainLoop();
247 return 0;
248 }