-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmathmap.c
3124 lines (2470 loc) · 81.9 KB
/
mathmap.c
1
2
3
4
5
6
7
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
83
84
85
86
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
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
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
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* MathMap plug-in --- generate an image by means of a mathematical expression
* Copyright (C) 1997-2010 Mark Probst
*
* Copyright (C) 2008 Serge van Thillo
*
* Plug-In structure based on:
* Whirl plug-in --- distort an image into a whirlpool
* Copyright (C) 1997 Federico Mena Quintero
*
* 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.
*/
#define MATHMAP_MANUAL_URL "http://www.complang.tuwien.ac.at/schani/mathmap/manual.html"
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#ifdef THREADED_FINAL_RENDER
#include <pthread.h>
#endif
#include <glib.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include <gsl/gsl_errno.h>
#ifdef USE_GTKSOURCEVIEW
#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksourcelanguage.h>
#ifdef USE_GTKSOURCEVIEW1
#include <gtksourceview/gtksourcelanguagesmanager.h>
#else
#include <gtksourceview/gtksourcelanguagemanager.h>
#endif
#endif
#include "exprtree.h"
#include "builtins/builtins.h"
#include "tags.h"
#include "scanner.h"
#include "vars.h"
#include "userval.h"
#include "internals.h"
#include "macros.h"
#include "jump.h"
#include "mathmap.h"
#include "expression_db.h"
#include "designer/designer.h"
#define DEFAULT_PREVIEW_SIZE 384
/* Even more stuff from Quartics plugins */
#define CHECK_SIZE 8
#define CHECK_DARK ((int) (1.0 / 3.0 * 255))
#define CHECK_LIGHT ((int) (2.0 / 3.0 * 255))
#ifdef __MINGW32__
#define SOURCEVIEW_FONT "Courier New 10"
#else
#define SOURCEVIEW_FONT "DejaVu Sans Mono Book 7"
#endif
#define EXPRESSIONS_DIR "expressions"
#define DEFAULT_EXPRESSION \
"# Welcome to MathMap!\n" \
"\n" \
"# MathMap is a programmable tool for image manipulation.\n" \
"\n" \
"# Here in the \"Expression\" tab is where you can enter and\n" \
"# modify MathMap programs, called \"filters\". To get you\n" \
"# started there is a very simple filter which doesn't modify\n" \
"# its input image down below.\n" \
"\n" \
"# In the \"Filters\" tab you'll find lots of more interesting\n" \
"# filters you can try out, study and modify. Most of those\n" \
"# filters have parameters that you can play around with in\n" \
"# the \"User Values\" tab.\n" \
"\n" \
"# Finally, the \"Composer\" tab provides a graphical way of\n" \
"# combining two or more filters into a more complex\n" \
"# \"composition\", which in turn can be used in yet more\n" \
"# complex ones.\n" \
"\n" \
"\n" \
"# This is the simple demo filter. It's called \"demo\".\n" \
"# \"image in\" means that the filter takes a single input,\n" \
"# namely an image that we call \"in\".\n" \
"filter demo (image in)\n" \
" # A filter must calculate a pixel at a specific position\n" \
" # which is called \"xy\", which is a coordinate pair consisting\n" \
" # of the X-coordinate \"x\" and the Y-coordinate \"y\".\n" \
"\n" \
" # Here we just get the pixel at the same position from the\n" \
" # input image:\n" \
" in(xy)\n" \
"\n" \
" # Another. even simpler possibility, is to always produce\n" \
" # the same color, for example:\n" \
" #grayColor(0.5)\n" \
"\n" \
" # Or we can produce one color if the X-coordinate is\n" \
" # positive, and a different one otherwise:\n" \
" #if x>0 then rgbColor(1,0,0) else rgbColor(0,1,0) end\n" \
"end\n"
#define DEFAULT_NUMBER_FRAMES 10
#define FLAG_ANTIALIASING 1
#define FLAG_SUPERSAMPLING 2
#define FLAG_ANIMATION 4
#define FLAG_PERIODIC 8
#define MAX_EXPRESSION_LENGTH 65536
/***** Types *****/
typedef struct {
gint flags;
gint frames;
gfloat param_t;
gchar expression[MAX_EXPRESSION_LENGTH];
} mathmap_vals_t;
typedef struct {
GtkWidget *preview;
GdkPixbuf *pixbuf;
guchar *wimage;
gint run;
} mathmap_interface_t;
/***** Prototypes *****/
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void expression_copy (gchar *dest, const gchar *src);
static gboolean generate_code (void);
static void do_mathmap (int frame_num, float t);
static gint32 mathmap_layer_copy (gint32 layerID);
static void update_userval_table (void);
static void load_design (const char *filename);
static void update_gradient (void);
static gint mathmap_dialog (int);
static void dialog_update_preview (void);
static void dialog_scale_update (GtkAdjustment *adjustment, gint *value);
static void dialog_t_update (GtkAdjustment *adjustment, gfloat *value);
static void dialog_text_changed (GtkTextBuffer * buffer, gpointer user_data);
static void dialog_text_update (void);
static void dialog_antialiasing_update (GtkWidget *widget, gpointer data);
static void dialog_supersampling_update (GtkWidget *widget, gpointer data);
static void dialog_auto_preview_update (GtkWidget *widget, gpointer data);
static void dialog_fast_preview_update (GtkWidget *widget, gpointer data);
static void dialog_edge_behaviour_update (GtkWidget *widget, gpointer data);
static void dialog_edge_color_changed (GtkWidget *color_well, gpointer data);
static void dialog_animation_update (GtkWidget *widget, gpointer data);
static void dialog_periodic_update (GtkWidget *widget, gpointer data);
static void calc_preview_size (int max_width, int max_height, int *width, int *height);
static gboolean alloc_preview_pixbuf (int width, int height);
static void dialog_preview_size_allocate (GtkWidget *widget, GtkAllocation *allocation, gpointer user_data);
static void dialog_preview_callback (GtkWidget *widget, gpointer data);
/*static void dialog_preview_click (GtkWidget *widget, GdkEvent *event);*/
static void refresh_preview (void);
static void dialog_load_callback (GtkWidget *widget, gpointer data);
static void dialog_save_callback (GtkWidget *widget, gpointer data);
static void dialog_save_as_callback (GtkWidget *widget, gpointer data);
static void dialog_ok_callback (GtkWidget *widget, gpointer data);
static void dialog_help_callback (GtkWidget *widget, gpointer data);
static void dialog_about_callback (GtkWidget *widget, gpointer data);
static void dialog_tree_changed (GtkTreeSelection *tree, gpointer data);
static void dialog_response (GtkWidget *widget, gint response_id, gpointer data);
static void designer_tree_callback (GtkTreeSelection *tree, gpointer data);
static void design_save_callback (GtkWidget *widget, gpointer data);
static void design_save_as_callback (GtkWidget *widget, gpointer data);
/***** Variables *****/
GimpPlugInInfo PLUG_IN_INFO = {
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run /* run_proc */
}; /* PLUG_IN_INFO */
static mathmap_vals_t mmvals = {
FLAG_ANTIALIASING | FLAG_PERIODIC, /* flags */
DEFAULT_NUMBER_FRAMES, /* frames */
0.0, /* t */
DEFAULT_EXPRESSION /* expression */
}; /* mmvals */
static mathmap_interface_t wint = {
NULL, /* preview */
NULL, /* wimage */
FALSE /* run */
}; /* wint */
static GimpRunMode run_mode;
static gint32 image_id;
static gint32 layer_id;
static GimpDrawable *output_drawable;
static gint tile_width, tile_height;
gint sel_x1, sel_y1, sel_x2, sel_y2;
gint sel_width, sel_height;
static long num_pixels_requested = 0;
static gboolean ignore_dialog_tree_changes = FALSE;
static gboolean ignore_designer_tree_changes = FALSE;
//static int debug_tuples = 0;
//static pixel_debug_info_t pixel_debug_infos[PREVIEW_SIZE * PREVIEW_SIZE];
GtkTextBuffer *source_buffer;
GtkTextMark *source_marker = NULL;
GtkWidget *mathmap_dialog_window,
*expression_entry,
*animation_table,
*frame_table,
*edge_color_x_well,
*edge_color_y_well,
*uservalues_scrolled_window,
*uservalues_table,
*tree_scrolled_window,
*designer_widget,
*designer_tree_scrolled_window,
*notebook;
#ifdef THREADED_FINAL_RENDER
pthread_mutex_t get_gimp_pixel_mutex;
#define NUM_FINAL_RENDER_CPUS (get_num_cpus())
#else
#define NUM_FINAL_RENDER_CPUS 1
#endif
int previewing = 0, auto_preview = 1, fast_preview = 1;
int expression_changed = 1;
color_t gradient_samples[USER_GRADIENT_POINTS];
int output_bpp;
int edge_behaviour_x_mode = EDGE_BEHAVIOUR_COLOR;
int edge_behaviour_y_mode = EDGE_BEHAVIOUR_COLOR;
int fast_image_source_scale;
static GimpRGB edge_color_x = { 0.0, 0.0, 0.0, 0.0 };
static GimpRGB edge_color_y = { 0.0, 0.0, 0.0, 0.0 };
mathmap_t *mathmap = NULL;
mathmap_invocation_t *invocation = NULL;
static char *current_filename = NULL;
static char *current_design_filename = NULL;
static expression_db_t *filters_edb = NULL;
static expression_db_t *designer_edb = NULL;
static designer_design_type_t *the_design_type = NULL;
static designer_design_t *the_current_design = NULL;
/***** Functions *****/
/*****/
static void
expression_copy (gchar *dest, const gchar *src)
{
assert(strlen(src) < MAX_EXPRESSION_LENGTH);
strcpy(dest, src);
}
static void
set_current_filename (const char *new_filename)
{
if (current_filename != NULL)
g_free(current_filename);
if (new_filename == NULL)
current_filename = NULL;
else
current_filename = g_strdup(new_filename);
}
static void
set_current_design_filename (const char *new_filename)
{
if (current_design_filename != NULL)
g_free(current_design_filename);
if (new_filename == NULL)
current_design_filename = NULL;
else
current_design_filename = g_strdup(new_filename);
}
static void
set_filter_source (const char *source, const char *path)
{
set_current_filename(path);
delete_expression_marker();
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(source_buffer), source, strlen(source));
expression_copy(mmvals.expression, source);
}
/*****/
static gint
my_gimp_main (const GimpPlugInInfo *info, int argc, char *argv[])
{
int i;
gsl_set_error_handler_off();
init_gettext();
for (i = 0; i < USER_GRADIENT_POINTS; ++i)
{
float v = (float)i / (float)(USER_GRADIENT_POINTS - 1);
gradient_samples[i] = MAKE_RGBA_COLOR_FLOAT(v, v, v, 1.0);
}
for (i = 1; i < argc; ++i)
if (strcmp(argv[i], "-gimp") == 0)
{
cmd_line_mode = 0;
return gimp_main(info, argc, argv);
}
cmd_line_mode = 1;
return cmdline_main(argc, argv);
}
#define gimp_main my_gimp_main
MAIN()
#undef gimp_main
/*****/
static char*
get_rc_file_name (char *name, int global)
{
gchar *mathmap_name = (name == 0) ? "mathmap" : g_strconcat("mathmap", G_DIR_SEPARATOR_S, name, NULL);
gchar *filename;
assert(mathmap_name != 0);
if (global)
filename = g_strconcat(gimp_data_directory(), G_DIR_SEPARATOR_S, mathmap_name, NULL);
else
filename = gimp_personal_rc_file(mathmap_name);
assert(filename != 0);
if (name != 0)
g_free(mathmap_name);
return filename;
}
static char*
lookup_rc_file (char *name, gboolean report_error)
{
gchar *local_filename, *global_filename, *filename;
local_filename = get_rc_file_name(name, 0);
if (g_file_test(local_filename, G_FILE_TEST_EXISTS))
filename = local_filename;
else
{
global_filename = get_rc_file_name(name, 1);
if (g_file_test(global_filename, G_FILE_TEST_EXISTS))
{
g_free(local_filename);
filename = global_filename;
}
else
{
if (report_error)
g_warning(_("Could not find file `%s' - should be either `%s' or `%s'."),
name, local_filename, global_filename);
g_free(local_filename);
g_free(global_filename);
filename = NULL;
}
}
return filename;
}
/*****/
static expression_db_t*
read_expressions (void)
{
static char *path_local = 0, *path_global = 0;
expression_db_t *edb_local, *edb_global;
if (path_local == 0)
{
path_local = get_rc_file_name(EXPRESSIONS_DIR, 0);
path_global = get_rc_file_name(EXPRESSIONS_DIR, 1);
}
edb_local = read_expression_db(path_local);
edb_global = read_expression_db(path_global);
edb_global = merge_expression_dbs(edb_global, edb_local);
free_expression_db(edb_local);
return edb_global;
}
static void
register_expression_db (expression_db_t *edb, char *symbol_prefix, char *menu_prefix)
{
int symbol_prefix_len = strlen(symbol_prefix);
int menu_prefix_len = strlen(menu_prefix);
for (; edb != 0; edb = edb->next)
{
char *symbol, *menu;
int i;
int name_len;
name_len = strlen(edb->name);
symbol = g_malloc(symbol_prefix_len + name_len + 2);
sprintf(symbol, "%s_%s", symbol_prefix, edb->name);
menu = g_malloc(menu_prefix_len + name_len + 2);
sprintf(menu, "%s/%s", menu_prefix, edb->name);
for (i = symbol_prefix_len + 1; i < symbol_prefix_len + 1 + name_len; ++i)
if (symbol[i] == ' ')
symbol[i] = '_';
else
symbol[i] = tolower(symbol[i]);
if (edb->kind == EXPRESSION_DB_GROUP)
register_expression_db(edb->v.group.subs, symbol, menu);
else
{
static GimpParamDef args[] = {
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
{ GIMP_PDB_INT32, "flags", "1: Antialiasing 2: Supersampling 4: Animate 8: Periodic" },
{ GIMP_PDB_INT32, "frames", "Number of frames" },
{ GIMP_PDB_FLOAT, "param_t", "The parameter t (if not animating)" },
{ GIMP_PDB_STRING, "expression", "The expression" }
};
static GimpParamDef *return_vals = NULL;
static int nargs = sizeof(args) / sizeof(args[0]);
static int nreturn_vals = 0;
gimp_plugin_menu_register (mathmap, "<Image>/Filters/Generic/");
#ifdef DEBUG_OUTPUT
fprintf(stderr, "registering %s (%s)\n", symbol, menu);
#endif
gimp_install_procedure(symbol,
"Generate an image using a mathematical expression.",
"Generates an image by means of a mathematical expression. The expression "
"can also refer to the data of an original image. Thus, arbitrary "
"distortions can be constructed. Even animations can be generated.",
"Mark Probst",
"Mark Probst",
MATHMAP_DATE ", " MATHMAP_VERSION,
menu,
"RGB*, GRAY*",
GIMP_PLUGIN,
nargs,
nreturn_vals,
args,
return_vals);
}
g_free(menu);
g_free(symbol);
}
}
static void
register_examples (void)
{
expression_db_t *edb = read_expressions();
if (edb == 0)
return;
register_expression_db(edb, "mathmap", "<Image>/Filters/Generic/MathMap");
free_expression_db(edb);
}
static char*
expression_for_symbol (const char *symbol, expression_db_t *edb)
{
for (; edb != 0; edb = edb->next)
{
int i;
int name_len;
int is_group = edb->kind == EXPRESSION_DB_GROUP;
name_len = strlen(edb->name);
if (name_len > strlen(symbol))
continue;
if ((!is_group && name_len != strlen(symbol))
|| (is_group && name_len == strlen(symbol)))
continue;
if (is_group && symbol[name_len] != '_')
continue;
for (i = 0; i < name_len; ++i)
if ((edb->name[i] == ' ' && symbol[i] != '_')
|| (edb->name[i] != ' ' && symbol[i] != tolower(edb->name[i])))
break;
if (i == name_len)
{
if (is_group)
{
char *exp = expression_for_symbol(symbol + name_len + 1, edb->v.group.subs);
if (exp != 0)
return exp;
}
else
return read_expression(edb->v.expression.path);
}
}
return 0;
}
static void
query(void)
{
static GimpParamDef args[] = {
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
{ GIMP_PDB_INT32, "flags", "1: Antialiasing 2: Supersampling 4: Animate 8: Periodic" },
{ GIMP_PDB_INT32, "frames", "Number of frames" },
{ GIMP_PDB_FLOAT, "param_t", "The parameter t (if not animating)" },
{ GIMP_PDB_STRING, "expression", "MathMap expression" }
};
static GimpParamDef *return_vals = NULL;
static int nargs = sizeof(args) / sizeof(args[0]);
static int nreturn_vals = 0;
gimp_install_procedure("plug_in_mathmap",
"Generate an image using a mathematical expression.",
"Generates an image by means of a mathematical expression. The expression "
"can also refer to the data of an original image. Thus, arbitrary "
"distortions can be constructed. Even animations can be generated.",
"Mark Probst",
"Mark Probst",
MATHMAP_DATE ", " MATHMAP_VERSION,
"<Image>/Filters/Generic/MathMap/MathMap",
"RGB*, GRAY*",
GIMP_PLUGIN,
nargs,
nreturn_vals,
args,
return_vals);
register_examples();
}
/*****/
static void
run (const gchar *name, gint nparams, const GimpParam *param, gint *nreturn_vals, GimpParam **return_vals)
{
static GimpParam values[1];
GimpPDBStatusType status;
int mutable_expression = 1;
input_drawable_t *drawable;
GimpDrawable *gimp_drawable;
int default_preview_width, default_preview_height;
if (strncmp(name, "mathmap_", 8) == 0)
{
char *exp = expression_for_symbol(name + 8, read_expressions());
if (exp != 0)
{
expression_copy(mmvals.expression, exp);
mutable_expression = 0;
}
}
status = GIMP_PDB_SUCCESS;
run_mode = param[0].data.d_int32;
image_id = param[1].data.d_int32;
layer_id = gimp_image_get_active_layer(image_id);
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = status;
*nreturn_vals = 1;
*return_vals = values;
/* Get the active drawable info */
gimp_drawable_mask_bounds(param[2].data.d_drawable, &sel_x1, &sel_y1, &sel_x2, &sel_y2);
sel_width = sel_x2 - sel_x1;
sel_height = sel_y2 - sel_y1;
tile_width = gimp_tile_width();
tile_height = gimp_tile_height();
/* Calculate preview size */
calc_preview_size(DEFAULT_PREVIEW_SIZE, DEFAULT_PREVIEW_SIZE,
&default_preview_width, &default_preview_height);
/* Calculate fast image source scaling factor */
fast_image_source_scale = MAX(sel_width / default_preview_width, 1);
assert (fast_image_source_scale >= 1);
/* Allocate drawable structure */
drawable = alloc_gimp_input_drawable(gimp_drawable_get(param[2].data.d_drawable), TRUE);
assert(drawable != 0);
output_bpp = drawable->v.gimp.bpp;
gimp_drawable = get_gimp_input_drawable(drawable);
/* Init MathMap engine */
init_builtins();
init_tags();
init_macros();
init_compiler();
#ifdef THREADED_FINAL_RENDER
pthread_mutex_init(&get_gimp_pixel_mutex, NULL);
#endif
/* See how we will run */
switch (run_mode) {
case GIMP_RUN_INTERACTIVE:
/* Possibly retrieve data */
gimp_get_data(name, &mmvals);
/* Get information from the dialog */
update_gradient();
if (!mathmap_dialog(mutable_expression))
return;
break;
case GIMP_RUN_NONINTERACTIVE:
/* Make sure all the arguments are present */
if (nparams != 7)
status = GIMP_PDB_CALLING_ERROR;
if (status == GIMP_PDB_SUCCESS)
{
mmvals.flags = param[3].data.d_int32;
mmvals.frames = param[4].data.d_int32;
mmvals.param_t = param[5].data.d_float;
expression_copy(mmvals.expression, param[6].data.d_string);
if (!generate_code())
status = GIMP_PDB_CALLING_ERROR;
}
break;
case GIMP_RUN_WITH_LAST_VALS:
/* Possibly retrieve data */
gimp_get_data(name, &mmvals);
if (!generate_code())
status = GIMP_PDB_CALLING_ERROR;
break;
default:
break;
} /* switch */
/* Mathmap the image */
if ((status == GIMP_PDB_SUCCESS)
&& (gimp_drawable_is_rgb(GIMP_DRAWABLE_ID(gimp_drawable))
|| gimp_drawable_is_gray(GIMP_DRAWABLE_ID(gimp_drawable))))
{
int animation_enabled = mmvals.flags & FLAG_ANIMATION;
update_gradient();
/* Set the tile cache size */
gimp_tile_cache_ntiles((gimp_drawable->width + gimp_tile_width() - 1)
/ gimp_tile_width());
/* Run! */
if (animation_enabled)
{
int frame;
gimp_image_undo_group_start(image_id);
for (frame = 0; frame < mmvals.frames; ++frame)
{
gint32 layer;
char layer_name[100];
float t;
if (mmvals.flags & FLAG_PERIODIC)
t = (double)frame / (double)mmvals.frames;
else if (mmvals.frames < 2)
t = 0.0;
else
t = (double)frame / (double)(mmvals.frames - 1);
layer = mathmap_layer_copy(layer_id);
sprintf(layer_name, _("Frame %d"), frame + 1);
gimp_drawable_set_name(layer, layer_name);
output_drawable = gimp_drawable_get(layer);
gimp_image_insert_layer(image_id, layer, 0, 0);
do_mathmap(frame, t);
}
gimp_image_undo_group_end(image_id);
}
else
{
output_drawable = gimp_drawable;
do_mathmap(-1, mmvals.param_t);
}
/* If run mode is interactive, flush displays */
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush();
/* Store data */
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data(name, &mmvals, sizeof(mathmap_vals_t));
} else if (status == GIMP_PDB_SUCCESS)
status = GIMP_PDB_EXECUTION_ERROR;
values[0].data.d_status = status;
gimp_drawable_detach(gimp_drawable);
#ifdef DEBUG_OUTPUT
g_print("%ld pixels requested\n", num_pixels_requested);
#endif
} /* run */
/*****/
void
mathmap_message_dialog (const char *message)
{
GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW(mathmap_dialog_window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
"%s", message);
g_signal_connect_swapped (dialog, "response",
G_CALLBACK (gtk_widget_destroy),
dialog);
gtk_window_set_title(GTK_WINDOW(dialog), _("MathMap Message"));
gtk_widget_show(dialog);
}
void
mathmap_message_dialog_modal (const char *message)
{
GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW(mathmap_dialog_window),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
"%s", message);
gtk_window_set_title(GTK_WINDOW(dialog), _("MathMap Message"));
gtk_widget_show(dialog);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
/*****/
static void
node_focussed_callback (GtkWidget *widget, designer_node_t *node)
{
char *source;
source = make_filter_source_from_design(node->design, "__composer_filter__");
set_filter_source(source, NULL);
g_free(source);
}
static void
design_changed_callback (GtkWidget *widget, designer_design_t *design)
{
designer_node_t *node = design->root;
if (node != NULL)
node_focussed_callback(widget, node);
}
static gboolean
node_title_change_callback (GtkWidget *widget, designer_node_t *node, const char *name)
{
if (strlen (name) == 0)
{
mathmap_message_dialog_modal(_("Node title cannot be empty"));
return FALSE;
}
if (designer_node_set_name (node, name))
{
design_changed_callback(widget, node->design);
return TRUE;
}
mathmap_message_dialog_modal(_("Another node already has that name"));
return FALSE;
}
static void
load_design (const char *filename)
{
designer_design_t *design;
design = designer_load_design(the_design_type, filename,
&designer_widget_design_loaded_callback,
&designer_widget_node_aux_load_callback,
NULL,
designer_widget);
if (design == NULL)
{
char *message = g_strdup_printf(_("Cannot read composer file `%s'"), filename);
mathmap_message_dialog(message);
g_free(message);
return;
}
/* FIXME: free old design */
the_current_design = design;
set_current_design_filename(filename);
design_changed_callback(designer_widget, the_current_design);
}
/*****/
static gint32
mathmap_layer_copy(gint32 layerID)
{
GimpParam *return_vals;
int nreturn_vals;
gint32 nlayer;
return_vals = gimp_run_procedure ("gimp_layer_copy",
&nreturn_vals,
GIMP_PDB_LAYER, layerID,
GIMP_PDB_INT32, TRUE,
GIMP_PDB_END);
if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
nlayer = return_vals[1].data.d_layer;
else
nlayer = -1;
gimp_destroy_params(return_vals, nreturn_vals);
return nlayer;
}
/*****/
gradient_t*
get_default_gradient (void)
{
static gradient_t gradient;
static gboolean inited = FALSE;
if (!inited)
{
gradient.values = gradient_samples;
inited = TRUE;
}
return &gradient;
}
static void
update_gradient (void)
{
gdouble *samples;
int i, num_samples;
gchar *gradient_name;
gradient_name = gimp_context_get_gradient();
assert(gradient_name != 0);
gimp_gradient_get_uniform_samples(gradient_name, USER_GRADIENT_POINTS, FALSE, &num_samples, &samples);
for (i = 0; i < num_samples / 4; ++i)
gradient_samples[i] = MAKE_RGBA_COLOR_FLOAT(samples[i * 4 + 0], samples[i * 4 + 1],
samples[i * 4 + 2], samples[i * 4 + 3]);
}
/*****/
static gboolean
generate_code (void)
{
if (expression_changed)
{
static char *support_paths[3];
mathmap_t *new_mathmap;
if (run_mode == GIMP_RUN_INTERACTIVE && expression_entry != 0)
dialog_text_update();
if (mathmap != 0)
unload_mathmap(mathmap);
if (!support_paths[0])
{
support_paths[0] = get_rc_file_name(NULL, FALSE);
support_paths[1] = get_rc_file_name(NULL, TRUE);
support_paths[2] = NULL;
}
new_mathmap = compile_mathmap(mmvals.expression, support_paths, DEFAULT_OPTIMIZATION_TIMEOUT, FALSE);
if (new_mathmap == 0)
{
g_print(_("Error: %s\n"), error_string);
mathmap_message_dialog(error_string);
if (scanner_region_is_valid(error_region))
set_expression_marker(error_region.start.row, error_region.start.column,