[gnome-color-manager] trivial: Add a function gcm_calibrate_set_from_exif()



commit b70216aec60666c1492e3caebf4a0cd0ee6e5c0f
Author: Richard Hughes <richard hughsie com>
Date:   Fri Feb 12 12:48:26 2010 +0000

    trivial: Add a function gcm_calibrate_set_from_exif()

 data/tests/test.tif        |  Bin 0 -> 3484 bytes
 src/gcm-calibrate-argyll.c |   34 ++++-----
 src/gcm-calibrate.c        |  174 +++++++++++++++++++++++++++++++++++++------
 src/gcm-calibrate.h        |    6 ++
 src/gcm-self-test.c        |    2 +
 5 files changed, 173 insertions(+), 43 deletions(-)
---
diff --git a/data/tests/test.tif b/data/tests/test.tif
new file mode 100644
index 0000000..8a7f51c
Binary files /dev/null and b/data/tests/test.tif differ
diff --git a/src/gcm-calibrate-argyll.c b/src/gcm-calibrate-argyll.c
index 009d0d3..6fa1e5f 100644
--- a/src/gcm-calibrate-argyll.c
+++ b/src/gcm-calibrate-argyll.c
@@ -631,10 +631,10 @@ gcm_calibrate_argyll_display_generate_profile (GcmCalibrateArgyll *calibrate_arg
 	gchar *description_new = NULL;
 	gchar *command = NULL;
 	gchar *basename = NULL;
-	gchar *description = NULL;
-	gchar *manufacturer = NULL;
+	const gchar *description = NULL;
+	const gchar *manufacturer = NULL;
+	const gchar *model = NULL;
 	gchar *device = NULL;
-	gchar *model = NULL;
 	GPtrArray *array = NULL;
 	const gchar *title;
 	const gchar *message;
@@ -642,12 +642,14 @@ gcm_calibrate_argyll_display_generate_profile (GcmCalibrateArgyll *calibrate_arg
 	/* get shared data */
 	g_object_get (calibrate_argyll,
 		      "basename", &basename,
-		      "description", &description,
-		      "manufacturer", &manufacturer,
-		      "model", &model,
 		      "device", &device,
 		      NULL);
 
+	/* get, returning fallbacks if nothing was set */
+	model = gcm_calibrate_get_model_fallback (GCM_CALIBRATE (calibrate_argyll));
+	manufacturer = gcm_calibrate_get_manufacturer_fallback (GCM_CALIBRATE (calibrate_argyll));
+	description = gcm_calibrate_get_description_fallback (GCM_CALIBRATE (calibrate_argyll));
+
 	/* get correct name of the command */
 	command = gcm_calibrate_argyll_get_tool_filename ("colprof", error);
 	if (command == NULL) {
@@ -714,9 +716,6 @@ out:
 		g_date_free (date);
 	g_free (basename);
 	g_free (command);
-	g_free (manufacturer);
-	g_free (model);
-	g_free (description);
 	g_free (description_new);
 	g_free (device);
 	g_free (copyright);
@@ -916,13 +915,13 @@ gcm_calibrate_argyll_device_generate_profile (GcmCalibrateArgyll *calibrate_argy
 	gchar **argv = NULL;
 	GDate *date = NULL;
 	gchar *description_tmp = NULL;
-	gchar *description = NULL;
+	const gchar *description;
 	gchar *copyright = NULL;
 	GPtrArray *array = NULL;
 	gchar *command = NULL;
 	gchar *basename = NULL;
-	gchar *manufacturer = NULL;
-	gchar *model = NULL;
+	const gchar *manufacturer;
+	const gchar *model;
 	gchar *device = NULL;
 	const gchar *title;
 	const gchar *message;
@@ -930,12 +929,14 @@ gcm_calibrate_argyll_device_generate_profile (GcmCalibrateArgyll *calibrate_argy
 	/* get shared data */
 	g_object_get (calibrate_argyll,
 		      "basename", &basename,
-		      "description", &description,
-		      "manufacturer", &manufacturer,
-		      "model", &model,
 		      "device", &device,
 		      NULL);
 
+	/* get, returning fallbacks if nothing was set */
+	model = gcm_calibrate_get_model_fallback (GCM_CALIBRATE (calibrate_argyll));
+	manufacturer = gcm_calibrate_get_manufacturer_fallback (GCM_CALIBRATE (calibrate_argyll));
+	description = gcm_calibrate_get_description_fallback (GCM_CALIBRATE (calibrate_argyll));
+
 	/* get correct name of the command */
 	command = gcm_calibrate_argyll_get_tool_filename ("colprof", error);
 	if (command == NULL) {
@@ -1008,9 +1009,6 @@ out:
 	g_free (description_tmp);
 	g_free (copyright);
 	g_free (basename);
-	g_free (manufacturer);
-	g_free (model);
-	g_free (description);
 	g_free (command);
 	g_free (device);
 	g_strfreev (argv);
diff --git a/src/gcm-calibrate.c b/src/gcm-calibrate.c
index 41a5e74..38c8ff1 100644
--- a/src/gcm-calibrate.c
+++ b/src/gcm-calibrate.c
@@ -30,6 +30,8 @@
 
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
+#include <tiff.h>
+#include <tiffio.h>
 
 #include "gcm-calibrate.h"
 #include "gcm-utils.h"
@@ -69,6 +71,7 @@ enum {
 	PROP_BASENAME,
 	PROP_MODEL,
 	PROP_DESCRIPTION,
+	PROP_SERIAL,
 	PROP_DEVICE,
 	PROP_MANUFACTURER,
 	PROP_IS_LCD,
@@ -82,6 +85,47 @@ enum {
 
 G_DEFINE_TYPE (GcmCalibrate, gcm_calibrate, G_TYPE_OBJECT)
 
+/**
+ * gcm_calibrate_get_model_fallback:
+ **/
+const gchar *
+gcm_calibrate_get_model_fallback (GcmCalibrate *calibrate)
+{
+	GcmCalibratePrivate *priv = calibrate->priv;
+	if (priv->model != NULL)
+		return priv->model;
+
+	/* TRANSLATORS: this is saved in the profile */
+	return _("Unknown model");
+}
+
+/**
+ * gcm_calibrate_get_description_fallback:
+ **/
+const gchar *
+gcm_calibrate_get_description_fallback (GcmCalibrate *calibrate)
+{
+	GcmCalibratePrivate *priv = calibrate->priv;
+	if (priv->description != NULL)
+		return priv->description;
+
+	/* TRANSLATORS: this is saved in the profile */
+	return _("Unknown description");
+}
+
+/**
+ * gcm_calibrate_get_manufacturer_fallback:
+ **/
+const gchar *
+gcm_calibrate_get_manufacturer_fallback (GcmCalibrate *calibrate)
+{
+	GcmCalibratePrivate *priv = calibrate->priv;
+	if (priv->manufacturer != NULL)
+		return priv->manufacturer;
+
+	/* TRANSLATORS: this is saved in the profile */
+	return _("Unknown manufacturer");
+}
 
 /**
  * gcm_calibrate_get_time:
@@ -174,31 +218,8 @@ gcm_calibrate_set_from_device (GcmCalibrate *calibrate, GcmDevice *device, GErro
 		      "title", &description,
 		      "manufacturer", &manufacturer,
 		      NULL);
-	if (native_device == NULL) {
-		g_set_error (error, 1, 0, "failed to get output");
-		ret = FALSE;
-		goto out;
-	}
-
-	/* get model */
-	if (model == NULL) {
-		/* TRANSLATORS: this is saved in the profile */
-		model = g_strdup (_("Unknown model"));
-	}
-
-	/* get description */
-	if (description == NULL) {
-		/* TRANSLATORS: this is saved in the profile */
-		description = g_strdup (_("Unknown device"));
-	}
 
-	/* get manufacturer */
-	if (manufacturer == NULL) {
-		/* TRANSLATORS: this is saved in the profile */
-		manufacturer = g_strdup (_("Unknown manufacturer"));
-	}
-
-	/* set the proper output name */
+	/* set the proper values */
 	g_object_set (calibrate,
 		      "model", model,
 		      "description", description,
@@ -211,13 +232,17 @@ gcm_calibrate_set_from_device (GcmCalibrate *calibrate, GcmDevice *device, GErro
 
 	/* display specific properties */
 	if (type == GCM_DEVICE_TYPE_ENUM_DISPLAY) {
+		if (native_device == NULL) {
+			g_set_error (error, 1, 0, "failed to get output");
+			ret = FALSE;
+			goto out;
+		}
 		g_object_set (calibrate,
 			      "output-name", native_device,
 			      NULL);
 	}
 
 out:
-	g_free (device);
 	g_free (native_device);
 	g_free (manufacturer);
 	g_free (model);
@@ -227,6 +252,48 @@ out:
 }
 
 /**
+ * gcm_calibrate_set_from_exif:
+ **/
+gboolean
+gcm_calibrate_set_from_exif (GcmCalibrate *calibrate, const gchar *filename, GError **error)
+{
+	const gchar *manufacturer = NULL;
+	const gchar *model = NULL;
+	gchar *description = NULL;
+	const gchar *serial = NULL;
+	TIFF *tiff;
+	gboolean ret = TRUE;
+
+	/* open file */
+	tiff = TIFFOpen (filename, "r");
+	TIFFGetField (tiff,TIFFTAG_MAKE, &manufacturer);
+	TIFFGetField (tiff,TIFFTAG_MODEL, &model);
+	TIFFGetField (tiff,TIFFTAG_CAMERASERIALNUMBER, &serial);
+
+	/* we failed to get data */
+	if (manufacturer == NULL && model == NULL) {
+		g_set_error (error, 1, 0, "failed to get EXIF data from TIFF");
+		ret = FALSE;
+		goto out;
+	}
+
+	/* do the best we can */
+	description = g_strdup_printf ("%s - %s", manufacturer, model);
+
+	/* set the proper values */
+	g_object_set (calibrate,
+		      "model", model,
+		      "description", description,
+		      "manufacturer", manufacturer,
+		      "serial", serial,
+		      NULL);
+out:
+	g_free (description);
+	TIFFClose (tiff);
+	return ret;
+}
+
+/**
  * gcm_calibrate_display:
  **/
 gboolean
@@ -455,6 +522,9 @@ gcm_calibrate_get_property (GObject *object, guint prop_id, GValue *value, GPara
 	case PROP_DESCRIPTION:
 		g_value_set_string (value, priv->description);
 		break;
+	case PROP_SERIAL:
+		g_value_set_string (value, priv->serial);
+		break;
 	case PROP_DEVICE:
 		g_value_set_string (value, priv->device);
 		break;
@@ -533,6 +603,10 @@ gcm_calibrate_set_property (GObject *object, guint prop_id, const GValue *value,
 		g_free (priv->description);
 		priv->description = g_strdup (g_value_get_string (value));
 		break;
+	case PROP_SERIAL:
+		g_free (priv->serial);
+		priv->serial = g_strdup (g_value_get_string (value));
+		break;
 	case PROP_DEVICE:
 		g_free (priv->device);
 		priv->device = g_strdup (g_value_get_string (value));
@@ -632,6 +706,14 @@ gcm_calibrate_class_init (GcmCalibrateClass *klass)
 	g_object_class_install_property (object_class, PROP_DESCRIPTION, pspec);
 
 	/**
+	 * GcmCalibrate:serial:
+	 */
+	pspec = g_param_spec_string ("serial", NULL, NULL,
+				     NULL,
+				     G_PARAM_READWRITE);
+	g_object_class_install_property (object_class, PROP_SERIAL, pspec);
+
+	/**
 	 * GcmCalibrate:device:
 	 */
 	pspec = g_param_spec_string ("device", NULL, NULL,
@@ -707,3 +789,45 @@ gcm_calibrate_new (void)
 	return GCM_CALIBRATE (calibrate);
 }
 
+/***************************************************************************
+ ***                          MAKE CHECK TESTS                           ***
+ ***************************************************************************/
+#ifdef EGG_TEST
+#include "egg-test.h"
+
+void
+gcm_calibrate_test (EggTest *test)
+{
+	GcmCalibrate *calibrate;
+	gboolean ret;
+	GError *error = NULL;
+	gchar *filename;
+
+	if (!egg_test_start (test, "GcmCalibrate"))
+		return;
+
+	/************************************************************/
+	egg_test_title (test, "get a calibrate object");
+	calibrate = gcm_calibrate_new ();
+	egg_test_assert (test, calibrate != NULL);
+
+	/************************************************************/
+	egg_test_title (test, "calibrate display manually");
+	filename = egg_test_get_data_file ("test.tif");
+	ret = gcm_calibrate_set_from_exif (GCM_CALIBRATE(calibrate), filename, &error);
+	if (!ret)
+		egg_test_failed (test, "error: %s", error->message);
+	else if (g_strcmp0 (gcm_calibrate_get_model_fallback (calibrate), "NIKON D60") != 0)
+		egg_test_failed (test, "got model: %s", gcm_calibrate_get_model_fallback (calibrate));
+	else if (g_strcmp0 (gcm_calibrate_get_manufacturer_fallback (calibrate), "NIKON CORPORATION") != 0)
+		egg_test_failed (test, "got manufacturer: %s", gcm_calibrate_get_manufacturer_fallback (calibrate));
+	else
+		egg_test_success (test, NULL);
+
+	g_object_unref (calibrate);
+	g_free (filename);
+
+	egg_test_end (test);
+}
+#endif
+
diff --git a/src/gcm-calibrate.h b/src/gcm-calibrate.h
index 4e69229..d83b95e 100644
--- a/src/gcm-calibrate.h
+++ b/src/gcm-calibrate.h
@@ -74,6 +74,12 @@ gboolean	 gcm_calibrate_device			(GcmCalibrate	*calibrate,
 gboolean	 gcm_calibrate_set_from_device		(GcmCalibrate	*calibrate,
 							 GcmDevice	*device,
 							 GError		**error);
+gboolean	 gcm_calibrate_set_from_exif		(GcmCalibrate	*calibrate,
+							 const gchar	*filename,
+							 GError		**error);
+const gchar	*gcm_calibrate_get_model_fallback	(GcmCalibrate	*calibrate);
+const gchar	*gcm_calibrate_get_description_fallback	(GcmCalibrate	*calibrate);
+const gchar	*gcm_calibrate_get_manufacturer_fallback (GcmCalibrate	*calibrate);
 
 G_END_DECLS
 
diff --git a/src/gcm-self-test.c b/src/gcm-self-test.c
index c50cfb1..a023b32 100644
--- a/src/gcm-self-test.c
+++ b/src/gcm-self-test.c
@@ -36,6 +36,7 @@ void gcm_dmi_test (EggTest *test);
 void gcm_xyz_test (EggTest *test);
 void gcm_cie_widget_test (EggTest *test);
 void gcm_trc_widget_test (EggTest *test);
+void gcm_calibrate_test (EggTest *test);
 void gcm_calibrate_manual_test (EggTest *test);
 
 int
@@ -50,6 +51,7 @@ main (int argc, char **argv)
 	egg_debug_init (&argc, &argv);
 
 	/* components */
+	gcm_calibrate_test (test);
 	gcm_edid_test (test);
 	gcm_tables_test (test);
 	gcm_utils_test (test);



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