exchange.c 15.6 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
/*
 * This is a plug-in for the GIMP.
 *
 * 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
23
24
25
26
27
28
 *
 *
 */

/*
 * Exchange one color with the other (settable threshold to convert from
 * one color-shade to another...might do wonders on certain images, or be
 * totally useless on others).
 * 
 * Author: robert@experimental.net
 * 
 * TODO:
29
 *		- locken van scales met elkaar
Elliot Lee's avatar
Elliot Lee committed
30
31
 */

32
33
34
35
36
37
/* 
 * 1999/03/17   Fixed RUN_NONINTERACTIVE and RUN_WITH_LAST_VALS. 
 *              There were uninitialized variables.
 *                                        --Sven <sven@gimp.org>
 */

38
39
#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
40
41
#include <stdio.h>
#include <stdlib.h>
42
43
44

#include <gtk/gtk.h>

Elliot Lee's avatar
Elliot Lee committed
45
#include "libgimp/gimp.h"
46
47
#include "libgimp/gimpui.h"

48
#include "libgimp/stdplugins-intl.h"
Elliot Lee's avatar
Elliot Lee committed
49
50
51
52

/* big scales */
#define	SCALE_WIDTH	225

53
54
55
/* preview width/height */
#define PREVIEW_SIZE	128

Elliot Lee's avatar
Elliot Lee committed
56
57
58
59
/* datastructure to store parameters in */
typedef struct
{
	guchar	fromred, fromgreen, fromblue, tored, togreen, toblue;
60
	guchar	red_threshold, green_threshold, blue_threshold;
Elliot Lee's avatar
Elliot Lee committed
61
62
63
64
65
66
67
68
	gint32	image;
	gint32	drawable;
}	myParams;

/* lets prototype */
static void	query();
static void	run(char *, int, GParam *, int *, GParam **);
static int	doDialog();
69
70
static void	exchange();
static void	real_exchange(gint, gint, gint, gint, int);
Elliot Lee's avatar
Elliot Lee committed
71
static void	doLabelAndScale(char *, GtkWidget *, guchar *);
72
static void	update_preview();
Elliot Lee's avatar
Elliot Lee committed
73
74

static void	ok_callback(GtkWidget *, gpointer);
75
static void	lock_callback(GtkWidget *, gpointer);
Elliot Lee's avatar
Elliot Lee committed
76
77
78
static void	scale_callback(GtkAdjustment *, gpointer);

/* some global variables */
79
GDrawable	*drw;
Elliot Lee's avatar
Elliot Lee committed
80
81
myParams	xargs = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int		running = 0;
82
83
84
85
86
GPixelRgn	origregion;
GtkWidget	*preview;
gint		sel_x1, sel_y1, sel_x2, sel_y2;
gint		prev_width, prev_height, sel_width, sel_height;
gint		lock_thres = 0;
Elliot Lee's avatar
Elliot Lee committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

/* lets declare what we want to do */
GPlugInInfo PLUG_IN_INFO =
{
	NULL,			       /* init_proc */
	NULL,			       /* quit_proc */
	query,			       /* query_proc */
	run,			       /* run_proc */
};

/* run program */
MAIN()

/* tell GIMP who we are */
static
void	query()
{
	static GParamDef args[] =
	{
		{ PARAM_INT32, "run_mode", "Interactive" },
		{ PARAM_IMAGE, "image", "Input image" },
		{ PARAM_DRAWABLE, "drawable", "Input drawable" },
		{ PARAM_INT8, "fromred", "Red value (from)" },
		{ PARAM_INT8, "fromgreen", "Green value (from)" },
		{ PARAM_INT8, "fromblue", "Blue value (from)" },
		{ PARAM_INT8, "tored", "Red value (to)" },
		{ PARAM_INT8, "togreen", "Green value (to)" },
		{ PARAM_INT8, "toblue", "Blue value (to)" },
115
116
117
		{ PARAM_INT8, "red_threshold", "Red threshold" },
		{ PARAM_INT8, "green_threshold", "Green threshold" },
		{ PARAM_INT8, "blue_threshold", "Blue threshold" },
Elliot Lee's avatar
Elliot Lee committed
118
119
120
121
122
	};
	static GParamDef *return_vals = NULL;
	static int nargs = sizeof(args) / sizeof(args[0]);
	static int nreturn_vals = 0;

123
124
    INIT_I18N();

Elliot Lee's avatar
Elliot Lee committed
125
	gimp_install_procedure("plug_in_exchange",
126
127
			       _("Color Exchange"),
			       _("Exchange one color with another, optionally setting a threshold to convert from one shade to another"),
Elliot Lee's avatar
Elliot Lee committed
128
129
130
			       "robert@experimental.net",
			       "robert@experimental.net",
			       "June 17th, 1997",
131
			       N_("<Image>/Filters/Colors/Map/Color Exchange..."),
Elliot Lee's avatar
Elliot Lee committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
			       "RGB*",
			       PROC_PLUG_IN,
			       nargs, nreturn_vals,
			       args, return_vals);
}

/* main function */
static
void	run(char *name, int nparams, GParam *param, int *nreturn_vals, GParam **return_vals)
{
	static GParam	values[1];
	GRunModeType	runmode;
	GStatusType 	status = STATUS_SUCCESS;

	*nreturn_vals = 1;
	*return_vals = values;

	values[0].type = PARAM_STATUS;
	values[0].data.d_status = status;

152
153
154
155
156
157
	runmode = param[0].data.d_int32;
	xargs.image = param[1].data.d_image;
	xargs.drawable = param[2].data.d_drawable;
	drw = gimp_drawable_get(xargs.drawable);

	switch (runmode)
Elliot Lee's avatar
Elliot Lee committed
158
	{
159
	        case RUN_INTERACTIVE:
160
              INIT_I18N_UI();
Elliot Lee's avatar
Elliot Lee committed
161
162
163
164
165
				/* retrieve stored arguments (if any) */
				gimp_get_data("plug_in_exchange", &xargs);
				/* initialize using foreground color */
				gimp_palette_get_foreground(&xargs.fromred, &xargs.fromgreen, &xargs.fromblue);
				/* and initialize some other things */
166
167
168
169
170
171
172
173
174
175
176
				gimp_drawable_mask_bounds(drw->id, &sel_x1, &sel_y1, &sel_x2, &sel_y2);
				sel_width = sel_x2 - sel_x1;
				sel_height = sel_y2 - sel_y1;
				if (sel_width > PREVIEW_SIZE)
						prev_width = PREVIEW_SIZE;
				else
						prev_width = sel_width;
				if (sel_height > PREVIEW_SIZE)
						prev_height = PREVIEW_SIZE;
				else
						prev_height = sel_height;
Elliot Lee's avatar
Elliot Lee committed
177
178
179
180
				if (!doDialog())
					return;
				break;
		case RUN_WITH_LAST_VALS:
181
          INIT_I18N();
182
				gimp_get_data("plug_in_exchange", &xargs);
Elliot Lee's avatar
Elliot Lee committed
183
184
185
186
187
188
189
190
				/* 
				 * instead of recalling the last-set values,
				 * run with the current foreground as 'from'
				 * color, making ALT-F somewhat more useful.
				 */
				gimp_palette_get_foreground(&xargs.fromred, &xargs.fromgreen, &xargs.fromblue);
				break;
		case RUN_NONINTERACTIVE:
191
          INIT_I18N();
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
				if (nparams != 10)
					status = STATUS_EXECUTION_ERROR;
				if (status == STATUS_SUCCESS)
				{
					xargs.fromred = param[3].data.d_int8;
					xargs.fromgreen = param[4].data.d_int8;
					xargs.fromblue = param[5].data.d_int8;
					xargs.tored = param[6].data.d_int8;
					xargs.togreen = param[7].data.d_int8;
					xargs.toblue = param[8].data.d_int8;
					xargs.red_threshold = param[9].data.d_int32;
					xargs.green_threshold = param[10].data.d_int32;
					xargs.blue_threshold = param[11].data.d_int32;
				}
				break;
Elliot Lee's avatar
Elliot Lee committed
207
208
209
210
211
		default:	
				break;
	}
	if (status == STATUS_SUCCESS)
	{
Marc Lehmann's avatar
Marc Lehmann committed
212
		if (gimp_drawable_is_rgb(drw->id))
Elliot Lee's avatar
Elliot Lee committed
213
		{
214
			gimp_progress_init( _("Color Exchange..."));
215
			gimp_tile_cache_ntiles(2 * (drw->width / gimp_tile_width() + 1));
216
217
			exchange();	
			gimp_drawable_detach(drw);
Elliot Lee's avatar
Elliot Lee committed
218
			/* store our settings */
219
220
			if (runmode == RUN_INTERACTIVE)
			  gimp_set_data("plug_in_exchange", &xargs, sizeof(myParams));
Elliot Lee's avatar
Elliot Lee committed
221
			/* and flush */
222
223
			if (runmode != RUN_NONINTERACTIVE)
			  gimp_displays_flush ();
Elliot Lee's avatar
Elliot Lee committed
224
225
226
227
228
229
230
231
232
		}
		else
			status = STATUS_EXECUTION_ERROR;
	}
	values[0].data.d_status = status;
}

