The Battle for Wesnoth  1.19.1+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 /**
40  * Fill a rectangle on a given surface. Alias for SDL_FillRect.
41  *
42  * @param dst The surface to operate on.
43  * @param dst_rect The rectangle to fill.
44  * @param color Color of the rectangle.
45  */
46 inline void fill_surface_rect(surface& dst, SDL_Rect* dst_rect, const uint32_t color)
47 {
48  SDL_FillRect(dst, dst_rect, color);
49 }
50 
51 } // namespace sdl
52 
53 
54 inline void sdl_blit(const surface& src, const SDL_Rect* src_rect, surface& dst, SDL_Rect* dst_rect){
55  // Note: this is incorrect when both src and dst combine transparent pixels.
56  // The correct equation is, per-pixel:
57  // outA = srcA + dstA * (1 - srcA)
58  // outRGB = (srcRGB * srcA + dstRGB * dstA * (1 - srcA)) / outA
59  // When outA is 0, outRGB can of course be anything.
60  // TODO: implement proper transparent blending using the above formula
61  SDL_BlitSurface(src, src_rect, dst, dst_rect);
62 }
63 
64 /** Scale a surface using xBRZ algorithm
65  * @param surf The source surface
66  * @param z The scaling factor. Should be an integer 2-5 (1 is tolerated).
67  * @return The scaled surface
68  */
69 surface scale_surface_xbrz(const surface & surf, std::size_t z);
70 
71 /** Scale a surface using the nearest neighbor algorithm (provided by xBRZ lib)
72  * @param surf The sources surface
73  * @param w The width of the resulting surface.
74  * @param h The height of the resulting surface.
75  * @return The rescaled surface.
76  */
77 surface scale_surface_nn(const surface & surf, int w, int h);
78 
79 /** Scale a surface using alpha-weighted modified bilinear filtering
80  * Note: causes artifacts with alpha gradients, for example in some portraits
81  * @param surf The source surface.
82  * @param w The width of the resulting surface.
83  * @param h The height of the resulting surface.
84  * @return A surface containing the scaled version of the source.
85  * @retval 0 Returned upon error.
86  * @retval surf Returned if w == surf->w and h == surf->h.
87  */
88 surface scale_surface(const surface &surf, int w, int h);
89 
90 /** Scale a surface using simple bilinear filtering (discarding rgb from source
91  * pixels with 0 alpha)
92  * @param surf The source surface.
93  * @param w The width of the resulting surface.
94  * @param h The height of the resulting surface.
95  * @return A surface containing the scaled version of the source.
96  * @retval 0 Returned upon error.
97  * @retval surf Returned if w == surf->w and h == surf->h.
98  */
99 surface scale_surface_legacy(const surface &surf, int w, int h);
100 
101 /** Scale a surface using modified nearest neighbour algorithm. Use only if
102  * preserving sharp edges is a priority (e.g. minimap).
103  * @param surf The source surface.
104  * @param w The width of the resulting surface.
105  * @param h The height of the resulting surface.
106  * @return A surface containing the scaled version of the source.
107  * @retval 0 Returned upon error.
108  * @retval surf Returned if w == surf->w and h == surf->h.
109  */
110 surface scale_surface_sharp(const surface& surf, int w, int h);
111 
112 surface adjust_surface_color(const surface &surf, int r, int g, int b);
113 surface greyscale_image(const surface &surf);
114 surface monochrome_image(const surface &surf, const int threshold);
115 surface sepia_image(const surface &surf);
116 surface negative_image(const surface &surf, const int thresholdR, const int thresholdG, const int thresholdB);
117 surface alpha_to_greyscale(const surface & surf);
118 surface wipe_alpha(const surface & surf);
119 /** create an heavy shadow of the image, by blurring, increasing alpha and darkening */
120 surface shadow_image(const surface &surf, int scale = 1);
121 
122 enum channel { RED, GREEN, BLUE, ALPHA };
124 
125 /**
126  * Recolors a surface using a map with source and converted palette values.
127  * This is most often used for team-coloring.
128  *
129  * @param surf The source surface.
130  * @param map_rgb Map of color values, with the keys corresponding to the
131  * source palette, and the values to the recolored palette.
132  * @return A recolored surface, or a null surface if there are
133  * problems with the source.
134  */
135 surface recolor_image(surface surf, const color_range_map& map_rgb);
136 
137 surface brighten_image(const surface &surf, int32_t amount);
138 
139 /** Get a portion of the screen.
140  * Send nullptr if the portion is outside of the screen.
141  * @param surf The source surface.
142  * @param rect The portion of the source surface to copy.
143  * @return A surface containing the portion of the source.
144  * No RLE or Alpha bits are set.
145  * @retval 0 if error or the portion is outside of the surface.
146  */
147 surface get_surface_portion(const surface &surf, SDL_Rect &rect);
148 
149 void adjust_surface_alpha(surface& surf, uint8_t alpha_mod);
150 surface adjust_surface_alpha_add(const surface &surf, int amount);
151 
152 /** Applies a mask on a surface. */
153 surface mask_surface(const surface &surf, const surface &mask, bool* empty_result = nullptr, const std::string& filename = std::string());
154 
155 /** Check if a surface fit into a mask */
156 bool in_mask_surface(const surface &surf, const surface &mask);
157 
158 /**
159  * Light surf using lightmap
160  * @param surf The source surface.
161  * @param lightmap add/subtract this color to surf
162  * but RGB values are converted to (X-128)*2
163  * to cover the full (-256,256) spectrum.
164  * Should already be neutral
165 */
166 surface light_surface(const surface &surf, const surface &lightmap);
167 
168 /**
169  * Cross-fades a surface.
170  *
171  * @param surf The source surface.
172  * @param depth The depth of the blurring.
173  * @return A new, blurred, neutral surface.
174  */
175 surface blur_surface(const surface &surf, int depth = 1);
176 
177 /**
178  * Cross-fades a surface in place.
179  *
180  * @param surf The surface to blur, must have 32 bits per pixel.
181  * @param rect The part of the surface to blur.
182  * @param depth The depth of the blurring.
183  */
184 void blur_surface(surface& surf, SDL_Rect rect, int depth = 1);
185 
186 /**
187  * Cross-fades a surface with alpha channel.
188  *
189  * @param surf The source surface.
190  * @param depth The depth of the blurring.
191  * @return A new, blurred, neutral surface.
192  */
193 surface blur_alpha_surface(const surface &surf, int depth = 1);
194 
195 /** Cuts a rectangle from a surface. */
196 surface cut_surface(const surface &surf, const SDL_Rect& r);
197 
198 /**
199  * Blends a surface with a color.
200  *
201  * Every pixel in the surface will be blended with the @p color given. The
202  * final color of a pixel is amount * @p color + (1 - amount) * original.
203  *
204  * @param surf The surface to blend.
205  * @param amount The amount of the new color is determined by
206  * @p color. Must be a number in the range
207  * [0, 1].
208  * @param color The color to blend width, note its alpha
209  * channel is ignored.
210  *
211  * @return The blended surface.
212  */
214  const surface &surf
215  , const double amount
216  , const color_t color);
217 
218 /**
219  * Rotates a surface by any degrees.
220  *
221  * @pre @p zoom >= @p offset Otherwise @return will have empty pixels.
222  * @pre @p offset > 0 Otherwise the procedure will not return.
223  *
224  * @param surf The surface to rotate.
225  * @param angle The angle of rotation.
226  * @param zoom Which zoom level to use for calculating the result.
227  * @param offset Pixel offset when scanning the zoomed source.
228  *
229  * @return The rotated surface.
230  */
231 surface rotate_any_surface(const surface& surf, float angle,
232  int zoom, int offset);
233 
234 /**
235  * Rotates a surface 180 degrees.
236  *
237  * @param surf The surface to rotate.
238  *
239  * @return The rotated surface.
240  */
241 surface rotate_180_surface(const surface &surf);
242 
243 /**
244  * Rotates a surface 90 degrees.
245  *
246  * @param surf The surface to rotate.
247  * @param clockwise Whether the rotation should be clockwise (true)
248  * or counter-clockwise (false).
249  *
250  * @return The rotated surface.
251  */
252 surface rotate_90_surface(const surface &surf, bool clockwise);
253 
254 surface flip_surface(const surface &surf);
255 surface flop_surface(const surface &surf);
256 
258 
259 /**
260  * Helper methods for setting/getting a single pixel in an image.
261  * Lifted from http://sdl.beuc.net/sdl.wiki/Pixel_Access
262  *
263  * @param surf The image to get or receive the pixel from.
264  * @param surf_lock The locked surface to make sure the pointers are valid.
265  * @param x The position in the row of the pixel.
266  * @param y The row of the pixel.
267  * @param pixel The pixel value.
268  */
269 void put_pixel(const surface& surf, surface_lock& surf_lock, int x, int y, uint32_t pixel);
270 uint32_t get_pixel(const surface& surf, const const_surface_lock& surf_lock, int x, int y);
271 
double g
Definition: astarsearch.cpp:63
Helper class for pinning SDL surfaces into memory.
Definition: surface.hpp:123
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.
void fill_surface_rect(surface &dst, SDL_Rect *dst_rect, const uint32_t color)
Fill a rectangle on a given surface.
Definition: utils.hpp:46
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, const ScalerCfg &cfg=ScalerCfg(), int yFirst=0, int yLast=std::numeric_limits< int >::max())
Definition: xbrz.cpp:1189
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
surface alpha_to_greyscale(const surface &surf)
Definition: utils.cpp:662
surface 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:857
surface adjust_surface_color(const surface &surf, int r, int g, int b)
Definition: utils.cpp:447
surface negative_image(const surface &surf, const int thresholdR, const int thresholdG, const int thresholdB)
Definition: utils.cpp:619
surface swap_channels_image(const surface &surf, channel r, channel g, channel b, channel a)
Definition: utils.cpp:755
surface blur_surface(const surface &surf, int depth=1)
Cross-fades a surface.
Definition: utils.cpp:1167
surface light_surface(const surface &surf, const surface &lightmap)
Light surf using lightmap.
Definition: utils.cpp:1101
surface scale_surface_nn(const surface &surf, int w, int h)
Scale a surface using the nearest neighbor algorithm (provided by xBRZ lib)
Definition: utils.cpp:94
surface shadow_image(const surface &surf, int scale=1)
create an heavy shadow of the image, by blurring, increasing alpha and darkening
Definition: utils.cpp:718
surface get_surface_portion(const surface &surf, SDL_Rect &rect)
Get a portion of the screen.
Definition: utils.cpp:1800
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:260
surface get_non_transparent_portion(const surface &surf)
Definition: utils.cpp:1850
void put_pixel(const surface &surf, surface_lock &surf_lock, int x, int y, uint32_t pixel)
Helper methods for setting/getting a single pixel in an image.
Definition: utils.cpp:1611
surface adjust_surface_alpha_add(const surface &surf, int amount)
Definition: utils.cpp:947
surface flip_surface(const surface &surf)
Definition: utils.cpp:1742
void adjust_surface_alpha(surface &surf, uint8_t alpha_mod)
Definition: utils.cpp:938
surface sepia_image(const surface &surf)
Definition: utils.cpp:577
surface blend_surface(const surface &surf, const double amount, const color_t color)
Blends a surface with a color.
Definition: utils.cpp:1507
void sdl_blit(const surface &src, const SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
Definition: utils.hpp:54
uint32_t get_pixel(const surface &surf, const const_surface_lock &surf_lock, int x, int y)
Definition: utils.cpp:1642
surface cut_surface(const surface &surf, const SDL_Rect &r)
Cuts a rectangle from a surface.
Definition: utils.cpp:1449
surface wipe_alpha(const surface &surf)
Definition: utils.cpp:690
surface flop_surface(const surface &surf)
Definition: utils.cpp:1771
surface brighten_image(const surface &surf, int32_t amount)
Definition: utils.cpp:896
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:397
surface monochrome_image(const surface &surf, const int threshold)
Definition: utils.cpp:537
surface rotate_90_surface(const surface &surf, bool clockwise)
Rotates a surface 90 degrees.
Definition: utils.cpp:1707
surface rotate_180_surface(const surface &surf)
Rotates a surface 180 degrees.
Definition: utils.cpp:1665
surface greyscale_image(const surface &surf)
Definition: utils.cpp:492
surface mask_surface(const surface &surf, const surface &mask, bool *empty_result=nullptr, const std::string &filename=std::string())
Applies a mask on a surface.
Definition: utils.cpp:985
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:131
surface rotate_any_surface(const surface &surf, float angle, int zoom, int offset)
Rotates a surface by any degrees.
Definition: utils.cpp:1554
surface blur_alpha_surface(const surface &surf, int depth=1)
Cross-fades a surface with alpha channel.
Definition: utils.cpp:1302
channel
Definition: utils.hpp:122
@ BLUE
Definition: utils.hpp:122
@ ALPHA
Definition: utils.hpp:122
@ GREEN
Definition: utils.hpp:122
@ RED
Definition: utils.hpp:122
bool in_mask_surface(const surface &surf, const surface &mask)
Check if a surface fit into a mask.
Definition: utils.cpp:1055
#define h
#define a
#define b