gimpbrushpipe.c 8.36 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 * Copyright (C) 1999 Adrian Likins and Tor Lillqvist
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8 9 10 11 12 13 14 15
 * (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
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 18
 */

19 20
#include "config.h"

21
#include <gdk-pixbuf/gdk-pixbuf.h>
22
#include <gegl.h>
23

24 25
#include "libgimpmath/gimpmath.h"

26
#include "core-types.h"
27

28
#include "gimpbrush-private.h"
29
#include "gimpbrushpipe.h"
30
#include "gimpbrushpipe-load.h"
31

32

33
static void        gimp_brush_pipe_finalize         (GObject          *object);
34

35 36
static gint64      gimp_brush_pipe_get_memsize      (GimpObject       *object,
                                                     gint64           *gui_size);
37

38 39 40 41 42 43
static gboolean    gimp_brush_pipe_get_popup_size   (GimpViewable     *viewable,
                                                     gint              width,
                                                     gint              height,
                                                     gboolean          dot_for_dot,
                                                     gint             *popup_width,
                                                     gint             *popup_height);
44

45 46
static void        gimp_brush_pipe_begin_use        (GimpBrush        *brush);
static void        gimp_brush_pipe_end_use          (GimpBrush        *brush);
47 48 49 50 51 52
static GimpBrush * gimp_brush_pipe_select_brush     (GimpBrush        *brush,
                                                     const GimpCoords *last_coords,
                                                     const GimpCoords *current_coords);
static gboolean    gimp_brush_pipe_want_null_motion (GimpBrush        *brush,
                                                     const GimpCoords *last_coords,
                                                     const GimpCoords *current_coords);
53

54

55
G_DEFINE_TYPE (GimpBrushPipe, gimp_brush_pipe, GIMP_TYPE_BRUSH);
56

57
#define parent_class gimp_brush_pipe_parent_class
58

59 60 61 62

static void
gimp_brush_pipe_class_init (GimpBrushPipeClass *klass)
{
63 64 65 66
  GObjectClass      *object_class      = G_OBJECT_CLASS (klass);
  GimpObjectClass   *gimp_object_class = GIMP_OBJECT_CLASS (klass);
  GimpViewableClass *viewable_class    = GIMP_VIEWABLE_CLASS (klass);
  GimpBrushClass    *brush_class       = GIMP_BRUSH_CLASS (klass);
67

68 69 70
  object_class->finalize         = gimp_brush_pipe_finalize;

  gimp_object_class->get_memsize = gimp_brush_pipe_get_memsize;
71

72 73
  viewable_class->get_popup_size = gimp_brush_pipe_get_popup_size;

74 75
  brush_class->begin_use         = gimp_brush_pipe_begin_use;
  brush_class->end_use           = gimp_brush_pipe_end_use;
76 77
  brush_class->select_brush      = gimp_brush_pipe_select_brush;
  brush_class->want_null_motion  = gimp_brush_pipe_want_null_motion;
78 79 80 81 82 83 84 85 86
}

static void
gimp_brush_pipe_init (GimpBrushPipe *pipe)
{
  pipe->current   = NULL;
  pipe->dimension = 0;
  pipe->rank      = NULL;
  pipe->stride    = NULL;
87
  pipe->n_brushes = 0;
88 89 90 91 92 93 94 95
  pipe->brushes   = NULL;
  pipe->select    = NULL;
  pipe->index     = NULL;
}

static void
gimp_brush_pipe_finalize (GObject *object)
{
Michael Natterer's avatar
Michael Natterer committed
96
  GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (object);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

  if (pipe->rank)
    {
      g_free (pipe->rank);
      pipe->rank = NULL;
    }
  if (pipe->stride)
    {
      g_free (pipe->stride);
      pipe->stride = NULL;
    }

  if (pipe->brushes)
    {
      gint i;

113
      for (i = 0; i < pipe->n_brushes; i++)
114 115
        if (pipe->brushes[i])
          g_object_unref (pipe->brushes[i]);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

      g_free (pipe->brushes);
      pipe->brushes = NULL;
    }

  if (pipe->select)
    {
      g_free (pipe->select);
      pipe->select = NULL;
    }
  if (pipe->index)
    {
      g_free (pipe->index);
      pipe->index = NULL;
    }

132 133
  GIMP_BRUSH (pipe)->priv->mask   = NULL;
  GIMP_BRUSH (pipe)->priv->pixmap = NULL;
134 135 136 137

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

138
static gint64
139
gimp_brush_pipe_get_memsize (GimpObject *object,
140
                             gint64     *gui_size)
141
{
Michael Natterer's avatar
Michael Natterer committed
142
  GimpBrushPipe *pipe    = GIMP_BRUSH_PIPE (object);
143
  gint64         memsize = 0;
144 145 146 147 148 149
  gint           i;

  memsize += pipe->dimension * (sizeof (gint) /* rank   */ +
                                sizeof (gint) /* stride */ +
                                sizeof (PipeSelectModes));

150
  for (i = 0; i < pipe->n_brushes; i++)
151 152
    memsize += gimp_object_get_memsize (GIMP_OBJECT (pipe->brushes[i]),
                                        gui_size);
153

154 155
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
156 157
}

158
static gboolean
159 160 161 162 163 164
gimp_brush_pipe_get_popup_size (GimpViewable *viewable,
                                gint          width,
                                gint          height,
                                gboolean      dot_for_dot,
                                gint         *popup_width,
                                gint         *popup_height)
165
{
166
  return gimp_viewable_get_size (viewable, popup_width, popup_height);
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
static void
gimp_brush_pipe_begin_use (GimpBrush *brush)
{
  GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush);
  gint           i;

  GIMP_BRUSH_CLASS (parent_class)->begin_use (brush);

  for (i = 0; i < pipe->n_brushes; i++)
    if (pipe->brushes[i])
      gimp_brush_begin_use (pipe->brushes[i]);
}

static void
gimp_brush_pipe_end_use (GimpBrush *brush)
{
  GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush);
  gint           i;

  GIMP_BRUSH_CLASS (parent_class)->end_use (brush);

  for (i = 0; i < pipe->n_brushes; i++)
    if (pipe->brushes[i])
      gimp_brush_end_use (pipe->brushes[i]);
}

195
static GimpBrush *
196 197 198
gimp_brush_pipe_select_brush (GimpBrush        *brush,
                              const GimpCoords *last_coords,
                              const GimpCoords *current_coords)
199
{
Michael Natterer's avatar
Michael Natterer committed
200
  GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush);
201
  gint           i, brushix, ix;
202

203
  if (pipe->n_brushes == 1)
204
    return GIMP_BRUSH (pipe->current);
205

206 207 208 209
  brushix = 0;
  for (i = 0; i < pipe->dimension; i++)
    {
      switch (pipe->select[i])
210 211 212 213 214 215
        {
        case PIPE_SELECT_INCREMENTAL:
          ix = (pipe->index[i] + 1) % pipe->rank[i];
          break;

        case PIPE_SELECT_ANGULAR:
216 217 218
          /* Coords angle is already nomalized,
           * offset by 90 degrees is still needed
           * because hoses were made PS compatible*/
219
          ix = (gint) RINT ((1.0 - current_coords->direction + 0.25) * pipe->rank[i]) % pipe->rank[i];
220 221
          break;

222
        case PIPE_SELECT_VELOCITY:
223
          ix = ROUND (current_coords->velocity * pipe->rank[i]);
224 225
          break;

226 227
        case PIPE_SELECT_RANDOM:
          /* This probably isn't the right way */
228
          ix = g_random_int_range (0, pipe->rank[i]);
229 230 231
          break;

        case PIPE_SELECT_PRESSURE:
232
          ix = RINT (current_coords->pressure * (pipe->rank[i] - 1));
233 234 235
          break;

        case PIPE_SELECT_TILT_X:
236
          ix = RINT (current_coords->xtilt / 2.0 * pipe->rank[i]) + pipe->rank[i] / 2;
237 238 239
          break;

        case PIPE_SELECT_TILT_Y:
240
          ix = RINT (current_coords->ytilt / 2.0 * pipe->rank[i]) + pipe->rank[i] / 2;
241 242 243 244 245 246 247
          break;

        case PIPE_SELECT_CONSTANT:
        default:
          ix = pipe->index[i];
          break;
        }
248

Michael Natterer's avatar
Michael Natterer committed
249
      pipe->index[i] = CLAMP (ix, 0, pipe->rank[i] - 1);
250
      brushix += pipe->stride[i] * pipe->index[i];
251 252
    }

253
  /* Make sure is inside bounds */
254
  brushix = CLAMP (brushix, 0, pipe->n_brushes - 1);
255

256 257 258
  pipe->current = pipe->brushes[brushix];

  return GIMP_BRUSH (pipe->current);
259
}
260

261
static gboolean
262 263 264
gimp_brush_pipe_want_null_motion (GimpBrush        *brush,
                                  const GimpCoords *last_coords,
                                  const GimpCoords *current_coords)
265
{
Michael Natterer's avatar
Michael Natterer committed
266
  GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush);
267
  gint           i;
268

269
  if (pipe->n_brushes == 1)
270 271 272 273 274 275 276 277
    return TRUE;

  for (i = 0; i < pipe->dimension; i++)
    if (pipe->select[i] == PIPE_SELECT_ANGULAR)
      return FALSE;

  return TRUE;
}