airbrush.c 10.8 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
18
 */
#include <stdlib.h>
19

Elliot Lee's avatar
Elliot Lee committed
20
#include "appenv.h"
21
#include "airbrush.h"
Elliot Lee's avatar
Elliot Lee committed
22
23
24
#include "drawable.h"
#include "errors.h"
#include "gdisplay.h"
25
26
27
28
#include "gimpbrushpipe.h"
#include "gradient.h"
#include "gimage.h"
#include "gimpui.h"
Elliot Lee's avatar
Elliot Lee committed
29
30
#include "paint_funcs.h"
#include "paint_core.h"
31
#include "paint_options.h"
Elliot Lee's avatar
Elliot Lee committed
32
33
34
#include "selection.h"
#include "tools.h"

Sven Neumann's avatar
Sven Neumann committed
35
#include "config.h"
36
37
#include "libgimp/gimpintl.h"

38
/*  The maximum amount of pressure that can be exerted  */
39
#define MAX_PRESSURE  0.075
40

41
/* Default pressure setting */
42
#define AIRBRUSH_PRESSURE_DEFAULT    10.0
43
#define AIRBRUSH_INCREMENTAL_DEFAULT FALSE
44

45
46
#define OFF           0
#define ON            1
Elliot Lee's avatar
Elliot Lee committed
47

48
49
50
/*  the airbrush structures  */

typedef struct _AirbrushTimeout AirbrushTimeout;
51

Elliot Lee's avatar
Elliot Lee committed
52
53
struct _AirbrushTimeout
{
54
  PaintCore    *paint_core;
55
  GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
56
57
58
};

typedef struct _AirbrushOptions AirbrushOptions;
59

Elliot Lee's avatar
Elliot Lee committed
60
61
struct _AirbrushOptions
{
62
  PaintOptions paint_options;
63

64
65
  gdouble      rate;
  gdouble      rate_d;
66
67
  GtkObject   *rate_w;

68
69
  gdouble      pressure;
  gdouble      pressure_d;
70
  GtkObject   *pressure_w;
Elliot Lee's avatar
Elliot Lee committed
71
72
};

73
74

/*  the airbrush tool options  */
75
static AirbrushOptions *airbrush_options = NULL; 
Elliot Lee's avatar
Elliot Lee committed
76
77

/*  local variables  */
78
static gint             timer;  /*  timer for successive paint applications  */
79
static gint             timer_state = OFF;       /*  state of airbrush tool  */
Elliot Lee's avatar
Elliot Lee committed
80
static AirbrushTimeout  airbrush_timeout;
81

82
static gdouble          non_gui_pressure;
83
static gboolean         non_gui_incremental;
84
85

/*  forward function declarations  */
86
87
88
89
static void   airbrush_motion   (PaintCore *, GimpDrawable *,
				 PaintPressureOptions *,
				 gdouble, PaintApplicationMode);
static gint   airbrush_time_out (gpointer);
90
91
92


/*  functions  */
Elliot Lee's avatar
Elliot Lee committed
93
94

static void
95
airbrush_options_reset (void)
96
97
98
{
  AirbrushOptions *options = airbrush_options;

99
100
  paint_options_reset ((PaintOptions *) options);

101
102
103
104
105
106
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->rate_w),
			    options->rate_d);
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->pressure_w),
			    options->pressure_d);
}

