From 0574a4eb37d906c730ef814caa13ee870a5fb67e Mon Sep 17 00:00:00 2001 From: Jash Date: Thu, 12 Nov 2026 19:31:03 +0530 Subject: [PATCH] fix: blend the Y plane in place so the chroma stays current after an Assign refs #5601 --- src/zm_image.cpp | 15 ++++++++------- tests/zm_image.cpp | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/zm_image.cpp b/src/zm_image.cpp index fc32c309ca..85caf68d8c 100644 --- a/src/zm_image.cpp +++ b/src/zm_image.cpp @@ -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(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(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; diff --git a/tests/zm_image.cpp b/tests/zm_image.cpp index db806e65c5..badd6f624a 100644 --- a/tests/zm_image.cpp +++ b/tests/zm_image.cpp @@ -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); + } }