The Battle for Wesnoth  1.19.7+dev
utils.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 <cstdlib>
25 #include <string>
26 
27 namespace sdl
28 {
29 
30 /** Returns the runtime SDL version. */
32 
33 /**
34  * Returns true if the runtime SDL version is at or greater than the
35  * specified version, false otherwise.
36  */
37 bool runtime_at_least(uint8_t major, uint8_t minor = 0, uint8_t patch = 0);
38 
39 } // namespace sdl
40 
41 
42 inline void sdl_blit(const surface& src, const SDL_Rect* src_rect, surface& dst, SDL_Rect* dst_rect){
43  // Note: this is incorrect when both src and dst combine transparent pixels.
44  // The correct equation is, per-pixel:
45  // outA = srcA + dstA * (1 - srcA)
46  // outRGB = (srcRGB * srcA + dstRGB * dstA * (1 - srcA)) / outA
47  // When outA is 0, outRGB can of course be anything.
48  // TODO: implement proper transparent blending using the above formula
49  SDL_BlitSurface(src, src_rect, dst, dst_rect);
50 }
51 
52 /** Scale a surface using xBRZ algorithm
53  * @param surf The source surface
54  * @param z The scaling factor. Should be an integer 2-5 (1 is tolerated).
55  * @return The scaled surface
56  */
57 surface scale_surface_xbrz(const surface & surf, std::size_t z);
58 
59 /** Scale a surface using alpha-weighted modified bilinear filtering
60  * Note: causes artifacts with alpha gradients, for example in some portraits
61  * @param surf The source surface.
62  * @param w The width of the resulting surface.
63  * @param h The height of the resulting surface.
64  * @return A surface containing the scaled version of the source.
65  * @retval 0 Returned upon error.
66  * @retval surf Returned if w == surf->w and h == surf->h.
67  */
68 surface scale_surface(const surface &surf, int w, int h);
69 
70 /** Scale a surface using simple bilinear filtering (discarding rgb from source
71  * pixels with 0 alpha)
72  * @param surf The source surface.
73  * @param w The width of the resulting surface.
74  * @param h The height of the resulting surface.
75  * @return A surface containing the scaled version of the source.
76  * @retval 0 Returned upon error.
77  * @retval surf Returned if w == surf->w and h == surf->h.
78  */
79 surface scale_surface_legacy(const surface &surf, int w, int h);
80 
81 /** Scale a surface using modified nearest neighbour algorithm. Use only if
82  * preserving sharp edges is a priority (e.g. minimap).
83  * @param surf The source surface.
84  * @param w The width of the resulting surface.
85  * @param h The height of the resulting surface.
86  * @return A surface containing the scaled version of the source.
87  * @retval 0 Returned upon error.
88  * @retval surf Returned if w == surf->w and h == surf->h.
89  */
90 surface scale_surface_sharp(const surface& surf, int w, int h);
91 
92 void adjust_surface_color(surface& surf, int r, int g, int b);
94 void monochrome_image(surface& surf, const int threshold);
95 void sepia_image(surface& surf);
96 void negative_image(surface& surf, const int thresholdR, const int thresholdG, const int thresholdB);
98 void wipe_alpha(surface& surf);
99 /** create an heavy shadow of the image, by blurring, increasing alpha and darkening */
100 void shadow_image(surface& surf, int scale = 1);
101 
102 enum channel { RED, GREEN, BLUE, ALPHA };
104 
105 /**
106  * Recolors a surface using a map with source and converted palette values.
107  * This is most often used for team-coloring.
108  *
109  * @param surf The source surface.
110  * @param map_rgb Map of color values, with the keys corresponding to the
111  * source palette, and the values to the recolored palette.
112  */
113 void recolor_image(surface& surf, const color_range_map& map_rgb);
114 
115 void brighten_image(surface& surf, int32_t amount);
116 
117 /** Get a portion of the screen.
118  * Send nullptr if the portion is outside of the screen.
119  * @param surf The source surface.
120  * @param rect The portion of the source surface to copy.
121  * @return A surface containing the portion of the source.
122  * No RLE or Alpha bits are set.
123  * @retval 0 if error or the portion is outside of the surface.
124  */
125 surface get_surface_portion(const surface &surf, SDL_Rect &rect);
126 
127 void adjust_surface_alpha(surface& surf, uint8_t alpha_mod);
128 void adjust_surface_alpha_add(surface& surf, int amount);
129 
130 /** Applies a mask on a surface. */
131 void mask_surface(surface& surf, const surface& mask, bool* empty_result = nullptr, const std::string& filename = std::string());
132 
133 /** Check if a surface fit into a mask */
134 bool in_mask_surface(const surface& surf, const surface& mask);
135 
136 /**
137  * Light surf using lightmap
138  * @param surf The source surface.
139  * @param lightmap add/subtract this color to surf
140  * but RGB values are converted to (X-128)*2
141  * to cover the full (-256,256) spectrum.
142  * Should already be neutral
143 */
144 void light_surface(surface& surf, const surface &lightmap);
145 
146 /**
147  * Cross-fades a surface in place.
148  *
149  * @param surf The surface to blur, must have 32 bits per pixel.
150  * @param rect The part of the surface to blur.
151  * @param depth The depth of the blurring.
152  */
153 void blur_surface(surface& surf, SDL_Rect rect, int depth = 1);
154 
155 /**
156  * Cross-fades a surface with alpha channel.
157  *
158  * @param surf The source surface.
159  * @param depth The depth of the blurring.
160  */
161 void blur_alpha_surface(surface& surf, int depth = 1);
162 
163 /** Cuts a rectangle from a surface. */
164 surface cut_surface(const surface &surf, const SDL_Rect& r);
165 
166 /**
167  * Blends a surface with a color.
168  *
169  * Every pixel in the surface will be blended with the @p color given. The
170  * final color of a pixel is amount * @p color + (1 - amount) * original.
171  *
172  * @param surf The surface to blend.
173  * @param amount The amount of the new color is determined by
174  * @p color. Must be a number in the range
175  * [0, 1].
176  * @param color The color to blend width, note its alpha
177  * channel is ignored.
178  */
179 void blend_surface(surface& surf, const double amount, const color_t color);
180 
181 /**
182  * Rotates a surface by any degrees.
183  *
184  * @pre @p zoom >= @p offset Otherwise @return will have empty pixels.
185  * @pre @p offset > 0 Otherwise the procedure will not return.
186  *
187  * @param surf The surface to rotate.
188  * @param angle The angle of rotation.
189  * @param zoom Which zoom level to use for calculating the result.
190  * @param offset Pixel offset when scanning the zoomed source.
191  *
192  * @return The rotated surface.
193  */
194 surface rotate_any_surface(const surface& surf, float angle, int zoom, int offset);
195 
196 /**
197  * Rotates a surface 180 degrees.
198  *
199  * @param surf The surface to rotate.
200  *
201  * @return The rotated surface.
202  */
204 
205 /**
206  * Rotates a surface 90 degrees.
207  *
208  * @param surf The surface to rotate.
209  * @param clockwise Whether the rotation should be clockwise (true)
210  * or counter-clockwise (false).
211  *
212  * @return The rotated surface.
213  */
214 surface rotate_90_surface(const surface& surf, bool clockwise);
215 
216 void flip_surface(surface& surf);
217 void flop_surface(surface& surf);
218 
double g
Definition: astarsearch.cpp:63
Represents version numbers.
std::unordered_map< color_t, color_t > color_range_map
Definition: color_range.hpp:30
int w
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:36
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:43
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:1170
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:59
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
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:805
void blend_surface(surface &surf, const double amount, const color_t color)
Blends a surface with a color.
Definition: utils.cpp:1274
void sepia_image(surface &surf)
Definition: utils.cpp:492
void flip_surface(surface &surf)
Definition: utils.cpp:1441
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:711
surface get_surface_portion(const surface &surf, SDL_Rect &rect)
Get a portion of the screen.
Definition: utils.cpp:1473
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:221
void wipe_alpha(surface &surf)
Definition: utils.cpp:572
void brighten_image(surface &surf, int32_t amount)
Definition: utils.cpp:742
void greyscale_image(surface &surf)
Definition: utils.cpp:429
void adjust_surface_alpha(surface &surf, uint8_t alpha_mod)
Definition: utils.cpp:771
void swap_channels_image(surface &surf, channel r, channel g, channel b, channel a)
Definition: utils.cpp:619
void blur_alpha_surface(surface &surf, int depth=1)
Cross-fades a surface with alpha channel.
Definition: utils.cpp:1077
void negative_image(surface &surf, const int thresholdR, const int thresholdG, const int thresholdB)
Definition: utils.cpp:523
void adjust_surface_color(surface &surf, int r, int g, int b)
Definition: utils.cpp:401
void alpha_to_greyscale(surface &surf)
Definition: utils.cpp:555
rect get_non_transparent_portion(const surface &surf)
Definition: utils.cpp:1517
void shadow_image(surface &surf, int scale=1)
create an heavy shadow of the image, by blurring, increasing alpha and darkening
Definition: utils.cpp:589
void sdl_blit(const surface &src, const SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
Definition: utils.hpp:42
void monochrome_image(surface &surf, const int threshold)
Definition: utils.cpp:463
surface cut_surface(const surface &surf, const SDL_Rect &r)
Cuts a rectangle from a surface.
Definition: utils.cpp:1215
void light_surface(surface &surf, const surface &lightmap)
Light surf using lightmap.
Definition: utils.cpp:904
void flop_surface(surface &surf)
Definition: utils.cpp:1457
surface scale_surface_xbrz(const surface &surf, std::size_t z)
Scale a surface using xBRZ algorithm.
Definition: utils.cpp:57
surface scale_surface_sharp(const surface &surf, int w, int h)
Scale a surface using modified nearest neighbour algorithm.
Definition: utils.cpp:354
void adjust_surface_alpha_add(surface &surf, int amount)
Definition: utils.cpp:780
surface rotate_90_surface(const surface &surf, bool clockwise)
Rotates a surface 90 degrees.
Definition: utils.cpp:1407
surface rotate_180_surface(const surface &surf)
Rotates a surface 180 degrees.
Definition: utils.cpp:1367
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:95
surface rotate_any_surface(const surface &surf, float angle, int zoom, int offset)
Rotates a surface by any degrees.
Definition: utils.cpp:1305
void blur_surface(surface &surf, SDL_Rect rect, int depth=1)
Cross-fades a surface in place.
Definition: utils.cpp:961
channel
Definition: utils.hpp:102
@ BLUE
Definition: utils.hpp:102
@ ALPHA
Definition: utils.hpp:102
@ GREEN
Definition: utils.hpp:102
@ RED
Definition: utils.hpp:102
bool in_mask_surface(const surface &surf, const surface &mask)
Check if a surface fit into a mask.
Definition: utils.cpp:866
#define h
#define b