]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - palette.c
added rampsize parameter to BuildGammaTable8 and 16
[xonotic/darkplaces.git] / palette.c
index 0f76f3727205c5e4e83f7a0f8fbd7165bd9649ec..21974d4de9e8c1b1dd905bb7d141df157ae461d7 100644 (file)
--- a/palette.c
+++ b/palette.c
@@ -2,19 +2,18 @@
 #include "quakedef.h"
 
 unsigned int palette_complete[256];
+unsigned int palette_font[256];
+unsigned int palette_alpha[256];
+unsigned int palette_nocolormap[256];
+unsigned int palette_nocolormapnofullbrights[256];
 unsigned int palette_nofullbrights[256];
 unsigned int palette_onlyfullbrights[256];
-unsigned int palette_nocolormapnofullbrights[256];
-unsigned int palette_nocolormap[256];
 unsigned int palette_pantsaswhite[256];
 unsigned int palette_shirtaswhite[256];
-unsigned int palette_alpha[256];
-unsigned int palette_font[256];
-
-qbyte host_basepal[768];
+unsigned int palette_transparent[256];
 
 // John Carmack said the quake palette.lmp can be considered public domain because it is not an important asset to id, so I include it here as a fallback if no external palette file is found.
-qbyte host_quakepal[768] =
+unsigned char host_quakepal[768] =
 {
 0,0,0,15,15,15,31,31,31,47,47,47,63,63,63,75,75,75,91,91,91,107,107,107,123,123,123,139,139,139,155,155,155,171,171,171,187,187,187,203,203,203,219,219,219,235,235,235,15,11,7,23,15,11,31,23,11,39,27,15,47,35,19,55,43,23,63,47,23,75,55,27,83,59,27,91,67,31,99,75,31,107,83,31,115,87,31,123,95,35,131,103,35,143,111,35,11,11,15,19,19,27,27,27,39,39,39,51,47,47,63,55,55,75,63,63,87,71,71,103,79,79,115,91,91,127,99,99,
 139,107,107,151,115,115,163,123,123,175,131,131,187,139,139,203,0,0,0,7,7,0,11,11,0,19,19,0,27,27,0,35,35,0,43,43,7,47,47,7,55,55,7,63,63,7,71,71,7,75,75,11,83,83,11,91,91,11,99,99,11,107,107,15,7,0,0,15,0,0,23,0,0,31,0,0,39,0,0,47,0,0,55,0,0,63,0,0,71,0,0,79,0,0,87,0,0,95,0,0,103,0,0,111,0,0,119,0,0,127,0,0,19,19,0,27,27,0,35,35,0,47,43,0,55,47,0,67,
@@ -24,30 +23,19 @@ qbyte host_quakepal[768] =
 43,175,47,47,159,47,47,143,47,47,127,47,47,111,47,47,95,43,43,79,35,35,63,27,27,47,19,19,31,11,11,15,43,0,0,59,0,0,75,7,0,95,7,0,111,15,0,127,23,7,147,31,7,163,39,11,183,51,15,195,75,27,207,99,43,219,127,59,227,151,79,231,171,95,239,191,119,247,211,139,167,123,59,183,155,55,199,195,55,231,227,87,127,191,255,171,231,255,215,255,255,103,0,0,139,0,0,179,0,0,215,0,0,255,0,0,255,243,147,255,247,199,255,255,255,159,91,83
 };
 
