diff options
author | Richard Hughes <richard@hughsie.com> | 2014-01-07 20:27:41 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2014-01-07 22:23:49 +0000 |
commit | a341b4ee74af2e4ed023a094a40f7f18a7831cd8 (patch) | |
tree | 45f59937daca2aac392d1c8a8c722bc720038041 /contrib | |
parent | 067a623ac9c0da20cd634b9afb69d8934dc60484 (diff) | |
download | colord-a341b4ee74af2e4ed023a094a40f7f18a7831cd8.tar.gz |
Add a cd-csv2cmf binary that allows us to generate some data files
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/Makefile.am | 3 | ||||
-rw-r--r-- | contrib/csv2cmf/Makefile.am | 25 | ||||
-rw-r--r-- | contrib/csv2cmf/cd-main.c | 165 |
3 files changed, 192 insertions, 1 deletions
diff --git a/contrib/Makefile.am b/contrib/Makefile.am index 954524c..e484d5a 100644 --- a/contrib/Makefile.am +++ b/contrib/Makefile.am @@ -1,4 +1,5 @@ -SUBDIRS = \ +SUBDIRS = \ + csv2cmf \ session-helper if HAVE_SANE diff --git a/contrib/csv2cmf/Makefile.am b/contrib/csv2cmf/Makefile.am new file mode 100644 index 0000000..34bd3ed --- /dev/null +++ b/contrib/csv2cmf/Makefile.am @@ -0,0 +1,25 @@ +AM_CPPFLAGS = \ + $(GLIB_CFLAGS) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/lib \ + -I$(top_builddir)/lib \ + -DG_LOG_DOMAIN=\"CdCvs2cmf\" \ + -DLOCALEDIR=\""$(localedir)"\" + +COLORD_LIBS = \ + $(top_builddir)/lib/colord/libcolordprivate.la + +noinst_PROGRAMS = \ + cd-csv2cmf + +cd_csv2cmf_SOURCES = \ + cd-main.c + +cd_csv2cmf_LDADD = \ + $(COLORD_LIBS) \ + $(GLIB_LIBS) + +cd_csv2cmf_CFLAGS = \ + $(WARNINGFLAGS_C) + +-include $(top_srcdir)/git.mk diff --git a/contrib/csv2cmf/cd-main.c b/contrib/csv2cmf/cd-main.c new file mode 100644 index 0000000..1fe5eda --- /dev/null +++ b/contrib/csv2cmf/cd-main.c @@ -0,0 +1,165 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2014 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 <stdlib.h> +#include <gio/gio.h> +#include <colord/colord.h> + +typedef struct { + guint nm; + CdColorXYZ xyz; +} CdSpectrumData; + +/** + * cd_csv2cmf_data_free: + **/ +static void +cd_csv2cmf_data_free (CdSpectrumData *data) +{ + g_slice_free (CdSpectrumData, data); +} + +/** + * main: + **/ +int +main (int argc, char *argv[]) +{ + CdIt8 *cmf = NULL; + CdSpectrum *spectrum[3]; + CdSpectrumData *tmp; + GError *error = NULL; + GFile *file = NULL; + GPtrArray *array = NULL; + GPtrArray *spectra = NULL; + gboolean ret; + gchar **lines = NULL; + gchar **split; + gchar *data = NULL; + gchar *originator = NULL; + gchar *title = NULL; + guint i; + guint retval = 1; + + /* check args */ + if (argc != 3) { + retval = 0; + g_print ("Incorrect syntax: expected cd-cvs2cmf a.csv b.cmf\n"); + goto out; + } + + /* get data */ + ret = g_file_get_contents (argv[1], &data, NULL, &error); + if (!ret) { + retval = 0; + g_print ("Failed to get contents: %s\n", error->message); + g_error_free (error); + goto out; + } + + /* parse lines */ + array = g_ptr_array_new_with_free_func ((GDestroyNotify) cd_csv2cmf_data_free); + lines = g_strsplit (data, "\n", -1); + for (i = 0; lines[i] != NULL; i++) { + split = g_strsplit (lines[i], ",", -1); + if (g_strv_length (split) == 4) { + tmp = g_slice_new0 (CdSpectrumData); + tmp->nm = atoi (split[0]); + cd_color_xyz_set (&tmp->xyz, + atof (split[1]), + atof (split[2]), + atof (split[3])); + g_ptr_array_add (array, tmp); + } else { + g_print ("Ignoring data line: %s", lines[i]); + } + g_strfreev (split); + } + + /* did we get enough data */ + if (array->len < 3) { + retval = 0; + g_print ("Not enough data in the CSV file\n"); + goto out; + } + + for (i = 0; i < 3; i++) + spectrum[i] = cd_spectrum_sized_new (array->len); + + cd_spectrum_set_id (spectrum[0], "X"); + cd_spectrum_set_id (spectrum[1], "Y"); + cd_spectrum_set_id (spectrum[2], "Z"); + + /* get the first point */ + tmp = g_ptr_array_index (array, 0); + for (i = 0; i < 3; i++) + cd_spectrum_set_start (spectrum[i], tmp->nm); + + /* get the last point */ + tmp = g_ptr_array_index (array, array->len - 1); + for (i = 0; i < 3; i++) + cd_spectrum_set_end (spectrum[i], tmp->nm); + + /* add data to the spectra */ + for (i = 0; i < array->len; i++) { + tmp = g_ptr_array_index (array, i); + cd_spectrum_add_value (spectrum[0], tmp->xyz.X); + cd_spectrum_add_value (spectrum[1], tmp->xyz.Y); + cd_spectrum_add_value (spectrum[2], tmp->xyz.Z); + } + + /* add spectra to the CMF file */ + spectra = g_ptr_array_new_with_free_func ((GDestroyNotify) cd_spectrum_free); + for (i = 0; i < 3; i++) + g_ptr_array_add (spectra, spectrum[i]); + cmf = cd_it8_new_with_kind (CD_IT8_KIND_CMF); + originator = g_path_get_basename (argv[0]); + cd_it8_set_originator (cmf, originator); + title = g_path_get_basename (argv[1]); + cd_it8_set_title (cmf, title); + cd_it8_set_spectral_data (cmf, spectra); + + /* save */ + file = g_file_new_for_path (argv[2]); + ret = cd_it8_save_to_file (cmf, file, &error); + if (!ret) { + retval = 0; + g_print ("Failed to save file: %s\n", error->message); + g_error_free (error); + goto out; + } +out: + if (array != NULL) + g_ptr_array_unref (array); + if (spectra != NULL) + g_ptr_array_unref (spectra); + if (cmf != NULL) + g_object_unref (cmf); + if (file != NULL) + g_object_unref (file); + g_free (data); + g_free (title); + g_free (originator); + g_strfreev (lines); + return retval; +} |