[gimp] app: add gimp_suggest_trc_for_component_type()



commit 792264ba6a7b3a026496bb0acdcfcb8edd709d61
Author: Michael Natterer <mitch gimp org>
Date:   Fri May 31 17:26:50 2019 +0200

    app: add gimp_suggest_trc_for_component_type()
    
    and use it in GimpTemplateEditor and convert-precision-dialog, instead
    of duplicating the logic.

 app/core/gimp-utils.c                  | 31 +++++++++++++-
 app/core/gimp-utils.h                  | 78 ++++++++++++++++++----------------
 app/dialogs/convert-precision-dialog.c | 27 ++----------
 app/widgets/gimptemplateeditor.c       | 37 ++++------------
 4 files changed, 83 insertions(+), 90 deletions(-)
---
diff --git a/app/core/gimp-utils.c b/app/core/gimp-utils.c
index 8f1a29e42d..3cc9d34f4a 100644
--- a/app/core/gimp-utils.c
+++ b/app/core/gimp-utils.c
@@ -772,7 +772,6 @@ gimp_file_with_new_extension (GFile *file,
   return ret;
 }
 
-
 /**
  * gimp_file_delete_recursive:
  * @file: #GFile to delete from file system.
@@ -947,6 +946,36 @@ gimp_g_list_compare (GList *list1,
   return 0;
 }
 
+GimpTRCType
+gimp_suggest_trc_for_component_type (GimpComponentType component_type,
+                                     GimpTRCType       old_trc)
+{
+  GimpTRCType new_trc = old_trc;
+
+  switch (component_type)
+    {
+    case GIMP_COMPONENT_TYPE_U8:
+      /* default to non-linear when converting 8 bit */
+      new_trc = GIMP_TRC_NON_LINEAR;
+      break;
+
+    case GIMP_COMPONENT_TYPE_U16:
+    case GIMP_COMPONENT_TYPE_U32:
+    default:
+      /* leave TRC alone by default when converting to 16/32 bit int */
+      break;
+
+    case GIMP_COMPONENT_TYPE_HALF:
+    case GIMP_COMPONENT_TYPE_FLOAT:
+    case GIMP_COMPONENT_TYPE_DOUBLE:
+      /* default to linear when converting to floating point */
+      new_trc = GIMP_TRC_LINEAR;
+      break;
+    }
+
+  return new_trc;
+}
+
 
 /*  debug stuff  */
 
