PR #21010 opened by Kacper Michajłow (kasper93)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21010
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21010.patch

This makes consistent colors conversion between double and u8 and makes
sure that values are consistent across different platforms. Especially
when x87 math was used.

Note that libcairo internally also rounds when converting double to
integers, see _cairo_color_double_to_short().

Fixes: filter-drawvg-interpreter


From f8d26097e7a9244f70f0bec4796de07067b527b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= <[email protected]>
Date: Tue, 25 Nov 2025 02:03:47 +0100
Subject: [PATCH] avfilter/vf_drawvg: round color values and avoid intermediate
 casting to double
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This makes consistent colors conversion between double and u8 and makes
sure that values are consistent across different platforms. Especially
when x87 math was used.

Note that libcairo internally also rounds when converting double to
integers, see _cairo_color_double_to_short().

Fixes: filter-drawvg-interpreter
Signed-off-by: Kacper Michajłow <[email protected]>
---
 libavfilter/tests/drawvg.c               |  2 +-
 libavfilter/vf_drawvg.c                  | 14 ++++++--------
 tests/ref/fate/filter-drawvg-interpreter |  8 ++++----
 3 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/libavfilter/tests/drawvg.c b/libavfilter/tests/drawvg.c
index 2d151bf4d2..3d98448531 100644
--- a/libavfilter/tests/drawvg.c
+++ b/libavfilter/tests/drawvg.c
@@ -167,7 +167,7 @@ void cairo_set_source(cairo_t *cr, cairo_pattern_t *source) 
{
     printf("%s", __func__);
 
 #define PRINT_COLOR(prefix) \
-    printf(prefix "#%02x%02x%02x%02x", (int)(r*255), (int)(g*255), 
(int)(b*255), (int)(a*255))
+    printf(prefix "#%02lx%02lx%02lx%02lx", lround(r*0xFF), lround(g*0xFF), 
lround(b*0xFF), lround(a*0xFF))
 
     switch (cairo_pattern_get_type(source)) {
     case CAIRO_PATTERN_TYPE_SOLID:
diff --git a/libavfilter/vf_drawvg.c b/libavfilter/vf_drawvg.c
index fd9270ee13..5e47ac8a21 100644
--- a/libavfilter/vf_drawvg.c
+++ b/libavfilter/vf_drawvg.c
@@ -1838,7 +1838,9 @@ static int vgs_eval(
                 if (a->type == ARG_COLOR) {
                     memcpy(color, a->color, sizeof(color));
                 } else {
-                    uint32_t c = 
av_be2ne32((uint32_t)state->vars[a->variable]);
+                    uint32_t c;
+                    memcpy(&c, &state->vars[a->variable], sizeof(c));
+                    c = av_be2ne32(c);
                     memcpy(color, &c, sizeof(color));
                 }
 
@@ -1981,14 +1983,10 @@ static int vgs_eval(
                 b = numerics[3];
             }
 
-            #define C(v, o) ((uint32_t)(av_clipd(v, 0, 1) * 255) << o)
+            #define C(v, o) (lround(av_clipd(v, 0, 1) * 0xFF) << o)
 
-            state->vars[user_var] = (double)(
-                C(r, 24)
-                | C(g, 16)
-                | C(b, 8)
-                | C(numerics[4], 0)
-            );
+            uint32_t color = C(r, 24) | C(g, 16) | C(b, 8) | C(numerics[4], 0);
+            memcpy(&state->vars[user_var], &color, sizeof(color));
 
             #undef C
 
diff --git a/tests/ref/fate/filter-drawvg-interpreter 
b/tests/ref/fate/filter-drawvg-interpreter
index 21c6ccd848..3fc33e9c07 100644
--- a/tests/ref/fate/filter-drawvg-interpreter
+++ b/tests/ref/fate/filter-drawvg-interpreter
@@ -64,16 +64,16 @@ cairo_set_dash [ -1.0 ] 4.0
 cairo_set_dash [ ] 0.0
 cairo_move_to 1.0 2.0
 cairo_rel_line_to -1.0 -2.0
-cairo_set_source #19334c66
+cairo_set_source #1a334d66
 cairo_set_fill_rule 0
 cairo_fill
-cairo_set_source #475b3d66
+cairo_set_source #475c3d66
 cairo_set_fill_rule 0
 cairo_fill
-cairo_set_source #7f99b2cc
+cairo_set_source #8099b3cc
 cairo_set_fill_rule 0
 cairo_fill
-cairo_set_source #a8d7efe5
+cairo_set_source #a8d8f0e6
 cairo_set_fill_rule 0
 cairo_fill
 cairo_rel_line_to 1.0 3.0
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to