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

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

#ifndef _O_BINARY
#define _O_BINARY 0
#endif

37
#include <glib-object.h>
Sven Neumann's avatar
Sven Neumann committed
38

39 40 41 42
#ifdef G_OS_WIN32
#include <io.h>
#endif

43
#include "libgimpmath/gimpmath.h"
44
#include "libgimpbase/gimpparasiteio.h"
45

Michael Natterer's avatar
Michael Natterer committed
46
#include "core-types.h"
47

48
#include "gimpbrush.h"
49
#include "gimpbrush-header.h"
50
#include "gimpbrushpipe.h"
51
#include "gimppattern-header.h"
52

53 54
#include "libgimp/gimpintl.h"

55

56 57 58
static void        gimp_brush_pipe_class_init       (GimpBrushPipeClass *klass);
static void        gimp_brush_pipe_init             (GimpBrushPipe      *pipe);

Michael Natterer's avatar
Michael Natterer committed
59
static void        gimp_brush_pipe_finalize         (GObject    *object);
60

61 62
static gsize       gimp_brush_pipe_get_memsize      (GimpObject *object);

Michael Natterer's avatar
Michael Natterer committed
63 64 65 66 67 68
static GimpBrush * gimp_brush_pipe_select_brush     (GimpBrush  *brush,
                                                     GimpCoords *last_coords,
                                                     GimpCoords *cur_coords);
static gboolean    gimp_brush_pipe_want_null_motion (GimpBrush  *brush,
                                                     GimpCoords *last_coords,
                                                     GimpCoords *cur_coords);
69

70

71 72 73
static GimpBrushClass *parent_class = NULL;


74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
GType
gimp_brush_pipe_get_type (void)
{
  static GType brush_type = 0;

  if (! brush_type)
    {
      static const GTypeInfo brush_info =
      {
        sizeof (GimpBrushPipeClass),
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_brush_pipe_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data     */
	sizeof (GimpBrushPipe),
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_brush_pipe_init,
      };

      brush_type = g_type_register_static (GIMP_TYPE_BRUSH,
					   "GimpBrushPipe", 
					   &brush_info, 0);
    }

  return brush_type;
}

static void
gimp_brush_pipe_class_init (GimpBrushPipeClass *klass)
{
105 106 107
  GObjectClass    *object_class;
  GimpObjectClass *gimp_object_class;
  GimpBrushClass  *brush_class;
108

109 110 111
  object_class      = G_OBJECT_CLASS (klass);
  gimp_object_class = GIMP_OBJECT_CLASS (klass);
  brush_class       = GIMP_BRUSH_CLASS (klass);
112 113 114

  parent_class = g_type_class_peek_parent (klass);

115 116 117
  object_class->finalize         = gimp_brush_pipe_finalize;

  gimp_object_class->get_memsize = gimp_brush_pipe_get_memsize;
118

119 120
  brush_class->select_brush      = gimp_brush_pipe_select_brush;
  brush_class->want_null_motion  = gimp_brush_pipe_want_null_motion;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
}

static void
gimp_brush_pipe_init (GimpBrushPipe *pipe)
{
  pipe->current   = NULL;
  pipe->dimension = 0;
  pipe->rank      = NULL;
  pipe->stride    = NULL;
  pipe->nbrushes  = 0;
  pipe->brushes   = NULL;
  pipe->select    = NULL;
  pipe->index     = NULL;
}

static void
gimp_brush_pipe_finalize (GObject *object)
{
  GimpBrushPipe *pipe;

  g_return_if_fail (GIMP_IS_BRUSH_PIPE (object));

  pipe = GIMP_BRUSH_PIPE (object);

  if (pipe->rank)
    {
      g_free (pipe->rank);
      pipe->rank = NULL;
    }
  if (pipe->stride)
    {
      g_free (pipe->stride);
      pipe->stride = NULL;
    }

  if (pipe->brushes)
    {
      gint i;

      for (i = 0; i < pipe->nbrushes; i++)
	if (pipe->brushes[i])
	  g_object_unref (G_OBJECT (pipe->brushes[i]));

      g_free (pipe->brushes);
      pipe->brushes = NULL;
    }

  if (pipe->select)
    {
      g_free (pipe->select);
      pipe->select = NULL;
    }
  if (pipe->index)
    {
      g_free (pipe->index);
      pipe->index = NULL;
    }

  GIMP_BRUSH (pipe)->mask   = NULL;
  GIMP_BRUSH (pipe)->pixmap = NULL;

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
static gsize
gimp_brush_pipe_get_memsize (GimpObject *object)
{
  GimpBrushPipe *pipe;
  gsize          memsize = 0;
  gint           i;

  pipe = GIMP_BRUSH_PIPE (object);

  memsize += pipe->dimension * (sizeof (gint) /* rank   */ +
                                sizeof (gint) /* stride */ +
                                sizeof (PipeSelectModes));

  for (i = 0; i < pipe->nbrushes; i++)
    {
      memsize += gimp_object_get_memsize (GIMP_OBJECT (pipe->brushes[i]));
    }

  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object);
}

