]> icculus.org git repositories - divverent/darkplaces.git/blob - palette.c
Fix for memset glibc bug crash
[divverent/darkplaces.git] / palette.c
1
2 #include "quakedef.h"
3
4 unsigned int d_8to24table[256];
5 //byte d_15to8table[32768];
6 byte host_basepal[768];
7 byte texgamma[256];
8
9 cvar_t vid_gamma = {CVAR_SAVE, "vid_gamma", "1"};
10 cvar_t vid_brightness = {CVAR_SAVE, "vid_brightness", "1"};
11 cvar_t vid_contrast = {CVAR_SAVE, "vid_contrast", "1"};
12
13 void Palette_Setup8to24(void)
14 {
15         byte *in, *out;
16         unsigned short i;
17
18         in = host_basepal;
19         out = (byte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
20         for (i=0 ; i<255 ; i++)
21         {
22                 *out++ = *in++;
23                 *out++ = *in++;
24                 *out++ = *in++;
25                 *out++ = 255;
26         }
27         d_8to24table[255] = 0; // completely transparent black
28 }
29
30 /*
31 void    Palette_Setup15to8(void)
32 {
33         byte    *pal;
34         unsigned r,g,b;
35         unsigned v;
36         int     r1,g1,b1;
37         int             j,k,l;
38         unsigned short i;
39
40         for (i = 0;i < 32768;i++)
41         {
42                 r = ((i & 0x001F) << 3)+4;
43                 g = ((i & 0x03E0) >> 2)+4;
44                 b = ((i & 0x7C00) >> 7)+4;
45                 pal = (unsigned char *)d_8to24table;
46                 for (v = 0, k = 0, l = 1000000000;v < 256;v++, pal += 4)
47                 {
48                         r1 = r - pal[0];
49                         g1 = g - pal[1];
50                         b1 = b - pal[2];
51                         j = r1*r1+g1*g1+b1*b1;
52                         if (j < l)
53                         {
54                                 k = v;
55                                 l = j;
56                         }
57                 }
58                 d_15to8table[i] = k;
59         }
60 }
61 */
62
63 void BuildGammaTable8(float prescale, float gamma, float scale, float base, byte *out)
64 {
65         int i, adjusted;
66         double invgamma, d;
67
68         gamma = bound(0.1, gamma, 5.0);
69         if (gamma == 1) // LordHavoc: dodge the math
70         {
71                 for (i = 0;i < 256;i++)
72                 {
73                         adjusted = (int) (i * prescale * scale + base * 255.0);
74                         out[i] = bound(0, adjusted, 255);
75                 }
76         }
77         else
78         {
79                 invgamma = 1.0 / gamma;
80                 prescale /= 255.0f;
81                 for (i = 0;i < 256;i++)
82                 {
83                         d = pow((double) i * prescale, invgamma) * scale + base;
84                         adjusted = (int) (255.0 * d);
85                         out[i] = bound(0, adjusted, 255);
86                 }
87         }
88 }
89
90 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
91 {
92         int i, adjusted;
93         double invgamma, d;
94
95         gamma = bound(0.1, gamma, 5.0);
96         if (gamma == 1) // LordHavoc: dodge the math
97         {
98                 for (i = 0;i < 256;i++)
99                 {
100                         adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
101                         out[i] = bound(0, adjusted, 65535);
102                 }
103         }
104         else
105         {
106                 invgamma = 1.0 / gamma;
107                 prescale /= 255.0f;
108                 for (i = 0;i < 256;i++)
109                 {
110                         d = pow((double) i * prescale, invgamma) * scale + base;
111                         adjusted = (int) (65535.0 * d);
112                         out[i] = bound(0, adjusted, 65535);
113                 }
114         }
115 }
116
117 qboolean hardwaregammasupported = false;
118 void VID_UpdateGamma(qboolean force)
119 {
120         static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1, cachelighthalf = -1;
121
122         // LordHavoc: don't mess with gamma tables if running dedicated
123         if (cls.state == ca_dedicated)
124                 return;
125
126         if (!force && vid_gamma.value == cachegamma && vid_brightness.value == cachebrightness && vid_contrast.value == cachecontrast && lighthalf == cachelighthalf)
127                 return;
128
129         if (vid_gamma.value < 0.1)
130                 Cvar_SetValue("vid_gamma", 0.1);
131         if (vid_gamma.value > 5.0)
132                 Cvar_SetValue("vid_gamma", 5.0);
133
134         if (vid_brightness.value < 1.0)
135                 Cvar_SetValue("vid_brightness", 1.0);
136         if (vid_brightness.value > 5.0)
137                 Cvar_SetValue("vid_brightness", 5.0);
138
139         if (vid_contrast.value < 0.2)
140                 Cvar_SetValue("vid_contrast", 0.2);
141         if (vid_contrast.value > 1)
142                 Cvar_SetValue("vid_contrast", 1);
143
144         cachegamma = vid_gamma.value;
145         cachebrightness = vid_brightness.value;
146         cachecontrast = vid_contrast.value;
147         cachelighthalf = lighthalf;
148
149         hardwaregammasupported = VID_SetGamma((cachelighthalf ? 2.0f : 1.0f), cachegamma, cachebrightness * cachecontrast, 1 - cachecontrast);
150         if (!hardwaregammasupported)
151                 Con_Printf("Hardware gamma not supported.\n");
152 }
153
154 void Gamma_Init(void)
155 {
156         Cvar_RegisterVariable(&vid_gamma);
157         Cvar_RegisterVariable(&vid_brightness);
158         Cvar_RegisterVariable(&vid_contrast);
159 }
160
161 void Palette_Init(void)
162 {
163         byte *pal;
164         pal = (byte *)COM_LoadFile ("gfx/palette.lmp", false);
165         if (!pal)
166                 Sys_Error ("Couldn't load gfx/palette.lmp");
167         memcpy(host_basepal, pal, 765);
168         Mem_Free(pal);
169         host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
170         Palette_Setup8to24();
171 //      Palette_Setup15to8();
172 }