Merge branch '7.8'
[mesa.git] / progs / samples / copy.c
1 /*
2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the name of
8 * Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Silicon Graphics.
11 *
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
13 * ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <GL/glew.h>
29 #include <GL/glut.h>
30
31
32 #include "loadppm.c"
33
34 GLenum doubleBuffer;
35 GLint windW, windH;
36
37 char *fileName = 0;
38 PPMImage *image;
39 float zoom;
40 GLint x, y;
41
42 static void Init(void)
43 {
44
45 glClearColor(0.0, 0.0, 0.0, 0.0);
46
47 x = 0;
48 y = windH;
49 zoom = 1.8;
50 }
51
52 static void Reshape(int width, int height)
53 {
54
55 windW = (GLint)width;
56 windH = (GLint)height;
57
58 glViewport(0, 0, windW, windH);
59
60 glMatrixMode(GL_PROJECTION);
61 glLoadIdentity();
62 gluOrtho2D(0, windW, 0, windH);
63 glMatrixMode(GL_MODELVIEW);
64 }
65
66 static void Key(unsigned char key, int x, int y)
67 {
68
69 switch (key) {
70 case 27:
71 exit(1);
72 case 'Z':
73 zoom += 0.2;
74 break;
75 case 'z':
76 zoom -= 0.2;
77 if (zoom < 0.2) {
78 zoom = 0.2;
79 }
80 break;
81 default:
82 return;
83 }
84
85 glutPostRedisplay();
86 }
87
88 static void Mouse(int button, int state, int mouseX, int mouseY)
89 {
90 if (state != GLUT_DOWN)
91 return;
92 x = (GLint)mouseX;
93 y = (GLint)mouseY;
94
95 glutPostRedisplay();
96 }
97
98 static void Draw(void)
99 {
100 GLint src[3], dst[3];
101
102 glClear(GL_COLOR_BUFFER_BIT);
103
104 src[0] = (int) ((windW / 2.0) - (image->sizeX / 2.0));
105 src[1] = (int) ((windH / 2.0) - (image->sizeY / 2.0));
106 src[2] = 0;
107 glWindowPos3ivARB(src);
108
109 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
110 glPixelZoom(1.0, 1.0);
111 glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
112 image->data);
113
114 dst[0] = x;
115 dst[1] = windH - y;
116 dst[2] = 0;
117 glWindowPos3ivARB(dst);
118
119 glPixelZoom(zoom, zoom);
120 glCopyPixels(src[0], src[1],
121 image->sizeX, image->sizeY, GL_COLOR);
122
123 glFlush();
124
125 if (doubleBuffer) {
126 glutSwapBuffers();
127 }
128 }
129
130 static GLenum Args(int argc, char **argv)
131 {
132 GLint i;
133
134 doubleBuffer = GL_FALSE;
135
136 for (i = 1; i < argc; i++) {
137 if (strcmp(argv[i], "-sb") == 0) {
138 doubleBuffer = GL_FALSE;
139 } else if (strcmp(argv[i], "-db") == 0) {
140 doubleBuffer = GL_TRUE;
141 } else if (strcmp(argv[i], "-f") == 0) {
142 if (i+1 >= argc || argv[i+1][0] == '-') {
143 printf("-f (No file name).\n");
144 return GL_FALSE;
145 } else {
146 fileName = argv[++i];
147 }
148 } else {
149 printf("%s (Bad option).\n", argv[i]);
150 return GL_FALSE;
151 }
152 }
153 return GL_TRUE;
154 }
155
156 int main(int argc, char **argv)
157 {
158 GLenum type;
159
160 glutInit(&argc, argv);
161
162 if (Args(argc, argv) == GL_FALSE) {
163 exit(1);
164 }
165
166 if (fileName == 0) {
167 printf("No image file.\n");
168 exit(1);
169 }
170
171 image = LoadPPM(fileName);
172
173 windW = 2*300;
174 windH = 2*300;
175 glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH);
176
177 type = GLUT_RGB;
178 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
179 glutInitDisplayMode(type);
180
181 if (glutCreateWindow("Copy Test") == GL_FALSE) {
182 exit(1);
183 }
184
185 glewInit();
186 Init();
187
188 glutReshapeFunc(Reshape);
189 glutKeyboardFunc(Key);
190 glutMouseFunc(Mouse);
191 glutDisplayFunc(Draw);
192 glutMainLoop();
193 return 0;
194 }