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  *  minmax.c
45  *  Determine the minimum and maximum values of a group of pixels.
46  *  This demonstrates use of the glMinmax() call.
47  */
48 #include <glad/glad.h>
49 #include "glut_wrap.h"
50 #include <assert.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 
54 
55 static GLubyte  *pixels;
56 static GLsizei   width, height;
57 
58 
bswap(GLuint x)59 static GLuint bswap(GLuint x)
60 {
61    const GLuint ui = 1;
62    const GLubyte *ubp = (const GLubyte *) &ui;
63    if (*ubp == 1) {
64       /* we're on little endiang so byteswap x */
65       GLsizei y =  ((x >> 24)
66                     | ((x >> 8) & 0xff00)
67                     | ((x << 8) & 0xff0000)
68                     | ((x << 24) & 0xff000000));
69       return y;
70    }
71    else {
72       return x;
73    }
74 }
75 
76 
77 static GLubyte *
readImage(const char * filename,GLsizei * width,GLsizei * height)78 readImage( const char* filename, GLsizei* width, GLsizei *height )
79 {
80     int       n;
81     GLubyte*  pixels;
82     size_t    num_read;
83 
84     FILE* infile = fopen( filename, "rb" );
85 
86     if ( !infile ) {
87 	fprintf( stderr, "Unable to open file '%s'\n", filename );
88 	return NULL;
89     }
90 
91     num_read = fread( width, sizeof( GLsizei ), 1, infile );
92     assert(num_read == 1);
93     num_read = fread( height, sizeof( GLsizei ), 1, infile );
94     assert(num_read == 1);
95 
96     *width = bswap(*width);
97     *height = bswap(*height);
98 
99     n = 3 * (*width) * (*height);
100 
101     pixels = (GLubyte *) malloc( n * sizeof( GLubyte ));
102     if ( !pixels ) {
103 	fprintf( stderr, "Unable to malloc() bytes for pixels\n" );
104 	fclose( infile );
105 	return NULL;
106     }
107 
108     num_read = fread( pixels, sizeof( GLubyte ), n, infile );
109     assert(num_read == n);
110 
111     fclose( infile );
112 
113     return pixels;
114 }
115 
init(void)116 static void init(void)
117 {
118    if (!glutExtensionSupported("GL_ARB_imaging")) {
119       fprintf(stderr, "Sorry, this program requires GL_ARB_imaging.\n");
120       exit(1);
121    }
122 
123    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
124    glClearColor(0.0, 0.0, 0.0, 0.0);
125 
126    glMinmax(GL_MINMAX, GL_RGB, GL_FALSE);
127    glEnable(GL_MINMAX);
128 }
129 
display(void)130 static void display(void)
131 {
132    GLubyte  values[6];
133 
134    glClear(GL_COLOR_BUFFER_BIT);
135    glRasterPos2i(1, 1);
136    glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
137    glFlush();
138 
139    glGetMinmax(GL_MINMAX, GL_TRUE, GL_RGB, GL_UNSIGNED_BYTE, values);
140    printf(" Red   : min = %d   max = %d\n", values[0], values[3]);
141    printf(" Green : min = %d   max = %d\n", values[1], values[4]);
142    printf(" Blue  : min = %d   max = %d\n", values[2], values[5]);
143 }
144 
reshape(int w,int h)145 static void reshape(int w, int h)
146 {
147    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
148    glMatrixMode(GL_PROJECTION);
149    glLoadIdentity();
150    glOrtho(0, w, 0, h, -1.0, 1.0);
151    glMatrixMode(GL_MODELVIEW);
152 }
153 
keyboard(unsigned char key,int x,int y)154 static void keyboard(unsigned char key, int x, int y)
155 {
156    switch (key) {
157       case 27:
158          exit(0);
159    }
160 }
161 
162 /*  Main Loop
163  *  Open window with initial window size, title bar,
164  *  RGBA display mode, and handle input events.
165  */
main(int argc,char ** argv)166 int main(int argc, char** argv)
167 {
168    pixels = readImage("leeds.bin", &width, &height);
169 
170    glutInit(&argc, argv);
171    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
172    glutInitWindowSize(width, height);
173    glutInitWindowPosition(100, 100);
174    glutCreateWindow(argv[0]);
175    gladLoadGL();
176    init();
177    glutReshapeFunc(reshape);
178    glutKeyboardFunc(keyboard);
179    glutDisplayFunc(display);
180    glutMainLoop();
181    return 0;
182 }
183