SDL_Extensions.cpp 20 KB

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