-void Palette_Setup8to24(void)
+void Palette_SetupSpecialPalettes(void)
 {
        int i;
        int fullbright_start, fullbright_end;
        int pants_start, pants_end;
        int shirt_start, shirt_end;
        int reversed_start, reversed_end;
-       qbyte *in, *out;
-       qbyte *colormap;
+       int transparentcolor;
+       unsigned char *colormap;
+       fs_offset_t filesize;
 
-       in = host_basepal;
-       out = (qbyte *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
-       for (i = 0;i < 255;i++)
-       {
-               *out++ = *in++;
-               *out++ = *in++;
-               *out++ = *in++;
-               *out++ = 255;
-       }
-       palette_complete[255] = 0; // completely transparent black
-
-       // FIXME: fullbright_start should be read from colormap.lmp
-       colormap = FS_LoadFile("gfx/colormap.lmp", true);
-       if (colormap && fs_filesize >= 16385)
+       colormap = FS_LoadFile("gfx/colormap.lmp", tempmempool, true, &filesize);
+       if (colormap && filesize >= 16385)
                fullbright_start = 256 - colormap[16384];
        else
                fullbright_start = 256;
@@ -60,18 +48,21 @@ void Palette_Setup8to24(void)
        shirt_end = 32;
        reversed_start = 128;
        reversed_end = 224;
+       transparentcolor = 255;
+
+       for (i = 0;i < 256;i++)
+               palette_transparent[i] = palette_complete[i];
+       palette_transparent[transparentcolor] = 0;
 
        for (i = 0;i < fullbright_start;i++)
                palette_nofullbrights[i] = palette_complete[i];
-       for (i = fullbright_start;i < 255;i++)
+       for (i = fullbright_start;i < fullbright_end;i++)
                palette_nofullbrights[i] = palette_complete[0];
-       palette_nofullbrights[255] = 0;
 
        for (i = 0;i < 256;i++)
                palette_onlyfullbrights[i] = palette_complete[0];
        for (i = fullbright_start;i < fullbright_end;i++)
                palette_onlyfullbrights[i] = palette_complete[i];
-       palette_onlyfullbrights[255] = 0;
 
        for (i = 0;i < 256;i++)
                palette_nocolormapnofullbrights[i] = palette_complete[i];
@@ -81,7 +72,6 @@ void Palette_Setup8to24(void)
                palette_nocolormapnofullbrights[i] = palette_complete[0];
        for (i = fullbright_start;i < fullbright_end;i++)
                palette_nocolormapnofullbrights[i] = palette_complete[0];
-       palette_nocolormapnofullbrights[255] = 0;
 
        for (i = 0;i < 256;i++)
                palette_nocolormap[i] = palette_complete[i];
@@ -89,7 +79,6 @@ void Palette_Setup8to24(void)
                palette_nocolormap[i] = palette_complete[0];
        for (i = shirt_start;i < shirt_end;i++)
                palette_nocolormap[i] = palette_complete[0];
-       palette_nocolormap[255] = 0;
 
        for (i = 0;i < 256;i++)
                palette_pantsaswhite[i] = palette_complete[0];
@@ -111,67 +100,40 @@ void Palette_Setup8to24(void)
                        palette_shirtaswhite[i] = palette_complete[i - shirt_start];
        }
 
-       for (i = 0;i < 255;i++)
+       for (i = 0;i < 256;i++)
                palette_alpha[i] = 0xFFFFFFFF;
-       palette_alpha[255] = 0;
+       palette_alpha[transparentcolor] = 0;
 
-       palette_font[0] = 0;
-       for (i = 1;i < 255;i++)
+       for (i = 0;i < 256;i++)
                palette_font[i] = palette_complete[i];
-       palette_font[255] = 0;
+       palette_font[0] = 0;
 }
 
-void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
+void BuildGammaTable8(float prescale, float gamma, float scale, float base, unsigned char *out, int rampsize)
 {
        int i, adjusted;
-       double invgamma, d;
+       double invgamma;
 
-       gamma = bound(0.1, gamma, 5.0);
-       if (gamma == 1) // LordHavoc: dodge the math
+       invgamma = 1.0 / gamma;
+       prescale /= (double) (rampsize - 1);
+       for (i = 0;i < rampsize;i++)
        {
-               for (i = 0;i < 256;i++)
-               {
-                       adjusted = (int) (i * prescale * scale + base * 255.0);
-                       out[i] = bound(0, adjusted, 255);
-               }
-       }
-       else
-       {
-               invgamma = 1.0 / gamma;
-               prescale /= 255.0f;
-               for (i = 0;i < 256;i++)
-               {
-                       d = pow((double) i * prescale, invgamma) * scale + base;
-                       adjusted = (int) (255.0 * d);
-                       out[i] = bound(0, adjusted, 255);
-               }
+               adjusted = (int) (255.0 * (pow((double) i * prescale, invgamma) * scale + base) + 0.5);
+               out[i] = bound(0, adjusted, 255);
        }
 }
 
