CML_explorer.c 89.4 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * CML_explorer.c -- This is a plug-in for The GIMP 1.0
5
 * Time-stamp: <1997/11/22 23:54:47 narazaki@InetQ.or.jp>
Elliot Lee's avatar
Elliot Lee committed
6
 * Copyright (C) 1997 Shuji Narazaki <narazaki@InetQ.or.jp>
7
 * Version: 1.0.11
Elliot Lee's avatar
Elliot Lee committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
 * URL: http://www.inetq.or.jp/~narazaki/TheGIMP/
 *
 * 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
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Comment:
 *  CML is the abbreviation of Coupled-Map Lattice that is a model of
 *  complex systems, proposed by a physicist[1,2].
 *
 *  Similar models are summaried as follows:
 *
 *			Value	 Time	  Space
 *  Coupled-Map Lattice	cont.	 discrete discrete
 *  Celluar Automata	discrete discrete discrete
 *  Diffrential Eq.	cont.	 cont.	  cont.
 *
 *  (But this program uses a parameter: hold-rate to avoid very fast changes.
 *  Thus time is rather continuous than discrete.
 *  Yes, this change to model changes the output completely.)
 *
 *  References:
 *  1. Kunihiko Kaneko, Period-doubling of kind-antikink patterns,
 *     quasi-periodicity in antiferro-like structures and spatial
 *     intermittency in coupled map lattices -- Toward a prelude to a
 *     "field theory of chaos", Prog. Theor. Phys. 72 (1984) 480.
 *
 *  2. Kunihiko Kaneko ed., Theory and Applications of Coupled Map
 *     Lattices (Wiley, 1993).
 *
 *  About Parameter File:
 *  I assume that the possible longest line in CMP parameter file is 1023.
 *  Please read CML_save_to_file_callback if you want know details of syntax.
 *
 *  Format version 1.0 starts with:
 *    ; This is a parameter file for CML_explorer
 *    ; File format version: 1.0
 *    ;
 *    	Hue
 *
 *  The old format for CML_explorer included in gimp-0.99.[89] is:
 *    ; CML parameter file (version: 1.0)
 *    ;	Hue
 *
 * (This file format is interpreted as format version 0.99 now.)
 *
 * Thanks:
 *  This version contains patches from:
 *    Tim Mooney <mooney@dogbert.cc.ndsu.NoDak.edu>
 *    Sean P Cier <scier@andrew.cmu.edu>
 *    David Mosberger-Tang <davidm@azstarnet.com>
 *    Michael Sweet <mike@easysw.com>
 *
 */

#include "gtk/gtk.h"
#include "libgimp/gimp.h"
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>		/* for seed of random number */

Manish Singh's avatar
Manish Singh committed
83 84 85 86
#ifndef M_PI
#define M_PI    3.14159265358979323846
#endif /* M_PI */

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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 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
#define PARAM_FILE_FORMAT_VERSION	1.0
#define	PLUG_IN_NAME	"plug_in_CML_explorer"
#define SHORT_NAME	"CML_explorer"
#define PROGRESS_NAME	"CML_explorer: evoluting..."
#define MENU_POSITION	"<Image>/Filters/Render/CML explorer"
#define	VERBOSE_DIALOGS	1
#define	MAIN_FUNCTION	CML
#define INTERFACE	CML_explorer_interface
#define	DIALOG		CML_explorer_dialog
#define VALS		CML_explorer_vals
#define OK_CALLBACK	CML_explorer_ok_callback
#define PROGRESS_UPDATE_NUM	100
#define CML_LINE_SIZE	1024
#define TILE_CACHE_SIZE 32
/* gtkWrapper */
/* gtkW is the abbreviation of gtk Wrapper */
#define GTKW_ENTRY_WIDTH	60
#define GTKW_SCALE_WIDTH	130
#define GTKW_PREVIEW_WIDTH	64
#define GTKW_PREVIEW_HEIGHT	220
#define	GTKW_BORDER_WIDTH	3
#define GTKW_FLOAT_MIN_ERROR	0.000001
#define GTKW_ENTRY_BUFFER_SIZE	32
/* gtkW type */
typedef struct
{
  GtkWidget	*widget;
  gpointer	value;
  void	 	(*updater)();
} gtkW_widget_table;

typedef struct
{
  guchar *name;
  gpointer data;
} gtkW_menu_item;
/* gtkW global variables */
gint	gtkW_border_width = GTKW_BORDER_WIDTH;
gint	gtkW_border_height = 0;
gint	gtkW_homogeneous_layout	= FALSE;
gint	gtkW_frame_shadow_type = GTK_SHADOW_ETCHED_IN;
gint	gtkW_align_x = GTK_FILL|GTK_EXPAND;
gint	gtkW_align_y = GTK_FILL;
gint	gtkW_tooltips_color_allocated = FALSE;
GdkColor	tooltips_foreground, tooltips_background;
/* gtkW callback */
static void	gtkW_close_callback (GtkWidget *widget, gpointer data);
static void	gtkW_toggle_update (GtkWidget *widget, gpointer data);
static void	gtkW_iscale_update (GtkAdjustment *adjustment, gpointer data);
static void	gtkW_ientry_update (GtkWidget *widget, gpointer data);
static void	gtkW_dscale_update (GtkAdjustment *adjustment, gpointer data);
static void	gtkW_dentry_update (GtkWidget *widget, gpointer data);
static void	gtkW_dscale_entry_change_value (gtkW_widget_table *wtable);
static void	gtkW_iscale_entry_change_value (gtkW_widget_table *wtable);
static void	gtkW_menu_change_value (gtkW_widget_table *wtable);
static void	gtkW_menu_update (GtkWidget *widget, gpointer   data);
static void	gtkW_toggle_change_value (gtkW_widget_table	*wtable);
/* gtkW method */
GtkWidget	*gtkW_check_button_new (GtkWidget	*parent,
					guchar		*name,
					GtkSignalFunc	update,
					gint		*value);
static GtkWidget	*gtkW_dialog_new (guchar	*name,
					  GtkSignalFunc	ok_callback,
					  GtkSignalFunc	close_callback);
static GtkWidget	*gtkW_frame_new (GtkWidget *parent, guchar *name);
static GtkWidget	*gtkW_hbox_new (GtkWidget *parent);
static void	gtkW_message_dialog (gint gtk_was_initialized, guchar *message);
static GtkWidget	*gtkW_message_dialog_new (guchar * name);
static GtkWidget	*gtkW_table_add_button (GtkWidget	*table,
						gchar		*name,
						gint		x0,
						gint		x1,
						gint		y,
						GtkSignalFunc	callback,
						gpointer value);
static GtkWidget	*gtkW_table_add_dscale_entry (GtkWidget	*table,
						      gchar	*name,
						      gint	x,
						      gint	y,
						      GtkSignalFunc scale_update,
						      GtkSignalFunc entry_update,
						      gdouble	*value,
						      gdouble	min,
						      gdouble	max,
						      gdouble	step,
						      gpointer	table_entry);
static GtkWidget	*gtkW_table_add_iscale_entry (GtkWidget	*table,
						      gchar	*name,
						      gint	x,
						      gint	y,
						      GtkSignalFunc scale_update,
						      GtkSignalFunc entry_update,
						      gint	*value,
						      gdouble	min,
						      gdouble	max,
						      gdouble	step,
						      gpointer	table_entry);
static GtkWidget	 *gtkW_table_add_menu (GtkWidget	*parent,
					       guchar		*name,
					       gint		x,
					       gint		y,
					       GtkSignalFunc	imenu_update,
					       gint		*val,
					       gtkW_menu_item	*item,
					       gint		item_num,
					       gpointer		table_entry);
static GtkWidget	*gtkW_table_add_toggle (GtkWidget	*table,
						guchar		*name,
						gint		x,
						gint		y,
						GtkSignalFunc	update,
						gint		*value,
						gpointer	widget_entry);
static GtkWidget	*gtkW_table_new (GtkWidget *parent, gint col, gint row);
static GtkWidget	*gtkW_vbox_add_button (GtkWidget	*vbox,
					       guchar		*name,
					       GtkSignalFunc	update,
					       gpointer		data);
static GtkWidget	*gtkW_vbox_new (GtkWidget *parent);
static GtkTooltips	*gtkW_tooltips_new (GtkWidget *frame);
/* end of GtkW */

#define	RANDOM	((gdouble) ((double) rand ()/((double) RAND_MAX)))
#define CANNONIZE(p, x)	(255*(((p).range_h - (p).range_l)*(x) + (p).range_l))
#define HCANNONIZE(p, x)	(254*(((p).range_h - (p).range_l)*(x) + (p).range_l))
#define POS_IN_TORUS(i,size)	((i < 0) ? size + i : ((size <= i) ? i - size : i))