Elliot Lee's avatar
Elliot Lee committed
107
static AirbrushOptions *
108
airbrush_options_new (void)
Elliot Lee's avatar
Elliot Lee committed
109
110
111
{
  AirbrushOptions *options;

112
113
114
115
116
  GtkWidget *vbox;
  GtkWidget *table;
  GtkWidget *scale;

  /*  the new airbrush tool options structure  */
Michael Natterer's avatar
Michael Natterer committed
117
  options = g_new (AirbrushOptions, 1);
118
119
120
  paint_options_init ((PaintOptions *) options,
		      AIRBRUSH,
		      airbrush_options_reset);
121
  options->rate     = options->rate_d     = 80.0;
122
  options->pressure = options->pressure_d = AIRBRUSH_PRESSURE_DEFAULT;
Elliot Lee's avatar
Elliot Lee committed
123
124

  /*  the main vbox  */
125
  vbox = ((ToolOptions *) options)->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
126
127

  /*  the rate scale  */
128
  table = gtk_table_new (2, 2, FALSE);
129
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 4);
130
131
132
133
134
135
136
137
138
  gtk_table_set_row_spacings (GTK_TABLE (table), 1);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  options->rate_w =
    gtk_adjustment_new (options->rate_d, 0.0, 150.0, 1.0, 1.0, 0.0);
  scale = gtk_hscale_new (GTK_ADJUSTMENT (options->rate_w));
  gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (scale), GTK_UPDATE_DELAYED);
  gtk_signal_connect (GTK_OBJECT (options->rate_w), "value_changed",
Michael Natterer's avatar
Michael Natterer committed
139
		      GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
140
		      &options->rate);
141
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
142
			     _("Rate:"), 1.0, 1.0,
143
			     scale, 1, FALSE);
Elliot Lee's avatar
Elliot Lee committed
144
145

  /*  the pressure scale  */
146
147
148
149
150
151
  options->pressure_w =
    gtk_adjustment_new (options->pressure_d, 0.0, 100.0, 1.0, 1.0, 0.0);
  scale = gtk_hscale_new (GTK_ADJUSTMENT (options->pressure_w));
  gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (scale), GTK_UPDATE_DELAYED);
  gtk_signal_connect (GTK_OBJECT (options->pressure_w), "value_changed",
Michael Natterer's avatar
Michael Natterer committed
152
		      GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
153
		      &options->pressure);
154
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
155
			     _("Pressure:"), 1.0, 1.0,
156
			     scale, 1, FALSE);
157
158

  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
159
160
161
162

  return options;
}

163
Tool *
Manish Singh's avatar
Manish Singh committed
164
tools_new_airbrush (void)
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
{
  Tool * tool;
  PaintCore * private;

  /*  The tool options  */
  if (! airbrush_options)
    {
      airbrush_options = airbrush_options_new ();
      tools_register (AIRBRUSH, (ToolOptions *) airbrush_options);
    }

  tool = paint_core_new (AIRBRUSH);

  private = (PaintCore *) tool->private;
  private->paint_func = airbrush_paint_func;
180
  private->pick_colors = TRUE;
181
  private->flags |= TOOL_CAN_HANDLE_CHANGING_BRUSH;
182
183
184
185

  return tool;
}

Elliot Lee's avatar
Elliot Lee committed
186
void *
187
airbrush_paint_func (PaintCore    *paint_core,
188
		     GimpDrawable *drawable,
189
		     int           state)
Elliot Lee's avatar
Elliot Lee committed
190
{
191
  GimpBrush *brush;
192
  gdouble rate;
Elliot Lee's avatar
Elliot Lee committed
193

194
195
196
  if (!drawable) 
    return NULL;

197
  brush = gimp_context_get_brush (NULL);
Elliot Lee's avatar
Elliot Lee committed
198
199
200
  switch (state)
    {
    case INIT_PAINT :
201
      /* timer_state = OFF; */
202
      if (timer_state == ON)
Michael Natterer's avatar
Michael Natterer committed
203
204
205
206
	{
	  g_warning ("killing stray timer, please report to lewing@gimp.org");
	  gtk_timeout_remove (timer);
	}
Elliot Lee's avatar
Elliot Lee committed
207
208
209
210
211
212
213
214
      timer_state = OFF;
      break;

    case MOTION_PAINT :
      if (timer_state == ON)
	gtk_timeout_remove (timer);
      timer_state = OFF;

215
216
217
      airbrush_motion (paint_core, drawable,
		       airbrush_options->paint_options.pressure_options,
		       airbrush_options->pressure,
218
219
		       airbrush_options->paint_options.incremental ?
		       INCREMENTAL : CONSTANT);
Elliot Lee's avatar
Elliot Lee committed
220
221

      if (airbrush_options->rate != 0.0)
222
223
224
	{
	  airbrush_timeout.paint_core = paint_core;
	  airbrush_timeout.drawable = drawable;
225
226
227
228
	  rate = airbrush_options->paint_options.pressure_options->rate ? 
	    (10000 / (airbrush_options->rate * 2.0 * paint_core->curpressure)) : 
	    (10000 / airbrush_options->rate);
	  timer = gtk_timeout_add (rate, airbrush_time_out, NULL);
229
230
	  timer_state = ON;
	}
Elliot Lee's avatar
Elliot Lee committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
      break;

    case FINISH_PAINT :
      if (timer_state == ON)
	gtk_timeout_remove (timer);
      timer_state = OFF;
      break;

    default :
      break;
    }

  return NULL;
}


