glx: indent -br -i3 -npcs --no-tabs compsize.c
[mesa.git] / src / glx / x11 / compsize.c
1 /* -*- mode: c; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3; coding: utf-8-unix -*- */
2 /*
3 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
4 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice including the dates of first publication and
14 * either this permission notice or a reference to
15 * http://oss.sgi.com/projects/FreeB/
16 * shall be included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 * Except as contained in this notice, the name of Silicon Graphics, Inc.
27 * shall not be used in advertising or otherwise to promote the sale, use or
28 * other dealings in this Software without prior written authorization from
29 * Silicon Graphics, Inc.
30 */
31
32 #include <GL/gl.h>
33 #include "indirect_size.h"
34 #include "glxclient.h"
35
36 /*
37 ** Return the number of elements per group of a specified format
38 */
39 GLint
40 __glElementsPerGroup(GLenum format, GLenum type)
41 {
42 /*
43 ** To make row length computation valid for image extraction,
44 ** packed pixel types assume elements per group equals one.
45 */
46 switch (type) {
47 case GL_UNSIGNED_BYTE_3_3_2:
48 case GL_UNSIGNED_BYTE_2_3_3_REV:
49 case GL_UNSIGNED_SHORT_5_6_5:
50 case GL_UNSIGNED_SHORT_5_6_5_REV:
51 case GL_UNSIGNED_SHORT_4_4_4_4:
52 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
53 case GL_UNSIGNED_SHORT_5_5_5_1:
54 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
55 case GL_UNSIGNED_SHORT_8_8_APPLE:
56 case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
57 case GL_UNSIGNED_SHORT_15_1_MESA:
58 case GL_UNSIGNED_SHORT_1_15_REV_MESA:
59 case GL_UNSIGNED_INT_8_8_8_8:
60 case GL_UNSIGNED_INT_8_8_8_8_REV:
61 case GL_UNSIGNED_INT_10_10_10_2:
62 case GL_UNSIGNED_INT_2_10_10_10_REV:
63 case GL_UNSIGNED_INT_24_8_NV:
64 case GL_UNSIGNED_INT_24_8_MESA:
65 case GL_UNSIGNED_INT_8_24_REV_MESA:
66 return 1;
67 default:
68 break;
69 }
70
71 switch (format) {
72 case GL_RGB:
73 case GL_BGR:
74 return 3;
75 case GL_422_EXT:
76 case GL_422_REV_EXT:
77 case GL_422_AVERAGE_EXT:
78 case GL_422_REV_AVERAGE_EXT:
79 case GL_YCBCR_422_APPLE:
80 case GL_LUMINANCE_ALPHA:
81 return 2;
82 case GL_RGBA:
83 case GL_BGRA:
84 case GL_ABGR_EXT:
85 return 4;
86 case GL_COLOR_INDEX:
87 case GL_STENCIL_INDEX:
88 case GL_DEPTH_COMPONENT:
89 case GL_RED:
90 case GL_GREEN:
91 case GL_BLUE:
92 case GL_ALPHA:
93 case GL_LUMINANCE:
94 case GL_INTENSITY:
95 return 1;
96 default:
97 return 0;
98 }
99 }
100
101 /*
102 ** Return the number of bytes per element, based on the element type (other
103 ** than GL_BITMAP).
104 */
105 GLint
106 __glBytesPerElement(GLenum type)
107 {
108 switch (type) {
109 case GL_UNSIGNED_SHORT:
110 case GL_SHORT:
111 case GL_UNSIGNED_SHORT_5_6_5:
112 case GL_UNSIGNED_SHORT_5_6_5_REV:
113 case GL_UNSIGNED_SHORT_4_4_4_4:
114 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
115 case GL_UNSIGNED_SHORT_5_5_5_1:
116 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
117 case GL_UNSIGNED_SHORT_8_8_APPLE:
118 case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
119 case GL_UNSIGNED_SHORT_15_1_MESA:
120 case GL_UNSIGNED_SHORT_1_15_REV_MESA:
121 return 2;
122 case GL_UNSIGNED_BYTE:
123 case GL_BYTE:
124 case GL_UNSIGNED_BYTE_3_3_2:
125 case GL_UNSIGNED_BYTE_2_3_3_REV:
126 return 1;
127 case GL_INT:
128 case GL_UNSIGNED_INT:
129 case GL_FLOAT:
130 case GL_UNSIGNED_INT_8_8_8_8:
131 case GL_UNSIGNED_INT_8_8_8_8_REV:
132 case GL_UNSIGNED_INT_10_10_10_2:
133 case GL_UNSIGNED_INT_2_10_10_10_REV:
134 case GL_UNSIGNED_INT_24_8_NV:
135 case GL_UNSIGNED_INT_24_8_MESA:
136 case GL_UNSIGNED_INT_8_24_REV_MESA:
137 return 4;
138 default:
139 return 0;
140 }
141 }
142
143 /*
144 ** Compute memory required for internal packed array of data of given type
145 ** and format.
146 */
147 GLint
148 __glImageSize(GLsizei width, GLsizei height, GLsizei depth,
149 GLenum format, GLenum type, GLenum target)
150 {
151 int bytes_per_row;
152 int components;
153
154 switch (target) {
155 case GL_PROXY_TEXTURE_1D:
156 case GL_PROXY_TEXTURE_2D:
157 case GL_PROXY_TEXTURE_3D:
158 case GL_PROXY_TEXTURE_4D_SGIS:
159 case GL_PROXY_TEXTURE_CUBE_MAP:
160 case GL_PROXY_TEXTURE_RECTANGLE_ARB:
161 case GL_PROXY_HISTOGRAM:
162 case GL_PROXY_COLOR_TABLE:
163 case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
164 case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
165 case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
166 case GL_PROXY_POST_IMAGE_TRANSFORM_COLOR_TABLE_HP:
167 return 0;
168 }
169
170 if (width < 0 || height < 0 || depth < 0) {
171 return 0;
172 }
173
174 /*
175 ** Zero is returned if either format or type are invalid.
176 */
177 components = __glElementsPerGroup(format, type);
178 if (type == GL_BITMAP) {
179 if (format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX) {
180 bytes_per_row = (width + 7) >> 3;
181 }
182 else {
183 return 0;
184 }
185 }
186 else {
187 bytes_per_row = __glBytesPerElement(type) * width;
188 }
189
190 return bytes_per_row * height * depth * components;
191 }