[gimp] libgimpbase: add utility functions to convert between pixels and units



commit 52041715a965b23fd64cb52770c71e9729da3435
Author: Michael Natterer <mitch gimp org>
Date:   Sun Feb 21 16:45:01 2010 +0100

    libgimpbase: add utility functions to convert between pixels and units

 libgimpbase/gimpbase.def |    2 ++
 libgimpbase/gimpunit.c   |   41 +++++++++++++++++++++++++++++++++++++++++
 libgimpbase/gimpunit.h   |    7 +++++++
 3 files changed, 50 insertions(+), 0 deletions(-)
---
diff --git a/libgimpbase/gimpbase.def b/libgimpbase/gimpbase.def
index 2a1990a..76bbccc 100644
--- a/libgimpbase/gimpbase.def
+++ b/libgimpbase/gimpbase.def
@@ -80,6 +80,7 @@ EXPORTS
 	gimp_pdb_proc_type_get_type
 	gimp_pdb_status_type_get_type
 	gimp_personal_rc_file
+	gimp_pixels_to_units
 	gimp_pixpipe_params_build
 	gimp_pixpipe_params_init
 	gimp_pixpipe_params_parse
@@ -117,6 +118,7 @@ EXPORTS
 	gimp_unit_get_type
 	gimp_unit_new
 	gimp_unit_set_deletion_flag
+	gimp_units_to_pixels
 	gimp_user_directory
 	gimp_user_directory_get_type
 	gimp_utf8_strtrim
diff --git a/libgimpbase/gimpunit.c b/libgimpbase/gimpunit.c
index e5714ca..6bc174a 100644
--- a/libgimpbase/gimpunit.c
+++ b/libgimpbase/gimpunit.c
@@ -459,3 +459,44 @@ gimp_param_spec_unit (const gchar *name,
   return G_PARAM_SPEC (pspec);
 }
 
+/**
+ * gimp_pixels_to_units:
+ * @pixels:     value in pixels
+ * @unit:       unit to convert to
+ * @resolution: respoution in DPI
+ *
+ * Returns: %pixels converted to units.
+ *
+ * Since: GIMP 2.8
+ **/
+gdouble
+gimp_pixels_to_units (gdouble  pixels,
+                      GimpUnit unit,
+                      gdouble  resolution)
+{
+  if (unit == GIMP_UNIT_PIXEL)
+    return pixels;
+
+  return pixels * gimp_unit_get_factor (unit) / resolution;
+}
+
+/**
+ * gimp_inits_to_pixels:
+ * @value:      value in units
+ * @unit:       unit of %value
+ * @resolution: respoution in DPI
+ *
+ * Returns: %value converted to pixels.
+ *
+ * Since: GIMP 2.8
+ **/
+gdouble
+gimp_units_to_pixels (gdouble  value,
+                      GimpUnit unit,
+                      gdouble  resolution)
+{
+  if (unit == GIMP_UNIT_PIXEL)
+    return value;
+
+  return value * resolution / gimp_unit_get_factor (unit);
+}
diff --git a/libgimpbase/gimpunit.h b/libgimpbase/gimpunit.h
index c97d096..00c9749 100644
--- a/libgimpbase/gimpunit.h
+++ b/libgimpbase/gimpunit.h
@@ -81,6 +81,13 @@ const gchar * gimp_unit_get_abbreviation    (GimpUnit  unit);
 const gchar * gimp_unit_get_singular        (GimpUnit  unit);
 const gchar * gimp_unit_get_plural          (GimpUnit  unit);
 
+gdouble       gimp_pixels_to_units          (gdouble   pixels,
+                                             GimpUnit  unit,
+                                             gdouble   resolution);
+gdouble       gimp_units_to_pixels          (gdouble   units,
+                                             GimpUnit  unit,
+                                             gdouble   resolution);
+
 
 G_END_DECLS
 



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