The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Enumerations | Functions
utils.hpp File Reference
#include "color_range.hpp"
#include "color.hpp"
#include "sdl/surface.hpp"
#include "utils/math.hpp"
#include "version.hpp"
#include <SDL.h>
#include <cstdlib>
#include <map>
#include <string>
Include dependency graph for utils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  channel { RED, GREEN, BLUE, ALPHA }
 

Functions

version_info sdl_get_version ()
 
void sdl_blit (const surface &src, SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
 
void sdl_copy_portion (const surface &screen, SDL_Rect *screen_rect, surface &dst, SDL_Rect *dst_rect)
 
bool is_neutral (const surface &surf)
 Check that the surface is neutral bpp 32. More...
 
surface make_neutral_surface (const surface &surf)
 
surface create_neutral_surface (int w, int h)
 
surface stretch_surface_horizontal (const surface &surf, const unsigned w)
 Stretches a surface in the horizontal direction. More...
 
surface stretch_surface_vertical (const surface &surf, const unsigned h)
 Stretches a surface in the vertical direction. More...
 
surface scale_surface_xbrz (const surface &surf, 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 tile_surface (const surface &surf, int w, int h, bool centered=true)
 Tile a surface. More...
 
surface adjust_surface_color (const surface &surf, int r, int g, int b)
 
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)
 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, fixed_t amount)
 
surface get_surface_portion (const surface &surf, SDL_Rect &rect)
 Get a portion of the screen. More...
 
void adjust_surface_alpha (surface &surf, fixed_t amount)
 
surface adjust_surface_alpha_add (const surface &surf, int amount)
 
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. More...
 
bool in_mask_surface (const surface &surf, const surface &mask)
 Check if a surface fit into a mask. More...
 
surface submerge_alpha (const surface &surf, int depth, float alpha_base, float alpha_delta)
 Progressively reduce alpha of bottom part of the surface. More...
 
surface light_surface (const surface &surf, const surface &lightmap)
 Light surf using lightmap. More...
 
surface blur_surface (const surface &surf, int depth=1)
 Cross-fades a surface. More...
 
void blur_surface (surface &surf, SDL_Rect rect, int depth=1)
 Cross-fades a surface in place. More...
 
surface blur_alpha_surface (const surface &surf, int depth=1)
 Cross-fades a surface with alpha channel. More...
 
