summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2016-10-05 19:50:01 +0100
committerRichard Hughes <richard@hughsie.com>2016-10-05 19:50:01 +0100
commit3d8891426ec7a2a8d35f22b77e971487ee97518a (patch)
treed45641e26995db124d6e628bce50f068f87f0926
parent862d0e95e45d37e0565147c79b239dbb0d5f3756 (diff)
downloadcolord-3d8891426ec7a2a8d35f22b77e971487ee97518a.tar.gz
colord: Add cd_spectrum_resample_to_size()
-rw-r--r--lib/colord/cd-spectrum.c31
-rw-r--r--lib/colord/cd-spectrum.h2
-rw-r--r--lib/colord/cd-test-private.c32
3 files changed, 65 insertions, 0 deletions
diff --git a/lib/colord/cd-spectrum.c b/lib/colord/cd-spectrum.c
index 59a72a9..9ee2a22 100644
--- a/lib/colord/cd-spectrum.c
+++ b/lib/colord/cd-spectrum.c
@@ -951,3 +951,34 @@ cd_spectrum_resample (CdSpectrum *spectrum,
cd_spectrum_set_end (sp, end);
return sp;
}
+
+/**
+ * cd_spectrum_resample_to_size:
+ * @spectrum: a #CdSpectrum instance
+ * @size: the output spectrum size
+ *
+ * Resample a new spectrum with the desired number of points.
+ *
+ * Return value: a #CdSpectrum instance
+ *
+ * Since: 1.3.4
+ **/
+CdSpectrum *
+cd_spectrum_resample_to_size (CdSpectrum *spectrum, guint size)
+{
+ gdouble inc;
+ guint i;
+ CdSpectrum *sp;
+
+ sp = cd_spectrum_new ();
+ cd_spectrum_set_start (sp, spectrum->start);
+ cd_spectrum_set_end (sp, spectrum->end);
+
+ inc = (spectrum->end - spectrum->start) / (gdouble) (size - 1);
+ for (i = 0; i < size; i++) {
+ gdouble nm = spectrum->start + ((gdouble) i * inc);
+ gdouble tmp = cd_spectrum_get_value_for_nm (spectrum, nm);
+ cd_spectrum_add_value (sp, tmp);
+ }
+ return sp;
+}
diff --git a/lib/colord/cd-spectrum.h b/lib/colord/cd-spectrum.h
index 40a9bb5..83af8c1 100644
--- a/lib/colord/cd-spectrum.h
+++ b/lib/colord/cd-spectrum.h
@@ -108,6 +108,8 @@ CdSpectrum *cd_spectrum_resample (CdSpectrum *spectrum,
gdouble start,
gdouble end,
gdouble resolution);
+CdSpectrum *cd_spectrum_resample_to_size (CdSpectrum *spectrum,
+ guint size);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(CdSpectrum, cd_spectrum_free)
diff --git a/lib/colord/cd-test-private.c b/lib/colord/cd-test-private.c
index b9f10eb..c68991b 100644
--- a/lib/colord/cd-test-private.c
+++ b/lib/colord/cd-test-private.c
@@ -319,7 +319,9 @@ colord_spect_cx_func (void)
{
gdouble tmp;
g_autoptr(CdSpectrum) sp = NULL;
+ g_autoptr(CdSpectrum) sp_linear = NULL;
g_autoptr(CdSpectrum) sp_resampled = NULL;
+ g_autoptr(CdSpectrum) sp_size = NULL;
/* create test spectrum */
sp = cd_spectrum_new ();
@@ -367,6 +369,36 @@ colord_spect_cx_func (void)
g_assert_cmpfloat (fabs (tmp - 1.998f), <, 0.001);
tmp = cd_spectrum_get_value_for_nm (sp_resampled, 300.f);
g_assert_cmpfloat (fabs (tmp - 2.498f), <, 0.001);
+
+ /* resample to a set number of points */
+ sp_linear = cd_spectrum_new ();
+ cd_spectrum_set_start (sp_linear, 100);
+ cd_spectrum_set_end (sp_linear, 200);
+ cd_spectrum_add_value (sp_linear, 1.f);
+ cd_spectrum_add_value (sp_linear, 2.f);
+ cd_spectrum_add_value (sp_linear, 3.f);
+
+ /* check values */
+ sp_size = cd_spectrum_resample_to_size (sp_linear, 4);
+ tmp = cd_spectrum_get_value (sp_size, 0);
+ g_assert_cmpfloat (fabs (tmp - 1.f), <, 0.001);
+ tmp = cd_spectrum_get_value (sp_size, 1);
+ g_assert_cmpfloat (fabs (tmp - 1.66f), <, 0.01);
+ tmp = cd_spectrum_get_value (sp_size, 2);
+ g_assert_cmpfloat (fabs (tmp - 2.33f), <, 0.01);
+ tmp = cd_spectrum_get_value (sp_size, 3);
+ g_assert_cmpfloat (fabs (tmp - 3.f), <, 0.001);
+
+ /* check spacing */
+ tmp = cd_spectrum_get_wavelength (sp_size, 0);
+ g_assert_cmpfloat (fabs (tmp - 100), <, 0.001);
+ tmp = cd_spectrum_get_wavelength (sp_size, 1);
+ g_assert_cmpfloat (fabs (tmp - 133.33f), <, 0.01);
+ tmp = cd_spectrum_get_wavelength (sp_size, 2);
+ g_assert_cmpfloat (fabs (tmp - 166.66), <, 0.01);
+ tmp = cd_spectrum_get_wavelength (sp_size, 3);
+ g_assert_cmpfloat (fabs (tmp - 200), <, 0.001);
+
}
static void