gimpbrushgenerated-save.c 13.7 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
 *
 * gimp_brush_generated module Copyright 1998 Jay Cox <jaycox@earthlink.net>
 *
 * 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 23 24 25
#include "config.h"

#include <stdio.h>
#include <string.h>

26 27 28 29
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

30
#include <glib-object.h>
Sven Neumann's avatar
Sven Neumann committed
31

32 33
#include "libgimpmath/gimpmath.h"

Michael Natterer's avatar
Michael Natterer committed
34
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
35

Michael Natterer's avatar
Michael Natterer committed
36 37 38
#include "base/base-config.h"
#include "base/temp-buf.h"

39 40
#include "gimpbrushgenerated.h"

41

42 43
#define OVERSAMPLING 5

44

45
/*  local function prototypes  */
Michael Natterer's avatar
Michael Natterer committed
46 47 48
static void   gimp_brush_generated_class_init (GimpBrushGeneratedClass *klass);
static void   gimp_brush_generated_init       (GimpBrushGenerated      *brush);

49 50 51 52
static gboolean   gimp_brush_generated_save      (GimpData           *data);
static void       gimp_brush_generated_dirty     (GimpData           *data);
static gchar    * gimp_brush_generated_get_extension (GimpData       *data);
static GimpData * gimp_brush_generated_duplicate (GimpData           *data);
53

54

55
static GimpBrushClass *parent_class = NULL;
56

57

58
GType
59
gimp_brush_generated_get_type (void)
60
{
61
  static GType brush_type = 0;
62

63
  if (! brush_type)
64
    {
65
      static const GTypeInfo brush_info =
66
      {
67 68 69 70 71 72
        sizeof (GimpBrushGeneratedClass),
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_brush_generated_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data     */
73
	sizeof (GimpBrushGenerated),
74 75
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_brush_generated_init,
76 77
      };

78 79 80
      brush_type = g_type_register_static (GIMP_TYPE_BRUSH,
					   "GimpBrushGenerated", 
					   &brush_info, 0);
81 82
    }

83
  return brush_type;
84 85
}

86 87
static void
gimp_brush_generated_class_init (GimpBrushGeneratedClass *klass)
88
{
89
  GimpDataClass *data_class;
90

91
  data_class = GIMP_DATA_CLASS (klass);
92

93
  parent_class = g_type_class_peek_parent (klass);
94

95 96 97
  data_class->save          = gimp_brush_generated_save;
  data_class->dirty         = gimp_brush_generated_dirty;
  data_class->get_extension = gimp_brush_generated_get_extension;
98
  data_class->duplicate     = gimp_brush_generated_duplicate;
99 100
}

101 102
static void
gimp_brush_generated_init (GimpBrushGenerated *brush)
103
{
104 105 106 107 108
  brush->radius       = 5.0;
  brush->hardness     = 0.0;
  brush->angle        = 0.0;
  brush->aspect_ratio = 1.0;
  brush->freeze       = 0;
109 110
}

111 112 113 114 115
static gboolean
gimp_brush_generated_save (GimpData *data)
{
  GimpBrushGenerated *brush;
  FILE               *fp;
116
  gchar               buf[G_ASCII_DTOSTR_BUF_SIZE];
Michael Natterer's avatar
Michael Natterer committed
117

118 119
  g_return_val_if_fail (data != NULL, FALSE);
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (data), FALSE);
Michael Natterer's avatar
Michael Natterer committed
120

121
  brush = GIMP_BRUSH_GENERATED (data);
Michael Natterer's avatar
Michael Natterer committed
122 123

  /*  we are (finaly) ready to try to save the generated brush file  */
124
  if ((fp = fopen (data->filename, "wb")) == NULL)
125
    {
126 127
      g_warning ("Unable to save file %s", data->filename);
      return FALSE;
128 129 130 131 132 133 134 135 136
    }

  /* write magic header */
  fprintf (fp, "GIMP-VBR\n");

  /* write version */
  fprintf (fp, "1.0\n");

  /* write name */
137
  fprintf (fp, "%.255s\n", GIMP_OBJECT (brush)->name);
138 139

  /* write brush spacing */
140 141 142
  fprintf (fp, "%s\n", 
           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", 
                            GIMP_BRUSH (brush)->spacing));
143 144

  /* write brush radius */
145 146 147
  fprintf (fp, "%s\n", 
           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", 
                            brush->radius));
148 149

  /* write brush hardness */
150 151 152
  fprintf (fp, "%s\n",
           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", 
                            brush->hardness));
153 154

  /* write brush aspect_ratio */
155 156 157
  fprintf (fp, "%s\n",
           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", 
                            brush->aspect_ratio));
