SDL_Extensions.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * SDL_Extensions.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 "SDL_Extensions.h"
  12. #include "SDL_PixelAccess.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../render/Graphics.h"
  15. #include "../render/IScreenHandler.h"
  16. #include "../render/Colors.h"
  17. #include "../CMT.h"
  18. #include "../xBRZ/xbrz.h"
  19. #include "../../lib/GameConstants.h"
  20. #include <tbb/parallel_for.h>
  21. #include <SDL_render.h>
  22. #include <SDL_surface.h>
  23. #include <SDL_version.h>
  24. Rect CSDL_Ext::fromSDL(const SDL_Rect & rect)
  25. {
  26. return Rect(Point(rect.x, rect.y), Point(rect.w, rect.h));
  27. }
  28. SDL_Rect CSDL_Ext::toSDL(const Rect & rect)
  29. {
  30. SDL_Rect result;
  31. result.x = rect.x;
  32. result.y = rect.y;
  33. result.w = rect.w;
  34. result.h = rect.h;
  35. return result;
  36. }
  37. ColorRGBA CSDL_Ext::fromSDL(const SDL_Color & color)
  38. {
  39. return { color.r, color.g, color.b, color.a };
  40. }
  41. SDL_Color CSDL_Ext::toSDL(const ColorRGBA & color)
  42. {
  43. SDL_Color result;
  44. result.r = color.r;
  45. result.g = color.g;
  46. result.b = color.b;
  47. result.a = color.a;
  48. return result;
  49. }
  50. void CSDL_Ext::setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
  51. {
  52. SDL_SetPaletteColors(surface->format->palette,colors,firstcolor,ncolors);
  53. }
  54. void CSDL_Ext::setAlpha(SDL_Surface * bg, int value)
  55. {
  56. SDL_SetSurfaceAlphaMod(bg, value);
  57. }
  58. SDL_Surface * CSDL_Ext::newSurface(const Point & dimensions)
  59. {
  60. return newSurface(dimensions, screen);
  61. }
  62. SDL_Surface * CSDL_Ext::newSurface(const Point & dimensions, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
  63. {
  64. SDL_Surface * ret = SDL_CreateRGBSurface(0,dimensions.x,dimensions.y,mod->format->BitsPerPixel,mod->format->Rmask,mod->format->Gmask,mod->format->Bmask,mod->format->Amask);
  65. if(ret == nullptr)
  66. {
  67. const char * error = SDL_GetError();
  68. std::string messagePattern = "Failed to create SDL Surface of size %d x %d, %d bpp. Reason: %s";
  69. std::string message = boost::str(boost::format(messagePattern) % dimensions.x % dimensions.y % mod->format->BitsPerPixel % error);
  70. handleFatalError(message, true);
  71. }
  72. if (mod->format->palette)
  73. {
  74. assert(ret->format->palette);
  75. assert(ret->format->palette->ncolors >= mod->format->palette->ncolors);
  76. memcpy(ret->format->palette->colors, mod->format->palette->colors, mod->format->palette->ncolors * sizeof(SDL_Color));
  77. }
  78. return ret;
  79. }
  80. SDL_Surface * CSDL_Ext::copySurface(SDL_Surface * mod) //returns copy of given surface
  81. {
  82. //return SDL_DisplayFormat(mod);
  83. return SDL_ConvertSurface(mod, mod->format, mod->flags);
  84. }
  85. template<int bpp>
  86. SDL_Surface * CSDL_Ext::createSurfaceWithBpp(int width, int height)
  87. {
  88. uint32_t rMask = 0, gMask = 0, bMask = 0, aMask = 0;
  89. Channels::px<bpp>::r.set((uint8_t*)&rMask, 255);
  90. Channels::px<bpp>::g.set((uint8_t*)&gMask, 255);
  91. Channels::px<bpp>::b.set((uint8_t*)&bMask, 255);
  92. Channels::px<bpp>::a.set((uint8_t*)&aMask, 255);
  93. return SDL_CreateRGBSurface(0, width, height, bpp * 8, rMask, gMask, bMask, aMask);
  94. }
  95. void CSDL_Ext::blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  96. {
  97. CSDL_Ext::blitSurface(src, dst, Point(x, y));
  98. }
  99. void CSDL_Ext::blitAt(SDL_Surface * src, const Rect & pos, SDL_Surface * dst)
  100. {
  101. if (src)
  102. blitAt(src,pos.x,pos.y,dst);
  103. }
  104. // Vertical flip
  105. SDL_Surface * CSDL_Ext::verticalFlip(SDL_Surface * toRot)
  106. {
  107. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  108. SDL_LockSurface(ret);
  109. SDL_LockSurface(toRot);
  110. const int bpp = ret->format->BytesPerPixel;
  111. char * src = reinterpret_cast<char *>(toRot->pixels);
  112. char * dst = reinterpret_cast<char *>(ret->pixels);
  113. for(int i=0; i<ret->h; i++)
  114. {
  115. //FIXME: optimization bugged
  116. // if (bpp == 1)
  117. // {
  118. // // much faster for 8-bit surfaces (majority of our data)
  119. // std::reverse_copy(src, src + toRot->pitch, dst);
  120. // }
  121. // else
  122. // {
  123. char * srcPxl = src;
  124. char * dstPxl = dst + ret->w * bpp;
  125. for(int j=0; j<ret->w; j++)
  126. {
  127. dstPxl -= bpp;
  128. std::copy(srcPxl, srcPxl + bpp, dstPxl);
  129. srcPxl += bpp;
  130. }
  131. // }
  132. src += toRot->pitch;
  133. dst += ret->pitch;
  134. }
  135. SDL_UnlockSurface(ret);
  136. SDL_UnlockSurface(toRot);
  137. return ret;
  138. }
  139. // Horizontal flip
  140. SDL_Surface * CSDL_Ext::horizontalFlip(SDL_Surface * toRot)
  141. {
  142. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  143. SDL_LockSurface(ret);
  144. SDL_LockSurface(toRot);
  145. char * src = reinterpret_cast<char *>(toRot->pixels);
  146. char * dst = reinterpret_cast<char *>(ret->pixels) + ret->h * ret->pitch;
  147. for(int i=0; i<ret->h; i++)
  148. {
  149. dst -= ret->pitch;
  150. std::copy(src, src + toRot->pitch, dst);
  151. src += toRot->pitch;
  152. }
  153. SDL_UnlockSurface(ret);
  154. SDL_UnlockSurface(toRot);
  155. return ret;
  156. }
  157. uint32_t CSDL_Ext::getPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte)
  158. {
  159. int bpp = surface->format->BytesPerPixel;
  160. /* Here p is the address to the pixel we want to retrieve */
  161. uint8_t *p = (uint8_t *)surface->pixels + y * surface->pitch + x * bpp;
  162. switch(bpp)
  163. {
  164. case 1:
  165. if(colorByte)
  166. return colorTouint32_t(surface->format->palette->colors+(*p));
  167. else
  168. return *p;
  169. case 2:
  170. return *(uint16_t *)p;
  171. case 3:
  172. return p[0] | p[1] << 8 | p[2] << 16;
  173. case 4:
  174. return *(uint32_t *)p;
  175. default:
  176. return 0; // shouldn't happen, but avoids warnings
  177. }
  178. }
  179. template<int bpp, bool useAlpha>
  180. int CSDL_Ext::blit8bppAlphaTo24bppT(const SDL_Surface * src, const Rect & srcRectInput, SDL_Surface * dst, const Point & dstPointInput, [[maybe_unused]] uint8_t alpha)
  181. {
  182. SDL_Rect srcRectInstance = CSDL_Ext::toSDL(srcRectInput);
  183. SDL_Rect dstRectInstance = CSDL_Ext::toSDL(Rect(dstPointInput, srcRectInput.dimensions()));
  184. SDL_Rect * srcRect =&srcRectInstance;
  185. SDL_Rect * dstRect =&dstRectInstance;
  186. /* Make sure the surfaces aren't locked */
  187. if ( ! src || ! dst )
  188. {
  189. SDL_SetError("SDL_UpperBlit: passed a nullptr surface");
  190. return -1;
  191. }
  192. if ( src->locked || dst->locked )
  193. {
  194. SDL_SetError("Surfaces must not be locked during blit");
  195. return -1;
  196. }
  197. if (src->format->BytesPerPixel==1 && (bpp==3 || bpp==4 || bpp==2)) //everything's ok
  198. {
  199. SDL_Rect fulldst;
  200. int srcx;
  201. int srcy;
  202. int w;
  203. int h;
  204. /* If the destination rectangle is nullptr, use the entire dest surface */
  205. if ( dstRect == nullptr )
  206. {
  207. fulldst.x = fulldst.y = 0;
  208. dstRect = &fulldst;
  209. }
  210. /* clip the source rectangle to the source surface */
  211. if(srcRect)
  212. {
  213. int maxw;
  214. int maxh;
  215. srcx = srcRect->x;
  216. w = srcRect->w;
  217. if(srcx < 0)
  218. {
  219. w += srcx;
  220. dstRect->x -= srcx;
  221. srcx = 0;
  222. }
  223. maxw = src->w - srcx;
  224. if(maxw < w)
  225. w = maxw;
  226. srcy = srcRect->y;
  227. h = srcRect->h;
  228. if(srcy < 0)
  229. {
  230. h += srcy;
  231. dstRect->y -= srcy;
  232. srcy = 0;
  233. }
  234. maxh = src->h - srcy;
  235. if(maxh < h)
  236. h = maxh;
  237. }
  238. else
  239. {
  240. srcx = srcy = 0;
  241. w = src->w;
  242. h = src->h;
  243. }
  244. /* clip the destination rectangle against the clip rectangle */
  245. {
  246. SDL_Rect *clip = &dst->clip_rect;
  247. int dx;
  248. int dy;
  249. dx = clip->x - dstRect->x;
  250. if(dx > 0)
  251. {
  252. w -= dx;
  253. dstRect->x += dx;
  254. srcx += dx;
  255. }
  256. dx = dstRect->x + w - clip->x - clip->w;
  257. if(dx > 0)
  258. w -= dx;
  259. dy = clip->y - dstRect->y;
  260. if(dy > 0)
  261. {
  262. h -= dy;
  263. dstRect->y += dy;
  264. srcy += dy;
  265. }
  266. dy = dstRect->y + h - clip->y - clip->h;
  267. if(dy > 0)
  268. h -= dy;
  269. }
  270. if(w > 0 && h > 0)
  271. {
  272. dstRect->w = w;
  273. dstRect->h = h;
  274. if(SDL_LockSurface(dst))
  275. return -1; //if we cannot lock the surface
  276. const SDL_Color *colors = src->format->palette->colors;
  277. uint8_t *colory = (uint8_t*)src->pixels + srcy*src->pitch + srcx;
  278. uint8_t *py = (uint8_t*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  279. for(int y=0; y<h; ++y, colory+=src->pitch, py+=dst->pitch)
  280. {
  281. uint8_t *color = colory;
  282. uint8_t *p = py;
  283. for(int x = 0; x < w; ++x)
  284. {
  285. const SDL_Color &tbc = colors[*color++]; //color to blit
  286. if constexpr (useAlpha)
  287. ColorPutter<bpp>::PutColorAlphaSwitch(p, tbc.r, tbc.g, tbc.b, int(alpha) * tbc.a / 255 );
  288. else
  289. ColorPutter<bpp>::PutColorAlphaSwitch(p, tbc.r, tbc.g, tbc.b, tbc.a);
  290. p += bpp;
  291. }
  292. }
  293. SDL_UnlockSurface(dst);
  294. }
  295. }
  296. return 0;
  297. }
  298. int CSDL_Ext::blit8bppAlphaTo24bpp(const SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dstPoint, uint8_t alpha)
  299. {
  300. if (alpha == SDL_ALPHA_OPAQUE)
  301. {
  302. switch(dst->format->BytesPerPixel)
  303. {
  304. case 3: return blit8bppAlphaTo24bppT<3, false>(src, srcRect, dst, dstPoint, alpha);
  305. case 4: return blit8bppAlphaTo24bppT<4, false>(src, srcRect, dst, dstPoint, alpha);
  306. }
  307. }
  308. else
  309. {
  310. switch(dst->format->BytesPerPixel)
  311. {
  312. case 3: return blit8bppAlphaTo24bppT<3, true>(src, srcRect, dst, dstPoint, alpha);
  313. case 4: return blit8bppAlphaTo24bppT<4, true>(src, srcRect, dst, dstPoint, alpha);
  314. }
  315. }
  316. logGlobal->error("%d bpp is not supported!", (int)dst->format->BitsPerPixel);
  317. return -1;
  318. }
  319. uint32_t CSDL_Ext::colorTouint32_t(const SDL_Color * color)
  320. {
  321. uint32_t ret = 0;
  322. ret+=color->a;
  323. ret<<=8; //*=256
  324. ret+=color->b;
  325. ret<<=8; //*=256
  326. ret+=color->g;
  327. ret<<=8; //*=256
  328. ret+=color->r;
  329. return ret;
  330. }
  331. static void drawLineXDashed(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color)
  332. {
  333. double length(x2 - x1);
  334. for(int x = x1; x <= x2; x++)
  335. {
  336. double f = (x - x1) / length;
  337. int y = vstd::lerp(y1, y2, f);
  338. if (std::abs(x - x1) % 5 != 4)
  339. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, x, y, color.r, color.g, color.b);
  340. }
  341. }
  342. static void drawLineYDashed(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color)
  343. {
  344. double length(y2 - y1);
  345. for(int y = y1; y <= y2; y++)
  346. {
  347. double f = (y - y1) / length;
  348. int x = vstd::lerp(x1, x2, f);
  349. if (std::abs(y - y1) % 5 != 4)
  350. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, x, y, color.r, color.g, color.b);
  351. }
  352. }
  353. static void drawLineX(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2)
  354. {
  355. double length(x2 - x1);
  356. for(int x = x1; x <= x2; x++)
  357. {
  358. double f = (x - x1) / length;
  359. int y = vstd::lerp(y1, y2, f);
  360. uint8_t r = vstd::lerp(color1.r, color2.r, f);
  361. uint8_t g = vstd::lerp(color1.g, color2.g, f);
  362. uint8_t b = vstd::lerp(color1.b, color2.b, f);
  363. uint8_t a = vstd::lerp(color1.a, color2.a, f);
  364. uint8_t *p = CSDL_Ext::getPxPtr(sur, x, y);
  365. ColorPutter<4>::PutColor(p, r,g,b,a);
  366. }
  367. }
  368. static void drawLineY(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2)
  369. {
  370. double length(y2 - y1);
  371. for(int y = y1; y <= y2; y++)
  372. {
  373. double f = (y - y1) / length;
  374. int x = vstd::lerp(x1, x2, f);
  375. uint8_t r = vstd::lerp(color1.r, color2.r, f);
  376. uint8_t g = vstd::lerp(color1.g, color2.g, f);
  377. uint8_t b = vstd::lerp(color1.b, color2.b, f);
  378. uint8_t a = vstd::lerp(color1.a, color2.a, f);
  379. uint8_t *p = CSDL_Ext::getPxPtr(sur, x, y);
  380. ColorPutter<4>::PutColor(p, r,g,b,a);
  381. }
  382. }
  383. void CSDL_Ext::drawLine(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color1, const SDL_Color & color2, int thickness)
  384. {
  385. //FIXME: duplicated code with drawLineDashed
  386. int width = std::abs(from.x - dest.x);
  387. int height = std::abs(from.y - dest.y);
  388. if(width == 0 && height == 0)
  389. {
  390. uint8_t * p = CSDL_Ext::getPxPtr(sur, from.x, from.y);
  391. ColorPutter<4>::PutColorAlpha(p, color1);
  392. return;
  393. }
  394. for(int i = 0; i < thickness; ++i)
  395. {
  396. if(width > height)
  397. {
  398. if(from.x < dest.x)
  399. drawLineX(sur, from.x, from.y + i, dest.x, dest.y + i, color1, color2);
  400. else
  401. drawLineX(sur, dest.x, dest.y + i, from.x, from.y + i, color2, color1);
  402. }
  403. else
  404. {
  405. if(from.y < dest.y)
  406. drawLineY(sur, from.x + i, from.y, dest.x + i, dest.y, color1, color2);
  407. else
  408. drawLineY(sur, dest.x + i, dest.y, from.x + i, from.y, color2, color1);
  409. }
  410. }
  411. }
  412. void CSDL_Ext::drawLineDashed(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color)
  413. {
  414. //FIXME: duplicated code with drawLine
  415. int width = std::abs(from.x - dest.x);
  416. int height = std::abs(from.y - dest.y);
  417. if ( width == 0 && height == 0)
  418. {
  419. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, from.x, from.y, color.r, color.g, color.b);
  420. return;
  421. }
  422. if (width > height)
  423. {
  424. if ( from.x < dest.x)
  425. drawLineXDashed(sur, from.x, from.y, dest.x, dest.y, color);
  426. else
  427. drawLineXDashed(sur, dest.x, dest.y, from.x, from.y, color);
  428. }
  429. else
  430. {
  431. if ( from.y < dest.y)
  432. drawLineYDashed(sur, from.x, from.y, dest.x, dest.y, color);
  433. else
  434. drawLineYDashed(sur, dest.x, dest.y, from.x, from.y, color);
  435. }
  436. }
  437. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const SDL_Color &color, int depth)
  438. {
  439. depth = std::max(1, depth);
  440. for(int depthIterator = 0; depthIterator < depth; depthIterator++)
  441. {
  442. for(int i = 0; i < w; i++)
  443. {
  444. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+i,y+depthIterator,color.r,color.g,color.b);
  445. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+i,y+h-1-depthIterator,color.r,color.g,color.b);
  446. }
  447. for(int i = 0; i < h; i++)
  448. {
  449. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+depthIterator,y+i,color.r,color.g,color.b);
  450. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+w-1-depthIterator,y+i,color.r,color.g,color.b);
  451. }
  452. }
  453. }
  454. void CSDL_Ext::drawBorder( SDL_Surface * sur, const Rect &r, const SDL_Color &color, int depth)
  455. {
  456. drawBorder(sur, r.x, r.y, r.w, r.h, color, depth);
  457. }
  458. CSDL_Ext::TColorPutter CSDL_Ext::getPutterFor(SDL_Surface * const &dest)
  459. {
  460. switch(dest->format->BytesPerPixel)
  461. {
  462. case 3:
  463. return ColorPutter<3>::PutColor;
  464. case 4:
  465. return ColorPutter<4>::PutColor;
  466. default:
  467. logGlobal->error("%d bpp is not supported!", (int)dest->format->BitsPerPixel);
  468. return nullptr;
  469. }
  470. }
  471. uint8_t * CSDL_Ext::getPxPtr(const SDL_Surface * const &srf, const int x, const int y)
  472. {
  473. return (uint8_t *)srf->pixels + y * srf->pitch + x * srf->format->BytesPerPixel;
  474. }
  475. bool CSDL_Ext::isTransparent( SDL_Surface * srf, const Point & position )
  476. {
  477. return isTransparent(srf, position.x, position.y);
  478. }
  479. bool CSDL_Ext::isTransparent( SDL_Surface * srf, int x, int y )
  480. {
  481. if (x < 0 || y < 0 || x >= srf->w || y >= srf->h)
  482. return true;
  483. SDL_Color color;
  484. SDL_GetRGBA(CSDL_Ext::getPixel(srf, x, y), srf->format, &color.r, &color.g, &color.b, &color.a);
  485. bool pixelTransparent = color.a < 128;
  486. bool pixelCyan = (color.r == 0 && color.g == 255 && color.b == 255);
  487. return pixelTransparent || pixelCyan;
  488. }
  489. void CSDL_Ext::putPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const uint8_t & R, const uint8_t & G, const uint8_t & B, uint8_t A)
  490. {
  491. uint8_t *p = getPxPtr(ekran, x, y);
  492. getPutterFor(ekran)(p, R, G, B);
  493. switch(ekran->format->BytesPerPixel)
  494. {
  495. case 3: Channels::px<3>::a.set(p, A); break;
  496. case 4: Channels::px<4>::a.set(p, A); break;
  497. }
  498. }
  499. void CSDL_Ext::putPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const uint8_t & R, const uint8_t & G, const uint8_t & B, uint8_t A)
  500. {
  501. const SDL_Rect & rect = ekran->clip_rect;
  502. if(x >= rect.x && x < rect.w + rect.x
  503. && y >= rect.y && y < rect.h + rect.y)
  504. CSDL_Ext::putPixelWithoutRefresh(ekran, x, y, R, G, B, A);
  505. }
  506. template<int bpp>
  507. void CSDL_Ext::convertToGrayscaleBpp(SDL_Surface * surf, const Rect & rect )
  508. {
  509. uint8_t * pixels = static_cast<uint8_t*>(surf->pixels);
  510. for(int yp = rect.top(); yp < rect.bottom(); ++yp)
  511. {
  512. uint8_t * pixel_from = pixels + yp * surf->pitch + rect.left() * surf->format->BytesPerPixel;
  513. uint8_t * pixel_dest = pixels + yp * surf->pitch + rect.right() * surf->format->BytesPerPixel;
  514. for (uint8_t * pixel = pixel_from; pixel < pixel_dest; pixel += surf->format->BytesPerPixel)
  515. {
  516. int r = Channels::px<bpp>::r.get(pixel);
  517. int g = Channels::px<bpp>::g.get(pixel);
  518. int b = Channels::px<bpp>::b.get(pixel);
  519. int gray = static_cast<int>(0.299 * r + 0.587 * g + 0.114 *b);
  520. Channels::px<bpp>::r.set(pixel, gray);
  521. Channels::px<bpp>::g.set(pixel, gray);
  522. Channels::px<bpp>::b.set(pixel, gray);
  523. }
  524. }
  525. }
  526. void CSDL_Ext::convertToGrayscale( SDL_Surface * surf, const Rect & rect )
  527. {
  528. switch(surf->format->BytesPerPixel)
  529. {
  530. case 3: convertToGrayscaleBpp<3>(surf, rect); break;
  531. case 4: convertToGrayscaleBpp<4>(surf, rect); break;
  532. }
  533. }
  534. // scaling via bilinear interpolation algorithm.
  535. // NOTE: best results are for scaling in range 50%...200%.
  536. // And upscaling looks awful right now - should be fixed somehow
  537. SDL_Surface * CSDL_Ext::scaleSurface(SDL_Surface * surf, int width, int height)
  538. {
  539. if(!surf || !width || !height)
  540. return nullptr;
  541. // TODO: use xBRZ if possible? E.g. when scaling to 150% do 100% -> 200% via xBRZ and then linear downscale 200% -> 150%?
  542. // Need to investigate which is optimal for performance and for visuals
  543. SDL_Surface * intermediate = SDL_ConvertSurface(surf, screen->format, 0);
  544. SDL_Surface * ret = newSurface(Point(width, height), intermediate);
  545. #if SDL_VERSION_ATLEAST(2,0,16)
  546. SDL_SoftStretchLinear(intermediate, nullptr, ret, nullptr);
  547. #else
  548. SDL_SoftStretch(intermediate, nullptr, ret, nullptr);
  549. #endif
  550. SDL_FreeSurface(intermediate);
  551. return ret;
  552. }
  553. SDL_Surface * CSDL_Ext::scaleSurfaceIntegerFactor(SDL_Surface * surf, int factor, EScalingAlgorithm algorithm)
  554. {
  555. if(surf == nullptr || factor == 0)
  556. return nullptr;
  557. int newWidth = surf->w * factor;
  558. int newHight = surf->h * factor;
  559. SDL_Surface * intermediate = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_ARGB8888, 0);
  560. SDL_Surface * ret = newSurface(Point(newWidth, newHight), intermediate);
  561. assert(intermediate->pitch == intermediate->w * 4);
  562. assert(ret->pitch == ret->w * 4);
  563. const uint32_t * srcPixels = static_cast<const uint32_t*>(intermediate->pixels);
  564. uint32_t * dstPixels = static_cast<uint32_t*>(ret->pixels);
  565. switch (algorithm)
  566. {
  567. case EScalingAlgorithm::NEAREST:
  568. xbrz::nearestNeighborScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  569. break;
  570. case EScalingAlgorithm::BILINEAR:
  571. xbrz::bilinearScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
  572. break;
  573. case EScalingAlgorithm::XBRZ_ALPHA:
  574. case EScalingAlgorithm::XBRZ_OPAQUE:
  575. {
  576. auto format = algorithm == EScalingAlgorithm::XBRZ_OPAQUE ? xbrz::ColorFormat::ARGB_CLAMPED : xbrz::ColorFormat::ARGB;
  577. if(intermediate->h < 32)
  578. {
  579. // for tiny images tbb incurs too high overhead
  580. xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, format, {});
  581. }
  582. else
  583. {
  584. // xbrz recommends granulation of 16, but according to tests, for smaller images granulation of 4 is actually the best option
  585. const int granulation = intermediate->h > 400 ? 16 : 4;
  586. tbb::parallel_for(tbb::blocked_range<size_t>(0, intermediate->h, granulation), [factor, srcPixels, dstPixels, intermediate, format](const tbb::blocked_range<size_t> & r)
  587. {
  588. xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, format, {}, r.begin(), r.end());
  589. });
  590. }
  591. break;
  592. }
  593. default:
  594. throw std::runtime_error("invalid scaling algorithm!");
  595. }
  596. SDL_FreeSurface(intermediate);
  597. return ret;
  598. }
  599. void CSDL_Ext::blitSurface(SDL_Surface * src, const Rect & srcRectInput, SDL_Surface * dst, const Point & dstPoint)
  600. {
  601. SDL_Rect srcRect = CSDL_Ext::toSDL(srcRectInput);
  602. SDL_Rect dstRect = CSDL_Ext::toSDL(Rect(dstPoint, srcRectInput.dimensions()));
  603. int result = SDL_UpperBlit(src, &srcRect, dst, &dstRect);
  604. if (result != 0)
  605. logGlobal->error("SDL_UpperBlit failed! %s", SDL_GetError());
  606. }
  607. void CSDL_Ext::blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest)
  608. {
  609. Rect allSurface( Point(0,0), Point(src->w, src->h));
  610. blitSurface(src, allSurface, dst, dest);
  611. }
  612. void CSDL_Ext::fillSurface( SDL_Surface *dst, const SDL_Color & color )
  613. {
  614. Rect allSurface( Point(0,0), Point(dst->w, dst->h));
  615. fillRect(dst, allSurface, color);
  616. }
  617. void CSDL_Ext::fillRect( SDL_Surface *dst, const Rect & dstrect, const SDL_Color & color)
  618. {
  619. SDL_Rect newRect = CSDL_Ext::toSDL(dstrect);
  620. uint32_t sdlColor = SDL_MapRGBA(dst->format, color.r, color.g, color.b, color.a);
  621. SDL_FillRect(dst, &newRect, sdlColor);
  622. }
  623. void CSDL_Ext::fillRectBlended( SDL_Surface *dst, const Rect & dstrect, const SDL_Color & color)
  624. {
  625. SDL_Rect newRect = CSDL_Ext::toSDL(dstrect);
  626. uint32_t sdlColor = SDL_MapRGBA(dst->format, color.r, color.g, color.b, color.a);
  627. SDL_Surface * tmp = SDL_CreateRGBSurface(0, newRect.w, newRect.h, dst->format->BitsPerPixel, dst->format->Rmask, dst->format->Gmask, dst->format->Bmask, dst->format->Amask);
  628. SDL_FillRect(tmp, nullptr, sdlColor);
  629. SDL_BlitSurface(tmp, nullptr, dst, &newRect);
  630. SDL_FreeSurface(tmp);
  631. }
  632. STRONG_INLINE static uint32_t mapColor(SDL_Surface * surface, SDL_Color color)
  633. {
  634. return SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a);
  635. }
  636. void CSDL_Ext::setColorKey(SDL_Surface * surface, SDL_Color color)
  637. {
  638. uint32_t key = mapColor(surface,color);
  639. SDL_SetColorKey(surface, SDL_TRUE, key);
  640. }
  641. void CSDL_Ext::setDefaultColorKey(SDL_Surface * surface)
  642. {
  643. setColorKey(surface, toSDL(Colors::DEFAULT_KEY_COLOR));
  644. }
  645. void CSDL_Ext::setDefaultColorKeyPresize(SDL_Surface * surface)
  646. {
  647. uint32_t key = mapColor(surface, toSDL(Colors::DEFAULT_KEY_COLOR));
  648. auto & color = surface->format->palette->colors[key];
  649. // set color key only if exactly such color was found
  650. if (color.r == Colors::DEFAULT_KEY_COLOR.r && color.g == Colors::DEFAULT_KEY_COLOR.g && color.b == Colors::DEFAULT_KEY_COLOR.b)
  651. {
  652. SDL_SetColorKey(surface, SDL_TRUE, key);
  653. color.a = SDL_ALPHA_TRANSPARENT;
  654. }
  655. }
  656. void CSDL_Ext::setClipRect(SDL_Surface * src, const Rect & other)
  657. {
  658. SDL_Rect rect = CSDL_Ext::toSDL(other);
  659. SDL_SetClipRect(src, &rect);
  660. }
  661. void CSDL_Ext::getClipRect(SDL_Surface * src, Rect & other)
  662. {
  663. SDL_Rect rect;
  664. SDL_GetClipRect(src, &rect);
  665. other = CSDL_Ext::fromSDL(rect);
  666. }
  667. int CSDL_Ext::CClipRectGuard::getScalingFactor() const
  668. {
  669. return GH.screenHandler().getScalingFactor();
  670. }
  671. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<3>(int, int);
  672. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<4>(int, int);