~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/common/gradient-map.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: james.westby@ubuntu.com-20081006133041-3panbkcanaymfsmp
Tags: upstream-2.6.0
ImportĀ upstreamĀ versionĀ 2.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * Gradient Map plug-in
 
5
 * Copyright (C) 1997 Eiichi Takamori <taka@ma1.seikyou.ne.jp>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
#include <string.h>
 
24
 
 
25
#include <libgimp/gimp.h>
 
26
 
 
27
#include "libgimp/stdplugins-intl.h"
 
28
 
 
29
 
 
30
/* Some useful macros */
 
31
 
 
32
#define GRADMAP_PROC    "plug-in-gradmap"
 
33
#define PALETTEMAP_PROC "plug-in-palettemap"
 
34
#define PLUG_IN_BINARY  "gradient-map"
 
35
#define NSAMPLES        256
 
36
#define LUMINOSITY(X)   (GIMP_RGB_LUMINANCE (X[0], X[1], X[2]) + 0.5)
 
37
 
 
38
typedef enum
 
39
  {
 
40
    GRADIENT_MODE = 1,
 
41
    PALETTE_MODE
 
42
  } MapMode;
 
43
 
 
44
/* Declare a local function.
 
45
 */
 
46
static void     query       (void);
 
47
static void     run         (const gchar      *name,
 
48
                             gint              nparams,
 
49
                             const GimpParam  *param,
 
50
                             gint             *nreturn_vals,
 
51
                             GimpParam       **return_vals);
 
52
 
 
53
static void     map                  (GimpDrawable     *drawable,
 
54
                                      MapMode           mode);
 
55
static guchar * get_samples_gradient (GimpDrawable     *drawable);
 
56
static guchar * get_samples_palette  (GimpDrawable     *drawable);
 
57
static void     map_func             (const guchar     *src,
 
58
                                      guchar           *dest,
 
59
                                      gint              bpp,
 
60
                                      gpointer          data);
 
61
 
 
62
 
 
63
const GimpPlugInInfo PLUG_IN_INFO =
 
64
{
 
65
  NULL,  /* init_proc  */
 
66
  NULL,  /* quit_proc  */
 
67
  query, /* query_proc */
 
68
  run,   /* run_proc   */
 
69
};
 
70
 
 
71
MAIN ()
 
72
 
 
73
static void
 
74
query (void)
 
75
{
 
76
  static const GimpParamDef args[]=
 
77
  {
 
78
    { GIMP_PDB_INT32,    "run-mode", "Interactive, non-interactive" },
 
79
    { GIMP_PDB_IMAGE,    "image",    "Input image (unused)"         },
 
80
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable"               }
 
81
  };
 
82
 
 
83
  gimp_install_procedure (GRADMAP_PROC,
 
84
                          N_("Recolor the image using colors from the active gradient"),
 
85
                          "This plug-in maps the contents of the specified "
 
86
                          "drawable with active gradient. It calculates "
 
87
                          "luminosity of each pixel and replaces the pixel "
 
88
                          "by the sample of active gradient at the position "
 
89
                          "proportional to that luminosity. Complete black "
 
90
                          "pixel becomes the leftmost color of the gradient, "
 
91
                          "and complete white becomes the rightmost. Works on "
 
92
                          "both Grayscale and RGB image with/without alpha "
 
93
                          "channel.",
 
94
                          "Eiichi Takamori",
 
95
                          "Eiichi Takamori",
 
96
                          "1997",
 
97
                          N_("_Gradient Map"),
 
98
                          "RGB*, GRAY*",
 
99
                          GIMP_PLUGIN,
 
100
                          G_N_ELEMENTS (args), 0,
 
101
                          args, NULL);
 
102
 
 
103
  gimp_plugin_menu_register (GRADMAP_PROC, "<Image>/Colors/Map");
 
104
 
 
105
  gimp_install_procedure (PALETTEMAP_PROC,
 
106
                          N_("Recolor the image using colors from the active palette"),
 
107
                          "This plug-in maps the contents of the specified "
 
108
                          "drawable with the active palette. It calculates "
 
109
                          "luminosity of each pixel and replaces the pixel "
 
110
                          "by the palette sample  at the corresponding "
 
111
                          "index. A complete black "
 
112
                          "pixel becomes the lowest palette entry, "
 
113
                          "and complete white becomes the highest. Works on "
 
114
                          "both Grayscale and RGB image with/without alpha "
 
115
                          "channel.",
 
116
                          "Bill Skaggs",
 
117
                          "Bill Skaggs",
 
118
                          "2004",
 
119
                          N_("_Palette Map"),
 
120
                          "RGB*, GRAY*",
 
121
                          GIMP_PLUGIN,
 
122
                          G_N_ELEMENTS (args), 0,
 
123
                          args, NULL);
 
124
 
 
125
  gimp_plugin_menu_register (PALETTEMAP_PROC, "<Image>/Colors/Map");
 
126
}
 
