The Battle for Wesnoth  1.17.6+dev
Functions
utils.cpp File Reference

Support-routines for the SDL-graphics-library. More...

#include "sdl/utils.hpp"
#include "sdl/rect.hpp"
#include "color.hpp"
#include "log.hpp"
#include "xBRZ/xbrz.hpp"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <boost/circular_buffer.hpp>
#include <boost/math/constants/constants.hpp>
Include dependency graph for utils.cpp:

Go to the source code of this file.

Functions

surface scale_surface_xbrz (const surface &surf, std::size_t z)
 Scale a surface using xBRZ algorithm. More...
 
surface scale_surface_nn (const surface &surf, int w, int h)
 Scale a surface using the nearest neighbor algorithm (provided by xBRZ lib) More...
 
surface scale_surface (const surface &surf, int w, int h)
 Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gradients, for example in some portraits. More...
 
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) More...
 
surface scale_surface_sharp (const surface &surf, int w, int h)
 Scale a surface using modified nearest neighbour algorithm. More...
 
surface adjust_surface_color (const surface &surf, int red, int green, int blue)
 
surface greyscale_image (const surface &surf)
 
surface monochrome_image (const surface &surf, const int threshold)
 
surface sepia_image (const surface &surf)
 
surface negative_image (const surface &surf, const int thresholdR, const int thresholdG, const int thresholdB)
 
surface alpha_to_greyscale (const surface &surf)
 
surface wipe_alpha (const surface &surf)
 
surface shadow_image (const surface &surf, int scale)
 create an heavy shadow of the image, by blurring, increasing alpha and darkening More...
 
surface swap_channels_image (const surface &surf, channel r, channel g, channel b, channel a)
 
surface recolor_image (surface surf, const color_range_map &map_rgb)
 Recolors a surface using a map with source and converted palette values. More...
 
surface brighten_image (const surface &surf, int32_t amount)
 
void adjust_surface_alpha (surface &surf, uint8_t alpha_mod)
 
surface adjust_surface_alpha_add (const surface &surf, int amount)
 
surface mask_surface (const surface &surf, const surface &mask, bool *empty_result, const std::string &filename)
 Applies a mask on a surface. More...
 
bool in_mask_surface (const surface &surf, const surface &mask)
 Check if a surface fit into a mask. More...
 
surface light_surface (const surface &surf, const surface &lightmap)
 Light surf using lightmap. More...
 
surface blur_surface (const surface &surf, int depth)
 Cross-fades a surface. More...
 
void blur_surface (surface &surf, SDL_Rect rect, int depth)
 Cross-fades a surface in place. More...
 
surface blur_alpha_surface (const surface &surf, int depth)
 Cross-fades a surface with alpha channel. More...
 
surface cut_surface (const surface &surf, const SDL_Rect &r)
 Cuts a rectangle from a surface. More...
 
surface blend_surface (const surface &surf, const double amount, const color_t color)
 Blends a surface with a color. More...
 
surface rotate_any_surface (const surface &surf, float angle, int zoom, int offset)
 Rotates a surface by any degrees. More...
 
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. More...
 
uint32_t get_pixel (const surface &surf, const const_surface_lock &surf_lock, int x, int y)
 
surface rotate_180_surface (const surface &surf)
 Rotates a surface 180 degrees. More...
 
surface rotate_90_surface (const surface &surf, bool clockwise)
 Rotates a surface 90 degrees. More...
 
surface flip_surface (const surface &surf)
 
surface flop_surface (const surface &surf)
 
surface get_surface_portion (const surface &src, SDL_Rect &area)
 Get a portion of the screen. More...
 
SDL_Rect get_non_transparent_portion (const surface &surf)
 

Detailed Description

Support-routines for the SDL-graphics-library.

Definition in file utils.cpp.

Function Documentation

◆ adjust_surface_alpha()

void adjust_surface_alpha ( surface surf,
uint8_t  alpha_mod 
)