void
tools_free_airbrush (Tool *tool)
{
  if (timer_state == ON)
    gtk_timeout_remove (timer);
  timer_state = OFF;
253
254

  paint_core_free (tool);
Elliot Lee's avatar
Elliot Lee committed
255
256
257
258
259
260
261
262
}


static gint
airbrush_time_out (gpointer client_data)
{
  /*  service the timer  */
  airbrush_motion (airbrush_timeout.paint_core,
263
		   airbrush_timeout.drawable,
264
		   airbrush_options->paint_options.pressure_options,
265
		   airbrush_options->pressure,
266
267
		   airbrush_options->paint_options.incremental ?
		   INCREMENTAL : CONSTANT);
Elliot Lee's avatar
Elliot Lee committed
268
269
270
271
  gdisplays_flush ();

  /*  restart the timer  */
  if (airbrush_options->rate != 0.0)
272
273
274
275
276
277
278
279
280
281
282
    {
      if (airbrush_options->paint_options.pressure_options->rate)
	{
	  /* set a new timer */
	  timer = gtk_timeout_add ((10000 / (airbrush_options->rate * 2.0 * airbrush_timeout.paint_core->curpressure)), 
				   airbrush_time_out, NULL);
	  return FALSE;
	}
      else 
	return TRUE;
    }
Elliot Lee's avatar
Elliot Lee committed
283
284
285
286
287
288
  else
    return FALSE;
}


static void
289
290
291
292
293
airbrush_motion (PaintCore	      *paint_core,
		 GimpDrawable	      *drawable,
		 PaintPressureOptions *pressure_options,
		 double		       pressure,
		 PaintApplicationMode  mode)
Elliot Lee's avatar
Elliot Lee committed
294
295
296
297
{
  GImage *gimage;
  TempBuf * area;
  unsigned char col[MAX_CHANNELS];
298
  gdouble scale;
Elliot Lee's avatar
Elliot Lee committed
299

300
301
302
303
  if (!drawable) 
    return;

  if (! (gimage = drawable_gimage (drawable)))
Elliot Lee's avatar
Elliot Lee committed
304
305
    return;

306
307
308
309
310
311
  if (pressure_options->size)
    scale = paint_core->curpressure;
  else
    scale = 1.0;

  if (! (area = paint_core_get_paint_area (paint_core, drawable, scale)))
Elliot Lee's avatar
Elliot Lee committed
312
313
    return;

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  /*  color the pixels  */
  if (pressure_options->color)
    {
      gdouble r, g, b, a;
      
      gradient_get_color_at (gimp_context_get_gradient (NULL),
			     paint_core->curpressure, &r, &g, &b, &a);
      col[0] = r * 255.0;
      col[1] = g * 255.0;
      col[2] = b * 255.0;
      col[3] = a * 255.0;
      mode = INCREMENTAL;
      color_pixels (temp_buf_data (area), col,
		    area->width * area->height, area->bytes);
    }
  else if (GIMP_IS_BRUSH_PIXMAP (paint_core->brush))
330
331
    {
      mode = INCREMENTAL;
332
333
      paint_core_color_area_with_pixmap (paint_core, gimage, drawable, area, 
					 scale, SOFT);
334
335
336
    }
  else
    {
337
338
      gimage_get_foreground (gimage, drawable, col);
      col[area->bytes - 1] = OPAQUE_OPACITY;
339
340
341
      color_pixels (temp_buf_data (area), col,
		    area->width * area->height, area->bytes);
    }
Elliot Lee's avatar
Elliot Lee committed
342

343
344
  if (pressure_options->pressure)
    pressure = pressure * 2.0 * paint_core->curpressure;
Owen Taylor's avatar
Owen Taylor committed
345

Elliot Lee's avatar
Elliot Lee committed
346
  /*  paste the newly painted area to the image  */
347
  paint_core_paste_canvas (paint_core, drawable,
348
			   MIN (pressure, 255),
349
350
			   (gint) (gimp_context_get_opacity (NULL) * 255),
			   gimp_context_get_paint_mode (NULL),
351
			   SOFT, scale, mode);
Elliot Lee's avatar
Elliot Lee committed
352
353
354
}

