[gnome-color-manager] Add named color profile support to gcm-viewer



commit 48b6ab8289d9286271ae5f9bbc1046e9747dc87a
Author: Richard Hughes <richard hughsie com>
Date:   Mon Apr 11 13:13:54 2011 +0100

    Add named color profile support to gcm-viewer

 data/gcm-viewer.ui |   78 +++++++++++++++++++++++++++++++
 src/gcm-profile.c  |  105 ++++++++++++++++++++++++++++++++++++++++++
 src/gcm-profile.h  |    2 +
 src/gcm-viewer.c   |  129 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 310 insertions(+), 4 deletions(-)
---
diff --git a/data/gcm-viewer.ui b/data/gcm-viewer.ui
index 734f7b5..94473d0 100644
--- a/data/gcm-viewer.ui
+++ b/data/gcm-viewer.ui
@@ -1304,6 +1304,84 @@
                     <property name="tab_fill">False</property>
                   </packing>
                 </child>
+                <child>
+                  <object class="GtkVBox" id="vbox_named_colors">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="border_width">9</property>
+                    <property name="spacing">6</property>
+                    <child>
+                      <object class="GtkBox" id="box_named_colors">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="spacing">6</property>
+                        <child>
+                          <object class="GtkScrolledWindow" id="scrolledwindow_named_colors">
+                            <property name="visible">True</property>
+                            <property name="can_focus">True</property>
+                            <property name="shadow_type">in</property>
+                            <child>
+                              <object class="GtkTreeView" id="treeview_named_colors">
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="model">liststore_named_colors</property>
+                                <property name="headers_visible">False</property>
+                              </object>
+                            </child>
+                          </object>
+                          <packing>
+                            <property name="expand">True</property>
+                            <property name="fill">True</property>
+                            <property name="position">0</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <object class="GtkImage" id="image_named_colors">
+                            <property name="can_focus">False</property>
+                            <property name="stock">gtk-cdrom</property>
+                            <property name="icon-size">6</property>
+                          </object>
+                          <packing>
+                            <property name="expand">False</property>
+                            <property name="fill">False</property>
+                            <property name="position">1</property>
+                          </packing>
+                        </child>
+                      </object>
+                      <packing>
+                        <property name="expand">True</property>
+                        <property name="fill">True</property>
+                        <property name="position">0</property>
+                      </packing>
+                    </child>
+                    <child>
+                      <object class="GtkLabel" id="label24">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">Named colors are specific colors that are defined in the profile</property>
+                      </object>
+                      <packing>
+                        <property name="expand">False</property>
+                        <property name="fill">False</property>
+                        <property name="position">1</property>
+                      </packing>
+                    </child>
+                  </object>
+                  <packing>
+                    <property name="position">7</property>
+                  </packing>
+                </child>
+                <child type="tab">
+                  <object class="GtkLabel" id="label21">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="label" translatable="yes">Named Colors</property>
+                  </object>
+                  <packing>
+                    <property name="position">7</property>
+                    <property name="tab_fill">False</property>
+                  </packing>
+                </child>
               </object>
               <packing>
                 <property name="expand">True</property>
diff --git a/src/gcm-profile.c b/src/gcm-profile.c
index 9c43d97..4d292de 100644
--- a/src/gcm-profile.c
+++ b/src/gcm-profile.c
@@ -38,6 +38,7 @@
 
 #include "gcm-profile.h"
 #include "gcm-hull.h"
+#include "gcm-named-color.h"
 
 static void     gcm_profile_finalize	(GObject     *object);
 
