forked from bunder/qpakman
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathim_gen.cc
182 lines (141 loc) · 4.1 KB
/
im_gen.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//------------------------------------------------------------------------
// Generation of Special files
//------------------------------------------------------------------------
//
// Copyright (c) 2008 Andrew J Apted
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------
#include "headers.h"
#include "main.h"
#include <map>
#include <algorithm>
#include "pakfile.h"
#include "im_color.h"
#include "im_image.h"
#include "im_gen.h"
#include "im_mip.h"
#include "q1_structs.h"
static void GEN_InversePalette(const char *filename)
{
// Hexen II lump
printf("Generating inverse palette: %s\n", filename);
FILE *fp = fopen(filename, "wb");
if (! fp)
FatalError("Cannot create file! %s\n", strerror(errno));
COL_SetFullBright(true);
COL_SetTransparent(0); // will not occur
char buffer[64];
for (int r = 0; r < 64; r++)
for (int g = 0; g < 64; g++)
{
for (int b = 0; b < 64; b++)
{
buffer[b] = COL_MapColor(MAKE_RGB(r<<2, g<<2, b<<2));
}
if (1 != fwrite(buffer, sizeof(buffer), 1, fp))
{
fclose(fp);
FatalError("Error writing file!\n");
}
}
fclose(fp);
printf("Finished.\n");
}
static void GEN_16to8_Table(const char *filename)
{
// Quake II lump
printf("Generating Inverse16 table: %s\n", filename);
FILE *fp = fopen(filename, "wb");
if (! fp)
FatalError("Cannot create file! %s\n", strerror(errno));
COL_SetFullBright(true);
COL_SetTransparent(0); // will not occur
char buffer[32];
for (int b = 0; b < 32; b++)
for (int g = 0; g < 64; g++)
{
for (int r = 0; r < 32; r++)
{
buffer[r] = COL_MapColor(MAKE_RGB(r<<3, g<<2, b<<3));
}
if (1 != fwrite(buffer, sizeof(buffer), 1, fp))
{
fclose(fp);
FatalError("Error writing file!\n");
}
}
fclose(fp);
printf("Finished.\n");
}
//------------------------------------------------------------------------
static void GEN_TintTable(const char *filename, int x_mul, int y_mul)
{
// Hexen II lumps
printf("Generating tint table: %s\n", filename);
FILE *fp = fopen(filename, "wb");
if (! fp)
FatalError("Cannot create file! %s\n", strerror(errno));
COL_SetFullBright(true);
COL_SetTransparent(0); // will not occur
char buffer[256]; // one row
for (int y = 0; y < 256; y++)
{
for (int x = 0; x < 256; x++)
{
u32_t XC = COL_ReadPalette(x);
u32_t YC = COL_ReadPalette(y);
int r = (RGB_R(XC) * x_mul + RGB_R(YC) * y_mul) >> 8;
int g = (RGB_G(XC) * x_mul + RGB_G(YC) * y_mul) >> 8;
int b = (RGB_B(XC) * x_mul + RGB_B(YC) * y_mul) >> 8;
// clamp the result
if (r & 0xF00) r = 255;
if (g & 0xF00) g = 255;
if (b & 0xF00) b = 255;
buffer[x] = COL_MapColor(MAKE_RGB(r, g, b));
}
if (1 != fwrite(buffer, sizeof(buffer), 1, fp))
{
fclose(fp);
FatalError("Error writing file!\n");
}
}
fclose(fp);
printf("Finished.\n");
}
//------------------------------------------------------------------------
bool GEN_TryCreateSpecial(const char *filename)
{
if (StringCaseCmp(FindBaseName(filename), "invpal.lmp") == 0)
{
GEN_InversePalette(filename);
return true;
}
if (StringCaseCmp(FindBaseName(filename), "16to8.dat") == 0)
{
GEN_16to8_Table(filename);
return true;
}
if (StringCaseCmp(FindBaseName(filename), "tinttab2.lmp") == 0)
{
GEN_TintTable(filename, 165, 91);
return true;
}
else if (StringCaseCmp(FindBaseName(filename), "tinttab.lmp") == 0)
{
GEN_TintTable(filename, 128, 200);
return true;
}
return false;
}
//--- editor settings ---
// vi:ts=2:sw=2:expandtab