Repository navigation
Expand file tree
/
Copy pathraster_colorizer.cpp
More file actions
320 lines (274 loc) · 9.63 KB
/
Copy pathraster_colorizer.cpp
File metadata and controls
320 lines (274 loc) · 9.63 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// mapnik
#include <mapnik/debug.hpp>
#include <mapnik/value_types.hpp>
#include <mapnik/value.hpp> // for to_double
#include <mapnik/feature.hpp>
#include <mapnik/raster.hpp>
#include <mapnik/raster_colorizer.hpp>
#include <mapnik/enumeration.hpp>
// stl
#include <limits>
#include <cmath>
namespace mapnik
{
//! \brief Strings for the colorizer_mode enumeration
static const char *colorizer_mode_strings[] = {
"inherit",
"linear",
"discrete",
"exact",
""
};
IMPLEMENT_ENUM( colorizer_mode, colorizer_mode_strings )
colorizer_stop::colorizer_stop(float value, colorizer_mode mode,
color const& _color,
std::string const& label)
: value_(value)
, mode_(mode)
, color_(_color)
, label_(label)
{
}
colorizer_stop::colorizer_stop(colorizer_stop const& stop)
: value_(stop.value_)
, mode_(stop.mode_)
, color_(stop.color_)
, label_(stop.label_)
{
}
colorizer_stop::~colorizer_stop()
{
}
bool colorizer_stop::operator==(colorizer_stop const& other) const
{
return (value_ == other.value_) &&
(color_ == other.color_) &&
(mode_ == other.mode_) &&
(label_ == other.label_);
}
std::string colorizer_stop::to_string() const
{
std::stringstream ss;
ss << color_.to_string() << " " << value_ << " " << mode_.as_string();
return ss.str();
}
raster_colorizer::raster_colorizer(colorizer_mode mode, color const& _color)
: default_mode_(mode)
, default_color_(_color)
, epsilon_(std::numeric_limits<float>::epsilon())
{
}
raster_colorizer::~raster_colorizer()
{
}
bool raster_colorizer::add_stop(colorizer_stop const& stop)
{
//make sure stops are added in order of value
if(stops_.size())
{
if(stop.get_value() <= stops_.back().get_value())
{
return false;
}
}
stops_.push_back(stop);
return true;
}
template <typename T>
void raster_colorizer::colorize(image_rgba8 & out, T const& in,
boost::optional<double> const& nodata,
feature_impl const& f) const
{
using image_type = T;
using pixel_type = typename image_type::pixel_type;
// TODO: assuming in/out have the same width/height for now
std::uint32_t * out_data = out.data();
pixel_type const* in_data = in.data();
int len = out.width() * out.height();
for (int i=0; i<len; ++i)
{
pixel_type value = in_data[i];
if (nodata && (std::fabs(value - *nodata) < epsilon_))
{
out_data[i] = 0; // rgba(0,0,0,0)
}
else
{
out_data[i] = get_color(value);
}
}
}
inline unsigned interpolate(unsigned start, unsigned end, float fraction)
{
return static_cast<unsigned>(fraction * ((float)end - (float)start) + start);
}
unsigned raster_colorizer::get_color(float value) const
{
int stopCount = stops_.size();
//use default color if no stops
if (stopCount == 0)
{
return default_color_.rgba();
}
//1 - Find the stop that the value is in
int stopIdx = -1;
bool foundStopIdx = false;
for(int i=0; i<stopCount; ++i)
{
if(value < stops_[i].get_value())
{
stopIdx = i-1;
foundStopIdx = true;
break;
}
}
if(!foundStopIdx)
{
stopIdx = stopCount-1;
}
//2 - Find the next stop
int nextStopIdx = stopIdx + 1;
if(nextStopIdx >= stopCount)
{
//there is no next stop
nextStopIdx = stopCount - 1;
}
//3 - Work out the mode
colorizer_mode stopMode;
if( stopIdx == -1 )
{
//before the first stop
stopMode = default_mode_;
}
else
{
stopMode = stops_[stopIdx].get_mode();
if(stopMode == COLORIZER_INHERIT)
{
stopMode = default_mode_;
}
}
//4 - Calculate the colour
color stopColor;
color nextStopColor;
float stopValue = 0;
float nextStopValue = 0;
color outputColor = get_default_color();
if(stopIdx == -1)
{
stopColor = default_color_;
nextStopColor = stops_[nextStopIdx].get_color();
stopValue = value;
nextStopValue = stops_[nextStopIdx].get_value();
}
else
{
stopColor = stops_[stopIdx].get_color();
nextStopColor = stops_[nextStopIdx].get_color();
stopValue = stops_[stopIdx].get_value();
nextStopValue = stops_[nextStopIdx].get_value();
}
switch(stopMode)
{
case COLORIZER_LINEAR:
{
//deal with this separately so we don't have to worry about div0
if(nextStopValue == stopValue)
{
outputColor = stopColor;
}
else
{
float fraction = (value - stopValue) / (nextStopValue - stopValue);
unsigned r = interpolate(stopColor.red(), nextStopColor.red(),fraction);
unsigned g = interpolate(stopColor.green(), nextStopColor.green(),fraction);
unsigned b = interpolate(stopColor.blue(), nextStopColor.blue(),fraction);
unsigned a = interpolate(stopColor.alpha(), nextStopColor.alpha(),fraction);
outputColor.set_red(r);
outputColor.set_green(g);
outputColor.set_blue(b);
outputColor.set_alpha(a);
}
}
break;
case COLORIZER_DISCRETE:
outputColor = stopColor;
break;
case COLORIZER_EXACT:
default:
//approximately equal (within epsilon)
if(std::fabs(value - stopValue) < epsilon_)
{
outputColor = stopColor;
}
else
{
outputColor = default_color_;
}
break;
}
/*
MAPNIK_LOG_DEBUG(raster_colorizer) << "raster_colorizer: get_color " << value;
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tstopIdx: " << stopIdx;
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tnextStopIdx: " << nextStopIdx;
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tstopValue: " << stopValue;
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tnextStopValue: " << nextStopValue;
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tstopColor: " << stopColor.to_string();
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tnextStopColor: " << nextStopColor.to_string();
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tstopMode: " << stopMode.as_string();
MAPNIK_LOG_DEBUG(raster_colorizer) << "\toutputColor: " << outputColor.to_string();
*/
return outputColor.rgba();
}
template void raster_colorizer::colorize(image_rgba8 & out, image_gray8 const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
template void raster_colorizer::colorize(image_rgba8 & out, image_gray8s const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
template void raster_colorizer::colorize(image_rgba8 & out, image_gray16 const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
template void raster_colorizer::colorize(image_rgba8 & out, image_gray16s const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
template void raster_colorizer::colorize(image_rgba8 & out, image_gray32 const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
template void raster_colorizer::colorize(image_rgba8 & out, image_gray32s const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
template void raster_colorizer::colorize(image_rgba8 & out, image_gray32f const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
template void raster_colorizer::colorize(image_rgba8 & out, image_gray64 const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
template void raster_colorizer::colorize(image_rgba8 & out, image_gray64s const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
template void raster_colorizer::colorize(image_rgba8 & out, image_gray64f const& in,
boost::optional<double>const& nodata,
feature_impl const& f) const;
}