forked from mapnik/mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette.cpp
253 lines (222 loc) · 6.77 KB
/
palette.cpp
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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 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
*
*****************************************************************************/
#include <mapnik/palette.hpp>
#include <mapnik/config_error.hpp>
// stl
#include <sstream>
#include <iomanip>
#include <iterator>
namespace mapnik
{
rgb::rgb(rgba const& c)
: r(c.r), g(c.g), b(c.b) {}
// ordering by mean(a,r,g,b), a, r, g, b
bool rgba::mean_sort_cmp::operator() (const rgba& x, const rgba& y) const
{
int t1 = (int)x.a + x.r + x.g + x.b;
int t2 = (int)y.a + y.r + y.g + y.b;
if (t1 != t2) return t1 < t2;
// https://github.com/mapnik/mapnik/issues/1087
if (x.a != y.a) return x.a < y.a;
if (x.r != y.r) return x.r < y.r;
if (x.g != y.g) return x.g < y.g;
return x.b < y.b;
}
rgba_palette::rgba_palette(std::string const& pal, palette_type type)
: colors_(0)
{
#ifdef USE_DENSE_HASH_MAP
color_hashmap_.set_empty_key(0);
#endif
parse(pal, type);
}
rgba_palette::rgba_palette()
: colors_(0)
{
#ifdef USE_DENSE_HASH_MAP
color_hashmap_.set_empty_key(0);
#endif
}
const std::vector<rgb>& rgba_palette::palette() const
{
return rgb_pal_;
}
const std::vector<unsigned>& rgba_palette::alphaTable() const
{
return alpha_pal_;
}
bool rgba_palette::valid() const
{
return colors_ > 0;
}
std::string rgba_palette::to_string() const
{
size_t length = rgb_pal_.size();
size_t alphaLength = alpha_pal_.size();
std::ostringstream str("");
str << "[Palette " << length;
if (length == 1)
{
str << " color";
}
else
{
str << " colors";
}
str << std::hex << std::setfill('0');
for (unsigned i = 0; i < length; i++) {
str << " #";
str << std::setw(2) << (unsigned)rgb_pal_[i].r;
str << std::setw(2) << (unsigned)rgb_pal_[i].g;
str << std::setw(2) << (unsigned)rgb_pal_[i].b;
if (i < alphaLength) str << std::setw(2) << alpha_pal_[i];
}
str << "]";
return str.str();
}
// return color index in returned earlier palette
unsigned char rgba_palette::quantize(unsigned val) const
{
unsigned char index = 0;
if (colors_ == 1 || val == 0) return index;
rgba_hash_table::iterator it = color_hashmap_.find(val);
if (it != color_hashmap_.end())
{
index = it->second;
}
else
{
rgba c(val);
int dr, dg, db, da;
int dist, newdist;
// find closest match based on mean of r,g,b,a
std::vector<rgba>::const_iterator pit =
std::lower_bound(sorted_pal_.begin(), sorted_pal_.end(), c, rgba::mean_sort_cmp());
index = std::distance(sorted_pal_.begin(),pit);
if (index == sorted_pal_.size()) index--;
dr = sorted_pal_[index].r - c.r;
dg = sorted_pal_[index].g - c.g;
db = sorted_pal_[index].b - c.b;
da = sorted_pal_[index].a - c.a;
dist = dr*dr + dg*dg + db*db + da*da;
int poz = index;
// search neighbour positions in both directions for better match
for (int i = poz - 1; i >= 0; i--)
{
dr = sorted_pal_[i].r - c.r;
dg = sorted_pal_[i].g - c.g;
db = sorted_pal_[i].b - c.b;
da = sorted_pal_[i].a - c.a;
// stop criteria based on properties of used sorting
if ((dr+db+dg+da) * (dr+db+dg+da) / 4 > dist)
{
break;
}
newdist = dr*dr + dg*dg + db*db + da*da;
if (newdist < dist)
{
index = i;
dist = newdist;
}
}
for (unsigned i = poz + 1; i < sorted_pal_.size(); i++)
{
dr = sorted_pal_[i].r - c.r;
dg = sorted_pal_[i].g - c.g;
db = sorted_pal_[i].b - c.b;
da = sorted_pal_[i].a - c.a;
// stop criteria based on properties of used sorting
if ((dr+db+dg+da) * (dr+db+dg+da) / 4 > dist)
{
break;
}
newdist = dr*dr + dg*dg + db*db + da*da;
if (newdist < dist)
{
index = i;
dist = newdist;
}
}
// Cache found index for the color c into the hashmap.
color_hashmap_[val] = index;
}
return index;
}
void rgba_palette::parse(std::string const& pal, palette_type type)
{
unsigned length = pal.length();
if ((type == PALETTE_RGBA && length % 4 > 0) ||
(type == PALETTE_RGB && length % 3 > 0) ||
(type == PALETTE_ACT && length != 772))
{
throw config_error("invalid palette length");
}
if (type == PALETTE_ACT)
{
length = (pal[768] << 8 | pal[769]) * 3;
}
sorted_pal_.clear();
rgb_pal_.clear();
alpha_pal_.clear();
if (type == PALETTE_RGBA)
{
for (unsigned i = 0; i < length; i += 4)
{
sorted_pal_.push_back(rgba(pal[i], pal[i + 1], pal[i + 2], pal[i + 3]));
}
}
else
{
for (unsigned i = 0; i < length; i += 3)
{
sorted_pal_.push_back(rgba(pal[i], pal[i + 1], pal[i + 2], 0xFF));
}
}
// Make sure we have at least one entry in the palette.
if (sorted_pal_.size() == 0)
{
sorted_pal_.push_back(rgba(0, 0, 0, 0));
}
colors_ = sorted_pal_.size();
#ifdef USE_DENSE_HASH_MAP
color_hashmap_.resize((colors_*2));
#endif
color_hashmap_.clear();
// Sort palette for binary searching in quantization
std::sort(sorted_pal_.begin(), sorted_pal_.end(), rgba::mean_sort_cmp());
// Insert all palette colors into the hashmap and into the palette vectors.
for (unsigned i = 0; i < colors_; i++)
{
rgba c = sorted_pal_[i];
unsigned val = c.r | (c.g << 8) | (c.b << 16) | (c.a << 24);
if (val != 0)
{
color_hashmap_[val] = i;
}
rgb_pal_.push_back(rgb(c));
if (c.a < 0xFF)
{
alpha_pal_.push_back(c.a);
}
}
}
} // namespace mapnik