gimppenciltool.c 5.17 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 19 20 21 22
 */
#include <stdlib.h>
#include "appenv.h"
#include "drawable.h"
#include "errors.h"
#include "gdisplay.h"
23
#include "gimpbrushpipe.h"
Elliot Lee's avatar
Elliot Lee committed
24 25
#include "paint_funcs.h"
#include "paint_core.h"
26
#include "paint_options.h"
Elliot Lee's avatar
Elliot Lee committed
27
#include "paintbrush.h"
28
#include "palette.h"
Elliot Lee's avatar
Elliot Lee committed
29 30 31 32
#include "pencil.h"
#include "selection.h"
#include "tools.h"

33 34
#include "libgimp/gimpintl.h"

35 36

/*  the pencil tool options  */
37

38
static PaintOptions *pencil_options = NULL; 
39

Elliot Lee's avatar
Elliot Lee committed
40
/*  forward function declarations  */
41
static void      pencil_motion (PaintCore *, GimpDrawable *, gboolean);
Elliot Lee's avatar
Elliot Lee committed
42

43
static gboolean  non_gui_incremental = FALSE;
44 45

#define PENCIL_INCREMENTAL_DEFAULT FALSE
46 47

/*  functions  */
Elliot Lee's avatar
Elliot Lee committed
48 49

void *
50 51 52
pencil_paint_func (PaintCore    *paint_core,
		   GimpDrawable *drawable,
		   int           state)
Elliot Lee's avatar
Elliot Lee committed
53 54 55 56 57 58 59
{
  switch (state)
    {
    case INIT_PAINT :
      break;

    case MOTION_PAINT :
60
      pencil_motion (paint_core, drawable, pencil_options->incremental);
Elliot Lee's avatar
Elliot Lee committed
61 62 63 64 65 66 67 68 69 70 71 72
      break;

    case FINISH_PAINT :
      break;

    default :
      break;
    }

  return NULL;
}

73 74 75
void
pencil_options_reset (void)
{
76
  paint_options_reset (pencil_options);
77 78
}

Elliot Lee's avatar
Elliot Lee committed
79
Tool *
80
tools_new_pencil (void)
Elliot Lee's avatar
Elliot Lee committed
81 82 83 84
{
  Tool * tool;
  PaintCore * private;

85
  /*  The tool options  */
Elliot Lee's avatar
Elliot Lee committed
86
  if (!pencil_options)
87
    {
88 89
      pencil_options = paint_options_new (PENCIL, pencil_options_reset);
      tools_register (PENCIL, (ToolOptions *) pencil_options);
90
    }
Elliot Lee's avatar
Elliot Lee committed
91 92 93 94 95

  tool = paint_core_new (PENCIL);

  private = (PaintCore *) tool->private;
  private->paint_func = pencil_paint_func;
96
  private->pick_colors = TRUE;
97
  private->flags |= TOOL_CAN_HANDLE_CHANGING_BRUSH;
Elliot Lee's avatar
Elliot Lee committed
98 99 100 101 102

  return tool;
}

void
103
tools_free_pencil (Tool *tool)
Elliot Lee's avatar
Elliot Lee committed
104 105 106 107
{
  paint_core_free (tool);
}

108
static void
109
pencil_motion (PaintCore    *paint_core,
110
	       GimpDrawable *drawable,
111
	       gboolean	     increment)
Elliot Lee's avatar
Elliot Lee committed
112 113 114 115
{
  GImage *gimage;
  TempBuf * area;
  unsigned char col[MAX_CHANNELS];
116
  gint opacity;
117
  PaintApplicationMode paint_appl_mode = increment ? INCREMENTAL : CONSTANT;
Elliot Lee's avatar
Elliot Lee committed
118

119
  if (! (gimage = drawable_gimage (drawable)))
Elliot Lee's avatar
Elliot Lee committed
120 121 122
    return;

  /*  Get a region which can be used to paint to  */
123
  if (! (area = paint_core_get_paint_area (paint_core, drawable)))
Elliot Lee's avatar
Elliot Lee committed
124 125
    return;

126
  if (GIMP_IS_BRUSH_PIXMAP (paint_core->brush))
127 128
    {
      /* if its a pixmap, do pixmap stuff */
129
      color_area_with_pixmap (paint_core, gimage, drawable, area);
130
      paint_appl_mode = INCREMENTAL;
131 132 133 134
    }
  else
    {
      /*  color the pixels  */
135 136
      gimage_get_foreground (gimage, drawable, col);
      col[area->bytes - 1] = OPAQUE_OPACITY;
137 138 139
      color_pixels (temp_buf_data (area), col,
		    area->width * area->height, area->bytes);
    }
Elliot Lee's avatar
Elliot Lee committed
140

141 142 143 144 145 146 147 148
  /*Make the opacity dependent on the current pressure 
    This makes a more natural pencil since light pressure
    on a graphite pen will give transparent line */

  opacity = 255 * gimp_context_get_opacity (NULL) * (paint_core->curpressure / 0.5);
  if (opacity > 255)
    opacity = 255; 

149 150


Elliot Lee's avatar
Elliot Lee committed
151
  /*  paste the newly painted canvas to the gimage which is being worked on  */
152
  paint_core_paste_canvas (paint_core, drawable, opacity,
153 154
			   (int) (gimp_context_get_opacity (NULL) * 255),
			   gimp_context_get_paint_mode (NULL),
155
			   HARD, paint_appl_mode);
Elliot Lee's avatar
Elliot Lee committed
156 157 158
}

static void *
159
pencil_non_gui_paint_func (PaintCore    *paint_core,
160 161
			   GimpDrawable *drawable,
			   int           state)
Elliot Lee's avatar
Elliot Lee committed
162
{
163
  pencil_motion (paint_core, drawable, non_gui_incremental);
Elliot Lee's avatar
Elliot Lee committed
164 165 166 167 168

  return NULL;
}


169 170
gboolean
pencil_non_gui (GimpDrawable *drawable,
171 172
		int           num_strokes,
		double       *stroke_array)
Elliot Lee's avatar
Elliot Lee committed
173 174 175
{
  int i;

176
  if (paint_core_init (&non_gui_paint_core, drawable,
177
		       stroke_array[0], stroke_array[1]))
Elliot Lee's avatar
Elliot Lee committed
178
    {
179
      /* Set the paint core's paint func */
Elliot Lee's avatar
Elliot Lee committed
180 181 182 183 184
      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];

185
      pencil_non_gui_paint_func (&non_gui_paint_core, drawable, 0);
Elliot Lee's avatar
Elliot Lee committed
186 187

      for (i = 1; i < num_strokes; i++)
188 189 190
       {
         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
191

192
         paint_core_interpolate (&non_gui_paint_core, drawable);
Elliot Lee's avatar
Elliot Lee committed
193

194 195 196
         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
197

198
      /* Finish the painting */
199
      paint_core_finish (&non_gui_paint_core, drawable, -1);
Elliot Lee's avatar
Elliot Lee committed
200

201
      /* Cleanup */
Elliot Lee's avatar
Elliot Lee committed
202
      paint_core_cleanup ();
203
      return TRUE;
Elliot Lee's avatar
Elliot Lee committed
204
    }
205 206
  else
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
207
}