fix: blend the Y plane in place so the chroma stays current after an Assign refs #5601

This commit is contained in:
Jash committed 2026-11-12 19:31:03 +05:30
1 parent 7def219319
commit 0574a4eb37
2 files changed
+23 -7

No files matched your search

+8 -7
View File
@@ -2475,24 +2475,25 @@ void Image::Blend( const Image &image, int transparency ) {
if ( transparency <= 0 )
return;
// Delta only reads the Y plane of planar YUV, so blend that in place and leave the chroma alone.
if (imagePixFormat != AV_PIX_FMT_GRAY8 && zm_bytes_per_pixel(imagePixFormat) == 1) {
(*blend)(buffer, image.buffer, buffer, static_cast<size_t>(linesize) * height, transparency);
return;
}
// Reuse persistent blend buffer to avoid per-frame alloc/free
if (blend_buffer_size_ < size) {
if (blend_buffer_) zm_freealigned(blend_buffer_);
blend_buffer_ = AllocBuffer(size);
blend_buffer_size_ = size;
memcpy(blend_buffer_, buffer, size);
}
// Delta only reads the Y plane of planar YUV, so leave the chroma alone.
const bool planar_yuv = imagePixFormat != AV_PIX_FMT_GRAY8 && zm_bytes_per_pixel(imagePixFormat) == 1;
const size_t blend_size = planar_yuv ? static_cast<size_t>(linesize) * height : size;
#ifdef ZM_IMAGE_PROFILING
TimePoint start = std::chrono::steady_clock::now();
#endif
/* Do the blending */
(*blend)(buffer, image.buffer, blend_buffer_, blend_size, transparency);
(*blend)(buffer, image.buffer, blend_buffer_, size, transparency);
#ifdef ZM_IMAGE_PROFILING
TimePoint end = std::chrono::steady_clock::now();
@@ -2506,7 +2507,7 @@ void Image::Blend( const Image &image, int transparency ) {
#endif
if (holdbuffer) {
(*fptr_imgbufcpy)(buffer, blend_buffer_, blend_size);
(*fptr_imgbufcpy)(buffer, blend_buffer_, size);
} else {
std::swap(buffer, blend_buffer_);
buffertype = ZM_BUFTYPE_ZM;
+15
View File
@@ -746,4 +746,19 @@ TEST_CASE("Image::Blend YUV420P blends only luma", "[image]") {
REQUIRE(p.data[2][y * p.stride[2] + x] == 0x22);
}
}
// A same-sized Assign reuses the buffer; the next blend must keep its chroma.
Image fresh(w, h, ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_YUV420P);
Planes np = plane_view(fresh, AV_PIX_FMT_YUV420P, w, h);
memset(np.data[0], 0x80, ysize);
memset(np.data[1], 0x33, np.stride[1] * (h / 2));
memset(np.data[2], 0x44, np.stride[2] * (h / 2));
ref.Assign(fresh);
ref.Blend(frame, 25);
Planes p = plane_view(ref, AV_PIX_FMT_YUV420P, w, h);
for (int y = 0; y < h / 2; y++)
for (int x = 0; x < w / 2; x++) {
REQUIRE(p.data[1][y * p.stride[1] + x] == 0x33);
REQUIRE(p.data[2][y * p.stride[2] + x] == 0x44);
}
}