SDLImage.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. 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. graphics->blueToPlayersAdv(surf, player);
  165. }
  166. void SDLImage::setAlpha(uint8_t value)
  167. {
  168. CSDL_Ext::setAlpha (surf, value);
  169. if (value != 255)
  170. SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
  171. }
  172. void SDLImage::setBlitMode(EImageBlitMode mode)
  173. {
  174. blitMode = mode;
  175. if (blitMode != EImageBlitMode::OPAQUE && surf->format->Amask != 0)
  176. SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
  177. else
  178. SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE);
  179. }
  180. void SDLImage::setFlagColor(PlayerColor player)
  181. {
  182. if(player.isValidPlayer() || player==PlayerColor::NEUTRAL)
  183. CSDL_Ext::setPlayerColor(surf, player);
  184. }
  185. bool SDLImage::isTransparent(const Point & coords) const
  186. {
  187. return CSDL_Ext::isTransparent(surf, coords.x, coords.y);
  188. }
  189. Point SDLImage::dimensions() const
  190. {
  191. return fullSize;
  192. }
  193. void SDLImage::horizontalFlip()
  194. {
  195. margins.y = fullSize.y - surf->h - margins.y;
  196. //todo: modify in-place
  197. SDL_Surface * flipped = CSDL_Ext::horizontalFlip(surf);
  198. SDL_FreeSurface(surf);
  199. surf = flipped;
  200. }
  201. void SDLImage::verticalFlip()
  202. {
  203. margins.x = fullSize.x - surf->w - margins.x;
  204. //todo: modify in-place
  205. SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
  206. SDL_FreeSurface(surf);
  207. surf = flipped;
  208. }
  209. void SDLImage::doubleFlip()
  210. {
  211. horizontalFlip();
  212. verticalFlip();
  213. }
  214. // Keep the original palette, in order to do color switching operation
  215. void SDLImage::savePalette()
  216. {
  217. // For some images that don't have palette, skip this
  218. if(surf->format->palette == nullptr)
  219. return;
  220. if(originalPalette == nullptr)
  221. originalPalette = SDL_AllocPalette(DEFAULT_PALETTE_COLORS);
  222. SDL_SetPaletteColors(originalPalette, surf->format->palette->colors, 0, DEFAULT_PALETTE_COLORS);
  223. }
  224. void SDLImage::shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove)
  225. {
  226. if(surf->format->palette)
  227. {
  228. std::vector<SDL_Color> shifterColors(colorsToMove);
  229. for(uint32_t i=0; i<colorsToMove; ++i)
  230. {
  231. shifterColors[(i+distanceToMove)%colorsToMove] = originalPalette->colors[firstColorID + i];
  232. }
  233. CSDL_Ext::setColors(surf, shifterColors.data(), firstColorID, colorsToMove);
  234. }
  235. }
  236. void SDLImage::adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask)
  237. {
  238. if(originalPalette == nullptr)
  239. return;
  240. SDL_Palette* palette = surf->format->palette;
  241. // Note: here we skip first colors in the palette that are predefined in H3 images
  242. for(int i = 0; i < palette->ncolors; i++)
  243. {
  244. if(i < std::numeric_limits<uint32_t>::digits && ((colorsToSkipMask >> i) & 1) == 1)
  245. continue;
  246. palette->colors[i] = CSDL_Ext::toSDL(shifter.shiftColor(CSDL_Ext::fromSDL(originalPalette->colors[i])));
  247. }
  248. }
  249. void SDLImage::resetPalette()
  250. {
  251. if(originalPalette == nullptr)
  252. return;
  253. // Always keept the original palette not changed, copy a new palette to assign to surface
  254. SDL_SetPaletteColors(surf->format->palette, originalPalette->colors, 0, originalPalette->ncolors);
  255. }
  256. void SDLImage::resetPalette( int colorID )
  257. {
  258. if(originalPalette == nullptr)
  259. return;
  260. // Always keept the original palette not changed, copy a new palette to assign to surface
  261. SDL_SetPaletteColors(surf->format->palette, originalPalette->colors + colorID, colorID, 1);
  262. }
  263. void SDLImage::setSpecialPallete(const IImage::SpecialPalette & specialPalette, uint32_t colorsToSkipMask)
  264. {
  265. if(surf->format->palette)
  266. {
  267. size_t last = std::min<size_t>(specialPalette.size(), surf->format->palette->ncolors);
  268. for (size_t i = 0; i < last; ++i)
  269. {
  270. if(i < std::numeric_limits<uint32_t>::digits && ((colorsToSkipMask >> i) & 1) == 1)
  271. surf->format->palette->colors[i] = CSDL_Ext::toSDL(specialPalette[i]);
  272. }
  273. }
  274. }
  275. SDLImage::~SDLImage()
  276. {
  277. SDL_FreeSurface(surf);
  278. if(originalPalette != nullptr)
  279. {
  280. SDL_FreePalette(originalPalette);
  281. originalPalette = nullptr;
  282. }
  283. }