diff mbox series

[vsp-tests,04/16] gen-image: Round sub-sampled chroma towards zero

Message ID 20250409004758.11014-5-laurent.pinchart@ideasonboard.com (mailing list archive)
State New
Delegated to: Geert Uytterhoeven
Headers show
Series Add color space conversion test | expand

Commit Message

Laurent Pinchart April 9, 2025, 12:47 a.m. UTC
Based on tests, the VSP1 hardware seems to round averaged chroma values
towards 0 when subsampling YUV formats. Replicate the same calculation
in gen-image.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/gen-image.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/src/gen-image.c b/src/gen-image.c
index 170d69490399..d053d7906159 100644
--- a/src/gen-image.c
+++ b/src/gen-image.c
@@ -618,8 +618,15 @@  static void image_format_yuv_packed(const struct image *input, struct image *out
 				o_c[2*x + u_offset] = idata[3*x + 1];
 				o_c[2*x + v_offset] = idata[3*x + 2];
 			} else {
-				o_c[2*x + u_offset] = (idata[3*x + 1] + idata[3*x + 4]) / 2;
-				o_c[2*x + v_offset] = (idata[3*x + 2] + idata[3*x + 5]) / 2;
+				unsigned int u = idata[3*x + 1] + idata[3*x + 4];
+				unsigned int v = idata[3*x + 2] + idata[3*x + 5];
+
+				/* Round towards the middle value. */
+				u += u < 0x100 ? 1 : 0;
+				v += v < 0x100 ? 1 : 0;
+
+				o_c[2*x + u_offset] = u / 2;
+				o_c[2*x + v_offset] = v / 2;
 			}
 		}
 
@@ -674,8 +681,15 @@  static void image_format_yuv_planar(const struct image *input, struct image *out
 			}
 		} else {
 			for (x = 0; x < output->width; x += xsub) {
-				o_u[x*c_stride/xsub] = (idata[3*x + 1] + idata[3*x + 4]) / 2;
-				o_v[x*c_stride/xsub] = (idata[3*x + 2] + idata[3*x + 5]) / 2;
+				unsigned int u = idata[3*x + 1] + idata[3*x + 4];
+				unsigned int v = idata[3*x + 2] + idata[3*x + 5];
+
+				/* Round towards the middle value. */
+				u += u < 0x100 ? 1 : 0;
+				v += v < 0x100 ? 1 : 0;
+
+				o_u[x*c_stride/xsub] = u / 2;
+				o_v[x*c_stride/xsub] = v / 2;
 			}
 		}