The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
image_modifications.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Ignacio R. Morelle <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /** @file */
16 
17 #ifndef IMAGE_MODIFICATIONS_HPP_INCLUDED
18 #define IMAGE_MODIFICATIONS_HPP_INCLUDED
19 
21 #include "sdl/utils.hpp"
22 #include <queue>
23 
24 namespace image {
25 
26 class modification;
27 
28 
29 /// A modified priority queue used to order image modifications.
30 /// The priorities for this queue are to order modifications by priority(),
31 /// then by the order they are added to the queue.
33  // Invariant for this class:
34  // At the beginning and end of each member function call, there
35  // are no empty vectors in priorities_.
36 public:
38  : priorities_()
39  {
40  }
41 
43 
44  bool empty() const { return priorities_.empty(); }
45  void push(modification * mod);
46  void pop();
47  size_t size() const;
48  modification * top() const;
49 
50 private: // data
51  /// Map from a mod's priority() to the mods having that priority.
52  typedef std::map<int, std::vector<modification *>, std::greater<int> > map_type;
53  /// Map from a mod's priority() to the mods having that priority.
54  map_type priorities_;
55 };
56 
57 
58 /// Base abstract class for an image-path modification
60 {
61 public:
62 
63  /** Exception thrown by the operator() when an error occurs. */
64  struct texception
66  {
67  /**
68  * Constructor.
69  *
70  * @pre message_stream.str()[message_stream.str().size() - 1] == '\n'
71  *
72  * @param message_stream Stream with the error message regarding
73  * the failed operation.
74  */
75  texception(const std::stringstream& message_stream);
76 
77  /**
78  * Constructor.
79  *
80  * @pre message[message.size() - 1] == '\n'
81  *
82  * @param message String with the error message regarding
83  * the failed operation.
84  */
86 
87  ~texception() throw() {}
88 
89  /** The error message regarding the failed operation. */
91 
92  private:
93 
95  };
96 
97  /// Decodes modifications from a modification string
98  static modification_queue decode(const std::string&);
99 
100  virtual ~modification() {}
101 
102  ///Applies the image-path modification on the specified surface
103  virtual surface operator()(const surface& src) const = 0;
104 
105  /// Specifies the priority of the modification
106  virtual int priority() const { return 0; }
107 };
108 
109 /**
110  * Recolor (RC/TC/PAL) modification.
111  * It is used not only for color-range-based recoloring ("~RC(magenta>teal)")
112  * but also for team-color-based color range selection and recoloring
113  * ("~TC(3,magenta)") and palette switches ("~PAL(000000,005000 > FFFFFF,FF00FF)").
114  */
116 {
117 public:
118  /**
119  * Default constructor.
120  */
122  : rc_map_()
123  {}
124  /**
125  * RC-map based constructor.
126  * @param recolor_map The palette switch map.
127  */
128  rc_modification(const std::map<Uint32, Uint32>& recolor_map)
129  : rc_map_(recolor_map)
130  {}
131  virtual surface operator()(const surface& src) const;
132 
133  // The rc modification has a higher priority
134  virtual int priority() const { return 1; }
135 
136  bool no_op() const { return rc_map_.empty(); }
137 
138  const std::map<Uint32, Uint32>& map() const { return rc_map_;}
139  std::map<Uint32, Uint32>& map() { return rc_map_;}
140 
141 private:
142  std::map<Uint32, Uint32> rc_map_;
143 };
144 
145 /**
146  * Mirror (FL) modification.
147  */
149 {
150 public:
151  /**
152  * Constructor.
153  * @param horiz Horizontal mirror flag.
154  * @param vert Vertical mirror flag.
155  */
156  fl_modification(bool horiz = false, bool vert = false)
157  : horiz_(horiz)
158  , vert_(vert)
159  {}
160  virtual surface operator()(const surface& src) const;
161 
162  void set_horiz(bool val) { horiz_ = val; }
163  void set_vert(bool val) { vert_ = val; }
164  bool get_horiz() const { return horiz_; }
165  bool get_vert() const { return vert_; }
166  /** Toggle horizontal mirror flag.
167  * @return The new flag state after toggling. */
168  bool toggle_horiz() { return((horiz_ = !horiz_)); }
169  /** Toggle vertical mirror flag.
170  * @return The new flag state after toggling. */
171  bool toggle_vert() { return((vert_ = !vert_)); }
172 
173  bool no_op() const { return ((!horiz_) && (!vert_)); }
174 
175 private:
176  bool horiz_;
177  bool vert_;
178 };
179 
180 /**
181  * Rotate (ROTATE) modification.
182  */
184 {
185 public:
186  /**
187  * Constructor.
188  *
189  * @pre zoom >= offset Otherwise the result will have empty pixels.
190  * @pre offset > 0 Otherwise the procedure will not return.
191  *
192  * @param degrees Amount of rotation (in degrees).
193  * Positive values are clockwise; negative are counter-clockwise.
194  * @param zoom The zoom level to calculate the rotation from.
195  * Greater values result in better results and increased runtime.
196  * This parameter will be ignored if @a degrees is a multiple of 90.
197  * @param offset Determines the step size of the scanning of the zoomed source.
198  * Different offsets can produce better results, try them out.
199  * Greater values result in decreased runtime.
200  * This parameter will be ignored if @a degrees is a multiple of 90.
201  * If @a offset is greater than @a zoom the result will have empty pixels.
202  */
203  rotate_modification(int degrees = 90, int zoom = 16, int offset = 8)
204  : degrees_(degrees), zoom_(zoom), offset_(offset)
205  {}
206  virtual surface operator()(const surface& src) const;
207 
208  bool no_op() const { return degrees_ % 360 == 0; }
209 
210 private:
211  int degrees_;
212  int zoom_;
213  int offset_;
214 };
215 
216 /**
217  * Grayscale (GS) modification.
218  */
220 {
221 public:
222  virtual surface operator()(const surface& src) const;
223 };
224 
225 /**
226  * Black and white (BW) modification.
227  */
229 {
230 public:
231  bw_modification(int threshold): threshold_(threshold) {}
232  virtual surface operator()(const surface& src) const;
233 private:
235 };
236 
237 /**
238  * Give to the image a sepia tint (SEPIA)
239  */
241 {
242  virtual surface operator()(const surface &src) const;
243 };
244 
245 /**
246  * Make an image negative (NEG)
247  */
249 {
250  public:
251  negative_modification(int r, int g, int b): red_(r), green_(g), blue_(b) {}
252  virtual surface operator()(const surface &src) const;
253  private:
255 };
256 
257 /**
258  * Plot Alpha (Alpha) modification
259  */
261 {
262 public:
263  virtual surface operator()(const surface& src) const;
264 };
265 
266 /**
267  * Wipe Alpha (Wipe_Alpha) modification
268  */
270 {
271 public:
272  virtual surface operator()(const surface& src) const;
273 };
274 
275 /**
276  * Adjust Alpha (ADJUST_ALPHA) modification
277  */
279 {
280 public:
282  : amount_(amount)
283  {}
284 
285  virtual surface operator()(const surface& src) const;
286 
287 private:
289 };
290 
291 /**
292  * Crop (CROP) modification.
293  */
295 {
296 public:
297  crop_modification(const SDL_Rect& slice)
298  : slice_(slice)
299  {}
300  virtual surface operator()(const surface& src) const;
301 
302  const SDL_Rect& get_slice() const;
303 
304 private:
305  SDL_Rect slice_;
306 };
307 
308 /**
309  * Scale (BLIT) modification.
310  */
311 
313 {
314 public:
315  blit_modification(const surface& surf, int x, int y)
316  : surf_(surf), x_(x), y_(y)
317  {}
318  virtual surface operator()(const surface& src) const;
319 
320  const surface& get_surface() const;
321  int get_x() const;
322  int get_y() const;
323 
324 private:
326  int x_;
327  int y_;
328 };
329 
330 /**
331  * Mask (MASK) modification.
332  */
333 
335 {
336 public:
337  mask_modification(const surface& mask, int x, int y)
338  : mask_(mask), x_(x), y_(y)
339  {}
340  virtual surface operator()(const surface& src) const;
341 
342  const surface& get_mask() const;
343  int get_x() const;
344  int get_y() const;
345 
346 private:
348  int x_;
349  int y_;
350 };
351 
352 /**
353  * LIGHT (L) modification.
354  */
355 
357 {
358 public:
360  : surf_(surf)
361  {}
362  virtual surface operator()(const surface& src) const;
363 
364  const surface& get_surface() const;
365 
366 private:
368 };
369 
370 /**
371  * Scaling modifications base class.
372  */
374 {
375 public:
376  scale_modification(int width, int height, std::string fn, bool use_nn)
377  : w_(width), h_(height), nn_(use_nn), fn_(fn)
378  {}
379  virtual surface operator()(const surface& src) const;
380  virtual std::pair<int,int> calculate_size(const surface& src) const = 0;
381  int get_w() const;
382  int get_h() const;
383 
384 private:
385  int w_, h_;
386  bool nn_;
387 protected:
389 };
390 
391 /**
392  * Scale exact modification. (SCALE, SCALE_SHARP)
393  */
395 {
396 public:
398  : scale_modification(width, height, fn, use_nn)
399  {}
400  virtual std::pair<int,int> calculate_size(const surface& src) const;
401 };
402 
403 /**
404  * Scale into (SCALE_INTO) modification. (SCALE_INTO, SCALE_INTO_SHARP)
405  * Preserves aspect ratio.
406  */
408 {
409 public:
410  scale_into_modification(int width, int height, std::string fn, bool use_nn)
411  : scale_modification(width, height, fn, use_nn)
412  {}
413  virtual std::pair<int,int> calculate_size(const surface& src) const;
414 };
415 
416 /**
417  * xBRZ scale (xBRZ) modification
418  */
420 {
421 public:
423  : z_(z)
424  {}
425 
426  virtual surface operator()(const surface& src) const;
427 
428 private:
429  int z_;
430 };
431 
432 /**
433  * Opacity (O) modification
434  */
436 {
437 public:
438  o_modification(float opacity)
439  : opacity_(opacity)
440  {}
441  virtual surface operator()(const surface& src) const;
442  float get_opacity() const;
443 
444 private:
445  float opacity_;
446 };
447 
448 /**
449  * Color-shift (CS, R, G, B) modification.
450  */
452 {
453 public:
454  cs_modification(int r, int g, int b)
455  : r_(r), g_(g), b_(b)
456  {}
457  virtual surface operator()(const surface& src) const;
458  int get_r() const;
459  int get_g() const;
460  int get_b() const;
461 
462 private:
463  int r_, g_, b_;
464 };
465 
466 /**
467  * Color blending (BLEND) modification
468  */
470 {
471 public:
472  blend_modification(int r, int g, int b, float a)
473  : r_(r), g_(g), b_(b), a_(a)
474  {}
475  virtual surface operator()(const surface& src) const;
476  int get_r() const;
477  int get_g() const;
478  int get_b() const;
479  float get_a() const;
480 
481 private:
482  int r_, g_, b_;
483  float a_;
484 };
485 
486 /**
487  * Gaussian-like blur (BL) modification.
488  */
490 {
491 public:
493  : depth_(depth)
494  {}
495  virtual surface operator()(const surface& src) const;
496  int get_depth() const;
497 
498 private:
499  int depth_;
500 };
501 
502 /**
503  * Overlay with ToD brightening (BRIGHTEN).
504  */
506 {
507  virtual surface operator()(const surface &src) const;
508 };
509 
510 /**
511  * Overlay with ToD darkening (DARKEN).
512  */
514 {
515  virtual surface operator()(const surface &src) const;
516 };
517 
518 /**
519  * Fill background with a color (BG).
520  */
522 {
523  background_modification(SDL_Color const &c): color_(c) {}
524  virtual surface operator()(const surface &src) const;
525  const SDL_Color& get_color() const;
526 
527 private:
528  SDL_Color color_;
529 };
530 
531 /**
532  * Channel swap (SWAP).
533  */
535 {
536 public:
538  virtual surface operator()(const surface& src) const;
539 private:
544 };
545 
546 } /* end namespace image */
547 
548 #endif /* !defined(IMAGE_MODIFICATIONS_HPP_INCLUDED) */
Grayscale (GS) modification.
Scale (BLIT) modification.
GLdouble GLdouble z
Definition: glew.h:1527
blend_modification(int r, int g, int b, float a)
Fill background with a color (BG).
const std::map< Uint32, Uint32 > & map() const
mask_modification(const surface &mask, int x, int y)
Gaussian-like blur (BL) modification.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glew.h:1222
Color-shift (CS, R, G, B) modification.
#define IMPLEMENT_LUA_JAILBREAK_EXCEPTION(type)
Helper macro for classes deriving from tlua_jailbreak_exception.
const GLfloat * c
Definition: glew.h:12741
Adjust Alpha (ADJUST_ALPHA) modification.
Black and white (BW) modification.
const surface & get_mask() const
Sint32 fixed_t
Definition: drawer.hpp:40
A modified priority queue used to order image modifications.
Overlay with ToD darkening (DARKEN).
GLuint const GLfloat * val
Definition: glew.h:2614
Plot Alpha (Alpha) modification.
rc_modification()
Default constructor.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
GLboolean GLboolean g
Definition: glew.h:7319
std::map< Uint32, Uint32 > & map()
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
GLenum src
Definition: glew.h:2392
xBRZ scale (xBRZ) modification
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
Mask (MASK) modification.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
Recolor (RC/TC/PAL) modification.
const surface & get_surface() const
Make an image negative (NEG)
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
virtual int priority() const
Specifies the priority of the modification.
void push(modification *mod)
Adds mod to the queue (unless mod is nullptr).
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
GLintptr offset
Definition: glew.h:1650
bool toggle_vert()
Toggle vertical mirror flag.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
cs_modification(int r, int g, int b)
virtual std::pair< int, int > calculate_size(const surface &src) const
std::map< int, std::vector< modification * >, std::greater< int > > map_type
Map from a mod's priority() to the mods having that priority.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
const surface & get_surface() const
negative_modification(int r, int g, int b)
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
Crop (CROP) modification.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
virtual std::pair< int, int > calculate_size(const surface &src) const =0
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
Color blending (BLEND) modification.
virtual int priority() const
Specifies the priority of the modification.
Overlay with ToD brightening (BRIGHTEN).
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
GLenum GLint GLuint mask
Definition: glew.h:1813
Base abstract class for an image-path modification.
std::map< Uint32, Uint32 > rc_map_
background_modification(SDL_Color const &c)
surf
Definition: filter.cpp:143
const std::string &parameters float amount
Definition: filter.cpp:132
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
static modification_queue decode(const std::string &)
Decodes modifications from a modification string.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
Give to the image a sepia tint (SEPIA)
rc_modification(const std::map< Uint32, Uint32 > &recolor_map)
RC-map based constructor.
Rotate (ROTATE) modification.
LIGHT (L) modification.
modification * top() const
Returns the top element in the queue .
Base class for exceptions that want to be thrown 'through' lua.
scale_into_modification(int width, int height, std::string fn, bool use_nn)
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
light_modification(const surface &surf)
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
size_t size() const
Returns the number of elements in the queue.
Opacity (O) modification.
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
Wipe Alpha (Wipe_Alpha) modification.
blit_modification(const surface &surf, int x, int y)
const std::string message
The error message regarding the failed operation.
scale_exact_modification(int width, int height, std::string fn, bool use_nn)
scale_modification(int width, int height, std::string fn, bool use_nn)
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
Exception thrown by the operator() when an error occurs.
bool toggle_horiz()
Toggle horizontal mirror flag.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
this module manages the cache of images.
Definition: image.cpp:75
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
virtual surface operator()(const surface &src) const =0
Applies the image-path modification on the specified surface.
virtual std::pair< int, int > calculate_size(const surface &src) const
map_type priorities_
Map from a mod's priority() to the mods having that priority.
Scale into (SCALE_INTO) modification.
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
const SDL_Color & get_color() const
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
rotate_modification(int degrees=90, int zoom=16, int offset=8)
Constructor.
texception(const std::stringstream &message_stream)
Constructor.
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
void pop()
Removes the top element from the queue.
swap_modification(channel r, channel g, channel b, channel a)
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
const SDL_Rect & get_slice() const
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual surface operator()(const surface &src) const
Applies the image-path modification on the specified surface.
Mirror (FL) modification.
fl_modification(bool horiz=false, bool vert=false)
Constructor.
Scaling modifications base class.
crop_modification(const SDL_Rect &slice)
channel
Definition: utils.hpp:250