[gimp] app: add GimpGenericTransformTool



commit 4c71ee8e7a60be84bfec1b0aa64ca003ab867e61
Author: Ell <ell_se yahoo com>
Date:   Sat Jan 27 06:08:55 2018 -0500

    app: add GimpGenericTransformTool
    
    A subclass of GimpTransformTool, to be used as a base class for
    transform tools that calculate their matrix based on 4 pairs of
    input/output points, and that display the transform matrix as their
    GUI (this includes the unified, perspective, and handle transform
    tools; the next commit ports them over to
    GimpGenericTransformTool).
    
    When the resulting matrix of the input/output mapping sends any of
    the output points to, or past, infinity, GimpGenericTransformTool
    sets GimpTransformTool::transform_valid to FALSE, and displays an
    appropriate message in the tool GUI, instead of showing the matrix.

 app/core/gimp-transform-utils.c      |   56 ++++++++++
 app/core/gimp-transform-utils.h      |    3 +
 app/tools/Makefile.am                |    2 +
 app/tools/gimpgenerictransformtool.c |  187 ++++++++++++++++++++++++++++++++++
 app/tools/gimpgenerictransformtool.h |   60 +++++++++++
 app/tools/tools-types.h              |    1 +
 po/POTFILES.in                       |    1 +
 7 files changed, 310 insertions(+), 0 deletions(-)
---
diff --git a/app/core/gimp-transform-utils.c b/app/core/gimp-transform-utils.c
index 93bc640..a4a05be 100644
--- a/app/core/gimp-transform-utils.c
+++ b/app/core/gimp-transform-utils.c
@@ -519,6 +519,62 @@ gimp_transform_matrix_handles (GimpMatrix3 *matrix,
 }
 
 gboolean
