histogram.c revision 32001f49
1/*
2 * Copyright (c) 1993-2003, Silicon Graphics, Inc.
3 * All Rights Reserved
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose and without fee is hereby granted, provided that the above
7 * copyright notice appear in all copies and that both the copyright
8 * notice and this permission notice appear in supporting documentation,
9 * and that the name of Silicon Graphics, Inc. not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission.
12 *
13 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
14 * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
16 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
17 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
18 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
20 * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD
21 * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF
22 * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE
24 * OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * US Government Users Restricted Rights
27 * Use, duplication, or disclosure by the Government is subject to
28 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
29 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
30 * clause at DFARS 252.227-7013 and/or in similar or successor clauses
31 * in the FAR or the DOD or NASA FAR Supplement.  Unpublished - rights
32 * reserved under the copyright laws of the United States.
33 *
34 * Contractor/manufacturer is:
35 *	Silicon Graphics, Inc.
36 *	1500 Crittenden Lane
37 *	Mountain View, CA  94043
38 *	United State of America
39 *
40 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
41 */
42
43/*
44 *  histogram.c
45 *  Compute the histogram of the image.  This program illustrates the
46 *  use of the glHistogram() function.
47 */
48
49#include <GL/glew.h>
50#include "glut_wrap.h"
51#include <assert.h>
52#include <stdlib.h>
53#include <stdio.h>
54
55#define HISTOGRAM_SIZE  256   /* Must be a power of 2 */
56
57
58static GLubyte  *pixels;
59static GLsizei   width, height;
60
61
62
63static GLuint bswap(GLuint x)
64{
65   const GLuint ui = 1;
66   const GLubyte *ubp = (const GLubyte *) &ui;
67   if (*ubp == 1) {
68      /* we're on little endiang so byteswap x */
69      GLsizei y =  ((x >> 24)
70                    | ((x >> 8) & 0xff00)
71                    | ((x << 8) & 0xff0000)
72                    | ((x << 24) & 0xff000000));
73      return y;
74   }
75   else {
76      return x;
77   }
78}
79
80
81static GLubyte*
82readImage( const char* filename, GLsizei* width, GLsizei *height )
83{
84    int       n;
85    GLubyte*  pixels;
86    size_t    num_read;
87
88    FILE* infile = fopen( filename, "rb" );
89
90    if ( !infile ) {
91	fprintf( stderr, "Unable to open file '%s'\n", filename );
92        exit(1);
93    }
94
95    num_read = fread( width, sizeof( GLsizei ), 1, infile );
96    assert(num_read == 1);
97    num_read = fread( height, sizeof( GLsizei ), 1, infile );
98    assert(num_read == 1);
99
100    *width = bswap(*width);
101    *height = bswap(*height);
102
103    n = 3 * (*width) * (*height);
104
105    pixels = (GLubyte *) malloc( n * sizeof( GLubyte ));
106    if ( !pixels ) {
107	fprintf( stderr, "Unable to malloc() bytes for pixels\n" );
108	fclose( infile );
109	return NULL;
110    }
111
112    num_read = fread( pixels, sizeof( GLubyte ), n, infile );
113    assert(num_read == n);
114
115    fclose( infile );
116
117    return pixels;
118}
119
120static void init(void)
121{
122   if (!glutExtensionSupported("GL_ARB_imaging")) {
123      fprintf(stderr, "Sorry, this program requires GL_ARB_imaging.\n");
124      exit(1);
125   }
126
127   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
128   glClearColor(0.0, 0.0, 0.0, 0.0);
129
130   glHistogram(GL_HISTOGRAM, HISTOGRAM_SIZE, GL_RGB, GL_FALSE);
131   glEnable(GL_HISTOGRAM);
132}
133
134static void display(void)
135{
136   int i;
137   GLushort values[HISTOGRAM_SIZE][3];
138
139   glClear(GL_COLOR_BUFFER_BIT);
140   glRasterPos2i(1, 1);
141   glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
142
143   glGetHistogram(GL_HISTOGRAM, GL_TRUE, GL_RGB, GL_UNSIGNED_SHORT, values);
144
145   /* Plot histogram */
146
147   glBegin(GL_LINE_STRIP);
148   glColor3f(1.0, 0.0, 0.0);
149   for ( i = 0; i < HISTOGRAM_SIZE; i++ )
150       glVertex2s(i, values[i][0]);
151   glEnd();
152
153   glBegin(GL_LINE_STRIP);
154   glColor3f(0.0, 1.0, 0.0);
155   for ( i = 0; i < HISTOGRAM_SIZE; i++ )
156       glVertex2s(i, values[i][1]);
157   glEnd();
158
159   glBegin(GL_LINE_STRIP);
160   glColor3f(0.0, 0.0, 1.0);
161   for ( i = 0; i < HISTOGRAM_SIZE; i++ )
162       glVertex2s(i, values[i][2]);
163   glEnd();
164   glFlush();
165}
166
167static void reshape(int w, int h)
168{
169   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
170   glMatrixMode(GL_PROJECTION);
171   glLoadIdentity();
172   glOrtho(0, 256, 0, 10000, -1.0, 1.0);
173   glMatrixMode(GL_MODELVIEW);
174}
175
176static void keyboard(unsigned char key, int x, int y)
177{
178   static GLboolean sink = GL_FALSE;
179
180   switch (key) {
181      case 's' :
182	  sink = !sink;
183	  glHistogram(GL_HISTOGRAM, HISTOGRAM_SIZE, GL_RGB, sink);
184	  break;
185
186      case 27:
187         exit(0);
188   }
189   glutPostRedisplay();
190
191}
192
193/*  Main Loop
194 *  Open window with initial window size, title bar,
195 *  RGBA display mode, and handle input events.
196 */
197int main(int argc, char** argv)
198{
199   pixels = readImage("leeds.bin", &width, &height);
200
201   glutInit(&argc, argv);
202   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
203   glutInitWindowSize(width, height);
204   glutInitWindowPosition(100, 100);
205   glutCreateWindow(argv[0]);
206   glewInit();
207   init();
208   glutReshapeFunc(reshape);
209   glutKeyboardFunc(keyboard);
210   glutDisplayFunc(display);
211   glutMainLoop();
212   return 0;
213}
214