gimpbrushpipe.c 10.3 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 <stdlib.h>
23
#include <string.h>
24
#include <ctype.h>
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#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
41 42
#include <gtk/gtk.h>

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

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

56 57
#include "libgimp/gimpintl.h"

58

59
static GimpBrushClass  *parent_class;
60 61


62 63 64
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);
65

66

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

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

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

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

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

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

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

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

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

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

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

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

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

  return TRUE;
}

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

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

  pipe = GIMP_BRUSH_PIPE (object);

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

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

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

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

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

185
  object_class = GTK_OBJECT_CLASS (klass);
186 187 188 189 190 191
  brush_class = GIMP_BRUSH_CLASS (klass);

  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;
192

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

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

209 210
GtkType
gimp_brush_pipe_get_type (void)
211
{
Michael Natterer's avatar
Michael Natterer committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  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
      };

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

231 232 233
  return type;
}

234 235 236
#include <errno.h>

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

  g_return_val_if_fail (filename != NULL, NULL);

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

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

260 261 262 263 264 265
  /*  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)
266
    {
267 268 269 270 271 272 273 274 275 276
      pipe = GIMP_BRUSH_PIPE (gtk_type_new (GIMP_TYPE_BRUSH_PIPE));      
      GIMP_BRUSH (pipe)->name = buffer->str;
    }
  g_string_free (buffer, FALSE);

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

  /*  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);
    }

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

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

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

308
      pipe->dimension = params.dim;
309 310 311 312
      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
313 314 315
      /* placement is not used at all ?? */
      if (params.free_placement_string)
	g_free (params.placement);
316

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

352 353
  g_string_free (buffer, TRUE);

354 355 356
  totalcells = 1;		/* Not all necessarily present, maybe */
  for (i = 0; i < pipe->dimension; i++)
    totalcells *= pipe->rank[i];
357
  pipe->stride = g_new0 (gint, pipe->dimension);
358 359 360 361 362 363 364 365
  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);
366

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

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

373 374
      if (pipe->brushes[pipe->nbrushes])
	{
375 376
	  gtk_object_ref (GTK_OBJECT (pipe->brushes[pipe->nbrushes]));
	  gtk_object_sink (GTK_OBJECT (pipe->brushes[pipe->nbrushes]));
377
	  
Sven Neumann's avatar
Sven Neumann committed
378
	  g_free (GIMP_BRUSH (pipe->brushes[pipe->nbrushes])->name);
379
	  GIMP_BRUSH (pipe->brushes[pipe->nbrushes])->name = NULL;
380
	}
381
      else
382
	{
383 384 385
	  g_message (_("Failed to load one of the brushes in the brush pipe\n\"%s\""), 
		       filename);
	  close (fd);
386 387 388
	  gtk_object_sink (GTK_OBJECT (pipe));
	  return NULL;
	}
389
  
390
      pipe->nbrushes++;
391
    }
392

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

396
  /*  just to satisfy the code that relies on this crap  */
Michael Natterer's avatar
Michael Natterer committed
397

398 399
  GIMP_BRUSH (pipe)->mask   = pipe->brushes[0]->mask;
  GIMP_BRUSH (pipe)->pixmap = pipe->brushes[0]->pixmap;
400

401
  close (fd);
402

403
  return GIMP_BRUSH (pipe);
404
}