158 159

  /* write brush angle */
160 161 162
  fprintf (fp, "%s\n",
           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", 
                            brush->angle));
163 164 165

  fclose (fp);

166
  return TRUE;
167
}
168

169 170 171 172 173 174
static gchar *
gimp_brush_generated_get_extension (GimpData *data)
{
  return GIMP_BRUSH_GENERATED_FILE_EXTENSION;
}

175 176 177 178 179 180 181 182 183 184 185 186 187
static GimpData *
gimp_brush_generated_duplicate (GimpData *data)
{
  GimpBrushGenerated *brush;

  brush = GIMP_BRUSH_GENERATED (data);

  return gimp_brush_generated_new (brush->radius,
				   brush->hardness,
				   brush->angle,
				   brush->aspect_ratio);
}

188 189
static double
gauss (gdouble f)
190
{ 
191 192
  /* this aint' a real gauss function */
  if (f < -.5)
193
    {
194
      f = -1.0 - f;
195
      return (2.0 * f*f);
196
    }
197

198
  if (f < .5)
199
    return (1.0 - 2.0 * f*f);
200

201
  f = 1.0 -f;
202
  return (2.0 * f*f);
203
}
204

205 206
static void
gimp_brush_generated_dirty (GimpData *data)
207
{
208
  GimpBrushGenerated *brush;
209
  register GimpBrush *gbrush = NULL;
210 211 212 213 214 215 216 217
  register gint       x, y;
  register guchar    *centerp;
  register gdouble    d;
  register gdouble    exponent;
  register guchar     a;
  register gint       length;
  register guchar    *lookup;
  register gdouble    sum, c, s, tx, ty;
218
  gdouble buffer[OVERSAMPLING];
219
  gint    width, height;
220

221 222
  brush = GIMP_BRUSH_GENERATED (data);

223
  if (brush->freeze) /* if we are frozen defer rerendering till later */
224
    return;
225

226
  gbrush = GIMP_BRUSH (brush);
227

Michael Natterer's avatar
Michael Natterer committed
228
  if (base_config->stingy_memory_use && gbrush->mask)
229 230
    temp_buf_unswap (gbrush->mask);

231
  if (gbrush->mask)
232
    {
233
      temp_buf_free (gbrush->mask);
234
    }
235

236
  /* compute the range of the brush. should do a better job than this? */
237 238
  s = sin (gimp_deg_to_rad (brush->angle));
  c = cos (gimp_deg_to_rad (brush->angle));
239

240 241 242 243 244 245 246 247
  tx = MAX (fabs (c*ceil (brush->radius) - s*ceil (brush->radius)
		  / brush->aspect_ratio), 
	    fabs (c*ceil (brush->radius) + s*ceil (brush->radius)
		  / brush->aspect_ratio));
  ty = MAX (fabs (s*ceil (brush->radius) + c*ceil (brush->radius)
		  / brush->aspect_ratio),
	    fabs (s*ceil (brush->radius) - c*ceil (brush->radius)
		  / brush->aspect_ratio));
248

249
  if (brush->radius > tx)
250
    width = ceil (tx);
251
  else
252
    width = ceil (brush->radius);
253

254
  if (brush->radius > ty)
255
    height = ceil (ty);
256
  else
257
    height = ceil (brush->radius);
258

259
  /* compute the axis for spacing */
260 261
  GIMP_BRUSH (brush)->x_axis.x =        c * brush->radius;
  GIMP_BRUSH (brush)->x_axis.y = -1.0 * s * brush->radius;
262

263 264
  GIMP_BRUSH (brush)->y_axis.x = (s * brush->radius / brush->aspect_ratio);
  GIMP_BRUSH (brush)->y_axis.y = (c * brush->radius / brush->aspect_ratio);
265
  
266 267
  gbrush->mask = temp_buf_new (width * 2 + 1,
			       height * 2 + 1,
268
			       1, width, height, 0);
269 270
  centerp = &gbrush->mask->data[height * gbrush->mask->width + width];

271 272 273 274
  if ((1.0 - brush->hardness) < 0.000001)
    exponent = 1000000; 
  else
    exponent = 1/(1.0 - brush->hardness);
275

276
  /* set up lookup table */
277
  length = ceil (sqrt (2 * ceil (brush->radius+1) * ceil (brush->radius+1))+1) * OVERSAMPLING;
278
  lookup = g_malloc (length);
279
  sum = 0.0;
280

281
  for (x = 0; x < OVERSAMPLING; x++)
282
    {
283
      d = fabs ((x + 0.5) / OVERSAMPLING - 0.5);
284
      if (d > brush->radius)
285 286 287 288 289 290 291 292 293 294 295 296
	buffer[x] = 0.0;
      else
	/* buffer[x] =  (1.0 - pow (d/brush->radius, exponent)); */
	buffer[x] = gauss (pow (d/brush->radius, exponent));
      sum += buffer[x];
    }

  for (x = 0; d < brush->radius || sum > 0.00001; d += 1.0 / OVERSAMPLING)
    {
      sum -= buffer[x % OVERSAMPLING];
      if (d > brush->radius)
	buffer[x % OVERSAMPLING] = 0.0;
297 298
      else
	/* buffer[x%OVERSAMPLING] =  (1.0 - pow (d/brush->radius, exponent)); */
299
	buffer[x % OVERSAMPLING] = gauss (pow (d/brush->radius, exponent));
300
      sum += buffer[x%OVERSAMPLING];
301
      lookup[x++] = RINT (sum * (255.0 / OVERSAMPLING));
302
    }
303
  while (x < length)
304 305 306 307
    {
      lookup[x++] = 0;
    }
  /* compute one half and mirror it */
308 309
  for (y = 0; y <= height; y++)
    {
310 311 312 313 314 315 316
      for (x = -width; x <= width; x++)
	{
	  tx = c*x - s*y;
	  ty = c*y + s*x;
	  ty *= brush->aspect_ratio;
	  d = sqrt (tx*tx + ty*ty);
	  if (d < brush->radius+1)
317
	    a = lookup[(gint) RINT (d * OVERSAMPLING)];
318 319 320 321 322
	  else
	    a = 0;
	  centerp[   y*gbrush->mask->width + x] = a;
	  centerp[-1*y*gbrush->mask->width - x] = a;
	}
323
    }
324

325
  g_free (lookup);
326

327 328
  if (GIMP_DATA_CLASS (parent_class)->dirty)
    GIMP_DATA_CLASS (parent_class)->dirty (data);
329 330
}

