The source release of the qutils.

This commit is contained in:
Travis Bradshaw
2012-01-31 15:48:05 -06:00
commit 6df9737f9c
111 changed files with 23929 additions and 0 deletions

301
qutils/QLUMPY/QLUMPY.C Normal file
View File

@@ -0,0 +1,301 @@
#define VERSION "2.0"
#include "qlumpy.h"
#define MAXLUMP 0x50000 // biggest possible lump
byte *byteimage, *lbmpalette;
int byteimagewidth, byteimageheight;
char basepath[1024];
char lumpname[16];
char destfile[1024];
byte *lumpbuffer, *lump_p;
qboolean savesingle;
qboolean outputcreated;
/*
=============================================================================
MAIN
=============================================================================
*/
void GrabRaw (void);
void GrabPalette (void);
void GrabPic (void);
void GrabMip (void);
void GrabColormap (void);
void GrabColormap2 (void);
typedef struct
{
char *name;
void (*function) (void);
} command_t;
command_t commands[] =
{
{"palette",GrabPalette},
{"colormap",GrabColormap},
{"qpic",GrabPic},
{"miptex",GrabMip},
{"raw",GrabRaw},
{"colormap2",GrabColormap2},
{NULL,NULL} // list terminator
};
/*
==============
LoadScreen
==============
*/
void LoadScreen (char *name)
{
char *expanded;
expanded = ExpandPathAndArchive (name);
printf ("grabbing from %s...\n",expanded);
LoadLBM (expanded, &byteimage, &lbmpalette);
byteimagewidth = bmhd.w;
byteimageheight = bmhd.h;
}
/*
================
CreateOutput
================
*/
void CreateOutput (void)
{
outputcreated = true;
//
// create the output wadfile file
//
NewWad (destfile, false); // create a new wadfile
}
/*
===============
WriteLump
===============
*/
void WriteLump (int type, int compression)
{
int size;
if (!outputcreated)
CreateOutput ();
//
// dword align the size
//
while ((int)lump_p&3)
*lump_p++ = 0;
size = lump_p - lumpbuffer;
if (size > MAXLUMP)
Error ("Lump size exceeded %d, memory corrupted!",MAXLUMP);
//
// write the grabbed lump to the wadfile
//
AddLump (lumpname,lumpbuffer,size,type, compression);
}
/*
===========
WriteFile
Save as a seperate file instead of as a wadfile lump
===========
*/
void WriteFile (void)
{
char filename[1024];
char *exp;
sprintf (filename,"%s/%s.lmp", destfile, lumpname);
exp = ExpandPath(filename);
printf ("saved %s\n", exp);
SaveFile (exp, lumpbuffer, lump_p-lumpbuffer);
}
/*
================
ParseScript
================
*/
void ParseScript (void)
{
int cmd;
int size;
do
{
//
// get a command / lump name
//
GetToken (true);
if (endofscript)
break;
if (!Q_strcasecmp (token,"$LOAD"))
{
GetToken (false);
LoadScreen (token);
continue;
}
if (!Q_strcasecmp (token,"$DEST"))
{
GetToken (false);
strcpy (destfile, ExpandPath(token));
continue;
}
if (!Q_strcasecmp (token,"$SINGLEDEST"))
{
GetToken (false);
strcpy (destfile, token);
savesingle = true;
continue;
}
//
// new lump
//
if (strlen(token) >= sizeof(lumpname) )
Error ("\"%s\" is too long to be a lump name",token);
memset (lumpname,0,sizeof(lumpname));
strcpy (lumpname, token);
for (size=0 ; size<sizeof(lumpname) ; size++)
lumpname[size] = tolower(lumpname[size]);
//
// get the grab command
//
lump_p = lumpbuffer;
GetToken (false);
//
// call a routine to grab some data and put it in lumpbuffer
// with lump_p pointing after the last byte to be saved
//
for (cmd=0 ; commands[cmd].name ; cmd++)
if ( !Q_strcasecmp(token,commands[cmd].name) )
{
commands[cmd].function ();
break;
}
if ( !commands[cmd].name )
Error ("Unrecognized token '%s' at line %i",token,scriptline);
grabbed++;
if (savesingle)
WriteFile ();
else
WriteLump (TYP_LUMPY+cmd, 0);
} while (script_p < scriptend_p);
}
/*
=================
ProcessLumpyScript
Loads a script file, then grabs everything from it
=================
*/
void ProcessLumpyScript (char *basename)
{
char script[256];
printf ("qlumpy script: %s\n",basename);
//
// create default destination directory
//
strcpy (destfile, ExpandPath(basename));
StripExtension (destfile);
strcat (destfile,".wad"); // unless the script overrides, save in cwd
//
// save in a wadfile by default
//
savesingle = false;
grabbed = 0;
outputcreated = false;
//
// read in the script file
//
strcpy (script, basename);
DefaultExtension (script, ".ls");
LoadScriptFile (script);
strcpy (basepath, basename);
ParseScript (); // execute load / grab commands
if (!savesingle)
{
WriteWad (); // write out the wad directory
printf ("%i lumps grabbed in a wad file\n",grabbed);
}
else
printf ("%i lumps written seperately\n",grabbed);
}
/*
==============================
main
==============================
*/
int main (int argc, char **argv)
{
int i;
printf ("\nqlumpy "VERSION" by John Carmack, copyright (c) 1994 Id Software\n");
if (argc == 1)
Error ("qlumpy [-archive directory] scriptfile [scriptfile ...]");
lumpbuffer = malloc (MAXLUMP);
if (!strcmp(argv[1], "-archive"))
{
archive = true;
strcpy (archivedir, argv[2]);
printf ("Archiving source to: %s\n", archivedir);
i = 3;
}
else
i = 1;
for ( ; i<argc ; i++)
{
SetQdirFromPath (argv[i]);
ProcessLumpyScript (argv[i]);
}
return 0;
}