gtkW_menu_item function_menu [] =
{
#define	CML_KEEP_VALUES	0
  { "Keep image's values", NULL },
#define	CML_KEEP_FIRST	1
  { "Keep the first value", NULL },
#define	CML_FILL	2
  { "Fill with parameter k", NULL },
#define	CML_LOGIST	3
  { "k{x(1-x)}^p", NULL },
#define	CML_LOGIST_STEP	4
  { "k{x(1-x)}^p stepped", NULL },
#define	CML_POWER	5
  { "kx^p", NULL },
#define	CML_POWER_STEP	6
  { "kx^p stepped", NULL },
#define	CML_REV_POWER	7
  { "k(1 - x^p)", NULL },
#define	CML_REV_POWER_STEP	8
  { "k(1 - x^p) stepped", NULL },
#define	CML_DELTA	9
  { "Delta function", NULL },
#define	CML_DELTA_STEP	10
  { "Delta function stepped", NULL },
#define CML_SIN_CURVE	11
  { "sin^p-based function", NULL },
#define CML_SIN_CURVE_STEP	12
  { "sin^p, stepped", NULL },
};

gtkW_menu_item composition_menu [] =
{
#define COMP_NONE		0
  { "None", NULL },
#define COMP_MAX_LINEAR		1
  { "Max (x, -)", NULL },
#define COMP_MAX_LINEAR_P1	2
  { "Max (x+d, -)", NULL },
#define COMP_MAX_LINEAR_M1	3
  { "Max (x-d, -)", NULL },
#define COMP_MIN_LINEAR		4
  { "Mix (x, -)", NULL },
#define COMP_MIN_LINEAR_P1	5
  { "Min (x+d, -)", NULL },
#define COMP_MIN_LINEAR_M1	6
  { "Min (x-d, -)", NULL },
#define COMP_MAX_LINEAR_P1L	7
  { "Max (x+d, -), (x < 0.5)", NULL },
#define COMP_MAX_LINEAR_P1U	8
  { "Max (x+d, -), (0.5 < x)", NULL },
#define COMP_MAX_LINEAR_M1L	9
  { "Max (x-d, -), (x < 0.5)", NULL },
#define COMP_MAX_LINEAR_M1U	10
  { "Max (x-d, -), (0.5 < x)", NULL },
#define COMP_MIN_LINEAR_P1L	11
  { "Min (x+d, -), (x < 0.5)", NULL },
#define COMP_MIN_LINEAR_P1U	12
  { "Min (x+d, -), (0.5 < x)", NULL },
#define COMP_MIN_LINEAR_M1L	13
  { "Min (x-d, -), (x < 0.5)", NULL },
#define COMP_MIN_LINEAR_M1U	14
  { "Min (x-d, -), (0.5 < x)", NULL }
};

gtkW_menu_item arrange_menu [] =
{
#define	STANDARD	0
  { "Standard", NULL },
#define	AVERAGE		1
  { "Use average value", NULL },
#define	ANTILOG		2
  { "Use reverse value", NULL },
#define	RAND_POWER0	3
  { "With random power (0,10)", NULL },
#define	RAND_POWER1	4
  { "With random power (0,1)", NULL },
#define	RAND_POWER2	5
  { "With gradient power (0,1)", NULL },
#define	MULTIPLY_RANDOM0	6
  { "Multiply rand. value (0,1)", NULL },
#define	MULTIPLY_RANDOM1	7
  { "Multiply rand. value (0,2)", NULL },
#define	MULTIPLY_GRADIENT	8
  { "Multiply gradient (0,1)", NULL },
#define RAND_AND_P	9
  { "With p and random (0,1)", NULL },
};

gtkW_menu_item initial_value_menu [] =
{
  { "All black", NULL },	/* 0 */
  { "All gray", NULL },		/* 1 */
  { "All white", NULL },	/* 2 */
  { "The first row of the image", NULL }, /* 3 */
  { "Continuous gradient", NULL }, /* 4 */
  { "Continuous grad. w/o gap", NULL }, /* 5 */
#define	CML_INITIAL_RANDOM_INDEPENDENT	6
  { "Random, ch. independent", NULL },
#define	CML_INITIAL_RANDOM_SHARED	7
  { "Random shared", NULL },
#define	CML_INITIAL_RANDOM_FROM_SEED	8
  { "Randoms from seed" , NULL },
#define	CML_INITIAL_RANDOM_FROM_SEED_SHARED	9
  { "Randoms from seed (shared)" , NULL }
} ;

#define CML_PARAM_NUM	15
typedef struct
{
  gint	  function;
  gint	  composition;
  gint	  arrange;
  gint	  cyclic_range;
  gdouble mod_rate;		/* diff / old-value */
  gdouble env_sensitivity;	/* self-diff : env-diff */
  gint	  diffusion_dist;
  gdouble ch_sensitivity;
  gint	  range_num;
  gdouble power;
  gdouble parameter_k;
  gdouble range_l;
  gdouble range_h;
  gdouble mutation_rate;
  gdouble mutation_dist;
} CML_PARAM;

typedef struct
{
  CML_PARAM hue;
  CML_PARAM sat;
  CML_PARAM val;
  gint	initial_value;
  gint	scale;
  gint	start_offset;
  gint	seed;
  guchar last_file_name[256];
} ValueType;

static ValueType VALS =
{
  /* function     composition  arra
   cyc chng sens diff cor  n  pow   k   (l,h) rnd dist */
  {CML_SIN_CURVE, COMP_NONE, STANDARD,
   1,  0.5, 0.7,  2,  0.0, 1, 1.0, 1.0, 0, 1, 0.0, 0.1},
  {CML_FILL     , COMP_NONE, STANDARD,
   0,  0.6, 0.1,  2,  0.0, 1, 1.4, 0.9, 0, 0.9, 0.0, 0.1},
  {CML_FILL     , COMP_NONE, STANDARD,
   0,  0.5, 0.2,  2,  0.0, 1, 2.0, 1.0, 0, 0.9, 0.0, 0.1},
  6,				/* random value 1 */
  1,				/* scale */
  0,				/* start_offset */
  0,				/* seed */
  ""
};

gtkW_menu_item channel_menu [] =
{
  { "Hue", (gpointer) &VALS.hue },
  { "Saturation", (gpointer) &VALS.sat },
  { "Value", (gpointer) &VALS.val },
};

gtkW_menu_item sload_menu [] =
{
  { "NULL", NULL },
  { "Hue", NULL },
  { "Saturation", NULL },
  { "Value", NULL },
};

static void	query	(void);
static void	run	(char	*name,
			 int	nparams,
			 GParam	*param,
			 int	*nreturn_vals,
			 GParam **return_vals);
static GStatusType	MAIN_FUNCTION (gint preview_p);
static void	CML_compute_next_step (gint size,
		       gdouble **h, gdouble **s, gdouble **v,
		       gdouble **hn, gdouble **sn, gdouble **vn,
		       gdouble **haux, gdouble **saux, gdouble **vaux);
static gdouble CML_next_value (gdouble *vec, gint pos, gint size,
			       gdouble c1, gdouble c2,
			       CML_PARAM *param, gdouble aux);
static void	rgb_to_hsv (int *r, int *g, int *b);
static void	hsv_to_rgb (int *h, int *s, int *v);
static gdouble	logistic_function (CML_PARAM *param, gdouble x, gdouble power);
static gint	DIALOG ();
static GtkWidget *CML_dialog_sub_panel_new (GtkWidget *parent,
					    guchar *name,
					    CML_PARAM *param,
					    gint channel_id);
static GtkWidget *CML_dialog_advanced_panel_new (GtkWidget *parent, guchar *name);
void	preview_update ();
static void	function_graph_new (GtkWidget *widget, gpointer data);
static void	CML_set_or_randomize_seed_callback (GtkWidget *widget, gpointer data);
static void	CML_copy_parameters_callback (GtkWidget *widget, gpointer data);
static void	CML_initial_value_sensitives_update ();
static void	CML_menu_update (GtkWidget *widget, gpointer data);
static void	CML_initial_value_menu_update (GtkWidget *widget, gpointer data);
static void	OK_CALLBACK (GtkWidget *widget, gpointer   data);
static void	CML_save_to_file_callback (GtkWidget *widget, gpointer client_data);
static void	CML_execute_save_to_file (GtkWidget *widget, gpointer client_data);
static gint	force_overwrite (char *filename);
static void	CML_overwrite_ok_callback (GtkWidget *widget, gpointer   data);
static void	CML_preview_update_callback (GtkWidget *widget, gpointer   data);
static void	CML_load_from_file_callback (GtkWidget *widget, gpointer client_data);
static gint	CML_load_parameter_file (guchar *filename, gint interactive_mode);
static void	CML_execute_load_from_file (GtkWidget *widget, gpointer client_data);
static gint	parse_line_to_gint (FILE *file, gint *flag);
static gdouble	parse_line_to_gdouble (FILE *file, gint *flag);

GPlugInInfo PLUG_IN_INFO =
{
  NULL,				/* init_proc  */
  NULL,				/* quit_proc */
  query,			/* query_proc */
  run,				/* run_proc */
};

typedef struct
{
  gint run;
} Interface;

static Interface INTERFACE = { FALSE };

