SDL_Extensions.cpp 22 KB

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