Definition at line 975 of file utils.cpp.

Referenced by font::floating_label::create_texture().

◆ adjust_surface_alpha_add()

surface adjust_surface_alpha_add ( const surface surf,
int  amount 
)

Definition at line 984 of file utils.cpp.

References b, surface::clone(), g, surface_locker< T >::pixels(), and PLAIN_LOG.

◆ adjust_surface_color()

surface adjust_surface_color ( const surface surf,
int  red,
int  green,
int  blue 
)

◆ alpha_to_greyscale()

surface alpha_to_greyscale ( const surface surf)

◆ blend_surface()

surface blend_surface ( const surface surf,
const double  amount,
const color_t  color 
)

Blends a surface with a color.

Every pixel in the surface will be blended with the color given. The final color of a pixel is amount * color + (1 - amount) * original.

Parameters
surfThe surface to blend.
amountThe amount of the new color is determined by color. Must be a number in the range [0, 1].
colorThe color to blend width, note its alpha channel is ignored.
Returns
The blended surface.

Definition at line 1544 of file utils.cpp.

References a, b, surface::clone(), g, surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by image::blend_modification::operator()().

◆ blur_alpha_surface()

surface blur_alpha_surface ( const surface surf,
int  depth = 1 
)

Cross-fades a surface with alpha channel.

Parameters
surfThe source surface.
depthThe depth of the blurring.
Returns
A new, blurred, neutral surface.

Definition at line 1339 of file utils.cpp.

References surface::clone(), p, surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by image::bl_modification::operator()(), and shadow_image().

◆ blur_surface() [1/2]

surface blur_surface ( const surface surf,
int  depth = 1 
)

Cross-fades a surface.

Parameters
surfThe source surface.
depthThe depth of the blurring.
Returns
A new, blurred, neutral surface.

Definition at line 1204 of file utils.cpp.

References blur_surface(), surface::clone(), and PLAIN_LOG.

Referenced by blur_surface(), and gui2::canvas::draw().

◆ blur_surface() [2/2]

void blur_surface ( surface surf,
SDL_Rect  rect,
int  depth = 1 
)

Cross-fades a surface in place.

Parameters
surfThe surface to blur, must have 32 bits per pixel.
rectThe part of the surface to blur.
depthThe depth of the blurring.

Definition at line 1223 of file utils.cpp.

References p, and surface_locker< T >::pixels().

◆ brighten_image()

surface brighten_image ( const surface surf,
int32_t  amount 
)

◆ cut_surface()

surface cut_surface ( const surface surf,
const SDL_Rect &  r 
)

Cuts a rectangle from a surface.

Definition at line 1486 of file utils.cpp.

References surface_locker< T >::pixels(), PLAIN_LOG, and utf8::size().

Referenced by image::get_hexed(), image::load_image_sub_file(), and image::crop_modification::operator()().

◆ flip_surface()

surface flip_surface ( const surface surf)

◆ flop_surface()

surface flop_surface ( const surface surf)

◆ get_non_transparent_portion()

SDL_Rect get_non_transparent_portion ( const surface surf)

Definition at line 1888 of file utils.cpp.

References surface::clone(), n, surface_locker< T >::pixels(), and PLAIN_LOG.

◆ get_pixel()

uint32_t get_pixel ( const surface surf,
const const_surface_lock surf_lock,
int  x,
int  y 
)

Definition at line 1679 of file utils.cpp.

References surface_locker< T >::pixels().

Referenced by rotate_any_surface().

◆ get_surface_portion()

surface get_surface_portion ( const surface surf,
SDL_Rect &  rect 
)

Get a portion of the screen.

Send nullptr if the portion is outside of the screen.

Parameters
surfThe source surface.
rectThe portion of the source surface to copy.
Returns
A surface containing the portion of the source. No RLE or Alpha bits are set.
Return values
0if error or the portion is outside of the surface.