GtkWidget	*preview;
gtkW_widget_table	widget_pointers[4][CML_PARAM_NUM];
typedef struct
{
  GtkWidget	*widget;
  gint		logic;
} CML_sensitive_widget_table;
#define	RANDOM_SENSITIVES_NUM	5
CML_sensitive_widget_table	random_sensitives[RANDOM_SENSITIVES_NUM] =
{ { NULL, 0 }, { NULL, 0 }, { NULL, 0 }, { NULL, 0 }, { NULL, 0} };
gint		drawable_id = 0;
gint		copy_source = 0;
gint		copy_destination = 0;
gint		selective_load_source = 0;
gint		selective_load_destination = 0;
gint		CML_preview_defer = FALSE;
gint		overwritable = 0;

gdouble		*mem_chank0 = NULL;
gint		mem_chank0_size = 0;
guchar		*mem_chank1 = NULL;
gint		mem_chank1_size = 0;
guchar		*mem_chank2 = NULL;
gint		mem_chank2_size = 0;

467
MAIN ()
Elliot Lee's avatar
Elliot Lee committed
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

static void
query ()
{
  static GParamDef args [] =
  {
    { PARAM_INT32, "run_mode", "Interactive, non-interactive"},
    { PARAM_IMAGE, "image", "Input image (not used)"},
    { PARAM_DRAWABLE, "drawable", "Input drawable" },
    { PARAM_STRING, "parameter_file_name", "the name of paremter file. CML_explorer makes an image with its settings." },
  };
  static GParamDef *return_vals = NULL;
  static int nargs = sizeof (args) / sizeof (args[0]);
  static int nreturn_vals = 0;

  gimp_install_procedure (PLUG_IN_NAME,
			  "Make an image of Coupled-Map Lattice",
			  "Make an image of Coupled-Map Lattice (CML). CML is a kind of Cellula Automata on continuous (value) domain. In RUN_NONINTERACTIVE, the name of a prameter file is passed as the 4th arg. You can control CML_explorer via parameter file.",
			  /*  Or do you want to call me with over 50 args? */
			  "Shuji Narazaki (narazaki@InetQ.or.jp); http://www.inetq.or.jp/~narazaki/TheGIMP/",
			  "Shuji Narazaki",
			  "1997",
			  MENU_POSITION,
			  "RGB*, GRAY*",
			  PROC_PLUG_IN,
			  nargs, nreturn_vals,
			  args, return_vals);
}