diff --git a/app/core/gimp-utils.h b/app/core/gimp-utils.h
index 172f2ad2e0..be3965f459 100644
--- a/app/core/gimp-utils.h
+++ b/app/core/gimp-utils.h
@@ -69,43 +69,47 @@ gboolean     gimp_get_fill_params                  (GimpContext      *context,
 #define GIMP_CONSTRAIN_LINE_45_DEGREES 4
 #define GIMP_CONSTRAIN_LINE_15_DEGREES 12
 
-void         gimp_constrain_line                   (gdouble          start_x,
-                                                    gdouble          start_y,
-                                                    gdouble         *end_x,
-                                                    gdouble         *end_y,
-                                                    gint             n_snap_lines,
-                                                    gdouble          offset_angle,
-                                                    gdouble          xres,
-                                                    gdouble          yres);
-
-gint         gimp_file_compare                     (GFile           *file1,
-                                                    GFile           *file2);
-gboolean     gimp_file_is_executable               (GFile           *file);
-gchar      * gimp_file_get_extension               (GFile           *file);
-GFile      * gimp_file_with_new_extension          (GFile           *file,
-                                                    GFile           *ext_file);
-gboolean     gimp_file_delete_recursive            (GFile           *file,
-                                                    GError         **error);
-
-gchar      * gimp_data_input_stream_read_line_always (GDataInputStream  *stream,
-                                                      gsize             *length,
-                                                      GCancellable      *cancellable,
-                                                      GError           **error);
-
-gboolean     gimp_ascii_strtoi                     (const gchar     *nptr,
-                                                    gchar          **endptr,
-                                                    gint             base,
-                                                    gint            *result);
-gboolean     gimp_ascii_strtod                     (const gchar     *nptr,
-                                                    gchar          **endptr,
-                                                    gdouble         *result);
-
-gint         gimp_g_list_compare                   (GList           *list1,
-                                                    GList           *list2);
-
-GimpImage  * gimp_create_image_from_buffer         (Gimp            *gimp,
-                                                    GeglBuffer      *buffer,
-                                                    const gchar     *image_name);
+void         gimp_constrain_line                   (gdouble            start_x,
+                                                    gdouble            start_y,
+                                                    gdouble           *end_x,
+                                                    gdouble           *end_y,
+                                                    gint               n_snap_lines,
+                                                    gdouble            offset_angle,
+                                                    gdouble            xres,
+                                                    gdouble            yres);
+
+gint         gimp_file_compare                     (GFile             *file1,
+                                                    GFile             *file2);
+gboolean     gimp_file_is_executable               (GFile             *file);
+gchar      * gimp_file_get_extension               (GFile             *file);
+GFile      * gimp_file_with_new_extension          (GFile             *file,
+                                                    GFile             *ext_file);
+gboolean     gimp_file_delete_recursive            (GFile             *file,
+                                                    GError           **error);
+
+gchar      * gimp_data_input_stream_read_line_always
+                                                   (GDataInputStream  *stream,
+                                                    gsize             *length,
+                                                    GCancellable      *cancellable,
+                                                    GError           **error);
+
+gboolean     gimp_ascii_strtoi                     (const gchar       *nptr,
+                                                    gchar            **endptr,
+                                                    gint               base,
+                                                    gint              *result);
+gboolean     gimp_ascii_strtod                     (const gchar       *nptr,
+                                                    gchar            **endptr,
+                                                    gdouble           *result);
+
+gint         gimp_g_list_compare                   (GList             *list1,
+                                                    GList             *list2);
+
+GimpTRCType  gimp_suggest_trc_for_component_type   (GimpComponentType  component_type,
+                                                    GimpTRCType        old_trc);
+
+GimpImage  * gimp_create_image_from_buffer         (Gimp              *gimp,
+                                                    GeglBuffer        *buffer,
+                                                    const gchar       *image_name);
 
 
 #endif /* __APP_GIMP_UTILS_H__ */
diff --git a/app/dialogs/convert-precision-dialog.c b/app/dialogs/convert-precision-dialog.c
index 2d84b5ed0c..683a94d86e 100644
--- a/app/dialogs/convert-precision-dialog.c
+++ b/app/dialogs/convert-precision-dialog.c
@@ -29,6 +29,7 @@
 
 #include "core/gimpcontext.h"
 #include "core/gimpimage.h"
+#include "core/gimp-utils.h"
 
 #include "widgets/gimphelp-ids.h"
 #include "widgets/gimpviewabledialog.h"
@@ -114,30 +115,8 @@ convert_precision_dialog_new (GimpImage                    *image,
   dither = (new_bits <  old_bits &&
             new_bits <= CONVERT_PRECISION_DIALOG_MAX_DITHER_BITS);
 
-  /* when changing this logic, also change the same switch()
-   * in gimptemplateeditor.h
-   */
-  switch (component_type)
-    {
-    case GIMP_COMPONENT_TYPE_U8:
-      /* default to gamma when converting 8 bit */
-      trc = GIMP_TRC_NON_LINEAR;
-      break;
-
-    case GIMP_COMPONENT_TYPE_U16:
-    case GIMP_COMPONENT_TYPE_U32:
-    default:
-      /* leave gamma alone by default when converting to 16/32 bit int */
-      trc = gimp_babl_format_get_trc (old_format);
-      break;
-
-    case GIMP_COMPONENT_TYPE_HALF:
-    case GIMP_COMPONENT_TYPE_FLOAT:
-    case GIMP_COMPONENT_TYPE_DOUBLE:
-      /* default to linear when converting to floating point */
-      trc = GIMP_TRC_LINEAR;
-      break;
-    }
+  trc = gimp_babl_format_get_trc (old_format);
+  trc = gimp_suggest_trc_for_component_type (component_type, trc);
 
   private = g_slice_new0 (ConvertDialog);
 
diff --git a/app/widgets/gimptemplateeditor.c b/app/widgets/gimptemplateeditor.c
index ab16be20bb..2adf110fac 100644
--- a/app/widgets/gimptemplateeditor.c
+++ b/app/widgets/gimptemplateeditor.c
@@ -35,6 +35,7 @@
 
 #include "core/gimp.h"
 #include "core/gimptemplate.h"
+#include "core/gimp-utils.h"
 
 #include "gimppropwidgets.h"
 #include "gimptemplateeditor.h"
@@ -630,41 +631,21 @@ gimp_template_editor_precision_changed (GtkWidget          *widget,
 {
   GimpTemplateEditorPrivate *private = GET_PRIVATE (editor);
   GimpComponentType          component_type;
+  GimpTRCType                trc;
 
   gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget),
                                  (gint *) &component_type);
 
-  g_object_set (private->template,
-                "component-type", component_type,
+  g_object_get (private->template,
+                "trc", &trc,
                 NULL);
 
-  /* when changing this logic, also change the same switch()
-   * in convert-precision-dialog.c
-   */
-  switch (component_type)
-    {
-    case GIMP_COMPONENT_TYPE_U8:
-      /* default to gamma for 8 bit */
-      g_object_set (private->template,
-                    "trc", GIMP_TRC_NON_LINEAR,
-                    NULL);
-      break;
+  trc = gimp_suggest_trc_for_component_type (component_type, trc);
 
-    case GIMP_COMPONENT_TYPE_U16:
-    case GIMP_COMPONENT_TYPE_U32:
-    default:
-      /* leave gamma alone by default for 16/32 bit int */
-      break;
-
-    case GIMP_COMPONENT_TYPE_HALF:
-    case GIMP_COMPONENT_TYPE_FLOAT:
-    case GIMP_COMPONENT_TYPE_DOUBLE:
-      /* default to linear for floating point */
-      g_object_set (private->template,
-                    "trc", GIMP_TRC_LINEAR,
-                    NULL);
-      break;
-    }
+  g_object_set (private->template,
+                "component-type", component_type,
+                "trc",            trc,
+                NULL);
 }
 
 static void


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