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