206
static GimpBrush *
Michael Natterer's avatar
Michael Natterer committed
207 208 209
gimp_brush_pipe_select_brush (GimpBrush  *brush,
                              GimpCoords *last_coords,
                              GimpCoords *cur_coords)
210
{
211
  GimpBrushPipe *pipe;
212 213
  gint           i, brushix, ix;
  gdouble        angle;
214

Michael Natterer's avatar
Michael Natterer committed
215
  pipe = GIMP_BRUSH_PIPE (brush);
216

217 218
  if (pipe->nbrushes == 1)
    return GIMP_BRUSH (pipe->current);
219

220 221 222 223 224 225
  brushix = 0;
  for (i = 0; i < pipe->dimension; i++)
    {
      switch (pipe->select[i])
	{
	case PIPE_SELECT_INCREMENTAL:
226
	  ix = (pipe->index[i] + 1) % pipe->rank[i];
227 228
	  break;
	case PIPE_SELECT_ANGULAR:
Michael Natterer's avatar
Michael Natterer committed
229 230
	  angle = atan2 (cur_coords->y - last_coords->y,
			 cur_coords->x - last_coords->x);
231 232 233
	  /* Offset angle to be compatible with PSP tubes */
	  angle += G_PI_2;
	  /* Map it to the [0..2*G_PI) interval */
234
	  if (angle < 0)
235 236 237 238
	    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]);
239 240 241
	  break;
	case PIPE_SELECT_RANDOM:
	  /* This probably isn't the right way */
242
	  ix = rand () % pipe->rank[i];
243 244
	  break;
	case PIPE_SELECT_PRESSURE:
Michael Natterer's avatar
Michael Natterer committed
245
	  ix = RINT (cur_coords->pressure * (pipe->rank[i] - 1));
246 247
	  break;
	case PIPE_SELECT_TILT_X:
Michael Natterer's avatar
Michael Natterer committed
248
	  ix = RINT (cur_coords->xtilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
249 250
	  break;
	case PIPE_SELECT_TILT_Y:
Michael Natterer's avatar
Michael Natterer committed
251
	  ix = RINT (cur_coords->ytilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
252
	  break;
Adam D. Moss's avatar
Adam D. Moss committed
253 254 255 256
	case PIPE_SELECT_CONSTANT:
	default:
	  ix = pipe->index[i];
	  break;
257
	}
258
      pipe->index[i] = CLAMP (ix, 0, pipe->rank[i]-1);
259
      brushix += pipe->stride[i] * pipe->index[i];
260 261
    }

262
  /* Make sure is inside bounds */
263
  brushix = CLAMP (brushix, 0, pipe->nbrushes-1);
264

265 266 267
  pipe->current = pipe->brushes[brushix];

  return GIMP_BRUSH (pipe->current);
268
}
269

270
static gboolean
Michael Natterer's avatar
Michael Natterer committed
271 272 273
gimp_brush_pipe_want_null_motion (GimpBrush  *brush,
                                  GimpCoords *last_coords,
                                  GimpCoords *cur_coords)
274 275
{
  GimpBrushPipe *pipe;
276
  gint           i;
277

Michael Natterer's avatar
Michael Natterer committed
278
  pipe = GIMP_BRUSH_PIPE (brush);
279 280 281 282 283 284 285 286 287 288 289

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

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

  return TRUE;
}