-void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
+void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out, int rampsize)
 {
        int i, adjusted;
-       double invgamma, d;
+       double invgamma;
 
-       gamma = bound(0.1, gamma, 5.0);
-       if (gamma == 1) // LordHavoc: dodge the math
-       {
-               for (i = 0;i < 256;i++)
-               {
-                       adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
-                       out[i] = bound(0, adjusted, 65535);
-               }
-       }
-       else
+       invgamma = 1.0 / gamma;
+       prescale /= (double) (rampsize - 1);
+       for (i = 0;i < rampsize;i++)
        {
-               invgamma = 1.0 / gamma;
-               prescale /= 255.0f;
-               for (i = 0;i < 256;i++)
-               {
-                       d = pow((double) i * prescale, invgamma) * scale + base;
-                       adjusted = (int) (65535.0 * d);
-                       out[i] = bound(0, adjusted, 65535);
-               }
+               adjusted = (int) (65535.0 * (pow((double) i * prescale, invgamma) * scale + base) + 0.5);
+               out[i] = bound(0, adjusted, 65535);
        }
 }
 
@@ -179,30 +141,22 @@ void Palette_Init(void)
 {
        int i;
        float gamma, scale, base;
-       qbyte *pal;
-       qbyte temp[256];
-       pal = (qbyte *)FS_LoadFile ("gfx/palette.lmp", false);
-       if (pal && fs_filesize >= 768)
-       {
-               memcpy(host_basepal, pal, 765);
-               Mem_Free(pal);
-       }
-       else
-       {
-               Con_DPrint("Couldn't load gfx/palette.lmp, falling back on internal palette\n");
-               memcpy(host_basepal, host_quakepal, 765);
-       }
-       host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
+       fs_offset_t filesize;
+       unsigned char *in, *out, *palfile;
+       unsigned char texturegammaramp[256];
 
        gamma = 1;
        scale = 1;
        base = 0;
+// COMMANDLINEOPTION: Client: -texgamma <number> sets the quake palette gamma, allowing you to make quake textures brighter/darker, not recommended
        i = COM_CheckParm("-texgamma");
        if (i)
                gamma = atof(com_argv[i + 1]);
+// COMMANDLINEOPTION: Client: -texcontrast <number> sets the quake palette contrast, allowing you to make quake textures brighter/darker, not recommended
        i = COM_CheckParm("-texcontrast");
        if (i)
                scale = atof(com_argv[i + 1]);
+// COMMANDLINEOPTION: Client: -texbrightness <number> sets the quake palette brightness (brightness of black), allowing you to make quake textures brighter/darker, not recommended
        i = COM_CheckParm("-texbrightness");
        if (i)
                base = atof(com_argv[i + 1]);
@@ -210,10 +164,27 @@ void Palette_Init(void)
        scale = bound(0.01, scale, 10.0);
        base = bound(0, base, 0.95);
 
-       BuildGammaTable8(1.0f, gamma, scale, base, temp);
-       for (i = 3;i < 765;i++)
-               host_basepal[i] = temp[host_basepal[i]];
+       BuildGammaTable8(1.0f, gamma, scale, base, texturegammaramp, 256);
+
+       palfile = (unsigned char *)FS_LoadFile ("gfx/palette.lmp", tempmempool, false, &filesize);
+       if (palfile && filesize >= 768)
+               in = palfile;
+       else
+       {
+               Con_DPrint("Couldn't load gfx/palette.lmp, falling back on internal palette\n");
+               in = host_quakepal;
+       }
+       out = (unsigned char *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
+       for (i = 0;i < 256;i++)
+       {
+               *out++ = texturegammaramp[*in++];
+               *out++ = texturegammaramp[*in++];
+               *out++ = texturegammaramp[*in++];
+               *out++ = 255;
+       }
+       if (palfile)
+               Mem_Free(palfile);
 
-       Palette_Setup8to24();
+       Palette_SetupSpecialPalettes();
 }