gimpbrushgenerated-load.c 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 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.
 */

#include "appenv.h"
#include "gimpbrushgenerated.h"
#include "paint_core.h"
#include <math.h>
25
#include <stdio.h>
26
27
28

#define OVERSAMPLING 5

29
30
31
static void
gimp_brush_generated_generate(GimpBrushGenerated *brush);

32
33
34
static GimpObjectClass* parent_class;

static void
35
gimp_brush_generated_destroy(GtkObject *object)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{
  GTK_OBJECT_CLASS(parent_class)->destroy (object);
}

static void
gimp_brush_generated_class_init (GimpBrushGeneratedClass *klass)
{
  GtkObjectClass *object_class;
  
  object_class = GTK_OBJECT_CLASS(klass);

  parent_class = gtk_type_class (GIMP_TYPE_BRUSH);
  object_class->destroy =  gimp_brush_generated_destroy;
}


void
gimp_brush_generated_init(GimpBrushGenerated *brush)
{
  brush->radius        = 5.0;
  brush->hardness      = 0.0;
  brush->angle         = 0.0;
  brush->aspect_ratio  = 1.0;
59
  brush->freeze        = 0;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
}

guint gimp_brush_generated_get_type(void)
{
  static GtkType type;
  if(!type){
    GtkTypeInfo info={
      "GimpBrushGenerated",
      sizeof(GimpBrushGenerated),
      sizeof(GimpBrushGeneratedClass),
      (GtkClassInitFunc)gimp_brush_generated_class_init,
      (GtkObjectInitFunc)gimp_brush_generated_init,
      NULL,
      NULL };
    type=gtk_type_unique(GIMP_TYPE_BRUSH, &info);
  }
  return type;
}

GimpBrushGenerated *gimp_brush_generated_new(float radius, float hardness,
				    float angle, float aspect_ratio)
{
  GimpBrushGenerated *brush;

/* set up normal brush data */
  brush = GIMP_BRUSH_GENERATED(gimp_type_new(gimp_brush_generated_get_type ()));

  GIMP_BRUSH(brush)->name = g_strdup ("AAAGenerated");
  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_brush_generated_generate(brush);

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
  return brush;
}

GimpBrushGenerated *
gimp_brush_generated_load (char *file_name)
{
  GimpBrushGenerated *brush;
  FILE *fp;
  char string[256];
  float fl;
  float version;
  if ((fp = fopen(file_name, "r")) == NULL)
    return NULL;

/* make sure the file we are reading is the right type */
  fscanf(fp, "%8s", string);
  g_return_val_if_fail(strcmp(string, "GIMP-VBR") == 0, NULL);
/* make sure we are reading a compatible version */
  fscanf(fp, "%f", &version);
  g_return_val_if_fail(version < 2.0, NULL);

/* create new brush */
  brush = GIMP_BRUSH_GENERATED(gimp_type_new(gimp_brush_generated_get_type()));
  GIMP_BRUSH(brush)->filename = g_strdup (file_name);
  gimp_brush_generated_freeze(brush);

/* read name */
  fscanf(fp, "%255s", string);
  GIMP_BRUSH(brush)->name = g_strdup (string);
/* read brush spacing */
  fscanf(fp, "%f", &fl);
  GIMP_BRUSH(brush)->spacing = fl;
/* read brush radius */
  fscanf(fp, "%f", &fl);
  gimp_brush_generated_set_radius(brush, fl);
/* read brush hardness */
  fscanf(fp, "%f", &fl);
  gimp_brush_generated_set_hardness(brush, fl);
/* read brush aspect_ratio */
  fscanf(fp, "%f", &fl);
  gimp_brush_generated_set_aspect_ratio(brush, fl);
/* read brush angle */
  fscanf(fp, "%f", &fl);
  gimp_brush_generated_set_angle(brush, fl);

  fclose(fp);

  gimp_brush_generated_thaw(brush);
147
148
149
150

  return brush;
}

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
void
gimp_brush_generated_save (GimpBrushGenerated *brush, 
			  char *file_name)
{
/* WARNING: untested function */
  FILE *fp;
  if ((fp = fopen(file_name, "w")) == NULL)
  {
    g_warning("Unable to save file %s", file_name);
    return;
  }
/* write magic header */
  fprintf(fp, "GIMP-VBR\n");
/* write version */
  fprintf(fp, "1.0\n");
/* write name */
  fprintf(fp, "%s", GIMP_BRUSH(brush)->name);
/* write brush spacing */
  fprintf(fp, "%f", (float)GIMP_BRUSH(brush)->spacing);
/* write brush radius */
  fprintf(fp, "%f", brush->radius);
/* write brush hardness */
  fprintf(fp, "%f", brush->hardness);
/* write brush aspect_ratio */
  fprintf(fp, "%f", brush->aspect_ratio);
/* write brush angle */
  fprintf(fp, "%f", brush->angle);

  fclose(fp);
}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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--;
  if (brush->freeze == 0)
    gimp_brush_generated_generate(brush);
}
202
203
204
205
206
207
208
209
210
211
212
213
214
static
double gauss(double f)
{ /* this aint' a real gauss function */
    if (f < -.5)
    {
      f = -1.0-f;
      return (2.0*f*f);
    }
    if (f < .5)
      return (1.0-2.0*f*f);
    f = 1.0 -f;
    return (2.0*f*f);
}
215

