File: ColorMap.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (211 lines) | stat: -rw-r--r-- 4,237 bytes parent folder | download
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
#include "StdAfx.h"

#include <sstream>
#include <cstring>

#include "mmgr.h"

#include "ColorMap.h"
#include "Bitmap.h"
#include "Bitmap.h"
#include "FileSystem/FileHandler.h"
#include "Util.h"
#include "Exceptions.h"

std::vector<CColorMap *> CColorMap::colorMaps;
std::map<std::string, CColorMap *> CColorMap::colorMapsMap;

CR_BIND(CColorMap, );

CColorMap::CColorMap(void)
{
	map = 0;
}

CColorMap::CColorMap(std::vector<float> &vec)
{
	map=0;

	if(vec.size()<8) //needs at least two colors
		throw content_error("Too few colors in colormap.");

	unsigned char *lmap = new unsigned char[vec.size() - vec.size()%4];
	xsize = (vec.size() - vec.size()%4)/4;
	ysize = 1;
	nxsize = xsize-1;
	nysize = ysize-1;

	for(int i=0; i<xsize*4; i++)
	{
		lmap[i] = int(vec[i]*255);
	}
	LoadMap(lmap, xsize*4);
	delete [] lmap;
}

CColorMap::CColorMap(std::string filename)
{
	map=0;

	CBitmap bitmap;

	if (!bitmap.Load(filename))
		throw content_error("Could not load texture from file " + filename);

	if(bitmap.type != CBitmap::BitmapTypeStandardRGBA || (bitmap.xsize<2))
		throw content_error("Unsupported bitmap format in file " + filename);

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

	LoadMap(bitmap.mem, xsize*ysize*4);
}

CColorMap::CColorMap(const unsigned char *buf, int num)
{
	map=0;
	xsize = num/4;
	ysize = 1;
	LoadMap(buf, num);
	nxsize = xsize-1;
	nysize = ysize-1;
}

CColorMap::~CColorMap(void)
{
	delete [] map;
}

void CColorMap::DeleteColormaps()
{
	for(size_t i=0; i<colorMaps.size(); i++)
	{
		delete colorMaps[i];
	}
	colorMaps.clear();
}

void CColorMap::LoadMap(const unsigned char *buf, int num)
{
	delete [] map;

	map = new unsigned char[num];
	std::memcpy(map, buf, num);
}

CColorMap* CColorMap::LoadFromBitmapFile(std::string filename)
{
	std::string lowfilename = filename;
	StringToLowerInPlace(lowfilename);
	CColorMap *map;
	if(colorMapsMap.find(lowfilename)==colorMapsMap.end())
	{
		map = new CColorMap(filename);
		colorMapsMap[lowfilename] = map;
		colorMaps.push_back(map);
	}
	else
	{
		map = colorMapsMap.find(lowfilename)->second;
	}
	return map;
}

CColorMap* CColorMap::LoadFromFloatVector(std::vector<float> &vec)
{
	CColorMap *map = new CColorMap(vec);
	colorMaps.push_back(map);
	return map;
}

CColorMap* CColorMap::LoadFromFloatString(std::string fstring)
{
	std::stringstream stream;
	stream << fstring;
	std::vector<float> vec;

	float value;
	while(stream >> value)
	{
		vec.push_back(value);
	}

	return CColorMap::LoadFromFloatVector(vec);
}

CColorMap* CColorMap::LoadFromDefString(std::string dstring)
{
	std::stringstream stream;
	stream << dstring;
	std::vector<float> vec;

	float value;
	while(stream >> value)
	{
		vec.push_back(value);
	}

	if(vec.size()>0)
		return CColorMap::LoadFromFloatVector(vec);
	else
		return CColorMap::LoadFromBitmapFile("bitmaps\\" + dstring);
}

CColorMap* CColorMap::Load8f(float r1,float g1,float b1,float a1,float r2,float g2,float b2,float a2)
{
	std::vector<float> vec;
	vec.push_back(r1);
	vec.push_back(g1);
	vec.push_back(b1);
	vec.push_back(a1);
	vec.push_back(r2);
	vec.push_back(g2);
	vec.push_back(b2);
	vec.push_back(a2);
	return CColorMap::LoadFromFloatVector(vec);
}

CColorMap* CColorMap::Load12f(float r1,float g1,float b1,float a1,float r2,float g2,float b2,float a2,float r3,float g3,float b3,float a3)
{
	std::vector<float> vec;
	vec.push_back(r1);
	vec.push_back(g1);
	vec.push_back(b1);
	vec.push_back(a1);
	vec.push_back(r2);
	vec.push_back(g2);
	vec.push_back(b2);
	vec.push_back(a2);
	vec.push_back(r3);
	vec.push_back(g3);
	vec.push_back(b3);
	vec.push_back(a3);
	return CColorMap::LoadFromFloatVector(vec);
}

unsigned char* CColorMap::GetColor(unsigned char *color, float pos)
{
	if(pos>=1.0f)
	{
		*((int*)color)=(((int*)map)[nxsize]);
		return color;
	}

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

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

	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;

	return color;
}