gimpbrushpipe-load.c 12.4 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
22
#include "config.h"

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

27
#include "apptypes.h"
28
29
30
31
32
#include "appenv.h"
#include "brush_header.h"
#include "pattern_header.h"
#include "patterns.h"
#include "gimpbrush.h"
33
34
#include "gimpbrushpipe.h"
#include "gimpbrushpipeP.h"
35
36
#include "paint_core.h"
#include "gimprc.h"
37
38

#include "libgimp/gimpmath.h"
39
#include "libgimp/parasiteio.h"
40

41
42
#include "libgimp/gimpintl.h"

Michael Natterer's avatar
Michael Natterer committed
43
44
static GimpBrushClass       *gimp_brush_class;
static GimpBrushPixmapClass *gimp_brush_pixmap_class;
45

Michael Natterer's avatar
Michael Natterer committed
46
47
static GimpBrush *gimp_brush_pixmap_select_brush     (PaintCore *paint_core);
static gboolean   gimp_brush_pixmap_want_null_motion (PaintCore *paint_core);
48

49
static void
50
51
52
53
54
55
56
gimp_brush_pixmap_destroy (GtkObject *object)
{
  GimpBrushPixmap *pixmap;

  g_return_if_fail (GIMP_IS_BRUSH_PIXMAP (object));

  pixmap = GIMP_BRUSH_PIXMAP (object);
Michael Natterer's avatar
Michael Natterer committed
57

58
59
  temp_buf_free (pixmap->pixmap_mask);

Michael Natterer's avatar
Michael Natterer committed
60
61
  if (GTK_OBJECT_CLASS (gimp_brush_class)->destroy)
    (* GTK_OBJECT_CLASS (gimp_brush_class)->destroy) (object);
62
63
64
65
66
67
68
69
70
71
72
73
74
}

static void
gimp_brush_pixmap_class_init (GimpBrushPixmapClass *klass)
{
  GtkObjectClass *object_class;
  GimpBrushClass *brush_class;
  
  object_class = GTK_OBJECT_CLASS (klass);
  brush_class = GIMP_BRUSH_CLASS (klass);

  gimp_brush_class = gtk_type_class (gimp_brush_get_type ());

Michael Natterer's avatar
Michael Natterer committed
75
76
  object_class->destroy         = gimp_brush_pixmap_destroy;
  brush_class->select_brush     = gimp_brush_pixmap_select_brush;
77
  brush_class->want_null_motion = gimp_brush_pixmap_want_null_motion;
78
79
80
81
82
83
}

void
gimp_brush_pixmap_init (GimpBrushPixmap *brush)
{
  brush->pixmap_mask = NULL;
84
  brush->pipe        = NULL;
85
86
87
88
89
}

GtkType
gimp_brush_pixmap_get_type (void)
{
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  static GtkType type = 0;

  if (!type)
    {
      GtkTypeInfo info =
      {
	"GimpBrushPixmap",
	sizeof (GimpBrushPixmap),
	sizeof (GimpBrushPixmapClass),
	(GtkClassInitFunc) gimp_brush_pixmap_class_init,
	(GtkObjectInitFunc) gimp_brush_pixmap_init,
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
	(GtkClassInitFunc) NULL
      };

      type = gtk_type_unique (GIMP_TYPE_BRUSH, &info);
    }

109
110
  return type;
}
111

112
113
114
static GimpBrush *
gimp_brush_pixmap_select_brush (PaintCore *paint_core)
{
115
  GimpBrushPipe *pipe;
116
  int i, brushix, ix;
117
  double angle;
118
119
120

  g_return_val_if_fail (GIMP_IS_BRUSH_PIXMAP (paint_core->brush), NULL);

121
  pipe = GIMP_BRUSH_PIXMAP (paint_core->brush)->pipe;
122

123
124
  if (pipe->nbrushes == 1)
    return GIMP_BRUSH (pipe->current);
125

126
127
128
129
130
131
  brushix = 0;
  for (i = 0; i < pipe->dimension; i++)
    {
      switch (pipe->select[i])
	{
	case PIPE_SELECT_INCREMENTAL:
132
	  ix = (pipe->index[i] + 1) % pipe->rank[i];
133
134
135
136
	  break;
	case PIPE_SELECT_ANGULAR:
	  angle = atan2 (paint_core->cury - paint_core->lasty,
			 paint_core->curx - paint_core->lastx);
137
138
139
	  /* Offset angle to be compatible with PSP tubes */
	  angle += G_PI_2;
	  /* Map it to the [0..2*G_PI) interval */
140
	  if (angle < 0)
141
142
143
144
	    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]);
145
146
147
	  break;
	case PIPE_SELECT_RANDOM:
	  /* This probably isn't the right way */
148
	  ix = rand () % pipe->rank[i];
149
150
	  break;
	case PIPE_SELECT_PRESSURE:
151
	  ix = RINT (paint_core->curpressure * (pipe->rank[i] - 1));
152
153
	  break;
	case PIPE_SELECT_TILT_X:
154
	  ix = RINT (paint_core->curxtilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
155
156
	  break;
	case PIPE_SELECT_TILT_Y:
157
	  ix = RINT (paint_core->curytilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
158
	  break;
Adam D. Moss's avatar
Adam D. Moss committed
159
160
161
162
	case PIPE_SELECT_CONSTANT:
	default:
	  ix = pipe->index[i];
	  break;
163
	}
164
      pipe->index[i] = CLAMP (ix, 0, pipe->rank[i]-1);
165
166
      brushix += pipe->stride[i] * pipe->index[i];
      /* g_print ("ix at %d: %d, brushix: %d\n", i, ix, brushix); */
167
168
    }

169
  /* Make sure is inside bounds */
170
  brushix = CLAMP (brushix, 0, pipe->nbrushes-1);
171

172
173
174
  pipe->current = pipe->brushes[brushix];

  return GIMP_BRUSH (pipe->current);
175
}
176

