-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
filter.cpp
249 lines (210 loc) · 6.48 KB
/
filter.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
/*
Copyright (C) 2012 - 2014 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "wesmage/filter.hpp"
#include "serialization/string_utils.hpp"
#include "wesmage/exit.hpp"
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/function.hpp>
#include <iostream>
/** Contains the definition of a filter. */
struct tfilter
{
/**
* The functor to call for the filter.
*
* @p surf The surface to apply the filter to.
* @p parameters A string with the parameters for the
* functor. The code is expected to be
* supplied on the command line. So it should
* be validated.
*/
typedef boost::function<void(
surface& surf
, const std::string& parameters
)>
tfunctor;
tfilter(
const std::string& name__
, const std::string& description__
, const tfunctor& functor__)
: name(name__)
, description(description__)
, functor(functor__)
{
}
/**
* The name of the filter.
*
* This value must be unique.
*/
std::string name;
/**
* Description of the filter.
*
* The exact format of the string is documented at @ref filter_list.
*/
tfilter_description description;
/** The functor to call for the filter. */
tfunctor functor;
};
/**
* The list of the available filters.
*
* The map contains:
* * @p first The name of the filter.
* * @p second The filter itself.
*/
static std::map<std::string, tfilter> filters;
/** Helper structure to register a filter to the @ref filters. */
struct tregister_filter
{
tregister_filter(const std::pair<std::string, tfilter>& filter)
{
filters.insert(filter);
}
};
/**
* Register macro for a filter.
*
* @param name The name of the filter.
* @param description A pipe-symbol separated list with the
* description. The name is automatically
* prefixed, so the item should start with a
* pipe-symbol. When the list is splitted in
* to a vector of string its contents should
* be compatible with the constructor of
* @ref tfilter_description.
*/
#define REGISTER(name, description) \
tregister_filter register_filter_##name(std::make_pair( \
#name \
, tfilter(#name, #name description, boost::bind(name, _1, _2))));
static void
scale(surface& surf, const std::string& parameters)
{
unsigned width, height;
const int count = sscanf(parameters.c_str(), "%u,%u", &width, &height);
if(count != 2) {
std::cerr << "Error: Arguments to scale »"
<< parameters
<< "« are not compatible.\n";
throw texit(EXIT_FAILURE);
}
surf = scale_surface(surf, width, height);
}
REGISTER(scale,
"|Scales the size of an image."
"|new_width"
"|unsigned"
"|The width in pixel of the image after scaling."
"|new_height"
"|unsigned"
"|The height in pixel of the image after scaling.")
static void
brighten(surface& surf, const std::string& parameters)
{
float amount;
const int count = sscanf(parameters.c_str(), "%f", &amount);
if(count != 1) {
std::cerr << "Error: Arguments to brighten »"
<< parameters
<< "« are not compatible.\n";
throw texit(EXIT_FAILURE);
}
surf = brighten_image(surf, amount);
}
REGISTER(brighten,
"|Brightens an image."
"|amount"
"|float"
"|The amount the image should be brightened. The value of the every "
"color channel is multiplied by this value. Value less than zero "
"are set to zero. The alpha channel is not modified.")
static void
blend(surface& surf, const std::string& parameters)
{
float amount;
unsigned color;
const int count = sscanf(parameters.c_str(), "%f,%x", &amount, &color);
if(count != 2) {
std::cerr << "Error: Arguments to blend »"
<< parameters
<< "« are not compatible.\n";
throw texit(EXIT_FAILURE);
}
surf = blend_surface(surf, amount, color);
}
REGISTER(blend,
"|Blends an image with another color."
"|amount"
"|float"
"|The amount every pixel needs to be blended with its original value. "
"The formula is:\n"
"result = amount * color + (1 - amount) * original\n"
"The value needs to be in the range [0, 1]."
"|color"
"|unsigned"
"|The color to blend with. The value should be given as 32-bit "
"hexadecimal value. The first fields should look like AARRGGBB, "
"where AA is the alpha channel, RR is the red channel, GG is the "
"green channel and BB is the blue channel. (Note the alpha channel "
"is ignored.")
void
filter_apply(surface& surf, const std::string& filter)
{
std::vector<std::string> f = utils::split(filter, ':', utils::STRIP_SPACES);
if(f.size() != 2) {
std::cerr << "Error: Filter »"
<< filter
<< "« doesn't contain the expected separator »:«\n";
throw texit(EXIT_FAILURE);
}
std::map<std::string, tfilter>::iterator itor = filters.find(f[0]);
if(itor == filters.end()) {
std::cerr << "Error: Filter »" << f[0] << "« is unknown.\n";
throw texit(EXIT_FAILURE);
}
itor->second.functor(surf, f[1]);
}
tfilter_description::tfilter_description(const std::string& fmt)
: name()
, description()
, parameters()
{
std::vector<std::string> elements(utils::split(fmt, '|'));
/* Validate there is at least a header part. */
assert(elements.size() >= 2);
name = elements[0];
description = elements[1];
/* Validate every parameter indeed has three fields. */
assert((elements.size() - 2) % 3 == 0);
for(size_t i = 2; i < elements.size(); i += 3) {
tfilter_description::tparameter parameter =
{
elements[i]
, elements[i + 1]
, elements[i + 2]
};
parameters.push_back(parameter);
}
}
std::vector<tfilter_description>
filter_list()
{
std::vector<tfilter_description> result;
typedef std::pair<std::string, tfilter> thack;
BOOST_FOREACH(const thack& filter, filters) {
result.push_back(filter.second.description);
}
return result;
}