@@ -1919,6 +1920,110 @@ out:
 }
 
 /**
+ * gcm_profile_get_named_colors:
+ **/
+GPtrArray *
+gcm_profile_get_named_colors (GcmProfile *profile, GError **error)
+{
+	CdColorLab lab;
+	CdColorXYZ xyz;
+	cmsHPROFILE profile_lab = NULL;
+	cmsHPROFILE profile_xyz = NULL;
+	cmsHTRANSFORM xform = NULL;
+	cmsNAMEDCOLORLIST *nc2 = NULL;
+	cmsUInt16Number pcs[3];
+	cmsUInt32Number count;
+	gboolean ret;
+	gchar name[cmsMAX_PATH];
+	gchar prefix[33];
+	gchar suffix[33];
+	gchar *utf8_tmp;
+	GcmNamedColor *nc;
+	GcmProfilePrivate *priv = profile->priv;
+	GError *error_local = NULL;
+	GPtrArray *array = NULL;
+	GString *string;
+	guint i;
+
+	/* setup a dummy transform so we can get all the named colors */
+	profile_lab = cmsCreateLab2Profile (NULL);
+	profile_xyz = cmsCreateXYZProfile ();
+	xform = cmsCreateTransform (profile_lab, TYPE_Lab_DBL,
+				    profile_xyz, TYPE_XYZ_DBL,
+				    INTENT_ABSOLUTE_COLORIMETRIC, 0);
+	if (xform == NULL) {
+		g_set_error_literal (error, 1, 0, "no transform");
+		goto out;
+	}
+
+	/* retrieve named color list from transform */
+	nc2 = cmsReadTag (priv->lcms_profile, cmsSigNamedColor2Tag);
+	if (nc2 == NULL) {
+		g_set_error_literal (error, 1, 0, "no named color list");
+		goto out;
+	}
+
+	/* get the number of NCs */
+	count = cmsNamedColorCount (nc2);
+	if (count == 0) {
+		g_set_error_literal (error, 1, 0, "no named colors");
+		goto out;
+	}
+	array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
+	for (i=0; i<count; i++) {
+
+		/* parse title */
+		string = g_string_new ("");
+		ret = cmsNamedColorInfo (nc2, i,
+					 name,
+					 prefix,
+					 suffix,
+					 (cmsUInt16Number *)&pcs,
+					 NULL);
+		if (!ret) {
+			g_warning ("failed to get NC #%i", i);
+			goto out;
+		}
+		if (prefix[0] != '\0')
+			g_string_append_printf (string, "%s ", prefix);
+		g_string_append (string, name);
+		if (suffix[0] != '\0')
+			g_string_append_printf (string, " %s", suffix);
+		utf8_tmp = g_locale_to_utf8 (string->str, -1,
+					     NULL, NULL,
+					     &error_local);
+		if (utf8_tmp == NULL) {
+			g_warning ("failed to convert '%s' to utf8: %s",
+				   string->str, error_local->message);
+			g_clear_error (&error_local);
+			g_string_free (string, TRUE);
+			continue;
+		}
+
+		/* get color */
+		cmsLabEncoded2Float ((cmsCIELab *) &lab, pcs);
+		cmsDoTransform (xform, &lab, &xyz, 1);
+
+		/* create new nc */
+		nc = gcm_named_color_new ();
+		gcm_named_color_set_title (nc, utf8_tmp);
+		gcm_named_color_set_value (nc, &xyz);
+		g_ptr_array_add (array, nc);
+
+		g_free (utf8_tmp);
+		g_string_free (string, TRUE);
+	}
+out:
+	if (profile_lab != NULL)
+		cmsCloseProfile (profile_lab);
+	if (profile_xyz != NULL)
+		cmsCloseProfile (profile_xyz);
+	if (xform != NULL)
+		cmsDeleteTransform (xform);
+	return array;
+}
+
+/**
  * gcm_profile_file_monitor_changed_cb:
  **/
 static void
