SDLImageScaler.cpp 6.9 KB

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