1848b8605Smrg/*
2848b8605Smrg * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3848b8605Smrg * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4848b8605Smrg *
5848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6848b8605Smrg * copy of this software and associated documentation files (the "Software"),
7848b8605Smrg * to deal in the Software without restriction, including without limitation
8848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
10848b8605Smrg * Software is furnished to do so, subject to the following conditions:
11848b8605Smrg *
12848b8605Smrg * The above copyright notice including the dates of first publication and
13848b8605Smrg * either this permission notice or a reference to
14848b8605Smrg * http://oss.sgi.com/projects/FreeB/
15848b8605Smrg * shall be included in all copies or substantial portions of the Software.
16848b8605Smrg *
17848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20848b8605Smrg * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21848b8605Smrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22848b8605Smrg * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23848b8605Smrg * SOFTWARE.
24848b8605Smrg *
25848b8605Smrg * Except as contained in this notice, the name of Silicon Graphics, Inc.
26848b8605Smrg * shall not be used in advertising or otherwise to promote the sale, use or
27848b8605Smrg * other dealings in this Software without prior written authorization from
28848b8605Smrg * Silicon Graphics, Inc.
29848b8605Smrg */
30848b8605Smrg
31848b8605Smrg#include <GL/gl.h>
32848b8605Smrg#include "glxclient.h"
33848b8605Smrg
34848b8605Smrg/*
35848b8605Smrg** Return the number of elements per group of a specified format
36848b8605Smrg*/
37848b8605SmrgGLint
38848b8605Smrg__glElementsPerGroup(GLenum format, GLenum type)
39848b8605Smrg{
40848b8605Smrg   /*
41848b8605Smrg    ** To make row length computation valid for image extraction,
42848b8605Smrg    ** packed pixel types assume elements per group equals one.
43848b8605Smrg    */
44848b8605Smrg   switch (type) {
45848b8605Smrg   case GL_UNSIGNED_BYTE_3_3_2:
46848b8605Smrg   case GL_UNSIGNED_BYTE_2_3_3_REV:
47848b8605Smrg   case GL_UNSIGNED_SHORT_5_6_5:
48848b8605Smrg   case GL_UNSIGNED_SHORT_5_6_5_REV:
49848b8605Smrg   case GL_UNSIGNED_SHORT_4_4_4_4:
50848b8605Smrg   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
51848b8605Smrg   case GL_UNSIGNED_SHORT_5_5_5_1:
52848b8605Smrg   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
53848b8605Smrg   case GL_UNSIGNED_SHORT_8_8_APPLE:
54848b8605Smrg   case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
55848b8605Smrg   case GL_UNSIGNED_INT_8_8_8_8:
56848b8605Smrg   case GL_UNSIGNED_INT_8_8_8_8_REV:
57848b8605Smrg   case GL_UNSIGNED_INT_10_10_10_2:
58848b8605Smrg   case GL_UNSIGNED_INT_2_10_10_10_REV:
59848b8605Smrg   case GL_UNSIGNED_INT_24_8_NV:
60848b8605Smrg      return 1;
61848b8605Smrg   default:
62848b8605Smrg      break;
63848b8605Smrg   }
64848b8605Smrg
65848b8605Smrg   switch (format) {
66848b8605Smrg   case GL_RGB:
67848b8605Smrg   case GL_BGR:
68b8e80941Smrg   case GL_RGB_INTEGER_EXT:
69b8e80941Smrg   case GL_BGR_INTEGER_EXT:
70848b8605Smrg      return 3;
71848b8605Smrg   case GL_RG:
72848b8605Smrg   case GL_422_EXT:
73848b8605Smrg   case GL_422_REV_EXT:
74848b8605Smrg   case GL_422_AVERAGE_EXT:
75848b8605Smrg   case GL_422_REV_AVERAGE_EXT:
76848b8605Smrg   case GL_DEPTH_STENCIL_NV:
77848b8605Smrg   case GL_YCBCR_422_APPLE:
78848b8605Smrg   case GL_LUMINANCE_ALPHA:
79b8e80941Smrg   case GL_LUMINANCE_ALPHA_INTEGER_EXT:
80848b8605Smrg      return 2;
81848b8605Smrg   case GL_RGBA:
82848b8605Smrg   case GL_BGRA:
83848b8605Smrg   case GL_ABGR_EXT:
84b8e80941Smrg   case GL_RGBA_INTEGER_EXT:
85b8e80941Smrg   case GL_BGRA_INTEGER_EXT:
86848b8605Smrg      return 4;
87848b8605Smrg   case GL_COLOR_INDEX:
88848b8605Smrg   case GL_STENCIL_INDEX:
89848b8605Smrg   case GL_DEPTH_COMPONENT:
90848b8605Smrg   case GL_RED:
91848b8605Smrg   case GL_GREEN:
92848b8605Smrg   case GL_BLUE:
93848b8605Smrg   case GL_ALPHA:
94848b8605Smrg   case GL_LUMINANCE:
95848b8605Smrg   case GL_INTENSITY:
96b8e80941Smrg   case GL_RED_INTEGER_EXT:
97b8e80941Smrg   case GL_GREEN_INTEGER_EXT:
98b8e80941Smrg   case GL_BLUE_INTEGER_EXT:
99b8e80941Smrg   case GL_ALPHA_INTEGER_EXT:
100b8e80941Smrg   case GL_LUMINANCE_INTEGER_EXT:
101848b8605Smrg      return 1;
102848b8605Smrg   default:
103848b8605Smrg      return 0;
104848b8605Smrg   }
105848b8605Smrg}
106848b8605Smrg
107848b8605Smrg/*
108848b8605Smrg** Return the number of bytes per element, based on the element type (other
109848b8605Smrg** than GL_BITMAP).
110848b8605Smrg*/
111848b8605SmrgGLint
112848b8605Smrg__glBytesPerElement(GLenum type)
113848b8605Smrg{
114848b8605Smrg   switch (type) {
115848b8605Smrg   case GL_UNSIGNED_SHORT:
116848b8605Smrg   case GL_SHORT:
117848b8605Smrg   case GL_UNSIGNED_SHORT_5_6_5:
118848b8605Smrg   case GL_UNSIGNED_SHORT_5_6_5_REV:
119848b8605Smrg   case GL_UNSIGNED_SHORT_4_4_4_4:
120848b8605Smrg   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
121848b8605Smrg   case GL_UNSIGNED_SHORT_5_5_5_1:
122848b8605Smrg   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
123848b8605Smrg   case GL_UNSIGNED_SHORT_8_8_APPLE:
124848b8605Smrg   case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
125848b8605Smrg      return 2;
126848b8605Smrg   case GL_UNSIGNED_BYTE:
127848b8605Smrg   case GL_BYTE:
128848b8605Smrg   case GL_UNSIGNED_BYTE_3_3_2:
129848b8605Smrg   case GL_UNSIGNED_BYTE_2_3_3_REV:
130848b8605Smrg      return 1;
131848b8605Smrg   case GL_INT:
132848b8605Smrg   case GL_UNSIGNED_INT:
133848b8605Smrg   case GL_FLOAT:
134848b8605Smrg   case GL_UNSIGNED_INT_8_8_8_8:
135848b8605Smrg   case GL_UNSIGNED_INT_8_8_8_8_REV:
136848b8605Smrg   case GL_UNSIGNED_INT_10_10_10_2:
137848b8605Smrg   case GL_UNSIGNED_INT_2_10_10_10_REV:
138848b8605Smrg   case GL_UNSIGNED_INT_24_8_NV:
139848b8605Smrg      return 4;
140848b8605Smrg   default:
141848b8605Smrg      return 0;
142848b8605Smrg   }
143848b8605Smrg}
144848b8605Smrg
145848b8605Smrg/*
146848b8605Smrg** Compute memory required for internal packed array of data of given type
147848b8605Smrg** and format.
148848b8605Smrg*/
149848b8605SmrgGLint
150848b8605Smrg__glImageSize(GLsizei width, GLsizei height, GLsizei depth,
151848b8605Smrg              GLenum format, GLenum type, GLenum target)
152848b8605Smrg{
153848b8605Smrg   int bytes_per_row;
154848b8605Smrg   int components;
155848b8605Smrg
156848b8605Smrg   switch (target) {
157848b8605Smrg   case GL_PROXY_TEXTURE_1D:
158848b8605Smrg   case GL_PROXY_TEXTURE_2D:
159848b8605Smrg   case GL_PROXY_TEXTURE_3D:
160848b8605Smrg   case GL_PROXY_TEXTURE_4D_SGIS:
161848b8605Smrg   case GL_PROXY_TEXTURE_CUBE_MAP:
162848b8605Smrg   case GL_PROXY_TEXTURE_RECTANGLE_ARB:
163848b8605Smrg   case GL_PROXY_HISTOGRAM:
164848b8605Smrg   case GL_PROXY_COLOR_TABLE:
165848b8605Smrg   case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
166848b8605Smrg   case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
167848b8605Smrg   case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
168848b8605Smrg   case GL_PROXY_POST_IMAGE_TRANSFORM_COLOR_TABLE_HP:
169848b8605Smrg      return 0;
170848b8605Smrg   }
171848b8605Smrg
172848b8605Smrg   if (width < 0 || height < 0 || depth < 0) {
173848b8605Smrg      return 0;
174848b8605Smrg   }
175848b8605Smrg
176848b8605Smrg   /*
177848b8605Smrg    ** Zero is returned if either format or type are invalid.
178848b8605Smrg    */
179848b8605Smrg   components = __glElementsPerGroup(format, type);
180848b8605Smrg   if (type == GL_BITMAP) {
181848b8605Smrg      if (format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX) {
182848b8605Smrg         bytes_per_row = (width + 7) >> 3;
183848b8605Smrg      }
184848b8605Smrg      else {
185848b8605Smrg         return 0;
186848b8605Smrg      }
187848b8605Smrg   }
188848b8605Smrg   else {
189848b8605Smrg      bytes_per_row = __glBytesPerElement(type) * width;
190848b8605Smrg   }
191848b8605Smrg
192848b8605Smrg   return bytes_per_row * height * depth * components;
193848b8605Smrg}
194