gimpbrushpipe.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 * Copyright (C) 1999 Adrian Likins and Tor Lillqvist
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

20 21
#include "config.h"

22
#include <errno.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <ctype.h>
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>

#ifdef G_OS_WIN32
#include <io.h>
#endif

#ifndef _O_BINARY
#define _O_BINARY 0
#endif

Sven Neumann's avatar
Sven Neumann committed
42 43
#include <gtk/gtk.h>

44
#include "apptypes.h"
45 46 47 48 49
#include "appenv.h"
#include "brush_header.h"
#include "pattern_header.h"
#include "patterns.h"
#include "gimpbrush.h"
50
#include "gimpbrushpipe.h"
51 52
#include "paint_core.h"
#include "gimprc.h"
53 54

#include "libgimp/gimpmath.h"
55
#include "libgimp/gimpparasiteio.h"
56

57 58
#include "libgimp/gimpintl.h"

59

60 61 62
static GimpBrush * gimp_brush_pipe_select_brush     (PaintCore *paint_core);
static gboolean    gimp_brush_pipe_want_null_motion (PaintCore *paint_core);
static void        gimp_brush_pipe_destroy          (GtkObject *object);
63

64

65 66 67
static GimpBrushClass *parent_class = NULL;


68
static GimpBrush *
69
gimp_brush_pipe_select_brush (PaintCore *paint_core)
70
{
71
  GimpBrushPipe *pipe;
72 73
  gint           i, brushix, ix;
  gdouble        angle;
74

75 76
  g_return_val_if_fail (paint_core != NULL, NULL);
  g_return_val_if_fail (GIMP_IS_BRUSH_PIPE (paint_core->brush), NULL);
77

78
  pipe = GIMP_BRUSH_PIPE (paint_core->brush);
79

80 81
  if (pipe->nbrushes == 1)
    return GIMP_BRUSH (pipe->current);
82

83 84 85 86 87 88
  brushix = 0;
  for (i = 0; i < pipe->dimension; i++)
    {
      switch (pipe->select[i])
	{
	case PIPE_SELECT_INCREMENTAL:
89
	  ix = (pipe->index[i] + 1) % pipe->rank[i];
90 91 92 93
	  break;
	case PIPE_SELECT_ANGULAR:
	  angle = atan2 (paint_core->cury - paint_core->lasty,
			 paint_core->curx - paint_core->lastx);
94 95 96
	  /* Offset angle to be compatible with PSP tubes */
	  angle += G_PI_2;
	  /* Map it to the [0..2*G_PI) interval */
97
	  if (angle < 0)
98 99 100 101
	    angle += 2.0 * G_PI;
	  else if (angle > 2.0 * G_PI)
	    angle -= 2.0 * G_PI;
	  ix = RINT (angle / (2.0 * G_PI) * pipe->rank[i]);
102 103 104
	  break;
	case PIPE_SELECT_RANDOM:
	  /* This probably isn't the right way */
105
	  ix = rand () % pipe->rank[i];
106 107
	  break;
	case PIPE_SELECT_PRESSURE:
108
	  ix = RINT (paint_core->curpressure * (pipe->rank[i] - 1));
109 110
	  break;
	case PIPE_SELECT_TILT_X:
111
	  ix = RINT (paint_core->curxtilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
112 113
	  break;
	case PIPE_SELECT_TILT_Y:
114
	  ix = RINT (paint_core->curytilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
115
	  break;
Adam D. Moss's avatar
Adam D. Moss committed
116 117 118 119
	case PIPE_SELECT_CONSTANT:
	default:
	  ix = pipe->index[i];
	  break;
120
	}
121
      pipe->index[i] = CLAMP (ix, 0, pipe->rank[i]-1);
122
      brushix += pipe->stride[i] * pipe->index[i];
123 124
    }

125
  /* Make sure is inside bounds */
126
  brushix = CLAMP (brushix, 0, pipe->nbrushes-1);
127

128 129 130
  pipe->current = pipe->brushes[brushix];

  return GIMP_BRUSH (pipe->current);
131
}
132

133
static gboolean
134
gimp_brush_pipe_want_null_motion (PaintCore *paint_core)
135 136
{
  GimpBrushPipe *pipe;
137
  gint           i;
138

139 140
  g_return_val_if_fail (paint_core != NULL, TRUE);
  g_return_val_if_fail (GIMP_IS_BRUSH_PIPE (paint_core->brush), TRUE);
141

142
  pipe = GIMP_BRUSH_PIPE (paint_core->brush);
143 144 145 146 147 148 149 150 151 152 153

  if (pipe->nbrushes == 1)
    return TRUE;

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

  return TRUE;
}

154
static void
Michael Natterer's avatar
Michael Natterer committed
155
gimp_brush_pipe_destroy (GtkObject *object)
156
{
157
  GimpBrushPipe *pipe;
158
  gint           i;
159 160 161 162 163 164 165

  g_return_if_fail (object != NULL);
  g_return_if_fail (GIMP_IS_BRUSH_PIPE (object));

  pipe = GIMP_BRUSH_PIPE (object);

  g_free (pipe->rank);
166
  g_free (pipe->stride);
167 168

  for (i = 1; i < pipe->nbrushes; i++)
169 170
    if (pipe->brushes[i])
      gtk_object_unref (GTK_OBJECT (pipe->brushes[i]));
171 172 173 174 175

  g_free (pipe->brushes);
  g_free (pipe->select);
  g_free (pipe->index);

176 177
  if (GTK_OBJECT_CLASS (parent_class)->destroy)
    GTK_OBJECT_CLASS (parent_class)->destroy (object);
178 179 180 181 182 183
}

static void
gimp_brush_pipe_class_init (GimpBrushPipeClass *klass)
{
  GtkObjectClass *object_class;
184
  GimpBrushClass *brush_class;
Michael Natterer's avatar
Michael Natterer committed
185

186 187
  object_class = (GtkObjectClass *) klass;
  brush_class  = (GimpBrushClass *) klass;
188 189 190 191 192

  parent_class = gtk_type_class (GIMP_TYPE_BRUSH);

  brush_class->select_brush     = gimp_brush_pipe_select_brush;
  brush_class->want_null_motion = gimp_brush_pipe_want_null_motion;
193

Michael Natterer's avatar
Michael Natterer committed
194
  object_class->destroy = gimp_brush_pipe_destroy;
195 196 197
}

void
198
gimp_brush_pipe_init (GimpBrushPipe *pipe)
199
{
200
  pipe->current   = NULL;
201
  pipe->dimension = 0;
Michael Natterer's avatar
Michael Natterer committed
202
  pipe->rank      = NULL;
203
  pipe->stride    = NULL;
Michael Natterer's avatar
Michael Natterer committed
204
  pipe->nbrushes  = 0;
205
  pipe->brushes   = NULL;
Michael Natterer's avatar
Michael Natterer committed
206 207
  pipe->select    = NULL;
  pipe->index     = NULL;
208 209
}

210 211
GtkType
gimp_brush_pipe_get_type (void)
212
{
Michael Natterer's avatar
Michael Natterer committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  static GtkType type = 0;

  if (!type)
    {
      GtkTypeInfo info =
      {
	"GimpBrushPipe",
	sizeof (GimpBrushPipe),
	sizeof (GimpBrushPipeClass),
	(GtkClassInitFunc) gimp_brush_pipe_class_init,
	(GtkObjectInitFunc) gimp_brush_pipe_init,
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
	(GtkClassInitFunc) NULL
      };

229
      type = gtk_type_unique (GIMP_TYPE_BRUSH, &info);
Michael Natterer's avatar
Michael Natterer committed
230 231
    }

232 233 234
  return type;
}

235
GimpBrush *
236
gimp_brush_pipe_load (const gchar *filename)
237
{
238
  GimpBrushPipe     *pipe = NULL;
239
  GimpPixPipeParams  params;
240 241 242 243 244 245 246
  gint               i;
  gint               num_of_brushes = 0;
  gint               totalcells;
  gchar             *paramstring;
  GString           *buffer;
  gchar              c;
  gint               fd;
247 248 249 250 251

  g_return_val_if_fail (filename != NULL, NULL);

  fd = open (filename, O_RDONLY | _O_BINARY);
  if (fd == -1)
252
    {
253
      g_message ("Couldn't open file '%s'", filename);
254 255
      return NULL;
    }
256

257
  /* The file format starts with a painfully simple text header */
258

259 260 261 262 263 264
  /*  get the name  */
  buffer = g_string_new (NULL);
  while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
    g_string_append_c (buffer, c);
    
  if (buffer->len > 0 && buffer->len < 1024)
265
    {
266
      pipe = GIMP_BRUSH_PIPE (gtk_type_new (GIMP_TYPE_BRUSH_PIPE));      
267
      gimp_object_set_name (GIMP_OBJECT (pipe), buffer->str);
268
    }
269
  g_string_free (buffer, TRUE);
270 271 272 273 274 275

  if (!pipe)
    {
      g_message ("Couldn't read name for brush pipe from file '%s'\n", 
		 filename);
      close (fd);
276 277
      return NULL;
    }
278 279 280 281 282 283 284 285 286 287 288

  /*  get the number of brushes  */
  buffer = g_string_new (NULL);
  while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
    g_string_append_c (buffer, c);

  if (buffer->len > 0 && buffer->len < 1024)
    {
      num_of_brushes = strtol (buffer->str, &paramstring, 10);
    }

289 290
  if (num_of_brushes < 1)
    {
291 292 293
      g_message (_("Brush pipes should have at least one brush:\n\"%s\""), 
		 filename);
      close (fd);
294
      gtk_object_sink (GTK_OBJECT (pipe));
295
      g_string_free (buffer, TRUE);
296 297
      return NULL;
    }
298

Michael Natterer's avatar
Michael Natterer committed
299
  while (*paramstring && isspace (*paramstring))
300
    paramstring++;
301

302
  if (*paramstring)
303
    {
304 305
      gimp_pixpipe_params_init (&params);
      gimp_pixpipe_params_parse (paramstring, &params);
306

307
      pipe->dimension = params.dim;
308 309 310 311
      pipe->rank      = g_new0 (gint, pipe->dimension);
      pipe->select    = g_new0 (PipeSelectModes, pipe->dimension);
      pipe->index     = g_new0 (gint, pipe->dimension);

Sven Neumann's avatar
Sven Neumann committed
312 313 314
      /* placement is not used at all ?? */
      if (params.free_placement_string)
	g_free (params.placement);
315

316 317
      for (i = 0; i < pipe->dimension; i++)
	{
318 319
	  pipe->rank[i] = params.rank[i];
	  if (strcmp (params.selection[i], "incremental") == 0)
320
	    pipe->select[i] = PIPE_SELECT_INCREMENTAL;
321
	  else if (strcmp (params.selection[i], "angular") == 0)
322
	    pipe->select[i] = PIPE_SELECT_ANGULAR;
323
	  else if (strcmp (params.selection[i], "velocity") == 0)
324
	    pipe->select[i] = PIPE_SELECT_VELOCITY;
325
	  else if (strcmp (params.selection[i], "random") == 0)
326
	    pipe->select[i] = PIPE_SELECT_RANDOM;
327
	  else if (strcmp (params.selection[i], "pressure") == 0)
328
	    pipe->select[i] = PIPE_SELECT_PRESSURE;
329
	  else if (strcmp (params.selection[i], "xtilt") == 0)
330
	    pipe->select[i] = PIPE_SELECT_TILT_X;
331
	  else if (strcmp (params.selection[i], "ytilt") == 0)
332 333 334
	    pipe->select[i] = PIPE_SELECT_TILT_Y;
	  else
	    pipe->select[i] = PIPE_SELECT_CONSTANT;
Sven Neumann's avatar
Sven Neumann committed
335 336
	  if (params.free_selection_string)
	    g_free (params.selection[i]);
337 338 339 340 341 342
	  pipe->index[i] = 0;
	}
    }
  else
    {
      pipe->dimension = 1;
Michael Natterer's avatar
Michael Natterer committed
343 344 345
      pipe->rank      = g_new (gint, 1);
      pipe->rank[0]   = num_of_brushes;
      pipe->select    = g_new (PipeSelectModes, 1);
346
      pipe->select[0] = PIPE_SELECT_INCREMENTAL;
Michael Natterer's avatar
Michael Natterer committed
347 348
      pipe->index     = g_new (gint, 1);
      pipe->index[0]  = 0;
349 350
    }

351 352
  g_string_free (buffer, TRUE);

353 354 355
  totalcells = 1;		/* Not all necessarily present, maybe */
  for (i = 0; i < pipe->dimension; i++)
    totalcells *= pipe->rank[i];
356
  pipe->stride = g_new0 (gint, pipe->dimension);
357 358 359 360 361 362 363 364
  for (i = 0; i < pipe->dimension; i++)
    {
      if (i == 0)
	pipe->stride[i] = totalcells / pipe->rank[i];
      else
	pipe->stride[i] = pipe->stride[i-1] / pipe->rank[i];
    }
  g_assert (pipe->stride[pipe->dimension-1] == 1);
365

366
  pipe->brushes = g_new0 (GimpBrush *, num_of_brushes);
367 368 369

  while (pipe->nbrushes < num_of_brushes)
    {
370
      pipe->brushes[pipe->nbrushes] = gimp_brush_load_brush (fd, filename);
371

372 373
      if (pipe->brushes[pipe->nbrushes])
	{
374 375
	  gtk_object_ref (GTK_OBJECT (pipe->brushes[pipe->nbrushes]));
	  gtk_object_sink (GTK_OBJECT (pipe->brushes[pipe->nbrushes]));
376 377 378

	  gimp_object_set_name (GIMP_OBJECT (pipe->brushes[pipe->nbrushes]),
				NULL);
379
	}
380
      else
381
	{
382 383 384
	  g_message (_("Failed to load one of the brushes in the brush pipe\n\"%s\""), 
		       filename);
	  close (fd);
385 386 387
	  gtk_object_sink (GTK_OBJECT (pipe));
	  return NULL;
	}
388
  
389
      pipe->nbrushes++;
390
    }
391

392 393
  /* Current brush is the first one. */
  pipe->current = pipe->brushes[0];
394

395
  /*  just to satisfy the code that relies on this crap  */
396 397 398 399 400 401
  GIMP_BRUSH (pipe)->filename = g_strdup (filename);
  GIMP_BRUSH (pipe)->spacing  = pipe->current->spacing;
  GIMP_BRUSH (pipe)->x_axis   = pipe->current->x_axis;
  GIMP_BRUSH (pipe)->y_axis   = pipe->current->y_axis;
  GIMP_BRUSH (pipe)->mask     = pipe->current->mask;
  GIMP_BRUSH (pipe)->pixmap   = pipe->current->pixmap;
402

403
  close (fd);
404

405
  return GIMP_BRUSH (pipe);
406
}