SDLImage.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 "CAnimation.h"
  12. #include "SDL_Extensions.h"
  13. #include "ColorFilter.h"
  14. #include "../CBitmapHandler.h"
  15. #include "../Graphics.h"
  16. #include "../../lib/filesystem/Filesystem.h"
  17. #include "../../lib/filesystem/ISimpleResourceLoader.h"
  18. #include "../../lib/JsonNode.h"
  19. #include "../../lib/CRandomGenerator.h"
  20. #include "../../lib/vcmi_endian.h"
  21. #include <SDL_surface.h>
  22. class SDLImageLoader;
  23. std::shared_ptr<IImage> IImage::createFromFile( const std::string & path )
  24. {
  25. return std::shared_ptr<IImage>(new SDLImage(path));
  26. }
  27. std::shared_ptr<IImage> IImage::createFromSurface( SDL_Surface * source )
  28. {
  29. return std::shared_ptr<IImage>(new SDLImage(source, true));
  30. }
  31. IImage::IImage() = default;
  32. IImage::~IImage() = default;
  33. int IImage::width() const
  34. {
  35. return dimensions().x;
  36. }
  37. int IImage::height() const
  38. {
  39. return dimensions().y;
  40. }
  41. SDLImage::SDLImage(CDefFile * data, size_t frame, size_t group)
  42. : surf(nullptr),
  43. margins(0, 0),
  44. fullSize(0, 0),
  45. originalPalette(nullptr)
  46. {
  47. SDLImageLoader loader(this);
  48. data->loadFrame(frame, group, loader);
  49. savePalette();
  50. }
  51. SDLImage::SDLImage(SDL_Surface * from, bool extraRef)
  52. : surf(nullptr),
  53. margins(0, 0),
  54. fullSize(0, 0),
  55. originalPalette(nullptr)
  56. {
  57. surf = from;
  58. if (surf == nullptr)
  59. return;
  60. savePalette();
  61. if (extraRef)
  62. surf->refcount++;
  63. fullSize.x = surf->w;
  64. fullSize.y = surf->h;
  65. }
  66. SDLImage::SDLImage(const JsonNode & conf)
  67. : surf(nullptr),
  68. margins(0, 0),
  69. fullSize(0, 0),
  70. originalPalette(nullptr)
  71. {
  72. std::string filename = conf["file"].String();
  73. surf = BitmapHandler::loadBitmap(filename);
  74. if(surf == nullptr)
  75. return;
  76. savePalette();
  77. const JsonNode & jsonMargins = conf["margins"];
  78. margins.x = static_cast<int>(jsonMargins["left"].Integer());
  79. margins.y = static_cast<int>(jsonMargins["top"].Integer());
  80. fullSize.x = static_cast<int>(conf["width"].Integer());
  81. fullSize.y = static_cast<int>(conf["height"].Integer());
  82. if(fullSize.x == 0)
  83. {
  84. fullSize.x = margins.x + surf->w + (int)jsonMargins["right"].Integer();
  85. }
  86. if(fullSize.y == 0)
  87. {
  88. fullSize.y = margins.y + surf->h + (int)jsonMargins["bottom"].Integer();
  89. }
  90. }
  91. SDLImage::SDLImage(std::string filename)
  92. : surf(nullptr),
  93. margins(0, 0),
  94. fullSize(0, 0),
  95. originalPalette(nullptr)
  96. {
  97. surf = BitmapHandler::loadBitmap(filename);
  98. if(surf == nullptr)
  99. {
  100. logGlobal->error("Error: failed to load image %s", filename);
  101. return;
  102. }
  103. else
  104. {
  105. savePalette();
  106. fullSize.x = surf->w;
  107. fullSize.y = surf->h;
  108. }
  109. }
  110. void SDLImage::draw(SDL_Surface *where, int posX, int posY, const Rect *src) const
  111. {
  112. if(!surf)
  113. return;
  114. Rect destRect(posX, posY, surf->w, surf->h);
  115. draw(where, &destRect, src);
  116. }
  117. void SDLImage::draw(SDL_Surface* where, const Rect * dest, const Rect* src) const
  118. {
  119. if (!surf)
  120. return;
  121. Rect sourceRect(0, 0, surf->w, surf->h);
  122. Point destShift(0, 0);
  123. if(src)
  124. {
  125. if(src->x < margins.x)
  126. destShift.x += margins.x - src->x;
  127. if(src->y < margins.y)
  128. destShift.y += margins.y - src->y;
  129. sourceRect = Rect(*src).intersect(Rect(margins.x, margins.y, surf->w, surf->h));
  130. sourceRect -= margins;
  131. }
  132. else
  133. destShift = margins;
  134. if(dest)
  135. destShift += dest->topLeft();
  136. uint8_t perSurfaceAlpha;
  137. if (SDL_GetSurfaceAlphaMod(surf, &perSurfaceAlpha) != 0)
  138. logGlobal->error("SDL_GetSurfaceAlphaMod faied! %s", SDL_GetError());
  139. if(surf->format->BitsPerPixel == 8 && perSurfaceAlpha == SDL_ALPHA_OPAQUE)
  140. {
  141. CSDL_Ext::blit8bppAlphaTo24bpp(surf, sourceRect, where, destShift);
  142. }
  143. else
  144. {
  145. CSDL_Ext::blitSurface(surf, sourceRect, where, destShift);
  146. }
  147. }
  148. std::shared_ptr<IImage> SDLImage::scaleFast(const Point & size) const
  149. {
  150. float scaleX = float(size.x) / width();
  151. float scaleY = float(size.y) / height();
  152. auto scaled = CSDL_Ext::scaleSurfaceFast(surf, (int)(surf->w * scaleX), (int)(surf->h * scaleY));
  153. if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point
  154. CSDL_Ext::setColorKey(scaled, scaled->format->palette->colors[0]);
  155. else if(scaled->format && scaled->format->Amask)
  156. SDL_SetSurfaceBlendMode(scaled, SDL_BLENDMODE_BLEND);//just in case
  157. else
  158. CSDL_Ext::setDefaultColorKey(scaled);//just in case
  159. SDLImage * ret = new SDLImage(scaled, false);
  160. ret->fullSize.x = (int) round((float)fullSize.x * scaleX);
  161. ret->fullSize.y = (int) round((float)fullSize.y * scaleY);
  162. ret->margins.x = (int) round((float)margins.x * scaleX);
  163. ret->margins.y = (int) round((float)margins.y * scaleY);
  164. return std::shared_ptr<IImage>(ret);
  165. }
  166. void SDLImage::exportBitmap(const boost::filesystem::path& path) const
  167. {
  168. SDL_SaveBMP(surf, path.string().c_str());
  169. }
  170. void SDLImage::playerColored(PlayerColor player)
  171. {
  172. graphics->blueToPlayersAdv(surf, player);
  173. }
  174. void SDLImage::setAlpha(uint8_t value)
  175. {
  176. CSDL_Ext::setAlpha (surf, value);
  177. SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
  178. }
  179. void SDLImage::setFlagColor(PlayerColor player)
  180. {
  181. if(player < PlayerColor::PLAYER_LIMIT || player==PlayerColor::NEUTRAL)
  182. CSDL_Ext::setPlayerColor(surf, player);
  183. }
  184. bool SDLImage::isTransparent(const Point & coords) const
  185. {
  186. return CSDL_Ext::isTransparent(surf, coords.x, coords.y);
  187. }
  188. Point SDLImage::dimensions() const
  189. {
  190. return fullSize;
  191. }
  192. void SDLImage::horizontalFlip()
  193. {
  194. margins.y = fullSize.y - surf->h - margins.y;
  195. //todo: modify in-place
  196. SDL_Surface * flipped = CSDL_Ext::horizontalFlip(surf);
  197. SDL_FreeSurface(surf);
  198. surf = flipped;
  199. }
  200. void SDLImage::verticalFlip()
  201. {
  202. margins.x = fullSize.x - surf->w - margins.x;
  203. //todo: modify in-place
  204. SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
  205. SDL_FreeSurface(surf);
  206. surf = flipped;
  207. }
  208. // Keep the original palette, in order to do color switching operation
  209. void SDLImage::savePalette()
  210. {
  211. // For some images that don't have palette, skip this
  212. if(surf->format->palette == nullptr)
  213. return;
  214. if(originalPalette == nullptr)
  215. originalPalette = SDL_AllocPalette(DEFAULT_PALETTE_COLORS);
  216. SDL_SetPaletteColors(originalPalette, surf->format->palette->colors, 0, DEFAULT_PALETTE_COLORS);
  217. }
  218. void SDLImage::shiftPalette(int from, int howMany)
  219. {
  220. //works with at most 16 colors, if needed more -> increase values
  221. assert(howMany < 16);
  222. if(surf->format->palette)
  223. {
  224. SDL_Color palette[16];
  225. for(int i=0; i<howMany; ++i)
  226. {
  227. palette[(i+1)%howMany] = surf->format->palette->colors[from + i];
  228. }
  229. CSDL_Ext::setColors(surf, palette, from, howMany);
  230. }
  231. }
  232. void SDLImage::adjustPalette(const ColorFilter & shifter, size_t colorsToSkip)
  233. {
  234. if(originalPalette == nullptr)
  235. return;
  236. SDL_Palette* palette = surf->format->palette;
  237. // Note: here we skip first colors in the palette that are predefined in H3 images
  238. for(int i = colorsToSkip; i < palette->ncolors; i++)
  239. {
  240. palette->colors[i] = shifter.shiftColor(originalPalette->colors[i]);
  241. }
  242. }
  243. void SDLImage::resetPalette()
  244. {
  245. if(originalPalette == nullptr)
  246. return;
  247. // Always keept the original palette not changed, copy a new palette to assign to surface
  248. SDL_SetPaletteColors(surf->format->palette, originalPalette->colors, 0, originalPalette->ncolors);
  249. }
  250. void SDLImage::resetPalette( int colorID )
  251. {
  252. if(originalPalette == nullptr)
  253. return;
  254. // Always keept the original palette not changed, copy a new palette to assign to surface
  255. SDL_SetPaletteColors(surf->format->palette, originalPalette->colors + colorID, colorID, 1);
  256. }
  257. void SDLImage::setSpecialPallete(const IImage::SpecialPalette & SpecialPalette)
  258. {
  259. if(surf->format->palette)
  260. {
  261. CSDL_Ext::setColors(surf, const_cast<SDL_Color *>(SpecialPalette.data()), 1, 7);
  262. }
  263. }
  264. SDLImage::~SDLImage()
  265. {
  266. SDL_FreeSurface(surf);
  267. if(originalPalette != nullptr)
  268. {
  269. SDL_FreePalette(originalPalette);
  270. originalPalette = nullptr;
  271. }
  272. }