📄 tr_image.c
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_image.c
#include "tr_local.h"
/*
* Include file for users of JPEG library.
* You will need to have included system headers that define at least
* the typedefs FILE and size_t before you can include jpeglib.h.
* (stdio.h is sufficient on ANSI-conforming systems.)
* You may also wish to include "jerror.h".
*/
#define JPEG_INTERNALS
#include "../jpeg-6/jpeglib.h"
static void LoadBMP( const char *name, byte **pic, int *width, int *height );
static void LoadTGA( const char *name, byte **pic, int *width, int *height );
static void LoadJPG( const char *name, byte **pic, int *width, int *height );
static byte s_intensitytable[256];
static unsigned char s_gammatable[256];
int gl_filter_min = GL_LINEAR_MIPMAP_NEAREST;
int gl_filter_max = GL_LINEAR;
#define FILE_HASH_SIZE 1024
static image_t* hashTable[FILE_HASH_SIZE];
/*
** R_GammaCorrect
*/
void R_GammaCorrect( byte *buffer, int bufSize ) {
int i;
for ( i = 0; i < bufSize; i++ ) {
buffer[i] = s_gammatable[buffer[i]];
}
}
typedef struct {
char *name;
int minimize, maximize;
} textureMode_t;
textureMode_t modes[] = {
{"GL_NEAREST", GL_NEAREST, GL_NEAREST},
{"GL_LINEAR", GL_LINEAR, GL_LINEAR},
{"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
{"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
{"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
{"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
};
/*
================
return a hash value for the filename
================
*/
static long generateHashValue( const char *fname ) {
int i;
long hash;
char letter;
hash = 0;
i = 0;
while (fname[i] != '\0') {
letter = tolower(fname[i]);
if (letter =='.') break; // don't include extension
if (letter =='\\') letter = '/'; // damn path names
hash+=(long)(letter)*(i+119);
i++;
}
hash &= (FILE_HASH_SIZE-1);
return hash;
}
/*
===============
GL_TextureMode
===============
*/
void GL_TextureMode( const char *string ) {
int i;
image_t *glt;
for ( i=0 ; i< 6 ; i++ ) {
if ( !Q_stricmp( modes[i].name, string ) ) {
break;
}
}
// hack to prevent trilinear from being set on voodoo,
// because their driver freaks...
if ( i == 5 && glConfig.hardwareType == GLHW_3DFX_2D3D ) {
ri.Printf( PRINT_ALL, "Refusing to set trilinear on a voodoo.\n" );
i = 3;
}
if ( i == 6 ) {
ri.Printf (PRINT_ALL, "bad filter name\n");
return;
}
gl_filter_min = modes[i].minimize;
gl_filter_max = modes[i].maximize;
// change all the existing mipmap texture objects
for ( i = 0 ; i < tr.numImages ; i++ ) {
glt = tr.images[ i ];
if ( glt->mipmap ) {
GL_Bind (glt);
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
}
}
}
/*
===============
R_SumOfUsedImages
===============
*/
int R_SumOfUsedImages( void ) {
int total;
int i;
total = 0;
for ( i = 0; i < tr.numImages; i++ ) {
if ( tr.images[i]->frameUsed == tr.frameCount ) {
total += tr.images[i]->uploadWidth * tr.images[i]->uploadHeight;
}
}
return total;
}
/*
===============
R_ImageList_f
===============
*/
void R_ImageList_f( void ) {
int i;
image_t *image;
int texels;
const char *yesno[] = {
"no ", "yes"
};
ri.Printf (PRINT_ALL, "\n -w-- -h-- -mm- -TMU- -if-- wrap --name-------\n");
texels = 0;
for ( i = 0 ; i < tr.numImages ; i++ ) {
image = tr.images[ i ];
texels += image->uploadWidth*image->uploadHeight;
ri.Printf (PRINT_ALL, "%4i: %4i %4i %s %d ",
i, image->uploadWidth, image->uploadHeight, yesno[image->mipmap], image->TMU );
switch ( image->internalFormat ) {
case 1:
ri.Printf( PRINT_ALL, "I " );
break;
case 2:
ri.Printf( PRINT_ALL, "IA " );
break;
case 3:
ri.Printf( PRINT_ALL, "RGB " );
break;
case 4:
ri.Printf( PRINT_ALL, "RGBA " );
break;
case GL_RGBA8:
ri.Printf( PRINT_ALL, "RGBA8" );
break;
case GL_RGB8:
ri.Printf( PRINT_ALL, "RGB8" );
break;
case GL_RGB4_S3TC:
ri.Printf( PRINT_ALL, "S3TC " );
break;
case GL_RGBA4:
ri.Printf( PRINT_ALL, "RGBA4" );
break;
case GL_RGB5:
ri.Printf( PRINT_ALL, "RGB5 " );
break;
default:
ri.Printf( PRINT_ALL, "???? " );
}
switch ( image->wrapClampMode ) {
case GL_REPEAT:
ri.Printf( PRINT_ALL, "rept " );
break;
case GL_CLAMP:
ri.Printf( PRINT_ALL, "clmp " );
break;
default:
ri.Printf( PRINT_ALL, "%4i ", image->wrapClampMode );
break;
}
ri.Printf( PRINT_ALL, " %s\n", image->imgName );
}
ri.Printf (PRINT_ALL, " ---------\n");
ri.Printf (PRINT_ALL, " %i total texels (not including mipmaps)\n", texels);
ri.Printf (PRINT_ALL, " %i total images\n\n", tr.numImages );
}
//=======================================================================
/*
================
ResampleTexture
Used to resample images in a more general than quartering fashion.
This will only be filtered properly if the resampled size
is greater than half the original size.
If a larger shrinking is needed, use the mipmap function
before or after.
================
*/
static void ResampleTexture( unsigned *in, int inwidth, int inheight, unsigned *out,
int outwidth, int outheight ) {
int i, j;
unsigned *inrow, *inrow2;
unsigned frac, fracstep;
unsigned p1[2048], p2[2048];
byte *pix1, *pix2, *pix3, *pix4;
if (outwidth>2048)
ri.Error(ERR_DROP, "ResampleTexture: max width");
fracstep = inwidth*0x10000/outwidth;
frac = fracstep>>2;
for ( i=0 ; i<outwidth ; i++ ) {
p1[i] = 4*(frac>>16);
frac += fracstep;
}
frac = 3*(fracstep>>2);
for ( i=0 ; i<outwidth ; i++ ) {
p2[i] = 4*(frac>>16);
frac += fracstep;
}
for (i=0 ; i<outheight ; i++, out += outwidth) {
inrow = in + inwidth*(int)((i+0.25)*inheight/outheight);
inrow2 = in + inwidth*(int)((i+0.75)*inheight/outheight);
frac = fracstep >> 1;
for (j=0 ; j<outwidth ; j++) {
pix1 = (byte *)inrow + p1[j];
pix2 = (byte *)inrow + p2[j];
pix3 = (byte *)inrow2 + p1[j];
pix4 = (byte *)inrow2 + p2[j];
((byte *)(out+j))[0] = (pix1[0] + pix2[0] + pix3[0] + pix4[0])>>2;
((byte *)(out+j))[1] = (pix1[1] + pix2[1] + pix3[1] + pix4[1])>>2;
((byte *)(out+j))[2] = (pix1[2] + pix2[2] + pix3[2] + pix4[2])>>2;
((byte *)(out+j))[3] = (pix1[3] + pix2[3] + pix3[3] + pix4[3])>>2;
}
}
}
/*
================
R_LightScaleTexture
Scale up the pixel values in a texture to increase the
lighting range
================
*/
void R_LightScaleTexture (unsigned *in, int inwidth, int inheight, qboolean only_gamma )
{
if ( only_gamma )
{
if ( !glConfig.deviceSupportsGamma )
{
int i, c;
byte *p;
p = (byte *)in;
c = inwidth*inheight;
for (i=0 ; i<c ; i++, p+=4)
{
p[0] = s_gammatable[p[0]];
p[1] = s_gammatable[p[1]];
p[2] = s_gammatable[p[2]];
}
}
}
else
{
int i, c;
byte *p;
p = (byte *)in;
c = inwidth*inheight;
if ( glConfig.deviceSupportsGamma )
{
for (i=0 ; i<c ; i++, p+=4)
{
p[0] = s_intensitytable[p[0]];
p[1] = s_intensitytable[p[1]];
p[2] = s_intensitytable[p[2]];
}
}
else
{
for (i=0 ; i<c ; i++, p+=4)
{
p[0] = s_gammatable[s_intensitytable[p[0]]];
p[1] = s_gammatable[s_intensitytable[p[1]]];
p[2] = s_gammatable[s_intensitytable[p[2]]];
}
}
}
}
/*
================
R_MipMap2
Operates in place, quartering the size of the texture
Proper linear filter
================
*/
static void R_MipMap2( unsigned *in, int inWidth, int inHeight ) {
int i, j, k;
byte *outpix;
int inWidthMask, inHeightMask;
int total;
int outWidth, outHeight;
unsigned *temp;
outWidth = inWidth >> 1;
outHeight = inHeight >> 1;
temp = ri.Hunk_AllocateTempMemory( outWidth * outHeight * 4 );
inWidthMask = inWidth - 1;
inHeightMask = inHeight - 1;
for ( i = 0 ; i < outHeight ; i++ ) {
for ( j = 0 ; j < outWidth ; j++ ) {
outpix = (byte *) ( temp + i * outWidth + j );
for ( k = 0 ; k < 4 ; k++ ) {
total =
1 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] +
2 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] +
2 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] +
1 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k] +
2 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] +
4 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] +
4 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] +
2 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k] +
2 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] +
4 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] +
4 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] +
2 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k] +
1 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] +
2 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] +
2 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] +
1 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k];
outpix[k] = total / 36;
}
}
}
Com_Memcpy( in, temp, outWidth * outHeight * 4 );
ri.Hunk_FreeTempMemory( temp );
}
/*
================
R_MipMap
Operates in place, quartering the size of the texture
================
*/
static void R_MipMap (byte *in, int width, int height) {
int i, j;
byte *out;
int row;
if ( !r_simpleMipMaps->integer ) {
R_MipMap2( (unsigned *)in, width, height );
return;
}
if ( width == 1 && height == 1 ) {
return;
}
row = width * 4;
out = in;
width >>= 1;
height >>= 1;
if ( width == 0 || height == 0 ) {
width += height; // get largest
for (i=0 ; i<width ; i++, out+=4, in+=8 ) {
out[0] = ( in[0] + in[4] )>>1;
out[1] = ( in[1] + in[5] )>>1;
out[2] = ( in[2] + in[6] )>>1;
out[3] = ( in[3] + in[7] )>>1;
}
return;
}
for (i=0 ; i<height ; i++, in+=row) {
for (j=0 ; j<width ; j++, out+=4, in+=8) {
out[0] = (in[0] + in[4] + in[row+0] + in[row+4])>>2;
out[1] = (in[1] + in[5] + in[row+1] + in[row+5])>>2;
out[2] = (in[2] + in[6] + in[row+2] + in[row+6])>>2;
out[3] = (in[3] + in[7] + in[row+3] + in[row+7])>>2;
}
}
}
/*
==================
R_BlendOverTexture
Apply a color blend over a set of pixels
==================
*/
static void R_BlendOverTexture( byte *data, int pixelCount, byte blend[4] ) {
int i;
int inverseAlpha;
int premult[3];
inverseAlpha = 255 - blend[3];
premult[0] = blend[0] * blend[3];
premult[1] = blend[1] * blend[3];
premult[2] = blend[2] * blend[3];
for ( i = 0 ; i < pixelCount ; i++, data+=4 ) {
data[0] = ( data[0] * inverseAlpha + premult[0] ) >> 9;
data[1] = ( data[1] * inverseAlpha + premult[1] ) >> 9;
data[2] = ( data[2] * inverseAlpha + premult[2] ) >> 9;
}
}
byte mipBlendColors[16][4] = {
{0,0,0,0},
{255,0,0,128},
{0,255,0,128},
{0,0,255,128},
{255,0,0,128},
{0,255,0,128},
{0,0,255,128},
{255,0,0,128},
{0,255,0,128},
{0,0,255,128},
{255,0,0,128},
{0,255,0,128},
{0,0,255,128},
{255,0,0,128},
{0,255,0,128},
{0,0,255,128},
};
/*
===============
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -