colorify.c 9.92 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2
3
4
5
6
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * Colorify. Changes the pixel's luminosity to a specified color
 * Copyright (C) 1997 Francisco Bustamante
 *
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 3 of the License, or
10
11
12
13
14
15
16
17
 * (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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
 */

21
22
#include "config.h"

23
24
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
25

26
27
#include "libgimp/stdplugins-intl.h"

28

29
30
#define PLUG_IN_PROC    "plug-in-colorify"
#define PLUG_IN_BINARY  "colorify"
31
#define PLUG_IN_ROLE    "gimp-colorify"
32
33
#define PLUG_IN_VERSION "1.1"

Sven Neumann's avatar
Sven Neumann committed
34
35
#define COLOR_SIZE 30

36
static void      query (void);
37
static void      run   (const gchar      *name,
David Odin's avatar
David Odin committed
38
39
40
41
42
43
44
45
                        gint              nparams,
                        const GimpParam  *param,
                        gint             *nreturn_vals,
                        GimpParam       **return_vals);

static void      colorify                  (GimpDrawable *drawable,
                                            GimpPreview  *preview);
static gboolean  colorify_dialog           (GimpDrawable *drawable);
46
static void      predefined_color_callback (GtkWidget    *widget,
David Odin's avatar
David Odin committed
47
                                            gpointer      data);
48
49
50

typedef struct
{
Sven Neumann's avatar
Sven Neumann committed
51
  GimpRGB  color;
52
53
54
55
} ColorifyVals;

static ColorifyVals cvals =
{
56
  { 1.0, 1.0, 1.0, 1.0 }
57
58
};

59
static GimpRGB button_color[] =
60
{
61
62
63
64
65
66
67
  { 1.0, 0.0, 0.0, 1.0 },
  { 1.0, 1.0, 0.0, 1.0 },
  { 0.0, 1.0, 0.0, 1.0 },
  { 0.0, 1.0, 1.0, 1.0 },
  { 0.0, 0.0, 1.0, 1.0 },
  { 1.0, 0.0, 1.0, 1.0 },
  { 1.0, 1.0, 1.0, 1.0 },
68
69
};

70
const GimpPlugInInfo PLUG_IN_INFO =
71
{
72
73
74
75
  NULL,
  NULL,
  query,
  run,
76
77
};

78
static GtkWidget *custom_color_button = NULL;
79
80
81
82
83
84
85
86

static gint lum_red_lookup[256];
static gint lum_green_lookup[256];
static gint lum_blue_lookup[256];
static gint final_red_lookup[256];
static gint final_green_lookup[256];
static gint final_blue_lookup[256];

87
88
89
90

MAIN ()

static void
91
query (void)
92
{
93
  static const GimpParamDef args[] =
94
  {
95
    { GIMP_PDB_INT32,    "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
96
97
98
    { GIMP_PDB_IMAGE,    "image",    "Input image"    },
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
    { GIMP_PDB_COLOR,    "color",    "Color to apply" }
99
100
  };

101
  gimp_install_procedure (PLUG_IN_PROC,
102
                          N_("Replace all colors with shades of a specified color"),
David Odin's avatar
David Odin committed
103
104
105
106
                          "Makes an average of the RGB channels and uses it "
                          "to set the color",
                          "Francisco Bustamante",
                          "Francisco Bustamante",
107
                          PLUG_IN_VERSION,
108
                          N_("Colorif_y..."),
David Odin's avatar
David Odin committed
109
110
111
112
                          "RGB*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (args), 0,
                          args, NULL);
113

114
  gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Colors/Modify");
115
116
117
}

static void
118
119
120
121
122
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
123
{
Sven Neumann's avatar
Sven Neumann committed
124
125
126
  GimpPDBStatusType  status;
  static GimpParam   values[1];
  GimpDrawable      *drawable;
127
  GimpRunMode        run_mode;
128

129
  INIT_I18N ();
130

Sven Neumann's avatar
Sven Neumann committed
131
  status = GIMP_PDB_SUCCESS;
132
  run_mode = param[0].data.d_int32;
133

Sven Neumann's avatar
Sven Neumann committed
134
  values[0].type = GIMP_PDB_STATUS;
135
  values[0].data.d_status = status;
136

137
138
  *nreturn_vals = 1;
  *return_vals = values;
139

140
  drawable = gimp_drawable_get (param[2].data.d_drawable);
141

142
143
  switch (run_mode)
    {
Sven Neumann's avatar
Sven Neumann committed
144
    case GIMP_RUN_INTERACTIVE:
145
      gimp_get_data (PLUG_IN_PROC, &cvals);
David Odin's avatar
David Odin committed
146
147
      if (!colorify_dialog (drawable))
        return;
148
      break;
149

Sven Neumann's avatar
Sven Neumann committed
150
    case GIMP_RUN_NONINTERACTIVE:
151
      if (nparams != 4)
David Odin's avatar
David Odin committed
152
        status = GIMP_PDB_CALLING_ERROR;
153

Sven Neumann's avatar
Sven Neumann committed
154
      if (status == GIMP_PDB_SUCCESS)
David Odin's avatar
David Odin committed
155
        cvals.color = param[3].data.d_color;
156
157
      break;

Sven Neumann's avatar
Sven Neumann committed
158
    case GIMP_RUN_WITH_LAST_VALS:
159
      /*  Possibly retrieve data  */
160
      gimp_get_data (PLUG_IN_PROC, &cvals);
161
162
163
164
165
      break;

    default:
      break;
    }
166

Sven Neumann's avatar
Sven Neumann committed
167
  if (status == GIMP_PDB_SUCCESS)
168
    {
169
      gimp_progress_init (_("Colorifying"));
170

David Odin's avatar
David Odin committed
171
      colorify (drawable, NULL);
172

Sven Neumann's avatar
Sven Neumann committed
173
      if (run_mode == GIMP_RUN_INTERACTIVE)
174
        gimp_set_data (PLUG_IN_PROC, &cvals, sizeof (ColorifyVals));
175

Sven Neumann's avatar
Sven Neumann committed
176
      if (run_mode != GIMP_RUN_NONINTERACTIVE)
177
        gimp_displays_flush ();
178
    }
179

180
181
  gimp_drawable_detach (drawable);

182
  values[0].data.d_status = status;
183
184
}

185
static void
186
187
188
189
colorify_func (const guchar *src,
               guchar       *dest,
               gint          bpp,
               gpointer      data)
190
{
191
192
  gint lum;

David Odin's avatar
David Odin committed
193
194
195
  lum = lum_red_lookup[src[0]]   +
        lum_green_lookup[src[1]] +
        lum_blue_lookup[src[2]];
196

197
198
199
  dest[0] = final_red_lookup[lum];
  dest[1] = final_green_lookup[lum];
  dest[2] = final_blue_lookup[lum];
200

201
202
203
204
  if (bpp == 4)
    dest[3] = src[3];
}

205
static void
David Odin's avatar
David Odin committed
206
207
colorify (GimpDrawable *drawable,
          GimpPreview  *preview)
208
{
209
  gint  i;
210
211
212

  for (i = 0; i < 256; i ++)
    {
213
214
215
      lum_red_lookup[i]     = i * GIMP_RGB_LUMINANCE_RED;
      lum_green_lookup[i]   = i * GIMP_RGB_LUMINANCE_GREEN;
      lum_blue_lookup[i]    = i * GIMP_RGB_LUMINANCE_BLUE;
Sven Neumann's avatar
Sven Neumann committed
216
217
218
      final_red_lookup[i]   = i * cvals.color.r;
      final_green_lookup[i] = i * cvals.color.g;
      final_blue_lookup[i]  = i * cvals.color.b;
219
220
    }

David Odin's avatar
David Odin committed
221
222
223
224
225
  if (preview)
    {
      gint    width, height, bytes;
      guchar *src;

226
      src = gimp_zoom_preview_get_source (GIMP_ZOOM_PREVIEW (preview),
227
                                        &width, &height, &bytes);
David Odin's avatar
David Odin committed
228
229
230
231
232
233
234
235
236
237
      for (i = 0; i < width * height; i++)
        colorify_func (src + i * bytes, src + i * bytes, bytes, NULL);

      gimp_preview_draw_buffer (preview, src, width * bytes);
      g_free (src);
    }
  else
    {
      gimp_rgn_iterate2 (drawable, 0 /* unused */, colorify_func, NULL);
    }
238
239
}

Sven Neumann's avatar
Sven Neumann committed
240
static gboolean
David Odin's avatar
David Odin committed
241
colorify_dialog (GimpDrawable *drawable)
242
{
243
  GtkWidget *dialog;
David Odin's avatar
David Odin committed
244
245
  GtkWidget *main_vbox;
  GtkWidget *preview;
246
247
248
  GtkWidget *label;
  GtkWidget *button;
  GtkWidget *table;
249
  GtkWidget *color_area;
Sven Neumann's avatar
Sven Neumann committed
250
  gint       i;
251
  gboolean   run;
252

253
  gimp_ui_init (PLUG_IN_BINARY, TRUE);
254

255
  dialog = gimp_dialog_new (_("Colorify"), PLUG_IN_ROLE,
256
                            NULL, 0,
257
                            gimp_standard_help_func, PLUG_IN_PROC,
David Odin's avatar
David Odin committed
258
259
260
261
262

                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,

                            NULL);
263

264
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
265
266
267
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
268

269
  gimp_window_set_transient (GTK_WINDOW (dialog));
270

Michael Natterer's avatar
Michael Natterer committed
271
  main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
David Odin's avatar
David Odin committed
272
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
273
274
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                      main_vbox, TRUE, TRUE, 0);
David Odin's avatar
David Odin committed
275
  gtk_widget_show (main_vbox);
Sven Neumann's avatar
Sven Neumann committed
276

277
  preview = gimp_zoom_preview_new (drawable);
David Odin's avatar
David Odin committed
278
279
280
281
282
  gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
  gtk_widget_show (preview);
  g_signal_connect_swapped (preview, "invalidated",
                            G_CALLBACK (colorify),
                            drawable);
283

284
  table = gtk_table_new (2, 7, TRUE);
285
286
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
David Odin's avatar
David Odin committed
287
  gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
288
289
  gtk_widget_show (table);

290
  label = gtk_label_new (_("Custom color:"));
291
  gtk_table_attach (GTK_TABLE (table), label, 4, 6, 0, 1,
David Odin's avatar
David Odin committed
292
                    GTK_FILL, GTK_FILL, 0, 0);
293
294
  gtk_widget_show (label);

295
  custom_color_button = gimp_color_button_new (_("Colorify Custom Color"),
David Odin's avatar
David Odin committed
296
297
298
                                               COLOR_SIZE, COLOR_SIZE,
                                               &cvals.color,
                                               GIMP_COLOR_AREA_FLAT);
299
  g_signal_connect (custom_color_button, "color-changed",
300
                    G_CALLBACK (gimp_color_button_get_color),
David Odin's avatar
David Odin committed
301
                    &cvals.color);
302
  g_signal_connect_swapped (custom_color_button, "color-changed",
David Odin's avatar
David Odin committed
303
304
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);
305

306
  gtk_table_attach (GTK_TABLE (table), custom_color_button, 6, 7, 0, 1,
David Odin's avatar
David Odin committed
307
                    GTK_FILL, GTK_FILL, 0, 0);
308
  gtk_widget_show (custom_color_button);
Sven Neumann's avatar
Sven Neumann committed
309

310
311
  for (i = 0; i < 7; i++)
    {
Sven Neumann's avatar
Sven Neumann committed
312
      button = gtk_button_new ();
313
      color_area = gimp_color_area_new (&button_color[i],
David Odin's avatar
David Odin committed
314
315
                                        GIMP_COLOR_AREA_FLAT,
                                        GDK_BUTTON2_MASK);
316
      gtk_widget_set_size_request (GTK_WIDGET (color_area),
David Odin's avatar
David Odin committed
317
                                   COLOR_SIZE, COLOR_SIZE);
318
      gtk_container_add (GTK_CONTAINER (button), color_area);
319
      g_signal_connect (button, "clicked",
David Odin's avatar
David Odin committed
320
                        G_CALLBACK (predefined_color_callback),
321
                        &button_color[i]);
322
      gtk_widget_show (color_area);
323
324

      gtk_table_attach (GTK_TABLE (table), button, i, i + 1, 1, 2,
David Odin's avatar
David Odin committed
325
                        GTK_FILL, GTK_FILL, 0, 0);
326
327
328
329
330
      gtk_widget_show (button);
    }

  gtk_widget_show (dialog);

331
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
332

333
  gtk_widget_destroy (dialog);
334

335
  return run;
336
337
338
339
}

static void
predefined_color_callback (GtkWidget *widget,
David Odin's avatar
David Odin committed
340
                           gpointer   data)
341
{
342
  gimp_color_button_set_color (GIMP_COLOR_BUTTON (custom_color_button),
David Odin's avatar
David Odin committed
343
                               (GimpRGB *) data);
344
}