-
Notifications
You must be signed in to change notification settings - Fork 1
/
Colormap.cs
178 lines (139 loc) · 5.19 KB
/
Colormap.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Smith
{
public class Colormap
{
public readonly string Name;
public struct RGB
{
public byte R;
public byte G;
public byte B;
public bool SelfIlluminated;
public System.Drawing.Color Color
{
get
{
return System.Drawing.Color.FromArgb(R, G, B);
}
}
}
public float TintR;
public float TintG;
public float TintB;
public RGB[] Palette = new RGB[256];
public override string ToString()
{
return Name;
}
public int FindClosestColor(int r, int g, int b, bool skipSelfIlluminated, int skipIndex=-1)
{
#if false
// donno how we are supposed to know what palette entries are taboo:
// maybe its MaterialHeader.colorIndex (provided as skipIndex) but
// bah.mat (originally from matmaster) specifies 255 as colorindex
// yet never uses palette[0].. uncertain if matmaster is correct, tho
int smallestDiffIndex = (skipIndex == 0) ? 2 : 1;
#else
int smallestDiffIndex = (skipIndex == 0) ? 1 : 0;
#endif
for (int i=smallestDiffIndex 1; i<256; i )
{
if (i == skipIndex)
continue;
if (skipSelfIlluminated && Palette[i].SelfIlluminated)
continue;
int bestDiff = DetermineColorDiff(smallestDiffIndex, r, g, b);
int curDiff = DetermineColorDiff(i, r, g, b);
if (curDiff < bestDiff)
smallestDiffIndex = i;
// if diff is 0 then dont bother checking more
if (curDiff == 0)
break;
}
return smallestDiffIndex;
}
private int DetermineColorDiff(int i, int r, int g, int b)
{
RGB p = Palette[i];
int diff = 0;
diff = Math.Abs((int)p.R - r);
diff = Math.Abs((int)p.G - g);
diff = Math.Abs((int)p.B - b);
return diff;
}
public Colormap(string n, Stream s)
{
Name = n;
BinaryReader br = new BinaryReader(s);
br.ReadBytes(8);
bool transparency = (br.ReadInt32()!=0);
TintR = br.ReadSingle();
TintG = br.ReadSingle();
TintB = br.ReadSingle();
br.ReadBytes(40);
/*if (Tint != Color4.white() && Tint != Color4.black())
{
Log.Info("Using colormap with tint (" Name ")");
}*/
for (int x = 0; x < 256; x )
{
RGB rgb = new RGB();
rgb.R = br.ReadByte();
rgb.G = br.ReadByte();
rgb.B = br.ReadByte();
Palette[x] = rgb;
}
// maybe this cmp doesnt include anything else?
if (s.Position >= s.Length)
return;
// light table is stored as full palette source color -> dest color
// for each light level
byte[][] lightTables = new byte[64][];
for (int x = 0; x < 64; x )
lightTables[x] = br.ReadBytes(256);
// for now lets just see if lightlevel 0 and 63 are the same and store that as a flag in the palette
for (int x = 0; x < 256; x )
{
// dont make black pixels self illuminated
if (Palette[x].R == 0 && Palette[x].G == 0 && Palette[x].B == 0)
continue;
Palette[x].SelfIlluminated = (lightTables[0][x] == lightTables[63][x]);
if (Palette[x].SelfIlluminated)
{
//x = x;
}
}
// probably should care about lighting data but i dunno how to use lol
//br.ReadBytes(16384);
#if false
if (transparency)
{
byte[][] transtable = new byte[256][];
// care about transparency table?
for (int x = 0; x < 256; x )
{
transtable[x] = br.ReadBytes(256);
}
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(258, 258, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmp.SetPixel(0, 0, System.Drawing.Color.White);
bmp.SetPixel(1, 0, System.Drawing.Color.White);
bmp.SetPixel(0, 1, System.Drawing.Color.White);
bmp.SetPixel(1, 1, System.Drawing.Color.White);
for(int y=0; y<256; y )
for (int x = 0; x < 256; x )
{
byte b = transtable[y][x];
RGB rgb = Palette[b];
bmp.SetPixel(x 2, y 2, System.Drawing.Color.FromArgb(rgb.R, rgb.G, rgb.B));
}
// what in the hell is this
bmp.Save(@"c:\smith\tmp\transtable.bmp");
}
#endif
}
}
}