290
GimpData *
291
gimp_brush_pipe_load (const gchar *filename)
292
{
293
  GimpBrushPipe     *pipe = NULL;
294
  GimpPixPipeParams  params;
295 296 297 298 299 300 301
  gint               i;
  gint               num_of_brushes = 0;
  gint               totalcells;
  gchar             *paramstring;
  GString           *buffer;
  gchar              c;
  gint               fd;
302 303 304 305 306

  g_return_val_if_fail (filename != NULL, NULL);

  fd = open (filename, O_RDONLY | _O_BINARY);
  if (fd == -1)
307
    {
308
      g_message (_("Could not open file '%s'"), filename);
309 310
      return NULL;
    }
311

312
  /* The file format starts with a painfully simple text header */
313

314 315 316 317 318 319
  /*  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)
320
    {
Michael Natterer's avatar
Michael Natterer committed
321
      pipe = GIMP_BRUSH_PIPE (g_object_new (GIMP_TYPE_BRUSH_PIPE, NULL));
322 323 324 325 326 327 328

      if (g_utf8_validate (buffer->str, buffer->len, NULL))
        {
          gimp_object_set_name (GIMP_OBJECT (pipe), buffer->str);
        }
      else
        {
329
          g_message (_("Invalid UTF-8 string in brush file '%s'."), 
330 331 332
                     filename);
          gimp_object_set_name (GIMP_OBJECT (pipe), _("Unnamed"));
        }
333
    }
334
  g_string_free (buffer, TRUE);
335 336 337

  if (!pipe)
    {
338
      g_message (_("Fatal parsing error:\nBrush pipe file '%s' is corrupt."), 
339 340
		 filename);
      close (fd);
341 342
      return NULL;
    }
343 344 345 346 347 348 349 350 351 352 353

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

354 355
  if (num_of_brushes < 1)
    {
356
      g_message (_("Fatal parsing error:\nBrush pipe file '%s' is corrupt."), 
357 358
		 filename);
      close (fd);
359
      g_object_unref (G_OBJECT (pipe));
360
      g_string_free (buffer, TRUE);
361 362
      return NULL;
    }
363

364
  while (*paramstring && g_ascii_isspace (*paramstring))
365
    paramstring++;
366

367
  if (*paramstring)
368
    {
369 370
      gimp_pixpipe_params_init (&params);
      gimp_pixpipe_params_parse (paramstring, &params);
371

372
      pipe->dimension = params.dim;
373 374 375 376
      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
377 378 379
      /* placement is not used at all ?? */
      if (params.free_placement_string)
	g_free (params.placement);
380

381 382
      for (i = 0; i < pipe->dimension; i++)
	{
383 384
	  pipe->rank[i] = params.rank[i];
	  if (strcmp (params.selection[i], "incremental") == 0)
385
	    pipe->select[i] = PIPE_SELECT_INCREMENTAL;
386
	  else if (strcmp (params.selection[i], "angular") == 0)
387
	    pipe->select[i] = PIPE_SELECT_ANGULAR;
388
	  else if (strcmp (params.selection[i], "velocity") == 0)
389
	    pipe->select[i] = PIPE_SELECT_VELOCITY;
390
	  else if (strcmp (params.selection[i], "random") == 0)
391
	    pipe->select[i] = PIPE_SELECT_RANDOM;
392
	  else if (strcmp (params.selection[i], "pressure") == 0)
393
	    pipe->select[i] = PIPE_SELECT_PRESSURE;
394
	  else if (strcmp (params.selection[i], "xtilt") == 0)
395
	    pipe->select[i] = PIPE_SELECT_TILT_X;
396
	  else if (strcmp (params.selection[i], "ytilt") == 0)
397 398 399
	    pipe->select[i] = PIPE_SELECT_TILT_Y;
	  else
	    pipe->select[i] = PIPE_SELECT_CONSTANT;
Sven Neumann's avatar
Sven Neumann committed
400 401
	  if (params.free_selection_string)
	    g_free (params.selection[i]);
402 403 404 405 406 407
	  pipe->index[i] = 0;
	}
    }
  else
    {
      pipe->dimension = 1;
Michael Natterer's avatar
Michael Natterer committed
408 409 410
      pipe->rank      = g_new (gint, 1);
      pipe->rank[0]   = num_of_brushes;
      pipe->select    = g_new (PipeSelectModes, 1);
411
      pipe->select[0] = PIPE_SELECT_INCREMENTAL;
Michael Natterer's avatar
Michael Natterer committed
412 413
      pipe->index     = g_new (gint, 1);
      pipe->index[0]  = 0;
414 415
    }

416 417
  g_string_free (buffer, TRUE);

418 419 420
  totalcells = 1;		/* Not all necessarily present, maybe */
  for (i = 0; i < pipe->dimension; i++)
    totalcells *= pipe->rank[i];
421
  pipe->stride = g_new0 (gint, pipe->dimension);
422 423 424 425 426 427 428 429
  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);
430

431
  pipe->brushes = g_new0 (GimpBrush *, num_of_brushes);
432 433 434

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

437 438
      if (pipe->brushes[pipe->nbrushes])
	{
439 440
	  gimp_object_set_name (GIMP_OBJECT (pipe->brushes[pipe->nbrushes]),
				NULL);
441
	}
442
      else
443
	{
444 445
	  g_message (_("Fatal parsing error:\n"
                       "Brush pipe file '%s' is corrupt."), filename);
446
	  close (fd);
447
	  g_object_unref (G_OBJECT (pipe));
448 449
	  return NULL;
	}
450
  
451
      pipe->nbrushes++;
452
    }
453

454 455
  /* Current brush is the first one. */
  pipe->current = pipe->brushes[0];
456

457 458
  gimp_data_set_filename (GIMP_DATA (pipe), filename);

459
  /*  just to satisfy the code that relies on this crap  */
460 461 462 463 464
  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;
465

466
  close (fd);
467

468
  return GIMP_DATA (pipe);
469
}