1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
* 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 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
#include <sys/types.h>
37
#include <glib-object.h>
43
#include "libgimpbase/gimpbase.h"
44
#include "libgimpbase/gimpparasiteio.h"
45
#include "libgimpmath/gimpmath.h"
47
#include "core-types.h"
49
#include "base/temp-buf.h"
51
#include "gimpbrush.h"
52
#include "gimpbrush-header.h"
53
#include "gimpbrushpipe.h"
54
#include "gimppattern-header.h"
56
#include "gimp-intl.h"
59
static void gimp_brush_pipe_class_init (GimpBrushPipeClass *klass);
60
static void gimp_brush_pipe_init (GimpBrushPipe *pipe);
62
static void gimp_brush_pipe_finalize (GObject *object);
64
static gint64 gimp_brush_pipe_get_memsize (GimpObject *object,
67
static gboolean gimp_brush_pipe_get_popup_size (GimpViewable *viewable,
74
static GimpBrush * gimp_brush_pipe_select_brush (GimpBrush *brush,
75
GimpCoords *last_coords,
76
GimpCoords *cur_coords);
77
static gboolean gimp_brush_pipe_want_null_motion (GimpBrush *brush,
78
GimpCoords *last_coords,
79
GimpCoords *cur_coords);
82
static GimpBrushClass *parent_class = NULL;
86
gimp_brush_pipe_get_type (void)
88
static GType brush_type = 0;
92
static const GTypeInfo brush_info =
94
sizeof (GimpBrushPipeClass),
96
(GBaseFinalizeFunc) NULL,
97
(GClassInitFunc) gimp_brush_pipe_class_init,
98
NULL, /* class_finalize */
99
NULL, /* class_data */
100
sizeof (GimpBrushPipe),
102
(GInstanceInitFunc) gimp_brush_pipe_init,
105
brush_type = g_type_register_static (GIMP_TYPE_BRUSH,
114
gimp_brush_pipe_class_init (GimpBrushPipeClass *klass)
116
GObjectClass *object_class = G_OBJECT_CLASS (klass);
117
GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
118
GimpViewableClass *viewable_class = GIMP_VIEWABLE_CLASS (klass);
119
GimpBrushClass *brush_class = GIMP_BRUSH_CLASS (klass);
121
parent_class = g_type_class_peek_parent (klass);
123
object_class->finalize = gimp_brush_pipe_finalize;
125
gimp_object_class->get_memsize = gimp_brush_pipe_get_memsize;
127
viewable_class->get_popup_size = gimp_brush_pipe_get_popup_size;
129
brush_class->select_brush = gimp_brush_pipe_select_brush;
130
brush_class->want_null_motion = gimp_brush_pipe_want_null_motion;
134
gimp_brush_pipe_init (GimpBrushPipe *pipe)
136
pipe->current = NULL;
141
pipe->brushes = NULL;
147
gimp_brush_pipe_finalize (GObject *object)
149
GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (object);
158
g_free (pipe->stride);
166
for (i = 0; i < pipe->nbrushes; i++)
167
if (pipe->brushes[i])
168
g_object_unref (pipe->brushes[i]);
170
g_free (pipe->brushes);
171
pipe->brushes = NULL;
176
g_free (pipe->select);
181
g_free (pipe->index);
185
GIMP_BRUSH (pipe)->mask = NULL;
186
GIMP_BRUSH (pipe)->pixmap = NULL;
188
G_OBJECT_CLASS (parent_class)->finalize (object);
192
gimp_brush_pipe_get_memsize (GimpObject *object,
195
GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (object);
199
memsize += pipe->dimension * (sizeof (gint) /* rank */ +
200
sizeof (gint) /* stride */ +
201
sizeof (PipeSelectModes));
203
for (i = 0; i < pipe->nbrushes; i++)
204
memsize += gimp_object_get_memsize (GIMP_OBJECT (pipe->brushes[i]),
207
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
212
gimp_brush_pipe_get_popup_size (GimpViewable *viewable,
215
gboolean dot_for_dot,
219
GimpBrush *brush = GIMP_BRUSH (viewable);
221
*popup_width = brush->mask->width;
222
*popup_height = brush->mask->height;
228
gimp_brush_pipe_select_brush (GimpBrush *brush,
229
GimpCoords *last_coords,
230
GimpCoords *cur_coords)
232
GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush);
236
if (pipe->nbrushes == 1)
237
return GIMP_BRUSH (pipe->current);
240
for (i = 0; i < pipe->dimension; i++)
242
switch (pipe->select[i])
244
case PIPE_SELECT_INCREMENTAL:
245
ix = (pipe->index[i] + 1) % pipe->rank[i];
248
case PIPE_SELECT_ANGULAR:
249
angle = atan2 (cur_coords->y - last_coords->y,
250
cur_coords->x - last_coords->x);
251
/* Offset angle to be compatible with PSP tubes */
253
/* Map it to the [0..2*G_PI) interval */
256
else if (angle > 2.0 * G_PI)
258
ix = RINT (angle / (2.0 * G_PI) * pipe->rank[i]);
261
case PIPE_SELECT_RANDOM:
262
/* This probably isn't the right way */
263
ix = g_random_int_range (0, pipe->rank[i]);
266
case PIPE_SELECT_PRESSURE:
267
ix = RINT (cur_coords->pressure * (pipe->rank[i] - 1));
270
case PIPE_SELECT_TILT_X:
271
ix = RINT (cur_coords->xtilt / 2.0 * pipe->rank[i]) + pipe->rank[i] / 2;
274
case PIPE_SELECT_TILT_Y:
275
ix = RINT (cur_coords->ytilt / 2.0 * pipe->rank[i]) + pipe->rank[i] / 2;
278
case PIPE_SELECT_CONSTANT:
284
pipe->index[i] = CLAMP (ix, 0, pipe->rank[i] - 1);
285
brushix += pipe->stride[i] * pipe->index[i];
288
/* Make sure is inside bounds */
289
brushix = CLAMP (brushix, 0, pipe->nbrushes - 1);
291
pipe->current = pipe->brushes[brushix];
293
return GIMP_BRUSH (pipe->current);
297
gimp_brush_pipe_want_null_motion (GimpBrush *brush,
298
GimpCoords *last_coords,
299
GimpCoords *cur_coords)
301
GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush);
304
if (pipe->nbrushes == 1)
307
for (i = 0; i < pipe->dimension; i++)
308
if (pipe->select[i] == PIPE_SELECT_ANGULAR)
315
gimp_brush_pipe_load (const gchar *filename,
316
gboolean stingy_memory_use,
319
GimpBrushPipe *pipe = NULL;
320
GimpPixPipeParams params;
322
gint num_of_brushes = 0;
329
g_return_val_if_fail (filename != NULL, NULL);
330
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
331
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
333
fd = open (filename, O_RDONLY | _O_BINARY);
337
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
338
_("Could not open '%s' for reading: %s"),
339
gimp_filename_to_utf8 (filename), g_strerror (errno));
343
/* The file format starts with a painfully simple text header */
346
buffer = g_string_new (NULL);
347
while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
348
g_string_append_c (buffer, c);
350
if (buffer->len > 0 && buffer->len < 1024)
353
gimp_any_to_utf8 (buffer->str, buffer->len,
354
_("Invalid UTF-8 string in brush file '%s'."),
355
gimp_filename_to_utf8 (filename));
357
pipe = g_object_new (GIMP_TYPE_BRUSH_PIPE,
364
g_string_free (buffer, TRUE);
368
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
369
_("Fatal parse error in brush file '%s': "
371
gimp_filename_to_utf8 (filename));
376
/* get the number of brushes */
377
buffer = g_string_new (NULL);
378
while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
379
g_string_append_c (buffer, c);
381
if (buffer->len > 0 && buffer->len < 1024)
383
num_of_brushes = strtol (buffer->str, ¶mstring, 10);
386
if (num_of_brushes < 1)
388
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
389
_("Fatal parse error in brush file '%s': "
391
gimp_filename_to_utf8 (filename));
393
g_object_unref (pipe);
394
g_string_free (buffer, TRUE);
398
while (*paramstring && g_ascii_isspace (*paramstring))
403
gimp_pixpipe_params_init (¶ms);
404
gimp_pixpipe_params_parse (paramstring, ¶ms);
406
pipe->dimension = params.dim;
407
pipe->rank = g_new0 (gint, pipe->dimension);
408
pipe->select = g_new0 (PipeSelectModes, pipe->dimension);
409
pipe->index = g_new0 (gint, pipe->dimension);
411
/* placement is not used at all ?? */
412
if (params.free_placement_string)
413
g_free (params.placement);
415
for (i = 0; i < pipe->dimension; i++)
417
pipe->rank[i] = MAX (1, params.rank[i]);
418
if (strcmp (params.selection[i], "incremental") == 0)
419
pipe->select[i] = PIPE_SELECT_INCREMENTAL;
420
else if (strcmp (params.selection[i], "angular") == 0)
421
pipe->select[i] = PIPE_SELECT_ANGULAR;
422
else if (strcmp (params.selection[i], "velocity") == 0)
423
pipe->select[i] = PIPE_SELECT_VELOCITY;
424
else if (strcmp (params.selection[i], "random") == 0)
425
pipe->select[i] = PIPE_SELECT_RANDOM;
426
else if (strcmp (params.selection[i], "pressure") == 0)
427
pipe->select[i] = PIPE_SELECT_PRESSURE;
428
else if (strcmp (params.selection[i], "xtilt") == 0)
429
pipe->select[i] = PIPE_SELECT_TILT_X;
430
else if (strcmp (params.selection[i], "ytilt") == 0)
431
pipe->select[i] = PIPE_SELECT_TILT_Y;
433
pipe->select[i] = PIPE_SELECT_CONSTANT;
434
if (params.free_selection_string)
435
g_free (params.selection[i]);
442
pipe->rank = g_new (gint, 1);
443
pipe->rank[0] = num_of_brushes;
444
pipe->select = g_new (PipeSelectModes, 1);
445
pipe->select[0] = PIPE_SELECT_INCREMENTAL;
446
pipe->index = g_new (gint, 1);
450
g_string_free (buffer, TRUE);
452
totalcells = 1; /* Not all necessarily present, maybe */
453
for (i = 0; i < pipe->dimension; i++)
454
totalcells *= pipe->rank[i];
455
pipe->stride = g_new0 (gint, pipe->dimension);
456
for (i = 0; i < pipe->dimension; i++)
459
pipe->stride[i] = totalcells / pipe->rank[i];
461
pipe->stride[i] = pipe->stride[i-1] / pipe->rank[i];
463
g_assert (pipe->stride[pipe->dimension-1] == 1);
465
pipe->brushes = g_new0 (GimpBrush *, num_of_brushes);
467
while (pipe->nbrushes < num_of_brushes)
469
pipe->brushes[pipe->nbrushes] = gimp_brush_load_brush (fd, filename, NULL);
471
if (pipe->brushes[pipe->nbrushes])
473
gimp_object_set_name (GIMP_OBJECT (pipe->brushes[pipe->nbrushes]),
478
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
479
_("Fatal parse error in brush file '%s': "
481
gimp_filename_to_utf8 (filename));
483
g_object_unref (pipe);
492
/* Current brush is the first one. */
493
pipe->current = pipe->brushes[0];
495
/* just to satisfy the code that relies on this crap */
496
GIMP_BRUSH (pipe)->spacing = pipe->current->spacing;
497
GIMP_BRUSH (pipe)->x_axis = pipe->current->x_axis;
498
GIMP_BRUSH (pipe)->y_axis = pipe->current->y_axis;
499
GIMP_BRUSH (pipe)->mask = pipe->current->mask;
500
GIMP_BRUSH (pipe)->pixmap = pipe->current->pixmap;
502
return g_list_prepend (NULL, pipe);