home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / image_extensions / convolution.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.2 KB  |  203 lines

  1. /*
  2.  * Copyright 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* convolution.c
  19.  * A program to demonstrate the use of the convolution extention.
  20.  * It reads an image from a file and displays it in the left half of the 
  21.  * window. It then uses a convolution filter to sharpen the 
  22.  * image and displays it in the right half of the window.
  23.  * 
  24.  *    Escape key            - exit the program
  25.  */
  26. #include <GL/gl.h>
  27. #include <GL/glu.h>
  28. #include <GL/glut.h>
  29.  
  30. #include <math.h>
  31. #include <stdio.h>
  32.  
  33. #include "rgbImageFile.h"    /* should be in ../../include */
  34.  
  35. /*  Function Prototypes  */
  36.  
  37. GLvoid  initgfx( GLvoid );
  38. GLvoid  drawScene( GLvoid );
  39. GLvoid  reshape( GLsizei, GLsizei );
  40. GLvoid  keyboard( GLubyte, GLint, GLint );
  41.  
  42. void printHelp( char * );
  43.  
  44. /* Global Definitions */
  45.  
  46. #define KEY_ESC    27    /* ascii value for the escape key */
  47.  
  48. /* Global Variables */
  49.  
  50. static unsigned int     *image;
  51. static int        imgWidth, imgHeight;
  52. static GLsizei        winWidth, winHeight;
  53.  
  54. GLvoid
  55. main( int argc, char *argv[] )
  56. {
  57.     GLsizei            width, height;
  58.  
  59.     glutInit( &argc, argv );
  60.  
  61.     if (argc < 2) {
  62.         fprintf( stderr, "usage: %s <imageFileName>\n", argv[0] );
  63.         exit (1);
  64.     }
  65.  
  66.     image = rgbReadImageFile(argv[1], &imgWidth, &imgHeight);
  67.     winWidth = imgWidth * 2;
  68.     winHeight = imgHeight;
  69.     
  70.     glutInitWindowSize( 4, 4 );
  71.     glutInitWindowSize( winWidth, winHeight ); 
  72.     glutInitDisplayMode( GLUT_RGBA );
  73.     glutCreateWindow( argv[0] );
  74.     
  75.     initgfx();
  76.  
  77.     glutKeyboardFunc( keyboard );
  78.     glutReshapeFunc( reshape );
  79.     glutDisplayFunc( drawScene ); 
  80.  
  81.     printHelp( argv[0] );
  82.  
  83.      glutMainLoop();
  84. }
  85.  
  86. GLvoid
  87. printHelp( char *progname )
  88. {
  89.     fprintf(stdout, "\n%s - uses convolution extensions to display "
  90.         "a sharpened images\n\n"
  91.         "Escape key                - exit the program\n",
  92.         progname);
  93. }
  94.  
  95. GLvoid
  96. initgfx( void )
  97. {
  98.     GLfloat filterscale[4] = { 1.0, 1.0, 1.0, 1.0 };
  99.     GLfloat filterbias[4]  = { 0.0, 0.0, 0.0, 0.0 };
  100.     GLfloat postconvScale[4] = { 1.0, 1.0, 1.0, 1.0 };
  101.     GLfloat postconvBias [4]  = { 0.0, 0.0, 0.0, 0.0 };
  102.  
  103.     glClearColor( 0, 0, 0, 1 );
  104.  
  105.     if ( !glutExtensionSupported( "GL_EXT_convolution" ) ) {
  106.         fprintf(stderr, 
  107.          "EXT_convolution not supported in this implementation\n");
  108.     }
  109.  
  110. #ifdef    GL_EXT_convolution
  111.     glConvolutionParameteriEXT(GL_CONVOLUTION_2D_EXT,
  112.             GL_CONVOLUTION_BORDER_MODE_EXT,
  113.             GL_REDUCE_EXT /* only value currenly supported */);
  114.  
  115.     glConvolutionParameterfvEXT(GL_CONVOLUTION_2D_EXT,
  116.             GL_CONVOLUTION_FILTER_SCALE_EXT, filterscale);
  117.  
  118.     glConvolutionParameterfvEXT(GL_CONVOLUTION_2D_EXT,
  119.             GL_CONVOLUTION_FILTER_BIAS_EXT, filterbias);
  120.  
  121.     glPixelTransferf(GL_POST_CONVOLUTION_RED_SCALE_EXT, postconvScale[0]);
  122.     glPixelTransferf(GL_POST_CONVOLUTION_GREEN_SCALE_EXT, postconvScale[1]);
  123.     glPixelTransferf(GL_POST_CONVOLUTION_BLUE_SCALE_EXT, postconvScale[2]);
  124.     glPixelTransferf(GL_POST_CONVOLUTION_ALPHA_SCALE_EXT, postconvScale[3]);
  125.  
  126.     glPixelTransferf(GL_POST_CONVOLUTION_RED_BIAS_EXT, postconvBias[0]);
  127.     glPixelTransferf(GL_POST_CONVOLUTION_GREEN_BIAS_EXT, postconvBias[1]);
  128.     glPixelTransferf(GL_POST_CONVOLUTION_BLUE_BIAS_EXT, postconvBias[2]);
  129.     glPixelTransferf(GL_POST_CONVOLUTION_ALPHA_BIAS_EXT, postconvBias[3]);
  130. #endif
  131. }
  132.  
  133. GLvoid 
  134. keyboard( GLubyte key, GLint x, GLint y )
  135. {
  136.     switch (key) {
  137.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  138.         exit(0);
  139.     }
  140. }
  141.  
  142. GLvoid
  143. reshape( GLsizei width, GLsizei height )
  144. {
  145.     winWidth = width;
  146.     winHeight = height;
  147.  
  148.     /* Create the first viewport - the left half of the window */
  149.     glViewport( 0, 0, winWidth/2, winHeight );
  150.  
  151.     glMatrixMode( GL_PROJECTION );
  152.     glLoadIdentity();
  153.     gluOrtho2D( 0.0, (GLdouble) winWidth-1, 0.0, (GLdouble) winHeight-1 );
  154.     glMatrixMode( GL_MODELVIEW );
  155.     glLoadIdentity();
  156.     glTranslatef( 0.375, 0.375, 0.0 );
  157. }
  158.     
  159. GLvoid
  160. drawScene( GLvoid )
  161. {
  162.     static GLfloat sharpenImage[3*3] = { 
  163.             -0.5,        -0.5,         -0.5, 
  164.             -0.5,         5.0,         -0.5, 
  165.             -0.5,        -0.5,         -0.5
  166.             };
  167.  
  168.     /* Create the first viewport - the left half of the window */
  169.     glViewport( 0, 0, winWidth/2, winHeight );
  170.  
  171.     glClear( GL_COLOR_BUFFER_BIT );
  172.     
  173.     glRasterPos2f( 0, 0 );
  174.     glDrawPixels( imgWidth, imgHeight, GL_RGBA, GL_UNSIGNED_BYTE, image ); 
  175.  
  176.     glFinish();
  177.  
  178.     /* Create the second viewport - right half of window */
  179.     glViewport( winWidth/2 + 1, 0, winWidth/2, winHeight );
  180.  
  181. #ifdef    GL_EXT_convolution
  182.     glEnable(GL_CONVOLUTION_2D_EXT);
  183.     
  184.     /* draw pixels with convolution filter */
  185.     glConvolutionFilter2DEXT(GL_CONVOLUTION_2D_EXT,
  186.                 GL_LUMINANCE,
  187.                 3, 3,
  188.                 GL_LUMINANCE,
  189.                 GL_FLOAT,
  190.                 (const void*)sharpenImage);
  191. #endif
  192.  
  193.     glRasterPos2f( 0, 0 );
  194.     glDrawPixels( imgWidth, imgHeight, GL_RGBA, GL_UNSIGNED_BYTE, image ); 
  195.  
  196. #ifdef    GL_EXT_convolution
  197.     glDisable(GL_CONVOLUTION_2D_EXT);
  198. #endif
  199.     glFlush();
  200.  
  201.     checkError("drawScene");
  202. }
  203.