16
qutils/QLUMPY/QLUMPY.H Normal file
View File

@@ -0,0 +1,16 @@
#include "cmdlib.h"
#include "scriplib.h"
#include "lbmlib.h"
#include "wadlib.h"
extern byte *byteimage, *lbmpalette;
extern int byteimagewidth, byteimageheight;
#define SCRN(x,y) (*(byteimage+(y)*byteimagewidth+x))
extern byte *lump_p;
extern byte *lumpbuffer;
extern char lumpname[];

324
qutils/QLUMPY/QLUMPY.MAK Normal file
View File

@@ -0,0 +1,324 @@
# Microsoft Developer Studio Generated NMAKE File, Format Version 4.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
!IF "$(CFG)" == ""
CFG=qlumpy - Win32 Debug
!MESSAGE No configuration specified. Defaulting to qlumpy - Win32 Debug.
!ENDIF
!IF "$(CFG)" != "qlumpy - Win32 Release" && "$(CFG)" != "qlumpy - Win32 Debug"
!MESSAGE Invalid configuration "$(CFG)" specified.
!MESSAGE You can specify a configuration when running NMAKE on this makefile
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "qlumpy.mak" CFG="qlumpy - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "qlumpy - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "qlumpy - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
!ERROR An invalid configuration is specified.
!ENDIF
!IF "$(OS)" == "Windows_NT"
NULL=
!ELSE
NULL=nul
!ENDIF
################################################################################
# Begin Project
# PROP Target_Last_Scanned "qlumpy - Win32 Debug"
RSC=rc.exe
CPP=cl.exe
!IF "$(CFG)" == "qlumpy - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
OUTDIR=.\Release
INTDIR=.\Release
ALL : "$(OUTDIR)\qlumpy.exe"
CLEAN :
-@erase ".\Release\qlumpy.exe"
-@erase ".\Release\quakegrb.obj"
-@erase ".\Release\qlumpy.obj"
-@erase ".\Release\wadlib.obj"
-@erase ".\Release\scriplib.obj"
-@erase ".\Release\cmdlib.obj"
-@erase ".\Release\lbmlib.obj"
"$(OUTDIR)" :
if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
# ADD CPP /nologo /GX /O2 /I "..\common" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
CPP_PROJ=/nologo /ML /GX /O2 /I "..\common" /D "WIN32" /D "NDEBUG" /D\
"_CONSOLE" /Fp"$(INTDIR)/qlumpy.pch" /YX /Fo"$(INTDIR)/" /c
CPP_OBJS=.\Release/
CPP_SBRS=
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
BSC32_FLAGS=/nologo /o"$(OUTDIR)/qlumpy.bsc"
BSC32_SBRS=
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
LINK32_FLAGS=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib\
advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib\
odbccp32.lib /nologo /subsystem:console /incremental:no\
/pdb:"$(OUTDIR)/qlumpy.pdb" /machine:I386 /out:"$(OUTDIR)/qlumpy.exe"
LINK32_OBJS= \
".\Release\quakegrb.obj" \
".\Release\qlumpy.obj" \
".\Release\wadlib.obj" \
".\Release\scriplib.obj" \
".\Release\cmdlib.obj" \
".\Release\lbmlib.obj"
"$(OUTDIR)\qlumpy.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
$(LINK32) @<<
$(LINK32_FLAGS) $(LINK32_OBJS)
<<
!ELSEIF "$(CFG)" == "qlumpy - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Target_Dir ""
OUTDIR=.\Debug
INTDIR=.\Debug
ALL : "$(OUTDIR)\qlumpy.exe"
CLEAN :
-@erase ".\Debug\vc40.pdb"
-@erase ".\Debug\vc40.idb"
-@erase ".\Debug\qlumpy.exe"
-@erase ".\Debug\lbmlib.obj"
-@erase ".\Debug\scriplib.obj"
-@erase ".\Debug\cmdlib.obj"
-@erase ".\Debug\qlumpy.obj"
-@erase ".\Debug\quakegrb.obj"
-@erase ".\Debug\wadlib.obj"
-@erase ".\Debug\qlumpy.ilk"
-@erase ".\Debug\qlumpy.pdb"
"$(OUTDIR)" :
if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /YX /c
# ADD CPP /nologo /Gm /GX /Zi /Od /I "..\common" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /YX /c
CPP_PROJ=/nologo /MLd /Gm /GX /Zi /Od /I "..\common" /D "WIN32" /D "_DEBUG" /D\
"_CONSOLE" /Fp"$(INTDIR)/qlumpy.pch" /YX /Fo"$(INTDIR)/" /Fd"$(INTDIR)/" /c
CPP_OBJS=.\Debug/
CPP_SBRS=
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
BSC32_FLAGS=/nologo /o"$(OUTDIR)/qlumpy.bsc"
BSC32_SBRS=
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386
LINK32_FLAGS=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib\
advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib\
odbccp32.lib /nologo /subsystem:console /incremental:yes\
/pdb:"$(OUTDIR)/qlumpy.pdb" /debug /machine:I386 /out:"$(OUTDIR)/qlumpy.exe"
LINK32_OBJS= \
".\Debug\lbmlib.obj" \
".\Debug\scriplib.obj" \
".\Debug\cmdlib.obj" \
".\Debug\qlumpy.obj" \
".\Debug\quakegrb.obj" \
".\Debug\wadlib.obj"
"$(OUTDIR)\qlumpy.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
$(LINK32) @<<
$(LINK32_FLAGS) $(LINK32_OBJS)
<<
!ENDIF
.c{$(CPP_OBJS)}.obj:
$(CPP) $(CPP_PROJ) $<
.cpp{$(CPP_OBJS)}.obj:
$(CPP) $(CPP_PROJ) $<
.cxx{$(CPP_OBJS)}.obj:
$(CPP) $(CPP_PROJ) $<
.c{$(CPP_SBRS)}.sbr:
$(CPP) $(CPP_PROJ) $<
.cpp{$(CPP_SBRS)}.sbr:
$(CPP) $(CPP_PROJ) $<
.cxx{$(CPP_SBRS)}.sbr:
$(CPP) $(CPP_PROJ) $<
################################################################################
# Begin Target
# Name "qlumpy - Win32 Release"
# Name "qlumpy - Win32 Debug"
!IF "$(CFG)" == "qlumpy - Win32 Release"
!ELSEIF "$(CFG)" == "qlumpy - Win32 Debug"
!ENDIF
################################################################################
# Begin Source File
SOURCE=.\quakegrb.c
!IF "$(CFG)" == "qlumpy - Win32 Release"
DEP_CPP_QUAKE=\
".\qlumpy.h"\
".\..\common\cmdlib.h"\
".\..\common\scriplib.h"\
".\..\common\lbmlib.h"\
".\..\common\wadlib.h"\
"$(INTDIR)\quakegrb.obj" : $(SOURCE) $(DEP_CPP_QUAKE) "$(INTDIR)"
!ELSEIF "$(CFG)" == "qlumpy - Win32 Debug"
DEP_CPP_QUAKE=\
".\qlumpy.h"\
".\..\common\cmdlib.h"\
".\..\common\scriplib.h"\
".\..\common\lbmlib.h"\
"$(INTDIR)\quakegrb.obj" : $(SOURCE) $(DEP_CPP_QUAKE) "$(INTDIR)"
!ENDIF
# End Source File
################################################################################
# Begin Source File
SOURCE=.\qlumpy.c
!IF "$(CFG)" == "qlumpy - Win32 Release"
DEP_CPP_QLUMP=\
".\qlumpy.h"\
".\..\common\cmdlib.h"\
".\..\common\scriplib.h"\
".\..\common\lbmlib.h"\
".\..\common\wadlib.h"\
"$(INTDIR)\qlumpy.obj" : $(SOURCE) $(DEP_CPP_QLUMP) "$(INTDIR)"
!ELSEIF "$(CFG)" == "qlumpy - Win32 Debug"
DEP_CPP_QLUMP=\
".\qlumpy.h"\
".\..\common\cmdlib.h"\
".\..\common\scriplib.h"\
".\..\common\lbmlib.h"\
"$(INTDIR)\qlumpy.obj" : $(SOURCE) $(DEP_CPP_QLUMP) "$(INTDIR)"
!ENDIF
# End Source File
################################################################################
# Begin Source File
SOURCE=\quake\utils2\common\cmdlib.c
DEP_CPP_CMDLI=\
".\..\common\cmdlib.h"\
{$(INCLUDE)}"\sys\TYPES.H"\
{$(INCLUDE)}"\sys\STAT.H"\
"$(INTDIR)\cmdlib.obj" : $(SOURCE) $(DEP_CPP_CMDLI) "$(INTDIR)"
$(CPP) $(CPP_PROJ) $(SOURCE)
# End Source File
################################################################################
# Begin Source File
SOURCE=\quake\utils2\common\scriplib.c
DEP_CPP_SCRIP=\
".\..\common\cmdlib.h"\
".\..\common\scriplib.h"\
"$(INTDIR)\scriplib.obj" : $(SOURCE) $(DEP_CPP_SCRIP) "$(INTDIR)"
$(CPP) $(CPP_PROJ) $(SOURCE)
# End Source File
################################################################################
# Begin Source File
SOURCE=\quake\utils2\common\wadlib.c
DEP_CPP_WADLI=\
{$(INCLUDE)}"\sys\TYPES.H"\
{$(INCLUDE)}"\sys\STAT.H"\
".\..\common\cmdlib.h"\
".\..\common\wadlib.h"\
"$(INTDIR)\wadlib.obj" : $(SOURCE) $(DEP_CPP_WADLI) "$(INTDIR)"
$(CPP) $(CPP_PROJ) $(SOURCE)
# End Source File
################################################################################
# Begin Source File
SOURCE=\quake\utils2\common\lbmlib.c
DEP_CPP_LBMLI=\
".\..\common\cmdlib.h"\
".\..\common\lbmlib.h"\
"$(INTDIR)\lbmlib.obj" : $(SOURCE) $(DEP_CPP_LBMLI) "$(INTDIR)"
$(CPP) $(CPP_PROJ) $(SOURCE)
# End Source File
# End Target
# End Project
################################################################################

