📄 teximage.c
字号:
/*
* Mesa 3-D graphics library
* Version: 6.5
*
* Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* \file teximage.c
* Texture images manipulation functions.
*
* \note Mesa's native texture data type is GLchan. Native formats are
* GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA, and
* GL_COLOR_INDEX.
*
* \note Device drivers are free to implement any internal format they want.
*/
#include "glheader.h"
#include "bufferobj.h"
#include "context.h"
#include "convolve.h"
#include "image.h"
#include "imports.h"
#include "macros.h"
#include "state.h"
#include "texcompress.h"
#include "texformat.h"
#include "teximage.h"
#include "texstate.h"
#include "texstore.h"
#include "mtypes.h"
/**
* We allocate texture memory on 512-byte boundaries so we can use MMX/SSE
* elsewhere.
*/
void *
_mesa_alloc_texmemory(GLsizei bytes)
{
return _mesa_align_malloc(bytes, 512);
}
/**
* Free texture memory allocated with _mesa_alloc_texmemory()
*/
void
_mesa_free_texmemory(void *m)
{
_mesa_align_free(m);
}
#if 0
static void PrintTexture(GLcontext *ctx, const struct gl_texture_image *img)
{
#if CHAN_TYPE == GL_FLOAT
_mesa_problem(NULL, "PrintTexture doesn't support float channels");
#else
GLuint i, j, c;
const GLchan *data = (const GLchan *) img->Data;
if (!data) {
_mesa_printf("No texture data\n");
return;
}
switch (img->Format) {
case GL_ALPHA:
case GL_LUMINANCE:
case GL_INTENSITY:
case GL_COLOR_INDEX:
c = 1;
break;
case GL_LUMINANCE_ALPHA:
c = 2;
break;
case GL_RGB:
c = 3;
break;
case GL_RGBA:
c = 4;
break;
default:
_mesa_problem(NULL, "error in PrintTexture\n");
return;
}
for (i = 0; i < img->Height; i++) {
for (j = 0; j < img->Width; j++) {
if (c==1)
_mesa_printf("%02x ", data[0]);
else if (c==2)
_mesa_printf("%02x%02x ", data[0], data[1]);
else if (c==3)
_mesa_printf("%02x%02x%02x ", data[0], data[1], data[2]);
else if (c==4)
_mesa_printf("%02x%02x%02x%02x ", data[0], data[1], data[2], data[3]);
data += (img->RowStride - img->Width) * c;
}
_mesa_printf("\n");
}
#endif
}
#endif
/*
* Compute floor(log_base_2(n)).
* If n < 0 return -1.
*/
static int
logbase2( int n )
{
GLint i = 1;
GLint log2 = 0;
if (n < 0)
return -1;
if (n == 0)
return 0;
while ( n > i ) {
i *= 2;
log2++;
}
if (i != n) {
return log2 - 1;
}
else {
return log2;
}
}
/**
* Return the simple base format for a given internal texture format.
* For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
*
* \param ctx GL context.
* \param internalFormat the internal texture format token or 1, 2, 3, or 4.
*
* \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
* GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
*
* This is the format which is used during texture application (i.e. the
* texture format and env mode determine the arithmetic used.
*/
GLint
_mesa_base_tex_format( GLcontext *ctx, GLint internalFormat )
{
switch (internalFormat) {
case GL_ALPHA:
case GL_ALPHA4:
case GL_ALPHA8:
case GL_ALPHA12:
case GL_ALPHA16:
return GL_ALPHA;
case 1:
case GL_LUMINANCE:
case GL_LUMINANCE4:
case GL_LUMINANCE8:
case GL_LUMINANCE12:
case GL_LUMINANCE16:
return GL_LUMINANCE;
case 2:
case GL_LUMINANCE_ALPHA:
case GL_LUMINANCE4_ALPHA4:
case GL_LUMINANCE6_ALPHA2:
case GL_LUMINANCE8_ALPHA8:
case GL_LUMINANCE12_ALPHA4:
case GL_LUMINANCE12_ALPHA12:
case GL_LUMINANCE16_ALPHA16:
return GL_LUMINANCE_ALPHA;
case GL_INTENSITY:
case GL_INTENSITY4:
case GL_INTENSITY8:
case GL_INTENSITY12:
case GL_INTENSITY16:
return GL_INTENSITY;
case 3:
case GL_RGB:
case GL_R3_G3_B2:
case GL_RGB4:
case GL_RGB5:
case GL_RGB8:
case GL_RGB10:
case GL_RGB12:
case GL_RGB16:
return GL_RGB;
case 4:
case GL_RGBA:
case GL_RGBA2:
case GL_RGBA4:
case GL_RGB5_A1:
case GL_RGBA8:
case GL_RGB10_A2:
case GL_RGBA12:
case GL_RGBA16:
return GL_RGBA;
default:
; /* fallthrough */
}
if (ctx->Extensions.EXT_paletted_texture) {
switch (internalFormat) {
case GL_COLOR_INDEX:
case GL_COLOR_INDEX1_EXT:
case GL_COLOR_INDEX2_EXT:
case GL_COLOR_INDEX4_EXT:
case GL_COLOR_INDEX8_EXT:
case GL_COLOR_INDEX12_EXT:
case GL_COLOR_INDEX16_EXT:
return GL_COLOR_INDEX;
default:
; /* fallthrough */
}
}
if (ctx->Extensions.SGIX_depth_texture ||
ctx->Extensions.ARB_depth_texture) {
switch (internalFormat) {
case GL_DEPTH_COMPONENT:
case GL_DEPTH_COMPONENT16_SGIX:
case GL_DEPTH_COMPONENT24_SGIX:
case GL_DEPTH_COMPONENT32_SGIX:
return GL_DEPTH_COMPONENT;
default:
; /* fallthrough */
}
}
if (ctx->Extensions.ARB_texture_compression) {
switch (internalFormat) {
case GL_COMPRESSED_ALPHA:
return GL_ALPHA;
case GL_COMPRESSED_LUMINANCE:
return GL_LUMINANCE;
case GL_COMPRESSED_LUMINANCE_ALPHA:
return GL_LUMINANCE_ALPHA;
case GL_COMPRESSED_INTENSITY:
return GL_INTENSITY;
case GL_COMPRESSED_RGB:
return GL_RGB;
case GL_COMPRESSED_RGBA:
return GL_RGBA;
default:
; /* fallthrough */
}
}
if (ctx->Extensions.TDFX_texture_compression_FXT1) {
switch (internalFormat) {
case GL_COMPRESSED_RGB_FXT1_3DFX:
return GL_RGB;
case GL_COMPRESSED_RGBA_FXT1_3DFX:
return GL_RGBA;
default:
; /* fallthrough */
}
}
if (ctx->Extensions.EXT_texture_compression_s3tc) {
switch (internalFormat) {
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
return GL_RGB;
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
return GL_RGBA;
default:
; /* fallthrough */
}
}
if (ctx->Extensions.S3_s3tc) {
switch (internalFormat) {
case GL_RGB_S3TC:
case GL_RGB4_S3TC:
return GL_RGB;
case GL_RGBA_S3TC:
case GL_RGBA4_S3TC:
return GL_RGBA;
default:
; /* fallthrough */
}
}
if (ctx->Extensions.MESA_ycbcr_texture) {
if (internalFormat == GL_YCBCR_MESA)
return GL_YCBCR_MESA;
}
if (ctx->Extensions.ARB_texture_float) {
switch (internalFormat) {
case GL_ALPHA16F_ARB:
case GL_ALPHA32F_ARB:
return GL_ALPHA;
case GL_RGBA16F_ARB:
case GL_RGBA32F_ARB:
return GL_RGBA;
case GL_RGB16F_ARB:
case GL_RGB32F_ARB:
return GL_RGB;
case GL_INTENSITY16F_ARB:
case GL_INTENSITY32F_ARB:
return GL_INTENSITY;
case GL_LUMINANCE16F_ARB:
case GL_LUMINANCE32F_ARB:
return GL_LUMINANCE;
case GL_LUMINANCE_ALPHA16F_ARB:
case GL_LUMINANCE_ALPHA32F_ARB:
return GL_LUMINANCE_ALPHA;
default:
; /* fallthrough */
}
}
return -1; /* error */
}
/**
* Test if the given image format is a color/RGBA format (i.e., not color
* index, depth, stencil, etc).
* \param format the image format value (may by an internal texture format)
* \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
*/
static GLboolean
is_color_format(GLenum format)
{
switch (format) {
case GL_RED:
case GL_GREEN:
case GL_BLUE:
case GL_ALPHA:
case GL_ALPHA4:
case GL_ALPHA8:
case GL_ALPHA12:
case GL_ALPHA16:
case 1:
case GL_LUMINANCE:
case GL_LUMINANCE4:
case GL_LUMINANCE8:
case GL_LUMINANCE12:
case GL_LUMINANCE16:
case 2:
case GL_LUMINANCE_ALPHA:
case GL_LUMINANCE4_ALPHA4:
case GL_LUMINANCE6_ALPHA2:
case GL_LUMINANCE8_ALPHA8:
case GL_LUMINANCE12_ALPHA4:
case GL_LUMINANCE12_ALPHA12:
case GL_LUMINANCE16_ALPHA16:
case GL_INTENSITY:
case GL_INTENSITY4:
case GL_INTENSITY8:
case GL_INTENSITY12:
case GL_INTENSITY16:
case 3:
case GL_RGB:
case GL_BGR:
case GL_R3_G3_B2:
case GL_RGB4:
case GL_RGB5:
case GL_RGB8:
case GL_RGB10:
case GL_RGB12:
case GL_RGB16:
case 4:
case GL_ABGR_EXT:
case GL_RGBA:
case GL_BGRA:
case GL_RGBA2:
case GL_RGBA4:
case GL_RGB5_A1:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -