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 / histogram.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.5 KB  |  231 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. /*    histogram.c
  19.  *    A program to demonstrate the use of the histogram extension.
  20.  *    It reads an image and displays it in the left half of the window.
  21.  *    In the right half of the window the histogram is displayed for 
  22.  *    the image.
  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. /* size of histogram table */
  55. static int        histWidth = 256;
  56.  
  57. GLvoid
  58. main( int argc, char *argv[] )
  59. {
  60.     GLsizei            width, height;
  61.  
  62.     glutInit( &argc, argv );
  63.  
  64.     if (argc < 2) {
  65.         fprintf( stderr, "usage: %s <imageFileName>\n", argv[0] );
  66.         exit (1);
  67.     }
  68.  
  69.     image = rgbReadImageFile(argv[1], &imgWidth, &imgHeight);
  70.     winWidth = imgWidth * 2;
  71.     winHeight = imgHeight;
  72.     
  73.     glutInitWindowSize( 4, 4 );
  74.     glutInitWindowSize( winWidth, winHeight ); 
  75.     glutInitDisplayMode( GLUT_RGBA );
  76.     glutCreateWindow( argv[0] );
  77.     
  78.     initgfx();
  79.  
  80.     glutKeyboardFunc( keyboard );
  81.     glutReshapeFunc( reshape );
  82.     glutDisplayFunc( drawScene ); 
  83.  
  84.     printHelp( argv[0] );
  85.  
  86.      glutMainLoop();
  87. }
  88.  
  89. GLvoid
  90. printHelp( char *progname )
  91. {
  92.     fprintf(stdout, "\n%s - use the hisgtogram extension to display an\n"
  93.         "image with its histogram\n\n"
  94.         "Escape key                - exit the program\n",
  95.         progname);
  96. }
  97.  
  98. GLvoid
  99. initgfx( void )
  100. {
  101.     if ( !glutExtensionSupported( "GL_EXT_histogram" ) ) {
  102.         fprintf(stderr, 
  103.           "EXT_histogram not supported in this implementation\n");
  104.     }
  105.  
  106.  
  107. #ifdef    GL_EXT_histogram
  108.     glEnable(GL_HISTOGRAM_EXT);
  109.         glHistogramEXT(GL_HISTOGRAM_EXT,
  110.             histWidth /* width */,
  111.             GL_RGBA /* internalformat */,
  112.             GL_FALSE /* sink */);
  113.  
  114.     glEnable(GL_MINMAX_EXT);
  115.     glMinmaxEXT(GL_MINMAX_EXT,
  116.             GL_RGBA /* internalformat */,
  117.             GL_FALSE /* sink */);
  118. #endif
  119. }
  120.  
  121. GLvoid 
  122. keyboard( GLubyte key, GLint x, GLint y )
  123. {
  124.     switch (key) {
  125.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  126.         exit(0);
  127.     }
  128. }
  129.  
  130. GLvoid
  131. reshape( GLsizei width, GLsizei height )
  132. {
  133.     winWidth = width;
  134.     winHeight = height;
  135.  
  136.     /* Create the first viewport - the left half of the window */
  137.     glViewport( 0, 0, winWidth/2, winHeight );
  138.  
  139.     glMatrixMode( GL_PROJECTION );
  140.     glLoadIdentity();
  141.     gluOrtho2D( 0.0, (GLdouble) winWidth-1, 0.0, (GLdouble) winHeight-1 );
  142.     glMatrixMode( GL_MODELVIEW );
  143.     glLoadIdentity();
  144.     glTranslatef( 0.375, 0.375, 0.0 );
  145. }
  146.  
  147. static
  148. void displayHistogram( GLubyte *hgram, GLubyte *minmaxTable  )
  149. {
  150.     int i, j, channels = 3; /* RGB format */
  151.  
  152.     GLfloat colors[3][3];
  153.     GLfloat min[3], max[3];
  154.  
  155.     colors[0][0] = 1.0; colors[0][1] = colors[0][2] = 0.0;
  156.     colors[1][0] = 0.0; colors[1][1] = 1.0; colors[1][2] = 0.0;
  157.     colors[2][0] = 0.0; colors[2][1] = 0.0; colors[2][2] = 1.0;
  158.     min[0] = minmaxTable[0]; max[0] = minmaxTable[3];
  159.     min[1] = minmaxTable[1]; max[1] = minmaxTable[4];
  160.     min[2] = minmaxTable[2]; max[2] = minmaxTable[5];
  161.  
  162.     for (i = 0; i < channels; i++, hgram++) {
  163.         GLubyte *next, *cur;
  164.         GLfloat y, scale, height = imgHeight/channels;
  165.  
  166.         glColor3f(colors[i][0],colors[i][1],colors[i][2]);
  167.  
  168.         cur = hgram;
  169.         next = cur + channels;
  170.         for ( j = 0; j < histWidth; j++ ) {
  171.             if ( *cur < *next ) { cur = next; }
  172.             next += channels;
  173.         }
  174.         scale = (*cur == 0 ? scale = 0.0 : height / (GLfloat)(*cur));
  175.  
  176.         fprintf(stderr, "min[%d]=%f, max[%d]=%f, scale=%f\n", 
  177.             i, min[i], i, max[i], scale);
  178.  
  179.         glBegin(GL_LINE_STRIP);
  180.             next = (GLubyte *)hgram;
  181.             for (j = 0; j < histWidth; j++, next += channels) {
  182.                 y = *next * scale;
  183.                 glVertex2i(j, (GLuint) y);
  184.             }
  185.         glEnd();
  186.                 glTranslatef (0, height, 0.0);
  187.         }
  188. }
  189.  
  190. static GLubyte hgramTable[4096*4];
  191. static GLubyte minmaxTable[3*2*1];
  192.     
  193. GLvoid
  194. drawScene( GLvoid )
  195. {
  196.     /* Create the first viewport - the left half of the window */
  197.     glViewport( 0, 0, winWidth/2, winHeight );
  198.  
  199.     glClear( GL_COLOR_BUFFER_BIT );
  200.     
  201.     glRasterPos2f( 0, 0 );
  202.     glDrawPixels( imgWidth, imgHeight, GL_RGBA, GL_UNSIGNED_BYTE, image ); 
  203.  
  204. #ifdef    GL_EXT_histogram
  205.     glGetHistogramParameterivEXT(GL_HISTOGRAM_EXT,
  206.                 GL_HISTOGRAM_WIDTH_EXT,
  207.                 &histWidth);
  208.  
  209.     glGetHistogramEXT(GL_HISTOGRAM_EXT,
  210.                 GL_FALSE, /* reset */
  211.                 GL_RGB, /* return format */
  212.                 GL_UNSIGNED_BYTE, /* return type */
  213.                 (GLvoid *)hgramTable);
  214.  
  215.     glGetMinmaxEXT(GL_MINMAX_EXT,
  216.                 GL_FALSE, /* reset */
  217.                 GL_RGB, /* return format */
  218.                 GL_UNSIGNED_BYTE, /* return type */
  219.                 (GLvoid *)minmaxTable);
  220. #endif
  221.  
  222.     /* Create the second viewport - right half of window */
  223.     glViewport( winWidth/2 + 1, 0, winWidth/2, winHeight );
  224.  
  225.     displayHistogram( hgramTable, minmaxTable ); 
  226.  
  227.     glFlush();
  228.  
  229.     checkError("drawScene");
  230. }
  231.