~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/core/gimpbrushpipe.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
 
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
#include <errno.h>
 
25
 
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
#ifdef HAVE_UNISTD_H
 
29
#include <unistd.h>
 
30
#endif
 
31
#include <fcntl.h>
 
32
 
 
33
#ifndef _O_BINARY
 
34
#define _O_BINARY 0
 
35
#endif
 
36
 
 
37
#include <glib-object.h>
 
38
 
 
39
#ifdef G_OS_WIN32
 
40
#include <io.h>
 
41
#endif
 
42
 
 
43
#include "libgimpbase/gimpbase.h"
 
44
#include "libgimpbase/gimpparasiteio.h"
 
45
#include "libgimpmath/gimpmath.h"
 
46
 
 
47
#include "core-types.h"
 
48
 
 
49
#include "base/temp-buf.h"
 
50
 
 
51
#include "gimpbrush.h"
 
52
#include "gimpbrush-header.h"
 
53
#include "gimpbrushpipe.h"
 
54
#include "gimppattern-header.h"
 
55
 
 
56
#include "gimp-intl.h"
 
57
 
 
58
 
 
59
static void        gimp_brush_pipe_class_init       (GimpBrushPipeClass *klass);
 
60
static void        gimp_brush_pipe_init             (GimpBrushPipe      *pipe);
 
61
 
 
62
static void        gimp_brush_pipe_finalize         (GObject      *object);
 
63
 
 
64
static gint64      gimp_brush_pipe_get_memsize      (GimpObject   *object,
 
65
                                                     gint64       *gui_size);
 
66
 
 
67
static gboolean    gimp_brush_pipe_get_popup_size   (GimpViewable *viewable,
 
68
                                                     gint          width,
 
69
                                                     gint          height,
 
70
                                                     gboolean      dot_for_dot,
 
71
                                                     gint         *popup_width,
 
72
                                                     gint         *popup_height);
 
73
 
 
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);
 
80
 
 
81
 
 
82
static GimpBrushClass *parent_class = NULL;
 
83
 
 
84
 
 
85
GType
 
86
gimp_brush_pipe_get_type (void)
 
87
{
 
88
  static GType brush_type = 0;
 
89
 
 
90
  if (! brush_type)
 
91
    {
 
92
      static const GTypeInfo brush_info =
 
93
      {
 
94
        sizeof (GimpBrushPipeClass),
 
95
        (GBaseInitFunc) NULL,
 
96
        (GBaseFinalizeFunc) NULL,
 
97
        (GClassInitFunc) gimp_brush_pipe_class_init,
 
98
        NULL,           /* class_finalize */
 
99
        NULL,           /* class_data     */
 
100
        sizeof (GimpBrushPipe),
 
101
        0,              /* n_preallocs    */
 
102
        (GInstanceInitFunc) gimp_brush_pipe_init,
 
103
      };
 
104
 
 
105
      brush_type = g_type_register_static (GIMP_TYPE_BRUSH,
 
106
                                           "GimpBrushPipe",
 
107
                                           &brush_info, 0);
 
108
    }
 
109
 
 
110
  return brush_type;
 
111
}
 
112
 
 
113
static void
 
114
gimp_brush_pipe_class_init (GimpBrushPipeClass *klass)
 
115
{
 
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);
 
120
 
 
121
  parent_class = g_type_class_peek_parent (klass);
 
122
 
 
123
  object_class->finalize         = gimp_brush_pipe_finalize;
 
124
 
 
125
  gimp_object_class->get_memsize = gimp_brush_pipe_get_memsize;
 
126
 
 
127
  viewable_class->get_popup_size = gimp_brush_pipe_get_popup_size;
 
128
 
 
129
  brush_class->select_brush      = gimp_brush_pipe_select_brush;
 
130
  brush_class->want_null_motion  = gimp_brush_pipe_want_null_motion;
 
131
}
 
132
 
 
133
static void
 
134
gimp_brush_pipe_init (GimpBrushPipe *pipe)
 
