2
0

SDLImage.cpp 6.8 KB

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