diff --git a/src/gcm-profile.h b/src/gcm-profile.h
index da2bf90..c460048 100644
--- a/src/gcm-profile.h
+++ b/src/gcm-profile.h
@@ -140,6 +140,8 @@ const gchar	*gcm_profile_get_data			(GcmProfile	*profile,
 void		 gcm_profile_set_data			(GcmProfile	*profile,
 							 const gchar	*key,
 							 const gchar	*data);
+GPtrArray	*gcm_profile_get_named_colors		(GcmProfile	*profile,
+							 GError		**error);
 
 G_END_DECLS
 
diff --git a/src/gcm-viewer.c b/src/gcm-viewer.c
index 57a54d7..084b96e 100644
--- a/src/gcm-viewer.c
+++ b/src/gcm-viewer.c
@@ -34,10 +34,12 @@
 #endif
 
 #include "gcm-cell-renderer-profile-text.h"
+#include "gcm-cell-renderer-color.h"
 #include "gcm-cie-widget.h"
 #include "gcm-image.h"
 #include "gcm-profile.h"
 #include "gcm-trc-widget.h"
+#include "gcm-named-color.h"
 #include "gcm-utils.h"
 #include "gcm-debug.h"
 
@@ -60,6 +62,7 @@ typedef struct {
 	guint		 example_index;
 	gchar		*profile_id;
 	guint		 xid;
+	GtkListStore	*liststore_nc;
 } GcmViewerPrivate;
 
 typedef enum {
@@ -72,6 +75,13 @@ typedef enum {
 } GcmViewerGraphType;
 
 enum {
+	GCM_NAMED_COLORS_COLUMN_TITLE,
+	GCM_NAMED_COLORS_COLUMN_SORT,
+	GCM_NAMED_COLORS_COLUMN_COLOR,
+	GCM_NAMED_COLORS_COLUMN_LAST
+};
+
+enum {
 	GCM_PROFILES_COLUMN_ID,
 	GCM_PROFILES_COLUMN_SORT,
 	GCM_PROFILES_COLUMN_ICON,
@@ -171,6 +181,8 @@ gcm_viewer_profile_kind_to_icon_name (CdProfileKind kind)
 		return "view-refresh";
 	if (kind == CD_PROFILE_KIND_ABSTRACT)
 		return "insert-link";
+	if (kind == CD_PROFILE_KIND_NAMED_COLOR)
+		return "emblem-photos";
 	return "image-missing";
 }
 
@@ -619,6 +631,50 @@ gcm_viewer_profile_colorspace_to_string (CdColorspace colorspace)
 }
 
 /**
+ * gcm_viewer_add_named_colors:
+ **/
+static gboolean
+gcm_viewer_add_named_colors (GcmViewerPrivate *viewer,
+			     GcmProfile *profile)
+{
+	gboolean ret = FALSE;
+	GcmNamedColor *nc;
+	GError *error = NULL;
+	GPtrArray *ncs = NULL;
+	GtkTreeIter iter;
+	guint i;
+
+	/* get profile named colors */
+	ncs = gcm_profile_get_named_colors (profile, &error);
+	if (ncs == NULL) {
+		g_warning ("failed to get named colors: %s",
+			   error->message);
+		g_error_free (error);
+		goto out;
+	}
+
+	/* add items */
+	gtk_list_store_clear (viewer->liststore_nc);
+	for (i=0; i<ncs->len; i++) {
+		nc = g_ptr_array_index (ncs, i);
+		gtk_list_store_append (viewer->liststore_nc, &iter);
+		gtk_list_store_set (viewer->liststore_nc,
+				    &iter,
+				    GCM_NAMED_COLORS_COLUMN_SORT, "1",
+				    GCM_NAMED_COLORS_COLUMN_TITLE, gcm_named_color_get_title (nc),
+				    GCM_NAMED_COLORS_COLUMN_COLOR, gcm_named_color_get_value (nc),
+				    -1);
+	}
+
+	/* success */
+	ret= TRUE;
+out:
+	if (ncs != NULL)
+		g_ptr_array_unref (ncs);
+	return ret;
+}
+
+/**
  * gcm_viewer_set_profile:
  **/
 static void
@@ -656,13 +712,15 @@ gcm_viewer_set_profile (GcmViewerPrivate *viewer, CdProfile *profile)
 	g_object_unref (file);
 
 	/* set the preview widgets */
-	if (cd_profile_get_colorspace (profile) == CD_COLORSPACE_RGB) {
+	if (cd_profile_get_colorspace (profile) == CD_COLORSPACE_RGB &&
+	    cd_profile_get_kind (profile) != CD_PROFILE_KIND_NAMED_COLOR) {
 		gcm_image_set_input_profile (GCM_IMAGE(viewer->preview_widget_input), gcm_profile);
 		gcm_image_set_abstract_profile (GCM_IMAGE(viewer->preview_widget_input), NULL);
 		gcm_image_set_output_profile (GCM_IMAGE(viewer->preview_widget_output), gcm_profile);
 		gcm_image_set_abstract_profile (GCM_IMAGE(viewer->preview_widget_output), NULL);
 		show_section = TRUE;
-	} else if (cd_profile_get_colorspace (profile) == CD_COLORSPACE_LAB) {
+	} else if (cd_profile_get_colorspace (profile) == CD_COLORSPACE_LAB &&
+		   cd_profile_get_kind (profile) != CD_PROFILE_KIND_NAMED_COLOR) {
 		gcm_image_set_input_profile (GCM_IMAGE(viewer->preview_widget_input), NULL);
 		gcm_image_set_abstract_profile (GCM_IMAGE(viewer->preview_widget_input), gcm_profile);
 		gcm_image_set_output_profile (GCM_IMAGE(viewer->preview_widget_output), NULL);
@@ -677,7 +735,8 @@ gcm_viewer_set_profile (GcmViewerPrivate *viewer, CdProfile *profile)
 
 	/* setup cie widget */
 	widget = GTK_WIDGET (gtk_builder_get_object (viewer->builder, "vbox_cie"));
-	if (cd_profile_get_colorspace (profile) == CD_COLORSPACE_RGB) {
+	if (cd_profile_get_colorspace (profile) == CD_COLORSPACE_RGB &&
+	    cd_profile_get_kind (profile) != CD_PROFILE_KIND_NAMED_COLOR) {
 		gcm_cie_widget_set_from_profile (viewer->cie_widget,
 						 gcm_profile);
 		gtk_widget_show (widget);
@@ -840,6 +899,13 @@ gcm_viewer_set_profile (GcmViewerPrivate *viewer, CdProfile *profile)
 		gtk_label_set_label (GTK_LABEL(widget), profile_datetime);
 	}
 
+	/* setup named color tab */
+	ret = FALSE;
+	if (profile_kind == CD_PROFILE_KIND_NAMED_COLOR)
+		ret = gcm_viewer_add_named_colors (viewer, gcm_profile);
+	widget = GTK_WIDGET (gtk_builder_get_object (viewer->builder, "vbox_named_colors"));
+	gtk_widget_set_visible (widget, ret);
+
 	/* set delete sensitivity */
 	ret = gcm_profile_get_can_delete (gcm_profile);
 	widget = GTK_WIDGET (gtk_builder_get_object (viewer->builder, "toolbutton_profile_delete"));
@@ -978,6 +1044,46 @@ gcm_viewer_activate_cb (GApplication *application, GcmViewerPrivate *viewer)
 	gtk_window_present (window);
 }
 
+static void
+gcm_viewer_add_named_colors_columns (GcmViewerPrivate *viewer, GtkTreeView *treeview)
+{
+	GtkCellRenderer *renderer;
+	GtkTreeViewColumn *column;
+
+	/* column for text */
+	renderer = gtk_cell_renderer_text_new ();
+	g_object_set (renderer,
+		      "wrap-mode", PANGO_WRAP_WORD,
+		      NULL);
+	column = gtk_tree_view_column_new_with_attributes ("", renderer,
+							   "text", GCM_NAMED_COLORS_COLUMN_TITLE,
+							   NULL);
+	gtk_tree_view_column_set_sort_column_id (column, GCM_NAMED_COLORS_COLUMN_SORT);
+	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (viewer->liststore_nc),
+					      GCM_NAMED_COLORS_COLUMN_SORT,
+					      GTK_SORT_ASCENDING);
+	gtk_tree_view_append_column (treeview, column);
+	gtk_tree_view_column_set_expand (column, TRUE);
+
+	/* image */
+	renderer = gcm_cell_renderer_color_new ();
+	g_object_set (renderer, "stock-size", GTK_ICON_SIZE_MENU, NULL);
+	column = gtk_tree_view_column_new_with_attributes ("", renderer,
+							   "color", GCM_NAMED_COLORS_COLUMN_COLOR,
+							   NULL);
+	gtk_tree_view_append_column (treeview, column);
+	gtk_tree_view_column_set_expand (column, FALSE);
+}
+
+/**
+ * gcm_viewer_named_color_treeview_clicked:
+ **/
+static void
+gcm_viewer_named_color_treeview_clicked (GtkTreeSelection *selection, GcmViewerPrivate *viewer)
+{
+	g_debug ("named color changed");
+}
+
 /**
  * gcm_viewer_startup_cb:
  **/
@@ -1010,7 +1116,7 @@ gcm_viewer_startup_cb (GApplication *application, GcmViewerPrivate *viewer)
 
 	/* add application specific icons to search path */
 	gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
-	                                   GCM_DATA G_DIR_SEPARATOR_S "icons");
+					   GCM_DATA G_DIR_SEPARATOR_S "icons");
 
 	/* maintain a list of profiles */
 	viewer->client = cd_client_new ();
