SDLImage.cpp 8.3 KB

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