[igt-dev] [PATCH i-g-t 6/8] lib: Add igt_color_encoding

Ville Syrjala ville.syrjala at linux.intel.com
Tue Feb 27 21:21:18 UTC 2018


From: Ville Syrjälä <ville.syrjala at linux.intel.com>

Add some helpers for generating rgb<->ycbcr conversion matrices.

Signed-off-by: Ville Syrjälä <ville.syrjala at linux.intel.com>
---
 lib/Makefile.sources     |   2 +
 lib/igt_color_encoding.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_color_encoding.h |  45 ++++++++++++++
 lib/meson.build          |   1 +
 4 files changed, 196 insertions(+)
 create mode 100644 lib/igt_color_encoding.c
 create mode 100644 lib/igt_color_encoding.h

diff --git a/lib/Makefile.sources b/lib/Makefile.sources
index 622d73d28fcd..b8e9c0813a7e 100644
--- a/lib/Makefile.sources
+++ b/lib/Makefile.sources
@@ -21,6 +21,8 @@ lib_source_list =	 	\
 	igt_device.h		\
 	igt_aux.c		\
 	igt_aux.h		\
+	igt_color_encoding.c	\
+	igt_color_encoding.h	\
 	igt_edid_template.h	\
 	igt_gt.c		\
 	igt_gt.h		\
