1 /*
2  * (C) Copyright IBM Corporation 2005
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22  * USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \file invert.c
27  *
28  * Simple test of GL_MESA_pack_invert functionality.  Three squares are
29  * drawn.  The first two should look the same, and the third one should
30  * look inverted.
31  *
32  * \author Ian Romanick <idr@us.ibm.com>
33  */
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <glad/glad.h>
39 #include "glut_wrap.h"
40 
41 #include "readtex.h"
42 
43 #define IMAGE_FILE DEMOS_DATA_DIR "tree3.rgb"
44 
45 static int Width = 420;
46 static int Height = 150;
47 static const GLfloat Near = 5.0, Far = 25.0;
48 
49 static GLubyte * image = NULL;
50 static GLubyte * temp_image = NULL;
51 static GLuint img_width = 0;
52 static GLuint img_height = 0;
53 static GLuint img_format = 0;
54 
55 
56 
Display(void)57 static void Display( void )
58 {
59    GLint err;
60 
61 
62    glClearColor(0.2, 0.2, 0.8, 0);
63    glClear( GL_COLOR_BUFFER_BIT );
64 
65 
66    /* This is the "reference" square.
67     */
68 
69    glWindowPos2i( 5, 5 );
70    glDrawPixels( img_width, img_height, img_format, GL_UNSIGNED_BYTE, image );
71 
72    glPixelStorei( GL_PACK_INVERT_MESA, GL_FALSE );
73    err = glGetError();
74    if ( err != GL_NO_ERROR ) {
75       printf( "Setting PACK_INVERT_MESA to false generated an error (0x%04x).\n",
76 	      err );
77    }
78 
79    glReadPixels( 5, 5, img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
80    glWindowPos2i( 5 + 1 * (10 + img_width), 5 );
81    glDrawPixels( img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
82 
83    glPixelStorei( GL_PACK_INVERT_MESA, GL_TRUE );
84    err = glGetError();
85    if ( err != GL_NO_ERROR ) {
86       printf( "Setting PACK_INVERT_MESA to true generated an error (0x%04x).\n",
87 	      err );
88    }
89 
90    glReadPixels( 5, 5, img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
91    glWindowPos2i( 5 + 2 * (10 + img_width), 5 );
92    glDrawPixels( img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
93 
94    glutSwapBuffers();
95 }
96 
97 
Reshape(int width,int height)98 static void Reshape( int width, int height )
99 {
100    GLfloat ar = (float) width / (float) height;
101    Width = width;
102    Height = height;
103    glViewport( 0, 0, width, height );
104    glMatrixMode( GL_PROJECTION );
105    glLoadIdentity();
106    glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
107    glMatrixMode( GL_MODELVIEW );
108    glLoadIdentity();
109    glTranslatef( 0.0, 0.0, -15.0 );
110 }
111 
112 
Key(unsigned char key,int x,int y)113 static void Key( unsigned char key, int x, int y )
114 {
115    (void) x;
116    (void) y;
117    switch (key) {
118       case 27:
119          exit(0);
120          break;
121    }
122    glutPostRedisplay();
123 }
124 
125 
Init(void)126 static void Init( void )
127 {
128    printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
129    printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
130 
131    if ( !glutExtensionSupported("GL_MESA_pack_invert") ) {
132       printf("\nSorry, this program requires GL_MESA_pack_invert.\n");
133       exit(1);
134    }
135 
136    if ( !glutExtensionSupported("GL_ARB_window_pos") ) {
137       printf("\nSorry, this program requires GL_ARB_window_pos.\n");
138       exit(1);
139    }
140 
141    /* Do this check as a separate if-statement instead of as an else in case
142     * one of the required extensions is supported but glutGetProcAddress
143     * returns NULL.
144     */
145 
146    printf("\nThe left 2 squares should be the same color, and the right\n"
147 	  "square should look upside-down.\n");
148 
149 
150    image = LoadRGBImage( IMAGE_FILE, (GLint *) & img_width, (GLint *) & img_height,
151 			 & img_format );
152    if ( image == NULL ) {
153       printf( "Could not open image file \"%s\".\n", IMAGE_FILE );
154       exit(1);
155    }
156 
157    temp_image = malloc( 3 * img_height * img_width );
158    if ( temp_image == NULL ) {
159       printf( "Could not allocate memory for temporary image.\n" );
160       exit(1);
161    }
162 }
163 
164 
main(int argc,char * argv[])165 int main( int argc, char *argv[] )
166 {
167    glutInit( &argc, argv );
168    glutInitWindowPosition( 0, 0 );
169    glutInitWindowSize( Width, Height );
170    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
171    glutCreateWindow( "GL_MESA_pack_invert test" );
172    gladLoadGL();
173    glutReshapeFunc( Reshape );
174    glutKeyboardFunc( Key );
175    glutDisplayFunc( Display );
176    Init();
177    glutMainLoop();
178    return 0;
179 }
180