The Battle for Wesnoth  1.19.16+dev
utils.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "color_range.hpp"
19 #include "color.hpp"
20 #include "sdl/surface.hpp"
21 #include "utils/math.hpp"
22 #include "game_version.hpp"
23 
24 #include <string>
25 
26 struct rect;
27 
28 namespace sdl
29 {
30 
31 /** Returns the runtime SDL version. */
33 
34 /**
35  * Returns true if the runtime SDL version is at or greater than the
36  * specified version, false otherwise.
37  */
38 bool runtime_at_least(uint8_t major, uint8_t minor = 0, uint8_t patch = 0);
39 
40 } // namespace sdl
41 
42 
43 inline void sdl_blit(const surface& src, const SDL_Rect* src_rect, surface& dst, SDL_Rect* dst_rect){
44  // Note: this is incorrect when both src and dst combine transparent pixels.
45  // The correct equation is, per-pixel:
46  // outA = srcA + dstA * (1 - srcA)
47  // outRGB = (srcRGB * srcA + dstRGB * dstA * (1 - srcA)) / outA
48  // When outA is 0, outRGB can of course be anything.
49  // TODO: implement proper transparent blending using the above formula
50  SDL_BlitSurface(src, src_rect, dst, dst_rect);
51 }
52 
53 /** Scale a surface using xBRZ algorithm
54  * @param surf The source surface
55  * @param z The scaling factor. Should be an integer 2-5 (1 is tolerated).
56  * @return The scaled surface
57  */
58 surface scale_surface_xbrz(const surface & surf, std::size_t z);
59 
60 /** Scale a surface using alpha-weighted modified bilinear filtering
61  * Note: causes artifacts with alpha gradients, for example in some portraits
62  * @param surf The source surface.
63  * @param w The width of the resulting surface.
64  * @param h The height of the resulting surface.
65  * @return A surface containing the scaled version of the source.
66  * @retval 0 Returned upon error.
67  * @retval surf Returned if w == surf->w and h == surf->h.
68  */
69 surface scale_surface(const surface &surf, int w, int h);
70 
71 /** Scale a surface using simple bilinear filtering (discarding rgb from source
72  * pixels with 0 alpha)
73  * @param surf The source surface.
74  * @param w The width of the resulting surface.
75  * @param h The height of the resulting surface.
76  * @return A surface containing the scaled version of the source.
77  * @retval 0 Returned upon error.
78  * @retval surf Returned if w == surf->w and h == surf->h.
79  */
80 surface scale_surface_legacy(const surface &surf, int w, int h);
81 
82 /** Scale a surface using modified nearest neighbour algorithm. Use only if
83  * preserving sharp edges is a priority (e.g. minimap).
84  * @param surf The source surface.
85  * @param w The width of the resulting surface.
86  * @param h The height of the resulting surface.
87  * @return A surface containing the scaled version of the source.
88  * @retval 0 Returned upon error.
89  * @retval surf Returned if w == surf->w and h == surf->h.
90  */
91 surface scale_surface_sharp(const surface& surf, int w, int h);
92 
93 void adjust_surface_color(surface& surf, int r, int g, int b);
95 void monochrome_image(surface& surf, const int threshold);
96 void sepia_image(surface& surf);
97 void negative_image(surface& surf, const int thresholdR, const int thresholdG, const int thresholdB);
99 void wipe_alpha(surface& surf);
100 /** create an heavy shadow of the image, by blurring, increasing alpha and darkening */
101 void shadow_image(surface& surf, int scale = 1);
102 
103 enum channel { RED, GREEN, BLUE, ALPHA };
105 
106 /**
107  * Recolors a surface using a map with source and converted palette values.
108  * This is most often used for team-coloring.
109  *
110  * @param surf The source surface.
111  * @param map_rgb Map of color values, with the keys corresponding to the
112  * source palette, and the values to the recolored palette.
113  */
114 void recolor_image(surface& surf, const color_range_map& map_rgb);
115 
116 void brighten_image(surface& surf, int32_t amount);
117 
118 /** Get a portion of the screen.
119  * Send nullptr if the portion is outside of the screen.
120  * @param surf The source surface.
121  * @param rect The portion of the source surface to copy.
122  * @return A surface containing the portion of the source.
123  * No RLE or Alpha bits are set.
124  * @retval 0 if error or the portion is outside of the surface.
125  */
127 
128 void adjust_surface_alpha(surface& surf, uint8_t alpha_mod);
129 void adjust_surface_alpha_add(surface& surf, int amount);
130 
131 /** Applies a mask on a surface. */
132 void mask_surface(surface& surf, const surface& mask, bool* empty_result = nullptr, const std::string& filename = std::string());
133 
134 /** Check if a surface fit into a mask */
135 bool in_mask_surface(const surface& surf, const surface& mask);
136 
137 /**
138  * Light surf using lightmap
139  * @param surf The source surface.
140  * @param lightmap add/subtract this color to surf
141  * but RGB values are converted to (X-128)*2
142  * to cover the full (-256,256) spectrum.
143  * Should already be neutral
144 */
145 void light_surface(surface& surf, const surface &lightmap);
146 
147 /**
148  * Cross-fades a surface in place.
149  *
150  * @param surf The surface to blur, must have 32 bits per pixel.
151  * @param rect The part of the surface to blur.
152  * @param depth The depth of the blurring.
153  */
154 void blur_surface(surface& surf, rect rect, int depth = 1);
155 
156 /**
157  * Cross-fades a surface with alpha channel.
158  *
159  * @param surf The source surface.
160  * @param depth The depth of the blurring.
161  */
162 void blur_alpha_surface(surface& surf, int depth = 1);
163 
164 /** Cuts a rectangle from a surface. */
165 surface cut_surface(const surface &surf, const rect& r);
166 
167 /**
168  * Blends a surface with a color.
169  *
170  * Every pixel in the surface will be blended with the @p color given. The
171  * final color of a pixel is amount * @p color + (1 - amount) * original.
172  *
173  * @param surf The surface to blend.
174  * @param amount The amount of the new color is determined by
175  * @p color. Must be a number in the range
176  * [0, 1].
177  * @param color The color to blend width, note its alpha
178  * channel is ignored.
179  */
180 void blend_surface(surface& surf, const double amount, const color_t color);
181 
182 /**
183  * Rotates a surface by any degrees.
184  *
185  * @pre @p zoom >= @p offset Otherwise @return will have empty pixels.
186  * @pre @p offset > 0 Otherwise the procedure will not return.
187  *
188  * @param surf The surface to rotate.
189  * @param angle The angle of rotation.
190  * @param zoom Which zoom level to use for calculating the result.
191  * @param offset Pixel offset when scanning the zoomed source.
192  *
193  * @return The rotated surface.
194  */
195 surface rotate_any_surface(const surface& surf, float angle, int zoom, int offset);
196 
197 /**
198  * Rotates a surface 180 degrees.
199  *
200  * @param surf The surface to rotate.
201  *
202  * @return The rotated surface.
203  */
205 
206 /**
207  * Rotates a surface 90 degrees.
208  *
209  * @param surf The surface to rotate.
210  * @param clockwise Whether the rotation should be clockwise (true)
211  * or counter-clockwise (false).
212  *
213  * @return The rotated surface.
214  */
215 surface rotate_90_surface(const surface& surf, bool clockwise);
216 
217 void flip_surface(surface& surf);
218 void flop_surface(surface& surf);
219 
double g
Definition: astarsearch.cpp:63
Represents version numbers.
std::unordered_map< color_t, color_t > color_range_map
Definition: color_range.hpp:30
Interfaces for manipulating version numbers of engine, add-ons, etc.
General math utility functions.
version_info get_version()
Returns the runtime SDL version.
Definition: utils.cpp:40
bool runtime_at_least(uint8_t major, uint8_t minor=0, uint8_t patch=0)
Returns true if the runtime SDL version is at or greater than the specified version,...
Definition: utils.cpp:47
void scale(size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, ColorFormat colFmt, const ScalerCfg &cfg=ScalerCfg(), int yFirst=0, int yLast=std::numeric_limits< int >::max())
Definition: xbrz.cpp:1175
int w
Definition: pathfind.cpp:188
rect dst
Location on the final composed sheet.
surface surf
Image.
rect src
Non-transparent portion of the surface to compose.
std::string filename
Filename.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:61
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
void mask_surface(surface &surf, const surface &mask, bool *empty_result=nullptr, const std::string &filename=std::string())
Applies a mask on a surface.
Definition: utils.cpp:706
void blend_surface(surface &surf, const double amount, const color_t color)
Blends a surface with a color.
Definition: utils.cpp:1159
void sepia_image(surface &surf)
Definition: utils.cpp:462
void flip_surface(surface &surf)
Definition: utils.cpp:1323
void blur_surface(surface &surf, rect rect, int depth=1)
Cross-fades a surface in place.
Definition: utils.cpp:846
surface get_surface_portion(const surface &surf, rect &rect)
Get a portion of the screen.
Definition: utils.cpp:1355
void recolor_image(surface &surf, const color_range_map &map_rgb)
Recolors a surface using a map with source and converted palette values.
Definition: utils.cpp:634
surface scale_surface_legacy(const surface &surf, int w, int h)
Scale a surface using simple bilinear filtering (discarding rgb from source pixels with 0 alpha)
Definition: utils.cpp:225
void wipe_alpha(surface &surf)
Definition: utils.cpp:515
void brighten_image(surface &surf, int32_t amount)
Definition: utils.cpp:665
surface cut_surface(const surface &surf, const rect &r)
Cuts a rectangle from a surface.
Definition: utils.cpp:1100
void greyscale_image(surface &surf)
Definition: utils.cpp:422
void adjust_surface_alpha(surface &surf, uint8_t alpha_mod)
Definition: utils.cpp:683
void swap_channels_image(surface &surf, channel r, channel g, channel b, channel a)
Definition: utils.cpp:552
void blur_alpha_surface(surface &surf, int depth=1)
Cross-fades a surface with alpha channel.
Definition: utils.cpp:962
void negative_image(surface &surf, const int thresholdR, const int thresholdG, const int thresholdB)
Definition: utils.cpp:481
void adjust_surface_color(surface &surf, int r, int g, int b)
Definition: utils.cpp:405
void alpha_to_greyscale(surface &surf)
Definition: utils.cpp:502
rect get_non_transparent_portion(const surface &surf)
Definition: utils.cpp:1399
void shadow_image(surface &surf, int scale=1)
create an heavy shadow of the image, by blurring, increasing alpha and darkening
Definition: utils.cpp:527
void sdl_blit(const surface &src, const SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
Definition: utils.hpp:43
void monochrome_image(surface &surf, const int threshold)
Definition: utils.cpp:444
void light_surface(surface &surf, const surface &lightmap)
Light surf using lightmap.
Definition: utils.cpp:798
void flop_surface(surface &surf)
Definition: utils.cpp:1339
surface scale_surface_xbrz(const surface &surf, std::size_t z)
Scale a surface using xBRZ algorithm.
Definition: utils.cpp:61
surface scale_surface_sharp(const surface &surf, int w, int h)
Scale a surface using modified nearest neighbour algorithm.
Definition: utils.cpp:358
void adjust_surface_alpha_add(surface &surf, int amount)
Definition: utils.cpp:692
surface rotate_90_surface(const surface &surf, bool clockwise)
Rotates a surface 90 degrees.
Definition: utils.cpp:1289
surface rotate_180_surface(const surface &surf)
Rotates a surface 180 degrees.
Definition: utils.cpp:1249
surface scale_surface(const surface &surf, int w, int h)
Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gr...
Definition: utils.cpp:99
surface rotate_any_surface(const surface &surf, float angle, int zoom, int offset)
Rotates a surface by any degrees.
Definition: utils.cpp:1187
channel
Definition: utils.hpp:103
@ BLUE
Definition: utils.hpp:103
@ ALPHA
Definition: utils.hpp:103
@ GREEN
Definition: utils.hpp:103
@ RED
Definition: utils.hpp:103
bool in_mask_surface(const surface &surf, const surface &mask)
Check if a surface fit into a mask.
Definition: utils.cpp:760
#define h
#define b