diff --git a/lib/igt_color_encoding.c b/lib/igt_color_encoding.c
new file mode 100644
index 000000000000..a36e29c88b5a
--- /dev/null
+++ b/lib/igt_color_encoding.c
@@ -0,0 +1,148 @@
+/*
+ * Copyright © 2018 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "igt_color_encoding.h"
+#include "igt_matrix.h"
+
+const struct igt_color_encoding igt_ycbcr_bt601 = {
+	.name = "ITU-R BT.601 YCbCr",
+	.kr = .299f, .kb = .114f,
+};
+
+const struct igt_color_encoding igt_ycbcr_bt709 = {
+	.name = "ITU-R BT.709 YCbCr",
+	.kr = .2126f, .kb = .0722f,
+};
+
+const struct igt_color_encoding igt_ycbcr_bt2020 = {
+	.name = "ITU-R BT.2020 YCbCr",
+	.kr = .2627f, .kb = .0593f,
+};
+
+static struct igt_mat4 rgb_to_ycbcr_matrix(const struct igt_color_encoding *e)
+{
+	float kr, kg, kb;
+
+	kr = e->kr;
+	kb = e->kb;
+	kg = 1.0f - kr - kb;
+
+	struct igt_mat4 ret = {
+		.d[0 * 4 + 0] = kr,
+		.d[1 * 4 + 0] = kg,
+		.d[2 * 4 + 0] = kb,
+
+		.d[0 * 4 + 1] = -kr / (1.0f - kb),
+		.d[1 * 4 + 1] = -kg / (1.0f - kb),
+		.d[2 * 4 + 1] = 1.0f,
+
+		.d[0 * 4 + 2] = 1.0f,
+		.d[1 * 4 + 2] = -kg / (1.0f - kr),
+		.d[2 * 4 + 2] = -kb / (1.0f - kr),
+
+		.d[3 * 4 + 3] = 1.0f,
+	};
+
+	return ret;
+}
+
+static struct igt_mat4 ycbcr_to_rgb_matrix(const struct igt_color_encoding *e)
+{
+	float kr, kg, kb;
+
+	kr = e->kr;
+	kb = e->kb;
+	kg = 1.0f - kr - kb;
+
+	struct igt_mat4 ret = {
+		.d[0 * 4 + 0] = 1.0f,
+		.d[1 * 4 + 0] = 0.0f,
+		.d[2 * 4 + 0] = 1.0 - kr,
+
+		.d[0 * 4 + 1] = 1.0f,
+		.d[1 * 4 + 1] = -(1.0 - kb) * kb / kg,
+		.d[2 * 4 + 1] = -(1.0 - kr) * kr / kg,
+
+		.d[0 * 4 + 2] = 1.0f,
+		.d[1 * 4 + 2] = 1.0 - kb,
+		.d[2 * 4 + 2] = 0.0f,
+
+		.d[3 * 4 + 3] = 1.0f,
+	};
+
+	return ret;
+}
+
+static struct igt_mat4 ycbcr_limited_to_full_range_matrix(void)
+{
+	struct igt_mat4 t = igt_matrix_translate(-16.0f,
+						 -128.0f,
+						 -128.0f);
+	struct igt_mat4 s = igt_matrix_scale(255.0f / (235.0f - 16.0f),
+					     255.0f / (240.0f - 128.0f),
+					     255.0f / (240.0f - 128.0f));
+
+	return igt_matrix_multiply(&s, &t);
+}
+
+static struct igt_mat4 ycbcr_full_to_limited_range_matrix(void)
+{
+	struct igt_mat4 s = igt_matrix_scale((235.0f - 16.0f) / 255.0f,
+					     (240.0f - 128.0f) / 255.0f,
+					     (240.0f - 128.0f) / 255.0f);
+	struct igt_mat4 t = igt_matrix_translate(16.0f,
+						 128.0f,
+						 128.0f);
+
+	return igt_matrix_multiply(&t, &s);
+}
+
+struct igt_mat4 igt_ycbcr_to_rgb_matrix(const struct igt_color_encoding *e,
+					bool full_range)
+{
+	struct igt_mat4 c, r;
+
+	c = ycbcr_to_rgb_matrix(e);
+
+	if (full_range)
+		r = igt_matrix_translate(0.0f, -128.0f, -128.0f);
+	else
+		r = ycbcr_limited_to_full_range_matrix();
+
+	return igt_matrix_multiply(&c, &r);
+}
+
+struct igt_mat4 igt_rgb_to_ycbcr_matrix(const struct igt_color_encoding *e,
+					bool full_range)
+{
+	struct igt_mat4 c, r;
+
+	c = rgb_to_ycbcr_matrix(e);
+
+	if (full_range)
+		r = igt_matrix_translate(0.0f, 128.0f, 128.0f);
+	else
+		r = ycbcr_full_to_limited_range_matrix();
+
+	return igt_matrix_multiply(&r, &c);
+}
diff --git a/lib/igt_color_encoding.h b/lib/igt_color_encoding.h
new file mode 100644
index 000000000000..e8c6a3cc8bbe
--- /dev/null
+++ b/lib/igt_color_encoding.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright © 2018 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef __IGT_COLOR_ENCODING_H__
+#define __IGT_COLOR_ENCODING_H__
+
+#include <stdbool.h>
+
+#include "igt_matrix.h"
+
+struct igt_color_encoding {
+	const char *name;
+	float kr, kb;
+};
+
+const struct igt_color_encoding igt_ycbcr_bt709;
+const struct igt_color_encoding igt_ycbcr_bt601;
+const struct igt_color_encoding igt_ycbcr_bt2020;
+
+struct igt_mat4 igt_ycbcr_to_rgb_matrix(const struct igt_color_encoding *e,
+					bool full_range);
+struct igt_mat4 igt_rgb_to_ycbcr_matrix(const struct igt_color_encoding *e,
+					bool full_range);
+
+#endif /* __IGT_COLOR_ENCODING_H__ */
diff --git a/lib/meson.build b/lib/meson.build
index 8bf6f4433f64..0d4cd2ba9cb0 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -4,6 +4,7 @@ lib_sources = [
 	'i915/gem_scheduler.c',
 	'i915/gem_submission.c',
 	'i915/gem_ring.c',
+	'igt_color_encoding.c',
 	'igt_debugfs.c',
 	'igt_device.c',
 	'igt_aux.c',
-- 
2.13.6



More information about the igt-dev mailing list