177
178
179
180
static gboolean
gimp_brush_pixmap_want_null_motion (PaintCore *paint_core)
{
  GimpBrushPipe *pipe;
181
  gint i;
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

  g_return_val_if_fail (GIMP_IS_BRUSH_PIXMAP (paint_core->brush), TRUE);

  pipe = GIMP_BRUSH_PIXMAP (paint_core->brush)->pipe;

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

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

  return TRUE;
}

197
static void
Michael Natterer's avatar
Michael Natterer committed
198
gimp_brush_pipe_destroy (GtkObject *object)
199
{
200
  GimpBrushPipe *pipe;
Michael Natterer's avatar
Michael Natterer committed
201
  gint i;
202
203
204
205
206
207
208

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

  pipe = GIMP_BRUSH_PIPE (object);

  g_free (pipe->rank);
209
  g_free (pipe->stride);
210
211

  for (i = 1; i < pipe->nbrushes; i++)
Michael Natterer's avatar
Michael Natterer committed
212
    gtk_object_unref (GTK_OBJECT (pipe->brushes[i]));
213
214
215
216
217

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

Michael Natterer's avatar
Michael Natterer committed
218
219
  if (GTK_OBJECT_CLASS (gimp_brush_pixmap_class)->destroy)
    (* GTK_OBJECT_CLASS (gimp_brush_pixmap_class)->destroy) (object);
220
221
222
223
224
225
}

static void
gimp_brush_pipe_class_init (GimpBrushPipeClass *klass)
{
  GtkObjectClass *object_class;
Michael Natterer's avatar
Michael Natterer committed
226

227
228
  object_class = GTK_OBJECT_CLASS (klass);

Michael Natterer's avatar
Michael Natterer committed
229
230
  gimp_brush_pixmap_class = gtk_type_class (GIMP_TYPE_BRUSH_PIXMAP);
  object_class->destroy = gimp_brush_pipe_destroy;
231
232
233
}

void
234
gimp_brush_pipe_init (GimpBrushPipe *pipe)
235
{
236
  pipe->dimension = 0;
Michael Natterer's avatar
Michael Natterer committed
237
238
239
240
  pipe->rank      = NULL;
  pipe->nbrushes  = 0;
  pipe->select    = NULL;
  pipe->index     = NULL;
241
242
}

243
244
GtkType
gimp_brush_pipe_get_type (void)
245
{
Michael Natterer's avatar
Michael Natterer committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  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
      };

      type = gtk_type_unique (GIMP_TYPE_BRUSH_PIXMAP, &info);
    }

265
266
267
268
  return type;
}

