every malloc/calloc/free converted to qmalloc/qfree with tracking (memstats command...
[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 qgamma[256];
8 static float vid_gamma = 1.0;
9
10 void Palette_Setup8to24()
11 {
12         byte *in, *out;
13         unsigned short i;
14
15         in = host_basepal;
16         out = (byte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
17         for (i=0 ; i<255 ; i++)
18         {
19                 *out++ = *in++;
20                 *out++ = *in++;
21                 *out++ = *in++;
22                 *out++ = 255;
23         }
24         d_8to24table[255] = 0; // completely transparent black
25 }
26
27 /*
28 void    Palette_Setup15to8()
29 {
30         byte    *pal;
31         unsigned r,g,b;
32         unsigned v;
33         int     r1,g1,b1;
34         int             j,k,l;
35         unsigned short i;
36
37         for (i = 0;i < 32768;i++)
38         {
39                 r = ((i & 0x001F) << 3)+4;
40                 g = ((i & 0x03E0) >> 2)+4;
41                 b = ((i & 0x7C00) >> 7)+4;
42                 pal = (unsigned char *)d_8to24table;
43                 for (v = 0, k = 0, l = 1000000000;v < 256;v++, pal += 4)
44                 {
45                         r1 = r - pal[0];
46                         g1 = g - pal[1];
47                         b1 = b - pal[2];
48                         j = r1*r1+g1*g1+b1*b1;
49                         if (j < l)
50                         {
51                                 k = v;
52                                 l = j;
53                         }
54                 }
55                 d_15to8table[i] = k;
56         }
57 }
58 */
59
60 void Palette_Gamma ()
61 {
62         float   inf;
63         int             i;
64
65         vid_gamma = 1;
66         if ((i = COM_CheckParm("-gamma")))
67                 vid_gamma = atof(com_argv[i+1]);
68
69         if (vid_gamma == 1) // LordHavoc: dodge the math
70         {
71                 for (i = 0;i < 256;i++)
72                         qgamma[i] = i;
73         }
74         else
75         {
76                 for (i = 0;i < 256;i++)
77                 {
78                         inf = pow((i+1)/256.0, vid_gamma)*255 + 0.5;
79                         if (inf < 0) inf = 0;
80                         if (inf > 255) inf = 255;
81                         qgamma[i] = inf;
82                 }
83         }
84 }
85
86 void Palette_Init()
87 {
88         byte *pal;
89         pal = (byte *)COM_LoadMallocFile ("gfx/palette.lmp", false);
90         if (!pal)
91                 Sys_Error ("Couldn't load gfx/palette.lmp");
92         memcpy(host_basepal, pal, 765);
93         qfree(pal);
94         host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
95         Palette_Setup8to24();
96 //      Palette_Setup15to8();
97         Palette_Gamma();
98 }