BIN
qutils/QLUMPY/QLUMPY.MDP Normal file

Binary file not shown.

BIN
qutils/QLUMPY/QLUMPY.NCB Normal file

Binary file not shown.

523
qutils/QLUMPY/QUAKEGRB.C Normal file
View File

@@ -0,0 +1,523 @@
#include "qlumpy.h"
typedef struct
{
short ofs, length;
} row_t;
typedef struct
{
int width, height;
int widthbits, heightbits;
unsigned char data[4];
} qtex_t;
typedef struct
{
int width, height;
byte data[4]; // variably sized
} qpic_t;
#define SCRN(x,y) (*(byteimage+(y)*byteimagewidth+x))
/*
==============
GrabRaw
filename RAW x y width height
==============
*/
void GrabRaw (void)
{
int x,y,xl,yl,xh,yh,w,h;
byte *screen_p;
int linedelta;
GetToken (false);
xl = atoi (token);
GetToken (false);
yl = atoi (token);
GetToken (false);
w = atoi (token);
GetToken (false);
h = atoi (token);
xh = xl+w;
yh = yl+h;
screen_p = byteimage + yl*byteimagewidth + xl;
linedelta = byteimagewidth - w;
for (y=yl ; y<yh ; y++)
{
for (x=xl ; x<xh ; x++)
{
*lump_p++ = *screen_p;
*screen_p++ = 0;
}
screen_p += linedelta;
}
}
/*
==============
GrabPalette
filename PALETTE [startcolor endcolor]
==============
*/
void GrabPalette (void)
{
int start,end,length;
if (TokenAvailable())
{
GetToken (false);
start = atoi (token);
GetToken (false);
end = atoi (token);
}
else
{
start = 0;
end = 255;
}
length = 3*(end-start+1);
memcpy (lump_p, lbmpalette+start*3, length);
lump_p += length;
}
/*
==============
GrabPic
filename qpic x y width height
==============
*/
void GrabPic (void)
{
int x,y,xl,yl,xh,yh;
int width;
byte transcolor;
qpic_t *header;
GetToken (false);
xl = atoi (token);
GetToken (false);
yl = atoi (token);
GetToken (false);
xh = xl-1+atoi (token);
GetToken (false);
yh = yl-1+atoi (token);
if (xh<xl || yh<yl || xl < 0 || yl<0 || xh>319 || yh>199)
Error ("GrabPic: Bad size: %i, %i, %i, %i",xl,yl,xh,yh);
transcolor = 255;
//
// fill in header
//
header = (qpic_t *)lump_p;
width = xh-xl+1;
header->width = LittleLong(width);
header->height = LittleLong(yh-yl+1);
//
// start grabbing posts
//
lump_p = (byte *)header->data;
for (y=yl ; y<= yh ; y++)
for (x=xl ; x<=xh ; x++)
*lump_p++ = SCRN(x,y);
}
/*
=============================================================================
COLORMAP GRABBING
=============================================================================
*/
/*
===============
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;
//
// let any color go to 0 as a last resort
//
bestdistortion = ( (int)r*r + (int)g*g + (int)b*b )*2;
bestcolor = 0;
pal = lbmpalette + start*3;
for (i=start ; i<= stop ; i++)
{
dr = r - (int)pal[0];
dg = g - (int)pal[1];
db = b - (int)pal[2];
pal += 3;
distortion = dr*dr + dg*dg + db*db;
if (distortion < bestdistortion)
{
if (!distortion)
return i; // perfect match
bestdistortion = distortion;
bestcolor = i;
}
}
return bestcolor;
}
/*
==============
GrabColormap
filename COLORMAP levels fullbrights
the first map is an identiy 0-255
the final map is all black except for the fullbrights
the remaining maps are evenly spread
fullbright colors start at the top of the palette.
==============
*/
void GrabColormap (void)
{
int levels, brights;
int l, c;
float frac, red, green, blue;
GetToken (false);
levels = atoi (token);
GetToken (false);
brights = atoi (token);
// identity lump
for (l=0 ; l<256 ; l++)
*lump_p++ = l;
// shaded levels
for (l=1;l<levels;l++)
{
frac = 1.0 - (float)l/(levels-1);
for (c=0 ; c<256-brights ; c++)
{
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);
//
// note: 254 instead of 255 because 255 is the transparent color, and we
// don't want anything remapping to that
//
*lump_p++ = BestColor(red,green,blue, 0, 254);
}
for ( ; c<256 ; c++)
*lump_p++ = c;
}
*lump_p++ = brights;
}
/*
==============
GrabColormap2
experimental -- not used by quake
filename COLORMAP2 range levels fullbrights
fullbright colors start at the top of the palette.
Range can be greater than 1 to allow overbright color tables.
the first map is all 0
the last (levels-1) map is at range
==============
*/
void GrabColormap2 (void)
{
int levels, brights;
int l, c;
float frac, red, green, blue;
float range;
GetToken (false);
range = atof (token);
GetToken (false);
levels = atoi (token);
GetToken (false);
brights = atoi (token);
// shaded levels
for (l=0;l<levels;l++)
{
frac = range - range*(float)l/(levels-1);
for (c=0 ; c<256-brights ; c++)
{
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);
//
// note: 254 instead of 255 because 255 is the transparent color, and we
// don't want anything remapping to that
//
*lump_p++ = BestColor(red,green,blue, 0, 254);
}
// fullbrights allways stay the same
for ( ; c<256 ; c++)
*lump_p++ = c;
}
*lump_p++ = brights;
}
/*
=============================================================================
MIPTEX GRABBING
=============================================================================
*/
typedef struct
{
char name[16];
unsigned width, height;
unsigned offsets[4]; // four mip maps stored
} miptex_t;
byte pixdata[256];
int d_red, d_green, d_blue;
/*
=============
AveragePixels
=============
*/
byte AveragePixels (int count)
{
int r,g,b;
int i;
int vis;
int pix;
int dr, dg, db;
int bestdistortion, distortion;
int bestcolor;
byte *pal;
int fullbright;
int e;
vis = 0;
r = g = b = 0;
fullbright = 0;
for (i=0 ; i<count ; i++)
{
pix = pixdata[i];
if (pix == 255)
fullbright = 2;
else if (pix >= 240)
{
return pix;
if (!fullbright)
{
fullbright = true;
r = 0;
g = 0;
b = 0;
}
}
else
{
if (fullbright)
continue;
}
r += lbmpalette[pix*3];
g += lbmpalette[pix*3+1];
b += lbmpalette[pix*3+2];
vis++;
}
if (fullbright == 2)
return 255;
r /= vis;
g /= vis;
b /= vis;
if (!fullbright)
{
r += d_red;
g += d_green;
b += d_blue;
}
//
// find the best color
//
bestdistortion = r*r + g*g + b*b;
bestcolor = 0;
if (fullbright)
{
i = 240;
e = 255;
}
else
{
i = 0;
e = 240;
}
for ( ; i< e ; i++)
{
pix = i; //pixdata[i];
pal = lbmpalette + pix*3;
dr = r - (int)pal[0];
dg = g - (int)pal[1];
db = b - (int)pal[2];
distortion = dr*dr + dg*dg + db*db;
if (distortion < bestdistortion)
{
if (!distortion)
{
d_red = d_green = d_blue = 0; // no distortion yet
return pix; // perfect match
}
bestdistortion = distortion;
bestcolor = pix;
}
}
if (!fullbright)
{ // error diffusion
pal = lbmpalette + bestcolor*3;
d_red = r - (int)pal[0];
d_green = g - (int)pal[1];
d_blue = b - (int)pal[2];
}
return bestcolor;
}
/*
==============
GrabMip
filename MIP x y width height
must be multiples of sixteen
==============
*/
void GrabMip (void)
{
int x,y,xl,yl,xh,yh,w,h;
byte *screen_p, *source;
int linedelta;
miptex_t *qtex;
int miplevel, mipstep;
int xx, yy, pix;
int count;
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);
xh = xl+w;
yh = yl+h;
qtex = (miptex_t *)lump_p;
qtex->width = LittleLong(w);
qtex->height = LittleLong(h);
strcpy (qtex->name, lumpname);
lump_p = (byte *)&qtex->offsets[4];
screen_p = byteimage + yl*byteimagewidth + xl;
linedelta = byteimagewidth - w;
source = lump_p;
qtex->offsets[0] = LittleLong(lump_p - (byte *)qtex);
for (y=yl ; y<yh ; y++)
{
for (x=xl ; x<xh ; x++)
{
pix = *screen_p;
*screen_p++ = 0;
if (pix == 255)
pix = 0;
*lump_p++ = pix;
}
screen_p += linedelta;
}
//
// subsample for greater mip levels
//
d_red = d_green = d_blue = 0; // no distortion yet
for (miplevel = 1 ; miplevel<4 ; miplevel++)
{
qtex->offsets[miplevel] = LittleLong(lump_p - (byte *)qtex);
mipstep = 1<<miplevel;
for (y=0 ; y<h ; y+=mipstep)
{
for (x = 0 ; x<w ; x+= mipstep)
{
count = 0;
for (yy=0 ; yy<mipstep ; yy++)
for (xx=0 ; xx<mipstep ; xx++)
{
pixdata[count] = source[ (y+yy)*w + x + xx ];
count++;
}
*lump_p++ = AveragePixels (count);
}
}
}
}