135
{
 
136
  pipe->current   = NULL;
 
137
  pipe->dimension = 0;
 
138
  pipe->rank      = NULL;
 
139
  pipe->stride    = NULL;
 
140
  pipe->nbrushes  = 0;
 
141
  pipe->brushes   = NULL;
 
142
  pipe->select    = NULL;
 
143
  pipe->index     = NULL;
 
144
}
 
145
 
 
146
static void
 
147
gimp_brush_pipe_finalize (GObject *object)
 
148
{
 
149
  GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (object);
 
150
 
 
151
  if (pipe->rank)
 
152
    {
 
153
      g_free (pipe->rank);
 
154
      pipe->rank = NULL;
 
155
    }
 
156
  if (pipe->stride)
 
157
    {
 
158
      g_free (pipe->stride);
 
159
      pipe->stride = NULL;
 
160
    }
 
161
 
 
162
  if (pipe->brushes)
 
163
    {
 
164
      gint i;
 
165
 
 
166
      for (i = 0; i < pipe->nbrushes; i++)
 
167
        if (pipe->brushes[i])
 
168
          g_object_unref (pipe->brushes[i]);
 
169
 
 
170
      g_free (pipe->brushes);
 
171
      pipe->brushes = NULL;
 
172
    }
 
173
 
 
174
  if (pipe->select)
 
175
    {
 
176
      g_free (pipe->select);
 
177
      pipe->select = NULL;
 
178
    }
 
179
  if (pipe->index)
 
180
    {
 
181
      g_free (pipe->index);
 
182
      pipe->index = NULL;
 
183
    }
 
184
 
 
185
  GIMP_BRUSH (pipe)->mask   = NULL;
 
186
  GIMP_BRUSH (pipe)->pixmap = NULL;
 
187
 
 
188
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
189
}
 
190
 
 
191
static gint64
 
192
gimp_brush_pipe_get_memsize (GimpObject *object,
 
193
                             gint64     *gui_size)
 
194
{
 
195
  GimpBrushPipe *pipe    = GIMP_BRUSH_PIPE (object);
 
196
  gint64         memsize = 0;
 
197
  gint           i;
 
198
 
 
199
  memsize += pipe->dimension * (sizeof (gint) /* rank   */ +
 
200
                                sizeof (gint) /* stride */ +
 
201
                                sizeof (PipeSelectModes));
 
202
 
 
203
  for (i = 0; i < pipe->nbrushes; i++)
 
204
    memsize += gimp_object_get_memsize (GIMP_OBJECT (pipe->brushes[i]),
 
205
                                        gui_size);
 
206
 
 
207
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
 
208
                                                                  gui_size);
 
209
}
 
210
 
 
211
static gboolean
 
212
gimp_brush_pipe_get_popup_size (GimpViewable   *viewable,
 
213
                                gint            width,
 
214
                                gint            height,
 
215
                                gboolean        dot_for_dot,
 
216
                                gint           *popup_width,
 
217
                                gint           *popup_height)
 
218
{
 
219
  GimpBrush *brush = GIMP_BRUSH (viewable);
 
220
 
 
221
  *popup_width  = brush->mask->width;
 
222
  *popup_height = brush->mask->height;
 
223
 
 
224
  return TRUE;
 
225
}
 
226
 
 
227
static GimpBrush *
 
228
gimp_brush_pipe_select_brush (GimpBrush  *brush,
 
229
                              GimpCoords *last_coords,
 
230
                              GimpCoords *cur_coords)
 
231
{
 
232
  GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush);
 
233
  gint           i, brushix, ix;
 
234
  gdouble        angle;
 
235
 
 
236
  if (pipe->nbrushes == 1)
 
237
    return GIMP_BRUSH (pipe->current);
 
238
 
 
239
  brushix = 0;
 
240
  for (i = 0; i < pipe->dimension; i++)
 