static void *
355
airbrush_non_gui_paint_func (PaintCore    *paint_core,
356
			     GimpDrawable *drawable,
357
			     int           state)
Elliot Lee's avatar
Elliot Lee committed
358
{
359
360
  airbrush_motion (paint_core, drawable, &non_gui_pressure_options, 
		   non_gui_pressure, non_gui_incremental);
Elliot Lee's avatar
Elliot Lee committed
361
362
363
364

  return NULL;
}

365
366
367
368
369
370
371
372
373
374
375
gboolean
airbrush_non_gui_default (GimpDrawable *drawable,
			  int           num_strokes,
			  double       *stroke_array)
{
  AirbrushOptions *options = airbrush_options;
  gdouble pressure = AIRBRUSH_PRESSURE_DEFAULT;

  if(options)
    pressure = options->pressure;

376
  return airbrush_non_gui (drawable, pressure, num_strokes, stroke_array);
377
378
}

379
380
381
382
383
gboolean
airbrush_non_gui (GimpDrawable *drawable,
    		  double        pressure,
		  int           num_strokes,
		  double       *stroke_array)
Elliot Lee's avatar
Elliot Lee committed
384
385
386
{
  int i;

387
388
  if (paint_core_init (&non_gui_paint_core, drawable,
		       stroke_array[0], stroke_array[1]))
Elliot Lee's avatar
Elliot Lee committed
389
    {
390
      /* Set the paint core's paint func */
Elliot Lee's avatar
Elliot Lee committed
391
392
      non_gui_paint_core.paint_func = airbrush_non_gui_paint_func;

393
394
      non_gui_pressure = pressure;

Elliot Lee's avatar
Elliot Lee committed
395
396
397
      non_gui_paint_core.startx = non_gui_paint_core.lastx = stroke_array[0];
      non_gui_paint_core.starty = non_gui_paint_core.lasty = stroke_array[1];

398
      airbrush_non_gui_paint_func (&non_gui_paint_core, drawable, 0);
Elliot Lee's avatar
Elliot Lee committed
399
400
401
402
403
404

      for (i = 1; i < num_strokes; i++)
	{
	  non_gui_paint_core.curx = stroke_array[i * 2 + 0];
	  non_gui_paint_core.cury = stroke_array[i * 2 + 1];

405
	  paint_core_interpolate (&non_gui_paint_core, drawable);
Elliot Lee's avatar
Elliot Lee committed
406
407
408
409
410

	  non_gui_paint_core.lastx = non_gui_paint_core.curx;
	  non_gui_paint_core.lasty = non_gui_paint_core.cury;
	}

411
      /* Finish the painting */
412
      paint_core_finish (&non_gui_paint_core, drawable, -1);
Elliot Lee's avatar
Elliot Lee committed
413

414
      /* Cleanup */
Elliot Lee's avatar
Elliot Lee committed
415
      paint_core_cleanup ();
416
      return TRUE;
Elliot Lee's avatar
Elliot Lee committed
417
    }
418
419
  else
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
420
}