+gimp_transform_matrix_generic (GimpMatrix3       *matrix,
+                               const GimpVector2 *input_points,
+                               const GimpVector2 *output_points)
+{
+  GimpMatrix3 trafo;
+  gdouble     coeff[8 * 9];
+  gint        i;
+
+  g_return_val_if_fail (matrix != NULL, FALSE);
+  g_return_val_if_fail (input_points != NULL, FALSE);
+  g_return_val_if_fail (output_points != NULL, FALSE);
+
+  for (i = 0; i < 4; i++)
+    {
+      coeff[i * 9 + 0] = input_points[i].x;
+      coeff[i * 9 + 1] = input_points[i].y;
+      coeff[i * 9 + 2] = 1.0;
+      coeff[i * 9 + 3] = 0.0;
+      coeff[i * 9 + 4] = 0.0;
+      coeff[i * 9 + 5] = 0.0;
+      coeff[i * 9 + 6] = -input_points[i].x * output_points[i].x;
+      coeff[i * 9 + 7] = -input_points[i].y * output_points[i].x;
+      coeff[i * 9 + 8] =                      output_points[i].x;
+
+      coeff[(i + 4) * 9 + 0] = 0.0;
+      coeff[(i + 4) * 9 + 1] = 0.0;
+      coeff[(i + 4) * 9 + 2] = 0.0;
+      coeff[(i + 4) * 9 + 3] = input_points[i].x;
+      coeff[(i + 4) * 9 + 4] = input_points[i].y;
+      coeff[(i + 4) * 9 + 5] = 1.0;
+      coeff[(i + 4) * 9 + 6] = -input_points[i].x * output_points[i].y;
+      coeff[(i + 4) * 9 + 7] = -input_points[i].y * output_points[i].y;
+      coeff[(i + 4) * 9 + 8] =                      output_points[i].y;
+    }
+
+  if (! mod_gauss (coeff, (gdouble *) trafo.coeff, 8))
+    return FALSE;
+
+  trafo.coeff[2][2] = 1.0;
+
+  gimp_matrix3_mult (&trafo, matrix);
+
+  for (i = 0; i < 4; i++)
+    {
+      gdouble w = matrix->coeff[2][0] * input_points[i].x +
+                  matrix->coeff[2][1] * input_points[i].y +
+                  matrix->coeff[2][2];
+
+      if (w <= EPSILON)
+        return FALSE;
+    }
+
+  return TRUE;
+}
+
+gboolean
 gimp_transform_polygon_is_convex (gdouble x1,
                                   gdouble y1,
                                   gdouble x2,
diff --git a/app/core/gimp-transform-utils.h b/app/core/gimp-transform-utils.h
index 6033c09..69500a7 100644
--- a/app/core/gimp-transform-utils.h
+++ b/app/core/gimp-transform-utils.h
@@ -102,6 +102,9 @@ void       gimp_transform_matrix_handles       (GimpMatrix3         *matrix,
                                                 gdouble              t_y3,
                                                 gdouble              t_x4,
                                                 gdouble              t_y4);
+gboolean   gimp_transform_matrix_generic       (GimpMatrix3         *matrix,
+                                                const GimpVector2   *input_points,
+                                                const GimpVector2   *output_points);
 
 gboolean   gimp_transform_polygon_is_convex    (gdouble              x1,
                                                 gdouble              y1,
diff --git a/app/tools/Makefile.am b/app/tools/Makefile.am
index dfe640e..4b33a4c 100644
--- a/app/tools/Makefile.am
+++ b/app/tools/Makefile.am
@@ -100,6 +100,8 @@ libapptools_a_sources = \
        gimpfuzzyselecttool.h           \
        gimpgegltool.c                  \
        gimpgegltool.h                  \
+       gimpgenerictransformtool.c      \
+       gimpgenerictransformtool.h      \
        gimpguidetool.c                 \
        gimpguidetool.h                 \
        gimphandletransformoptions.c    \
diff --git a/app/tools/gimpgenerictransformtool.c b/app/tools/gimpgenerictransformtool.c
new file mode 100644
index 0000000..1eceb86
--- /dev/null
+++ b/app/tools/gimpgenerictransformtool.c
@@ -0,0 +1,187 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gegl.h>
+#include <gtk/gtk.h>
+
+#include "libgimpmath/gimpmath.h"
+#include "libgimpwidgets/gimpwidgets.h"
+
+#include "tools-types.h"
+
+#include "core/gimp-transform-utils.h"
+
+#include "widgets/gimphelp-ids.h"
+#include "widgets/gimpwidgets-utils.h"
+
+#include "display/gimpdisplay.h"
+#include "display/gimptoolgui.h"
+
+#include "gimpgenerictransformtool.h"
+#include "gimptoolcontrol.h"
+#include "gimptransformoptions.h"
+
+#include "gimp-intl.h"
+
+
+/*  local function prototypes  */
+
+static void   gimp_generic_transform_tool_dialog        (GimpTransformTool *tr_tool);
+static void   gimp_generic_transform_tool_dialog_update (GimpTransformTool *tr_tool);
+static void   gimp_generic_transform_tool_prepare       (GimpTransformTool *tr_tool);
+static void   gimp_generic_transform_tool_recalc_matrix (GimpTransformTool *tr_tool,
+                                                         GimpToolWidget    *widget);
+
+
+G_DEFINE_TYPE (GimpGenericTransformTool, gimp_generic_transform_tool,
+               GIMP_TYPE_TRANSFORM_TOOL)
+
+
+static void
+gimp_generic_transform_tool_class_init (GimpGenericTransformToolClass *klass)
+{
+  GimpTransformToolClass *trans_class = GIMP_TRANSFORM_TOOL_CLASS (klass);
+
+  trans_class->dialog        = gimp_generic_transform_tool_dialog;
+  trans_class->dialog_update = gimp_generic_transform_tool_dialog_update;
+  trans_class->prepare       = gimp_generic_transform_tool_prepare;
+  trans_class->recalc_matrix = gimp_generic_transform_tool_recalc_matrix;
+}
+
+static void
+gimp_generic_transform_tool_init (GimpGenericTransformTool *unified_tool)
+{
+}
+
+static void
+gimp_generic_transform_tool_dialog (GimpTransformTool *tr_tool)
+{
+  GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tr_tool);
+  GtkWidget                *frame;
+  GtkWidget                *vbox;
+  GtkWidget                *table;
+  GtkWidget                *label;
+  GtkSizeGroup             *size_group;
+  gint                      x, y;
+
+  frame = gimp_frame_new (_("Transform Matrix"));
+  gtk_box_pack_start (GTK_BOX (gimp_tool_gui_get_vbox (tr_tool->gui)), frame,
+                      FALSE, FALSE, 0);
+  gtk_widget_show (frame);
+
+  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+  gtk_container_add (GTK_CONTAINER (frame), vbox);
+  gtk_widget_show (vbox);
+
+  size_group = gtk_size_group_new (GTK_SIZE_GROUP_BOTH);
+
+  table = generic->matrix_table = gtk_table_new (3, 3, FALSE);
+  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
+  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
+  gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);
+  gtk_size_group_add_widget (size_group, table);
+  gtk_widget_show (table);
+
+  for (y = 0; y < 3; y++)
+    {
+      for (x = 0; x < 3; x++)
+        {
+          label = generic->matrix_labels[y][x] = gtk_label_new (" ");
+          gtk_label_set_xalign (GTK_LABEL (label), 1.0);
+          gtk_label_set_width_chars (GTK_LABEL (label), 12);
+          gtk_table_attach (GTK_TABLE (table), label,
+                            x, x + 1, y, y + 1, GTK_EXPAND, GTK_FILL, 0, 0);
+          gtk_widget_show (label);
+        }
+    }
+
+  label = generic->invalid_label = gtk_label_new (_("Invalid transform"));
+  gimp_label_set_attributes (GTK_LABEL (label),
+                             PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
+                             -1);
+  gtk_size_group_add_widget (size_group, label);
+  gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
+
+  g_object_unref (size_group);
+}
+
+static void
+gimp_generic_transform_tool_dialog_update (GimpTransformTool *tr_tool)
+{
+  GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tr_tool);
+
+  if (tr_tool->transform_valid)
+    {
+      gint x, y;
+
+      gtk_widget_show (generic->matrix_table);
+      gtk_widget_hide (generic->invalid_label);
+
+      for (y = 0; y < 3; y++)
+        {
+          for (x = 0; x < 3; x++)
+            {
+              gchar buf[32];
+
+              g_snprintf (buf, sizeof (buf),
+                          "%10.5f", tr_tool->transform.coeff[y][x]);
+
+              gtk_label_set_text (GTK_LABEL (generic->matrix_labels[y][x]), buf);
+            }
+        }
+    }
+  else
+    {
+      gtk_widget_show (generic->invalid_label);
+      gtk_widget_hide (generic->matrix_table);
+    }
+}
+
+static void
+gimp_generic_transform_tool_prepare (GimpTransformTool *tr_tool)
+{
+  GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tr_tool);
+
+  generic->input_points[0] = (GimpVector2) {tr_tool->x1, tr_tool->y1};
+  generic->input_points[1] = (GimpVector2) {tr_tool->x2, tr_tool->y1};
+  generic->input_points[2] = (GimpVector2) {tr_tool->x1, tr_tool->y2};
+  generic->input_points[3] = (GimpVector2) {tr_tool->x2, tr_tool->y2};
+
+  memcpy (generic->output_points, generic->input_points,
+          sizeof (generic->input_points));
+}
+
+static void
+gimp_generic_transform_tool_recalc_matrix (GimpTransformTool *tr_tool,
+                                           GimpToolWidget    *widget)
+{
+  GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tr_tool);
+
+  if (GIMP_GENERIC_TRANSFORM_TOOL_GET_CLASS (generic)->recalc_points)
+    {
+      GIMP_GENERIC_TRANSFORM_TOOL_GET_CLASS (generic)->recalc_points (generic,
+                                                                      widget);
+    }
+
+  gimp_matrix3_identity (&tr_tool->transform);
+  tr_tool->transform_valid =
+    gimp_transform_matrix_generic (&tr_tool->transform,
+                                   generic->input_points,
+                                   generic->output_points);
+}
diff --git a/app/tools/gimpgenerictransformtool.h b/app/tools/gimpgenerictransformtool.h
new file mode 100644
index 0000000..6160d4d
--- /dev/null
+++ b/app/tools/gimpgenerictransformtool.h
@@ -0,0 +1,60 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_GENERIC_TRANSFORM_TOOL_H__
+#define __GIMP_GENERIC_TRANSFORM_TOOL_H__
+
+
+#include "gimptransformtool.h"
+
+
+#define GIMP_TYPE_GENERIC_TRANSFORM_TOOL            (gimp_generic_transform_tool_get_type ())
+#define GIMP_GENERIC_TRANSFORM_TOOL(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GIMP_TYPE_GENERIC_TRANSFORM_TOOL, GimpGenericTransformTool))
+#define GIMP_GENERIC_TRANSFORM_TOOL_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), 
GIMP_TYPE_GENERIC_TRANSFORM_TOOL, GimpGenericTransformToolClass))
+#define GIMP_IS_GENERIC_TRANSFORM_TOOL(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
GIMP_TYPE_GENERIC_TRANSFORM_TOOL))
+#define GIMP_IS_GENERIC_TRANSFORM_TOOL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), 
GIMP_TYPE_GENERIC_TRANSFORM_TOOL))
+#define GIMP_GENERIC_TRANSFORM_TOOL_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), 
GIMP_TYPE_GENERIC_TRANSFORM_TOOL, GimpGenericTransformToolClass))
+
+
+typedef struct _GimpGenericTransformToolClass GimpGenericTransformToolClass;
+
+struct _GimpGenericTransformTool
+{
+  GimpTransformTool  parent_instance;
+
+  GimpVector2        input_points[4];
+  GimpVector2        output_points[4];
+
+  GtkWidget         *matrix_table;
+  GtkWidget         *matrix_labels[3][3];
+  GtkWidget         *invalid_label;
+};
+
+struct _GimpGenericTransformToolClass
+{
+  GimpTransformToolClass  parent_class;
+
+  /*  virtual functions  */
+  void   (* recalc_points) (GimpGenericTransformTool *generic,
+                            GimpToolWidget           *widget);
+};
+
+
+GType   gimp_generic_transform_tool_get_type (void) G_GNUC_CONST;
+
+
+#endif  /*  __GIMP_GENERIC_TRANSFORM_TOOL_H__  */
diff --git a/app/tools/tools-types.h b/app/tools/tools-types.h
index 88c4ddf..1986e57 100644
--- a/app/tools/tools-types.h
+++ b/app/tools/tools-types.h
@@ -34,6 +34,7 @@ typedef struct _GimpBrushTool                GimpBrushTool;
 typedef struct _GimpColorTool                GimpColorTool;
 typedef struct _GimpDrawTool                 GimpDrawTool;
 typedef struct _GimpFilterTool               GimpFilterTool;
+typedef struct _GimpGenericTransformTool     GimpGenericTransformTool;
 typedef struct _GimpPaintTool                GimpPaintTool;
 typedef struct _GimpTransformTool            GimpTransformTool;
 
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 8081e6e..c675f4e 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -434,6 +434,7 @@ app/tools/gimpforegroundselecttool.c
 app/tools/gimpfreeselecttool.c
 app/tools/gimpfuzzyselecttool.c
 app/tools/gimpgegltool.c
+app/tools/gimpgenerictransformtool.c
 app/tools/gimpguidetool.c
 app/tools/gimphandletransformoptions.c
 app/tools/gimphandletransformtool.c


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