30 #include <boost/circular_buffer.hpp>
31 #include <boost/math/constants/constants.hpp>
34 #define ERR_DP LOG_STREAM(err, log_display)
38 SDL_version sdl_version;
39 SDL_GetVersion(&sdl_version);
40 return version_info(sdl_version.major, sdl_version.minor, sdl_version.patch);
47 if(ver.major < major)
return false;
48 if(ver.major > major)
return true;
50 if(ver.minor < minor)
return false;
51 if(ver.minor > minor)
return true;
53 if(ver.patch < patch)
return false;
63 PLAIN_LOG <<
"Cannot use xbrz scaling with zoom factor > 5.";
72 surface dst(surf->w *z, surf->h * z);
79 if(surf ==
nullptr || dst ==
nullptr) {
80 PLAIN_LOG <<
"Could not create surface to scale onto";
97 assert(SDL_ALPHA_TRANSPARENT==0);
102 if(
w == surf->w &&
h == surf->h) {
110 if (
w == 0 ||
h ==0) {
115 if(surf ==
nullptr || dst ==
nullptr) {
116 PLAIN_LOG <<
"Could not create surface to scale onto";
134 assert(SDL_ALPHA_TRANSPARENT==0);
139 if(
w == surf->w &&
h == surf->h) {
147 if (
w == 0 ||
h ==0) {
152 if(surf ==
nullptr || dst ==
nullptr) {
153 PLAIN_LOG <<
"Could not create surface to scale onto";
161 const uint32_t*
const src_pixels = src_lock.
pixels();
162 uint32_t*
const dst_pixels = dst_lock.
pixels();
168 for(
int ydst = 0; ydst !=
h; ++ydst, ysrc += yratio) {
170 for(
int xdst = 0; xdst !=
w; ++xdst, xsrc += xratio) {
174 const uint32_t*
const src_word = src_pixels + ysrcint*surf->w + xsrcint;
175 uint32_t*
const dst_word = dst_pixels + ydst*dst->w + xdst;
176 const int dx = (xsrcint + 1 < surf->w) ? 1 : 0;
177 const int dy = (ysrcint + 1 < surf->h) ? surf->w : 0;
180 uint32_t rr,gg,bb,aa, temp;
182 uint32_t pix[4], bilin[4];
205 const int32_t
e = 0x000000FF & xsrc;
206 const int32_t
s = 0x000000FF & ysrc;
207 const int32_t
n = 0xFF -
s;
210 const int32_t we = 0xFF -
e;
213 pix[1] = *(src_word + dx);
214 pix[2] = *(src_word + dy);
215 pix[3] = *(src_word + dx + dy);
223 rr = bb = gg = aa = 0;
224 for (loc=0; loc<4; loc++) {
236 temp = (
a * bilin[loc]);
252 *dst_word = (
a << 24) + (r << 16) + (
g << 8) +
b;
263 assert(SDL_ALPHA_TRANSPARENT==0);
268 if(
w == surf->w &&
h == surf->h) {
276 if(surf ==
nullptr || dst ==
nullptr) {
277 PLAIN_LOG <<
"Could not create surface to scale onto";
285 const uint32_t*
const src_pixels = src_lock.
pixels();
286 uint32_t*
const dst_pixels = dst_lock.
pixels();
292 for(
int ydst = 0; ydst !=
h; ++ydst, ysrc += yratio) {
294 for(
int xdst = 0; xdst !=
w; ++xdst, xsrc += xratio) {
298 const uint32_t*
const src_word = src_pixels + ysrcint*surf->w + xsrcint;
299 uint32_t*
const dst_word = dst_pixels + ydst*dst->w + xdst;
300 const int dx = (xsrcint + 1 < surf->w) ? 1 : 0;
301 const int dy = (ysrcint + 1 < surf->h) ? surf->w : 0;
304 uint32_t rr,gg,bb,aa;
305 uint16_t avg_r, avg_g, avg_b;
306 uint32_t pix[4], bilin[4];
320 const int32_t east = 0x000000FF & xsrc;
321 const int32_t south = 0x000000FF & ysrc;
322 const int32_t north = 0xFF - south;
323 const int32_t west = 0xFF - east;
326 pix[1] = *(src_word + dx);
327 pix[2] = *(src_word + dy);
328 pix[3] = *(src_word + dx + dy);
330 bilin[0] = north*west;
331 bilin[1] = north*east;
332 bilin[2] = south*west;
333 bilin[3] = south*east;
339 avg_r = avg_g = avg_b = 0;
341 for (loc=0; loc<4; loc++) {
368 rr = gg = bb = aa = 0;
369 for (loc=0; loc<4; loc++) {
375 r =
static_cast<uint8_t
>(avg_r);
376 g =
static_cast<uint8_t
>(avg_g);
377 b =
static_cast<uint8_t
>(avg_b);
379 rr += r * bilin[loc];
380 gg +=
g * bilin[loc];
381 bb +=
b * bilin[loc];
382 aa +=
a * bilin[loc];
388 *dst_word = (
a << 24) + (r << 16) + (
g << 8) +
b;
400 assert(SDL_ALPHA_TRANSPARENT == 0);
402 if(surf ==
nullptr) {
405 if(
w == surf->w &&
h == surf->h) {
413 PLAIN_LOG <<
"Could not create surface to scale onto";
417 if(
w == 0 ||
h == 0) {
426 const uint32_t*
const src_pixels = src_lock.
pixels();
427 uint32_t*
const dst_pixels = dst_lock.
pixels();
429 const int src_w = surf->w;
430 const int src_h = surf->h;
432 const float xratio =
static_cast<float>(src_w) /
static_cast<float>(
w);
433 const float yratio =
static_cast<float>(src_h) /
static_cast<float>(
h);
434 for(
int ydst = 0; ydst !=
h; ++ydst) {
435 for(
int xdst = 0; xdst !=
w; ++xdst) {
437 const int xsrc = std::floor(
static_cast<float>(xdst) * xratio);
438 const int ysrc = std::floor(
static_cast<float>(ydst) * yratio);
439 dst_pixels[ydst * dst->w + xdst] = src_pixels[ysrc * src_w + xsrc];
459 if(nsurf ==
nullptr) {
460 PLAIN_LOG <<
"failed to make neutral surface";
466 uint32_t* beg = lock.
pixels();
467 uint32_t* end = beg + nsurf->w*surf->h;
470 uint8_t alpha = (*beg) >> 24;
478 r = std::max<int>(0,std::min<int>(255,
static_cast<int>(r)+
red));
479 g = std::max<int>(0,std::min<int>(255,
static_cast<int>(
g)+
green));
480 b = std::max<int>(0,std::min<int>(255,
static_cast<int>(
b)+
blue));
482 *beg = (alpha << 24) + (r << 16) + (
g << 8) +
b;
498 if(nsurf ==
nullptr) {
499 PLAIN_LOG <<
"failed to make neutral surface";
505 uint32_t* beg = lock.
pixels();
506 uint32_t* end = beg + nsurf->w*surf->h;
509 uint8_t alpha = (*beg) >> 24;
522 const uint8_t avg =
static_cast<uint8_t
>((
523 77 *
static_cast<uint16_t
>(r) +
524 150 *
static_cast<uint16_t
>(
g) +
525 29 *
static_cast<uint16_t
>(
b) ) / 256);
527 *beg = (alpha << 24) | (avg << 16) | (avg << 8) | avg;
543 if(nsurf ==
nullptr) {
544 PLAIN_LOG <<
"failed to make neutral surface";
550 uint32_t* beg = lock.
pixels();
551 uint32_t* end = beg + nsurf->w*surf->h;
554 uint8_t alpha = (*beg) >> 24;
557 uint8_t r,
g,
b, result;
565 result =
static_cast<uint8_t
>(0.299 * r + 0.587 *
g + 0.114 *
b) > threshold ? 255 : 0;
567 *beg = (alpha << 24) | (result << 16) | (result << 8) | result;
583 if(nsurf ==
nullptr) {
584 PLAIN_LOG <<
"failed to make neutral surface";
590 uint32_t* beg = lock.
pixels();
591 uint32_t* end = beg + nsurf->w*surf->h;
594 uint8_t alpha = (*beg) >> 24;
605 uint8_t outRed = std::min(255,
static_cast<int>((r * 0.393) + (
g * 0.769) + (
b * 0.189)));
606 uint8_t outGreen = std::min(255,
static_cast<int>((r * 0.349) + (
g * 0.686) + (
b * 0.168)));
607 uint8_t outBlue = std::min(255,
static_cast<int>((r * 0.272) + (
g * 0.534) + (
b * 0.131)));
609 *beg = (alpha << 24) | (outRed << 16) | (outGreen << 8) | (outBlue);
625 if(nsurf ==
nullptr) {
626 PLAIN_LOG <<
"failed to make neutral surface";
632 uint32_t* beg = lock.
pixels();
633 uint32_t* end = beg + nsurf->w*surf->h;
636 uint8_t alpha = (*beg) >> 24;
639 uint8_t r,
g,
b, newR, newG, newB;
648 newR = r > thresholdR ? 255 - r : r;
649 newG =
g > thresholdG ? 255 -
g :
g;
650 newB =
b > thresholdB ? 255 -
b :
b;
652 *beg = (alpha << 24) | (newR << 16) | (newG << 8) | (newB);
668 if(nsurf ==
nullptr) {
669 PLAIN_LOG <<
"failed to make neutral surface";
675 uint32_t* beg = lock.
pixels();
676 uint32_t* end = beg + nsurf->w*surf->h;
679 uint8_t alpha = (*beg) >> 24;
681 *beg = (0xff << 24) | (alpha << 16) | (alpha << 8) | alpha;
696 if(nsurf ==
nullptr) {
697 PLAIN_LOG <<
"failed to make neutral surface";
703 uint32_t* beg = lock.
pixels();
704 uint32_t* end = beg + nsurf->w*surf->h;
708 *beg = 0xff000000 | *beg;
726 if(nsurf ==
nullptr) {
727 PLAIN_LOG <<
"failed to blur the shadow surface";
733 uint32_t* beg = lock.
pixels();
734 uint32_t* end = beg + nsurf->w*surf->h;
737 uint8_t alpha = (*beg) >> 24;
743 *beg = (alpha*4) << 24;
760 if(nsurf ==
nullptr) {
761 PLAIN_LOG <<
"failed to make neutral surface";
767 uint32_t* beg = lock.
pixels();
768 uint32_t* end = beg + nsurf->w*surf->h;
771 uint8_t alpha = (*beg) >> 24;
774 uint8_t
red,
green,
blue, newRed, newGreen, newBlue, newAlpha;
847 *beg = (newAlpha << 24) | (newRed << 16) | (newGreen << 8) | newBlue;
862 if(map_rgb.empty()) {
867 if(nsurf ==
nullptr) {
868 PLAIN_LOG <<
"failed to make neutral surface";
873 uint32_t* beg = lock.
pixels();
874 uint32_t* end = beg + nsurf->w*surf->h;
877 uint8_t alpha = (*beg) >> 24;
882 uint32_t oldrgb = (*beg) | 0xFF000000;
885 if(
i != map_rgb.end()) {
886 *beg = (alpha << 24) | (
i->second.to_argb_bytes() & 0x00FFFFFF);
898 if(surf ==
nullptr) {
904 if(nsurf ==
nullptr) {
905 PLAIN_LOG <<
"could not make neutral surface...";
911 uint32_t* beg = lock.
pixels();
912 uint32_t* end = beg + nsurf->w*surf->h;
914 if (amount < 0) amount = 0;
916 uint8_t alpha = (*beg) >> 24;
928 *beg = (alpha << 24) + (r << 16) + (
g << 8) +
b;
940 if(surf ==
nullptr) {
944 SDL_SetSurfaceAlphaMod(surf, alpha_mod);
955 if(nsurf ==
nullptr) {
956 PLAIN_LOG <<
"could not make neutral surface...";
962 uint32_t* beg = lock.
pixels();
963 uint32_t* end = beg + nsurf->w*surf->h;
966 uint8_t alpha = (*beg) >> 24;
974 alpha = uint8_t(std::max<int>(0,std::min<int>(255,
static_cast<int>(alpha) + amount)));
975 *beg = (alpha << 24) + (r << 16) + (
g << 8) +
b;
987 if(surf ==
nullptr) {
988 *empty_result =
true;
991 if(mask ==
nullptr) {
998 if(nsurf ==
nullptr || nmask ==
nullptr) {
999 PLAIN_LOG <<
"could not make neutral surface...";
1002 if (nsurf->w != nmask->w) {
1007 std::stringstream ss;
1008 ss <<
"Detected an image with bad dimensions: ";
1009 if(!filename.empty()) ss << filename <<
": ";
1010 ss << nsurf->w <<
"x" << nsurf->h;
1012 PLAIN_LOG <<
"It will not be masked, please use: "<< nmask->w <<
"x" << nmask->h;
1021 uint32_t* beg = lock.
pixels();
1022 uint32_t* end = beg + nsurf->w*surf->h;
1023 const uint32_t* mbeg = mlock.
pixels();
1024 const uint32_t* mend = mbeg + nmask->w*nmask->h;
1026 while(beg != end && mbeg != mend) {
1027 uint8_t alpha = (*beg) >> 24;
1035 uint8_t malpha = (*mbeg) >> 24;
1036 if (alpha > malpha) {
1042 *beg = (alpha << 24) + (r << 16) + (
g << 8) +
b;
1050 *empty_result = empty;
1057 if(surf ==
nullptr) {
1060 if(mask ==
nullptr){
1064 if (surf->w != mask->w || surf->h != mask->h ) {
1072 if(nsurf ==
nullptr || nmask ==
nullptr) {
1073 PLAIN_LOG <<
"could not make neutral surface...";
1081 const uint32_t* mbeg = mlock.
pixels();
1082 const uint32_t* mend = mbeg + nmask->w*nmask->h;
1083 uint32_t* beg = lock.
pixels();
1086 while(mbeg != mend) {
1087 uint8_t malpha = (*mbeg) >> 24;
1089 uint8_t alpha = (*beg) >> 24;
1103 if(surf ==
nullptr) {
1106 if(lightmap ==
nullptr) {
1112 if(nsurf ==
nullptr) {
1113 PLAIN_LOG <<
"could not make neutral surface...";
1116 if (nsurf->w != lightmap->w) {
1121 PLAIN_LOG <<
"Detected an image with bad dimensions: " << nsurf->w <<
"x" << nsurf->h;
1122 PLAIN_LOG <<
"It will not be lighted, please use: "<< lightmap->w <<
"x" << lightmap->h;
1129 uint32_t* beg = lock.
pixels();
1130 uint32_t* end = beg + nsurf->w * nsurf->h;
1131 const uint32_t* lbeg = llock.
pixels();
1132 const uint32_t* lend = lbeg + lightmap->w * lightmap->h;
1134 while(beg != end && lbeg != lend) {
1135 uint8_t alpha = (*beg) >> 24;
1148 int dr = (
static_cast<int>(lr) - 128) * 2;
1149 int dg = (
static_cast<int>(
lg) - 128) * 2;
1150 int db = (
static_cast<int>(lb) - 128) * 2;
1152 r = std::max<int>(0,std::min<int>(255, r + dr));
1153 g = std::max<int>(0,std::min<int>(255,
g + dg));
1154 b = std::max<int>(0,std::min<int>(255,
b + db));
1156 *beg = (alpha << 24) + (r << 16) + (
g << 8) +
b;
1169 if(surf ==
nullptr) {
1175 if(res ==
nullptr) {
1176 PLAIN_LOG <<
"could not make neutral surface...";
1180 SDL_Rect
rect {0, 0, surf->w, surf->h};
1188 if(surf ==
nullptr) {
1192 const int max_blur = 256;
1193 if(depth > max_blur) {
1197 uint32_t queue[max_blur];
1198 const uint32_t* end_queue = queue + max_blur;
1200 const uint32_t ff = 0xff;
1202 const unsigned pixel_offset =
rect.y * surf->w +
rect.x;
1205 for(
int y = 0; y <
rect.h; ++y) {
1206 const uint32_t* front = &queue[0];
1207 uint32_t* back = &queue[0];
1209 uint32_t*
p = lock.
pixels() + pixel_offset + y * surf->w;
1210 for(
int x = 0; x <= depth && x <
rect.w; ++x, ++
p) {
1211 red += ((*p) >> 16)&0xFF;
1212 green += ((*p) >> 8)&0xFF;
1216 if(back == end_queue) {
1221 p = lock.
pixels() + pixel_offset + y * surf->w;
1222 for(
int x = 0; x <
rect.w; ++x, ++
p) {
1224 | (std::min(
red/avg,ff) << 16)
1225 | (std::min(
green/avg,ff) << 8)
1226 | std::min(
blue/avg,ff);
1229 red -= ((*front) >> 16)&0xFF;
1230 green -= ((*front) >> 8)&0xFF;
1231 blue -= *front&0xFF;
1234 if(front == end_queue) {
1239 if(x + depth+1 <
rect.w) {
1240 uint32_t* q =
p + depth+1;
1241 red += ((*q) >> 16)&0xFF;
1242 green += ((*q) >> 8)&0xFF;
1246 if(back == end_queue) {
1253 for(
int x = 0; x <
rect.w; ++x) {
1254 const uint32_t* front = &queue[0];
1255 uint32_t* back = &queue[0];
1257 uint32_t*
p = lock.
pixels() + pixel_offset + x;
1258 for(
int y = 0; y <= depth && y <
rect.h; ++y,
p += surf->w) {
1259 red += ((*p) >> 16)&0xFF;
1260 green += ((*p) >> 8)&0xFF;
1264 if(back == end_queue) {
1269 p = lock.
pixels() + pixel_offset + x;
1270 for(
int y = 0; y <
rect.h; ++y,
p += surf->w) {
1272 | (std::min(
red/avg,ff) << 16)
1273 | (std::min(
green/avg,ff) << 8)
1274 | std::min(
blue/avg,ff);
1277 red -= ((*front) >> 16)&0xFF;
1278 green -= ((*front) >> 8)&0xFF;
1279 blue -= *front&0xFF;
1282 if(front == end_queue) {
1287 if(y + depth+1 <
rect.h) {
1288 uint32_t* q =
p + (depth+1)*surf->w;
1289 red += ((*q) >> 16)&0xFF;
1290 green += ((*q) >> 8)&0xFF;
1294 if(back == end_queue) {
1304 if(surf ==
nullptr) {
1310 if(res ==
nullptr) {
1311 PLAIN_LOG <<
"could not make neutral surface...";
1315 const int max_blur = 256;
1316 if(depth > max_blur) {
1326 : alpha(((*
p) >> 24)&0xFF)
1327 ,
red(((*
p) >> 16)&0xFF)
1328 ,
green(((*
p) >> 8)&0xFF)
1329 ,
blue((*
p)&0xFF) {}
1338 Average& operator+=(
const Pixel& pix){
1339 red += pix.alpha * pix.red;
1340 green += pix.alpha * pix.green;
1341 blue += pix.alpha * pix.blue;
1345 Average& operator-=(
const Pixel& pix){
1346 red -= pix.alpha * pix.red;
1347 green -= pix.alpha * pix.green;
1348 blue -= pix.alpha * pix.blue;
1352 uint32_t operator()(
unsigned num){
1353 const uint32_t ff = 0xff;
1357 return (std::min(alpha/num,ff) << 24)
1358 | (std::min(
red/alpha,ff) << 16)
1359 | (std::min(
green/alpha,ff) << 8)
1360 | std::min(
blue/alpha,ff);
1364 boost::circular_buffer<Pixel> queue(depth*2+1);
1369 for(y = 0; y < res->h; ++y) {
1374 uint32_t*
p = lock.
pixels() + y*res->w;
1375 for(x = 0; x <= depth && x < res->w; ++x, ++
p) {
1376 assert(!queue.full());
1377 queue.push_back(Pixel{
p});
1378 avg += queue.back();
1383 for(x = 0; x < res->w; ++x, ++
p) {
1385 const uint32_t num = queue.size();
1390 avg -= queue.front();
1391 assert(!queue.empty());
1396 if(x + depth+1 < res->w) {
1397 uint32_t* q =
p + depth+1;
1398 assert(!queue.full());
1399 queue.push_back(Pixel{q});
1400 avg += queue.back();
1403 assert(
static_cast<int>(queue.size()) == std::min(depth, res->w));
1408 for(x = 0; x < res->w; ++x) {
1413 uint32_t*
p = lock.
pixels() + x;
1414 for(y = 0; y <= depth && y < res->h; ++y,
p += res->w) {
1415 assert(!queue.full());
1416 queue.push_back(Pixel{
p});
1417 avg += queue.back();
1422 for(y = 0; y < res->h; ++y,
p += res->w) {
1424 const uint32_t num = queue.size();
1429 avg -= queue.front();
1430 assert(!queue.empty());
1435 if(y + depth+1 < res->h) {
1436 uint32_t* q =
p + (depth+1)*res->w;
1437 assert(!queue.full());
1438 queue.push_back(Pixel{q});
1439 avg += queue.back();
1442 assert(
static_cast<int>(queue.size()) == std::min(depth, res->h));
1456 if(res ==
nullptr) {
1457 PLAIN_LOG <<
"Could not create a new surface in cut_surface()";
1461 std::size_t sbpp = surf->format->BytesPerPixel;
1462 std::size_t spitch = surf->pitch;
1463 std::size_t rbpp = res->format->BytesPerPixel;
1464 std::size_t rpitch = res->pitch;
1467 SDL_Rect src_rect = r;
1468 SDL_Rect dst_rect { 0, 0, r.w, r.h };
1470 if (src_rect.x < 0) {
1471 if (src_rect.x + src_rect.w <= 0)
1473 dst_rect.x -= src_rect.x;
1474 dst_rect.w += src_rect.x;
1475 src_rect.w += src_rect.x;
1478 if (src_rect.y < 0) {
1479 if (src_rect.y + src_rect.h <= 0)
1481 dst_rect.y -= src_rect.y;
1482 dst_rect.h += src_rect.y;
1483 src_rect.h += src_rect.y;
1487 if(src_rect.x >= surf->w || src_rect.y >= surf->h)
1493 const uint8_t* src =
reinterpret_cast<const uint8_t *
>(slock.
pixels());
1494 uint8_t* dest =
reinterpret_cast<uint8_t *
>(rlock.
pixels());
1496 for(
int y = 0; y < src_rect.h && (src_rect.y + y) < surf->h; ++y) {
1497 const uint8_t* line_src = src + (src_rect.y + y) * spitch + src_rect.x * sbpp;
1498 uint8_t* line_dest = dest + (dst_rect.y + y) * rpitch + dst_rect.x * rbpp;
1499 std::size_t
size = src_rect.w + src_rect.x <= surf->w ? src_rect.w : surf->w - src_rect.x;
1501 assert(rpitch >= src_rect.w * rbpp);
1502 memcpy(line_dest, line_src,
size * rbpp);
1509 ,
const double amount
1512 if(surf==
nullptr) {
1518 if(nsurf ==
nullptr) {
1519 PLAIN_LOG <<
"could not make neutral surface...";
1525 uint32_t* beg = lock.
pixels();
1526 uint32_t* end = beg + nsurf->w*surf->h;
1528 uint16_t ratio = amount * 256;
1529 const uint16_t
red = ratio * color.r;
1530 const uint16_t
green = ratio * color.g;
1531 const uint16_t
blue = ratio * color.b;
1532 ratio = 256 - ratio;
1535 uint8_t
a =
static_cast<uint8_t
>(*beg >> 24);
1536 uint8_t r = (ratio *
static_cast<uint8_t
>(*beg >> 16) +
red) >> 8;
1537 uint8_t
g = (ratio *
static_cast<uint8_t
>(*beg >> 8) +
green) >> 8;
1538 uint8_t
b = (ratio *
static_cast<uint8_t
>(*beg) +
blue) >> 8;
1540 *beg = (
a << 24) | (r << 16) | (
g << 8) |
b;
1556 int src_w, src_h, dst_w, dst_h;
1557 float min_x, min_y, sine, cosine;
1561 const float radians = angle * boost::math::constants::pi<float>() / 180;
1562 cosine = std::cos(radians);
1563 sine = std::sin(radians);
1565 src_w = surf->w * zoom;
1566 src_h = surf->h * zoom;
1568 const float point_1x = src_h * -sine;
1569 const float point_1y = src_h * cosine;
1570 const float point_2x = src_w * cosine - src_h * sine;
1571 const float point_2y = src_h * cosine + src_w * sine;
1572 const float point_3x = src_w * cosine;
1573 const float point_3y = src_w * sine;
1577 min_x = std::min(0.0F, std::min(point_1x, std::min(point_2x, point_3x)));
1578 min_y = std::min(0.0F, std::min(point_1y, std::min(point_2y, point_3y)));
1579 max_x = (angle > 90 && angle < 180) ? 0 : std::max(point_1x, std::max(point_2x, point_3x));
1580 max_y = (angle > 180 && angle < 270) ? 0 : std::max(point_1y, std::max(point_2y, point_3y));
1581 dst_w =
static_cast<int>(ceil(std::abs(max_x) - min_x)) / zoom;
1582 dst_h =
static_cast<int>(ceil(std::abs(max_y) - min_y)) / zoom;
1589 const float scale = 1.f / zoom;
1590 const int max_x = dst_w * zoom;
1591 const int max_y = dst_h * zoom;
1594 for (
int x = 0; x < max_x; x += offset)
1595 for (
int y = 0; y < max_y; y += offset) {
1597 const float source_x = (x + min_x)*cosine + (y + min_y)*sine;
1598 const float source_y = (y + min_y)*cosine - (x + min_x)*sine;
1600 if (source_x >= 0 && source_x < src_w
1601 && source_y >= 0 && source_y < src_h)
1604 get_pixel(src, src_lock, source_x, source_y));
1613 const int bpp = surf->format->BytesPerPixel;
1615 uint8_t*
const dst =
reinterpret_cast<uint8_t*
>(surf_lock.
pixels()) + y * surf->pitch + x * bpp;
1621 *
reinterpret_cast<uint16_t*
>(dst) = pixel;
1624 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
1625 dst[0] = (pixel >> 16) & 0xff;
1626 dst[1] = (pixel >> 8) & 0xff;
1627 dst[2] = pixel & 0xff;
1629 dst[0] = pixel & 0xff;
1630 dst[1] = (pixel >> 8) & 0xff;
1631 dst[2] = (pixel >> 16) & 0xff;
1635 *
reinterpret_cast<uint32_t*
>(dst) = pixel;
1644 const int bpp = surf->format->BytesPerPixel;
1646 const uint8_t*
const src =
reinterpret_cast<const uint8_t*
>(surf_lock.
pixels()) + y * surf->pitch + x * bpp;
1651 return *
reinterpret_cast<const uint16_t*
>(src);
1653 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
1654 return src[0] << 16 | src[1] << 8 | src[2];
1656 return src[0] | src[1] << 8 | src[2] << 16;
1659 return *
reinterpret_cast<const uint32_t*
>(src);
1667 if ( surf ==
nullptr )
1673 if ( nsurf ==
nullptr ) {
1674 PLAIN_LOG <<
"could not make neutral surface...";
1680 uint32_t*
const pixels = lock.
pixels();
1684 for (
int y=0; y != nsurf->h/2; ++y) {
1685 for(
int x=0; x != nsurf->w; ++x) {
1686 const int index1 = y*nsurf->w + x;
1687 const int index2 = (nsurf->h-y)*nsurf->w - x - 1;
1688 std::swap(pixels[index1],pixels[index2]);
1692 if (
is_odd(nsurf->h) ) {
1694 for (
int x=0; x != nsurf->w/2; ++x) {
1695 const int index1 = (nsurf->h/2)*nsurf->w + x;
1696 const int index2 = (nsurf->h/2)*nsurf->w + (nsurf->w - x - 1);
1697 std::swap(pixels[index1],pixels[index2]);
1709 if ( surf ==
nullptr )
1712 surface dst(surf->h, surf->w);
1714 if ( surf ==
nullptr || dst ==
nullptr ) {
1715 PLAIN_LOG <<
"could not make neutral surface...";
1723 const uint32_t*
const src_pixels = src_lock.
pixels();
1724 uint32_t*
const dst_pixels = dst_lock.
pixels();
1727 for(
int y = 0; y != surf->h; ++y) {
1728 for (
int x = 0; x != surf->w; ++x ) {
1729 const int src_index = y*surf->w + x;
1730 const int dst_index = clockwise ?
1731 x*dst->w + (dst->w-1-y) :
1732 (dst->h-1-x)*dst->w + y;
1733 dst_pixels[dst_index] = src_pixels[src_index];
1744 if(surf ==
nullptr) {
1750 if(nsurf ==
nullptr) {
1751 PLAIN_LOG <<
"could not make neutral surface...";
1757 uint32_t*
const pixels = lock.
pixels();
1759 for(
int y = 0; y != nsurf->h; ++y) {
1760 for(
int x = 0; x != nsurf->w/2; ++x) {
1761 const int index1 = y*nsurf->w + x;
1762 const int index2 = (y+1)*nsurf->w - x - 1;
1763 std::swap(pixels[index1],pixels[index2]);
1773 if(surf ==
nullptr) {
1779 if(nsurf ==
nullptr) {
1780 PLAIN_LOG <<
"could not make neutral surface...";
1786 uint32_t*
const pixels = lock.
pixels();
1788 for(
int x = 0; x != nsurf->w; ++x) {
1789 for(
int y = 0; y != nsurf->h/2; ++y) {
1790 const int index1 = y*nsurf->w + x;
1791 const int index2 = (nsurf->h-y-1)*surf->w + x;
1792 std::swap(pixels[index1],pixels[index2]);
1802 if (src ==
nullptr) {
1807 if(area.x >= src->w || area.y >= src->h || area.x + area.w < 0 || area.y + area.h < 0) {
1811 if(area.x + area.w > src->w) {
1812 area.w = src->w - area.x;
1814 if(area.y + area.h > src->h) {
1815 area.h = src->h - area.y;
1821 if(dst ==
nullptr) {
1822 PLAIN_LOG <<
"Could not create a new surface in get_surface_portion()";
1827 SDL_BlendMode src_blend;
1828 SDL_GetSurfaceBlendMode(src, &src_blend);
1829 SDL_SetSurfaceBlendMode(src, SDL_BLENDMODE_NONE);
1830 SDL_BlitSurface(src, &area, dst,
nullptr);
1831 SDL_SetSurfaceBlendMode(src, src_blend);
1843 bool operator()(uint32_t pixel)
const {
1844 uint8_t alpha = pixel >> 24;
1845 return alpha != 0x00;
1856 if(nsurf ==
nullptr) {
1857 PLAIN_LOG <<
"failed to make neutral surface";
1861 SDL_Rect res {0,0,0,0};
1862 const not_alpha calc;
1865 const uint32_t*
const pixels = lock.
pixels();
1868 for(
n = 0;
n != nsurf->h; ++
n) {
1869 const uint32_t*
const start_row = pixels +
n*nsurf->w;
1870 const uint32_t*
const end_row = start_row + nsurf->w;
1872 if(std::find_if(start_row,end_row,calc) != end_row)
1878 for(
n = 0;
n != nsurf->h-res.y; ++
n) {
1879 const uint32_t*
const start_row = pixels + (nsurf->h-
n-1)*surf->w;
1880 const uint32_t*
const end_row = start_row + nsurf->w;
1882 if(std::find_if(start_row,end_row,calc) != end_row)
1889 res.h = nsurf->h - res.y -
n;
1891 for(
n = 0;
n != nsurf->w; ++
n) {
1893 for(y = 0; y != nsurf->h; ++y) {
1894 const uint32_t pixel = pixels[y*nsurf->w +
n];
1905 for(
n = 0;
n != nsurf->w-res.x; ++
n) {
1907 for(y = 0; y != nsurf->h; ++y) {
1908 const uint32_t pixel = pixels[y*nsurf->w + surf->w -
n - 1];
1917 res.w = nsurf->w - res.x -
n;
1920 if(cropped && res.w > 0 && res.h > 0) {
1927 ERR_DP <<
"Failed to either crop or scale the surface";
Helper class for pinning SDL surfaces into memory.
surface clone() const
Makes a copy of this surface.
Represents version numbers.
std::unordered_map< color_t, color_t > color_range_map
void swap(config &lhs, config &rhs)
Implement non-member swap function for std::swap (calls config::swap).
Standard logging facilities (interface).
constexpr int fixed_point_to_int(int32_t n)
If positive, just bit shift.
constexpr bool is_odd(T num)
constexpr unsigned fixed_point_multiply(int32_t n1, int32_t n2)
constexpr int32_t fixed_point_divide(int n1, int n2)
version_info get_version()
Returns the runtime SDL version.
bool runtime_at_least(uint8_t major, uint8_t minor=0, uint8_t patch=0)
Returns true if the runtime SDL version is at or greater than the specified version,...
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
void nearestNeighborScale(const uint32_t *src, int srcWidth, int srcHeight, uint32_t *trg, int trgWidth, int trgHeight)
void scale(size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, const ScalerCfg &cfg=ScalerCfg(), int yFirst=0, int yLast=std::numeric_limits< int >::max())
The basic class for representing 8-bit RGB or RGBA colour values.
static constexpr color_t from_argb_bytes(uint32_t c)
Creates a new color_t object from a uint32_t variable.
An abstract description of a rectangle with integer coordinates.
static map_location::DIRECTION n
static map_location::DIRECTION s
surface alpha_to_greyscale(const surface &surf)
surface blur_alpha_surface(const surface &surf, int depth)
Cross-fades a surface with alpha channel.
surface adjust_surface_color(const surface &surf, int red, int green, int blue)
surface recolor_image(surface surf, const color_range_map &map_rgb)
Recolors a surface using a map with source and converted palette values.
surface negative_image(const surface &surf, const int thresholdR, const int thresholdG, const int thresholdB)
surface swap_channels_image(const surface &surf, channel r, channel g, channel b, channel a)
surface light_surface(const surface &surf, const surface &lightmap)
Light surf using lightmap.
surface shadow_image(const surface &surf, int scale)
create an heavy shadow of the image, by blurring, increasing alpha and darkening
surface scale_surface_nn(const surface &surf, int w, int h)
Scale a surface using the nearest neighbor algorithm (provided by xBRZ lib)
surface scale_surface_legacy(const surface &surf, int w, int h)
Scale a surface using simple bilinear filtering (discarding rgb from source pixels with 0 alpha)
surface get_non_transparent_portion(const surface &surf)
void put_pixel(const surface &surf, surface_lock &surf_lock, int x, int y, uint32_t pixel)
Helper methods for setting/getting a single pixel in an image.
surface adjust_surface_alpha_add(const surface &surf, int amount)
surface flip_surface(const surface &surf)
void adjust_surface_alpha(surface &surf, uint8_t alpha_mod)
surface sepia_image(const surface &surf)
surface blur_surface(const surface &surf, int depth)
Cross-fades a surface.
surface blend_surface(const surface &surf, const double amount, const color_t color)
Blends a surface with a color.
uint32_t get_pixel(const surface &surf, const const_surface_lock &surf_lock, int x, int y)
surface cut_surface(const surface &surf, const SDL_Rect &r)
Cuts a rectangle from a surface.
surface wipe_alpha(const surface &surf)
static lg::log_domain log_display("display")
surface flop_surface(const surface &surf)
surface brighten_image(const surface &surf, int32_t amount)
surface get_surface_portion(const surface &src, SDL_Rect &area)
Get a portion of the screen.
surface scale_surface_xbrz(const surface &surf, std::size_t z)
Scale a surface using xBRZ algorithm.
surface scale_surface_sharp(const surface &surf, int w, int h)
Scale a surface using modified nearest neighbour algorithm.
surface monochrome_image(const surface &surf, const int threshold)
surface rotate_90_surface(const surface &surf, bool clockwise)
Rotates a surface 90 degrees.
surface rotate_180_surface(const surface &surf)
Rotates a surface 180 degrees.
surface greyscale_image(const surface &surf)
surface scale_surface(const surface &surf, int w, int h)
Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gr...
surface mask_surface(const surface &surf, const surface &mask, bool *empty_result, const std::string &filename)
Applies a mask on a surface.
surface rotate_any_surface(const surface &surf, float angle, int zoom, int offset)
Rotates a surface by any degrees.
bool in_mask_surface(const surface &surf, const surface &mask)
Check if a surface fit into a mask.