SDLImageScaler.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 (!intermediate)
  109. return; // may happen on scaling of empty images
  110. if(!targetDimensions.x || !targetDimensions.y)
  111. throw std::runtime_error("invalid scaling dimensions!");
  112. Point inputSurfaceSize(intermediate->w, intermediate->h);
  113. Point outputSurfaceSize = targetDimensions * inputSurfaceSize / virtualDimensionsInput.dimensions();
  114. Point outputMargins = targetDimensions * virtualDimensionsInput.topLeft() / virtualDimensionsInput.dimensions();
  115. // TODO: use xBRZ if possible? E.g. when scaling to 150% do 100% -> 200% via xBRZ and then linear downscale 200% -> 150%?
  116. // Need to investigate which is optimal for performance and for visuals
  117. ret = CSDL_Ext::newSurface(Point(outputSurfaceSize.x, outputSurfaceSize.y), intermediate);
  118. virtualDimensionsOutput = Rect(outputMargins, targetDimensions); // TODO: account for input virtual size
  119. const uint32_t * srcPixels = static_cast<const uint32_t*>(intermediate->pixels);
  120. uint32_t * dstPixels = static_cast<uint32_t*>(ret->pixels);
  121. if (algorithm == EScalingAlgorithm::NEAREST)
  122. xbrz::nearestNeighborScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  123. else
  124. xbrz::bilinearScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  125. }
  126. void SDLImageScaler::scaleSurfaceIntegerFactor(int factor, EScalingAlgorithm algorithm)
  127. {
  128. if (!intermediate)
  129. return; // may happen on scaling of empty images
  130. if(factor == 0)
  131. throw std::runtime_error("invalid scaling factor!");
  132. int newWidth = intermediate->w * factor;
  133. int newHight = intermediate->h * factor;
  134. virtualDimensionsOutput = virtualDimensionsInput * factor;
  135. ret = CSDL_Ext::newSurface(Point(newWidth, newHight), intermediate);
  136. assert(intermediate->pitch == intermediate->w * 4);
  137. assert(ret->pitch == ret->w * 4);
  138. const uint32_t * srcPixels = static_cast<const uint32_t*>(intermediate->pixels);
  139. uint32_t * dstPixels = static_cast<uint32_t*>(ret->pixels);
  140. switch (algorithm)
  141. {
  142. case EScalingAlgorithm::NEAREST:
  143. xbrz::nearestNeighborScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  144. break;
  145. case EScalingAlgorithm::BILINEAR:
  146. xbrz::bilinearScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  147. break;
  148. case EScalingAlgorithm::XBRZ_ALPHA:
  149. case EScalingAlgorithm::XBRZ_OPAQUE:
  150. {
  151. auto format = algorithm == EScalingAlgorithm::XBRZ_OPAQUE ? xbrz::ColorFormat::ARGB_CLAMPED : xbrz::ColorFormat::ARGB;
  152. if(intermediate->h < 32)
  153. {
  154. // for tiny images tbb incurs too high overhead
  155. xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, format, {});
  156. }
  157. else
  158. {
  159. // xbrz recommends granulation of 16, but according to tests, for smaller images granulation of 4 is actually the best option
  160. const int granulation = intermediate->h > 400 ? 16 : 4;
  161. tbb::parallel_for(tbb::blocked_range<size_t>(0, intermediate->h, granulation), [this, factor, srcPixels, dstPixels, format](const tbb::blocked_range<size_t> & r)
  162. {
  163. xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, format, {}, r.begin(), r.end());
  164. });
  165. }
  166. break;
  167. }
  168. default:
  169. throw std::runtime_error("invalid scaling algorithm!");
  170. }
  171. }
  172. SDLImageScaler::SDLImageScaler(SDL_Surface * surf)
  173. :SDLImageScaler(surf, Rect(0,0,surf->w, surf->h), false)
  174. {
  175. }
  176. SDLImageScaler::SDLImageScaler(SDL_Surface * surf, const Rect & virtualDimensions, bool optimizeImage)
  177. {
  178. if (optimizeImage)
  179. {
  180. SDLImageOptimizer optimizer(surf, virtualDimensions);
  181. optimizer.optimizeSurface(screen);
  182. intermediate = optimizer.acquireResultSurface();
  183. virtualDimensionsInput = optimizer.getResultDimensions();
  184. }
  185. else
  186. {
  187. intermediate = surf;
  188. intermediate->refcount += 1;
  189. virtualDimensionsInput = virtualDimensions;
  190. }
  191. if (intermediate == surf)
  192. {
  193. SDL_FreeSurface(intermediate);
  194. intermediate = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_ARGB8888, 0);
  195. }
  196. }
  197. SDLImageScaler::~SDLImageScaler()
  198. {
  199. SDL_FreeSurface(intermediate);
  200. SDL_FreeSurface(ret);
  201. }
  202. SDL_Surface * SDLImageScaler::acquireResultSurface()
  203. {
  204. SDL_Surface * result = ret;
  205. ret = nullptr;
  206. return result;
  207. }
  208. const Rect & SDLImageScaler::getResultDimensions() const
  209. {
  210. return virtualDimensionsOutput;
  211. }