2008-07-25 07:31:37 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
#include "qdata.h"
|
|
|
|
#include "inout.h"
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char mip_prefix[1024]; // directory to dump the textures in
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
qboolean colormap_issued;
|
|
|
|
byte colormap_palette[768];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
RemapZero
|
|
|
|
|
|
|
|
Replaces all 0 bytes in an image with the closest palette entry.
|
|
|
|
This is because NT won't let us change index 0, so any palette
|
|
|
|
animation leaves those pixels untouched.
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void RemapZero( byte *pixels, byte *palette, int width, int height ){
|
|
|
|
int i, c;
|
|
|
|
int alt_zero;
|
|
|
|
int value, best;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
alt_zero = 0;
|
|
|
|
best = 9999999;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 1 ; i < 255 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
value = palette[i * 3 + 0] + palette[i * 3 + 1] + palette[i * 3 + 2];
|
|
|
|
if ( value < best ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
best = value;
|
|
|
|
alt_zero = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
c = width * height;
|
|
|
|
for ( i = 0 ; i < c ; i++ )
|
|
|
|
if ( pixels[i] == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
pixels[i] = alt_zero;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
Cmd_Grab
|
|
|
|
|
|
|
|
$grab filename x y width height
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Cmd_Grab( void ){
|
|
|
|
int xl,yl,w,h,y;
|
|
|
|
byte *cropped;
|
|
|
|
char savename[1024];
|
|
|
|
char dest[1024];
|
|
|
|
|
|
|
|
GetToken( false );
|
|
|
|
|
|
|
|
if ( token[0] == '/' || token[0] == '\\' ) {
|
|
|
|
sprintf( savename, "%s%s.pcx", gamedir, token + 1 );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
sprintf( savename, "%spics/%s.pcx", gamedir, token );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( g_release ) {
|
|
|
|
if ( token[0] == '/' || token[0] == '\\' ) {
|
|
|
|
sprintf( dest, "%s.pcx", token + 1 );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
sprintf( dest, "pics/%s.pcx", token );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ReleaseFile( dest );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GetToken( false );
|
|
|
|
xl = atoi( token );
|
|
|
|
GetToken( false );
|
|
|
|
yl = atoi( token );
|
|
|
|
GetToken( false );
|
|
|
|
w = atoi( token );
|
|
|
|
GetToken( false );
|
|
|
|
h = atoi( token );
|
|
|
|
|
|
|
|
if ( xl < 0 || yl < 0 || w < 0 || h < 0 || xl + w > byteimagewidth || yl + h > byteimageheight ) {
|
|
|
|
Error( "GrabPic: Bad size: %i, %i, %i, %i",xl,yl,w,h );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// crop it to the proper size
|
2012-03-17 20:01:54 +00:00
|
|
|
cropped = malloc( w * h );
|
|
|
|
for ( y = 0 ; y < h ; y++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( cropped + y * w, byteimage + ( y + yl ) * byteimagewidth + xl, w );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save off the new image
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "saving %s\n", savename );
|
|
|
|
CreatePath( savename );
|
|
|
|
WritePCXfile( savename, cropped, w, h, lbmpalette );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
free( cropped );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
Cmd_Raw
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
$grab filename x y width height
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Cmd_Raw( void ){
|
|
|
|
int xl,yl,w,h,y;
|
|
|
|
byte *cropped;
|
|
|
|
char savename[1024];
|
|
|
|
char dest[1024];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GetToken( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( savename, "%s%s.lmp", gamedir, token );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( g_release ) {
|
|
|
|
sprintf( dest, "%s.lmp", token );
|
|
|
|
ReleaseFile( dest );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GetToken( false );
|
|
|
|
xl = atoi( token );
|
|
|
|
GetToken( false );
|
|
|
|
yl = atoi( token );
|
|
|
|
GetToken( false );
|
|
|
|
w = atoi( token );
|
|
|
|
GetToken( false );
|
|
|
|
h = atoi( token );
|
|
|
|
|
|
|
|
if ( xl < 0 || yl < 0 || w < 0 || h < 0 || xl + w > byteimagewidth || yl + h > byteimageheight ) {
|
|
|
|
Error( "GrabPic: Bad size: %i, %i, %i, %i",xl,yl,w,h );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// crop it to the proper size
|
2012-03-17 20:01:54 +00:00
|
|
|
cropped = malloc( w * h );
|
|
|
|
for ( y = 0 ; y < h ; y++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( cropped + y * w, byteimage + ( y + yl ) * byteimagewidth + xl, w );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save off the new image
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "saving %s\n", savename );
|
|
|
|
CreatePath( savename );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
SaveFile( savename, cropped, w * h );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
free( cropped );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============================================================================
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
COLORMAP GRABBING
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
=============================================================================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
BestColor
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
byte BestColor( int r, int g, int b, int start, int stop ){
|
|
|
|
int i;
|
|
|
|
int dr, dg, db;
|
|
|
|
int bestdistortion, distortion;
|
|
|
|
int bestcolor;
|
|
|
|
byte *pal;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// let any color go to 0 as a last resort
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
bestdistortion = 256 * 256 * 4;
|
2007-11-04 03:34:51 +00:00
|
|
|
bestcolor = 0;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
pal = colormap_palette + start * 3;
|
|
|
|
for ( i = start ; i <= stop ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
dr = r - (int)pal[0];
|
|
|
|
dg = g - (int)pal[1];
|
|
|
|
db = b - (int)pal[2];
|
|
|
|
pal += 3;
|
2012-03-17 20:01:54 +00:00
|
|
|
distortion = dr * dr + dg * dg + db * db;
|
|
|
|
if ( distortion < bestdistortion ) {
|
|
|
|
if ( !distortion ) {
|
|
|
|
return i; // perfect match
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
bestdistortion = distortion;
|
|
|
|
bestcolor = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestcolor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
Cmd_Colormap
|
|
|
|
|
|
|
|
$colormap filename
|
|
|
|
|
|
|
|
the brightes colormap is first in the table (FIXME: reverse this now?)
|
|
|
|
|
|
|
|
64 rows of 256 : lightmaps
|
|
|
|
256 rows of 256 : translucency table
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Cmd_Colormap( void ){
|
|
|
|
int levels, brights;
|
|
|
|
int l, c;
|
|
|
|
float frac, red, green, blue;
|
|
|
|
float range;
|
|
|
|
byte *cropped, *lump_p;
|
|
|
|
char savename[1024];
|
|
|
|
char dest[1024];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
colormap_issued = true;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !g_release ) {
|
|
|
|
memcpy( colormap_palette, lbmpalette, 768 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !TokenAvailable() ) { // just setting colormap_issued
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GetToken( false );
|
|
|
|
sprintf( savename, "%spics/%s.pcx", gamedir, token );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( g_release ) {
|
|
|
|
sprintf( dest, "pics/%s.pcx", token );
|
|
|
|
ReleaseFile( dest );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
range = 2;
|
|
|
|
levels = 64;
|
2012-03-17 20:01:54 +00:00
|
|
|
brights = 1; // ignore 255 (transparent)
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
cropped = malloc( ( levels + 256 ) * 256 );
|
2007-11-04 03:34:51 +00:00
|
|
|
lump_p = cropped;
|
|
|
|
|
|
|
|
// shaded levels
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( l = 0; l < levels; l++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
frac = range - range * (float)l / ( levels - 1 );
|
|
|
|
for ( c = 0 ; c < 256 - brights ; c++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
red = lbmpalette[c * 3];
|
|
|
|
green = lbmpalette[c * 3 + 1];
|
|
|
|
blue = lbmpalette[c * 3 + 2];
|
|
|
|
|
|
|
|
red = (int)( red * frac + 0.5 );
|
|
|
|
green = (int)( green * frac + 0.5 );
|
|
|
|
blue = (int)( blue * frac + 0.5 );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
// note: 254 instead of 255 because 255 is the transparent color, and we
|
|
|
|
// don't want anything remapping to that
|
|
|
|
// don't use color 0, because NT can't remap that (or 255)
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
*lump_p++ = BestColor( red,green,blue, 1, 254 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fullbrights allways stay the same
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( ; c < 256 ; c++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
*lump_p++ = c;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// 66% transparancy table
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( l = 0; l < 255; l++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( c = 0 ; c < 255 ; c++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
red = lbmpalette[c * 3] * 0.33 + lbmpalette[l * 3] * 0.66;
|
|
|
|
green = lbmpalette[c * 3 + 1] * 0.33 + lbmpalette[l * 3 + 1] * 0.66;
|
|
|
|
blue = lbmpalette[c * 3 + 2] * 0.33 + lbmpalette[l * 3 + 2] * 0.66;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
*lump_p++ = BestColor( red,green,blue, 1, 254 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
*lump_p++ = 255;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( c = 0 ; c < 256 ; c++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
*lump_p++ = 255;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// save off the new image
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "saving %s\n", savename );
|
|
|
|
CreatePath( savename );
|
|
|
|
WritePCXfile( savename, cropped, 256, levels + 256, lbmpalette );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
free( cropped );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============================================================================
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
MIPTEX GRABBING
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
=============================================================================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
byte pixdata[256];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int d_red, d_green, d_blue;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
byte palmap[32][32][32];
|
|
|
|
qboolean palmap_built;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============
|
|
|
|
FindColor
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
int FindColor( int r, int g, int b ){
|
|
|
|
int bestcolor;
|
|
|
|
|
|
|
|
if ( r > 255 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
r = 255;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( r < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
r = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( g > 255 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
g = 255;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( g < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
g = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( b > 255 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
b = 255;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( b < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
b = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifndef TABLECOLORS
|
2012-03-17 20:01:54 +00:00
|
|
|
bestcolor = BestColor( r, g, b, 0, 254 );
|
2007-11-04 03:34:51 +00:00
|
|
|
#else
|
2012-03-17 20:01:54 +00:00
|
|
|
bestcolor = palmap[r >> 3][g >> 3][b >> 3];
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return bestcolor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void BuildPalmap( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef TABLECOLORS
|
2012-03-17 20:01:54 +00:00
|
|
|
int r, g, b;
|
|
|
|
int bestcolor;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( palmap_built ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
palmap_built = true;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( r = 4 ; r < 256 ; r += 8 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( g = 4 ; g < 256 ; g += 8 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( b = 4 ; b < 256 ; b += 8 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
bestcolor = BestColor( r, g, b, 1, 254 );
|
|
|
|
palmap[r >> 3][g >> 3][b >> 3] = bestcolor;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !colormap_issued ) {
|
|
|
|
Error( "You must issue a $colormap command first" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============
|
|
|
|
AveragePixels
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
byte AveragePixels( int count ){
|
|
|
|
int r,g,b;
|
|
|
|
int i;
|
|
|
|
int vis;
|
|
|
|
int pix;
|
|
|
|
int bestcolor;
|
|
|
|
byte *pal;
|
|
|
|
int fullbright;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
vis = 0;
|
|
|
|
r = g = b = 0;
|
|
|
|
fullbright = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < count ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
pix = pixdata[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
r += lbmpalette[pix * 3];
|
|
|
|
g += lbmpalette[pix * 3 + 1];
|
|
|
|
b += lbmpalette[pix * 3 + 2];
|
2007-11-04 03:34:51 +00:00
|
|
|
vis++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
r /= vis;
|
|
|
|
g /= vis;
|
|
|
|
b /= vis;
|
|
|
|
|
|
|
|
// error diffusion
|
|
|
|
r += d_red;
|
|
|
|
g += d_green;
|
|
|
|
b += d_blue;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
// find the best color
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
bestcolor = FindColor( r, g, b );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// error diffusion
|
2012-03-17 20:01:54 +00:00
|
|
|
pal = colormap_palette + bestcolor * 3;
|
2007-11-04 03:34:51 +00:00
|
|
|
d_red = r - (int)pal[0];
|
|
|
|
d_green = g - (int)pal[1];
|
|
|
|
d_blue = b - (int)pal[2];
|
|
|
|
|
|
|
|
return bestcolor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
pt_contents,
|
|
|
|
pt_flags,
|
|
|
|
pt_animvalue,
|
|
|
|
pt_flagvalue
|
|
|
|
} parmtype_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
char *name;
|
|
|
|
int flags;
|
|
|
|
parmtype_t type;
|
2007-11-04 03:34:51 +00:00
|
|
|
} mipparm_t;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
mipparm_t mipparms[] =
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
// utility content attributes
|
2012-03-17 20:01:54 +00:00
|
|
|
{"water", CONTENTS_WATER, pt_contents},
|
|
|
|
{"slime", CONTENTS_SLIME, pt_contents}, // mildly damaging
|
|
|
|
{"lava", CONTENTS_LAVA, pt_contents}, // very damaging
|
|
|
|
{"window", CONTENTS_WINDOW, pt_contents}, // solid, but doesn't eat internal textures
|
|
|
|
{"mist", CONTENTS_MIST, pt_contents}, // non-solid window
|
|
|
|
{"origin", CONTENTS_ORIGIN, pt_contents}, // center of rotating brushes
|
|
|
|
{"playerclip", CONTENTS_PLAYERCLIP, pt_contents},
|
|
|
|
{"monsterclip", CONTENTS_MONSTERCLIP, pt_contents},
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// utility surface attributes
|
2012-03-17 20:01:54 +00:00
|
|
|
{"hint", SURF_HINT, pt_flags},
|
|
|
|
{"skip", SURF_SKIP, pt_flags},
|
|
|
|
{"light", SURF_LIGHT, pt_flagvalue}, // value is the light quantity
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// texture chaining
|
2012-03-17 20:01:54 +00:00
|
|
|
{"anim", 0, pt_animvalue}, // value is the next animation
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// server attributes
|
2012-03-17 20:01:54 +00:00
|
|
|
{"slick", SURF_SLICK, pt_flags},
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// drawing attributes
|
2012-03-17 20:01:54 +00:00
|
|
|
{"sky", SURF_SKY, pt_flags},
|
|
|
|
{"warping", SURF_WARP, pt_flags}, // only valid with 64x64 textures
|
|
|
|
{"trans33", SURF_TRANS33, pt_flags}, // translucent should allso set fullbright
|
|
|
|
{"trans66", SURF_TRANS66, pt_flags},
|
|
|
|
{"flowing", SURF_FLOWING, pt_flags}, // flow direction towards angle 0
|
|
|
|
{"nodraw", SURF_NODRAW, pt_flags}, // for clip textures and trigger textures
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
{NULL, 0, pt_contents}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
Cmd_Mip
|
|
|
|
|
|
|
|
$mip filename x y width height <OPTIONS>
|
|
|
|
must be multiples of sixteen
|
|
|
|
SURF_WINDOW
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Cmd_Mip( void ){
|
|
|
|
int x,y,xl,yl,xh,yh,w,h;
|
2007-11-04 03:34:51 +00:00
|
|
|
byte *screen_p, *source;
|
2012-03-17 20:01:54 +00:00
|
|
|
int linedelta;
|
|
|
|
miptex_t *qtex;
|
|
|
|
int miplevel, mipstep;
|
|
|
|
int xx, yy, pix;
|
|
|
|
int count;
|
|
|
|
int flags, value, contents;
|
|
|
|
mipparm_t *mp;
|
|
|
|
char lumpname[64];
|
|
|
|
byte *lump_p;
|
|
|
|
char filename[1024];
|
|
|
|
char animname[64];
|
|
|
|
|
|
|
|
GetToken( false );
|
|
|
|
strcpy( lumpname, token );
|
|
|
|
|
|
|
|
GetToken( false );
|
|
|
|
xl = atoi( token );
|
|
|
|
GetToken( false );
|
|
|
|
yl = atoi( token );
|
|
|
|
GetToken( false );
|
|
|
|
w = atoi( token );
|
|
|
|
GetToken( false );
|
|
|
|
h = atoi( token );
|
|
|
|
|
|
|
|
if ( ( w & 15 ) || ( h & 15 ) ) {
|
|
|
|
Error( "line %i: miptex sizes must be multiples of 16", scriptline );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
contents = 0;
|
|
|
|
value = 0;
|
|
|
|
|
|
|
|
animname[0] = 0;
|
|
|
|
|
|
|
|
// get optional flags and values
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( TokenAvailable() )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
GetToken( false );
|
|
|
|
|
|
|
|
for ( mp = mipparms ; mp->name ; mp++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !strcmp( mp->name, token ) ) {
|
|
|
|
switch ( mp->type )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
case pt_animvalue:
|
2012-03-17 20:01:54 +00:00
|
|
|
GetToken( false ); // specify the next animation frame
|
|
|
|
strcpy( animname, token );
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
|
|
|
case pt_flags:
|
|
|
|
flags |= mp->flags;
|
|
|
|
break;
|
|
|
|
case pt_contents:
|
|
|
|
contents |= mp->flags;
|
|
|
|
break;
|
|
|
|
case pt_flagvalue:
|
|
|
|
flags |= mp->flags;
|
2012-03-17 20:01:54 +00:00
|
|
|
GetToken( false ); // specify the light value
|
|
|
|
value = atoi( token );
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !mp->name ) {
|
|
|
|
Error( "line %i: unknown parm %s", scriptline, token );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf( filename, "%stextures/%s/%s.wal", gamedir, mip_prefix, lumpname );
|
|
|
|
if ( g_release ) {
|
|
|
|
return; // textures are only released by $maps
|
|
|
|
|
|
|
|
}
|
|
|
|
xh = xl + w;
|
|
|
|
yh = yl + h;
|
|
|
|
|
|
|
|
qtex = malloc( sizeof( miptex_t ) + w * h * 2 );
|
|
|
|
memset( qtex, 0, sizeof( miptex_t ) );
|
|
|
|
|
|
|
|
qtex->width = LittleLong( w );
|
|
|
|
qtex->height = LittleLong( h );
|
|
|
|
qtex->flags = LittleLong( flags );
|
|
|
|
qtex->contents = LittleLong( contents );
|
|
|
|
qtex->value = LittleLong( value );
|
|
|
|
sprintf( qtex->name, "%s/%s", mip_prefix, lumpname );
|
|
|
|
if ( animname[0] ) {
|
|
|
|
sprintf( qtex->animname, "%s/%s", mip_prefix, animname );
|
|
|
|
}
|
|
|
|
|
|
|
|
lump_p = (byte *)( &qtex->value + 1 );
|
|
|
|
|
|
|
|
screen_p = byteimage + yl * byteimagewidth + xl;
|
2007-11-04 03:34:51 +00:00
|
|
|
linedelta = byteimagewidth - w;
|
|
|
|
|
|
|
|
source = lump_p;
|
2012-03-17 20:01:54 +00:00
|
|
|
qtex->offsets[0] = LittleLong( lump_p - (byte *)qtex );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = yl ; y < yh ; y++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( x = xl ; x < xh ; x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
pix = *screen_p++;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( pix == 255 ) {
|
|
|
|
pix = 1; // should never happen
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
*lump_p++ = pix;
|
|
|
|
}
|
|
|
|
screen_p += linedelta;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
// subsample for greater mip levels
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
d_red = d_green = d_blue = 0; // no distortion yet
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( miplevel = 1 ; miplevel < 4 ; miplevel++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
qtex->offsets[miplevel] = LittleLong( lump_p - (byte *)qtex );
|
|
|
|
|
|
|
|
mipstep = 1 << miplevel;
|
|
|
|
for ( y = 0 ; y < h ; y += mipstep )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( x = 0 ; x < w ; x += mipstep )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
count = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( yy = 0 ; yy < mipstep ; yy++ )
|
|
|
|
for ( xx = 0 ; xx < mipstep ; xx++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
pixdata[count] = source[ ( y + yy ) * w + x + xx ];
|
2007-11-04 03:34:51 +00:00
|
|
|
count++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
*lump_p++ = AveragePixels( count );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// dword align the size
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( (int)lump_p & 3 )
|
2007-11-04 03:34:51 +00:00
|
|
|
*lump_p++ = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// write it out
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "writing %s\n", filename );
|
|
|
|
SaveFile( filename, (byte *)qtex, lump_p - (byte *)qtex );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
free( qtex );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
Cmd_Mippal
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void Cmd_Mippal( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
colormap_issued = true;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( g_release ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( colormap_palette, lbmpalette, 768 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
BuildPalmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
Cmd_Mipdir
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void Cmd_Mipdir( void ){
|
|
|
|
char filename[1024];
|
|
|
|
|
|
|
|
GetToken( false );
|
|
|
|
strcpy( mip_prefix, token );
|
2007-11-04 03:34:51 +00:00
|
|
|
// create the directory if needed
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( filename, "%stextures", gamedir, mip_prefix );
|
|
|
|
Q_mkdir( filename );
|
|
|
|
sprintf( filename, "%stextures/%s", gamedir, mip_prefix );
|
|
|
|
Q_mkdir( filename );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============================================================================
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ENVIRONMENT MAP GRABBING
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Creates six pcx files from tga files without any palette edge seams
|
|
|
|
also copies the tga files for GL rendering.
|
|
|
|
=============================================================================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// 3dstudio environment map suffixes
|
2012-03-17 20:01:54 +00:00
|
|
|
char *suf[6] = {"rt", "ft", "lf", "bk", "up", "dn"};
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=================
|
|
|
|
Cmd_Environment
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
void Cmd_Environment( void ){
|
|
|
|
char name[1024];
|
|
|
|
int i, x, y;
|
|
|
|
byte image[256 * 256];
|
|
|
|
byte *tga;
|
|
|
|
|
|
|
|
GetToken( false );
|
|
|
|
|
|
|
|
if ( g_release ) {
|
|
|
|
for ( i = 0 ; i < 6 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( name, "env/%s%s.pcx", token, suf[i] );
|
|
|
|
ReleaseFile( name );
|
|
|
|
sprintf( name, "env/%s%s.tga", token, suf[i] );
|
|
|
|
ReleaseFile( name );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// get the palette
|
2012-03-17 20:01:54 +00:00
|
|
|
BuildPalmap();
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( name, "%senv/", gamedir );
|
|
|
|
CreatePath( name );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// convert the images
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 6 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( name, "%senv/%s%s.tga", gamedir, token, suf[i] );
|
|
|
|
printf( "loading %s...\n", name );
|
|
|
|
LoadTGA( name, &tga, NULL, NULL );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0 ; y < 256 ; y++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( x = 0 ; x < 256 ; x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
image[y * 256 + x] = FindColor( tga[( y * 256 + x ) * 4 + 0],tga[( y * 256 + x ) * 4 + 1],tga[( y * 256 + x ) * 4 + 2] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
free( tga );
|
|
|
|
sprintf( name, "%senv/%s%s.pcx", gamedir, token, suf[i] );
|
|
|
|
if ( FileTime( name ) != -1 ) {
|
|
|
|
printf( "%s already exists, not overwriting.\n", name );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
WritePCXfile( name, image, 256, 256, colormap_palette );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|