home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1995, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* pan.c
- * This program takes the name of an image file in SGI .rgb
- * format. It opens the file and creates a window displaying
- * a subimage taken from the middle of the image.
- *
- * The image can be panned by dragging the mouse with the
- * Left Mouse button pressed.
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include "rgbImageFile.h" /* should be in ../../include */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
-
- GLvoid adjustSkipParams( GLvoid );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- GLuint *image;
- GLint skipRows, skipPixels;
- GLint xStart, yStart;
-
- GLint imageWidth, imageHeight;
- GLint subimageWidth, subimageHeight;
-
- GLboolean panning = GL_FALSE;
-
- GLvoid
- main ( int argc, char *argv[])
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- if (argc < 2) {
- fprintf (stderr, "usage: %s <imageFileName>\n",
- argv[0] );
- exit (1);
- }
-
- image = rgbReadImageFile(argv[1], &imageWidth, &imageHeight);
-
- /* set up the window size and the sub-image size
- * to be 1/4 of the original image size.
- */
- subimageWidth = imageWidth/2;
- subimageHeight = imageHeight/2;
-
- glutInitWindowPosition( 100, 100 );
- glutInitWindowSize( subimageWidth, subimageHeight );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout,
- "\n%s, a program to pan an image\n\n"\
- "Escape key - exit the program\n"\
- "Left Mousebutton, down - pan image\n\n",
- progname);
- }
-
- GLvoid
- initgfx( void )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- glViewport( 0, 0, width, height);
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D( 0.0, (GLdouble) width, 0.0, (GLdouble) height );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.375, 0.375, 0.0 );
-
- subimageWidth = width;
- subimageHeight = height;
- if (subimageWidth > imageWidth)
- subimageWidth = imageWidth;
- if (subimageHeight > imageHeight)
- subimageHeight = imageHeight;
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- if (state == GLUT_DOWN) {
- xStart = x; /* save current mouse position */
- yStart = y;
- panning = GL_TRUE;
- } else {
- panning = GL_FALSE;
- }
- break;
- }
- }
-
- GLvoid
- motion( int x, int y )
- {
- if (panning) {
- /* adjust skip info based on how far the mouse moved */
- skipPixels -= (x - xStart);
- skipRows += (y - yStart);
-
-
- xStart = x; /* Update the stored mouse position */
- yStart = y;
-
- glutPostRedisplay();
- }
- }
-
- /* make sure the selected rectangle is always
- * completely inside the original image
- */
- GLvoid
- adjustSkipParams( GLvoid )
- {
- if (skipPixels < 0)
- skipPixels = 0;
- if (skipPixels > imageWidth-subimageWidth)
- skipPixels = imageWidth-subimageWidth;
-
- if (skipRows < 0)
- skipRows = 0;
- if (skipRows >= imageHeight-subimageHeight)
- skipRows = imageHeight-subimageHeight;
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- glClear( GL_COLOR_BUFFER_BIT );
-
- adjustSkipParams ();
-
- /* Set up to grab a portion of the original stored image. */
-
- /* Set the pixel width of the original image. */
- glPixelStorei( GL_UNPACK_ROW_LENGTH, imageWidth );
-
- /* Indicate how many pixels to skip in the x and y directions
- * (from the lower left corner of the original image) */
- glPixelStorei( GL_UNPACK_SKIP_PIXELS, skipPixels );
- glPixelStorei( GL_UNPACK_SKIP_ROWS, skipRows );
-
- /* In this example, the xy coordinates to map directly
- * to window coordinates. This would not be the case if any
- * projection or modeling transforms had been applied.
- */
- glRasterPos2i( 0, 0 );
- glDrawPixels (subimageWidth, subimageHeight, GL_RGBA,
- GL_UNSIGNED_BYTE, image);
-
- glutSwapBuffers();
- }
-