pencil.c 6.27 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 21 22 23
#include "appenv.h"
#include "drawable.h"
#include "errors.h"
#include "gdisplay.h"
24
#include "gimpbrushpipe.h"
25
#include "gradient.h"
Elliot Lee's avatar
Elliot Lee committed
26 27
#include "paint_funcs.h"
#include "paint_core.h"
28
#include "paint_options.h"
Elliot Lee's avatar
Elliot Lee committed
29 30 31
#include "paintbrush.h"
#include "pencil.h"
#include "selection.h"
32
#include "tool_options_ui.h"
Elliot Lee's avatar
Elliot Lee committed
33 34
#include "tools.h"

35 36
#include "libgimp/gimpintl.h"

37
/*  the pencil tool options  */
38 39 40 41 42
typedef struct _PencilOptions PencilOptions;
struct _PencilOptions
{
  PaintOptions  paint_options;
};
43

44
static PencilOptions *pencil_options = NULL; 
45

Elliot Lee's avatar
Elliot Lee committed
46
/*  forward function declarations  */
47 48
static void      pencil_motion (PaintCore *, GimpDrawable *, 
				PaintPressureOptions *, gboolean);
Elliot Lee's avatar
Elliot Lee committed
49

50
static gboolean  non_gui_incremental = FALSE;
51
static void      pencil_options_reset (void);
52 53

#define PENCIL_INCREMENTAL_DEFAULT FALSE
54 55

/*  functions  */
Elliot Lee's avatar
Elliot Lee committed
56 57

void *
58 59 60
pencil_paint_func (PaintCore    *paint_core,
		   GimpDrawable *drawable,
		   int           state)
Elliot Lee's avatar
Elliot Lee committed
61 62 63 64 65 66 67
{
  switch (state)
    {
    case INIT_PAINT :
      break;

    case MOTION_PAINT :
68 69 70
      pencil_motion (paint_core, drawable, 
		     pencil_options->paint_options.pressure_options, 
		     pencil_options->paint_options.incremental);
Elliot Lee's avatar
Elliot Lee committed
71 72 73 74 75 76 77 78 79 80 81 82
      break;

    case FINISH_PAINT :
      break;

    default :
      break;
    }

  return NULL;
}

83 84 85 86 87
static PencilOptions *
pencil_options_new (void)
{
  PencilOptions *options;

88
  options = (PencilOptions *) g_malloc (sizeof (PencilOptions));  
89 90 91 92 93 94 95
  paint_options_init ((PaintOptions *) options,
		      PENCIL,
		      pencil_options_reset);
  return options;
}

static void
96 97
pencil_options_reset (void)
{
98
  paint_options_reset ((PaintOptions *) pencil_options);
99 100
}

Elliot Lee's avatar
Elliot Lee committed
101
Tool *
102
tools_new_pencil (void)
Elliot Lee's avatar
Elliot Lee committed
103 104 105 106
{
  Tool * tool;
  PaintCore * private;

107
  /*  The tool options  */
Elliot Lee's avatar
Elliot Lee committed
108
  if (!pencil_options)
109
    {
110
      pencil_options = pencil_options_new ();
111
      tools_register (PENCIL, (ToolOptions *) pencil_options);
112
      pencil_options_reset();
113
    }
Elliot Lee's avatar
Elliot Lee committed
114 115 116 117 118

  tool = paint_core_new (PENCIL);

  private = (PaintCore *) tool->private;
  private->paint_func = pencil_paint_func;
119
  private->pick_colors = TRUE;
120
  private->flags |= TOOL_CAN_HANDLE_CHANGING_BRUSH;
Elliot Lee's avatar
Elliot Lee committed
121 122 123 124 125

  return tool;
}

void
126
tools_free_pencil (Tool *tool)
Elliot Lee's avatar
Elliot Lee committed
127 128 129 130
{
  paint_core_free (tool);
}

131
static void
132 133 134 135
pencil_motion (PaintCore            *paint_core,
	       GimpDrawable         *drawable,
	       PaintPressureOptions *pressure_options,
	       gboolean	             increment)
