The Battle for Wesnoth  1.19.12+dev
picture.cpp
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 /**
17  * @file
18  * Routines for images: load, scale, re-color, etc.
19  */
20 
21 #include "picture.hpp"
22 
23 #include "filesystem.hpp"
24 #include "game_config.hpp"
25 #include "image_modifications.hpp"
26 #include "log.hpp"
27 #include "serialization/base64.hpp"
29 #include "sdl/rect.hpp"
30 #include "sdl/texture.hpp"
31 
32 #include <SDL2/SDL_image.h>
33 
34 #include <boost/algorithm/string.hpp>
35 
36 #if BOOST_VERSION >= 108100
37 
38 #include <boost/unordered/unordered_flat_map.hpp>
39 template<typename Key, typename Value>
40 using cache_map = boost::unordered_flat_map<Key, Value>;
41 
42 #else
43 
44 #include <boost/unordered/unordered_map.hpp>
45 template<typename Key, typename Value>
46 using cache_map = boost::unordered_map<Key, Value>;
47 
48 #endif
49 
50 #include <array>
51 #include <set>
52 
53 static lg::log_domain log_image("image");
54 #define ERR_IMG LOG_STREAM(err, log_image)
55 #define WRN_IMG LOG_STREAM(warn, log_image)
56 #define LOG_IMG LOG_STREAM(info, log_image)
57 #define DBG_IMG LOG_STREAM(debug, log_image)
58 
59 static lg::log_domain log_config("config");
60 #define ERR_CFG LOG_STREAM(err, log_config)
61 
63 
64 namespace image
65 {
66 /** Hash function overload for boost::hash. Must be in the image namespace to satisfy ADL. */
67 std::size_t hash_value(const image::locator& val)
68 {
69  std::size_t hash = std::hash<unsigned>{}(val.type_);
70 
72  boost::hash_combine(hash, val.filename_);
73  }
74 
75  if(val.type_ == image::locator::SUB_FILE) {
76  boost::hash_combine(hash, val.loc_.x);
77  boost::hash_combine(hash, val.loc_.y);
78  boost::hash_combine(hash, val.center_x_);
79  boost::hash_combine(hash, val.center_y_);
80  boost::hash_combine(hash, val.modifications_);
81  }
82 
83  return hash;
84 }
85 
86 template<typename T>
88 {
89 public:
90  bool in_cache(const locator& item) const
91  {
92  return content_.find(item) != content_.end(); // TODO C++20: use content_.contains()
93  }
94 
95  /** Returns a pointer to the cached value, or nullptr if not found. */
96  const T* locate_in_cache(const locator& item) const
97  {
98  if(auto iter = content_.find(item); iter != content_.end()) {
99  return &iter->second;
100  } else {
101  return nullptr;
102  }
103  }
104 
105  /**
106  * Returns a reference to the cache item associated with the given key.
107  * If no corresponding value is found, a default instance will be created.
108  */
109  T& access_in_cache(const locator& item)
110  {
111  return content_[item];
112  }
113 
114  void add_to_cache(const locator& item, T data)
115  {
116  content_.insert_or_assign(item, std::move(data));
117  }
118 
119  void flush()
120  {
121  content_.clear();
122  }
123 
124 private:
126 };
127 
128 namespace
129 {
130 using surface_cache = cache_type<surface>;
131 using texture_cache = cache_type<texture>;
132 using bool_cache = cache_type<bool>;
133 
134 /** Type used to pair light possibilities with the corresponding lit surface. */
135 using lit_surface_variants = cache_map<std::vector<light_adjust>, surface>;
136 using lit_texture_variants = cache_map<std::vector<light_adjust>, texture>;
137 
138 /** Lit variants for each locator. */
139 using lit_surface_cache = cache_type<lit_surface_variants>;
140 using lit_texture_cache = cache_type<lit_texture_variants>;
141 
142 /** Definition of all image maps */
143 std::array<surface_cache, NUM_TYPES> surfaces_;
144 
145 /**
146  * Texture caches.
147  * Note that the latter two are temporary and should be removed once we have OGL and shader support.
148  */
149 using texture_cache_map = std::map<image::scale_quality, image::texture_cache>;
150 
151 texture_cache_map textures_;
152 texture_cache_map textures_hexed_;
153 texture_cache_map texture_tod_colored_;
154 
155 // cache storing if each image fit in a hex
156 image::bool_cache in_hex_info_;
157 
158 // cache storing if this is an empty hex
159 image::bool_cache is_empty_hex_;
160 
161 // caches storing the different lighted cases for each image
162 image::lit_surface_cache lit_surfaces_;
163 image::lit_texture_cache lit_textures_;
164 // caches storing each lightmap generated
165 image::lit_surface_variants surface_lightmaps_;
166 image::lit_texture_variants texture_lightmaps_;
167 
168 // diagnostics for tracking skipped cache impact
169 std::array<bool_cache, NUM_TYPES> skipped_cache_;
170 int duplicate_loads_ = 0;
171 int total_loads_ = 0;
172 
173 // const int cache_version_ = 0;
174 
175 std::map<std::string, bool> image_existence_map;
176 
177 // directories where we already cached file existence
178 std::set<std::string> precached_dirs;
179 
180 int red_adjust = 0, green_adjust = 0, blue_adjust = 0;
181 
182 const std::string data_uri_prefix = "data:";
183 struct parsed_data_URI{
184  explicit parsed_data_URI(std::string_view data_URI);
185  std::string_view scheme;
186  std::string_view mime;
187  std::string_view base64;
188  std::string_view data;
189  bool good;
190 };
191 parsed_data_URI::parsed_data_URI(std::string_view data_URI)
192 {
193  const std::size_t colon = data_URI.find(':');
194  const std::string_view after_scheme = data_URI.substr(colon + 1);
195 
196  const std::size_t comma = after_scheme.find(',');
197  const std::string_view type_info = after_scheme.substr(0, comma);
198 
199  const std::size_t semicolon = type_info.find(';');
200 
201  scheme = data_URI.substr(0, colon);
202  base64 = type_info.substr(semicolon + 1);
203  mime = type_info.substr(0, semicolon);
204  data = after_scheme.substr(comma + 1);
205  good = (scheme == "data" && base64 == "base64" && mime.length() > 0 && data.length() > 0);
206 }
207 
208 } // end anon namespace
209 
211 {
212  for(surface_cache& cache : surfaces_) {
213  cache.flush();
214  }
215  lit_surfaces_.flush();
216  lit_textures_.flush();
217  surface_lightmaps_.clear();
218  texture_lightmaps_.clear();
219  in_hex_info_.flush();
220  is_empty_hex_.flush();
221  textures_.clear();
222  textures_hexed_.clear();
223  texture_tod_colored_.clear();
224  image_existence_map.clear();
225  precached_dirs.clear();
226 }
227 
228 locator locator::clone(const std::string& mods) const
229 {
230  locator res = *this;
231  if(!mods.empty()) {
232  res.modifications_ += mods;
233  res.type_ = SUB_FILE;
234  }
235 
236  return res;
237 }
238 
239 std::ostream& operator<<(std::ostream& s, const locator& l)
240 {
241  s << l.get_filename();
242  if(!l.get_modifications().empty()) {
243  if(l.get_modifications()[0] != '~') {
244  s << '~';
245  }
246  s << l.get_modifications();
247  }
248  return s;
249 }
250 
251 locator::locator(const std::string& fn)
252  : filename_(fn)
253 {
254  if(filename_.empty()) {
255  return;
256  }
257 
258  if(boost::algorithm::starts_with(filename_, data_uri_prefix)) {
259  if(parsed_data_URI parsed{ filename_ }; !parsed.good) {
260  std::string_view view{ filename_ };
261  std::string_view stripped = view.substr(0, view.find(","));
262  ERR_IMG << "Invalid data URI: " << stripped;
263  }
264 
265  is_data_uri_ = true;
266  }
267 
268  if(const std::size_t markup_field = filename_.find('~'); markup_field != std::string::npos) {
269  type_ = SUB_FILE;
270  modifications_ = filename_.substr(markup_field, filename_.size() - markup_field);
271  filename_ = filename_.substr(0, markup_field);
272  } else {
273  type_ = FILE;
274  }
275 }
276 
277 locator::locator(const std::string& filename, const std::string& modifications)
278  : type_(SUB_FILE)
280  , modifications_(modifications)
281 {
282 }
283 
285  const std::string& filename,
286  const map_location& loc,
287  int center_x,
288  int center_y,
289  const std::string& modifications)
290  : type_(SUB_FILE)
292  , modifications_(modifications)
293  , loc_(loc)
294  , center_x_(center_x)
295  , center_y_(center_y)
296 {
297 }
298 
299 bool locator::operator==(const locator& a) const
300 {
301  if(a.type_ != type_) {
302  return false;
303  } else if(type_ == FILE) {
304  return filename_ == a.filename_;
305  } else if(type_ == SUB_FILE) {
306  return std::tie(filename_, loc_, modifications_, center_x_, center_y_) ==
307  std::tie(a.filename_, a.loc_, a.modifications_, a.center_x_, a.center_y_);
308  }
309 
310  return false;
311 }
312 
313 bool locator::operator<(const locator& a) const
314 {
315  if(type_ != a.type_) {
316  return type_ < a.type_;
317  } else if(type_ == FILE) {
318  return filename_ < a.filename_;
319  } else if(type_ == SUB_FILE) {
320  return std::tie(filename_, loc_, modifications_, center_x_, center_y_) <
321  std::tie(a.filename_, a.loc_, a.modifications_, a.center_x_, a.center_y_);
322  }
323 
324  return false;
325 }
326 
327 // Load overlay image and compose it with the original surface.
328 static void add_localized_overlay(const std::string& ovr_file, surface& orig_surf)
329 {
331  surface ovr_surf = IMG_Load_RW(rwops.release(), true); // SDL takes ownership of rwops
332  if(!ovr_surf) {
333  return;
334  }
335 
336  SDL_Rect area {0, 0, ovr_surf->w, ovr_surf->h};
337 
338  sdl_blit(ovr_surf, nullptr, orig_surf, &area);
339 }
340 
342 {
343  surface res;
344  const std::string& name = loc.get_filename();
345 
346  auto location = filesystem::get_binary_file_location("images", name);
347 
348  // Many images have been converted from PNG to WEBP format,
349  // but the old filename may still be saved in savegame files etc.
350  // If the file does not exist in ".png" format, also try ".webp".
351  // Similarly for ".jpg", which conveniently has the same number of letters as ".png".
352  if(!location && (boost::algorithm::ends_with(name, ".png") || boost::algorithm::ends_with(name, ".jpg"))) {
353  std::string webp_name = name.substr(0, name.size() - 4) + ".webp";
354  location = filesystem::get_binary_file_location("images", webp_name);
355  if(location) {
356  WRN_IMG << "Replaced missing '" << name << "' with found '"
357  << webp_name << "'.";
358  }
359  }
360 
361  {
362  if(location) {
363  // Check if there is a localized image.
364  const auto loc_location = filesystem::get_localized_path(location.value());
365  if(loc_location) {
366  location = loc_location.value();
367  }
368 
369  filesystem::rwops_ptr rwops = filesystem::make_read_RWops(location.value());
370  res = IMG_Load_RW(rwops.release(), true); // SDL takes ownership of rwops
371 
372  // If there was no standalone localized image, check if there is an overlay.
373  if(res && !loc_location) {
374  const auto ovr_location = filesystem::get_localized_path(location.value(), "--overlay");
375  if(ovr_location) {
376  add_localized_overlay(ovr_location.value(), res);
377  }
378  }
379  }
380  }
381 
382  if(!res && !name.empty()) {
383  ERR_IMG << "could not open image '" << name << "'";
386  if(name != game_config::images::blank)
388  }
389 
390  return res;
391 }
392 
394 {
395  // Create a new surface in-memory on which to apply the modifications
396  surface surf = get_surface(loc.get_filename(), UNSCALED).clone();
397  if(surf == nullptr) {
398  return nullptr;
399  }
400 
401  modification_queue mods = modification::decode(loc.get_modifications());
402 
403  while(!mods.empty()) {
404  try {
405  std::invoke(mods.top(), surf);
406  } catch(const image::modification::imod_exception& e) {
407  std::ostringstream ss;
408  ss << "\n";
409 
410  for(const std::string& mod_name : utils::parenthetical_split(loc.get_modifications(), '~')) {
411  ss << "\t" << mod_name << "\n";
412  }
413 
414  ERR_CFG << "Failed to apply a modification to an image:\n"
415  << "Image: " << loc.get_filename() << "\n"
416  << "Modifications: " << ss.str() << "\n"
417  << "Error: " << e.message;
418  }
419 
420  mods.pop();
421  }
422 
423  if(loc.get_loc().valid()) {
424  rect srcrect(
425  ((tile_size * 3) / 4) * loc.get_loc().x,
426  tile_size * loc.get_loc().y + (tile_size / 2) * (loc.get_loc().x % 2),
427  tile_size,
428  tile_size
429  );
430 
431  if(loc.get_center_x() >= 0 && loc.get_center_y() >= 0) {
432  srcrect.x += surf->w / 2 - loc.get_center_x();
433  srcrect.y += surf->h / 2 - loc.get_center_y();
434  }
435 
436  // cut and hex mask, but also check and cache if empty result
437  surface cut = cut_surface(surf, srcrect);
438  bool is_empty = false;
439  mask_surface(cut, get_hexmask(), &is_empty);
440 
441  // discard empty images to free memory
442  if(is_empty) {
443  // Safe because those images are only used by terrain rendering
444  // and it filters them out.
445  // A safer and more general way would be to keep only one copy of it
446  surf = nullptr;
447  } else {
448  surf = cut;
449  }
450 
451  is_empty_hex_.add_to_cache(loc, is_empty);
452  }
453 
454  return surf;
455 }
456 
458 {
459  surface surf;
460 
461  parsed_data_URI parsed{loc.get_filename()};
462 
463  if(!parsed.good) {
464  std::string_view fn = loc.get_filename();
465  std::string_view stripped = fn.substr(0, fn.find(","));
466  ERR_IMG << "Invalid data URI: " << stripped;
467  } else if(parsed.mime.substr(0, 5) != "image") {
468  ERR_IMG << "Data URI not of image MIME type: " << parsed.mime;
469  } else {
470  const std::vector<uint8_t> image_data = base64::decode(parsed.data);
471  filesystem::rwops_ptr rwops{SDL_RWFromConstMem(image_data.data(), image_data.size())};
472 
473  if(image_data.empty()) {
474  ERR_IMG << "Invalid encoding in data URI";
475  } else if(parsed.mime == "image/png") {
476  surf = IMG_LoadPNG_RW(rwops.release());
477  } else if(parsed.mime == "image/jpeg") {
478  surf = IMG_LoadJPG_RW(rwops.release());
479  } else if(parsed.mime == "image/webp") {
480  surf = IMG_LoadWEBP_RW(rwops.release());
481  } else {
482  ERR_IMG << "Invalid image MIME type: " << parsed.mime;
483  }
484  }
485 
486  return surf;
487 }
488 
489 light_adjust::light_adjust(int op, int rr, int gg, int bb)
490  : l(op), r(), g(), b()
491 {
492  constexpr int min = std::numeric_limits<int8_t>::min();
493  constexpr int max = std::numeric_limits<int8_t>::max();
494 
495  // Clamp in a wider type (int) to avoid truncating the input value prematurely
496  r = std::clamp(rr / 2, min, max);
497  g = std::clamp(gg / 2, min, max);
498  b = std::clamp(bb / 2, min, max);
499 }
500 
501 #ifndef __cpp_impl_three_way_comparison
503 {
504  return std::tie(l, r, g, b) == std::tie(o.l, o.r, o.g, o.b);
505 }
506 #endif
507 
508 /** Hash function overload for boost::hash. Must be in the image namespace to satisfy ADL. */
509 static std::size_t hash_value(const light_adjust& adj)
510 {
511  std::size_t hash{0};
512 
513  boost::hash_combine(hash, adj.l);
514  boost::hash_combine(hash, adj.r);
515  boost::hash_combine(hash, adj.g);
516  boost::hash_combine(hash, adj.b);
517 
518  return hash;
519 }
520 
521 static surface apply_light(surface surf, const std::vector<light_adjust>& ls)
522 {
523  // atomic lightmap operation are handled directly (important to end recursion)
524  if(ls.size() == 1) {
525  // if no lightmap (first char = -1) then we need the initial value
526  //(before the halving done for lightmap)
527  int m = ls[0].l == -1 ? 2 : 1;
528  adjust_surface_color(surf, ls[0].r * m, ls[0].g * m, ls[0].b * m);
529  return surf;
530  }
531 
532  const auto get_lightmap = [&ls]
533  {
534  const auto iter = surface_lightmaps_.find(ls);
535  if(iter != surface_lightmaps_.end()) {
536  return iter->second;
537  }
538 
539  // build all the paths for lightmap sources
540  static const std::string p = "terrain/light/light";
541  static const std::string lm_img[19] {
542  p + ".png",
543  p + "-concave-2-tr.png", p + "-concave-2-r.png", p + "-concave-2-br.png",
544  p + "-concave-2-bl.png", p + "-concave-2-l.png", p + "-concave-2-tl.png",
545  p + "-convex-br-bl.png", p + "-convex-bl-l.png", p + "-convex-l-tl.png",
546  p + "-convex-tl-tr.png", p + "-convex-tr-r.png", p + "-convex-r-br.png",
547  p + "-convex-l-bl.png", p + "-convex-tl-l.png", p + "-convex-tr-tl.png",
548  p + "-convex-r-tr.png", p + "-convex-br-r.png", p + "-convex-bl-br.png"
549  };
550 
551  // first image will be the base onto which we blit the others
552  surface base;
553 
554  for(const light_adjust& adj : ls) {
555  // get the corresponding image and apply the lightmap operation to it
556  // This allows to also cache lightmap parts.
557  // note that we avoid infinite recursion by using only atomic operation
558  surface lts = image::get_lighted_image(lm_img[adj.l], std::vector{adj});
559 
560  if(base == nullptr) {
561  // copy the cached image to avoid modifying the cache
562  base = lts.clone();
563  } else {
564  sdl_blit(lts, nullptr, base, nullptr);
565  }
566  }
567 
568  // cache the result
569  surface_lightmaps_[ls] = base;
570  return base;
571  };
572 
573  // apply the final lightmap
574  light_surface(surf, get_lightmap());
575  return surf;
576 }
577 
579 {
580  switch(loc.get_type()) {
581  case locator::FILE:
582  if(loc.is_data_uri()){
583  return load_image_data_uri(loc);
584  } else {
585  return load_image_file(loc);
586  }
587  case locator::SUB_FILE:
588  return load_image_sub_file(loc);
589  default:
590  return surface(nullptr);
591  }
592 }
593 
595 {
596 }
597 
599 {
600  flush_cache();
601 }
602 
603 void set_color_adjustment(int r, int g, int b)
604 {
605  if(r != red_adjust || g != green_adjust || b != blue_adjust) {
606  red_adjust = r;
607  green_adjust = g;
608  blue_adjust = b;
609  surfaces_[TOD_COLORED].flush();
610  lit_surfaces_.flush();
611  lit_textures_.flush();
612  texture_tod_colored_.clear();
613  }
614 }
615 
616 static surface get_hexed(const locator& i_locator, bool skip_cache = false)
617 {
618  surface image = get_surface(i_locator, UNSCALED, skip_cache).clone();
619  surface mask = get_hexmask();
620  // Ensure the image is the correct size by cropping and/or centering.
621  // TODO: this should probably be a function of sdl/utils
622  if(image && (image->w != mask->w || image->h != mask->h)) {
623  DBG_IMG << "adjusting [" << image->w << ',' << image->h << ']'
624  << " image to hex mask: " << i_locator;
625  // the fitted surface
626  surface fit(mask->w, mask->h);
627  // if the image is too large in either dimension, crop it.
628  if(image->w > mask->w || image->h >= mask->h) {
629  // fill the crop surface with transparency
630  SDL_FillRect(fit, nullptr, SDL_MapRGBA(fit->format, 0, 0, 0, 0));
631  // crop the input image to hexmask dimensions
632  int cutx = std::max(0, image->w - mask->w) / 2;
633  int cuty = std::max(0, image->h - mask->h) / 2;
634  int cutw = std::min(image->w, mask->w);
635  int cuth = std::min(image->h, mask->h);
636  image = cut_surface(image, {cutx, cuty, cutw, cuth});
637  // image will now have dimensions <= mask
638  }
639  // center image
640  int placex = (mask->w - image->w) / 2;
641  int placey = (mask->h - image->h) / 2;
642  rect dst = {placex, placey, image->w, image->h};
643  sdl_blit(image, nullptr, fit, &dst);
644  image = fit;
645  }
646  // hex cut tiles, also check and cache if empty result
647  bool is_empty = false;
648  mask_surface(image, mask, &is_empty, i_locator.get_filename());
649  is_empty_hex_.add_to_cache(i_locator, is_empty);
650  return image;
651 }
652 
653 static surface get_tod_colored(const locator& i_locator, bool skip_cache = false)
654 {
655  surface img = get_surface(i_locator, HEXED, skip_cache).clone();
656  adjust_surface_color(img, red_adjust, green_adjust, blue_adjust);
657  return img;
658 }
659 
660 /** translate type to a simpler one when possible */
661 static TYPE simplify_type(const image::locator& i_locator, TYPE type)
662 {
663  if(type == TOD_COLORED) {
664  if(red_adjust == 0 && green_adjust == 0 && blue_adjust == 0) {
665  type = HEXED;
666  }
667  }
668 
669  if(type == HEXED) {
670  // check if the image is already hex-cut by the location system
671  if(i_locator.get_loc().valid()) {
672  type = UNSCALED;
673  }
674  }
675 
676  return type;
677 }
678 
680  const image::locator& i_locator,
681  TYPE type,
682  bool skip_cache)
683 {
684  surface res;
685 
686  if(i_locator.is_void()) {
687  return res;
688  }
689 
690  type = simplify_type(i_locator, type);
691 
692  // select associated cache
693  if(type >= NUM_TYPES) {
694  WRN_IMG << "get_surface called with unknown image type";
695  return res;
696  }
697  surface_cache& imap = surfaces_[type];
698 
699  // return the image if already cached
700  if(const surface* cached_surf = imap.locate_in_cache(i_locator)) {
701  return *cached_surf;
702  } else {
703  DBG_IMG << "surface cache [" << type << "] miss: " << i_locator;
704  }
705 
706  // not cached, generate it
707  switch(type) {
708  case UNSCALED:
709  // If type is unscaled, directly load the image from the disk.
710  res = load_from_disk(i_locator);
711  break;
712  case TOD_COLORED:
713  res = get_tod_colored(i_locator, skip_cache);
714  break;
715  case HEXED:
716  res = get_hexed(i_locator, skip_cache);
717  break;
718  default:
719  throw game::error("get_surface somehow lost image type?");
720  }
721 
722  bool_cache& skip = skipped_cache_[type];
723 
724  // In cache...
725  if(const bool* cached_value = skip.locate_in_cache(i_locator)) {
726  // ... and cached as true
727  if(*cached_value) {
728  DBG_IMG << "duplicate load: " << i_locator
729  << " [" << type << "]"
730  << " (" << duplicate_loads_ << "/" << total_loads_ << " total)";
731  ++duplicate_loads_;
732  }
733  }
734 
735  ++total_loads_;
736 
737  if(skip_cache) {
738  DBG_IMG << "surface cache [" << type << "] skip: " << i_locator;
739  skip.add_to_cache(i_locator, true);
740  } else {
741  imap.add_to_cache(i_locator, res);
742  }
743 
744  return res;
745 }
746 
747 surface get_lighted_image(const image::locator& i_locator, const std::vector<light_adjust>& ls)
748 {
749  if(i_locator.is_void()) {
750  return {};
751  }
752 
753  lit_surface_variants& lvar = lit_surfaces_.access_in_cache(i_locator);
754 
755  // Check the matching list_string variants for this locator
756  if(const auto iter = lvar.find(ls); iter != lvar.end()) {
757  return iter->second;
758  } else {
759  DBG_IMG << "lit surface cache miss: " << i_locator;
760  }
761 
762  // not cached yet, generate it
763  surface res = apply_light(get_surface(i_locator, HEXED).clone(), ls);
764 
765  // record the lighted surface in the corresponding variants cache
766  lvar[ls] = res;
767  return res;
768 }
769 
770 texture get_lighted_texture(const image::locator& i_locator, const std::vector<light_adjust>& ls)
771 {
772  if(i_locator.is_void()) {
773  return texture();
774  }
775 
776  lit_texture_variants& lvar = lit_textures_.access_in_cache(i_locator);
777 
778  // Check the matching list_string variants for this locator
779  if(const auto iter = lvar.find(ls); iter != lvar.end()) {
780  return iter->second;
781  } else {
782  DBG_IMG << "lit texture cache miss: " << i_locator;
783  }
784 
785  // not cached yet, generate it
786  texture tex(get_lighted_image(i_locator, ls));
787 
788  // record the lighted texture in the corresponding variants cache
789  lvar[ls] = tex;
790  return tex;
791 }
792 
794 {
797 }
798 
799 point get_size(const locator& i_locator, bool skip_cache)
800 {
801  if(const surface s = get_surface(i_locator, UNSCALED, skip_cache)) {
802  return {s->w, s->h};
803  } else {
804  return {0, 0};
805  }
806 }
807 
808 bool is_in_hex(const locator& i_locator)
809 {
810  if(const bool* cached_value = in_hex_info_.locate_in_cache(i_locator)) {
811  return *cached_value;
812  } else {
813  bool res = in_mask_surface(get_surface(i_locator, UNSCALED), get_hexmask());
814  in_hex_info_.add_to_cache(i_locator, res);
815  return res;
816  }
817 }
818 
819 bool is_empty_hex(const locator& i_locator)
820 {
821  if(const bool* cached_value = is_empty_hex_.locate_in_cache(i_locator)) {
822  return *cached_value;
823  }
824 
825  surface surf = get_surface(i_locator, HEXED);
826 
827  // Empty state should be cached during surface fetch. Let's check again
828  if(const bool* cached_value = is_empty_hex_.locate_in_cache(i_locator)) {
829  return *cached_value;
830  }
831 
832  // Should never reach this point, but let's manually do it anyway.
833  surf = surf.clone();
834  bool is_empty = false;
835  mask_surface(surf, get_hexmask(), &is_empty);
836  is_empty_hex_.add_to_cache(i_locator, is_empty);
837  return is_empty;
838 }
839 
840 bool exists(const image::locator& i_locator)
841 {
842  if(i_locator.is_void()) {
843  return false;
844  }
845 
846  // The insertion will fail if there is already an element in the cache
847  // and this will point to the existing element.
848  auto [iter, success] = image_existence_map.emplace(i_locator.get_filename(), false);
849 
850  bool& cache = iter->second;
851  if(success) {
852  if(i_locator.is_data_uri()) {
853  cache = parsed_data_URI{i_locator.get_filename()}.good;
854  } else {
855  cache = filesystem::get_binary_file_location("images", i_locator.get_filename()).has_value();
856  }
857  }
858 
859  return cache;
860 }
861 
862 static void precache_file_existence_internal(const std::string& dir, const std::string& subdir)
863 {
864  const std::string checked_dir = dir + "/" + subdir;
865  if(precached_dirs.find(checked_dir) != precached_dirs.end()) {
866  return;
867  }
868 
869  precached_dirs.insert(checked_dir);
870 
871  if(!filesystem::is_directory(checked_dir)) {
872  return;
873  }
874 
875  std::vector<std::string> files_found;
876  std::vector<std::string> dirs_found;
877  filesystem::get_files_in_dir(checked_dir, &files_found, &dirs_found, filesystem::name_mode::FILE_NAME_ONLY,
879 
880  for(const auto& f : files_found) {
881  image_existence_map[subdir + f] = true;
882  }
883 
884  for(const auto& d : dirs_found) {
885  precache_file_existence_internal(dir, subdir + d + "/");
886  }
887 }
888 
889 void precache_file_existence(const std::string& subdir)
890 {
891  for(const auto& p : filesystem::get_binary_paths("images")) {
893  }
894 }
895 
896 bool precached_file_exists(const std::string& file)
897 {
898  const auto b = image_existence_map.find(file);
899  if(b != image_existence_map.end()) {
900  return b->second;
901  }
902 
903  return false;
904 }
905 
906 save_result save_image(const locator& i_locator, const std::string& filename)
907 {
908  return save_image(get_surface(i_locator), filename);
909 }
910 
911 save_result save_image(const surface& surf, const std::string& filename)
912 {
913  if(!surf) {
914  return save_result::no_image;
915  }
916 
917  if(boost::algorithm::ends_with(filename, ".jpeg") || boost::algorithm::ends_with(filename, ".jpg") || boost::algorithm::ends_with(filename, ".jpe")) {
918  LOG_IMG << "Writing a JPG image to " << filename;
919 
920  const int err = IMG_SaveJPG_RW(surf, filesystem::make_write_RWops(filename).release(), true, 75); // SDL takes ownership of the RWops
922  }
923 
924  if(boost::algorithm::ends_with(filename, ".png")) {
925  LOG_IMG << "Writing a PNG image to " << filename;
926 
927  const int err = IMG_SavePNG_RW(surf, filesystem::make_write_RWops(filename).release(), true); // SDL takes ownership of the RWops
929  }
930 
932 }
933 
934 /*
935  * TEXTURE INTERFACE ======================================================================
936  *
937  * The only important difference here is that textures must have their
938  * scale quality set before creation. All other handling is done by
939  * get_surface.
940  */
941 
942 texture get_texture(const image::locator& i_locator, TYPE type, bool skip_cache)
943 {
944  return get_texture(i_locator, scale_quality::nearest, type, skip_cache);
945 }
946 
947 /** Returns a texture for the corresponding image. */
948 texture get_texture(const image::locator& i_locator, scale_quality quality, TYPE type, bool skip_cache)
949 {
950  texture res;
951 
952  if(i_locator.is_void()) {
953  return res;
954  }
955 
956  type = simplify_type(i_locator, type);
957 
958  //
959  // Select the appropriate cache. We don't need caches for every single image types,
960  // since some types can be handled by render-time operations.
961  //
962  texture_cache* cache = nullptr;
963 
964  switch(type) {
965  case HEXED:
966  cache = &textures_hexed_[quality];
967  break;
968  case TOD_COLORED:
969  cache = &texture_tod_colored_[quality];
970  break;
971  default:
972  cache = &textures_[quality];
973  }
974 
975  //
976  // Now attempt to find a cached texture. If found, return it.
977  //
978  if(const texture* cached_texture = cache->locate_in_cache(i_locator)) {
979  return *cached_texture;
980  } else {
981  DBG_IMG << "texture cache [" << type << "] miss: " << i_locator;
982  }
983 
984  //
985  // No texture was cached. In that case, create a new one. The explicit cases require special
986  // handling with surfaces in order to generate the desired effect. This shouldn't be the case
987  // once we get OGL and shader support.
988  //
989 
990  // Get it from the surface cache, also setting the desired scale quality.
991  const bool linear_scaling = quality == scale_quality::linear ? true : false;
992  if(i_locator.get_modifications().empty()) {
993  // skip cache if we're loading plain files with no modifications
994  res = texture(get_surface(i_locator, type, true), linear_scaling);
995  } else {
996  res = texture(get_surface(i_locator, type, skip_cache), linear_scaling);
997  }
998 
999  // Cache the texture.
1000  if(skip_cache) {
1001  DBG_IMG << "texture cache [" << type << "] skip: " << i_locator;
1002  } else {
1003  cache->add_to_cache(i_locator, res);
1004  }
1005 
1006  return res;
1007 }
1008 
1009 } // end namespace image
std::string filename_
Definition: action_wml.cpp:534
map_location loc
Definition: move.cpp:172
double g
Definition: astarsearch.cpp:63
bool in_cache(const locator &item) const
Definition: picture.cpp:90
T & access_in_cache(const locator &item)
Returns a reference to the cache item associated with the given key.
Definition: picture.cpp:109
cache_map< locator, T > content_
Definition: picture.cpp:125
const T * locate_in_cache(const locator &item) const
Returns a pointer to the cached value, or nullptr if not found.
Definition: picture.cpp:96
void add_to_cache(const locator &item, T data)
Definition: picture.cpp:114
Generic locator abstracting the location of an image.
Definition: picture.hpp:59
bool is_void() const
Returns true if the locator does not correspond to an actual image.
Definition: picture.hpp:93
map_location loc_
Definition: picture.hpp:100
std::string filename_
Definition: picture.hpp:98
const std::string & get_filename() const
Definition: picture.hpp:82
bool is_data_uri() const
Definition: picture.hpp:83
const std::string & get_modifications() const
Definition: picture.hpp:87
bool is_data_uri_
Definition: picture.hpp:97
const map_location & get_loc() const
Definition: picture.hpp:84
locator::type type_
Definition: picture.hpp:96
locator()=default
bool operator<(const locator &a) const
Definition: picture.cpp:313
bool operator==(const locator &a) const
Definition: picture.cpp:299
locator clone(const std::string &mods) const
Returns a copy of this locator with the given IPF.
Definition: picture.cpp:228
std::string modifications_
Definition: picture.hpp:99
A modified priority queue used to order image modifications.
const modification & top() const
Returns a const reference to the top element in the queue.
void pop()
Removes the top element from the queue.
static modification_queue decode(const std::string &)
Decodes modifications from a modification string.
surface clone() const
Creates a new, duplicate surface in memory using the 'neutral' pixel format.
Definition: surface.cpp:97
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
Declarations for File-IO.
map_location loc_
Standard logging facilities (interface).
std::vector< uint8_t > decode(std::string_view in)
Definition: base64.cpp:221
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs, name_mode mode, filter_mode filter, reorder_mode reorder, file_tree_checksum *checksum)
Get a list of all files and/or directories in a given directory.
Definition: filesystem.cpp:459
std::unique_ptr< SDL_RWops, sdl_rwops_deleter > rwops_ptr
Definition: filesystem.hpp:61
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
rwops_ptr make_read_RWops(const std::string &path)
utils::optional< std::string > get_binary_file_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual file of a given type, if it exists.
utils::optional< std::string > get_localized_path(const std::string &file, const std::string &suff)
Returns the localized version of the given filename, if it exists.
rwops_ptr make_write_RWops(const std::string &path)
const std::vector< std::string > & get_binary_paths(const std::string &type)
Returns a vector with all possible paths to a given type of binary, e.g.
std::string terrain_mask
const bool & debug
Definition: game_config.cpp:95
unsigned int tile_size
Definition: game_config.cpp:55
Functions to load and save images from/to disk.
static surface load_image_sub_file(const image::locator &loc)
Definition: picture.cpp:393
bool is_empty_hex(const locator &i_locator)
Checks if an image is empty after hex masking.
Definition: picture.cpp:819
static void add_localized_overlay(const std::string &ovr_file, surface &orig_surf)
Definition: picture.cpp:328
bool precached_file_exists(const std::string &file)
Definition: picture.cpp:896
save_result
Definition: picture.hpp:271
static TYPE simplify_type(const image::locator &i_locator, TYPE type)
translate type to a simpler one when possible
Definition: picture.cpp:661
static surface load_image_data_uri(const image::locator &loc)
Definition: picture.cpp:457
static surface get_tod_colored(const locator &i_locator, bool skip_cache=false)
Definition: picture.cpp:653
bool exists(const image::locator &i_locator)
Returns true if the given image actually exists, without loading it.
Definition: picture.cpp:840
void flush_cache()
Purges all image caches.
Definition: picture.cpp:210
static void precache_file_existence_internal(const std::string &dir, const std::string &subdir)
Definition: picture.cpp:862
static surface load_from_disk(const locator &loc)
Definition: picture.cpp:578
surface get_surface(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image surface suitable for software manipulation.
Definition: picture.cpp:679
static surface apply_light(surface surf, const std::vector< light_adjust > &ls)
Definition: picture.cpp:521
surface get_hexmask()
Retrieves the standard hexagonal tile mask.
Definition: picture.cpp:793
std::ostream & operator<<(std::ostream &s, const locator &l)
Definition: picture.cpp:239
save_result save_image(const locator &i_locator, const std::string &filename)
Definition: picture.cpp:906
void precache_file_existence(const std::string &subdir)
Precache the existence of files in a binary path subdirectory (e.g.
Definition: picture.cpp:889
TYPE
Used to specify the rendering format of images.
Definition: picture.hpp:171
@ HEXED
Standard hexagonal tile mask applied, removing portions that don't fit.
Definition: picture.hpp:175
@ NUM_TYPES
Definition: picture.hpp:178
@ TOD_COLORED
Same as HEXED, but with Time of Day color tint applied.
Definition: picture.hpp:177
@ UNSCALED
Unmodified original-size image.
Definition: picture.hpp:173
texture get_texture(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image texture suitable for hardware-accelerated rendering.
Definition: picture.cpp:942
point get_size(const locator &i_locator, bool skip_cache)
Returns the width and height of an image.
Definition: picture.cpp:799
static surface get_hexed(const locator &i_locator, bool skip_cache=false)
Definition: picture.cpp:616
texture get_lighted_texture(const image::locator &i_locator, const std::vector< light_adjust > &ls)
Definition: picture.cpp:770
std::size_t hash_value(const image::locator &val)
Hash function overload for boost::hash.
Definition: picture.cpp:67
void set_color_adjustment(int r, int g, int b)
Changes Time of Day color tint for all applicable image types.
Definition: picture.cpp:603
surface get_lighted_image(const image::locator &i_locator, const std::vector< light_adjust > &ls)
Caches and returns an image with a lightmap applied to it.
Definition: picture.cpp:747
bool is_in_hex(const locator &i_locator)
Checks if an image fits into a single hex.
Definition: picture.cpp:808
scale_quality
Definition: picture.hpp:181
static surface load_image_file(const image::locator &loc)
Definition: picture.cpp:341
logger & err()
Definition: log.cpp:306
std::string img(const std::string &src, const std::string &align, bool floating)
Generates a Help markup tag corresponding to an image.
Definition: markup.cpp:31
std::vector< std::string > parenthetical_split(std::string_view val, const char separator, std::string_view left, std::string_view right, const int flags)
Splits a string based either on a separator, except then the text appears within specified parenthesi...
#define DBG_IMG
Definition: picture.cpp:57
static lg::log_domain log_image("image")
#define ERR_CFG
Definition: picture.cpp:60
std::string_view scheme
Definition: picture.cpp:185
boost::unordered_map< Key, Value > cache_map
Definition: picture.cpp:46
std::string_view mime
Definition: picture.cpp:186
bool good
Definition: picture.cpp:189
#define WRN_IMG
Definition: picture.cpp:55
std::string_view base64
Definition: picture.cpp:187
#define LOG_IMG
Definition: picture.cpp:56
#define ERR_IMG
Definition: picture.cpp:54
std::string_view data
Definition: picture.cpp:188
static lg::log_domain log_config("config")
Contains the SDL_Rect helper code.
rect dst
Location on the final composed sheet.
surface surf
Image.
std::string filename
Filename.
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
Type used to store color information of central and adjacent hexes.
Definition: picture.hpp:124
bool operator==(const light_adjust &o) const
Definition: picture.cpp:502
light_adjust(int op, int r, int g, int b)
Definition: picture.cpp:489
Exception thrown by the operator() when an error occurs.
Encapsulates the map of the game.
Definition: location.hpp:45
bool valid() const
Definition: location.hpp:110
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
mock_party p
static map_location::direction s
void mask_surface(surface &nsurf, const surface &nmask, bool *empty_result, const std::string &filename)
Applies a mask on a surface.
Definition: utils.cpp:706
void adjust_surface_color(surface &nsurf, int red, int green, int blue)
Definition: utils.cpp:405
surface cut_surface(const surface &surf, const SDL_Rect &r)
Cuts a rectangle from a surface.
Definition: utils.cpp:1100
void light_surface(surface &nsurf, const surface &lightmap)
Light surf using lightmap.
Definition: utils.cpp:798
bool in_mask_surface(const surface &nsurf, const surface &nmask)
Check if a surface fit into a mask.
Definition: utils.cpp:760
void sdl_blit(const surface &src, const SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
Definition: utils.hpp:44
#define d
#define e
#define f
#define b