GimpBrushPipe *
Michael Natterer's avatar
Michael Natterer committed
269
gimp_brush_pipe_load (gchar *filename)
270
271
{
  GimpBrushPipe *pipe;
Michael Natterer's avatar
Michael Natterer committed
272
273
274
275
  GPattern      *pattern;
  PixPipeParams  params;
  FILE  *fp;
  gchar  buf[1024];
Sven Neumann's avatar
Sven Neumann committed
276
  gchar *name;
Michael Natterer's avatar
Michael Natterer committed
277
278
279
  gint   i;
  gint   num_of_brushes;
  gint   totalcells;
280
  gchar *paramstring;
281

282
283
  if ((fp = fopen (filename, "rb")) == NULL)
    return NULL;
284

285
286
287
288
289
290
291
292
293
  /* The file format starts with a painfully simple text header
   * and we use a painfully simple way to read it
   */
  if (fgets (buf, 1024, fp) == NULL)
    {
      fclose (fp);
      return NULL;
    }
  buf[strlen (buf) - 1] = 0;
294

295
296
  pipe = GIMP_BRUSH_PIPE (gimp_type_new (GIMP_TYPE_BRUSH_PIPE));
  name = g_strdup (buf);
297
298

  /* get the number of brushes */
299
300
301
302
303
304
  if (fgets (buf, 1024, fp) == NULL)
    {
      fclose (fp);
      gimp_object_destroy (pipe);
      return NULL;
    }
Michael Natterer's avatar
Michael Natterer committed
305
  num_of_brushes = strtol (buf, &paramstring, 10);
306
307
  if (num_of_brushes < 1)
    {
308
      g_message (_("Brush pipes should have at least one brush."));
309
310
311
312
      fclose (fp);
      gimp_object_destroy (pipe);
      return NULL;
    }
313

Michael Natterer's avatar
Michael Natterer committed
314
  while (*paramstring && isspace (*paramstring))
315
    paramstring++;
316

317
  if (*paramstring)
318
    {
319
320
321
      pixpipeparams_init (&params);
      pixpipeparams_parse (paramstring, &params);
      pipe->dimension = params.dim;
Michael Natterer's avatar
Michael Natterer committed
322
323
324
      pipe->rank      = g_new (gint, pipe->dimension);
      pipe->select    = g_new (PipeSelectModes, pipe->dimension);
      pipe->index     = g_new (gint, pipe->dimension);
Sven Neumann's avatar
Sven Neumann committed
325
326
327
      /* placement is not used at all ?? */
      if (params.free_placement_string)
	g_free (params.placement);
328
329
      for (i = 0; i < pipe->dimension; i++)
	{
330
331
	  pipe->rank[i] = params.rank[i];
	  if (strcmp (params.selection[i], "incremental") == 0)
332
	    pipe->select[i] = PIPE_SELECT_INCREMENTAL;
333
	  else if (strcmp (params.selection[i], "angular") == 0)
334
	    pipe->select[i] = PIPE_SELECT_ANGULAR;
335
	  else if (strcmp (params.selection[i], "velocity") == 0)
336
	    pipe->select[i] = PIPE_SELECT_VELOCITY;
337
	  else if (strcmp (params.selection[i], "random") == 0)
338
	    pipe->select[i] = PIPE_SELECT_RANDOM;
339
	  else if (strcmp (params.selection[i], "pressure") == 0)
340
	    pipe->select[i] = PIPE_SELECT_PRESSURE;
341
	  else if (strcmp (params.selection[i], "xtilt") == 0)
342
	    pipe->select[i] = PIPE_SELECT_TILT_X;
343
	  else if (strcmp (params.selection[i], "ytilt") == 0)
344
345
346
	    pipe->select[i] = PIPE_SELECT_TILT_Y;
	  else
	    pipe->select[i] = PIPE_SELECT_CONSTANT;
Sven Neumann's avatar
Sven Neumann committed
347
348
	  if (params.free_selection_string)
	    g_free (params.selection[i]);
349
350
351
352
353
354
	  pipe->index[i] = 0;
	}
    }
  else
    {
      pipe->dimension = 1;
Michael Natterer's avatar
Michael Natterer committed
355
356
357
      pipe->rank      = g_new (gint, 1);
      pipe->rank[0]   = num_of_brushes;
      pipe->select    = g_new (PipeSelectModes, 1);
358
      pipe->select[0] = PIPE_SELECT_INCREMENTAL;
Michael Natterer's avatar
Michael Natterer committed
359
360
      pipe->index     = g_new (gint, 1);
      pipe->index[0]  = 0;
361
362
363
364
365
    }

  totalcells = 1;		/* Not all necessarily present, maybe */
  for (i = 0; i < pipe->dimension; i++)
    totalcells *= pipe->rank[i];
Michael Natterer's avatar
Michael Natterer committed
366
  pipe->stride = g_new (gint, pipe->dimension);
367
368
369
370
371
372
373
374
  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);
375

Michael Natterer's avatar
Michael Natterer committed
376
  pattern = g_new0 (GPattern, 1);
377

Michael Natterer's avatar
Michael Natterer committed
378
  pipe->brushes = g_new0 (GimpBrushPixmap *, num_of_brushes);
379

380
381
  /* First pixmap brush in the list is the pipe itself */
  pipe->brushes[0] = GIMP_BRUSH_PIXMAP (pipe);
382

