[gnome-color-manager] trivial: Add GcmHull for future use as a datastore for a 3D gamut hull



commit b165e76180462aa3ec5519f5dc3a00ceabc0a75d
Author: Richard Hughes <richard hughsie com>
Date:   Sat Mar 26 18:15:26 2011 +0000

    trivial: Add GcmHull for future use as a datastore for a 3D gamut hull

 src/Makefile.am     |    2 +
 src/gcm-hull.c      |  275 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gcm-hull.h      |   69 +++++++++++++
 src/gcm-self-test.c |   61 +++++++++++
 4 files changed, 407 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index ae4633d..92f35ac 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -34,6 +34,8 @@ libgcmshared_a_SOURCES =				\
 	gcm-brightness.h				\
 	gcm-buffer.c					\
 	gcm-buffer.h					\
+	gcm-hull.c					\
+	gcm-hull.h					\
 	gcm-calibrate-argyll.c				\
 	gcm-calibrate-argyll.h				\
 	gcm-calibrate.c					\
diff --git a/src/gcm-hull.c b/src/gcm-hull.c
new file mode 100644
index 0000000..1745c05
--- /dev/null
+++ b/src/gcm-hull.c
@@ -0,0 +1,275 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2011 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+#include <math.h>
+
+#include "gcm-color.h"
+#include "gcm-hull.h"
+
+static void     gcm_hull_finalize	(GObject     *object);
+
+#define GCM_HULL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GCM_TYPE_HULL, GcmHullPrivate))
+
+/**
+ * GcmHullPrivate:
+ *
+ * Private #GcmHull data
+ **/
+struct _GcmHullPrivate
+{
+	GPtrArray		*vertices;
+	GPtrArray		*faces;
+	guint			 flags;
+};
+
+typedef struct {
+	GcmColorXYZ	 xyz;
+	GcmColorRGB	 color;
+} GcmHullVertex;
+
+typedef struct {
+	guint		*data;
+	gsize		 size;
+} GcmHullFace;
+
+enum {
+	PROP_0,
+	PROP_FLAGS,
+	PROP_LAST
+};
+
+G_DEFINE_TYPE (GcmHull, gcm_hull, G_TYPE_OBJECT)
+
+/**
+ * g_hull_vertex_free:
+ **/
+static void
+g_hull_vertex_free (GcmHullVertex *vertex)
+{
+	g_slice_free (GcmHullVertex, vertex);
+}
+
+/**
+ * g_hull_face_free:
+ **/
+static void
+g_hull_face_free (GcmHullFace *face)
+{
+	g_free (face->data);
+	g_slice_free (GcmHullFace, face);
+}
+
+/**
+ * gcm_hull_add_vertex:
+ **/
+void
+gcm_hull_add_vertex (GcmHull *hull,
+		     GcmColorXYZ *xyz,
+		     GcmColorRGB *color)
+{
+	GcmHullVertex *new;
+	new = g_slice_new (GcmHullVertex);
+	gcm_color_copy_XYZ (xyz, &new->xyz);
+	gcm_color_copy_RGB (color, &new->color);
+	g_ptr_array_add (hull->priv->vertices, new);
+}
+
+/**
+ * gcm_hull_add_face:
+ **/
+void
+gcm_hull_add_face (GcmHull *hull,
+		   const guint *data,
+		   gsize size)
+{
+	GcmHullFace *face;
+	face = g_slice_new (GcmHullFace);
+	face->data = g_memdup (data, sizeof(guint) * size);
+	face->size = size;
+	g_ptr_array_add (hull->priv->faces, face);
+}
+
+/**
+ * gcm_hull_export_to_ply:
+ **/
+gchar *
+gcm_hull_export_to_ply (GcmHull *hull)
+{
+	GString *string;
+	guint i;
+	GcmHullFace *face;
+	GcmHullVertex *vertex;
+	GcmColorRGBint tmp;
+
+	string = g_string_new ("ply\nformat ascii 1.0\n");
+	g_string_append_printf (string, "element vertex %i\n",
+				hull->priv->vertices->len);
+	g_string_append (string, "property float x\n");
+	g_string_append (string, "property float y\n");
+	g_string_append (string, "property float z\n");
+	g_string_append (string, "property uchar red\n");
+	g_string_append (string, "property uchar green\n");
+	g_string_append (string, "property uchar blue\n");
+	g_string_append_printf (string, "element face %i\n",
+				hull->priv->faces->len);
+	g_string_append (string, "property list uchar uint vertex_indices\n");
+	g_string_append (string, "end_header\n");
+
+	for (i=0; i<hull->priv->vertices->len; i++) {
+		vertex = g_ptr_array_index (hull->priv->vertices, i);
+		gcm_color_convert_RGB_to_RGBint (&vertex->color, &tmp);
+		g_string_append_printf (string, "%lf %lf %lf %i %i %i\n",
+					vertex->xyz.X,
+					vertex->xyz.Y,
+					vertex->xyz.Z,
+					tmp.R,
+					tmp.G,
+					tmp.B);
+	}
+
+	for (i=0; i<hull->priv->faces->len; i++) {
+		face = g_ptr_array_index (hull->priv->faces, i);
+		g_string_append_printf (string, "3 %i %i %i\n",
+					face->data[0],
+					face->data[1],
+					face->data[2]);
+	}
+
+	return g_string_free (string, FALSE);
+}
+
+/**
+ * gcm_hull_get_flags:
+ **/
+guint
+gcm_hull_get_flags (GcmHull *hull)
+{
+	g_return_val_if_fail (GCM_IS_HULL (hull), 0);
+	return hull->priv->flags;
+}
+
+/**
+ * gcm_hull_set_flags:
+ **/
+void
+gcm_hull_set_flags (GcmHull *hull, guint flags)
+{
+	g_return_if_fail (GCM_IS_HULL (hull));
+	hull->priv->flags = flags;
+}
+
+
+/**
+ * gcm_hull_get_property:
+ **/
+static void
+gcm_hull_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+{
+	GcmHull *hull = GCM_HULL (object);
+	GcmHullPrivate *priv = hull->priv;
+
+	switch (prop_id) {
+	case PROP_FLAGS:
+		g_value_set_uint (value, priv->flags);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+/**
+ * gcm_hull_set_property:
+ **/
+static void
+gcm_hull_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+{
+	switch (prop_id) {
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+/**
+ * gcm_hull_class_init:
+ **/
+static void
+gcm_hull_class_init (GcmHullClass *klass)
+{
+	GParamSpec *pspec;
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	object_class->finalize = gcm_hull_finalize;
+	object_class->get_property = gcm_hull_get_property;
+	object_class->set_property = gcm_hull_set_property;
+
+	/**
+	 * GcmHull:width:
+	 */
+	pspec = g_param_spec_uint ("flags", "flags for rendering", NULL,
+				   0, G_MAXUINT, 0,
+				   G_PARAM_READABLE);
+	g_object_class_install_property (object_class, PROP_FLAGS, pspec);
+
+	g_type_class_add_private (klass, sizeof (GcmHullPrivate));
+}
+
+/**
+ * gcm_hull_init:
+ **/
+static void
+gcm_hull_init (GcmHull *hull)
+{
+	hull->priv = GCM_HULL_GET_PRIVATE (hull);
+	hull->priv->vertices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_hull_vertex_free);
+	hull->priv->faces = g_ptr_array_new_with_free_func ((GDestroyNotify) g_hull_face_free);
+}
+
+/**
+ * gcm_hull_finalize:
+ **/
+static void
+gcm_hull_finalize (GObject *object)
+{
+	GcmHull *hull = GCM_HULL (object);
+	GcmHullPrivate *priv = hull->priv;
+
+	g_ptr_array_unref (priv->vertices);
+	g_ptr_array_unref (priv->faces);
+
+	G_OBJECT_CLASS (gcm_hull_parent_class)->finalize (object);
+}
+
+/**
+ * gcm_hull_new:
+ *
+ * Return value: a new #GcmHull object.
+ **/
+GcmHull *
+gcm_hull_new (void)
+{
+	GcmHull *hull;
+	hull = g_object_new (GCM_TYPE_HULL, NULL);
+	return GCM_HULL (hull);
+}
diff --git a/src/gcm-hull.h b/src/gcm-hull.h
new file mode 100644
index 0000000..20d28da
--- /dev/null
+++ b/src/gcm-hull.h
@@ -0,0 +1,69 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2011 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __GCM_HULL_H
+#define __GCM_HULL_H
+
+#include <glib-object.h>
+
+#include "gcm-color.h"
+
+G_BEGIN_DECLS
+
+#define GCM_TYPE_HULL		(gcm_hull_get_type ())
+#define GCM_HULL(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), GCM_TYPE_HULL, GcmHull))
+#define GCM_HULL_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST((k), GCM_TYPE_HULL, GcmHullClass))
+#define GCM_IS_HULL(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), GCM_TYPE_HULL))
+#define GCM_IS_HULL_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), GCM_TYPE_HULL))
+#define GCM_HULL_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), GCM_TYPE_HULL, GcmHullClass))
+
+typedef struct _GcmHullPrivate	GcmHullPrivate;
+typedef struct _GcmHull		GcmHull;
+typedef struct _GcmHullClass	GcmHullClass;
+
+struct _GcmHull
+{
+	 GObject		 parent;
+	 GcmHullPrivate		*priv;
+};
+
+struct _GcmHullClass
+{
+	GObjectClass	parent_class;
+};
+
+GType		 gcm_hull_get_type		  	(void);
+GcmHull		*gcm_hull_new				(void);
+void		 gcm_hull_add_vertex			(GcmHull	*hull,
+							 GcmColorXYZ	*xyz,
+							 GcmColorRGB	*color);
+void		 gcm_hull_add_face			(GcmHull	*hull,
+							 const guint	*data,
+							 gsize		 size);
+gchar		*gcm_hull_export_to_ply			(GcmHull	*hull);
+guint		 gcm_hull_get_flags			(GcmHull	*hull);
+void		 gcm_hull_set_flags			(GcmHull	*hull,
+							 guint		 flags);
+
+G_END_DECLS
+
+#endif /* __GCM_HULL_H */
+
diff --git a/src/gcm-self-test.c b/src/gcm-self-test.c
index 01a3318..2a52059 100644
--- a/src/gcm-self-test.c
+++ b/src/gcm-self-test.c
@@ -37,6 +37,7 @@
 #include "gcm-edid.h"
 #include "gcm-exif.h"
 #include "gcm-gamma-widget.h"
+#include "gcm-hull.h"
 #include "gcm-image.h"
 #include "gcm-math.h"
 #include "gcm-print.h"
@@ -299,6 +300,65 @@ gcm_test_tables_func (void)
 }
 
 static void
+gcm_test_hull_func (void)
+{
+	GcmHull *hull;
+	GcmColorXYZ xyz;
+	GcmColorRGB color;
+	guint faces[3];
+	gchar *data;
+
+	hull = gcm_hull_new ();
+	g_assert (hull != NULL);
+
+	gcm_hull_set_flags (hull, 8);
+	g_assert_cmpint (gcm_hull_get_flags (hull), ==, 8);
+
+	/* add a point */
+	xyz.X = 1.0;
+	xyz.Y = 2.0;
+	xyz.Z = 3.0;
+	color.R = 0.25;
+	color.G = 0.5;
+	color.B = 1.0;
+	gcm_hull_add_vertex (hull, &xyz, &color);
+
+	/* add another two */
+	xyz.Z = 2.0;
+	gcm_hull_add_vertex (hull, &xyz, &color);
+	xyz.X = 2.0;
+	gcm_hull_add_vertex (hull, &xyz, &color);
+
+	/* add a face */
+	faces[0] = 0;
+	faces[1] = 1;
+	faces[2] = 2;
+	gcm_hull_add_face (hull, faces, 3);
+
+	/* export to a PLY file */
+	data = gcm_hull_export_to_ply (hull);
+	g_assert_cmpstr (data, ==, "ply\n"
+				   "format ascii 1.0\n"
+				   "element vertex 3\n"
+				   "property float x\n"
+				   "property float y\n"
+				   "property float z\n"
+				   "property uchar red\n"
+				   "property uchar green\n"
+				   "property uchar blue\n"
+				   "element face 1\n"
+				   "property list uchar uint vertex_indices\n"
+				   "end_header\n"
+				   "1.000000 2.000000 3.000000 63 127 255\n"
+				   "1.000000 2.000000 2.000000 63 127 255\n"
+				   "2.000000 2.000000 2.000000 63 127 255\n"
+				   "3 0 1 2\n");
+	g_free (data);
+
+	g_object_unref (hull);
+}
+
+static void
 gcm_test_profile_func (void)
 {
 	GcmProfile *profile;
@@ -1211,6 +1271,7 @@ main (int argc, char **argv)
 	g_test_add_func ("/color/utils", gcm_test_utils_func);
 	g_test_add_func ("/color/calibrate_dialog", gcm_test_calibrate_dialog_func);
 	g_test_add_func ("/color/math", gcm_test_math_func);
+	g_test_add_func ("/color/hull", gcm_test_hull_func);
 	g_test_add_func ("/color/sensor", gcm_test_sensor_func);
 	g_test_add_func ("/color/edid", gcm_test_edid_func);
 	g_test_add_func ("/color/tables", gcm_test_tables_func);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]