@@ -1084,6 +1190,21 @@ gcm_viewer_startup_cb (GApplication *application, GcmViewerPrivate *viewer)
 	g_signal_connect (widget, "clicked",
 			  G_CALLBACK (gcm_viewer_image_prev_cb), viewer);
 
+	/* use named colors */
+	widget = GTK_WIDGET (gtk_builder_get_object (viewer->builder,
+						     "treeview_named_colors"));
+	viewer->liststore_nc = gtk_list_store_new (GCM_NAMED_COLORS_COLUMN_LAST,
+					       G_TYPE_STRING,
+					       G_TYPE_STRING,
+					       CD_TYPE_COLOR_XYZ);
+	gtk_tree_view_set_model (GTK_TREE_VIEW (widget),
+				 GTK_TREE_MODEL (viewer->liststore_nc));
+	gcm_viewer_add_named_colors_columns (viewer, GTK_TREE_VIEW (widget));
+	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
+	gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
+	g_signal_connect (selection, "changed",
+			  G_CALLBACK (gcm_viewer_named_color_treeview_clicked), viewer);
+
 	/* use cie widget */
 	viewer->cie_widget = gcm_cie_widget_new ();
 	widget = GTK_WIDGET (gtk_builder_get_object (viewer->builder, "vbox_cie_widget"));



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