summaryrefslogtreecommitdiffstats
path: root/app/core/gimpimage-symmetry.c
blob: 366f63ef540891664f4553d97fb1059dad97e6db (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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpimage-symmetry.c
 * Copyright (C) 2015 Jehan <jehan@gimp.org>
 *
 * 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 <gdk-pixbuf/gdk-pixbuf.h>

#include "core-types.h"

#include "gimpsymmetry.h"
#include "gimpimage.h"
#include "gimpimage-private.h"
#include "gimpimage-symmetry.h"
#include "gimpsymmetry-mandala.h"
#include "gimpsymmetry-mirror.h"
#include "gimpsymmetry-tiling.h"


/**
 * gimp_image_symmetry_list:
 *
 * Returns a list of #GType of all existing symmetries.
 **/
GList *
gimp_image_symmetry_list (void)
{
  GList *list = NULL;

  list = g_list_prepend (list, GINT_TO_POINTER (GIMP_TYPE_MIRROR));
  list = g_list_prepend (list, GINT_TO_POINTER (GIMP_TYPE_TILING));
  list = g_list_prepend (list, GINT_TO_POINTER (GIMP_TYPE_MANDALA));

  return list;
}

/**
 * gimp_image_symmetry_new:
 * @image: the #GimpImage
 * @type:  the #GType of the symmetry
 *
 * Creates a new #GimpSymmetry of @type attached to @image.
 * @type must be a subtype of `GIMP_TYPE_SYMMETRY`.
 * Note that using the base @type `GIMP_TYPE_SYMMETRY` creates an
 * identity transformation.
 *
 * Returns: the new #GimpSymmetry.
 **/
GimpSymmetry *
gimp_image_symmetry_new (GimpImage *image,
                         GType      type)
{
  GimpSymmetry *sym = NULL;

  g_return_val_if_fail (g_type_is_a (type, GIMP_TYPE_SYMMETRY), NULL);

  sym = g_object_new (type,
                      "image", image,
                      NULL);

  return sym;
}

/**
 * gimp_image_symmetry_add:
 * @image: the #GimpImage
 * @type:  the #GType of the symmetry
 *
 * Add a symmetry of type @type to @image and make it the
 * active transformation.
 **/
void
gimp_image_symmetry_add (GimpImage    *image,
                         GimpSymmetry *sym)
{
  GimpImagePrivate *private;

  g_return_if_fail (GIMP_IS_IMAGE (image));
  g_return_if_fail (GIMP_IS_SYMMETRY (sym));

  private = GIMP_IMAGE_GET_PRIVATE (image);

  private->symmetries = g_list_prepend (private->symmetries,
                                        g_object_ref (sym));
}

/**
 * gimp_image_symmetry_remove:
 * @image:   the #GimpImage
 * @sym: the #GimpSymmetry
 *
 * Remove @sym from the list of symmetries of @image.
 * If it was the active transformation, unselect it first.
 **/
void
gimp_image_symmetry_remove (GimpImage    *image,
                            GimpSymmetry *sym)
{
  GimpImagePrivate *private;

  g_return_if_fail (GIMP_IS_SYMMETRY (sym));
  g_return_if_fail (GIMP_IS_IMAGE (image));

  private = GIMP_IMAGE_GET_PRIVATE (image);

  if (private->active_symmetry == sym)
    gimp_image_set_active_symmetry (image, GIMP_TYPE_SYMMETRY);

  private->symmetries = g_list_remove (private->symmetries, sym);
  g_object_unref (sym);
}

/**
 * gimp_image_symmetry_get:
 * @image: the #GimpImage
 *
 * Returns: the list of #GimpSymmetry set on @image.
 * The returned list belongs to @image and should not be freed.
 **/
GList *
gimp_image_symmetry_get (GimpImage *image)
{
  GimpImagePrivate *private;

  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);

  private = GIMP_IMAGE_GET_PRIVATE (image);

  return private->symmetries;
}

/**
 * gimp_image_set_active_symmetry:
 * @image: the #GimpImage
 * @type:  the #GType of the symmetry
 *
 * Select the symmetry of type @type.
 * Using the GType allows to select a transformation without
 * knowing whether one of the same @type was already created.
 *
 * Returns TRUE on success, FALSE if no such symmetry was found.
 **/
gboolean
gimp_image_set_active_symmetry (GimpImage *image,
                                GType      type)
{
  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);

  g_object_set (image,
                "symmetry", type,
                NULL);

  return TRUE;
}

/**
 * gimp_image_get_active_symmetry:
 * @image: the #GimpImage
 *
 * Returns the #GimpSymmetry transformation active on @image.
 **/
GimpSymmetry *
gimp_image_get_active_symmetry (GimpImage *image)
{
  GimpImagePrivate *private;

  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);

  private = GIMP_IMAGE_GET_PRIVATE (image);

  return private->active_symmetry;
}