static void
run (char	*name,
     int	nparams,
     GParam	*param,
     int	*nreturn_vals,
     GParam	**return_vals)
{
504
  GParam	*values;
Elliot Lee's avatar
Elliot Lee committed
505 506 507
  GStatusType	status = STATUS_EXECUTION_ERROR;
  GRunModeType	run_mode;

508
  values = g_new (GParam, 1);
Elliot Lee's avatar
Elliot Lee committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
  run_mode = param[0].data.d_int32;
  drawable_id = param[2].data.d_drawable;

  *nreturn_vals = 1;
  *return_vals = values;

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

  switch (run_mode)
    {
    case RUN_INTERACTIVE:
      gimp_get_data (PLUG_IN_NAME, &VALS);
      if (! DIALOG ())
	return;
#ifdef INTERACTIVE_DIALOG
      else
	status = STATUS_SUCCESS;
#endif
      break;
    case RUN_NONINTERACTIVE:
      {
	guchar *filename = param[3].data.d_string;

	if (! CML_load_parameter_file (filename, FALSE))
	  return;
	break;
      }
    case RUN_WITH_LAST_VALS:
      gimp_get_data (PLUG_IN_NAME, &VALS);
      break;
    }

  gimp_tile_cache_ntiles (TILE_CACHE_SIZE);
#ifdef INTERACTIVE_DIALOG
  if (run_mode != RUN_INTERACTIVE)
    status = MAIN_FUNCTION (FALSE);
#else
  status = MAIN_FUNCTION (FALSE);
#endif

  if (run_mode != RUN_NONINTERACTIVE)
    gimp_displays_flush();
  if (run_mode == RUN_INTERACTIVE && status == STATUS_SUCCESS)
    gimp_set_data (PLUG_IN_NAME, &VALS, sizeof (ValueType));

  if (mem_chank0)
    g_free (mem_chank0);
  if (mem_chank1)
    g_free (mem_chank1);
  if (mem_chank2)
    g_free (mem_chank2);

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

static GStatusType
MAIN_FUNCTION (gint preview_p)
{
  GDrawable	*drawable = NULL;
  GPixelRgn	dest_rgn, src_rgn;
  guchar	*dest_buffer = NULL;
  guchar	*src_buffer = NULL;
  gint		x1, x2, y1, y2;
  gint		dx, dy;
  gint		dest_has_alpha = FALSE;
  gint		dest_is_gray = FALSE;
  gint		src_has_alpha = FALSE;
  gint		src_is_gray = FALSE;
  gint		total, processed = 0;
  gint		keep_height = 1;
  gint		cell_num, width_by_pixel, height_by_pixel;
  gint		index;
  gint		src_bpp, src_bpl;
  gint		dest_bpp, dest_bpl;
  gdouble	*hues, *sats, *vals;
  gdouble	*newh, *news, *newv;
  gdouble	*haux, *saux, *vaux;

  /* open THE drawable */
  drawable = gimp_drawable_get (drawable_id);
  gimp_drawable_mask_bounds (drawable_id, &x1, &y1, &x2, &y2);
  src_has_alpha = dest_has_alpha = gimp_drawable_has_alpha (drawable_id);
  src_is_gray = dest_is_gray = gimp_drawable_gray (drawable_id);
  src_bpp = dest_bpp = (src_is_gray ? 1 : 3) + (src_has_alpha ? 1 : 0);

  if (preview_p)
    {
      dest_has_alpha = FALSE;
      dest_is_gray = FALSE;
      dest_bpp = 3;

      if (GTKW_PREVIEW_WIDTH < x2 - x1)	/* preview < drawable (selection) */
	x2 = x1 + GTKW_PREVIEW_WIDTH;
      if (GTKW_PREVIEW_HEIGHT < y2 - y1)
	y2 = y1 + GTKW_PREVIEW_HEIGHT;
    }
  width_by_pixel = x2 - x1;
  height_by_pixel = y2 - y1;
  dest_bpl = width_by_pixel * dest_bpp;
  src_bpl = width_by_pixel * src_bpp;
  cell_num = (width_by_pixel - 1)/ VALS.scale + 1;
  total = height_by_pixel * width_by_pixel;
  keep_height = VALS.scale;

  /* configure reusable memories */
  if (mem_chank0_size < 9 * cell_num * sizeof (gdouble))
    {
      if (mem_chank0)
	g_free (mem_chank0);
      mem_chank0_size = 9 * cell_num * sizeof (gdouble);
      mem_chank0 = (gdouble *) g_malloc (mem_chank0_size);
    }
  hues = mem_chank0;
  sats = mem_chank0 + cell_num;
  vals = mem_chank0 + 2 * cell_num;
  newh = mem_chank0 + 3 * cell_num;
  news = mem_chank0 + 4 * cell_num;
  newv = mem_chank0 + 5 * cell_num;
  haux = mem_chank0 + 6 * cell_num;
  saux = mem_chank0 + 7 * cell_num;
  vaux = mem_chank0 + 8 * cell_num;

  if (mem_chank1_size < src_bpl * keep_height)
    {
      if (mem_chank1)
	g_free (mem_chank1);
      mem_chank1_size = src_bpl * keep_height;
      mem_chank1 = (guchar *) g_malloc (mem_chank1_size);
    }
  src_buffer = mem_chank1;

  if (mem_chank2_size < dest_bpl * keep_height)
    {
      if (mem_chank2)
	g_free (mem_chank2);
      mem_chank2_size = dest_bpl * keep_height;
      mem_chank2 = (guchar *) g_malloc (mem_chank2_size);
    }
  dest_buffer = mem_chank2;
  
  if (! preview_p)
    gimp_pixel_rgn_init (&dest_rgn, drawable, x1, y1,
			 width_by_pixel, height_by_pixel,
			 TRUE, TRUE);
  
  gimp_pixel_rgn_init (&src_rgn, drawable, x1, y1,
		       width_by_pixel, height_by_pixel,
		       FALSE, FALSE);
  
  /* Note: Why seed is modulo of (1 << 15)? I use iscale_entry to control
     the value. Adjustment widget in Scale uses gdouble(?). But it is not
     precise to hold a long integer! Thus, in updating scale, the value will
     be rounded. Since I can't imagine another solution, I use short (16bit)
     integer as the seed of random number sequence. This approach works on
     my i386-based Linux.
  */
  if (VALS.initial_value < CML_INITIAL_RANDOM_FROM_SEED)
    VALS.seed = time (NULL) % (1 << 15);
  srand (VALS.seed);

  for (index = 0; index < cell_num; index++)
    {
      switch (VALS.hue.arrange)
	{
	case RAND_POWER0:
	  haux [index] = RANDOM * 10;
	  break;
	case RAND_POWER2:
	case MULTIPLY_GRADIENT:
	  haux [index] = (gdouble) abs ((index % 511) - 255) / (gdouble) 256;
	  break;
	case RAND_POWER1:
	case MULTIPLY_RANDOM0:
	  haux [index] = RANDOM;
	  break;
	case MULTIPLY_RANDOM1:
	  haux [index] = RANDOM * 2;
	  break;
	case RAND_AND_P:
	  haux [index] = (index % (2 * VALS.hue.diffusion_dist) == 0) ? (RANDOM) : VALS.hue.power;
	  break;
	default:
	  haux [index] = VALS.hue.power;
	  break;
	}
      switch (VALS.sat.arrange)
	{
	case RAND_POWER0:
	  saux [index] = RANDOM * 10;
	  break;
	case RAND_POWER2:
	case MULTIPLY_GRADIENT:
	  saux [index] = (gdouble) abs ((index % 511) - 255) / (gdouble) 256;
	  break;
	case RAND_POWER1:
	case MULTIPLY_RANDOM0:
	  saux [index] = RANDOM;
	  break;
	case MULTIPLY_RANDOM1:
	  saux [index] = RANDOM * 2;
	  break;
	case RAND_AND_P:
	  saux [index] = (index % (2 * VALS.sat.diffusion_dist) == 0) ? (RANDOM) : VALS.sat.power;
	  break;
	default:
	  saux [index] = VALS.sat.power;
	  break;
	}
      switch (VALS.val.arrange)
	{
	case RAND_POWER0:
	  vaux [index] = RANDOM * 10;
	  break;
	case RAND_POWER2:
	case MULTIPLY_GRADIENT:
	  vaux [index] = (gdouble) abs ((index % 511) - 255) / (gdouble) 256;
	  break;
	case RAND_POWER1:
	case MULTIPLY_RANDOM0:
	  vaux [index] = RANDOM;
	  break;
	case MULTIPLY_RANDOM1:
	  vaux [index] = RANDOM * 2;
	  break;
	case RAND_AND_P:
	  vaux [index] = (index % (2 * VALS.val.diffusion_dist) == 0) ? (RANDOM) : VALS.val.power;
	  break;
	default:
	  vaux [index] = VALS.val.power;
	  break;
	}
      switch (VALS.initial_value)
	{
	case 0:
	case 1:
	case 2:
	  hues[index] = sats[index] = vals[index] = 0.5 * (VALS.initial_value);
	  break;
	case 3:			/* use the values of the image (drawable) */
	  break;		/* copy from the drawable after this loop */
	case 4:			/* grandient 1 */
	  hues[index] = sats[index] = vals[index]
	    = (gdouble) (index % 256) / (gdouble) 256;
	  break;		/* gradinet 2 */
	case 5:
	  hues[index] = sats[index] = vals[index]
	    = (gdouble) abs ((index % 511) - 255) / (gdouble) 256;
	  break;
	case CML_INITIAL_RANDOM_INDEPENDENT:
	case CML_INITIAL_RANDOM_FROM_SEED:
	  hues[index] = RANDOM;
	  sats[index] = RANDOM;
	  vals[index] = RANDOM;
	  break;
	case CML_INITIAL_RANDOM_SHARED:
	case CML_INITIAL_RANDOM_FROM_SEED_SHARED:
	  hues[index] = sats[index] = vals[index] = RANDOM;
	  break;
	}
    }

  if (VALS.initial_value == 3)
    {
      int	index;

      for (index = 0; index < MIN (cell_num, width_by_pixel / VALS.scale); index++)
	{
	  guchar buffer[4];
	  int	rgbi[3];
	  int	i;

	  gimp_pixel_rgn_get_pixel (&src_rgn, buffer,
				    x1 + (index * VALS.scale), y1);
	  for (i = 0; i < 3; i++) rgbi[i] = buffer[i];
	  rgb_to_hsv (rgbi, rgbi + 1, rgbi + 2);
	  hues[index] = (gdouble) rgbi[0] / (gdouble) 255;
	  sats[index] = (gdouble) rgbi[1] / (gdouble) 255;
	  vals[index] = (gdouble) rgbi[2] / (gdouble) 255;
	}
    }
  if (! preview_p) gimp_progress_init (PROGRESS_NAME);

  /* rolling start */
  for (index = 0; index < VALS.start_offset; index++)
    CML_compute_next_step (cell_num, &hues, &sats, &vals, &newh, &news, &newv,
			   &haux, &saux, &vaux);

  /* rendering */
  for (dy = 0; dy < height_by_pixel; dy += VALS.scale)
    {
      gint	i = 0;
      gint	r, g, b, h, s, v;
      gint	offset_x, offset_y, dest_offset;

      if (height_by_pixel < dy + keep_height)
	keep_height = height_by_pixel - dy;
      
      if ((VALS.hue.function == CML_KEEP_VALUES) ||
	  (VALS.sat.function == CML_KEEP_VALUES) ||
	  (VALS.val.function == CML_KEEP_VALUES))
	gimp_pixel_rgn_get_rect (&src_rgn, src_buffer,
				 x1, y1 + dy, width_by_pixel, keep_height);
      
      CML_compute_next_step (cell_num, &hues, &sats, &vals, &newh, &news, &newv,
			     &haux, &saux, &vaux);

      for (dx = 0; dx < cell_num; dx++)
	{
	  h = r = HCANNONIZE (VALS.hue, hues[dx]);
	  s = g = CANNONIZE (VALS.sat, sats[dx]);
	  v = b = CANNONIZE (VALS.val, vals[dx]);

	  if (! dest_is_gray)
	    hsv_to_rgb (&r, &g, &b);
	  /* render destination */
	  for (offset_y = 0;
	       (offset_y < VALS.scale) && (dy + offset_y < height_by_pixel);
	       offset_y++)
	    for (offset_x = 0;
		 (offset_x < VALS.scale) && (dx * VALS.scale + offset_x < width_by_pixel);
		 offset_x++)
	      {
		if ((VALS.hue.function == CML_KEEP_VALUES) ||
		    (VALS.sat.function == CML_KEEP_VALUES) ||
		    (VALS.val.function == CML_KEEP_VALUES))
		  {
		    int	rgbi[3];
		    int	i;
		  
		    for (i = 0; i < src_bpp; i++)
		      rgbi[i] = src_buffer[offset_y * src_bpl
					  + (dx * VALS.scale + offset_x) * src_bpp + i];
		    if (src_is_gray && (VALS.val.function == CML_KEEP_VALUES))
		      b = rgbi[0];
		    else
		      {
			rgb_to_hsv (rgbi, rgbi + 1, rgbi + 2);

			r = (VALS.hue.function == CML_KEEP_VALUES) ? rgbi[0] : h;
			g = (VALS.sat.function == CML_KEEP_VALUES) ? rgbi[1] : s;
			b = (VALS.val.function == CML_KEEP_VALUES) ? rgbi[2] : v;
			hsv_to_rgb (&r, &g, &b);
		      }
		  }
		dest_offset = offset_y * dest_bpl + (dx * VALS.scale + offset_x) * dest_bpp;
		if (dest_is_gray)
		  dest_buffer[dest_offset++] = b;
		else
		  {
		    dest_buffer[dest_offset++] = r;
		    dest_buffer[dest_offset++] = g;
		    dest_buffer[dest_offset++] = b;
		  }
		if (dest_has_alpha)
		  dest_buffer[dest_offset] = 255;
		if ((!preview_p) && (++processed % (total / PROGRESS_UPDATE_NUM)) == 0)
		  gimp_progress_update ((double)processed /(double) total);
	      }
	}
      if (preview_p)
	for (i = 0; i < keep_height; i++)
	  gtk_preview_draw_row (GTK_PREVIEW (preview),
				dest_buffer + dest_bpl * i, 0, dy + i,
				width_by_pixel);
      else
	gimp_pixel_rgn_set_rect (&dest_rgn, dest_buffer, x1, y1 + dy,
				 width_by_pixel, keep_height);
    }
  if (preview_p)
    {
      gtk_widget_draw (preview, NULL);
      gdk_flush ();
    }
  else
    {
      gimp_progress_update (1.0);
      gimp_drawable_flush (drawable);
      gimp_drawable_merge_shadow (drawable->id, TRUE);
      gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
      gimp_drawable_detach (drawable);
    }

  return STATUS_SUCCESS;
}

static void
CML_compute_next_step (gint size,
		       gdouble **h, gdouble **s, gdouble **v,
		       gdouble **hn, gdouble **sn, gdouble **vn,
		       gdouble **haux, gdouble **saux, gdouble **vaux)
{
  int	index;

  for (index = 0; index < size; index++)
    (*hn)[index] = CML_next_value (*h, index, size,
				   (*s)[POS_IN_TORUS (index, size)],
				   (*v)[POS_IN_TORUS (index, size)],
				   &VALS.hue,
				   (*haux)[POS_IN_TORUS (index , size)]);
  for (index = 0; index < size; index++)
    (*sn)[index] = CML_next_value (*s, index, size,
				   (*v)[POS_IN_TORUS (index   , size)],
				   (*h)[POS_IN_TORUS (index   , size)],
				   &VALS.sat,
				   (*saux)[POS_IN_TORUS (index , size)]);
  for (index = 0; index < size; index++)
    (*vn)[index] = CML_next_value (*v, index, size,
				   (*h)[POS_IN_TORUS (index   , size)],
				   (*s)[POS_IN_TORUS (index   , size)],
				   &VALS.val,
				   (*vaux)[POS_IN_TORUS (index , size)]);
#define GD_SWAP(x, y)	{ gdouble *tmp = *x; *x = *y; *y = tmp; }
  GD_SWAP (h, hn);
  GD_SWAP (s, sn);
  GD_SWAP (v, vn);
#undef	SWAP
}

#define	AVE_DIST(x, y)	(((x) * (x) + (y) * (y))/ 2.0)
#define LOGISTICS(x)	logistic_function (param, x, power)
#define ENV_FACTOR(x)	(param->env_sensitivity * LOGISTICS (x))
#define C_ENV_FACTOR(x)	(param->mod_rate * ENV_FACTOR (x))
#define CHN_FACTOR(x)	(param->ch_sensitivity * LOGISTICS (x))
#define C_CHN_FACTOR(x)	(param->mod_rate * CHN_FACTOR (x))

static gdouble
CML_next_value (gdouble *vec, gint pos, gint size,
		gdouble c1, gdouble c2,
		CML_PARAM *param, gdouble power)
{
  gdouble	val = vec[pos];
  gdouble	diff = 0;
  gdouble	self_diff = 0;
  gdouble	by_env = 0;
  gdouble	self_mod_rate = 0;
  gdouble	hold_rate = 1 - param->mod_rate;
  gdouble	env_factor = 0;
  gint		index;

  self_mod_rate = (1 - param->env_sensitivity - param->ch_sensitivity);

  switch (param->arrange)
    {
    case ANTILOG:
      self_diff = self_mod_rate * LOGISTICS (1 - vec[pos]);
      for (index = 1; index <= param->diffusion_dist / 2; index++)
	env_factor += ENV_FACTOR (1 - vec[POS_IN_TORUS (pos + index, size)])
	  + ENV_FACTOR (1 - vec[POS_IN_TORUS (pos - index, size)]);
      if ((param->diffusion_dist % 2) == 1)
	env_factor += (ENV_FACTOR (1 - vec[POS_IN_TORUS (pos + index, size)])
		       + ENV_FACTOR (1 - vec[POS_IN_TORUS (pos - index, size)])) / 2;
      env_factor /= (gdouble) param->diffusion_dist;
      by_env = env_factor + (CHN_FACTOR (1 - c1) + CHN_FACTOR (1 - c2)) / 2;
      diff = param->mod_rate * (self_diff + by_env);
      val = hold_rate * vec[pos] + diff;
      break;
    case AVERAGE:
      self_diff = self_mod_rate * LOGISTICS (vec[pos]);
      for (index = 1; index <= param->diffusion_dist / 2; index++)
	env_factor += vec[POS_IN_TORUS (pos + index, size)] + vec[POS_IN_TORUS (pos - index, size)];
      if ((param->diffusion_dist % 2) == 1)
	env_factor += (vec[POS_IN_TORUS (pos + index, size)] + vec[POS_IN_TORUS (pos - index, size)]) / 2;
      env_factor /= (gdouble) param->diffusion_dist;
      by_env = ENV_FACTOR (env_factor) + (CHN_FACTOR (c1) + CHN_FACTOR (c2)) / 2;
      diff = param->mod_rate * (self_diff + by_env);
      val = hold_rate * vec[pos] + diff;
      break;
    case MULTIPLY_RANDOM0:
    case MULTIPLY_RANDOM1:
    case MULTIPLY_GRADIENT:
      {
	gdouble	tmp;
	
	tmp = power;
	power = param->power;
	self_diff = self_mod_rate * LOGISTICS (vec[pos]);
	for (index = 1; index <= param->diffusion_dist / 2; index++)
	  env_factor += ENV_FACTOR (vec[POS_IN_TORUS (pos + index, size)])
	    + ENV_FACTOR (vec[POS_IN_TORUS (pos - index, size)]);
	if ((param->diffusion_dist % 2) == 1)
	  env_factor += (ENV_FACTOR (vec[POS_IN_TORUS (pos + index, size)])
			 + ENV_FACTOR (vec[POS_IN_TORUS (pos - index, size)])) / 2;
	env_factor /= (gdouble) param->diffusion_dist;
	by_env = (env_factor + CHN_FACTOR (c1) + CHN_FACTOR (c2)) / 2;
	diff = pow (param->mod_rate * (self_diff + by_env), tmp);
	val = hold_rate * vec[pos] + diff;
	break;
      }
    case STANDARD:
    case RAND_POWER0:
    case RAND_POWER1:
    case RAND_POWER2:
    case RAND_AND_P:
    default:
      self_diff = self_mod_rate * LOGISTICS (vec[pos]);

      for (index = 1; index <= param->diffusion_dist / 2; index++)
	env_factor += ENV_FACTOR (vec[POS_IN_TORUS (pos + index, size)])
	  + ENV_FACTOR (vec[POS_IN_TORUS (pos - index, size)]);
      if ((param->diffusion_dist % 2) == 1)
	env_factor += (ENV_FACTOR (vec[POS_IN_TORUS (pos + index, size)])
		       + ENV_FACTOR (vec[POS_IN_TORUS (pos - index, size)])) / 2;
      env_factor /= (gdouble) param->diffusion_dist;
      by_env = env_factor + (CHN_FACTOR (c1) + CHN_FACTOR (c2)) / 2;
      diff = param->mod_rate * (self_diff + by_env);
      val = hold_rate * vec[pos] + diff;
      break;
    }
  /* finalize */
  if (RANDOM < param->mutation_rate)
    {
      val += ((RANDOM < 0.5) ? -1.0 : 1.0) * param->mutation_dist * RANDOM;
    }
  if (param->cyclic_range)
    {
      if (1.0 < val)
	val = val - (int) val;
      else if (val < 0.0)
	val = val - floor (val);
    }
  else
    /* The range of val should be [0,1], not [0,1).
      Cannonization shuold be done in color mapping phase. */
    val = CLAMP (val, 0.0, 1);
  return val;
}
#undef	AVE_DIST
#undef LOGISTICS
#undef ENV_FACTOR
#undef C_ENV_FACTOR
#undef CHN_FACTOR
#undef C_CHN_FACTOR

static gdouble
logistic_function (CML_PARAM *param, gdouble x, gdouble power)
{
  gdouble x1 = x;
  gdouble result = 0;
  int n = param->range_num;
  int step;

  step = (int) (x * (gdouble) n);
  x1 = (x - ((gdouble) step / (gdouble) n)) * n;
  switch (param->function)
    {
    case CML_KEEP_VALUES:
    case CML_KEEP_FIRST:
      result = x;
      return result;
      break;
    case CML_FILL:
      result = CLAMP (param->parameter_k, 0.0, 1.0);
      return result;
      break;
    case CML_LOGIST:
      result = param->parameter_k * pow (4 * x1 * (1.0 - x1), power);
      break;
    case CML_LOGIST_STEP:
      result = param->parameter_k * pow (4 * x1 * (1.0 - x1), power);
      result = (result + step) / (gdouble) n;
      break;
    case CML_POWER:
      result = param->parameter_k * pow (x1, power);
      break;
    case CML_POWER_STEP:
      result = param->parameter_k * pow (x1, power);
      result = (result + step) / (gdouble) n;
      break;
    case CML_REV_POWER:
      result = param->parameter_k * (1 - pow (x1, power));
      break;
    case CML_REV_POWER_STEP:
      result = param->parameter_k * (1 - pow (x1, power));
      result = (result + step) / (gdouble) n;
      break;
    case CML_DELTA:
      result = param->parameter_k * 2 * ((x1 < 0.5) ? x1 : (1.0 - x1));
      break;
    case CML_DELTA_STEP:
      result = param->parameter_k * 2 * ((x1 < 0.5) ? x1 : (1.0 - x1));
      result = (result + step) / (gdouble) n;
      break;
    case CML_SIN_CURVE:
      if (1.0 < power)
	result = 0.5 * (sin (M_PI * ABS (x1 - 0.5) / power) / sin (M_PI * 0.5 / power) + 1);
      else
	result = 0.5 * (pow (sin (M_PI * ABS (x1 - 0.5)), power) + 1);
      if (x1 < 0.5) result = 1 - result;
      break;
    case CML_SIN_CURVE_STEP:
      if (1.0 < power)
	result = 0.5 * (sin (M_PI * ABS (x1 - 0.5) / power) / sin (M_PI * 0.5 / power) + 1);
      else
	result = 0.5 * (pow (sin (M_PI * ABS (x1 - 0.5)), power) + 1);
      if (x1 < 0.5) result = 1 - result;
      result = (result + step) / (gdouble) n;
      break;
    }
  switch (param->composition)
    {
    case COMP_NONE:
      break;
    case COMP_MAX_LINEAR:
      result = MAX ((gdouble) x, (gdouble) result);
      break;
    case COMP_MAX_LINEAR_P1:
      result = MAX ((gdouble) x + (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MAX_LINEAR_P1L:
      if (x < 0.5)
	result = MAX ((gdouble) x + (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MAX_LINEAR_P1U:
      if (0.5 < x)
	result = MAX ((gdouble) x + (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MAX_LINEAR_M1:
      result = MAX ((gdouble) x - (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MAX_LINEAR_M1L:
      if (x < 0.5)
	result = MAX ((gdouble) x - (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MAX_LINEAR_M1U:
      if (0.5 < x)
	result = MAX ((gdouble) x - (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MIN_LINEAR:
      result = MIN ((gdouble) x, (gdouble) result);
      break;
    case COMP_MIN_LINEAR_P1:
      result = MIN ((gdouble) x + (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MIN_LINEAR_P1L:
      if (x < 0.5)
	result = MIN ((gdouble) x + (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MIN_LINEAR_P1U:
      if (0.5 < x)
	result = MIN ((gdouble) x + (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MIN_LINEAR_M1:
      result = MIN ((gdouble) x - (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MIN_LINEAR_M1L:
      if (x < 0.5)
	result = MIN ((gdouble) x - (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    case COMP_MIN_LINEAR_M1U:
      if (0.5 < x)
	result = MIN ((gdouble) x - (gdouble) 1 / (gdouble) 256, (gdouble) result);
      break;
    }
  return result;
}

static void
rgb_to_hsv (int *r,
	    int *g,
	    int *b)
{
  int red, green, blue;
  float h = 0 , s, v;
  int min, max;
  int delta;

  red = *r;
  green = *g;
  blue = *b;

  if (red > green)
    {
      if (red > blue)
	max = red;
      else
	max = blue;

      if (green < blue)
	min = green;
      else
	min = blue;
    }
  else
    {
      if (green > blue)
	max = green;
      else
	max = blue;

      if (red < blue)
	min = red;
      else
	min = blue;
    }

  v = max;

  if (max != 0)
    s = ((max - min) * 255) / (float) max;
  else
    s = 0;

  if (s == 0)
    h = 0;
  else
    {
      delta = max - min;
      if (red == max)
	h = (green - blue) / (float) delta;
      else if (green == max)
	h = 2 + (blue - red) / (float) delta;
      else if (blue == max)
	h = 4 + (red - green) / (float) delta;
      h *= 42.5;

      if (h < 0)
	h += 255;
      if (h > 255)
	h -= 255;
    }

  *r = h;
  *g = s;
  *b = v;
}

static void
hsv_to_rgb (int *h,
	    int *s,
	    int *v)
{
  float hue, saturation, value;
  float f, p, q, t;

  if (*s == 0)
    {
      *h = *v;
      *s = *v;
      *v = *v;
    }
  else
    {
      hue = *h * 6.0 / 255.0;
      if (hue == 6.0)
	hue = 0.0;
      saturation = *s / 255.0;
      value = *v / 255.0;

      f = hue - (int) hue;
      p = value * (1.0 - saturation);
      q = value * (1.0 - (saturation * f));
      t = value * (1.0 - (saturation * (1.0 - f)));

      switch ((int) hue)
	{
	case 0:
	  *h = value * 255;
	  *s = t * 255;
	  *v = p * 255;
	  break;
	case 1:
	  *h = q * 255;
	  *s = value * 255;
	  *v = p * 255;
	  break;
	case 2:
	  *h = p * 255;
	  *s = value * 255;
	  *v = t * 255;
	  break;
	case 3:
	  *h = p * 255;
	  *s = q * 255;
	  *v = value * 255;
	  break;
	case 4:
	  *h = t * 255;
	  *s = p * 255;
	  *v = value * 255;
	  break;
	case 5:
	  *h = value * 255;
	  *s = p * 255;
	  *v = q * 255;
	  break;
	}
    }
}

/* dialog stuff */
static int
DIALOG ()
{
  GtkWidget	*dlg;
  GtkWidget	*hbox;
  GtkWidget	*button;
1307
  GtkTooltips *tooltips;
Elliot Lee's avatar
Elliot Lee committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
  gchar		**argv;
  gint		argc;

  argc = 1;
  argv = g_new (gchar *, 1);
  argv[0] = g_strdup (PLUG_IN_NAME);
  gtk_init (&argc, &argv);
  gtk_rc_parse (gimp_gtkrc ());
  
  dlg = gtkW_dialog_new ("Coupled-Map-Lattice Explorer",
			 (GtkSignalFunc) OK_CALLBACK,
			 (GtkSignalFunc) gtkW_close_callback);

  memset(&widget_pointers, (int)0, sizeof(widget_pointers));

  CML_preview_defer = TRUE;
  hbox = gtkW_hbox_new (GTK_DIALOG (dlg)->vbox);
  {
    GtkWidget *table;
    GtkWidget *frame;
    GtkWidget *preview_box;
    GtkWidget *preview_table;
    GtkWidget *preview_frame;
    GtkWidget *hseparator;

    table = gtkW_table_new (hbox, 8, 1);
    frame = gtkW_frame_new (NULL, "Preview");
    gtk_table_attach (GTK_TABLE (table), frame, 0, 1, 0, 1,
		      0, 0, 0, gtkW_border_width);
    preview_box = gtkW_hbox_new (frame);
    gtkW_border_width = 0;
    preview_table = gtkW_table_new (preview_box, 1, 1);
    gtkW_border_width = GTKW_BORDER_WIDTH;
    gtkW_frame_shadow_type = GTK_SHADOW_IN;
    preview_frame = gtkW_frame_new (NULL, NULL);
    gtkW_frame_shadow_type = GTK_SHADOW_ETCHED_IN;
    gtk_widget_set_usize (preview_frame, 0, 0);

    gtk_container_border_width (GTK_CONTAINER (preview_frame), 0);
    gtk_table_attach (GTK_TABLE (preview_table), preview_frame,
		      0, 1, 0, 1, 0, 0, 0, 0);

    /* I don't understand well about the following codes */
    gdk_set_use_xshm (gimp_use_xshm ());
    gtk_preview_set_gamma (gimp_gamma ());
    gtk_preview_set_install_cmap (gimp_install_cmap ());
    {
      guchar *color_cube;
      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 ());

    preview = gtk_preview_new (GTK_PREVIEW_COLOR);
    gtk_preview_size (GTK_PREVIEW (preview), GTKW_PREVIEW_WIDTH, GTKW_PREVIEW_HEIGHT);
    gtk_container_add (GTK_CONTAINER (preview_frame), preview);
    gtk_widget_show (preview);

    {
      GtkWidget *hbox = gtkW_hbox_new (NULL);

      gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 1, 2,
			GTK_FILL, GTK_FILL|GTK_EXPAND, 0, 0);
    }

    button = gtkW_table_add_button (table, "New seed", 0, 1, 2,
				    (GtkSignalFunc) CML_preview_update_callback,
				    &VALS);
    random_sensitives[0].widget = button;
    random_sensitives[0].logic = TRUE;

    button = gtkW_table_add_button (table, "Fix seed", 0, 1, 3,
				    (GtkSignalFunc) CML_set_or_randomize_seed_callback,
				    &VALS);
    random_sensitives[1].widget = button;
    random_sensitives[1].logic = TRUE;

    button = gtkW_table_add_button (table, "Random seed", 0, 1, 4,
				    (GtkSignalFunc) CML_set_or_randomize_seed_callback,
				    &VALS);
    random_sensitives[2].widget = button;
    random_sensitives[2].logic = FALSE;

    hseparator = gtk_hseparator_new ();
    gtk_widget_show (hseparator);
    gtk_table_attach (GTK_TABLE (table), hseparator, 0, 1, 5, 6,
		      GTK_FILL, GTK_FILL,
		      gtkW_border_width, 2 * gtkW_border_width);

    gtkW_table_add_button (table, "Load", 0, 1, 6,
			   (GtkSignalFunc) CML_load_from_file_callback,
			   &VALS);
    gtkW_table_add_button (table, "Save", 0, 1, 7,
			   (GtkSignalFunc) CML_save_to_file_callback,
			   &VALS);
  }

  {
    GtkWidget *notebook;
    GtkWidget *label;
    GtkWidget *page;
    GtkWidget *parent = hbox;

    notebook = gtk_notebook_new ();
    gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
    gtk_box_pack_start (GTK_BOX (parent), notebook, TRUE, TRUE, 0);
    gtk_widget_show (notebook);
    parent = NULL;

    page = CML_dialog_sub_panel_new (parent, "Hue settings", &VALS.hue, 0);
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page,
			      gtk_label_new ("Hue"));

    page = CML_dialog_sub_panel_new (parent, "Saturation settings", &VALS.sat, 1);
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page,
			      gtk_label_new ("Saturation"));

    page = CML_dialog_sub_panel_new (parent, "Value (grayimage) settings", &VALS.val, 2);
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page,
			      gtk_label_new ("Value"));

    page = CML_dialog_advanced_panel_new (parent, "Advanced settings");
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook), page,
			      gtk_label_new ("Advanced"));

    {
      GtkWidget	*table;
      GtkWidget *subtable;
      GtkWidget *frame;
      GtkWidget *subframe;
      GtkWidget *box;
      GtkWidget *vbox;
      gint	index;
      gint	sindex;

      frame = gtkW_frame_new (NULL, "Other parameter settings");
      vbox = gtkW_vbox_new (frame);
      table = gtkW_table_new (vbox, 6, 1);
      index = 0;
      subframe = gtkW_frame_new (NULL, "Channel independed parameters");
      box = gtkW_vbox_new (subframe);
      subtable = gtkW_table_new (box, 3, 2);
      sindex = 0;
      gtkW_table_add_menu (subtable, "Initial value", 0, sindex++,
			   (GtkSignalFunc) CML_initial_value_menu_update,
			   &VALS.initial_value,
			   initial_value_menu,
			   sizeof (initial_value_menu) / sizeof (initial_value_menu[0]),
			   &widget_pointers[3][0]);
      gtkW_table_add_iscale_entry (subtable, "Zoom scale", 0, sindex++,
				   (GtkSignalFunc) gtkW_iscale_update,
				   (GtkSignalFunc) gtkW_ientry_update,
				   &VALS.scale,
				   1, 10, 1, &widget_pointers[3][1]);
      gtkW_table_add_iscale_entry (subtable, "Start offset", 0, sindex++,
				   (GtkSignalFunc) gtkW_iscale_update,
				   (GtkSignalFunc) gtkW_ientry_update,
				   &VALS.start_offset,
				   0, 100, 1, &widget_pointers[3][2]);

      gtk_table_attach (GTK_TABLE (table), subframe, 0, 1, index, index + 1 ,
			GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_SHRINK, 0, 0);
      index++;

      {
	GtkWidget *hbox = gtkW_hbox_new (NULL);

	gtkW_border_width = 0;
	gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, index, index + 1,
			  0, GTK_FILL|GTK_EXPAND, 0, 0);
	gtkW_border_width = GTKW_BORDER_WIDTH;
	index++;
      }

      subframe = gtkW_frame_new (NULL, "Seed of random (only for \"from seed\" modes)");
      box = gtkW_vbox_new (subframe);
      subtable = gtkW_table_new (box, 1, 2);
      sindex = 0;
      gtkW_table_add_iscale_entry (subtable, "Seed", 0, sindex++,
				   (GtkSignalFunc) gtkW_iscale_update,
				   (GtkSignalFunc) gtkW_ientry_update,
				   &VALS.seed, 0, 1 << 15, 1,
				   &widget_pointers[3][3]);
      random_sensitives[3].widget = subtable;
      random_sensitives[3].logic = FALSE;
      {
	GtkWidget *button;

	button = gtkW_vbox_add_button (box, "Switch to \"from seed\" with the last seed",
				       (GtkSignalFunc) CML_set_or_randomize_seed_callback,
				       &VALS);
	random_sensitives[4].widget = button;
	random_sensitives[4].logic = TRUE;

	tooltips = gtkW_tooltips_new (frame);
1505
	gtk_tooltips_set_tip  (tooltips, button, "\"Fix seed\" button is an alias of me.\nThe same seed produces the same image, if (1) the widths of images are same (this is the reason why image on drawable is different from preview), and (2) all mutation rates equal to zero.",NULL);
Elliot Lee's avatar
Elliot Lee committed
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
	gtk_tooltips_enable (tooltips);
      }
      gtk_table_attach (GTK_TABLE (table), subframe, 0, 1, index, index + 1,
			GTK_FILL|GTK_EXPAND, GTK_SHRINK|GTK_FILL, 0, 0);
      index++;

      {
	GtkWidget *hbox = gtkW_hbox_new (NULL);

	gtkW_border_width = 0;
	gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, index, index + 1,
			  0, GTK_FILL|GTK_EXPAND, 0, 0);
	gtkW_border_width = GTKW_BORDER_WIDTH;
	index++;
      }

      label = gtk_label_new ("Others");
      gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label);
    }
    {
      GtkWidget	*table, *subtable, *frame, *subframe, *box, *vbox;
      int	index, sindex;

      frame = gtkW_frame_new (NULL, "Misc operations");
      vbox = gtkW_vbox_new (frame);
      table = gtkW_table_new (vbox, 4, 1);
      index = 0;

      subframe = gtkW_frame_new (NULL, "Copy settings");
      box = gtkW_vbox_new (subframe);
      subtable = gtkW_table_new (box, 3, 2);
      sindex = 0;
      gtkW_table_add_menu (subtable, "Source ch.", 0, sindex++,
			   (GtkSignalFunc) gtkW_menu_update,
			   &copy_source,
			   channel_menu,
			   sizeof (channel_menu) / sizeof (channel_menu[0]),
			   NULL);
      gtkW_table_add_menu (subtable, "Destination ch.", 0, sindex++,
			   (GtkSignalFunc) gtkW_menu_update,
			   &copy_destination,
			   channel_menu,
			   sizeof (channel_menu) / sizeof (channel_menu[0]),
			   NULL);
      gtkW_table_add_button (subtable, "Do copy parameters",
			     0, 2, sindex++,
			     (GtkSignalFunc) CML_copy_parameters_callback,
			     &VALS);
      gtk_table_attach (GTK_TABLE (table), subframe, 0, 1, index, index + 1,
			GTK_FILL, GTK_FILL, 0, 0);
      index++;

      {
	GtkWidget *hbox = gtkW_hbox_new (NULL);

	gtkW_border_width = 0;
	gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, index, index + 1,
			  GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
	gtkW_border_width = GTKW_BORDER_WIDTH;
	index++;
      }

      subframe = gtkW_frame_new (NULL, "Selective load settings");
      box = gtkW_vbox_new (subframe);
      subtable = gtkW_table_new (box, 2, 2);
      sindex = 0;
      gtkW_table_add_menu (subtable, "Source ch. in file", 0, sindex++,
			   (GtkSignalFunc) gtkW_menu_update,
			   &selective_load_source,
			   sload_menu,
			   sizeof (sload_menu) / sizeof (sload_menu[0]),
			   NULL);
      gtkW_table_add_menu (subtable, "Destination ch.", 0, sindex++,
			   (GtkSignalFunc) gtkW_menu_update,
			   &selective_load_destination,
			   sload_menu,
			   sizeof (sload_menu) / sizeof (sload_menu[0]),
			   NULL);
      gtk_table_attach (GTK_TABLE (table), subframe, 0, 1, index, index + 1,
			GTK_FILL, GTK_FILL, 0, 0);
      index++;

      {
	GtkWidget *hbox = gtkW_hbox_new (NULL);

	gtkW_border_width = 0;
	gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, index, index + 1,
			  GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
	gtkW_border_width = GTKW_BORDER_WIDTH;
	index++;
      }

      label = gtk_label_new ("Misc ops.");
      gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label);
    }
  }
  CML_initial_value_sensitives_update ();

  /* Displaying preview might takes a long time. Thus, fisrt, dialog itself
     should be shown before making preview in it. */
  gtk_widget_show (dlg);
  gdk_flush ();
  CML_preview_defer = FALSE;
  preview_update ();

  gtk_main ();
1612 1613 1614

  gtk_object_unref (GTK_OBJECT (tooltips));

Elliot Lee's avatar
Elliot Lee committed
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
  gdk_flush ();

  return INTERFACE.run;
}


static GtkWidget *
CML_dialog_sub_panel_new (GtkWidget	*parent,
			  guchar	*name,
			  CML_PARAM	*param,
			  gint		channel_id)
{
  GtkWidget *subframe, *table;
  gpointer *chank;
  int	index = 0;

  subframe = gtkW_frame_new (parent, name);
  table = gtkW_table_new (subframe, 14, 2);

  gtkW_table_add_menu (table, "Function type", 0, index,
		       (GtkSignalFunc) CML_menu_update,
		       &param->function,
		       function_menu,
		       sizeof (function_menu) / sizeof (function_menu[0]),
		       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_menu (table, "Composition", 0, index,
		       (GtkSignalFunc) CML_menu_update,
		       &param->composition,
		       composition_menu,
		       sizeof (composition_menu) / sizeof (composition_menu[0]),
		       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_menu (table, "Misc arrange", 0, index,
		       (GtkSignalFunc) CML_menu_update,
		       &param->arrange,
		       arrange_menu,
		       sizeof (arrange_menu) / sizeof (arrange_menu[0]),
		       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_toggle (table, "Use cyclic range", 0, index,
			 (GtkSignalFunc) gtkW_toggle_update,
			 &param->cyclic_range,
			 &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_dscale_entry (table, "Mod. rate", 0, index,
			       (GtkSignalFunc) gtkW_dscale_update,
			       (GtkSignalFunc) gtkW_dentry_update,
			       &param->mod_rate,
			       0.0, 1.0, 0.01,
			       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_dscale_entry (table, "Env. sensitivity", 0, index,
			       (GtkSignalFunc) gtkW_dscale_update,
			       (GtkSignalFunc) gtkW_dentry_update,
			       &param->env_sensitivity,
			       0.0, 1.0, 0.01,
			       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_iscale_entry (table, "Diffusion dist.", 0, index,
			       (GtkSignalFunc) gtkW_iscale_update,
			       (GtkSignalFunc) gtkW_ientry_update,
			       &param->diffusion_dist,
			       2, 10, 1,
			       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_iscale_entry (table, "# of subranges", 0, index,
			       (GtkSignalFunc) gtkW_iscale_update,
			       (GtkSignalFunc) gtkW_ientry_update,
			       &param->range_num,
			       1, 10, 1,
			       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_dscale_entry (table, "P(ower factor)", 0, index,
			       (GtkSignalFunc) gtkW_dscale_update,
			       (GtkSignalFunc) gtkW_dentry_update,
			       &param->power,
			       0.0, 10.0, 0.01,
			       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_dscale_entry (table, "Parameter k", 0, index,
			       (GtkSignalFunc) gtkW_dscale_update,
			       (GtkSignalFunc) gtkW_dentry_update,
			       &param->parameter_k,
			       0.0, 10.0, 0.01,
			       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_dscale_entry (table, "Range low", 0, index,
			       (GtkSignalFunc) gtkW_dscale_update,
			       (GtkSignalFunc) gtkW_dentry_update,
			       &param->range_l,
			       0, 1.0, 0.01,
			       &widget_pointers[channel_id][index]);
  index++;
  gtkW_table_add_dscale_entry (table, "Range high", 0, index,
			       (GtkSignalFunc) gtkW_dscale_update,
			       (GtkSignalFunc) gtkW_dentry_update,
			       &param->range_h,
			       0, 1.0, 0.01,
			       &widget_pointers[channel_id][index]);
  index++;
  {
    GtkWidget *hbox = gtkW_hbox_new (NULL);

    gtkW_border_width = 0;
    gtk_table_attach (GTK_TABLE (table), hbox, 0, 2, index, index + 1,
		      0, GTK_FILL|GTK_EXPAND, 0, 0);
    gtkW_border_width = GTKW_BORDER_WIDTH;
    index++;
  }
  chank = (gpointer *) g_malloc (2 *sizeof (gpointer));
  chank[0] = (gpointer) channel_id;
  chank[1] = (gpointer) param;
  gtkW_table_add_button (table, "Plot the graph of the settings",
			 0, 2, index++,
			 (GtkSignalFunc) function_graph_new, chank);

  return subframe;
}

static GtkWidget *
CML_dialog_advanced_panel_new (GtkWidget *parent, guchar *name)
{
  GtkWidget	*frame;
  GtkWidget	*subframe;
  GtkWidget	*table;
  GtkWidget	*subtable;
  gint		index = 0;
  gint		widget_offset = 12;
  gint		channel_id;
  guchar	*ch_name[] = { "Hue", "Saturation", "Value" };
  CML_PARAM	*param;

  frame = gtkW_frame_new (parent, name);
  table = gtkW_table_new (frame, 3, 1);

  for (channel_id = 0; channel_id < 3; channel_id++)
    {
      param = (CML_PARAM *)&VALS + channel_id;

      subframe = gtkW_frame_new (NULL, ch_name[channel_id]);
      gtk_table_attach (GTK_TABLE (table), subframe,
			0, 1, channel_id, channel_id + 1,
			GTK_FILL, GTK_FILL, 0, 0);
      subtable = gtkW_table_new (subframe, 3, 2);

      index = 0;
      gtkW_table_add_dscale_entry (subtable, "Ch. sensitivity", 0, index,
				   (GtkSignalFunc) gtkW_dscale_update,
				   (GtkSignalFunc) gtkW_dentry_update,
				   &param->ch_sensitivity,
				   0.0, 1.0, 0.01,
				   &widget_pointers[channel_id][index + widget_offset]);
      index++;
      gtkW_table_add_dscale_entry (subtable, "Mutation rate", 0, index,
				   (GtkSignalFunc) gtkW_dscale_update,
				   (GtkSignalFunc) gtkW_dentry_update,
				   &param->mutation_rate,
				   0, 1.0, 0.01,
				   &widget_pointers[channel_id][index + widget_offset]);
      index++;
      gtkW_table_add_dscale_entry (subtable, "Mutation dist.", 0, index,
				   (GtkSignalFunc) gtkW_dscale_update,
				   (GtkSignalFunc) gtkW_dentry_update,
				   &param->mutation_dist,
				   0.0, 1.0, 0.01,
				   &widget_pointers[channel_id][index + widget_offset]);
    }
  return frame;
}

void
preview_update ()
{
  if (CML_preview_defer == FALSE)
    MAIN_FUNCTION (TRUE);
}

static void
function_graph_new (GtkWidget *widget, gpointer data)
{
  GtkWidget	*dlg;
  GtkWidget	*button;
  GtkWidget	*frame;
  GtkWidget	*vbox;
  GtkWidget	*preview;
  gint		channel_id = *(int *) data;
  CML_PARAM	*param = (CML_PARAM *) *((gpointer *)data + 1);

  dlg = gtk_dialog_new ();
  gtk_window_set_title (GTK_WINDOW (dlg), "Graph of the current settings");
  gtk_window_position (GTK_WINDOW (dlg), GTK_WIN_POS_MOUSE);
  gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
		      (GtkSignalFunc) gtkW_close_callback, NULL);
  /* Action Area */
  button = gtk_button_new_with_label ("OK");
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
			     (GtkSignalFunc) gtk_widget_destroy,
			     GTK_OBJECT(dlg));
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->action_area), button,
		      TRUE, TRUE, 0);
  gtk_widget_grab_default (button);
  gtk_widget_show (button);

  frame = gtkW_frame_new (GTK_DIALOG (dlg)->vbox, "The Graph");
  vbox = gtkW_vbox_new (frame);

  preview = gtk_preview_new (GTK_PREVIEW_COLOR);
  gtk_preview_size (GTK_PREVIEW (preview), 256, 256);
  gtk_box_pack_start (GTK_BOX (vbox), preview, TRUE, TRUE, 5);
  {
    int	x, y, last_y, yy;
    guchar rgbc[3];
    int rgbi[3];
    guchar black[] = { 0, 0, 0 };
    guchar white[] = { 255, 255, 255 };

    for (x = 0; x < 256; x++)
      {
#define FAST_AND_FULLCOLOR	1
#ifdef FAST_AND_FULLCOLOR
	/* hue */
	rgbi[0] = rgbi[1] = rgbi[2] = 127;
	if ((0 <= channel_id) && (channel_id <= 2))
	  rgbi[channel_id] = CANNONIZE ((*param), ((gdouble) x / (gdouble) 255));
	hsv_to_rgb (rgbi, rgbi+1, rgbi+2);
	for (y = 0; y < 3; y++)
	  rgbc[y] = rgbi[y];
#endif
	for (y = 0; y < 256; y++)
	  gtk_preview_draw_row (GTK_PREVIEW (preview),
#ifdef FAST_AND_FULLCOLOR
				rgbc,
#else
				white,
#endif
				x, y, 1);
	}
    y = 255 * CLAMP (logistic_function (param, x/(gdouble)255, param->power),
		     0, 1.0);
    for (x = 0; x < 256; x++)
      {
	last_y = y;
	/* curve */
	gtk_preview_draw_row (GTK_PREVIEW (preview), white, x, 255 - x, 1);
	y = 255 * CLAMP (logistic_function (param, x/(gdouble)255, param->power),
			 0, 1.0);
	for (yy = MIN (y, last_y); yy <= MAX (y, last_y); yy++)
	  gtk_preview_draw_row (GTK_PREVIEW (preview), black, x, 255 - yy, 1);
      }
  }
  gtk_widget_show (preview);
  gtk_widget_show (dlg);

  gtk_main ();
  gdk_flush ();
}

static void
CML_set_or_randomize_seed_callback (GtkWidget *widget, gpointer data)
{
  CML_preview_defer = TRUE;
  switch (VALS.initial_value)
    {
    case CML_INITIAL_RANDOM_INDEPENDENT:
      VALS.initial_value = CML_INITIAL_RANDOM_FROM_SEED;
      break;
    case CML_INITIAL_RANDOM_SHARED:
      VALS.initial_value = CML_INITIAL_RANDOM_FROM_SEED_SHARED;
      break;
    case CML_INITIAL_RANDOM_FROM_SEED:
      VALS.initial_value = CML_INITIAL_RANDOM_INDEPENDENT;
      break;
    case CML_INITIAL_RANDOM_FROM_SEED_SHARED:
      VALS.initial_value = CML_INITIAL_RANDOM_SHARED;
      break;
    default:
      break;
    }
  if (widget_pointers[3][3].widget && widget_pointers[3][3].updater)
    (widget_pointers[3][3].updater) (widget_pointers[3]+3);
  if (widget_pointers[3][0].widget && widget_pointers[3][0].updater)
    (widget_pointers[3][0].updater) (widget_pointers[3]);
  CML_initial_value_sensitives_update ();
  gdk_flush ();
  CML_preview_defer = FALSE;
}

static void
CML_copy_parameters_callback (GtkWidget *widget, gpointer data)
{
  int index;
  gtkW_widget_table *widgets;

  if (copy_source == copy_destination)
    {
1912
      gtkW_message_dialog (TRUE, "Warning: the source and the destination are the same channel.");
Elliot Lee's avatar
Elliot Lee committed
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983