2
0

SDLImage.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * SDLImage.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 "SDLImage.h"
  12. #include "SDLImageLoader.h"
  13. #include "SDL_Extensions.h"
  14. #include "../render/ColorFilter.h"
  15. #include "../render/Colors.h"
  16. #include "../render/CBitmapHandler.h"
  17. #include "../render/CDefFile.h"
  18. #include "../render/Graphics.h"
  19. #include "../xBRZ/xbrz.h"
  20. #include "../gui/CGuiHandler.h"
  21. #include "../render/IScreenHandler.h"
  22. #include <tbb/parallel_for.h>
  23. #include <SDL_surface.h>
  24. #include <SDL_image.h>
  25. class SDLImageLoader;
  26. int IImage::width() const
  27. {
  28. return dimensions().x;
  29. }
  30. int IImage::height() const
  31. {
  32. return dimensions().y;
  33. }
  34. SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group)
  35. : surf(nullptr),
  36. margins(0, 0),
  37. fullSize(0, 0),
  38. originalPalette(nullptr)
  39. {
  40. SDLImageLoader loader(this);
  41. data->loadFrame(frame, group, loader);
  42. savePalette();
  43. }
  44. SDLImageShared::SDLImageShared(SDL_Surface * from)
  45. : surf(nullptr),
  46. margins(0, 0),
  47. fullSize(0, 0),
  48. originalPalette(nullptr)
  49. {
  50. surf = from;
  51. if (surf == nullptr)
  52. return;
  53. savePalette();
  54. surf->refcount++;
  55. fullSize.x = surf->w;
  56. fullSize.y = surf->h;
  57. }
  58. SDLImageShared::SDLImageShared(const ImagePath & filename)
  59. : surf(nullptr),
  60. margins(0, 0),
  61. fullSize(0, 0),
  62. originalPalette(nullptr)
  63. {
  64. surf = BitmapHandler::loadBitmap(filename);
  65. if(surf == nullptr)
  66. {
  67. logGlobal->error("Error: failed to load image %s", filename.getOriginalName());
  68. return;
  69. }
  70. else
  71. {
  72. savePalette();
  73. fullSize.x = surf->w;
  74. fullSize.y = surf->h;
  75. optimizeSurface();
  76. }
  77. }
  78. void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const
  79. {
  80. if (!surf)
  81. return;
  82. Rect sourceRect(0, 0, surf->w, surf->h);
  83. Point destShift(0, 0);
  84. if(src)
  85. {
  86. if(src->x < margins.x)
  87. destShift.x += margins.x - src->x;
  88. if(src->y < margins.y)
  89. destShift.y += margins.y - src->y;
  90. sourceRect = Rect(*src).intersect(Rect(margins.x, margins.y, surf->w, surf->h));
  91. sourceRect -= margins;
  92. }
  93. else
  94. destShift = margins;
  95. destShift += dest;
  96. SDL_SetSurfaceColorMod(surf, colorMultiplier.r, colorMultiplier.g, colorMultiplier.b);
  97. SDL_SetSurfaceAlphaMod(surf, alpha);
  98. if (alpha != SDL_ALPHA_OPAQUE || (mode != EImageBlitMode::OPAQUE && surf->format->Amask != 0))
  99. SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
  100. else
  101. SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE);
  102. if (palette && surf->format->palette)
  103. SDL_SetSurfacePalette(surf, palette);
  104. if(surf->format->palette && mode != EImageBlitMode::OPAQUE && mode != EImageBlitMode::COLORKEY)
  105. {
  106. CSDL_Ext::blit8bppAlphaTo24bpp(surf, sourceRect, where, destShift, alpha);
  107. }
  108. else
  109. {
  110. CSDL_Ext::blitSurface(surf, sourceRect, where, destShift);
  111. }
  112. if (surf->format->palette)
  113. SDL_SetSurfacePalette(surf, originalPalette);
  114. }
  115. void SDLImageShared::optimizeSurface()
  116. {
  117. if (!surf)
  118. return;
  119. int left = surf->w;
  120. int top = surf->h;
  121. int right = 0;
  122. int bottom = 0;
  123. // locate fully-transparent area around image
  124. // H3 hadles this on format level, but mods or images scaled in runtime do not
  125. if (surf->format->palette)
  126. {
  127. for (int y = 0; y < surf->h; ++y)
  128. {
  129. const uint8_t * row = static_cast<uint8_t *>(surf->pixels) + y * surf->pitch;
  130. for (int x = 0; x < surf->w; ++x)
  131. {
  132. if (row[x] != 0)
  133. {
  134. // opaque or can be opaque (e.g. disabled shadow)
  135. top = std::min(top, y);
  136. left = std::min(left, x);
  137. right = std::max(right, x);
  138. bottom = std::max(bottom, y);
  139. }
  140. }
  141. }
  142. }
  143. else
  144. {
  145. for (int y = 0; y < surf->h; ++y)
  146. {
  147. for (int x = 0; x < surf->w; ++x)
  148. {
  149. ColorRGBA color;
  150. SDL_GetRGBA(CSDL_Ext::getPixel(surf, x, y), surf->format, &color.r, &color.g, &color.b, &color.a);
  151. if (color.a != SDL_ALPHA_TRANSPARENT)
  152. {
  153. // opaque
  154. top = std::min(top, y);
  155. left = std::min(left, x);
  156. right = std::max(right, x);
  157. bottom = std::max(bottom, y);
  158. }
  159. }
  160. }
  161. }
  162. if (left == surf->w)
  163. {
  164. // empty image - simply delete it
  165. SDL_FreeSurface(surf);
  166. surf = nullptr;
  167. return;
  168. }
  169. if (left != 0 || top != 0 || right != surf->w - 1 || bottom != surf->h - 1)
  170. {
  171. // non-zero border found
  172. Rect newDimensions(left, top, right - left + 1, bottom - top + 1);
  173. SDL_Rect rectSDL = CSDL_Ext::toSDL(newDimensions);
  174. auto newSurface = CSDL_Ext::newSurface(newDimensions.dimensions(), surf);
  175. SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE);
  176. SDL_BlitSurface(surf, &rectSDL, newSurface, nullptr);
  177. if (SDL_HasColorKey(surf))
  178. {
  179. uint32_t colorKey;
  180. SDL_GetColorKey(surf, &colorKey);
  181. SDL_SetColorKey(newSurface, SDL_TRUE, colorKey);
  182. }
  183. SDL_FreeSurface(surf);
  184. surf = newSurface;
  185. margins.x += left;
  186. margins.y += top;
  187. }
  188. }
  189. std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palette * palette, EImageBlitMode mode) const
  190. {
  191. if (factor <= 0)
  192. throw std::runtime_error("Unable to scale by integer value of " + std::to_string(factor));
  193. if (!surf)
  194. return shared_from_this();
  195. if (palette && surf->format->palette)
  196. SDL_SetSurfacePalette(surf, palette);
  197. SDL_Surface * scaled = nullptr;
  198. // dump heuristics to differentiate tileable UI elements from map object / combat assets
  199. if (mode == EImageBlitMode::OPAQUE || mode == EImageBlitMode::COLORKEY || mode == EImageBlitMode::SIMPLE)
  200. scaled = CSDL_Ext::scaleSurfaceIntegerFactor(surf, factor, EScalingAlgorithm::XBRZ_OPAQUE);
  201. else
  202. scaled = CSDL_Ext::scaleSurfaceIntegerFactor(surf, factor, EScalingAlgorithm::XBRZ_ALPHA);
  203. auto ret = std::make_shared<SDLImageShared>(scaled);
  204. ret->fullSize.x = fullSize.x * factor;
  205. ret->fullSize.y = fullSize.y * factor;
  206. ret->margins.x = (int) round((float)margins.x * factor);
  207. ret->margins.y = (int) round((float)margins.y * factor);
  208. ret->optimizeSurface();
  209. // erase our own reference
  210. SDL_FreeSurface(scaled);
  211. if (surf->format->palette)
  212. SDL_SetSurfacePalette(surf, originalPalette);
  213. return ret;
  214. }
  215. std::shared_ptr<const ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const
  216. {
  217. float scaleX = static_cast<float>(size.x) / fullSize.x;
  218. float scaleY = static_cast<float>(size.y) / fullSize.y;
  219. if (palette && surf->format->palette)
  220. SDL_SetSurfacePalette(surf, palette);
  221. auto scaled = CSDL_Ext::scaleSurface(surf, (int)(surf->w * scaleX), (int)(surf->h * scaleY), EScalingAlgorithm::BILINEAR);
  222. if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point
  223. CSDL_Ext::setColorKey(scaled, scaled->format->palette->colors[0]);
  224. else if(scaled->format && scaled->format->Amask)
  225. SDL_SetSurfaceBlendMode(scaled, SDL_BLENDMODE_BLEND);//just in case
  226. else
  227. CSDL_Ext::setDefaultColorKey(scaled);//just in case
  228. auto ret = std::make_shared<SDLImageShared>(scaled);
  229. ret->fullSize.x = (int) round((float)fullSize.x * scaleX);
  230. ret->fullSize.y = (int) round((float)fullSize.y * scaleY);
  231. ret->margins.x = (int) round((float)margins.x * scaleX);
  232. ret->margins.y = (int) round((float)margins.y * scaleY);
  233. // erase our own reference
  234. SDL_FreeSurface(scaled);
  235. if (surf->format->palette)
  236. SDL_SetSurfacePalette(surf, originalPalette);
  237. return ret;
  238. }
  239. void SDLImageShared::exportBitmap(const boost::filesystem::path& path, SDL_Palette * palette) const
  240. {
  241. if (!surf)
  242. return;
  243. if (palette && surf->format->palette)
  244. SDL_SetSurfacePalette(surf, palette);
  245. IMG_SavePNG(surf, path.string().c_str());
  246. if (palette && surf->format->palette)
  247. SDL_SetSurfacePalette(surf, originalPalette);
  248. }
  249. bool SDLImageShared::isTransparent(const Point & coords) const
  250. {
  251. if (surf)
  252. return CSDL_Ext::isTransparent(surf, coords.x - margins.x, coords.y - margins.y);
  253. else
  254. return true;
  255. }
  256. Rect SDLImageShared::contentRect() const
  257. {
  258. auto tmpMargins = margins;
  259. auto tmpSize = Point(surf->w, surf->h);
  260. return Rect(tmpMargins, tmpSize);
  261. }
  262. const SDL_Palette * SDLImageShared::getPalette() const
  263. {
  264. if (!surf)
  265. return nullptr;
  266. return surf->format->palette;
  267. }
  268. Point SDLImageShared::dimensions() const
  269. {
  270. return fullSize;
  271. }
  272. std::shared_ptr<const ISharedImage> SDLImageShared::horizontalFlip() const
  273. {
  274. if (!surf)
  275. return shared_from_this();
  276. SDL_Surface * flipped = CSDL_Ext::horizontalFlip(surf);
  277. auto ret = std::make_shared<SDLImageShared>(flipped);
  278. ret->fullSize = fullSize;
  279. ret->margins.x = margins.x;
  280. ret->margins.y = fullSize.y - surf->h - margins.y;
  281. ret->fullSize = fullSize;
  282. return ret;
  283. }
  284. std::shared_ptr<const ISharedImage> SDLImageShared::verticalFlip() const
  285. {
  286. if (!surf)
  287. return shared_from_this();
  288. SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
  289. auto ret = std::make_shared<SDLImageShared>(flipped);
  290. ret->fullSize = fullSize;
  291. ret->margins.x = fullSize.x - surf->w - margins.x;
  292. ret->margins.y = margins.y;
  293. ret->fullSize = fullSize;
  294. return ret;
  295. }
  296. // Keep the original palette, in order to do color switching operation
  297. void SDLImageShared::savePalette()
  298. {
  299. // For some images that don't have palette, skip this
  300. if(surf->format->palette == nullptr)
  301. return;
  302. if(originalPalette == nullptr)
  303. originalPalette = SDL_AllocPalette(surf->format->palette->ncolors);
  304. SDL_SetPaletteColors(originalPalette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
  305. }
  306. SDLImageShared::~SDLImageShared()
  307. {
  308. SDL_FreeSurface(surf);
  309. SDL_FreePalette(originalPalette);
  310. }