SDLImage.cpp 8.7 KB

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