127
 
 
128
static void
 
129
run (const gchar      *name,
 
130
     gint              nparams,
 
131
     const GimpParam  *param,
 
132
     gint             *nreturn_vals,
 
133
     GimpParam       **return_vals)
 
134
{
 
135
  static GimpParam   values[1];
 
136
  GimpDrawable      *drawable;
 
137
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
 
138
  GimpRunMode        run_mode;
 
139
 
 
140
  run_mode = param[0].data.d_int32;
 
141
 
 
142
  INIT_I18N();
 
143
 
 
144
  *nreturn_vals = 1;
 
145
  *return_vals  = values;
 
146
 
 
147
  values[0].type          = GIMP_PDB_STATUS;
 
148
  values[0].data.d_status = status;
 
149
 
 
150
  /*  Get the specified drawable  */
 
151
  drawable = gimp_drawable_get (param[2].data.d_drawable);
 
152
 
 
153
  /*  Make sure that the drawable is gray or RGB color  */
 
154
  if (gimp_drawable_is_rgb (drawable->drawable_id) ||
 
155
      gimp_drawable_is_gray (drawable->drawable_id))
 
156
    {
 
157
      MapMode mode = 0;
 
158
 
 
159
      if ( !strcmp (name, GRADMAP_PROC))
 
160
        {
 
161
          mode = GRADIENT_MODE;
 
162
          gimp_progress_init (_("Gradient Map"));
 
163
        }
 
164
      else if ( !strcmp (name, PALETTEMAP_PROC))
 
165
        {
 
166
          mode = PALETTE_MODE;
 
167
          gimp_progress_init (_("Palette Map"));
 
168
        }
 
169
      else
 
170
        {
 
171
          status = GIMP_PDB_CALLING_ERROR;
 
172
        }
 
173
 
 
174
      if (status == GIMP_PDB_SUCCESS)
 
175
        {
 
176
          if (mode)
 
177
            map (drawable, mode);
 
178
 
 
179
          if (run_mode != GIMP_RUN_NONINTERACTIVE)
 
180
            gimp_displays_flush ();
 
181
        }
 
182
    }
 
183
  else
 
184
    {
 
185
      status = GIMP_PDB_EXECUTION_ERROR;
 
186
    }
 
187
 
 
188
  values[0].data.d_status = status;
 
189
 
 
190
  gimp_drawable_detach (drawable);
 
191
}
 
192
 
 
193
typedef struct
 
194
{
 
195
  guchar   *samples;
 
196
  gboolean  is_rgb;
 
197
  gboolean  has_alpha;
 
198
  MapMode   mode;
 
199
} MapParam;
 
200
 
 
201
static void
 
202
map_func (const guchar *src,
 
203
          guchar       *dest,
 
204
          gint          bpp,
 
205
          gpointer      data)
 
206
{
 
207
  MapParam *param = data;
 
208
  gint      lum;
 
209
  gint      b;
 
210
  guchar   *samp;
 
211
 
 
212
  lum = (param->is_rgb) ? LUMINOSITY (src) : src[0];
 
213
  samp = &param->samples[lum * bpp];
 
214
 
 
215
  if (param->has_alpha)
 
216
    {
 
217
      for (b = 0; b < bpp - 1; b++)
 
218
        dest[b] = samp[b];
 
219
      dest[b] = ((guint)samp[b] * (guint)src[b]) / 255;
 
220
    }
 
221
  else
 
222
    {
 
223
      for (b = 0; b < bpp; b++)
 
224
        dest[b] = samp[b];
 
225
    }
 
226
}
 
227
 
 
228
static void
 
229
map (GimpDrawable *drawable,
 
230
     MapMode       mode)
 
231
{
 
232
  MapParam param;
 
233
 
 
234
  param.is_rgb = gimp_drawable_is_rgb (drawable->drawable_id);
 
235
  param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
 
236
 
 
237
  switch (mode)
 
238
    {
 
239
    case GRADIENT_MODE:
 
240
      param.samples = get_samples_gradient (drawable);
 
241
      break;
 
242
    case PALETTE_MODE:
 
243
      param.samples = get_samples_palette (drawable);
 
244
      break;
 
245
    default:
 
246
      g_error ("plug_in_gradmap: invalid mode");
 
247
    }
 
248
 
 
249
  gimp_rgn_iterate2 (drawable, 0 /* unused */, map_func, &param);
 
250
}
 