surface cut_surface (const surface &surf, SDL_Rect const &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...
 
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 create_compatible_surface (const surface &surf, int width=-1, int height=-1)
 
void blit_surface (const surface &src, const SDL_Rect *srcrect, surface &dst, const SDL_Rect *dstrect)
 Replacement for sdl_blit. More...
 
SDL_Rect get_non_transparent_portion (const surface &surf)
 
void put_pixel (const surface &surf, surface_lock &surf_lock, int x, int y, Uint32 pixel)
 Helper methods for setting/getting a single pixel in an image. More...
 
Uint32 get_pixel (const surface &surf, const const_surface_lock &surf_lock, int x, int y)
 
void draw_centered_on_background (surface surf, const SDL_Rect &rect, const color_t &color, surface target)
 

Enumeration Type Documentation

enum channel
Enumerator
RED 
GREEN 
BLUE 
ALPHA 

Definition at line 161 of file utils.hpp.

Function Documentation

void adjust_surface_alpha ( surface surf,
fixed_t  amount 
)
surface adjust_surface_alpha_add ( const surface surf,
int  amount 
)

Definition at line 1171 of file utils.cpp.

References b, g, make_neutral_surface(), and surface_locker< T >::pixels().

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

surface adjust_surface_color ( const surface surf,
int  r,
int  g,
int  b 
)
surface alpha_to_greyscale ( const surface surf)
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 1768 of file utils.cpp.

References a, color_t::b, b, g, color_t::g, make_neutral_surface(), surface_locker< T >::pixels(), and color_t::r.

Referenced by blend_image(), image::blend_modification::operator()(), and display::render_image().

void blit_surface ( const surface src,
const SDL_Rect *  srcrect,
surface dst,
const SDL_Rect *  dstrect 
)

Replacement for sdl_blit.

sdl_blit has problems with blitting partly transparent surfaces so this is a replacement. It ignores the SDL_SRCALPHA and SDL_SRCCOLORKEY flags. src and dst will have the SDL_RLEACCEL flag removed. The return value of SDL_BlistSurface is normally ignored so no return value. The rectangles are const and will not be modified.

Precondition
src contains a valid canvas.
dst contains a valid neutral canvas.
The caller must make sure the src fits on the dst.
Parameters
srcThe surface to blit.
srcrectThe region of the surface to blit
dstThe surface to blit on.
dstrectThe offset to blit the surface on, only x and y are used.

Definition at line 2082 of file utils.cpp.

References is_neutral(), make_neutral_surface(), surface_locker< T >::pixels(), and w.

Referenced by gui2::image_shape::draw(), gui2::text_shape::draw(), and font::render_text().

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

Cross-fades a surface with alpha channel.

Todo:
FIXME: This is just an adapted copy-paste of the normal blur but with blur alpha channel too

Definition at line 1587 of file utils.cpp.

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

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

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

Cross-fades a surface.

Definition at line 1452 of file utils.cpp.

References blur_surface(), and make_neutral_surface().

Referenced by gui2::canvas::blit(), blur_surface(), and gui::dialog_frame::draw_background().

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 1471 of file utils.cpp.

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

surface brighten_image ( const surface surf,
fixed_t  amount 
)
surface create_compatible_surface ( const surface surf,
int  width = -1,
int  height = -1 
)
surface create_neutral_surface ( int  w,
int  h 
)
surface cut_surface ( const surface surf,
SDL_Rect const &  r 
)

Cuts a rectangle from a surface.

Definition at line 1710 of file utils.cpp.

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

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

void draw_centered_on_background ( surface  surf,
const SDL_Rect &  rect,
const color_t color,
surface  target 
)

Definition at line 2347 of file utils.cpp.

References color_t::a, color_t::b, sdl::fill_surface_rect(), color_t::g, color_t::r, and sdl_blit().

Referenced by display::draw_minimap().

surface flip_surface ( const surface surf)
surface flop_surface ( const surface surf)
SDL_Rect get_non_transparent_portion ( const surface surf)

Definition at line 2278 of file utils.cpp.

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

Referenced by display::draw_image_for_report().

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

Definition at line 1903 of file utils.cpp.

References surface_locker< T >::pixels().

Referenced by rotate_any_surface().

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 2232 of file utils.cpp.

References create_compatible_surface(), and sdl_copy_portion().

Referenced by gui2::canvas::blit(), gui2::window::draw(), gui::dialog_frame::draw_background(), display::draw_image_for_report(), display::refresh_report(), halo::halo_impl::effect::render(), and surface_restorer::update().

surface greyscale_image ( const surface surf)
bool in_mask_surface ( const surface surf,
const surface mask 
)

Check if a surface fit into a mask.

Definition at line 1278 of file utils.cpp.

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

Referenced by image::is_in_hex().

bool is_neutral ( const surface surf)

Check that the surface is neutral bpp 32.

The surface may have an empty alpha channel.

Parameters
surfThe surface to test.
Returns
The status true if neutral, false if not.

Definition at line 42 of file utils.cpp.

References SDL_ALPHA_MASK, and SDL_RED_MASK.

Referenced by gui2::canvas::blit(), and blit_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 1386 of file utils.cpp.

References b, g, make_neutral_surface(), and surface_locker< T >::pixels().

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

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

Definition at line 761 of file utils.cpp.

References b, g, make_neutral_surface(), and surface_locker< T >::pixels().

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

surface negative_image ( const surface surf,
const int  thresholdR,
const int  thresholdG,
const int  thresholdB 
)
void put_pixel ( const surface surf,
surface_lock surf_lock,
int  x,
int  y,
Uint32  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.

Definition at line 1872 of file utils.cpp.

References surface_locker< T >::pixels().

Referenced by rotate_any_surface().

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 1081 of file utils.cpp.

References color_t::from_argb_bytes(), i, make_neutral_surface(), and surface_locker< T >::pixels().

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

surface rotate_180_surface ( const surface surf)

Rotates a surface 180 degrees.

Parameters
surfThe surface to rotate.
Returns
The rotated surface.

Definition at line 1926 of file utils.cpp.

References is_odd(), make_neutral_surface(), surface_locker< T >::pixels(), and swap().

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

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 1968 of file utils.cpp.

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

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

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 1815 of file utils.cpp.

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

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

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 262 of file utils.cpp.

References a, b, create_neutral_surface(), e, ftofxp, fxpdiv, fxptoi, g, h, make_neutral_surface(), n, surface_locker< T >::pixels(), s, and w.

Referenced by gui::dialog_frame::draw_border(), gui::tristate_button::draw_contents(), gui::scrollbar::draw_contents(), display::draw_image_for_report(), editor::item_palette::draw_item(), editor::unit_palette::draw_item(), editor::terrain_palette::draw_item(), draw_label(), gui::menu::imgsel_style::draw_row_bg(), gui::menu::style::get_item_image(), image::get_lighted_image(), gui::button::load_images(), image::light_modification::operator()(), rotate_any_surface(), image::select_algorithm(), editor::mouse_action_item::set_item_mouse_overlay(), editor::mouse_action_map_label::set_mouse_overlay(), editor::mouse_action_village::set_mouse_overlay(), editor::mouse_action_select::set_mouse_overlay(), editor::mouse_action_paste::set_mouse_overlay(), editor::mouse_action_starting_position::set_mouse_overlay(), editor::mouse_action::set_terrain_mouse_overlay(), and editor::mouse_action_unit::set_unit_mouse_overlay().

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 394 of file utils.cpp.

References a, b, preferences::editor::TransitionUpdateMode::count, create_neutral_surface(), ftofxp, fxpdiv, fxptoi, g, h, make_neutral_surface(), surface_locker< T >::pixels(), and w.

Referenced by gui2::image_shape::draw(), and image::scale_modification::operator()().

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 222 of file utils.cpp.

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

Referenced by image::select_algorithm().

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 534 of file utils.cpp.

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

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

surface scale_surface_xbrz ( const surface surf,
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 183 of file utils.cpp.

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

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

void sdl_blit ( const surface src,
SDL_Rect *  src_rect,
surface dst,
SDL_Rect *  dst_rect 
)
inline
void sdl_copy_portion ( const surface screen,
SDL_Rect *  screen_rect,
surface dst,
SDL_Rect *  dst_rect 
)
inline
version_info sdl_get_version ( )

Definition at line 35 of file utils.cpp.

Referenced by surface::free_surface(), CVideo::update_framebuffer(), and CVideo::~CVideo().

surface sepia_image ( const surface surf)

Definition at line 801 of file utils.cpp.

References b, g, make_neutral_surface(), and surface_locker< T >::pixels().

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

surface shadow_image ( const surface surf)

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

Definition at line 942 of file utils.cpp.

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

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

surface stretch_surface_horizontal ( const surface surf,
const unsigned  w 
)

Stretches a surface in the horizontal direction.

The stretches a surface it uses the first pixel in the horizontal direction of the original surface and copies that to the destination. This means only the first column of the original is used for the destination.

Parameters
surfThe source surface.
wThe width of the resulting surface.
Returns
A surface. returned.
Return values
0Returned upon error.
surfReturned if w == surf->w.

Definition at line 95 of file utils.cpp.

References create_neutral_surface(), make_neutral_surface(), surface_locker< T >::pixels(), and w.

Referenced by gui2::image_shape::draw().

surface stretch_surface_vertical ( const surface surf,
const unsigned  h 
)

Stretches a surface in the vertical direction.

The stretches a surface it uses the first pixel in the vertical direction of the original surface and copies that to the destination. This means only the first row of the original is used for the destination.

Parameters
surfThe source surface.
hThe height of the resulting surface.
Returns
A surface. returned.
Return values
surfReturned if h == surf->h.

Definition at line 140 of file utils.cpp.

References create_neutral_surface(), h, make_neutral_surface(), and surface_locker< T >::pixels().

Referenced by gui2::image_shape::draw().

surface submerge_alpha ( const surface surf,
int  depth,
float  alpha_base,
float  alpha_delta 
)

Progressively reduce alpha of bottom part of the surface.

Parameters
surfThe source surface.
depthThe height of the bottom part in pixels
alpha_baseThe alpha adjustment at the interface
alpha_deltaThe alpha adjustment reduction rate by pixel depth

Definition at line 1324 of file utils.cpp.

References a, b, d, ftofxp, fxpmult, g, make_neutral_surface(), and surface_locker< T >::pixels().

Referenced by display::render_image().

surface swap_channels_image ( const surface surf,
channel  r,
channel  g,
channel  b,
channel  a 
)
surface tile_surface ( const surface surf,
int  w,
int  h,
bool  centered = true 
)

Tile a surface.

Parameters
surfThe source surface.
wThe width of the resulting surface.
hThe height of the resulting surface.
centeredWhether to tile from the center outwards or from the top left (origin).
Returns
A surface containing the tiled version of the source.
Return values
0Returned upon error
surfReturned if w == surf->w and h == surf->h.

Definition at line 627 of file utils.cpp.

References create_neutral_surface(), h, i, make_neutral_surface(), surface_locker< T >::pixels(), sw, and w.

Referenced by gui2::image_shape::draw(), and draw_panel().

surface wipe_alpha ( const surface surf)