SDLImageScaler.cpp 6.6 KB

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