132001f49Smrg/*
232001f49Smrg * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
332001f49Smrg *
432001f49Smrg * Permission to use, copy, modify, distribute, and sell this software and
532001f49Smrg * its documentation for any purpose is hereby granted without fee, provided
632001f49Smrg * that (i) the above copyright notices and this permission notice appear in
732001f49Smrg * all copies of the software and related documentation, and (ii) the name of
832001f49Smrg * Silicon Graphics may not be used in any advertising or
932001f49Smrg * publicity relating to the software without the specific, prior written
1032001f49Smrg * permission of Silicon Graphics.
1132001f49Smrg *
1232001f49Smrg * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
1332001f49Smrg * ANY KIND,
1432001f49Smrg * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
1532001f49Smrg * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
1632001f49Smrg *
1732001f49Smrg * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
1832001f49Smrg * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
1932001f49Smrg * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
2032001f49Smrg * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
2132001f49Smrg * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
2232001f49Smrg * OF THIS SOFTWARE.
2332001f49Smrg */
2432001f49Smrg
2532001f49Smrg#include <stdio.h>
2632001f49Smrg#include <string.h>
2732001f49Smrg#include <stdlib.h>
2832001f49Smrg#include <GL/glew.h>
2932001f49Smrg#include "glut_wrap.h"
3032001f49Smrg
3132001f49Smrg
3232001f49Smrg#include "loadppm.c"
3332001f49Smrg
3432001f49SmrgGLenum doubleBuffer;
3532001f49SmrgGLint windW, windH;
3632001f49Smrg
3732001f49Smrgchar *fileName = 0;
3832001f49SmrgPPMImage *image;
3932001f49Smrgfloat zoom;
4032001f49SmrgGLint x, y;
4132001f49Smrg
4232001f49Smrgstatic void Init(void)
4332001f49Smrg{
4432001f49Smrg
4532001f49Smrg    glClearColor(0.0, 0.0, 0.0, 0.0);
4632001f49Smrg
4732001f49Smrg    x = 0;
4832001f49Smrg    y = windH;
4932001f49Smrg    zoom = 1.8;
5032001f49Smrg}
5132001f49Smrg
5232001f49Smrgstatic void Reshape(int width, int height)
5332001f49Smrg{
5432001f49Smrg
5532001f49Smrg    windW = (GLint)width;
5632001f49Smrg    windH = (GLint)height;
5732001f49Smrg
5832001f49Smrg    glViewport(0, 0, windW, windH);
5932001f49Smrg
6032001f49Smrg    glMatrixMode(GL_PROJECTION);
6132001f49Smrg    glLoadIdentity();
6232001f49Smrg    gluOrtho2D(0, windW, 0, windH);
6332001f49Smrg    glMatrixMode(GL_MODELVIEW);
6432001f49Smrg}
6532001f49Smrg
6632001f49Smrgstatic void Key(unsigned char key, int x, int y)
6732001f49Smrg{
6832001f49Smrg
6932001f49Smrg    switch (key) {
7032001f49Smrg      case 27:
7132001f49Smrg        exit(1);
7232001f49Smrg      case 'Z':
7332001f49Smrg	zoom += 0.2;
7432001f49Smrg	break;
7532001f49Smrg      case 'z':
7632001f49Smrg	zoom -= 0.2;
7732001f49Smrg	if (zoom < 0.2) {
7832001f49Smrg	    zoom = 0.2;
7932001f49Smrg	}
8032001f49Smrg	break;
8132001f49Smrg      default:
8232001f49Smrg	return;
8332001f49Smrg    }
8432001f49Smrg
8532001f49Smrg    glutPostRedisplay();
8632001f49Smrg}
8732001f49Smrg
8832001f49Smrgstatic void Mouse(int button, int state, int mouseX, int mouseY)
8932001f49Smrg{
9032001f49Smrg    if (state != GLUT_DOWN)
9132001f49Smrg	return;
9232001f49Smrg    x = (GLint)mouseX;
9332001f49Smrg    y = (GLint)mouseY;
9432001f49Smrg
9532001f49Smrg    glutPostRedisplay();
9632001f49Smrg}
9732001f49Smrg
9832001f49Smrgstatic void Draw(void)
9932001f49Smrg{
10032001f49Smrg    GLint src[3], dst[3];
10132001f49Smrg
10232001f49Smrg    glClear(GL_COLOR_BUFFER_BIT);
10332001f49Smrg
10432001f49Smrg    src[0] = (int) ((windW / 2.0) - (image->sizeX / 2.0));
10532001f49Smrg    src[1] = (int) ((windH / 2.0) - (image->sizeY / 2.0));
10632001f49Smrg    src[2] = 0;
10732001f49Smrg    glWindowPos3ivARB(src);
10832001f49Smrg
10932001f49Smrg    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
11032001f49Smrg    glPixelZoom(1.0, 1.0);
11132001f49Smrg    glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
11232001f49Smrg		 image->data);
11332001f49Smrg
11432001f49Smrg    dst[0] = x;
11532001f49Smrg    dst[1] = windH - y;
11632001f49Smrg    dst[2] = 0;
11732001f49Smrg    glWindowPos3ivARB(dst);
11832001f49Smrg
11932001f49Smrg    glPixelZoom(zoom, zoom);
12032001f49Smrg    glCopyPixels(src[0], src[1],
12132001f49Smrg		 image->sizeX, image->sizeY, GL_COLOR);
12232001f49Smrg
12332001f49Smrg    glFlush();
12432001f49Smrg
12532001f49Smrg    if (doubleBuffer) {
12632001f49Smrg	glutSwapBuffers();
12732001f49Smrg    }
12832001f49Smrg}
12932001f49Smrg
13032001f49Smrgstatic GLenum Args(int argc, char **argv)
13132001f49Smrg{
13232001f49Smrg    GLint i;
13332001f49Smrg
13432001f49Smrg    doubleBuffer = GL_FALSE;
13532001f49Smrg
13632001f49Smrg    for (i = 1; i < argc; i++) {
13732001f49Smrg	if (strcmp(argv[i], "-sb") == 0) {
13832001f49Smrg	    doubleBuffer = GL_FALSE;
13932001f49Smrg	} else if (strcmp(argv[i], "-db") == 0) {
14032001f49Smrg	    doubleBuffer = GL_TRUE;
14132001f49Smrg	} else if (strcmp(argv[i], "-f") == 0) {
14232001f49Smrg	    if (i+1 >= argc || argv[i+1][0] == '-') {
14332001f49Smrg		printf("-f (No file name).\n");
14432001f49Smrg		return GL_FALSE;
14532001f49Smrg	    } else {
14632001f49Smrg		fileName = argv[++i];
14732001f49Smrg	    }
14832001f49Smrg	} else {
14932001f49Smrg	    printf("%s (Bad option).\n", argv[i]);
15032001f49Smrg	    return GL_FALSE;
15132001f49Smrg	}
15232001f49Smrg    }
15332001f49Smrg    return GL_TRUE;
15432001f49Smrg}
15532001f49Smrg
15632001f49Smrgint main(int argc, char **argv)
15732001f49Smrg{
15832001f49Smrg    GLenum type;
15932001f49Smrg
16032001f49Smrg    glutInit(&argc, argv);
16132001f49Smrg
16232001f49Smrg    if (Args(argc, argv) == GL_FALSE) {
16332001f49Smrg	exit(1);
16432001f49Smrg    }
16532001f49Smrg
16632001f49Smrg    if (fileName == 0) {
16732001f49Smrg	printf("No image file.\n");
16832001f49Smrg	exit(1);
16932001f49Smrg    }
17032001f49Smrg
17132001f49Smrg    image = LoadPPM(fileName);
17232001f49Smrg
17332001f49Smrg    windW = 2*300;
17432001f49Smrg    windH = 2*300;
17532001f49Smrg    glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH);
17632001f49Smrg
17732001f49Smrg    type = GLUT_RGB;
17832001f49Smrg    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
17932001f49Smrg    glutInitDisplayMode(type);
18032001f49Smrg
18132001f49Smrg    if (glutCreateWindow("Copy Test") == GL_FALSE) {
18232001f49Smrg	exit(1);
18332001f49Smrg    }
18432001f49Smrg
18532001f49Smrg    glewInit();
18632001f49Smrg    Init();
18732001f49Smrg
18832001f49Smrg    glutReshapeFunc(Reshape);
18932001f49Smrg    glutKeyboardFunc(Key);
19032001f49Smrg    glutMouseFunc(Mouse);
19132001f49Smrg    glutDisplayFunc(Draw);
19232001f49Smrg    glutMainLoop();
19332001f49Smrg	return 0;
19432001f49Smrg}
195