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.
- */
-
- /* convolution.c
- * A program to demonstrate the use of the convolution extention.
- * It reads an image from a file and displays it in the left half of the
- * window. It then uses a convolution filter to sharpen the
- * image and displays it in the right half of the window.
- *
- * Escape key - exit the program
- */
- #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 );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static unsigned int *image;
- static int imgWidth, imgHeight;
- static GLsizei winWidth, winHeight;
-
- 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], &imgWidth, &imgHeight);
- winWidth = imgWidth * 2;
- winHeight = imgHeight;
-
- glutInitWindowSize( 4, 4 );
- glutInitWindowSize( winWidth, winHeight );
- glutInitDisplayMode( GLUT_RGBA );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - uses convolution extensions to display "
- "a sharpened images\n\n"
- "Escape key - exit the program\n",
- progname);
- }
-
- GLvoid
- initgfx( void )
- {
- GLfloat filterscale[4] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat filterbias[4] = { 0.0, 0.0, 0.0, 0.0 };
- GLfloat postconvScale[4] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat postconvBias [4] = { 0.0, 0.0, 0.0, 0.0 };
-
- glClearColor( 0, 0, 0, 1 );
-
- if ( !glutExtensionSupported( "GL_EXT_convolution" ) ) {
- fprintf(stderr,
- "EXT_convolution not supported in this implementation\n");
- }
-
- #ifdef GL_EXT_convolution
- glConvolutionParameteriEXT(GL_CONVOLUTION_2D_EXT,
- GL_CONVOLUTION_BORDER_MODE_EXT,
- GL_REDUCE_EXT /* only value currenly supported */);
-
- glConvolutionParameterfvEXT(GL_CONVOLUTION_2D_EXT,
- GL_CONVOLUTION_FILTER_SCALE_EXT, filterscale);
-
- glConvolutionParameterfvEXT(GL_CONVOLUTION_2D_EXT,
- GL_CONVOLUTION_FILTER_BIAS_EXT, filterbias);
-
- glPixelTransferf(GL_POST_CONVOLUTION_RED_SCALE_EXT, postconvScale[0]);
- glPixelTransferf(GL_POST_CONVOLUTION_GREEN_SCALE_EXT, postconvScale[1]);
- glPixelTransferf(GL_POST_CONVOLUTION_BLUE_SCALE_EXT, postconvScale[2]);
- glPixelTransferf(GL_POST_CONVOLUTION_ALPHA_SCALE_EXT, postconvScale[3]);
-
- glPixelTransferf(GL_POST_CONVOLUTION_RED_BIAS_EXT, postconvBias[0]);
- glPixelTransferf(GL_POST_CONVOLUTION_GREEN_BIAS_EXT, postconvBias[1]);
- glPixelTransferf(GL_POST_CONVOLUTION_BLUE_BIAS_EXT, postconvBias[2]);
- glPixelTransferf(GL_POST_CONVOLUTION_ALPHA_BIAS_EXT, postconvBias[3]);
- #endif
- }
-
- 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 )
- {
- winWidth = width;
- winHeight = height;
-
- /* Create the first viewport - the left half of the window */
- glViewport( 0, 0, winWidth/2, winHeight );
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D( 0.0, (GLdouble) winWidth-1, 0.0, (GLdouble) winHeight-1 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.375, 0.375, 0.0 );
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static GLfloat sharpenImage[3*3] = {
- -0.5, -0.5, -0.5,
- -0.5, 5.0, -0.5,
- -0.5, -0.5, -0.5
- };
-
- /* Create the first viewport - the left half of the window */
- glViewport( 0, 0, winWidth/2, winHeight );
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- glRasterPos2f( 0, 0 );
- glDrawPixels( imgWidth, imgHeight, GL_RGBA, GL_UNSIGNED_BYTE, image );
-
- glFinish();
-
- /* Create the second viewport - right half of window */
- glViewport( winWidth/2 + 1, 0, winWidth/2, winHeight );
-
- #ifdef GL_EXT_convolution
- glEnable(GL_CONVOLUTION_2D_EXT);
-
- /* draw pixels with convolution filter */
- glConvolutionFilter2DEXT(GL_CONVOLUTION_2D_EXT,
- GL_LUMINANCE,
- 3, 3,
- GL_LUMINANCE,
- GL_FLOAT,
- (const void*)sharpenImage);
- #endif
-
- glRasterPos2f( 0, 0 );
- glDrawPixels( imgWidth, imgHeight, GL_RGBA, GL_UNSIGNED_BYTE, image );
-
- #ifdef GL_EXT_convolution
- glDisable(GL_CONVOLUTION_2D_EXT);
- #endif
- glFlush();
-
- checkError("drawScene");
- }
-