Definition at line 1837 of file utils.cpp.

References PLAIN_LOG.

◆ greyscale_image()

surface greyscale_image ( const surface surf)

Definition at line 529 of file utils.cpp.

References b, surface::clone(), g, surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by image::gs_modification::operator()(), and sdl_blit().

◆ in_mask_surface()

bool in_mask_surface ( const surface surf,
const surface mask 
)

Check if a surface fit into a mask.

Definition at line 1092 of file utils.cpp.

References surface::clone(), surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by image::is_in_hex().

◆ light_surface()

surface light_surface ( const surface surf,
const surface lightmap 
)

Light surf using lightmap.

Parameters
surfThe source surface.
lightmapadd/subtract this color to surf but RGB values are converted to (X-128)*2 to cover the full (-256,256) spectrum. Should already be neutral

Definition at line 1138 of file utils.cpp.

References b, surface::clone(), g, surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by image::apply_light(), and image::light_modification::operator()().

◆ mask_surface()

surface mask_surface ( const surface surf,
const surface mask,
bool *  empty_result = nullptr,
const std::string &  filename = std::string() 
)

◆ monochrome_image()

surface monochrome_image ( const surface surf,
const int  threshold 
)

Definition at line 574 of file utils.cpp.

References b, surface::clone(), g, surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by image::bw_modification::operator()(), and sdl_blit().

◆ negative_image()

surface negative_image ( const surface surf,
const int  thresholdR,
const int  thresholdG,
const int  thresholdB 
)

◆ put_pixel()

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.

Lifted from http://sdl.beuc.net/sdl.wiki/Pixel_Access

Parameters
surfThe image to get or receive the pixel from.
surf_lockThe locked surface to make sure the pointers are valid.
xThe position in the row of the pixel.
yThe row of the pixel.
pixelThe pixel value.

Definition at line 1648 of file utils.cpp.

References surface_locker< T >::pixels().

Referenced by rotate_any_surface().

◆ recolor_image()

surface recolor_image ( surface  surf,
const color_range_map map_rgb 
)

Recolors a surface using a map with source and converted palette values.

This is most often used for team-coloring.

Parameters
surfThe source surface.
map_rgbMap of color values, with the keys corresponding to the source palette, and the values to the recolored palette.
Returns
A recolored surface, or a null surface if there are problems with the source.

Definition at line 894 of file utils.cpp.

References surface::clone(), color_t::from_argb_bytes(), i, surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by image::rc_modification::operator()().

◆ rotate_180_surface()

surface rotate_180_surface ( const surface surf)

Rotates a surface 180 degrees.

Parameters
surfThe surface to rotate.
Returns
The rotated surface.

Definition at line 1702 of file utils.cpp.

References surface::clone(), is_odd(), surface_locker< T >::pixels(), PLAIN_LOG, and swap().

Referenced by image::fl_modification::operator()(), and image::rotate_modification::operator()().

◆ rotate_90_surface()

surface rotate_90_surface ( const surface surf,
bool  clockwise 
)

Rotates a surface 90 degrees.

Parameters
surfThe surface to rotate.
clockwiseWhether the rotation should be clockwise (true) or counter-clockwise (false).
Returns
The rotated surface.

Definition at line 1744 of file utils.cpp.

References surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by image::rotate_modification::operator()().

◆ rotate_any_surface()

surface rotate_any_surface ( const surface surf,
float  angle,
int  zoom,
int  offset 
)

Rotates a surface by any degrees.

Precondition
zoom >= offset Otherwise
Returns
will have empty pixels.
Precondition
offset > 0 Otherwise the procedure will not return.
Parameters
surfThe surface to rotate.
angleThe angle of rotation.
zoomWhich zoom level to use for calculating the result.
offsetPixel offset when scanning the zoomed source.
Returns
The rotated surface.

Definition at line 1591 of file utils.cpp.