216
217
218
219
220
221
222
223
224
225
226
227
228
229
void
gimp_brush_generated_generate(GimpBrushGenerated *brush)
{
  register GimpBrush *gbrush = NULL;
  register int x, y;
  register guchar *centerp;
  register double d;
  register double exponent;
  register guchar a;
  register int length;
  register char *lookup;
  double buffer[OVERSAMPLING];
  register double sum, c, s, tx, ty;
  int width, height;
230
231
232
233
  g_return_if_fail (brush != NULL);
  g_return_if_fail (GIMP_IS_BRUSH_GENERATED (brush));
  
  if (brush->freeze) /* if we are frozen defer rerendering till later */
234
235
236
237
238
239
    return;
  gbrush = GIMP_BRUSH(brush);
  if (gbrush->mask)
  {
    temp_buf_free(gbrush->mask);
  }
240
  /* compute the range of the brush. should do a better job than this? */
241
242
  s = sin(brush->angle*M_PI/180.0);
  c = cos(brush->angle*M_PI/180.0);
243
244
245
246
247
248
249
250
251
252
253
  tx = MAXIMUM(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 = MAXIMUM(fabs(s*ceil(brush->radius) + c*ceil(brush->radius)
		    /brush->aspect_ratio),
	       fabs(s*ceil(brush->radius) - c*ceil(brush->radius)
		    /brush->aspect_ratio));
  if (brush->radius > tx)
    width = ceil(tx);
  else
254
    width = ceil(brush->radius);
255
256
257
258
  if (brush->radius > ty)
    height = ceil(ty);
  else
    height = ceil(brush->radius);
259
260
261
  /* compute the axis for spacing */
  GIMP_BRUSH(brush)->x_axis.x =      c*brush->radius;
  GIMP_BRUSH(brush)->x_axis.y = -1.0*s*brush->radius;
262

263
264
265
  GIMP_BRUSH(brush)->y_axis.x = (s*brush->radius / brush->aspect_ratio);
  GIMP_BRUSH(brush)->y_axis.y = (c*brush->radius / brush->aspect_ratio);
  
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  gbrush->mask = temp_buf_new(width*2 + 1,
			     height*2 + 1,
			     1, width, height, 0);
  centerp = &gbrush->mask->data[height*gbrush->mask->width + width];
  if ((1.0 - brush->hardness) < 0.000001)
    exponent = 1000000; 
  else
    exponent = 1/(1.0 - brush->hardness);
/* set up lookup table */
  length = ceil(sqrt(2*ceil(brush->radius+1)*ceil(brush->radius+1))+1) * OVERSAMPLING;
  lookup = g_malloc(length);
  sum = 0.0;
  for (x = 0; x < OVERSAMPLING; x++)
  {
    d = fabs((x+.5)/OVERSAMPLING - .5);
    if (d > brush->radius)
      buffer[x] = 0.0;
    else
284
285
/*      buffer[x] =  (1.0 - pow(d/brush->radius, exponent));*/
      buffer[x] =  gauss(pow(d/brush->radius, exponent));
286
287
288
289
290
291
292
293
    sum += buffer[x];
  }
  for (x = 0; d < brush->radius || sum > .00001; d += 1.0/OVERSAMPLING)
  {
    sum -= buffer[x%OVERSAMPLING];
    if (d > brush->radius)
      buffer[x%OVERSAMPLING] = 0.0;
    else
294
295
/*      buffer[x%OVERSAMPLING] =  (1.0 - pow(d/brush->radius, exponent));*/
      buffer[x%OVERSAMPLING] =  gauss(pow(d/brush->radius, exponent));
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    sum += buffer[x%OVERSAMPLING];
    lookup[x++] =  rint(sum*(255.0/OVERSAMPLING));
  }
  while (x < length)
  {
    lookup[x++] = 0;
  }
 /* compute one half and mirror it */
  for (y = 0; y <= height; y++)
  {
    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)
	a = lookup[(int)rint(d*OVERSAMPLING)];
      else
	a = 0;
      centerp[   y*gbrush->mask->width + x] = a;
      centerp[-1*y*gbrush->mask->width - x] = a;
    }
  }
  g_free (lookup);
