File: ColorMap.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (223 lines) | stat: -rw-r--r-- 5,213 bytes parent folder | download | duplicates (3)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <array>
#include <cstring> // memcpy

#include "ColorMap.h"
#include "Bitmap.h"
#include "System/Log/ILog.h"
#include "System/Exceptions.h"
#include "System/StringUtil.h"
#include "System/UnorderedMap.hpp"
#include "System/creg/STL_Map.h"

CR_BIND(CColorMap, )
CR_REG_METADATA(CColorMap, (
	CR_MEMBER(xsize),
	CR_IGNORED(nxsize),
	CR_MEMBER(ysize),
	CR_IGNORED(map),

	CR_SERIALIZER(Serialize),
	CR_POSTLOAD(PostLoad)
))


static std::array<CColorMap, 2048 + 2> colorMapsCache;
static spring::unordered_map<std::string, CColorMap*> namedColorMaps;

static size_t numColorMaps = 0;


void CColorMap::InitStatic()
{
	namedColorMaps.clear();
	namedColorMaps.reserve(colorMapsCache.size() - 2);

	// reuse inner ColorMap vectors when reloading
	// colorMapsCache.fill({});

	for (CColorMap& cm: colorMapsCache) {
		cm.Clear();
	}

	numColorMaps = 0;
}

CColorMap* CColorMap::LoadFromBitmapFile(const std::string& fileName)
{
	const auto fn = StringToLower(fileName);
	const auto it = namedColorMaps.find(fn);

	if (it != namedColorMaps.end())
		return it->second;

	// hand out a dummy if cache is full
	if (numColorMaps >= (colorMapsCache.size() - 2))
		return &colorMapsCache[colorMapsCache.size() - 2];

	colorMapsCache[numColorMaps] = {fileName};
	namedColorMaps[fn] = &colorMapsCache[numColorMaps];

	return &colorMapsCache[numColorMaps++];
}

CColorMap* CColorMap::LoadFromRawVector(const float* data, size_t size)
{
	CColorMap& cm = colorMapsCache[colorMapsCache.size() - 1];

	cm.Clear();
	cm.Load(data, size);

	// slowish, but gets invoked by /reloadcegs via LoadFromDefString callback
	// need to do a cache lookup or numColorMaps quickly spirals out of control
	for (size_t i = 0; i < numColorMaps; i++) {
		if (colorMapsCache[i].map.size() != cm.map.size())
			continue;

		if (memcmp(colorMapsCache[i].map.data(), cm.map.data(), cm.map.size() * sizeof(SColor)) == 0)
			return &colorMapsCache[i];
	}

	// ditto
	if (numColorMaps >= (colorMapsCache.size() - 2))
		return &colorMapsCache[colorMapsCache.size() - 2];

	colorMapsCache[numColorMaps].Clear();
	colorMapsCache[numColorMaps].Load(data, size);

	return &colorMapsCache[numColorMaps++];
}


CColorMap* CColorMap::LoadFromDefString(const std::string& defString)
{
	std::array<float, 4096> vec;

	size_t idx = 0;

	char* pos = const_cast<char*>(defString.c_str());
	char* end = nullptr;

	vec.fill(0.0f);

	for (float val; (val = std::strtof(pos, &end), pos != end && idx < vec.size()); pos = end) {
		vec[idx++] = val;
	}

	if (idx == 0)
		return (CColorMap::LoadFromBitmapFile("bitmaps\\" + defString));

	return (CColorMap::LoadFromRawVector(vec.data(), idx));
}



CColorMap::CColorMap(const std::string& fileName)
{
	CBitmap bitmap;

	if (!bitmap.Load(fileName)) {
		bitmap.Alloc(2, 2, 4);
		LOG_L(L_WARNING, "[ColorMap] could not load texture from file \"%s\"", fileName.c_str());
	}

	if (bitmap.compressed || (bitmap.channels != 4) || (bitmap.xsize < 2))
		throw content_error("[ColorMap] unsupported bitmap format in file " + fileName);

	xsize  = bitmap.xsize;
	ysize  = bitmap.ysize;
	nxsize = xsize - 1;

	LoadMap(bitmap.GetRawMem(), xsize * ysize);
}


void CColorMap::Load(const float* data, size_t size)
{
	if (size < 8)
		throw content_error("[ColorMap] less than two RGBA colors specified");

	xsize  = (size - (size % 4)) / 4;
	ysize  = 1;
	nxsize = xsize - 1;

	std::array<SColor, 4096> cmap;

	for (size_t i = 0, n = std::min(size_t(xsize), cmap.size()); i < n; ++i) {
		cmap[i] = SColor(&data[i * 4]);
	}

	LoadMap(&cmap[0].r, xsize);
}

void CColorMap::LoadMap(const unsigned char* buf, int num)
{
	map.clear();
	map.resize(num);

	std::memcpy(&map[0], buf, num * 4);
}

void CColorMap::GetColor(unsigned char* color, float pos)
{
	if (map.empty()) {
		// dummy map, just return grey
		color[0] = 128;
		color[1] = 128;
		color[2] = 128;
		color[3] = 255;
		return;
	}

	if (pos >= 1.0f) {
		*reinterpret_cast<SColor*>(color) = map.back();
		return;
	}

	const float fposn = pos * nxsize;
	const int iposn = (int) fposn;
	const float fracn = fposn - iposn;
	const int aa = (int) (fracn * 256);
	const int ia = 256 - aa;

	unsigned char* col1 = (unsigned char*) &map[iposn    ];
	unsigned char* col2 = (unsigned char*) &map[iposn + 1];

	color[0] = ((col1[0] * ia) + (col2[0] * aa)) >> 8;
	color[1] = ((col1[1] * ia) + (col2[1] * aa)) >> 8;
	color[2] = ((col1[2] * ia) + (col2[2] * aa)) >> 8;
	color[3] = ((col1[3] * ia) + (col2[3] * aa)) >> 8;
}

#ifdef USING_CREG
void CColorMap::SerializeColorMaps(creg::ISerializer* s)
{
	if (!s->IsWriting()) {
		for (CColorMap& cm: colorMapsCache) {
			cm.Clear();
		}
	}

	s->SerializeInt(&numColorMaps, sizeof(numColorMaps));
	for (size_t i = 0; i < numColorMaps; ++i) {
		s->SerializeObjectInstance(&colorMapsCache[i], CColorMap::StaticClass());
	}

	std::unique_ptr<creg::IType> mapType = creg::DeduceType<decltype(namedColorMaps)>::Get();
	mapType->Serialize(s, &namedColorMaps);
}

void CColorMap::PostLoad()
{
	nxsize = xsize - 1;
}

void CColorMap::Serialize(creg::ISerializer* s)
{
	if (!s->IsWriting())
		map.resize(xsize * ysize);

	s->Serialize(&map[0].r, xsize * ysize * 4);
}
#endif