251
 
 
252
/*
 
253
  Returns 256 samples of active gradient.
 
254
  Each sample has (gimp_drawable_bpp (drawable->drawable_id)) bytes.
 
255
 */
 
256
static guchar *
 
257
get_samples_gradient (GimpDrawable *drawable)
 
258
{
 
259
  gchar   *gradient_name;
 
260
  gint     n_f_samples;
 
261
  gdouble *f_samples, *f_samp;  /* float samples */
 
262
  guchar  *byte_samples, *b_samp;  /* byte samples */
 
263
  gint     bpp, color, has_alpha, alpha;
 
264
  gint     i, j;
 
265
 
 
266
  gradient_name = gimp_context_get_gradient ();
 
267
 
 
268
#ifdef __GNUC__
 
269
#warning FIXME: "reverse" hardcoded to FALSE.
 
270
#endif
 
271
  gimp_gradient_get_uniform_samples (gradient_name, NSAMPLES, FALSE,
 
272
                                     &n_f_samples, &f_samples);
 
273
 
 
274
  bpp       = gimp_drawable_bpp (drawable->drawable_id);
 
275
  color     = gimp_drawable_is_rgb (drawable->drawable_id);
 
276
  has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
 
277
  alpha     = (has_alpha ? bpp - 1 : bpp);
 
278
 
 
279
  byte_samples = g_new (guchar, NSAMPLES * bpp);
 
280
 
 
281
  for (i = 0; i < NSAMPLES; i++)
 
282
    {
 
283
      b_samp = &byte_samples[i * bpp];
 
284
      f_samp = &f_samples[i * 4];
 
285
      if (color)
 
286
        for (j = 0; j < 3; j++)
 
287
          b_samp[j] = f_samp[j] * 255;
 
288
      else
 
289
        b_samp[0] = LUMINOSITY (f_samp) * 255;
 
290
 
 
291
      if (has_alpha)
 
292
        b_samp[alpha] = f_samp[3] * 255;
 
293
    }
 
294
 
 
295
  g_free (f_samples);
 
296
 
 
297
  return byte_samples;
 
298
}
 
299
 
 
300
/*
 
301
  Returns 256 samples of the palette.
 
302
  Each sample has (gimp_drawable_bpp (drawable->drawable_id)) bytes.
 
303
 */
 
304
static guchar *
 
305
get_samples_palette (GimpDrawable *drawable)
 
306
{
 
307
  gchar   *palette_name;
 
308
  GimpRGB  color_sample;
 
309
  guchar  *byte_samples;
 
310
  guchar  *b_samp;
 
311
  gint     bpp, color, has_alpha, alpha;
 
312
  gint     i;
 
313
  gint     num_colors;
 
314
  gfloat   factor;
 
315
  gint     pal_entry;
 
316
 
 
317
  palette_name = gimp_context_get_palette ();
 
318
  gimp_palette_get_info (palette_name, &num_colors);
 
319
 
 
320
  bpp       = gimp_drawable_bpp (drawable->drawable_id);
 
321
  color     = gimp_drawable_is_rgb (drawable->drawable_id);
 
322
  has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
 
323
  alpha     = (has_alpha ? bpp - 1 : bpp);
 
324
 
 
325
  byte_samples = g_new (guchar, NSAMPLES * bpp);
 
326
  factor = ( (float) num_colors) / NSAMPLES;
 
327
 
 
328
  for (i = 0; i < NSAMPLES; i++)
 
329
    {
 
330
      b_samp = &byte_samples[i * bpp];
 
331
 
 
332
      pal_entry = CLAMP( (int)(i * factor), 0, num_colors);
 
333
      gimp_palette_entry_get_color (palette_name, pal_entry, &color_sample);
 
334
 
 
335
      if (color)
 
336
        gimp_rgb_get_uchar (&color_sample,
 
337
                            b_samp, b_samp + 1, b_samp + 2);
 
338
      else
 
339
        *b_samp = gimp_rgb_luminance_uchar (&color_sample);
 
340
 
 
341
      if (has_alpha)
 
342
        b_samp[alpha] = 255;
 
343
 
 
344
    }
 
345
 
 
346
  return byte_samples;
 
347
}