321
322
  gtk_signal_emit_by_name(GTK_OBJECT(brush), "dirty");
/*  brush_changed_notify(GIMP_BRUSH(brush)); */
323
324
325
326
327
328
329
330
331
332
}

float
gimp_brush_generated_set_radius (GimpBrushGenerated* brush, float radius)
{
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED(brush), -1.0);
  if (radius < 0.0)
    radius = 0.0;
  else if(radius > 32767.0)
    radius = 32767.0;
333
334
  if (radius == brush->radius)
    return radius;
335
  brush->radius = radius;
336
337
  if (!brush->freeze)
    gimp_brush_generated_generate(brush);
338
339
340
341
342
343
344
345
346
347
348
  return brush->radius;
}

float
gimp_brush_generated_set_hardness (GimpBrushGenerated* brush, float hardness)
{
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED(brush), -1.0);
  if (hardness < 0.0)
    hardness = 0.0;
  else if(hardness > 1.0)
    hardness = 1.0;
349
350
  if (brush->hardness == hardness)
    return hardness;
351
  brush->hardness = hardness;
352
353
  if (!brush->freeze)
    gimp_brush_generated_generate(brush);
354
355
356
357
358
359
360
361
362
363
364
  return brush->hardness;
}

float
gimp_brush_generated_set_angle (GimpBrushGenerated* brush, float angle)
{
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED(brush), -1.0);
  if (angle < 0.0)
    angle = -1.0 * fmod(angle, 180.0);
  else if(angle > 180.0)
    angle = fmod(angle, 180.0);
365
366
  if (brush->angle == angle)
    return angle;
367
  brush->angle = angle;
368
369
  if (!brush->freeze)
    gimp_brush_generated_generate(brush);
370
371
372
373
374
375
376
377
378
379
380
  return brush->angle;
}

float
gimp_brush_generated_set_aspect_ratio (GimpBrushGenerated* brush, float ratio)
{
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED(brush), -1.0);
  if (ratio < 1.0)
    ratio = 1.0;
  else if(ratio > 1000)
    ratio = 1000;
381
382
  if (brush->aspect_ratio == ratio)
    return ratio;
383
  brush->aspect_ratio = ratio;
384
385
  if (!brush->freeze)
    gimp_brush_generated_generate(brush);
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
  return brush->aspect_ratio;
}

float 
gimp_brush_generated_get_radius (GimpBrushGenerated* brush)
{
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED(brush), -1.0);
  return brush->radius;
}

float
gimp_brush_generated_get_hardness (GimpBrushGenerated* brush)
{
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED(brush), -1.0);
  return brush->hardness;
}

float
gimp_brush_generated_get_angle (GimpBrushGenerated* brush)
{
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED(brush), -1.0);
  return brush->angle;
}

float
gimp_brush_generated_get_aspect_ratio (GimpBrushGenerated* brush)
{
  g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED(brush), -1.0);
  return brush->aspect_ratio;
}