Skip to content

Commit 9a29058

Browse files
committed
Core (LV::Video): Account for underflow/overflow in the alpha blending of 32-bit videos.
1 parent d8b4866 commit 9a29058

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

libvisual/libvisual/private/lv_video_blit.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,37 @@ namespace LV {
6161
}
6262
}
6363

64-
void VideoBlit::blit_overlay_alphasrc (Video* dest, Video* src)
64+
void VideoBlit::blit_overlay_alphasrc (Video* dst, Video* src)
6565
{
66-
auto destbuf = static_cast<uint8_t*> (dest->get_pixels ());
67-
auto srcbuf = static_cast<uint8_t const*> (src->get_pixels ());
68-
6966
if (visual_cpu_has_mmx ()) {
70-
blit_overlay_alphasrc_mmx (dest, src);
67+
blit_overlay_alphasrc_mmx (dst, src);
7168
return;
7269
}
7370

71+
auto dst_pixel_row_ptr = static_cast<uint8_t*> (dst->get_pixels ());
72+
auto src_pixel_row_ptr = static_cast<uint8_t const*> (src->get_pixels ());
73+
7474
for (int y = 0; y < src->m_impl->height; y++) {
75+
auto dst_pixel = dst_pixel_row_ptr;
76+
auto src_pixel = src_pixel_row_ptr;
77+
7578
for (int x = 0; x < src->m_impl->width; x++) {
76-
uint8_t alpha = srcbuf[3];
79+
uint8_t const src_alpha = src_pixel[3];
80+
81+
uint16_t const c0 = static_cast<uint16_t> (src_pixel[0]) * src_alpha + static_cast<uint16_t> (dst_pixel[0]) * (255 - src_alpha);
82+
uint16_t const c1 = static_cast<uint16_t> (src_pixel[1]) * src_alpha + static_cast<uint16_t> (dst_pixel[1]) * (255 - src_alpha);
83+
uint16_t const c2 = static_cast<uint16_t> (src_pixel[2]) * src_alpha + static_cast<uint16_t> (dst_pixel[2]) * (255 - src_alpha);
7784

78-
destbuf[0] = (alpha * (srcbuf[0] - destbuf[0]) >> 8) + destbuf[0];
79-
destbuf[1] = (alpha * (srcbuf[1] - destbuf[1]) >> 8) + destbuf[1];
80-
destbuf[2] = (alpha * (srcbuf[2] - destbuf[2]) >> 8) + destbuf[2];
85+
dst_pixel[0] = c0 >> 8;
86+
dst_pixel[1] = c1 >> 8;
87+
dst_pixel[2] = c2 >> 8;
8188

82-
destbuf += dest->m_impl->bpp;
83-
srcbuf += src->m_impl->bpp;
89+
src_pixel += 4;
90+
dst_pixel += 4;
8491
}
8592

86-
destbuf += dest->m_impl->pitch - (dest->m_impl->width * dest->m_impl->bpp);
87-
srcbuf += src->m_impl->pitch - (src->m_impl->width * src->m_impl->bpp);
93+
dst_pixel_row_ptr += dst->m_impl->pitch;
94+
src_pixel_row_ptr += src->m_impl->pitch;
8895
}
8996
}
9097

0 commit comments

Comments
 (0)