Skip to content

Commit 158bfa6

Browse files
committed
Replace copyToWithAlpha with more appropriate functions
fixes #16316
1 parent 23bf50a commit 158bfa6

1 file changed

Lines changed: 28 additions & 32 deletions

File tree

‎src/client/imagesource.cpp‎

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,20 @@ video::IImage* SourceImageCache::getOrLoad(const std::string &name)
109109
* \param src Top image. This image must have the ECF_A8R8G8B8 color format.
110110
* \param dst Bottom image.
111111
* The top image is drawn onto this base image in-place.
112-
* \param dst_pos An offset vector to move src before drawing it onto dst
112+
* \param src_pos Offset into the Top image
113+
* \param dst_pos Offset into the Bottom image
113114
* \param size Size limit of the copied area
114115
*/
115116
template<bool overlay = false>
116-
static void blit_with_alpha(video::IImage *src, video::IImage *dst,
117-
v2s32 dst_pos, v2u32 size);
117+
static void blit_with_alpha2(video::IImage *src, video::IImage *dst,
118+
v2s32 src_pos, v2s32 dst_pos, v2u32 size);
119+
120+
template<bool overlay = false>
121+
static inline void blit_with_alpha(video::IImage *src, video::IImage *dst,
122+
v2s32 dst_pos, v2u32 size)
123+
{
124+
blit_with_alpha2<overlay>(src, dst, v2s32(), dst_pos, size);
125+
}
118126

119127
// Apply a color to an image. Uses an int (0-255) to calculate the ratio.
120128
// If the ratio is 255 or -1 and keep_alpha is true, then it multiples the
@@ -412,8 +420,8 @@ void blit_pixel(video::SColor src_col, video::SColor &dst_col)
412420
} // namespace (anonymous)
413421

414422
template<bool overlay>
415-
static void blit_with_alpha(video::IImage *src, video::IImage *dst, v2s32 dst_pos,
416-
v2u32 size)
423+
static void blit_with_alpha2(video::IImage *src, video::IImage *dst,
424+
v2s32 src_pos, v2s32 dst_pos, v2u32 size)
417425
{
418426
if (dst->getColorFormat() != video::ECF_A8R8G8B8)
419427
throw BaseException("blit_with_alpha() supports only ECF_A8R8G8B8 "
@@ -436,16 +444,16 @@ static void blit_with_alpha(video::IImage *src, video::IImage *dst, v2s32 dst_po
436444
reinterpret_cast<video::SColor *>(src->getData());
437445
video::SColor *pixels_dst =
438446
reinterpret_cast<video::SColor *>(dst->getData());
439-
// Limit y and x to the overlapping ranges
440-
// s.t. the positions are all in bounds after offsetting.
441-
u32 x_start = (u32)std::max(0, -dst_pos.X);
442-
u32 y_start = (u32)std::max(0, -dst_pos.Y);
443-
u32 x_end = (u32)std::min<s64>({size.X, src_dim.Width,
444-
dst_dim.Width - (s64)dst_pos.X});
445-
u32 y_end = (u32)std::min<s64>({size.Y, src_dim.Height,
446-
dst_dim.Height - (s64)dst_pos.Y});
447+
// Limit X and Y to the overlapping ranges
448+
// so that the positions are all in bounds after offsetting.
449+
u32 x_start = (u32)std::max({0, -dst_pos.X, -src_pos.X});
450+
u32 y_start = (u32)std::max({0, -dst_pos.Y, -src_pos.Y});
451+
u32 x_end = (u32)std::min({size.X, src_dim.Width - (s32)src_pos.X,
452+
dst_dim.Width - (s32)dst_pos.X});
453+
u32 y_end = (u32)std::min({size.Y, src_dim.Height - (s32)src_pos.Y,
454+
dst_dim.Height - (s32)dst_pos.Y});
447455
for (u32 y0 = y_start; y0 < y_end; ++y0) {
448-
size_t i_src = y0 * src_dim.Width + x_start;
456+
size_t i_src = (src_pos.Y + y0) * src_dim.Width + src_pos.X + x_start;
449457
size_t i_dst = (dst_pos.Y + y0) * dst_dim.Width + dst_pos.X + x_start;
450458
for (u32 x0 = x_start; x0 < x_end; ++x0) {
451459
blit_pixel<overlay>(pixels_src[i_src++], pixels_dst[i_dst++]);
@@ -1313,16 +1321,11 @@ bool ImageSource::generateImagePart(std::string_view part_of_name,
13131321
if (!baseimg)
13141322
baseimg = driver->createImage(video::ECF_A8R8G8B8, dim);
13151323

1316-
core::position2d<s32> pos_base(0, 0);
1317-
core::position2d<s32> clippos(0, 0);
1324+
v2s32 clippos(0, 0);
13181325
clippos.Y = dim.Height * (100-percent) / 100;
1319-
core::dimension2d<u32> clipdim = dim;
1326+
core::dimension2du clipdim = dim;
13201327
clipdim.Height = clipdim.Height * percent / 100 + 1;
1321-
core::rect<s32> cliprect(clippos, clipdim);
1322-
img->copyToWithAlpha(baseimg, pos_base,
1323-
core::rect<s32>(v2s32(0,0), dim),
1324-
video::SColor(255,255,255,255),
1325-
&cliprect);
1328+
blit_with_alpha2(img, baseimg, clippos, clippos, clipdim);
13261329
img->drop();
13271330
}
13281331
}
@@ -1357,16 +1360,11 @@ bool ImageSource::generateImagePart(std::string_view part_of_name,
13571360
video::IImage *img = driver->createImage(video::ECF_A8R8G8B8,
13581361
frame_size);
13591362

1360-
// Fill target image with transparency
1361-
img->fill(video::SColor(0,0,0,0));
1362-
13631363
core::dimension2d<u32> dim = frame_size;
13641364
core::position2d<s32> pos_dst(0, 0);
13651365
core::position2d<s32> pos_src(0, frame_index * frame_size.Y);
1366-
baseimg->copyToWithAlpha(img, pos_dst,
1367-
core::rect<s32>(pos_src, dim),
1368-
video::SColor(255,255,255,255),
1369-
NULL);
1366+
baseimg->copyTo(img, pos_dst,
1367+
core::rect<s32>(pos_src, dim), nullptr);
13701368
// Replace baseimg
13711369
baseimg->drop();
13721370
baseimg = img;
@@ -1626,12 +1624,10 @@ bool ImageSource::generateImagePart(std::string_view part_of_name,
16261624

16271625
video::IImage *img = driver->createImage(
16281626
video::ECF_A8R8G8B8, tile_dim);
1629-
img->fill(video::SColor(0,0,0,0));
16301627

16311628
v2u32 vdim(tile_dim);
16321629
core::rect<s32> rect(v2s32(x0 * vdim.X, y0 * vdim.Y), tile_dim);
1633-
baseimg->copyToWithAlpha(img, v2s32(0), rect,
1634-
video::SColor(255,255,255,255), NULL);
1630+
baseimg->copyTo(img, v2s32(0), rect, nullptr);
16351631

16361632
// Replace baseimg
16371633
baseimg->drop();

0 commit comments

Comments
 (0)