241
    {
 
242
      switch (pipe->select[i])
 
243
        {
 
244
        case PIPE_SELECT_INCREMENTAL:
 
245
          ix = (pipe->index[i] + 1) % pipe->rank[i];
 
246
          break;
 
247
 
 
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 */
 
252
          angle += G_PI_2;
 
253
          /* Map it to the [0..2*G_PI) interval */
 
254
          if (angle < 0)
 
255
            angle += 2.0 * G_PI;
 
256
          else if (angle > 2.0 * G_PI)
 
257
            angle -= 2.0 * G_PI;
 
258
          ix = RINT (angle / (2.0 * G_PI) * pipe->rank[i]);
 
259
          break;
 
260
 
 
261
        case PIPE_SELECT_RANDOM:
 
262
          /* This probably isn't the right way */
 
263
          ix = g_random_int_range (0, pipe->rank[i]);
 
264
          break;
 
265
 
 
266
        case PIPE_SELECT_PRESSURE:
 
267
          ix = RINT (cur_coords->pressure * (pipe->rank[i] - 1));
 
268
          break;
 
269
 
 
270
        case PIPE_SELECT_TILT_X:
 
271
          ix = RINT (cur_coords->xtilt / 2.0 * pipe->rank[i]) + pipe->rank[i] / 2;
 
272
          break;
 
273
 
 
274
        case PIPE_SELECT_TILT_Y:
 
275
          ix = RINT (cur_coords->ytilt / 2.0 * pipe->rank[i]) + pipe->rank[i] / 2;
 
276
          break;
 
277
 
 
278
        case PIPE_SELECT_CONSTANT:
 
279
        default:
 
280
          ix = pipe->index[i];
 
281
          break;
 
282
        }
 
283
 
 
284
      pipe->index[i] = CLAMP (ix, 0, pipe->rank[i] - 1);
 
285
      brushix += pipe->stride[i] * pipe->index[i];
 
286
    }
 
287
 
 
288
  /* Make sure is inside bounds */
 
289
  brushix = CLAMP (brushix, 0, pipe->nbrushes - 1);
 
290
 
 
291
  pipe->current = pipe->brushes[brushix];
 
292
 
 
293
  return GIMP_BRUSH (pipe->current);
 
294
}
 
295
 
 
296
static gboolean
 
297
gimp_brush_pipe_want_null_motion (GimpBrush  *brush,
 
298
                                  GimpCoords *last_coords,
 
299
                                  GimpCoords *cur_coords)
 
300
{
 
301
  GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush);
 
302
  gint           i;
 
303
 
 
304
  if (pipe->nbrushes == 1)
 
305
    return TRUE;
 
306
 
 
307
  for (i = 0; i < pipe->dimension; i++)
 
308
    if (pipe->select[i] == PIPE_SELECT_ANGULAR)
 
309
      return FALSE;
 
310
 
 
311
  return TRUE;
 
312
}
 
313
 
 
314
GList *
 
315
gimp_brush_pipe_load (const gchar  *filename,
 
316
                      gboolean      stingy_memory_use,
 
317
                      GError      **error)
 
318
{
 
319
  GimpBrushPipe     *pipe = NULL;
 
320
  GimpPixPipeParams  params;
 
321
  gint               i;
 
322
  gint               num_of_brushes = 0;
 
323
  gint               totalcells;
 
324
  gchar             *paramstring;
 
325
  GString           *buffer;
 
326
  gchar              c;
 
327
  gint               fd;
 
328
 
 
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);
 
332
 
 
333
  fd = open (filename, O_RDONLY | _O_BINARY);
 
334
 
 
335
  if (fd == -1)
 
336
    {
 
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));
 
340
      return NULL;
 
341
    }
 
342
 
 
343
  /* The file format starts with a painfully simple text header */
 
344
 
 
345
  /*  get the name  */
 
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);
 
349
 
 
350
  if (buffer->len > 0 && buffer->len < 1024)
 
351
    {
 
352
      gchar *utf8 =
 
353
        gimp_any_to_utf8 (buffer->str, buffer->len,
 
354
                          _("Invalid UTF-8 string in brush file '%s'."),
 
355
                          gimp_filename_to_utf8 (filename));
 
356
 
 
357
      pipe = g_object_new (GIMP_TYPE_BRUSH_PIPE,
 
358
                           "name", utf8,
 
359
                           NULL);
 
360
 
 
361
      g_free (utf8);
 
362
    }
 
