pencil.c 5.49 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 "gimpbrushpixmap.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
#include "pencil.h"
30 31
/* for color_area_with_pixmap */
#include "pixmapbrush.h"
Elliot Lee's avatar
Elliot Lee committed
32 33 34
#include "selection.h"
#include "tools.h"

35 36
#include "libgimp/gimpintl.h"

37 38

/*  the pencil tool options  */
39

40 41 42 43 44 45 46 47 48 49 50
typedef struct _PencilOptions PencilOptions;
struct _PencilOptions
{
  PaintOptions paint_options;

  gboolean      incremental;
  gboolean      incremental_d;
  GtkWidget    *incremental_w;
};

static PencilOptions *pencil_options = NULL; 
51

Elliot Lee's avatar
Elliot Lee committed
52
/*  forward function declarations  */
53
static void         pencil_motion   (PaintCore *, GimpDrawable *, gboolean);
Elliot Lee's avatar
Elliot Lee committed
54

55 56 57
static gboolean     non_gui_incremental = FALSE;

#define PENCIL_INCREMENTAL_DEFAULT FALSE
58 59

/*  functions  */
Elliot Lee's avatar
Elliot Lee committed
60 61

void *
62 63 64
pencil_paint_func (PaintCore    *paint_core,
		   GimpDrawable *drawable,
		   int           state)
Elliot Lee's avatar
Elliot Lee committed
65 66 67 68 69 70 71
{
  switch (state)
    {
    case INIT_PAINT :
      break;

    case MOTION_PAINT :
72
      pencil_motion (paint_core, drawable, pencil_options->incremental);
Elliot Lee's avatar
Elliot Lee committed
73 74 75 76 77 78 79 80 81 82 83 84
      break;

    case FINISH_PAINT :
      break;

    default :
      break;
    }

  return NULL;
}

85 86 87
void
pencil_options_reset (void)
{
88
  paint_options_reset (&pencil_options->paint_options);
89 90
}

Elliot Lee's avatar
Elliot Lee committed
91
Tool *
92
tools_new_pencil (void)
Elliot Lee's avatar
Elliot Lee committed
93 94 95 96
{
  Tool * tool;
  PaintCore * private;

97
  /*  The tool options  */
Elliot Lee's avatar
Elliot Lee committed
98
  if (!pencil_options)
99
    {
100 101
      pencil_options = paint_options_new (PENCIL, pencil_options_reset);
      tools_register (PENCIL, (ToolOptions *) pencil_options);
102
    }
Elliot Lee's avatar
Elliot Lee committed
103 104 105 106 107

  tool = paint_core_new (PENCIL);

  private = (PaintCore *) tool->private;
  private->paint_func = pencil_paint_func;
108
  private->pick_colors = TRUE;
Elliot Lee's avatar
Elliot Lee committed
109 110 111 112 113

  return tool;
}

void
114
tools_free_pencil (Tool *tool)
Elliot Lee's avatar
Elliot Lee committed
115 116 117 118
{
  paint_core_free (tool);
}

119
static void
120
pencil_motion (PaintCore    *paint_core,
121
	       GimpDrawable *drawable,
122
	       gboolean	     increment)
Elliot Lee's avatar
Elliot Lee committed
123 124 125 126
{
  GImage *gimage;
  TempBuf * area;
  unsigned char col[MAX_CHANNELS];
127
  gint opacity;
128
  PaintApplicationMode paint_appl_mode = increment ? INCREMENTAL : CONSTANT;
Elliot Lee's avatar
Elliot Lee committed
129

130
  if (! (gimage = drawable_gimage (drawable)))
Elliot Lee's avatar
Elliot Lee committed
131 132
    return;

133
  gimage_get_foreground (gimage, drawable, col);
Elliot Lee's avatar
Elliot Lee committed
134 135

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

  /*  set the alpha channel  */
Manish Singh's avatar
Manish Singh committed
140
  col[area->bytes - 1] = OPAQUE_OPACITY;
Elliot Lee's avatar
Elliot Lee committed
141

142

143
  if (GIMP_IS_BRUSH_PIXMAP (paint_core->brush))
144 145
    {
      /* if its a pixmap, do pixmap stuff */
146 147
      color_area_with_pixmap (gimage, drawable, area, paint_core->brush);
      paint_appl_mode = INCREMENTAL;
148 149 150 151 152 153 154
    }
  else
    {
      /*  color the pixels  */
      color_pixels (temp_buf_data (area), col,
		    area->width * area->height, area->bytes);
    }
Elliot Lee's avatar
Elliot Lee committed
155

156 157 158 159 160 161 162 163
  /*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; 

164 165


Elliot Lee's avatar
Elliot Lee committed
166
  /*  paste the newly painted canvas to the gimage which is being worked on  */
167
  paint_core_paste_canvas (paint_core, drawable, opacity,
168 169
			   (int) (gimp_context_get_opacity (NULL) * 255),
			   gimp_context_get_paint_mode (NULL),
170
			   HARD, paint_appl_mode);
Elliot Lee's avatar
Elliot Lee committed
171 172 173
}

static void *
174
pencil_non_gui_paint_func (PaintCore    *paint_core,
175 176
                          GimpDrawable *drawable,
                          int           state)
Elliot Lee's avatar
Elliot Lee committed
177
{
178
  pencil_motion (paint_core, drawable, non_gui_incremental );
Elliot Lee's avatar
Elliot Lee committed
179 180 181 182 183

  return NULL;
}


184 185
gboolean
pencil_non_gui (GimpDrawable *drawable,
186 187
               int           num_strokes,
               double       *stroke_array)
Elliot Lee's avatar
Elliot Lee committed
188 189 190
{
  int i;

191
  if (paint_core_init (&non_gui_paint_core, drawable,
192
		       stroke_array[0], stroke_array[1]))
Elliot Lee's avatar
Elliot Lee committed
193
    {
194
      /* Set the paint core's paint func */
Elliot Lee's avatar
Elliot Lee committed
195 196 197 198 199
      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];

200
      pencil_non_gui_paint_func (&non_gui_paint_core, drawable, 0);
Elliot Lee's avatar
Elliot Lee committed
201 202

      for (i = 1; i < num_strokes; i++)
203 204 205
       {
         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
206

207
         paint_core_interpolate (&non_gui_paint_core, drawable);
Elliot Lee's avatar
Elliot Lee committed
208

209 210 211
         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
212

213
      /* Finish the painting */
214
      paint_core_finish (&non_gui_paint_core, drawable, -1);
Elliot Lee's avatar
Elliot Lee committed
215

216
      /* Cleanup */
Elliot Lee's avatar
Elliot Lee committed
217
      paint_core_cleanup ();
218
      return TRUE;
Elliot Lee's avatar
Elliot Lee committed
219
    }
220 221
  else
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
222
}