Debian Codegraph @

prboom2/src/dsda/tranmap.c

raw
1 //
2 // Copyright(C) 2023 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA TRANMAP
16 //
17
18 #include <string.h>
19
20 #include "md5.h"
21 #include "lprintf.h"
22 #include "m_file.h"
23 #include "w_wad.h"
24 #include "z_zone.h"
25
26 #include "dsda/data_organizer.h"
27 #include "dsda/utility.h"
28
29 #include "tranmap.h"
30
31 static char* tranmap_base_dir;
32 static char* tranmap_palette_dir;
33 static dsda_cksum_t playpal_cksum;
34 static const int default_tranmap_alpha = 66;
35 static const int tranmap_length = 256 * 256;
36 static const byte* tranmap_data[100];
37
38 static void dsda_CalculatePlaypalCksum(void) {
39 struct MD5Context md5;
40 int lump;
41
42 lump = W_GetNumForName("PLAYPAL");
43
44 MD5Init(&md5);
45 MD5Update(&md5, W_LumpByNum(lump), W_LumpLength(lump));
46 MD5Final(playpal_cksum.bytes, &md5);
47 dsda_TranslateCheckSum(&playpal_cksum);
48 }
49
50 static void dsda_InitTranMapBaseDir(void) {
51 int length;
52 const char* data_root;
53
54 data_root = dsda_DataRoot();
55
56 length = strlen(data_root) + 10; // "/tranmaps\0"
57 tranmap_base_dir = Z_Malloc(length);
58 snprintf(tranmap_base_dir, length, "%s/tranmaps", data_root);
59
60 M_MakeDir(tranmap_base_dir, false);
61 }
62
63 static void dsda_InitTranMapPaletteDir(void) {
64 int length;
65
66 if (!tranmap_base_dir)
67 dsda_InitTranMapBaseDir();
68
69 if (!playpal_cksum.string[0])
70 dsda_CalculatePlaypalCksum();
71
72 length = strlen(tranmap_base_dir) + 34; // "/<cksum (32)>\0"
73 tranmap_palette_dir = Z_Malloc(length);
74 snprintf(tranmap_palette_dir, length, "%s/%s", tranmap_base_dir, playpal_cksum.string);
75
76 M_MakeDir(tranmap_palette_dir, false);
77 }
78
79 //
80 // R_InitTranMap
81 //
82 // Initialize translucency filter map
83 //
84 // By Lee Killough 2/21/98
85 //
86
87 #define TSC 12 /* number of fixed point digits in filter percent */
88
89 static byte* dsda_GenerateTranMap(unsigned int alpha) {
90 byte* buffer;
91 const byte* playpal;
92 int pal[3][256];
93 int tot[256];
94 int pal_w1[3][256];
95 int w1, w2;
96
97 playpal = W_LumpByName("PLAYPAL");
98
99 w1 = (alpha << TSC) / 100;
100 w2 = (1l << TSC) - w1;
101
102 buffer = Z_Malloc(tranmap_length);
103
104 // First, convert playpal into long int type, and transpose array,
105 // for fast inner-loop calculations. Precompute tot array.
106 {
107 register int i = 255;
108 register const unsigned char *p = playpal + 255 * 3;
109
110 do {
111 register int t, d;
112 pal_w1[0][i] = (pal[0][i] = t = p[0]) * w1;
113 d = t * t;
114 pal_w1[1][i] = (pal[1][i] = t = p[1]) * w1;
115 d += t * t;
116 pal_w1[2][i] = (pal[2][i] = t = p[2]) * w1;
117 d += t * t;
118 p -= 3;
119 tot[i] = d << (TSC - 1);
120 }
121 while (--i >= 0);
122 }
123
124 // Next, compute all entries using minimum arithmetic.
125 {
126 int i, j;
127 byte *tp = buffer;
128
129 for (i = 0; i < 256; i++) {
130 int r1 = pal[0][i] * w2;
131 int g1 = pal[1][i] * w2;
132 int b1 = pal[2][i] * w2;
133
134 for (j = 0; j < 256; j++, tp++) {
135 register int color = 255;
136 register int err;
137 int r = pal_w1[0][j] + r1;
138 int g = pal_w1[1][j] + g1;
139 int b = pal_w1[2][j] + b1;
140 int best = INT_MAX;
141
142 do {
143 err = tot[color] - pal[0][color] * r - pal[1][color] * g - pal[2][color] * b;
144 if (err < best) {
145 best = err;
146 *tp = color;
147 }
148 }
149 while (--color >= 0);
150 }
151 }
152 }
153
154 return buffer;
155 }
156
157 const byte* dsda_TranMap(unsigned int alpha) {
158 int length;
159 byte *buffer = NULL;
160
161 if (alpha > 99)
162 return NULL;
163
164 if (!tranmap_data[alpha]) {
165 char* filename;
166
167 if (!tranmap_palette_dir)
168 dsda_InitTranMapPaletteDir();
169
170 length = strlen(tranmap_palette_dir) + 16; // "/tranmap_99.dat\0"
171 filename = Z_Malloc(length);
172 snprintf(filename, length, "%s/tranmap_%02d.dat", tranmap_palette_dir, alpha);
173
174 length = M_ReadFile(filename, &buffer);
175 if (buffer && length != tranmap_length) {
176 Z_Free(buffer);
177 buffer = NULL;
178 }
179
180 if (!buffer) {
181 buffer = dsda_GenerateTranMap(alpha);
182
183 M_WriteFile(filename, buffer, tranmap_length);
184 }
185
186 tranmap_data[alpha] = buffer;
187 }
188
189 return tranmap_data[alpha];
190 }
191
192 const byte* dsda_DefaultTranMap(void) {
193 int lump;
194
195 lump = W_CheckNumForName("TRANMAP");
196
197 if (lump != LUMP_NOT_FOUND)
198 return W_LumpByNum(lump);
199
200 return dsda_TranMap(default_tranmap_alpha);
201 }