From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 079EB40C42 for ; Sat, 5 Nov 2022 15:28:56 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 021DC68B925; Sat, 5 Nov 2022 17:26:51 +0200 (EET) Received: from ssq0.pkh.me (laubervilliers-656-1-228-164.w92-154.abo.wanadoo.fr [92.154.28.164]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2CDB668B49D for ; Sat, 5 Nov 2022 17:26:38 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pkh.me; s=selector1; t=1667661982; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Xzq+/W+OdRJgfYr6HKIN0fpOk11RD2qajjnTW9l8EXo=; b=BNaGSENilyaqpQbeD1KS/a5Wchjyjdz5iL7glsV6dy3EWUrRU+HmkQgKq2YvVdn5+hpj+t c/p5XBjHkmWhSbK1WcxjR/Ok1Mwzhw5aHlYfzsK9mra8h80UJGGc6AM8xUOEtJITtFlooU JG1VrF7JGGE3rqlub61T8aVWNTbpzpQ= Received: from localhost (ssq0.pkh.me [local]) by ssq0.pkh.me (OpenSMTPD) with ESMTPA id be936ecb; Sat, 5 Nov 2022 15:26:22 +0000 (UTC) From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Nov 2022 16:26:11 +0100 Message-Id: <20221105152617.1809282-10-u@pkh.me> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221105152617.1809282-1-u@pkh.me> References: <20221105152617.1809282-1-u@pkh.me> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/15] avfilter/palettegen: average color in linear space X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: sRGB colors are gamma encoded, averaging them naively is incorrect. --- libavfilter/vf_palettegen.c | 18 ++++++++++-------- tests/ref/fate/filter-palettegen-1 | 2 +- tests/ref/fate/filter-palettegen-2 | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/libavfilter/vf_palettegen.c b/libavfilter/vf_palettegen.c index d335ef91e6..00bc323d17 100644 --- a/libavfilter/vf_palettegen.c +++ b/libavfilter/vf_palettegen.c @@ -30,6 +30,7 @@ #include "libavutil/intreadwrite.h" #include "avfilter.h" #include "internal.h" +#include "palette.h" /* Reference a color and how much it's used */ struct color_ref { @@ -186,21 +187,22 @@ static int get_next_box_id_to_split(PaletteGenContext *s) static uint32_t get_avg_color(struct color_ref * const *refs, const struct range_box *box) { - int i; + int i, r, g, b; const int n = box->len; - uint64_t r = 0, g = 0, b = 0, div = 0; + uint64_t div = 0; + double rf = 0.0, gf = 0.0, bf = 0.0; for (i = 0; i < n; i++) { const struct color_ref *ref = refs[box->start + i]; - r += (ref->color >> 16 & 0xff) * ref->count; - g += (ref->color >> 8 & 0xff) * ref->count; - b += (ref->color & 0xff) * ref->count; + rf += ff_srgb_u8_to_linear_f32(ref->color >> 16 & 0xff) * ref->count; + gf += ff_srgb_u8_to_linear_f32(ref->color >> 8 & 0xff) * ref->count; + bf += ff_srgb_u8_to_linear_f32(ref->color & 0xff) * ref->count; div += ref->count; } - r = r / div; - g = g / div; - b = b / div; + r = ff_linear_f32_to_srgb_u8(rf / div); + g = ff_linear_f32_to_srgb_u8(gf / div); + b = ff_linear_f32_to_srgb_u8(bf / div); return 0xffU<<24 | r<<16 | g<<8 | b; } diff --git a/tests/ref/fate/filter-palettegen-1 b/tests/ref/fate/filter-palettegen-1 index bebfd24e19..df3b714ebb 100644 --- a/tests/ref/fate/filter-palettegen-1 +++ b/tests/ref/fate/filter-palettegen-1 @@ -3,4 +3,4 @@ #codec_id 0: rawvideo #dimensions 0: 16x16 #sar 0: 1/1 -0, 0, 0, 1, 1024, 0x3395ef5a +0, 0, 0, 1, 1024, 0x69ec37aa diff --git a/tests/ref/fate/filter-palettegen-2 b/tests/ref/fate/filter-palettegen-2 index 9abec0fe8e..08320a8359 100644 --- a/tests/ref/fate/filter-palettegen-2 +++ b/tests/ref/fate/filter-palettegen-2 @@ -3,4 +3,4 @@ #codec_id 0: rawvideo #dimensions 0: 16x16 #sar 0: 1/1 -0, 0, 0, 1, 1024, 0x23e072c8 +0, 0, 0, 1, 1024, 0x76078b2e -- 2.38.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".