331
GimpData *
332 333 334 335 336 337 338 339
gimp_brush_generated_new (gfloat radius,
			  gfloat hardness,
			  gfloat angle,
			  gfloat aspect_ratio)
{
  GimpBrushGenerated *brush;

  /* set up normal brush data */
Michael Natterer's avatar
Michael Natterer committed
340
  brush = GIMP_BRUSH_GENERATED (g_object_new (GIMP_TYPE_BRUSH_GENERATED, NULL));
341 342 343 344 345 346 347 348 349 350 351 352 353 354

  gimp_object_set_name (GIMP_OBJECT (brush), "Untitled");

  GIMP_BRUSH (brush)->spacing = 20;

  /* set up gimp_brush_generated data */
  brush->radius       = radius;
  brush->hardness     = hardness;
  brush->angle        = angle;
  brush->aspect_ratio = aspect_ratio;

  /* render brush mask */
  gimp_data_dirty (GIMP_DATA (brush));

355
  return GIMP_DATA (brush);
356 357
}

358
GimpData *
359 360 361
gimp_brush_generated_load (const gchar *filename)
{
  GimpBrushGenerated *brush;
362 363
  FILE               *fp;
  gchar               string[256];
364 365 366 367 368 369 370 371 372

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

  /* make sure the file we are reading is the right type */
  fgets (string, 255, fp);
  
  if (strncmp (string, "GIMP-VBR", 8) != 0)
    return NULL;
373

374 375
  /* make sure we are reading a compatible version */
  fgets (string, 255, fp);
376 377
  if (strncmp (string, "1.0", 3))
    return NULL;
378 379

  /* create new brush */
Michael Natterer's avatar
Michael Natterer committed
380
  brush = GIMP_BRUSH_GENERATED (g_object_new (GIMP_TYPE_BRUSH_GENERATED, NULL));
381 382 383 384 385 386 387 388 389 390 391 392

  gimp_data_set_filename (GIMP_DATA (brush), filename);

  gimp_brush_generated_freeze (brush);

  /* read name */
  fgets (string, 255, fp);
  if (string[strlen (string) - 1] == '\n')
    string[strlen (string) - 1] = 0;
  gimp_object_set_name (GIMP_OBJECT (brush), string);

  /* read brush spacing */
393 394
  fgets (string, 255, fp);
  GIMP_BRUSH (brush)->spacing = g_ascii_strtod (string, NULL);
395 396

  /* read brush radius */
397 398
  fgets (string, 255, fp);
  gimp_brush_generated_set_radius (brush, g_ascii_strtod (string, NULL));
399 400

  /* read brush hardness */
401 402
  fgets (string, 255, fp);
  gimp_brush_generated_set_hardness (brush, g_ascii_strtod (string, NULL));
403 404

  /* read brush aspect_ratio */
405 406
  fgets (string, 255, fp);
  gimp_brush_generated_set_aspect_ratio (brush, g_ascii_strtod (string, NULL));
407 408

  /* read brush angle */
409 410
  fgets (string, 255, fp);
  gimp_brush_generated_set_angle (brush, g_ascii_strtod (string, NULL));
411 412 413 414 415 416 417

  fclose (fp);

  gimp_brush_generated_thaw (brush);

  GIMP_DATA (brush)->dirty = FALSE;

Michael Natterer's avatar
Michael Natterer committed
418
  if (base_config->stingy_memory_use)
419 420
    temp_buf_swap (GIMP_BRUSH (brush)->mask);

421
  return GIMP_DATA (brush);
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
}