363
 
 
364
  g_string_free (buffer, TRUE);
 
365
 
 
366
  if (! pipe)
 
367
    {
 
368
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
 
369
                   _("Fatal parse error in brush file '%s': "
 
370
                     "File is corrupt."),
 
371
                   gimp_filename_to_utf8 (filename));
 
372
      close (fd);
 
373
      return NULL;
 
374
    }
 
375
 
 
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);
 
380
 
 
381
  if (buffer->len > 0 && buffer->len < 1024)
 
382
    {
 
383
      num_of_brushes = strtol (buffer->str, &paramstring, 10);
 
384
    }
 
385
 
 
386
  if (num_of_brushes < 1)
 
387
    {
 
388
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
 
389
                   _("Fatal parse error in brush file '%s': "
 
390
                     "File is corrupt."),
 
391
                   gimp_filename_to_utf8 (filename));
 
392
      close (fd);
 
393
      g_object_unref (pipe);
 
394
      g_string_free (buffer, TRUE);
 
395
      return NULL;
 
396
    }
 
397
 
 
398
  while (*paramstring && g_ascii_isspace (*paramstring))
 
399
    paramstring++;
 
400
 
 
401
  if (*paramstring)
 
402
    {
 
403
      gimp_pixpipe_params_init (&params);
 
404
      gimp_pixpipe_params_parse (paramstring, &params);
 
405
 
 
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);
 
410
 
 
411
      /* placement is not used at all ?? */
 
412
      if (params.free_placement_string)
 
413
        g_free (params.placement);
 
414
 
 
415
      for (i = 0; i < pipe->dimension; i++)
 
416
        {
 
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;
 
432
          else
 
433
            pipe->select[i] = PIPE_SELECT_CONSTANT;
 
434
          if (params.free_selection_string)
 
435
            g_free (params.selection[i]);
 
436
          pipe->index[i] = 0;
 
437
        }
 
438
    }
 
439
  else
 
440
    {
 
441
      pipe->dimension = 1;
 
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);
 
447
      pipe->index[0]  = 0;
 
448
    }
 
449
 
 
450
  g_string_free (buffer, TRUE);
 
451
 
 
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++)
 
457
    {
 
458
      if (i == 0)
 
459
        pipe->stride[i] = totalcells / pipe->rank[i];
 
460
      else
 
461
        pipe->stride[i] = pipe->stride[i-1] / pipe->rank[i];
 
462
    }
 
463
  g_assert (pipe->stride[pipe->dimension-1] == 1);
 
464
 
 
465
  pipe->brushes = g_new0 (GimpBrush *, num_of_brushes);
 
466
 
 
467
  while (pipe->nbrushes < num_of_brushes)
 
468
    {
 
469
      pipe->brushes[pipe->nbrushes] = gimp_brush_load_brush (fd, filename, NULL);
 
470
 
 
471
      if (pipe->brushes[pipe->nbrushes])
 
472
        {
 
473
          gimp_object_set_name (GIMP_OBJECT (pipe->brushes[pipe->nbrushes]),
 
474
                                NULL);
 
475
        }
 
476
      else
 
477
        {
 
478
          g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
 
479
                       _("Fatal parse error in brush file '%s': "
 
480
                         "File is corrupt."),
 
481
                       gimp_filename_to_utf8 (filename));
 
482
          close (fd);
 
483
          g_object_unref (pipe);
 
484
          return NULL;
 
485
        }
 
486
 
 
487
      pipe->nbrushes++;
 
488
    }
 
489
 
 
490
  close (fd);
 
491
 
 
492
  /* Current brush is the first one. */
 
493
  pipe->current = pipe->brushes[0];
 
494
 
 
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;
 
501
 
 
502
  return g_list_prepend (NULL, pipe);
 
503
}