/* do the exchanging */
static
233
void	exchange()
Elliot Lee's avatar
Elliot Lee committed
234
{
235
236
	/* do the real exchange */
	real_exchange(-1, -1, -1, -1, 0);
Elliot Lee's avatar
Elliot Lee committed
237
238
239
}

/* show our dialog */
240
241
static int
doDialog (void)
Elliot Lee's avatar
Elliot Lee committed
242
{
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
  GtkWidget    *dialog;
  GtkWidget    *frame;
  GtkWidget    *table;
  GtkWidget    *mainbox;
  GtkWidget    *tobox;
  GtkWidget    *frombox;
  GtkWidget    *prevbox;
  guchar       *color_cube;
  gchar	      **argv;
  gint		argc;
  gint		framenumber;

  argc    = 1;
  argv    = g_new (gchar *, 1);
  argv[0] = g_strdup ("exchange");

  gtk_init (&argc, &argv);
  gtk_rc_parse (gimp_gtkrc ());

  /* stuff for preview */
  gtk_preview_set_gamma (gimp_gamma ());
  gtk_preview_set_install_cmap (gimp_install_cmap ()); 
  color_cube = gimp_color_cube ();
  gtk_preview_set_color_cube (color_cube[0], color_cube[1],
			      color_cube[2], color_cube[3]); 
  gtk_widget_set_default_visual (gtk_preview_get_visual ());
  gtk_widget_set_default_colormap (gtk_preview_get_cmap ());

  /* load pixelregion */
  gimp_pixel_rgn_init (&origregion, drw,
		       0, 0, PREVIEW_SIZE, PREVIEW_SIZE, FALSE, FALSE);

  /* set up the dialog */
  dialog = gimp_dialog_new (_("Color Exchange"), "exchange",
			    gimp_plugin_help_func, "filters/exchange.html",
			    GTK_WIN_POS_MOUSE,
			    FALSE, TRUE, FALSE,

			    _("OK"), ok_callback,
			    NULL, NULL, NULL, TRUE, FALSE,
			    _("Cancel"), gtk_widget_destroy,
			    NULL, 1, NULL, FALSE, TRUE,

			    NULL);

  gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
		      GTK_SIGNAL_FUNC (gtk_main_quit),
		      NULL);

  /* do some boxes here */
  mainbox = gtk_vbox_new (FALSE, 5);
  gtk_container_set_border_width (GTK_CONTAINER (mainbox), 10);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), mainbox,
		      TRUE, TRUE, 0);

  prevbox = gtk_hbox_new(FALSE, 5);
  gtk_container_set_border_width (GTK_CONTAINER (prevbox), 0);
  gtk_box_pack_start (GTK_BOX (mainbox), prevbox, TRUE, TRUE, 0);

  frombox = gtk_hbox_new (FALSE, 5);
  gtk_container_set_border_width (GTK_CONTAINER (frombox), 0);
  gtk_box_pack_start (GTK_BOX (mainbox), frombox, TRUE, TRUE, 0);

  tobox = gtk_hbox_new (FALSE, 5);
  gtk_container_set_border_width (GTK_CONTAINER (tobox), 0);
  gtk_box_pack_start (GTK_BOX (mainbox), tobox, TRUE, TRUE, 0);

  frame = gtk_frame_new (_("Preview"));
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  gtk_container_set_border_width (GTK_CONTAINER (frame), 0);
  gtk_box_pack_start (GTK_BOX (prevbox), frame, TRUE, TRUE, 0);
  gtk_widget_show (frame);

  preview = gtk_preview_new (GTK_PREVIEW_COLOR);
  gtk_preview_size (GTK_PREVIEW (preview), prev_width, prev_height);
  gtk_container_add (GTK_CONTAINER (frame), preview);
  update_preview ();
  gtk_widget_show (preview); 

  /* and our scales */
  for (framenumber = 0; framenumber < 2; framenumber++)
    {
      frame = gtk_frame_new (framenumber ? _("To color") : _("From color"));
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
      gtk_container_set_border_width (GTK_CONTAINER (frame), 0);
      gtk_box_pack_start (framenumber ? GTK_BOX (tobox) : GTK_BOX (frombox),
			  frame, TRUE, TRUE, 0);
      gtk_widget_show (frame);
      table = gtk_table_new (8, 2, FALSE);
      gtk_container_set_border_width (GTK_CONTAINER (table), 0);
      gtk_container_add (GTK_CONTAINER (frame), table);
      doLabelAndScale (_("Red"), table,
		       framenumber ? &xargs.tored : &xargs.fromred);
      if (! framenumber)
	doLabelAndScale (_("Red threshold"), table, &xargs.red_threshold);
      doLabelAndScale (_("Green"), table,
		       framenumber ? &xargs.togreen : &xargs.fromgreen);
      if (! framenumber)
	doLabelAndScale (_("Green threshold"), table, &xargs.green_threshold);
      doLabelAndScale (_("Blue"), table,
		       framenumber ? &xargs.toblue : &xargs.fromblue);
      if (! framenumber)
	doLabelAndScale (_("Blue threshold"), table, &xargs.blue_threshold);
      if (! framenumber)
Elliot Lee's avatar
Elliot Lee committed
347
	{
348
349
350
351
352
353
354
355
356
	  GtkWidget *button;

	  button = gtk_check_button_new_with_label (_("Lock thresholds"));
	  gtk_table_attach (GTK_TABLE (table), button, 1, 2, 6, 7,
			    GTK_FILL, 0, 0, 0);
	  gtk_signal_connect (GTK_OBJECT (button), "clicked",
			      (GtkSignalFunc) lock_callback,
			      dialog);
	  gtk_widget_show (button);
Elliot Lee's avatar
Elliot Lee committed
357
	}
358
359
360
361
362
363
364
365
366
      gtk_widget_show (table);
    }

  /* show everything */
  gtk_widget_show (prevbox);
  gtk_widget_show (tobox);
  gtk_widget_show (frombox);
  gtk_widget_show (mainbox);
  gtk_widget_show (dialog);