383
384
385
386
387
388
  /* Current pixmap brush is the first one. */
  pipe->current = pipe->brushes[0];

  while (pipe->nbrushes < num_of_brushes)
    {
      if (pipe->nbrushes > 0)
389
	{
390
391
	  pipe->brushes[pipe->nbrushes] =
	    GIMP_BRUSH_PIXMAP (gimp_type_new (GIMP_TYPE_BRUSH_PIXMAP));
Sven Neumann's avatar
Sven Neumann committed
392
	  g_free (GIMP_BRUSH (pipe->brushes[pipe->nbrushes])->name);
393
	  GIMP_BRUSH (pipe->brushes[pipe->nbrushes])->name = NULL;
394
	}
Michael Natterer's avatar
Michael Natterer committed
395

396
      pipe->brushes[pipe->nbrushes]->pipe = pipe;
397

398
399
400
      /* load the brush */
      if (!gimp_brush_load_brush (GIMP_BRUSH (pipe->brushes[pipe->nbrushes]),
				  fp, filename)
401
	  || !pattern_load (pattern, fp, filename))
402
       {
403
404
	  g_message (_("Failed to load one of the\n"
                       "brushes in the brush pipe."));
405
	  fclose (fp);
Sven Neumann's avatar
Sven Neumann committed
406
	  pattern_free (pattern);
407
	  gimp_object_destroy (pipe);
408
409
	  return NULL;
       }
410
411
412
413

      if (pipe->nbrushes == 0)
	{
	  /* Replace name with the whole pipe's name */
Sven Neumann's avatar
Sven Neumann committed
414
	  g_free (GIMP_BRUSH (pipe)->name);
415
416
417
	  GIMP_BRUSH (pipe)->name = name;
	}
      pipe->brushes[pipe->nbrushes]->pixmap_mask = pattern->mask;
Sven Neumann's avatar
Sven Neumann committed
418
      g_free (pattern->name);
419
      pipe->nbrushes++;
420
    }
421
422

  /*  Clean up  */
423
424
  fclose (fp);

425
  g_free (pattern);
Michael Natterer's avatar
Michael Natterer committed
426

427
428
429
430
  return pipe;
}

GimpBrushPipe *
Michael Natterer's avatar
Michael Natterer committed
431
gimp_brush_pixmap_load (gchar *filename)
432
433
{
  GimpBrushPipe *pipe;
434
  GPattern *pattern;
435
436
437
438
439
440
  FILE *fp;

  if ((fp = fopen (filename, "rb")) == NULL)
    return NULL;

  pipe = GIMP_BRUSH_PIPE (gimp_type_new (GIMP_TYPE_BRUSH_PIPE));
441

442
443
  /* A (single) pixmap brush is a pixmap pipe brush with just one pixmap */
  pipe->dimension = 1;
Michael Natterer's avatar
Michael Natterer committed
444
445
446
  pipe->rank      = g_new (gint, 1);
  pipe->rank[0]   = 1;
  pipe->select    = g_new (PipeSelectModes, 1);
447
  pipe->select[0] = PIPE_SELECT_INCREMENTAL;
Michael Natterer's avatar
Michael Natterer committed
448
449
  pipe->index     = g_new (gint, 1);
  pipe->index[0]  = 0;
450

Michael Natterer's avatar
Michael Natterer committed
451
  pattern = g_new0 (GPattern, 1);
452

Michael Natterer's avatar
Michael Natterer committed
453
  pipe->brushes = g_new (GimpBrushPixmap *, 1);
454
455

  pipe->brushes[0] = GIMP_BRUSH_PIXMAP (pipe);
Michael Natterer's avatar
Michael Natterer committed
456
  pipe->current    = pipe->brushes[0];
457
458
459
460
461
462

  pipe->brushes[0]->pipe = pipe;

  /* load the brush */
  if (!gimp_brush_load_brush (GIMP_BRUSH (pipe->brushes[0]),
			      fp, filename)
463
      || !pattern_load (pattern, fp, filename))
464
    {
Sven Neumann's avatar
Sven Neumann committed
465
      g_message (_("Failed to load pixmap brush."));
466
      fclose (fp);
Sven Neumann's avatar
Sven Neumann committed
467
      pattern_free (pattern);
468
469
470
      gimp_object_destroy (pipe);
      return NULL;
    }
Michael Natterer's avatar
Michael Natterer committed
471

472
473
474
475
476
477
  pipe->brushes[0]->pixmap_mask = pattern->mask;

  pipe->nbrushes = 1;
  /*  Clean up  */
  fclose (fp);

Sven Neumann's avatar
Sven Neumann committed
478
479
  g_free (pattern->name);
  g_free (pattern);
Michael Natterer's avatar
Michael Natterer committed
480

481
  return pipe;
482
483
484
485
486
487
488
489
490
491
}

TempBuf *
gimp_brush_pixmap_pixmap (GimpBrushPixmap *brush)
{
  g_return_val_if_fail (brush != NULL, NULL);
  g_return_val_if_fail (GIMP_IS_BRUSH_PIXMAP (brush), NULL);

  return brush->pixmap_mask;
}