jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame^] | 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/glut.h> |
| 29 | |
| 30 | |
| 31 | #include "loadppm.c" |
| 32 | |
| 33 | GLenum doubleBuffer; |
| 34 | GLint windW, windH; |
| 35 | |
| 36 | char *fileName = 0; |
| 37 | PPMImage *image; |
| 38 | float point[3]; |
| 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 | |
| 101 | glClear(GL_COLOR_BUFFER_BIT); |
| 102 | |
| 103 | point[0] = (windW / 2) - (image->sizeX / 2); |
| 104 | point[1] = (windH / 2) - (image->sizeY / 2); |
| 105 | point[2] = 0; |
| 106 | glRasterPos3fv(point); |
| 107 | |
| 108 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 109 | glPixelZoom(1.0, 1.0); |
| 110 | glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, |
| 111 | image->data); |
| 112 | |
| 113 | point[0] = (float)x; |
| 114 | point[1] = windH - (float)y; |
| 115 | point[2] = 0.0; |
| 116 | glRasterPos3fv(point); |
| 117 | |
| 118 | glPixelZoom(zoom, zoom); |
| 119 | glCopyPixels((windW/2)-(image->sizeX/2), |
| 120 | (windH/2)-(image->sizeY/2), |
| 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 = 300; |
| 174 | windH = 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 | Init(); |
| 186 | |
| 187 | glutReshapeFunc(Reshape); |
| 188 | glutKeyboardFunc(Key); |
| 189 | glutMouseFunc(Mouse); |
| 190 | glutDisplayFunc(Draw); |
| 191 | glutMainLoop(); |
| 192 | return 0; |
| 193 | } |