Elliot Lee's avatar
Elliot Lee committed
367

368
369
  gtk_main ();
  gdk_flush ();
Elliot Lee's avatar
Elliot Lee committed
370

371
  return running;
Elliot Lee's avatar
Elliot Lee committed
372
373
374
375
376
377
378
379
380
381
382
383
}

static
void	doLabelAndScale(char *labelname, GtkWidget *table, guchar *dest)
{
	static	int	idx = -1;
	GtkWidget	*label, *scale;
	GtkObject	*scale_data;

	idx++;
	label = gtk_label_new(labelname);
	gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
384
	gtk_table_attach(GTK_TABLE(table), label, 0, 1, idx, idx + 1, GTK_FILL, 0, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
385
386
387
388
389
390
391
	scale_data = gtk_adjustment_new(*dest, 0.0, 255.0, 0.0, 0.0, 0.0);
	scale = gtk_hscale_new(GTK_ADJUSTMENT(scale_data));
	gtk_widget_set_usize(scale, SCALE_WIDTH, 0);
	/* just need 1:1 resolution on scales */
	gtk_scale_set_digits(GTK_SCALE(scale), 0);
	gtk_table_attach(GTK_TABLE(table), scale, 1, 2, idx, idx + 1, GTK_FILL, 0, 0, 0);
	gtk_scale_set_value_pos(GTK_SCALE(scale), GTK_POS_TOP);
392
	gtk_range_set_update_policy(GTK_RANGE(scale), GTK_UPDATE_DISCONTINUOUS);
Elliot Lee's avatar
Elliot Lee committed
393
394
395
396
397
398
399
400
401
402
403
404
405
406
	gtk_signal_connect(GTK_OBJECT(scale_data), "value_changed",
			   (GtkSignalFunc) scale_callback,
			   dest);
	gtk_widget_show(label);
	gtk_widget_show(scale);
}

static
void	ok_callback(GtkWidget *widget, gpointer data)
{
	running = 1;
	gtk_widget_destroy(GTK_WIDGET(data));
}

407
408
409
410
411
412
static
void	lock_callback(GtkWidget *widget, gpointer data)
{
	lock_thres = 1 - lock_thres;
}

Elliot Lee's avatar
Elliot Lee committed
413
414
415
416
417
418
static
void	scale_callback(GtkAdjustment *adj, gpointer data)
{
	guchar	*val = data;

	*val = (guchar) adj->value;
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
	update_preview();
}

static
void	update_preview()
{
	real_exchange(sel_x1, sel_y1, sel_x1 + prev_width, sel_y1 + prev_height, 1);
	gtk_widget_draw(preview, NULL);
	gdk_flush();
}

static
void	real_exchange(gint x1, gint y1, gint x2, gint y2, int dopreview)
{
	GPixelRgn	srcPR, destPR;
	guchar		*src_row, *dest_row;
435
436
	int		x, y, bpp = drw->bpp;
	int		width, height;
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527

	/* fill if necessary */
	if (x1 == -1 || y1 == -1 || x2 == -1 || y2 == -1)
	{
		x1 = sel_x1;
		y1 = sel_y1;
		x2 = sel_x2;
		y2 = sel_y2;
	}
	/* check for valid coordinates */
	width = x2 - x1;
	height = y2 - y1;
	/* allocate memory */
	src_row = g_malloc(drw->width * bpp * sizeof(guchar));
	dest_row =  g_malloc(drw->width * bpp * sizeof(guchar));
	/* initialize the pixel regions */
	/*
	gimp_pixel_rgn_init(&srcPR, drw, x1, y1, width, height, FALSE, FALSE);
	*/
	gimp_pixel_rgn_init(&srcPR, drw, 0, 0, drw->width, drw->height, FALSE, FALSE);
	if (! dopreview)
		gimp_pixel_rgn_init(&destPR, drw, 0, 0, width, height, TRUE, TRUE);
	for (y = y1; y < y2; y++)
	{
		gimp_pixel_rgn_get_row(&srcPR, src_row, 0, y, drw->width);
		for (x = x1; x < x2; x++)
		{
			gint	pixel_red, pixel_green, pixel_blue;
			gint	min_red, max_red,
					min_green, max_green,
					min_blue, max_blue;
			gint	new_red, new_green, new_blue;
			gint	idx, rest;
			
			/* get boundary values */
			min_red = MAX(xargs.fromred - xargs.red_threshold, 0);
			min_green = MAX(xargs.fromgreen - xargs.green_threshold, 0);
			min_blue = MAX(xargs.fromblue - xargs.blue_threshold, 0);

			max_red = MIN(xargs.fromred + xargs.red_threshold, 255);
			max_green = MIN(xargs.fromgreen + xargs.green_threshold, 255);
			max_blue = MIN(xargs.fromblue + xargs.blue_threshold, 255);

			/* get current pixel-values */
			pixel_red = src_row[x * bpp];
			pixel_green = src_row[x * bpp + 1];
			pixel_blue = src_row[x * bpp + 2];

			/* shift down for preview */
			if (dopreview)
				idx = (x - x1) * bpp;
			else
				idx = x * bpp;

			/* want this pixel? */
			if (pixel_red >= min_red && pixel_red <= max_red && pixel_green >= min_green && pixel_green <= max_green && pixel_blue >= min_blue && pixel_blue <= max_blue)
			{
				gint	red_delta, green_delta, blue_delta;

				red_delta = pixel_red - xargs.fromred;
				green_delta = pixel_green - xargs.fromgreen;
				blue_delta = pixel_blue - xargs.fromblue;
				new_red = (guchar) MAX(MIN(xargs.tored + red_delta, 255), 0);
				new_green = (guchar) MAX(MIN(xargs.togreen + green_delta, 255), 0);
				new_blue = (guchar) MAX(MIN(xargs.toblue + blue_delta, 255), 0);
			}
			else
			{
				new_red = pixel_red;
				new_green = pixel_green;
				new_blue = pixel_blue;
			}

			/* fill buffer (cast it too) */
			dest_row[idx + 0] = (guchar) new_red;
			dest_row[idx + 1] = (guchar) new_green;
			dest_row[idx + 2] = (guchar) new_blue;
			
			/* copy rest (most likely alpha-channel) */
			for (rest = 3; rest < bpp; rest++)
				dest_row[idx + rest] = src_row[x * bpp + rest];
		}
		/* store the dest */
		if (dopreview)
			gtk_preview_draw_row(GTK_PREVIEW(preview), dest_row, 0, y - y1, width);
		else
			gimp_pixel_rgn_set_row(&destPR, dest_row, 0, y, drw->width);
		/* and tell the user what we're doing */
		if (! dopreview && (y % 10) == 0)
			gimp_progress_update((double) y / (double) height);
	}
528
529
	g_free(src_row);
	g_free(dest_row);
530
531
532
533
534
535
536
	if (! dopreview)
	{
		/* update the processed region */
		gimp_drawable_flush(drw);
		gimp_drawable_merge_shadow(drw->id, TRUE);
		gimp_drawable_update(drw->id, x1, y1, width, height);
	}
Elliot Lee's avatar
Elliot Lee committed
537
}
538
539
540
541
542
543
544
545