1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gegl.h>
21 #include <gtk/gtk.h>
22 
23 #include "libgimpmath/gimpmath.h"
24 #include "libgimpwidgets/gimpwidgets.h"
25 
26 #include "tools-types.h"
27 
28 #include "core/gimp-transform-utils.h"
29 
30 #include "widgets/gimphelp-ids.h"
31 #include "widgets/gimpwidgets-utils.h"
32 
33 #include "display/gimpdisplay.h"
34 #include "display/gimptoolgui.h"
35 
36 #include "gimpgenerictransformtool.h"
37 #include "gimptoolcontrol.h"
38 #include "gimptransformgridoptions.h"
39 
40 #include "gimp-intl.h"
41 
42 
43 /*  local function prototypes  */
44 
45 static gboolean   gimp_generic_transform_tool_info_to_matrix (GimpTransformGridTool *tg_tool,
46                                                               GimpMatrix3           *transform);
47 static void       gimp_generic_transform_tool_dialog         (GimpTransformGridTool *tg_tool);
48 static void       gimp_generic_transform_tool_dialog_update  (GimpTransformGridTool *tg_tool);
49 static void       gimp_generic_transform_tool_prepare        (GimpTransformGridTool *tg_tool);
50 
51 
G_DEFINE_TYPE(GimpGenericTransformTool,gimp_generic_transform_tool,GIMP_TYPE_TRANSFORM_GRID_TOOL)52 G_DEFINE_TYPE (GimpGenericTransformTool, gimp_generic_transform_tool,
53                GIMP_TYPE_TRANSFORM_GRID_TOOL)
54 
55 #define parent_class gimp_generic_transform_tool_parent_class
56 
57 
58 static void
59 gimp_generic_transform_tool_class_init (GimpGenericTransformToolClass *klass)
60 {
61   GimpTransformGridToolClass *tg_class = GIMP_TRANSFORM_GRID_TOOL_CLASS (klass);
62 
63   tg_class->info_to_matrix = gimp_generic_transform_tool_info_to_matrix;
64   tg_class->dialog         = gimp_generic_transform_tool_dialog;
65   tg_class->dialog_update  = gimp_generic_transform_tool_dialog_update;
66   tg_class->prepare        = gimp_generic_transform_tool_prepare;
67 }
68 
69 static void
gimp_generic_transform_tool_init(GimpGenericTransformTool * unified_tool)70 gimp_generic_transform_tool_init (GimpGenericTransformTool *unified_tool)
71 {
72 }
73 
74 static gboolean
gimp_generic_transform_tool_info_to_matrix(GimpTransformGridTool * tg_tool,GimpMatrix3 * transform)75 gimp_generic_transform_tool_info_to_matrix (GimpTransformGridTool *tg_tool,
76                                             GimpMatrix3           *transform)
77 {
78   GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tg_tool);
79 
80   if (GIMP_GENERIC_TRANSFORM_TOOL_GET_CLASS (generic)->info_to_points)
81     GIMP_GENERIC_TRANSFORM_TOOL_GET_CLASS (generic)->info_to_points (generic);
82 
83   gimp_matrix3_identity (transform);
84 
85   return gimp_transform_matrix_generic (transform,
86                                         generic->input_points,
87                                         generic->output_points);
88 }
89 
90 static void
gimp_generic_transform_tool_dialog(GimpTransformGridTool * tg_tool)91 gimp_generic_transform_tool_dialog (GimpTransformGridTool *tg_tool)
92 {
93   GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tg_tool);
94   GtkWidget                *frame;
95   GtkWidget                *vbox;
96   GtkWidget                *table;
97   GtkWidget                *label;
98   GtkSizeGroup             *size_group;
99   gint                      x, y;
100 
101   frame = gimp_frame_new (_("Transform Matrix"));
102   gtk_box_pack_start (GTK_BOX (gimp_tool_gui_get_vbox (tg_tool->gui)), frame,
103                       FALSE, FALSE, 0);
104   gtk_widget_show (frame);
105 
106   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
107   gtk_container_add (GTK_CONTAINER (frame), vbox);
108   gtk_widget_show (vbox);
109 
110   size_group = gtk_size_group_new (GTK_SIZE_GROUP_BOTH);
111 
112   table = generic->matrix_table = gtk_table_new (3, 3, FALSE);
113   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
114   gtk_table_set_col_spacings (GTK_TABLE (table), 2);
115   gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);
116   gtk_size_group_add_widget (size_group, table);
117   gtk_widget_show (table);
118 
119   for (y = 0; y < 3; y++)
120     {
121       for (x = 0; x < 3; x++)
122         {
123           label = generic->matrix_labels[y][x] = gtk_label_new (" ");
124           gtk_label_set_xalign (GTK_LABEL (label), 1.0);
125           gtk_label_set_width_chars (GTK_LABEL (label), 8);
126           gimp_label_set_attributes (GTK_LABEL (label),
127                                      PANGO_ATTR_SCALE, PANGO_SCALE_SMALL,
128                                      -1);
129           gtk_table_attach (GTK_TABLE (table), label,
130                             x, x + 1, y, y + 1, GTK_EXPAND, GTK_FILL, 0, 0);
131           gtk_widget_show (label);
132         }
133     }
134 
135   label = generic->invalid_label = gtk_label_new (_("Invalid transform"));
136   gimp_label_set_attributes (GTK_LABEL (label),
137                              PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
138                              -1);
139   gtk_size_group_add_widget (size_group, label);
140   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
141 
142   g_object_unref (size_group);
143 }
144 
145 static void
gimp_generic_transform_tool_dialog_update(GimpTransformGridTool * tg_tool)146 gimp_generic_transform_tool_dialog_update (GimpTransformGridTool *tg_tool)
147 {
148   GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tg_tool);
149   GimpMatrix3               transform;
150   gboolean                  transform_valid;
151 
152   transform_valid = gimp_transform_grid_tool_info_to_matrix (tg_tool,
153                                                              &transform);
154 
155   if (transform_valid)
156     {
157       gint x, y;
158 
159       gtk_widget_show (generic->matrix_table);
160       gtk_widget_hide (generic->invalid_label);
161 
162       for (y = 0; y < 3; y++)
163         {
164           for (x = 0; x < 3; x++)
165             {
166               gchar buf[32];
167 
168               g_snprintf (buf, sizeof (buf), "%.4f", transform.coeff[y][x]);
169 
170               gtk_label_set_text (GTK_LABEL (generic->matrix_labels[y][x]), buf);
171             }
172         }
173     }
174   else
175     {
176       gtk_widget_show (generic->invalid_label);
177       gtk_widget_hide (generic->matrix_table);
178     }
179 }
180 
181 static void
gimp_generic_transform_tool_prepare(GimpTransformGridTool * tg_tool)182 gimp_generic_transform_tool_prepare (GimpTransformGridTool *tg_tool)
183 {
184   GimpTransformTool        *tr_tool = GIMP_TRANSFORM_TOOL (tg_tool);
185   GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tg_tool);
186 
187   generic->input_points[0] = (GimpVector2) {tr_tool->x1, tr_tool->y1};
188   generic->input_points[1] = (GimpVector2) {tr_tool->x2, tr_tool->y1};
189   generic->input_points[2] = (GimpVector2) {tr_tool->x1, tr_tool->y2};
190   generic->input_points[3] = (GimpVector2) {tr_tool->x2, tr_tool->y2};
191 
192   memcpy (generic->output_points, generic->input_points,
193           sizeof (generic->input_points));
194 }
195