compsize.c revision 4b1f98241f9601c2b163bb41e45aa567f9cd61dd
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_422_EXT:
70   case GL_422_REV_EXT:
71   case GL_422_AVERAGE_EXT:
72   case GL_422_REV_AVERAGE_EXT:
73   case GL_YCBCR_422_APPLE:
74   case GL_LUMINANCE_ALPHA:
75      return 2;
76   case GL_RGBA:
77   case GL_BGRA:
78   case GL_ABGR_EXT:
79      return 4;
80   case GL_COLOR_INDEX:
81   case GL_STENCIL_INDEX:
82   case GL_DEPTH_COMPONENT:
83   case GL_RED:
84   case GL_GREEN:
85   case GL_BLUE:
86   case GL_ALPHA:
87   case GL_LUMINANCE:
88   case GL_INTENSITY:
89      return 1;
90   default:
91      return 0;
92   }
93}
94
95/*
96** Return the number of bytes per element, based on the element type (other
97** than GL_BITMAP).
98*/
99GLint
100__glBytesPerElement(GLenum type)
101{
102   switch (type) {
103   case GL_UNSIGNED_SHORT:
104   case GL_SHORT:
105   case GL_UNSIGNED_SHORT_5_6_5:
106   case GL_UNSIGNED_SHORT_5_6_5_REV:
107   case GL_UNSIGNED_SHORT_4_4_4_4:
108   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
109   case GL_UNSIGNED_SHORT_5_5_5_1:
110   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
111   case GL_UNSIGNED_SHORT_8_8_APPLE:
112   case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
113      return 2;
114   case GL_UNSIGNED_BYTE:
115   case GL_BYTE:
116   case GL_UNSIGNED_BYTE_3_3_2:
117   case GL_UNSIGNED_BYTE_2_3_3_REV:
118      return 1;
119   case GL_INT:
120   case GL_UNSIGNED_INT:
121   case GL_FLOAT:
122   case GL_UNSIGNED_INT_8_8_8_8:
123   case GL_UNSIGNED_INT_8_8_8_8_REV:
124   case GL_UNSIGNED_INT_10_10_10_2:
125   case GL_UNSIGNED_INT_2_10_10_10_REV:
126   case GL_UNSIGNED_INT_24_8_NV:
127      return 4;
128   default:
129      return 0;
130   }
131}
132
133/*
134** Compute memory required for internal packed array of data of given type
135** and format.
136*/
137GLint
138__glImageSize(GLsizei width, GLsizei height, GLsizei depth,
139              GLenum format, GLenum type, GLenum target)
140{
141   int bytes_per_row;
142   int components;
143
144   switch (target) {
145   case GL_PROXY_TEXTURE_1D:
146   case GL_PROXY_TEXTURE_2D:
147   case GL_PROXY_TEXTURE_3D:
148   case GL_PROXY_TEXTURE_4D_SGIS:
149   case GL_PROXY_TEXTURE_CUBE_MAP:
150   case GL_PROXY_TEXTURE_RECTANGLE_ARB:
151   case GL_PROXY_HISTOGRAM:
152   case GL_PROXY_COLOR_TABLE:
153   case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
154   case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
155   case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
156   case GL_PROXY_POST_IMAGE_TRANSFORM_COLOR_TABLE_HP:
157      return 0;
158   }
159
160   if (width < 0 || height < 0 || depth < 0) {
161      return 0;
162   }
163
164   /*
165    ** Zero is returned if either format or type are invalid.
166    */
167   components = __glElementsPerGroup(format, type);
168   if (type == GL_BITMAP) {
169      if (format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX) {
170         bytes_per_row = (width + 7) >> 3;
171      }
172      else {
173         return 0;
174      }
175   }
176   else {
177      bytes_per_row = __glBytesPerElement(type) * width;
178   }
179
180   return bytes_per_row * height * depth * components;
181}
182