summaryrefslogtreecommitdiffstats
path: root/app/tools/gimpgenerictransformtool.c
blob: 9f20af6fb1cbba5c86606bf34e4164217dbb8a13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* 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 <https://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 "gimptransformgridoptions.h"

#include "gimp-intl.h"


/*  local function prototypes  */

static gboolean   gimp_generic_transform_tool_info_to_matrix (GimpTransformGridTool *tg_tool,
                                                              GimpMatrix3           *transform);
static void       gimp_generic_transform_tool_dialog         (GimpTransformGridTool *tg_tool);
static void       gimp_generic_transform_tool_dialog_update  (GimpTransformGridTool *tg_tool);
static void       gimp_generic_transform_tool_prepare        (GimpTransformGridTool *tg_tool);


G_DEFINE_TYPE (GimpGenericTransformTool, gimp_generic_transform_tool,
               GIMP_TYPE_TRANSFORM_GRID_TOOL)

#define parent_class gimp_generic_transform_tool_parent_class


static void
gimp_generic_transform_tool_class_init (GimpGenericTransformToolClass *klass)
{
  GimpTransformGridToolClass *tg_class = GIMP_TRANSFORM_GRID_TOOL_CLASS (klass);

  tg_class->info_to_matrix = gimp_generic_transform_tool_info_to_matrix;
  tg_class->dialog         = gimp_generic_transform_tool_dialog;
  tg_class->dialog_update  = gimp_generic_transform_tool_dialog_update;
  tg_class->prepare        = gimp_generic_transform_tool_prepare;
}

static void
gimp_generic_transform_tool_init (GimpGenericTransformTool *unified_tool)
{
}

static gboolean
gimp_generic_transform_tool_info_to_matrix (GimpTransformGridTool *tg_tool,
                                            GimpMatrix3           *transform)
{
  GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tg_tool);

  if (GIMP_GENERIC_TRANSFORM_TOOL_GET_CLASS (generic)->info_to_points)
    GIMP_GENERIC_TRANSFORM_TOOL_GET_CLASS (generic)->info_to_points (generic);

  gimp_matrix3_identity (transform);

  return gimp_transform_matrix_generic (transform,
                                        generic->input_points,
                                        generic->output_points);
}

static void
gimp_generic_transform_tool_dialog (GimpTransformGridTool *tg_tool)
{
  GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tg_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 (tg_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), 8);
          gimp_label_set_attributes (GTK_LABEL (label),
                                     PANGO_ATTR_SCALE, PANGO_SCALE_SMALL,
                                     -1);
          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 (GimpTransformGridTool *tg_tool)
{
  GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tg_tool);
  GimpMatrix3               transform;
  gboolean                  transform_valid;

  transform_valid = gimp_transform_grid_tool_info_to_matrix (tg_tool,
                                                             &transform);

  if (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), "%.4f", 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 (GimpTransformGridTool *tg_tool)
{
  GimpTransformTool        *tr_tool = GIMP_TRANSFORM_TOOL (tg_tool);
  GimpGenericTransformTool *generic = GIMP_GENERIC_TRANSFORM_TOOL (tg_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));
}