Elliot Lee's avatar
Elliot Lee committed
136 137
{
  GImage *gimage;
138
  TempBuf *area;
Elliot Lee's avatar
Elliot Lee committed
139
  unsigned char col[MAX_CHANNELS];
140
  gint opacity;
141
  gdouble scale;
142
  PaintApplicationMode paint_appl_mode = increment ? INCREMENTAL : CONSTANT;
Elliot Lee's avatar
Elliot Lee committed
143

144
  if (! (gimage = drawable_gimage (drawable)))
Elliot Lee's avatar
Elliot Lee committed
145 146
    return;

147 148 149 150 151
  if (pressure_options->size)
    scale = paint_core->curpressure;
  else
    scale = 1.0;

Elliot Lee's avatar
Elliot Lee committed
152
  /*  Get a region which can be used to paint to  */
153
  if (! (area = paint_core_get_paint_area (paint_core, drawable, scale)))
Elliot Lee's avatar
Elliot Lee committed
154 155
    return;

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  /*  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;
      paint_appl_mode = INCREMENTAL;
      color_pixels (temp_buf_data (area), col,
		    area->width * area->height, area->bytes);
    }
  else if (GIMP_IS_BRUSH_PIXMAP (paint_core->brush))
172
    {
173
      /* if its a pixmap, do pixmap stuff */      
174 175
      paint_core_color_area_with_pixmap (paint_core, gimage, drawable, area, 
					 scale, HARD);
176
      paint_appl_mode = INCREMENTAL;
177 178 179
    }
  else
    {
180 181
      gimage_get_foreground (gimage, drawable, col);
      col[area->bytes - 1] = OPAQUE_OPACITY;
182 183 184
      color_pixels (temp_buf_data (area), col,
		    area->width * area->height, area->bytes);
    }
Elliot Lee's avatar
Elliot Lee committed
185

186 187 188
  opacity = 255 * gimp_context_get_opacity (NULL);
  if (pressure_options->opacity)
    opacity = opacity * 2.0 * paint_core->curpressure;
189

Elliot Lee's avatar
Elliot Lee committed
190
  /*  paste the newly painted canvas to the gimage which is being worked on  */
191 192
  paint_core_paste_canvas (paint_core, drawable, 
			   MIN (opacity, 255),
193 194
			   (int) (gimp_context_get_opacity (NULL) * 255),
			   gimp_context_get_paint_mode (NULL),
195
			   HARD, scale, paint_appl_mode);
Elliot Lee's avatar
Elliot Lee committed
196 197 198
}

static void *
199
pencil_non_gui_paint_func (PaintCore    *paint_core,
200 201
			   GimpDrawable *drawable,
			   int           state)
Elliot Lee's avatar
Elliot Lee committed
202
{
203 204
  pencil_motion (paint_core, drawable, &non_gui_pressure_options, 
		 non_gui_incremental);
Elliot Lee's avatar
Elliot Lee committed
205 206 207 208 209

  return NULL;
}


210 211
gboolean
pencil_non_gui (GimpDrawable *drawable,
212 213
		int           num_strokes,
		double       *stroke_array)
Elliot Lee's avatar
Elliot Lee committed
214 215 216
{
  int i;

217
  if (paint_core_init (&non_gui_paint_core, drawable,
218
		       stroke_array[0], stroke_array[1]))
Elliot Lee's avatar
Elliot Lee committed
219
    {
220
      /* Set the paint core's paint func */
Elliot Lee's avatar
Elliot Lee committed
221 222 223 224 225
      non_gui_paint_core.paint_func = pencil_non_gui_paint_func;

      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];

226
      pencil_non_gui_paint_func (&non_gui_paint_core, drawable, 0);
Elliot Lee's avatar
Elliot Lee committed
227 228

      for (i = 1; i < num_strokes; i++)
229 230 231
       {
         non_gui_paint_core.curx = stroke_array[i * 2 + 0];
         non_gui_paint_core.cury = stroke_array[i * 2 + 1];
Elliot Lee's avatar
Elliot Lee committed
232

233
         paint_core_interpolate (&non_gui_paint_core, drawable);
Elliot Lee's avatar
Elliot Lee committed
234

235 236 237
         non_gui_paint_core.lastx = non_gui_paint_core.curx;
         non_gui_paint_core.lasty = non_gui_paint_core.cury;
       }
Elliot Lee's avatar
Elliot Lee committed
238

239
      /* Finish the painting */
240
      paint_core_finish (&non_gui_paint_core, drawable, -1);
Elliot Lee's avatar
Elliot Lee committed
241

242
      /* Cleanup */
Elliot Lee's avatar
Elliot Lee committed
243
      paint_core_cleanup ();
244
      return TRUE;
Elliot Lee's avatar
Elliot Lee committed
245
    }
246 247
  else
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
248
}