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