SDLImageScaler.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * SDLImageScaler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "SDLImageScaler.h"
  12. #include "SDL_Extensions.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../CMT.h"
  15. #include "../xBRZ/xbrz.h"
  16. #include <tbb/parallel_for.h>
  17. #include <SDL_surface.h>
  18. SDLImageOptimizer::SDLImageOptimizer(SDL_Surface * surf, const Rect & virtualDimensions)
  19. : surf(surf)
  20. , virtualDimensions(virtualDimensions)
  21. {
  22. }
  23. void SDLImageOptimizer::optimizeSurface(SDL_Surface * formatSourceSurface)
  24. {
  25. if (!surf)
  26. return;
  27. int left = surf->w;
  28. int top = surf->h;
  29. int right = 0;
  30. int bottom = 0;
  31. // locate fully-transparent area around image
  32. // H3 hadles this on format level, but mods or images scaled in runtime do not
  33. if (surf->format->palette)
  34. {
  35. for (int y = 0; y < surf->h; ++y)
  36. {
  37. const uint8_t * row = static_cast<uint8_t *>(surf->pixels) + y * surf->pitch;
  38. for (int x = 0; x < surf->w; ++x)
  39. {
  40. if (row[x] != 0)
  41. {
  42. // opaque or can be opaque (e.g. disabled shadow)
  43. top = std::min(top, y);
  44. left = std::min(left, x);
  45. right = std::max(right, x);
  46. bottom = std::max(bottom, y);
  47. }
  48. }
  49. }
  50. }
  51. else
  52. {
  53. for (int y = 0; y < surf->h; ++y)
  54. {
  55. for (int x = 0; x < surf->w; ++x)
  56. {
  57. ColorRGBA color;
  58. SDL_GetRGBA(CSDL_Ext::getPixel(surf, x, y), surf->format, &color.r, &color.g, &color.b, &color.a);
  59. if (color.a != SDL_ALPHA_TRANSPARENT)
  60. {
  61. // opaque
  62. top = std::min(top, y);
  63. left = std::min(left, x);
  64. right = std::max(right, x);
  65. bottom = std::max(bottom, y);
  66. }
  67. }
  68. }
  69. }
  70. // empty image
  71. if (left == surf->w)
  72. return;
  73. if (left != 0 || top != 0 || right != surf->w - 1 || bottom != surf->h - 1)
  74. {
  75. // non-zero border found
  76. Rect newDimensions(left, top, right - left + 1, bottom - top + 1);
  77. SDL_Rect rectSDL = CSDL_Ext::toSDL(newDimensions);
  78. auto newSurface = CSDL_Ext::newSurface(newDimensions.dimensions(), formatSourceSurface);
  79. SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE);
  80. SDL_BlitSurface(surf, &rectSDL, newSurface, nullptr);
  81. if (SDL_HasColorKey(surf))
  82. {
  83. uint32_t colorKey;
  84. SDL_GetColorKey(surf, &colorKey);
  85. SDL_SetColorKey(newSurface, SDL_TRUE, colorKey);
  86. }
  87. output = newSurface;
  88. virtualDimensions.x += left;
  89. virtualDimensions.y += top;
  90. }
  91. else
  92. {
  93. output = surf;
  94. output->refcount += 1;
  95. }
  96. }
  97. SDL_Surface * SDLImageOptimizer::acquireResultSurface()
  98. {
  99. SDL_Surface * result = output;
  100. output = nullptr;
  101. return result;
  102. }
  103. const Rect & SDLImageOptimizer::getResultDimensions() const
  104. {
  105. return virtualDimensions;
  106. }
  107. void SDLImageScaler::scaleSurface(Point targetDimensions, EScalingAlgorithm algorithm)
  108. {
  109. if (!intermediate)
  110. return; // may happen on scaling of empty images
  111. if(!targetDimensions.x || !targetDimensions.y)
  112. throw std::runtime_error("invalid scaling dimensions!");
  113. Point inputSurfaceSize(intermediate->w, intermediate->h);
  114. Point outputSurfaceSize = targetDimensions * inputSurfaceSize / virtualDimensionsInput.dimensions();
  115. Point outputMargins = targetDimensions * virtualDimensionsInput.topLeft() / virtualDimensionsInput.dimensions();
  116. // TODO: use xBRZ if possible? E.g. when scaling to 150% do 100% -> 200% via xBRZ and then linear downscale 200% -> 150%?
  117. // Need to investigate which is optimal for performance and for visuals
  118. ret = CSDL_Ext::newSurface(Point(outputSurfaceSize.x, outputSurfaceSize.y), intermediate);
  119. virtualDimensionsOutput = Rect(outputMargins, targetDimensions); // TODO: account for input virtual size
  120. const uint32_t * srcPixels = static_cast<const uint32_t*>(intermediate->pixels);
  121. uint32_t * dstPixels = static_cast<uint32_t*>(ret->pixels);
  122. if (algorithm == EScalingAlgorithm::NEAREST)
  123. xbrz::nearestNeighborScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  124. else
  125. xbrz::bilinearScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  126. }
  127. void SDLImageScaler::scaleSurfaceIntegerFactor(int factor, EScalingAlgorithm algorithm)
  128. {
  129. if (!intermediate)
  130. return; // may happen on scaling of empty images
  131. if(factor == 0)
  132. throw std::runtime_error("invalid scaling factor!");
  133. int newWidth = intermediate->w * factor;
  134. int newHight = intermediate->h * factor;
  135. virtualDimensionsOutput = virtualDimensionsInput * factor;
  136. ret = CSDL_Ext::newSurface(Point(newWidth, newHight), intermediate);
  137. assert(intermediate->pitch == intermediate->w * 4);
  138. assert(ret->pitch == ret->w * 4);
  139. const uint32_t * srcPixels = static_cast<const uint32_t*>(intermediate->pixels);
  140. uint32_t * dstPixels = static_cast<uint32_t*>(ret->pixels);
  141. switch (algorithm)
  142. {
  143. case EScalingAlgorithm::NEAREST:
  144. xbrz::nearestNeighborScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  145. break;
  146. case EScalingAlgorithm::BILINEAR:
  147. xbrz::bilinearScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  148. break;
  149. case EScalingAlgorithm::XBRZ_ALPHA:
  150. case EScalingAlgorithm::XBRZ_OPAQUE:
  151. {
  152. auto format = algorithm == EScalingAlgorithm::XBRZ_OPAQUE ? xbrz::ColorFormat::ARGB_CLAMPED : xbrz::ColorFormat::ARGB;
  153. if(intermediate->h < 32)
  154. {
  155. // for tiny images tbb incurs too high overhead
  156. xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, format, {});
  157. }
  158. else
  159. {
  160. // xbrz recommends granulation of 16, but according to tests, for smaller images granulation of 4 is actually the best option
  161. const int granulation = intermediate->h > 400 ? 16 : 4;
  162. tbb::parallel_for(tbb::blocked_range<size_t>(0, intermediate->h, granulation), [this, factor, srcPixels, dstPixels, format](const tbb::blocked_range<size_t> & r)
  163. {
  164. xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, format, {}, r.begin(), r.end());
  165. });
  166. }
  167. break;
  168. }
  169. default:
  170. throw std::runtime_error("invalid scaling algorithm!");
  171. }
  172. }
  173. SDLImageScaler::SDLImageScaler(SDL_Surface * surf)
  174. :SDLImageScaler(surf, Rect(0,0,surf->w, surf->h), false)
  175. {
  176. }
  177. SDLImageScaler::SDLImageScaler(SDL_Surface * surf, const Rect & virtualDimensions, bool optimizeImage)
  178. {
  179. if (optimizeImage)
  180. {
  181. SDLImageOptimizer optimizer(surf, virtualDimensions);
  182. optimizer.optimizeSurface(screen);
  183. intermediate = optimizer.acquireResultSurface();
  184. virtualDimensionsInput = optimizer.getResultDimensions();
  185. }
  186. else
  187. {
  188. intermediate = surf;
  189. intermediate->refcount += 1;
  190. virtualDimensionsInput = virtualDimensions;
  191. }
  192. if (intermediate == surf)
  193. {
  194. SDL_FreeSurface(intermediate);
  195. intermediate = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_ARGB8888, 0);
  196. }
  197. if (intermediate == surf)
  198. throw std::runtime_error("Scaler uses same surface as input!");
  199. }
  200. SDLImageScaler::~SDLImageScaler()
  201. {
  202. GH.dispatchMainThread([surface = intermediate]()
  203. {
  204. // potentially SDL bug, execute SDL_FreeSurface in main thread to avoid thread races to its internal state
  205. // may be fixed somewhere between 2.26.5 - 2.30
  206. SDL_FreeSurface(surface);
  207. });
  208. }
  209. SDL_Surface * SDLImageScaler::acquireResultSurface()
  210. {
  211. SDL_Surface * result = ret;
  212. ret = nullptr;
  213. return result;
  214. }
  215. const Rect & SDLImageScaler::getResultDimensions() const
  216. {
  217. return virtualDimensionsOutput;
  218. }