SDLImage.cpp 8.8 KB

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