void
gimp_brush_generated_freeze (GimpBrushGenerated *brush)
{
  g_return_if_fail (brush != NULL);
  g_return_if_fail (GIMP_IS_BRUSH_GENERATED (brush));

  brush->freeze++;
}

void
gimp_brush_generated_thaw (GimpBrushGenerated *brush)
{
  g_return_if_fail (brush != NULL);
  g_return_if_fail (GIMP_IS_BRUSH_GENERATED (brush));
  
  if (brush->freeze > 0)
    brush->freeze--;

442
  if (!brush->freeze)
443 444 445
    gimp_data_dirty (GIMP_DATA (brush));
}

446 447 448
gfloat
gimp_brush_generated_set_radius (GimpBrushGenerated *brush,
				 gfloat              radius)
449
{
450 451
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (brush), -1.0);

452 453
  if (radius < 0.0)
    radius = 0.0;
454
  else if (radius > 32767.0)
455
    radius = 32767.0;
456

457 458
  if (radius == brush->radius)
    return radius;
459

460
  brush->radius = radius;
461

462
  if (!brush->freeze)
463
    gimp_data_dirty (GIMP_DATA (brush));
464

465 466 467
  return brush->radius;
}

468 469 470
gfloat
gimp_brush_generated_set_hardness (GimpBrushGenerated *brush,
				   gfloat              hardness)
471
{
472 473
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (brush), -1.0);

474 475
  if (hardness < 0.0)
    hardness = 0.0;
476
  else if (hardness > 1.0)
477
    hardness = 1.0;
478

479 480
  if (brush->hardness == hardness)
    return hardness;
481

482
  brush->hardness = hardness;
483

484
  if (!brush->freeze)
485
    gimp_data_dirty (GIMP_DATA (brush));
486

487 488 489
  return brush->hardness;
}

490 491 492
gfloat
gimp_brush_generated_set_angle (GimpBrushGenerated *brush,
				gfloat              angle)
493
{
494 495
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (brush), -1.0);

496
  if (angle < 0.0)
497
    angle = -1.0 * fmod (angle, 180.0);
498
  else if (angle > 180.0)
499
    angle = fmod (angle, 180.0);
500

501 502
  if (brush->angle == angle)
    return angle;
503

504
  brush->angle = angle;
505

506
  if (!brush->freeze)
507
    gimp_data_dirty (GIMP_DATA (brush));
508

509 510 511
  return brush->angle;
}

512 513 514
gfloat
gimp_brush_generated_set_aspect_ratio (GimpBrushGenerated *brush,
				       gfloat              ratio)
515
{
516 517
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (brush), -1.0);

518 519
  if (ratio < 1.0)
    ratio = 1.0;
520
  else if (ratio > 1000)
521
    ratio = 1000;
522

523 524
  if (brush->aspect_ratio == ratio)
    return ratio;
525

526
  brush->aspect_ratio = ratio;
527

528
  if (!brush->freeze)
529
    gimp_data_dirty (GIMP_DATA (brush));
530

531 532 533
  return brush->aspect_ratio;
}

534 535
gfloat
gimp_brush_generated_get_radius (const GimpBrushGenerated *brush)
536
{
537 538
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (brush), -1.0);

539 540 541
  return brush->radius;
}

542
gfloat
543
gimp_brush_generated_get_hardness (const GimpBrushGenerated *brush)
544
{
545 546
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (brush), -1.0);

547 548 549
  return brush->hardness;
}

550
gfloat
551
gimp_brush_generated_get_angle (const GimpBrushGenerated *brush)
552
{
553 554
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (brush), -1.0);

555 556 557
  return brush->angle;
}

558
gfloat
559
gimp_brush_generated_get_aspect_ratio (const GimpBrushGenerated *brush)
560
{
561 562
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (brush), -1.0);

563 564
  return brush->aspect_ratio;
}