References get_pixel(), put_pixel(), xbrz::scale(), and scale_surface().

Referenced by image::rotate_modification::operator()().

◆ scale_surface()

surface scale_surface ( const surface surf,
int  w,
int  h 
)

Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gradients, for example in some portraits.

Parameters
surfThe source surface.
wThe width of the resulting surface.
hThe height of the resulting surface.
Returns
A surface containing the scaled version of the source.
Return values
0Returned upon error.
surfReturned if w == surf->w and h == surf->h.

Definition at line 129 of file utils.cpp.

References a, b, e, fixed_point_divide(), fixed_point_to_int(), g, h, n, surface_locker< T >::pixels(), PLAIN_LOG, s, and w.

Referenced by image::light_modification::operator()(), video::read_pixels_low_res(), rotate_any_surface(), and sdl_blit().

◆ scale_surface_legacy()

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)

Parameters
surfThe source surface.
wThe width of the resulting surface.
hThe height of the resulting surface.
Returns
A surface containing the scaled version of the source.
Return values
0Returned upon error.
surfReturned if w == surf->w and h == surf->h.

Definition at line 258 of file utils.cpp.

References a, b, fixed_point_divide(), fixed_point_to_int(), g, h, surface_locker< T >::pixels(), PLAIN_LOG, and w.

Referenced by image::scale_modification::operator()(), and sdl_blit().

◆ scale_surface_nn()

surface scale_surface_nn ( const surface surf,
int  w,
int  h 
)

Scale a surface using the nearest neighbor algorithm (provided by xBRZ lib)

Parameters
surfThe sources surface
wThe width of the resulting surface.
hThe height of the resulting surface.
Returns
The rescaled surface.

Definition at line 92 of file utils.cpp.

References h, xbrz::nearestNeighborScale(), surface_locker< T >::pixels(), PLAIN_LOG, and w.

Referenced by sdl_blit().

◆ scale_surface_sharp()

surface scale_surface_sharp ( const surface surf,
int  w,
int  h 
)

Scale a surface using modified nearest neighbour algorithm.

Use only if preserving sharp edges is a priority (e.g. minimap).

Parameters
surfThe source surface.
wThe width of the resulting surface.
hThe height of the resulting surface.
Returns
A surface containing the scaled version of the source.
Return values
0Returned upon error.
surfReturned if w == surf->w and h == surf->h.

Definition at line 395 of file utils.cpp.

References a, b, g, h, surface_locker< T >::pixels(), PLAIN_LOG, and w.

Referenced by image::getMinimap(), image::scale_modification::operator()(), and sdl_blit().

◆ scale_surface_xbrz()

surface scale_surface_xbrz ( const surface surf,
std::size_t  z 
)

Scale a surface using xBRZ algorithm.

Parameters
surfThe source surface
zThe scaling factor. Should be an integer 2-5 (1 is tolerated).
Returns
The scaled surface

Definition at line 55 of file utils.cpp.

References surface_locker< T >::pixels(), PLAIN_LOG, and xbrz::scale().

Referenced by image::xbrz_modification::operator()(), and sdl_blit().

◆ sepia_image()

surface sepia_image ( const surface surf)

Definition at line 614 of file utils.cpp.

References b, surface::clone(), g, surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by image::sepia_modification::operator()(), and sdl_blit().

◆ shadow_image()

surface shadow_image ( const surface surf,
int  scale 
)

create an heavy shadow of the image, by blurring, increasing alpha and darkening

Definition at line 755 of file utils.cpp.

References blur_alpha_surface(), surface_locker< T >::pixels(), and PLAIN_LOG.

Referenced by font::floating_label::create_texture(), and sdl_blit().

◆ swap_channels_image()

surface swap_channels_image ( const surface surf,
channel  r,
channel  g,
channel  b,
channel  a 
)

◆ wipe_alpha()

surface wipe_alpha ( const surface surf)