Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 1 | /* |
Adam Jackson | dc8058c | 2008-09-19 17:16:53 -0400 | [diff] [blame] | 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 | */ |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 30 | |
| 31 | #include <GL/gl.h> |
Brian Paul | f2ce358 | 2006-06-12 19:56:57 +0000 | [diff] [blame] | 32 | #include "glxclient.h" |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | ** Return the number of elements per group of a specified format |
| 36 | */ |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 37 | GLint |
| 38 | __glElementsPerGroup(GLenum format, GLenum type) |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 39 | { |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 40 | /* |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 41 | ** To make row length computation valid for image extraction, |
| 42 | ** packed pixel types assume elements per group equals one. |
| 43 | */ |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 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: |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 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: |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 60 | return 1; |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 61 | default: |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 62 | break; |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 63 | } |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 64 | |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 65 | switch (format) { |
| 66 | case GL_RGB: |
| 67 | case GL_BGR: |
Adam Jackson | b15aba9 | 2015-07-21 11:43:42 -0400 | [diff] [blame] | 68 | case GL_RGB_INTEGER_EXT: |
| 69 | case GL_BGR_INTEGER_EXT: |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 70 | return 3; |
Ian Romanick | bb45ab0 | 2009-11-20 11:03:31 -0800 | [diff] [blame] | 71 | case GL_RG: |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 72 | case GL_422_EXT: |
| 73 | case GL_422_REV_EXT: |
| 74 | case GL_422_AVERAGE_EXT: |
| 75 | case GL_422_REV_AVERAGE_EXT: |
Jon TURNEY | 8937c16 | 2010-09-09 13:43:11 +0100 | [diff] [blame] | 76 | case GL_DEPTH_STENCIL_NV: |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 77 | case GL_YCBCR_422_APPLE: |
| 78 | case GL_LUMINANCE_ALPHA: |
Adam Jackson | b15aba9 | 2015-07-21 11:43:42 -0400 | [diff] [blame] | 79 | case GL_LUMINANCE_ALPHA_INTEGER_EXT: |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 80 | return 2; |
| 81 | case GL_RGBA: |
| 82 | case GL_BGRA: |
| 83 | case GL_ABGR_EXT: |
Adam Jackson | b15aba9 | 2015-07-21 11:43:42 -0400 | [diff] [blame] | 84 | case GL_RGBA_INTEGER_EXT: |
| 85 | case GL_BGRA_INTEGER_EXT: |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 86 | return 4; |
| 87 | case GL_COLOR_INDEX: |
| 88 | case GL_STENCIL_INDEX: |
| 89 | case GL_DEPTH_COMPONENT: |
| 90 | case GL_RED: |
| 91 | case GL_GREEN: |
| 92 | case GL_BLUE: |
| 93 | case GL_ALPHA: |
| 94 | case GL_LUMINANCE: |
| 95 | case GL_INTENSITY: |
Adam Jackson | b15aba9 | 2015-07-21 11:43:42 -0400 | [diff] [blame] | 96 | case GL_RED_INTEGER_EXT: |
| 97 | case GL_GREEN_INTEGER_EXT: |
| 98 | case GL_BLUE_INTEGER_EXT: |
| 99 | case GL_ALPHA_INTEGER_EXT: |
| 100 | case GL_LUMINANCE_INTEGER_EXT: |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 101 | return 1; |
| 102 | default: |
| 103 | return 0; |
| 104 | } |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | ** Return the number of bytes per element, based on the element type (other |
| 109 | ** than GL_BITMAP). |
| 110 | */ |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 111 | GLint |
| 112 | __glBytesPerElement(GLenum type) |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 113 | { |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 114 | switch (type) { |
| 115 | case GL_UNSIGNED_SHORT: |
| 116 | case GL_SHORT: |
| 117 | case GL_UNSIGNED_SHORT_5_6_5: |
| 118 | case GL_UNSIGNED_SHORT_5_6_5_REV: |
| 119 | case GL_UNSIGNED_SHORT_4_4_4_4: |
| 120 | case GL_UNSIGNED_SHORT_4_4_4_4_REV: |
| 121 | case GL_UNSIGNED_SHORT_5_5_5_1: |
| 122 | case GL_UNSIGNED_SHORT_1_5_5_5_REV: |
| 123 | case GL_UNSIGNED_SHORT_8_8_APPLE: |
| 124 | case GL_UNSIGNED_SHORT_8_8_REV_APPLE: |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 125 | return 2; |
| 126 | case GL_UNSIGNED_BYTE: |
| 127 | case GL_BYTE: |
| 128 | case GL_UNSIGNED_BYTE_3_3_2: |
| 129 | case GL_UNSIGNED_BYTE_2_3_3_REV: |
| 130 | return 1; |
| 131 | case GL_INT: |
| 132 | case GL_UNSIGNED_INT: |
| 133 | case GL_FLOAT: |
| 134 | case GL_UNSIGNED_INT_8_8_8_8: |
| 135 | case GL_UNSIGNED_INT_8_8_8_8_REV: |
| 136 | case GL_UNSIGNED_INT_10_10_10_2: |
| 137 | case GL_UNSIGNED_INT_2_10_10_10_REV: |
| 138 | case GL_UNSIGNED_INT_24_8_NV: |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 139 | return 4; |
| 140 | default: |
| 141 | return 0; |
| 142 | } |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | ** Compute memory required for internal packed array of data of given type |
| 147 | ** and format. |
| 148 | */ |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 149 | GLint |
| 150 | __glImageSize(GLsizei width, GLsizei height, GLsizei depth, |
| 151 | GLenum format, GLenum type, GLenum target) |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 152 | { |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 153 | int bytes_per_row; |
| 154 | int components; |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 155 | |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 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 | } |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 171 | |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 172 | if (width < 0 || height < 0 || depth < 0) { |
| 173 | return 0; |
| 174 | } |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 175 | |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 176 | /* |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 177 | ** Zero is returned if either format or type are invalid. |
| 178 | */ |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 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 | } |
| 184 | else { |
| 185 | return 0; |
| 186 | } |
| 187 | } |
| 188 | else { |
| 189 | bytes_per_row = __glBytesPerElement(type) * width; |
| 190 | } |
Ian Romanick | 5f1f229 | 2005-01-07 02:39:09 +0000 | [diff] [blame] | 191 | |
RALOVICH, Kristóf | f788a8e | 2008-10-13 13:12:37 +0200 | [diff] [blame] | 192 | return bytes_per_row * height * depth * components; |
Adam Jackson | cb3610e | 2004-10-25 21:09:16 +0000 | [diff] [blame] | 193 | } |