glx: added "glapi/" prefix to include
[mesa.git] / src / glx / x11 / compsize.c
1 /* $XFree86: xc/lib/GL/glx/compsize.c,v 1.6 2004/01/28 18:11:38 alanh Exp $ */
2 /*
3 ** License Applicability. Except to the extent portions of this file are
4 ** made subject to an alternative license as permitted in the SGI Free
5 ** Software License B, Version 1.1 (the "License"), the contents of this
6 ** file are subject only to the provisions of the License. You may not use
7 ** this file except in compliance with the License. You may obtain a copy
8 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
9 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
10 **
11 ** http://oss.sgi.com/projects/FreeB
12 **
13 ** Note that, as provided in the License, the Software is distributed on an
14 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
15 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
16 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
17 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
18 **
19 ** Original Code. The Original Code is: OpenGL Sample Implementation,
20 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
21 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
22 ** Copyright in any portions created by third parties is as indicated
23 ** elsewhere herein. All Rights Reserved.
24 **
25 ** Additional Notice Provisions: The application programming interfaces
26 ** established by SGI in conjunction with the Original Code are The
27 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
28 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
29 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
30 ** Window System(R) (Version 1.3), released October 19, 1998. This software
31 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
32 ** published by SGI, but has not been independently verified as being
33 ** compliant with the OpenGL(R) version 1.2.1 Specification.
34 **
35 */
36
37 #include <GL/gl.h>
38 #include "indirect_size.h"
39 #include "glxclient.h"
40
41 /*
42 ** Return the number of elements per group of a specified format
43 */
44 GLint __glElementsPerGroup(GLenum format, GLenum type)
45 {
46 /*
47 ** To make row length computation valid for image extraction,
48 ** packed pixel types assume elements per group equals one.
49 */
50 switch(type) {
51 case GL_UNSIGNED_BYTE_3_3_2:
52 case GL_UNSIGNED_BYTE_2_3_3_REV:
53 case GL_UNSIGNED_SHORT_5_6_5:
54 case GL_UNSIGNED_SHORT_5_6_5_REV:
55 case GL_UNSIGNED_SHORT_4_4_4_4:
56 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
57 case GL_UNSIGNED_SHORT_5_5_5_1:
58 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
59 case GL_UNSIGNED_SHORT_8_8_APPLE:
60 case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
61 case GL_UNSIGNED_SHORT_15_1_MESA:
62 case GL_UNSIGNED_SHORT_1_15_REV_MESA:
63 case GL_UNSIGNED_INT_8_8_8_8:
64 case GL_UNSIGNED_INT_8_8_8_8_REV:
65 case GL_UNSIGNED_INT_10_10_10_2:
66 case GL_UNSIGNED_INT_2_10_10_10_REV:
67 case GL_UNSIGNED_INT_24_8_NV:
68 case GL_UNSIGNED_INT_24_8_MESA:
69 case GL_UNSIGNED_INT_8_24_REV_MESA:
70 return 1;
71 default:
72 break;
73 }
74
75 switch(format) {
76 case GL_RGB:
77 case GL_BGR:
78 return 3;
79 case GL_422_EXT:
80 case GL_422_REV_EXT:
81 case GL_422_AVERAGE_EXT:
82 case GL_422_REV_AVERAGE_EXT:
83 case GL_YCBCR_422_APPLE:
84 case GL_LUMINANCE_ALPHA:
85 return 2;
86 case GL_RGBA:
87 case GL_BGRA:
88 case GL_ABGR_EXT:
89 return 4;
90 case GL_COLOR_INDEX:
91 case GL_STENCIL_INDEX:
92 case GL_DEPTH_COMPONENT:
93 case GL_RED:
94 case GL_GREEN:
95 case GL_BLUE:
96 case GL_ALPHA:
97 case GL_LUMINANCE:
98 case GL_INTENSITY:
99 return 1;
100 default:
101 return 0;
102 }
103 }
104
105 /*
106 ** Return the number of bytes per element, based on the element type (other
107 ** than GL_BITMAP).
108 */
109 GLint __glBytesPerElement(GLenum type)
110 {
111 switch(type) {
112 case GL_UNSIGNED_SHORT:
113 case GL_SHORT:
114 case GL_UNSIGNED_SHORT_5_6_5:
115 case GL_UNSIGNED_SHORT_5_6_5_REV:
116 case GL_UNSIGNED_SHORT_4_4_4_4:
117 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
118 case GL_UNSIGNED_SHORT_5_5_5_1:
119 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
120 case GL_UNSIGNED_SHORT_8_8_APPLE:
121 case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
122 case GL_UNSIGNED_SHORT_15_1_MESA:
123 case GL_UNSIGNED_SHORT_1_15_REV_MESA:
124 return 2;
125 case GL_UNSIGNED_BYTE:
126 case GL_BYTE:
127 case GL_UNSIGNED_BYTE_3_3_2:
128 case GL_UNSIGNED_BYTE_2_3_3_REV:
129 return 1;
130 case GL_INT:
131 case GL_UNSIGNED_INT:
132 case GL_FLOAT:
133 case GL_UNSIGNED_INT_8_8_8_8:
134 case GL_UNSIGNED_INT_8_8_8_8_REV:
135 case GL_UNSIGNED_INT_10_10_10_2:
136 case GL_UNSIGNED_INT_2_10_10_10_REV:
137 case GL_UNSIGNED_INT_24_8_NV:
138 case GL_UNSIGNED_INT_24_8_MESA:
139 case GL_UNSIGNED_INT_8_24_REV_MESA:
140 return 4;
141 default:
142 return 0;
143 }
144 }
145
146 /*
147 ** Compute memory required for internal packed array of data of given type
148 ** and format.
149 */
150 GLint __glImageSize(GLsizei width, GLsizei height, GLsizei depth,
151 GLenum format, GLenum type, GLenum target)
152 {
153 int bytes_per_row;
154 int components;
155
156 switch( target ) {
157 case GL_PROXY_TEXTURE_1D:
158 case GL_PROXY_TEXTURE_2D:
159 case GL_PROXY_TEXTURE_3D:
160 case GL_PROXY_TEXTURE_4D_SGIS:
161 case GL_PROXY_TEXTURE_CUBE_MAP:
162 case GL_PROXY_TEXTURE_RECTANGLE_ARB:
163 case GL_PROXY_HISTOGRAM:
164 case GL_PROXY_COLOR_TABLE:
165 case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
166 case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
167 case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
168 case GL_PROXY_POST_IMAGE_TRANSFORM_COLOR_TABLE_HP:
169 return 0;
170 }
171
172 if (width < 0 || height < 0 || depth < 0) {
173 return 0;
174 }
175
176 /*
177 ** Zero is returned if either format or type are invalid.
178 */
179 components = __glElementsPerGroup(format,type);
180 if (type == GL_BITMAP) {
181 if (format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX) {
182 bytes_per_row = (width + 7) >> 3;
183 } else {
184 return 0;
185 }
186 } else {
187 bytes_per_row = __glBytesPerElement(type) * width;
188 }
189
190 return bytes_per_row * height * depth * components;
191 }