-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmathmap_common.c
1509 lines (1197 loc) · 37.4 KB
/
mathmap_common.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
/* -*- c -*- */
/*
* mathmap_common.c
*
* MathMap
*
* Copyright (C) 1997-2009 Mark Probst
*
* 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.
*/
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#ifdef USE_PTHREADS
#include <pthread.h>
#endif
#include <locale.h>
#include <unistd.h>
#ifdef __MINGW32__
#include <windows.h>
#endif
#include <glib.h>
#include <glib/gstdio.h>
#include "internals.h"
#include "tags.h"
#include "jump.h"
#include "scanner.h"
#include "compiler.h"
#include "mathmap.h"
#include "compiler-internals.h"
#include "native-filters/native-filters.h"
int cmd_line_mode = 0;
mathmap_t *the_mathmap = 0;
/* from parser.y */
int yyparse (void);
static unsigned int
image_flags_from_options (option_t *options)
{
unsigned int flags = 0;
if (find_option_with_name(options, "pixel") == NULL)
{
flags |= IMAGE_FLAG_UNIT;
if (find_option_with_name(options, "stretched") == NULL)
flags |= IMAGE_FLAG_SQUARE;
}
return flags;
}
unsigned int
filter_flags (filter_t *filter)
{
g_assert(filter->kind == FILTER_MATHMAP);
return image_flags_from_options(filter->v.mathmap.decl->v.filter.options);
}
userval_info_t*
arg_decls_to_uservals (filter_t *filter, arg_decl_t *arg_decls)
{
userval_info_t *infos = NULL;
while (arg_decls != 0)
{
userval_info_t *result = 0;
if (lookup_userval(infos, arg_decls->name) != NULL)
{
sprintf(error_string, _("The argument `%s' is declared more than once."), arg_decls->name);
error_region = arg_decls->region;
JUMP(1);
}
switch (arg_decls->type)
{
case ARG_TYPE_INT :
if (arg_decls->v.integer.have_limits)
result = register_int_const(&infos, arg_decls->name,
arg_decls->v.integer.min, arg_decls->v.integer.max,
arg_decls->v.integer.default_value);
else
result = register_int_const(&infos, arg_decls->name, -100000, 100000, 0);
break;
case ARG_TYPE_FLOAT :
if (arg_decls->v.floating.have_limits)
result = register_float_const(&infos, arg_decls->name,
arg_decls->v.floating.min, arg_decls->v.floating.max,
arg_decls->v.floating.default_value);
else
result = register_float_const(&infos, arg_decls->name, -1.0, 1.0, 0.0);
break;
case ARG_TYPE_BOOL :
result = register_bool(&infos, arg_decls->name, arg_decls->v.boolean.default_value);
break;
case ARG_TYPE_COLOR :
result = register_color(&infos, arg_decls->name);
break;
case ARG_TYPE_GRADIENT :
result = register_gradient(&infos, arg_decls->name);
break;
case ARG_TYPE_CURVE :
result = register_curve(&infos, arg_decls->name);
break;
case ARG_TYPE_FILTER :
assert(0);
case ARG_TYPE_IMAGE :
result = register_image(&infos, arg_decls->name,
image_flags_from_options(arg_decls->options));
break;
default :
assert(0);
}
if (result == 0)
{
sprintf(error_string, _("Conflict for argument %s."), arg_decls->name);
error_region = arg_decls->region;
JUMP(1);
}
arg_decls = arg_decls->next;
}
return infos;
}
static int
count_userval_infos (userval_info_t *info)
{
int count = 0;
while (info != NULL)
{
++count;
info = info->next;
}
return count;
}
void
register_args_as_uservals (filter_t *filter, arg_decl_t *arg_decls)
{
arg_decl_t *decl;
for (decl = arg_decls; decl != NULL; decl = decl->next)
if (lookup_internal(filter->v.mathmap.internals, decl->name, TRUE) != NULL
|| lookup_variable_macro(decl->name, NULL) != NULL)
{
sprintf(error_string, _("Argument `%s' has the same name as an internal variable."), decl->name);
error_region = decl->region;
JUMP(1);
}
g_assert(filter->userval_infos == NULL && filter->num_uservals == 0);
filter->userval_infos = arg_decls_to_uservals(filter, arg_decls);
filter->num_uservals = count_userval_infos(filter->userval_infos);
}
static void
init_internals (filter_t *filter)
{
g_assert(filter->kind == FILTER_MATHMAP);
register_internal(&filter->v.mathmap.internals, "x", CONST_Y | CONST_T);
register_internal(&filter->v.mathmap.internals, "y", CONST_X | CONST_T);
register_internal(&filter->v.mathmap.internals, "r", CONST_T);
register_internal(&filter->v.mathmap.internals, "a", CONST_T);
register_internal(&filter->v.mathmap.internals, "t", CONST_X | CONST_Y);
register_internal(&filter->v.mathmap.internals, "R", CONST_X | CONST_Y | CONST_T);
register_internal(&filter->v.mathmap.internals, "__canvasPixelW", CONST_X | CONST_Y | CONST_T);
register_internal(&filter->v.mathmap.internals, "__canvasPixelH", CONST_X | CONST_Y | CONST_T);
register_internal(&filter->v.mathmap.internals, "__renderPixelW", CONST_X | CONST_Y | CONST_T);
register_internal(&filter->v.mathmap.internals, "__renderPixelH", CONST_X | CONST_Y | CONST_T);
register_internal(&filter->v.mathmap.internals, "frame", CONST_X | CONST_Y);
/* These are resolved by the compiler as bindings, i.e. calculated
in the filter code */
register_internal(&filter->v.mathmap.internals, "X", CONST_X | CONST_Y | CONST_T);
register_internal(&filter->v.mathmap.internals, "Y", CONST_X | CONST_Y | CONST_T);
register_internal(&filter->v.mathmap.internals, "W", CONST_X | CONST_Y | CONST_T);
register_internal(&filter->v.mathmap.internals, "H", CONST_X | CONST_Y | CONST_T);
}
void
start_parsing_filter (mathmap_t *mathmap, top_level_decl_t *decl)
{
filter_t *filter;
g_assert(mathmap->current_filter == NULL);
if (lookup_filter(mathmap->filters, decl->name) != NULL)
{
sprintf(error_string, _("Filter `%s' is defined more than once."), decl->name);
error_region = decl->region;
JUMP(1);
}
filter = g_new0(filter_t, 1);
filter->kind = FILTER_MATHMAP;
filter->name = g_strdup(decl->name);
filter->v.mathmap.decl = decl;
init_internals(filter);
filter->next = mathmap->filters;
mathmap->filters = filter;
mathmap->current_filter = filter;
}
void
finish_parsing_filter (mathmap_t *mathmap)
{
filter_t *filter = mathmap->current_filter;
g_assert(filter != NULL);
mathmap->current_filter = 0;
}
static void
free_filters (filter_t *filter)
{
while (filter != 0)
{
filter_t *next = filter->next;
g_free(filter->name);
if (filter->userval_infos != NULL)
free_userval_infos(filter->userval_infos);
if (filter->kind == FILTER_MATHMAP && filter->v.mathmap.variables != NULL)
free_variables(filter->v.mathmap.variables);
else if (filter->kind == FILTER_NATIVE)
g_free(filter->v.native.func_name);
g_free(filter);
filter = next;
}
}
void
unload_mathmap (mathmap_t *mathmap)
{
if (mathmap->module_info != 0)
{
#ifdef USE_LLVM
unload_llvm_code(mathmap);
#else
unload_c_code(mathmap->module_info);
#endif
mathmap->module_info = 0;
}
}
void
free_mathmap (mathmap_t *mathmap)
{
if (mathmap->filters != 0)
free_filters(mathmap->filters);
unload_mathmap(mathmap);
free(mathmap);
}
void
free_invocation (mathmap_invocation_t *invocation)
{
if (invocation->uservals != 0)
{
free_uservals(invocation->uservals, invocation->mathmap->main_filter->userval_infos);
free(invocation->uservals);
}
free(invocation->rows_finished);
g_mutex_free(invocation->native_filter_cache_mutex);
g_cond_free(invocation->native_filter_cache_cond);
mathmap_pools_free(&invocation->pools);
free(invocation);
}
static filter_t*
register_native_filter (mathmap_t *mathmap, const char *name, userval_info_t *userval_infos,
gboolean needs_rendered_images, gboolean is_pure,
const char *filter_func_name, native_filter_func_t func)
{
filter_t *filter = g_new0(filter_t, 1);
filter->kind = FILTER_NATIVE;
filter->name = g_strdup(name);
filter->userval_infos = userval_infos;
filter->num_uservals = count_userval_infos(userval_infos);
filter->v.native.needs_rendered_images = needs_rendered_images;
filter->v.native.is_pure = is_pure;
filter->v.native.func_name = g_strdup(filter_func_name);
filter->v.native.func = func;
filter->next = mathmap->filters;
mathmap->filters = filter;
return NULL;
}
static void
register_native_filters (mathmap_t *mathmap)
{
userval_info_t *infos;
infos = NULL;
register_image(&infos, "in", 0);
register_float_const(&infos, "horizontal_std_dev", 0.0, 2.0, 0.01);
register_float_const(&infos, "vertical_std_dev", 0.0, 2.0, 0.01);
register_native_filter(mathmap, "gaussian_blur", infos, TRUE, TRUE,
"native_filter_gaussian_blur", &native_filter_gaussian_blur);
infos = NULL;
register_image(&infos, "in", 0);
register_image(&infos, "kernel", 0);
register_bool(&infos, "normalize", 1.0);
register_bool(&infos, "copy_alpha", 1.0);
register_native_filter(mathmap, "convolve", infos, TRUE, TRUE,
"native_filter_convolve", &native_filter_convolve);
infos = NULL;
register_image(&infos, "in", 0);
register_image(&infos, "mask", 0);
register_bool(&infos, "copy_alpha", 1.0);
register_native_filter(mathmap, "half_convolve", infos, TRUE, TRUE,
"native_filter_half_convolve", &native_filter_half_convolve);
infos = NULL;
register_image(&infos, "in", 0);
register_bool(&infos, "ignore_alpha", 1.0);
register_native_filter(mathmap, "visualize_fft", infos, TRUE, TRUE,
"native_filter_visualize_fft", &native_filter_visualize_fft);
}
#define X_INTERNAL_INDEX 0
#define Y_INTERNAL_INDEX 1
#define R_INTERNAL_INDEX 2
#define A_INTERNAL_INDEX 3
int
does_filter_use_ra (filter_t *filter)
{
internal_t *r_internal, *a_internal;
g_assert(filter->kind == FILTER_MATHMAP);
r_internal = lookup_internal(filter->v.mathmap.internals, "r", 1);
a_internal = lookup_internal(filter->v.mathmap.internals, "a", 1);
g_assert(r_internal != NULL && a_internal != NULL);
return r_internal->is_used || a_internal->is_used;
}
int
does_filter_use_t (filter_t *filter)
{
internal_t *t_internal;
g_assert(filter->kind == FILTER_MATHMAP);
t_internal = lookup_internal(filter->v.mathmap.internals, "t", 1);
g_assert(t_internal != NULL);
return t_internal->is_used;
}
mathmap_t*
parse_mathmap (char *expression)
{
static mathmap_t *mathmap; /* this is static to avoid problems with longjmp. */
volatile gboolean need_end_scan = FALSE;
mathmap = g_new0(mathmap_t, 1);
the_mathmap = mathmap;
register_native_filters(mathmap);
DO_JUMP_CODE {
filter_t *filter;
scanFromString(expression);
need_end_scan = TRUE;
yyparse();
endScanningFromString();
need_end_scan = FALSE;
if (mathmap->filters == NULL || mathmap->filters->kind != FILTER_MATHMAP)
{
free_filters(mathmap->filters);
mathmap->filters = 0;
sprintf(error_string, _("At least one filter must be defined."));
error_region = scanner_null_region;
JUMP(1);
}
for (filter = mathmap->filters; filter != 0; filter = filter->next)
{
exprtree *expr;
if (filter->kind != FILTER_MATHMAP)
continue;
if (filter->v.mathmap.decl->type != TOP_LEVEL_FILTER)
{
free_filters(mathmap->filters);
mathmap->filters = 0;
sprintf(error_string, _("Top-level declarations can only be filters."));
error_region = scanner_null_region;
JUMP(1);
}
expr = filter->v.mathmap.decl->v.filter.body;
if (expr->result.number != rgba_tag_number
|| expr->result.length != 4)
{
sprintf(error_string, _("The filter `%s' must have the result type rgba:4."), filter->name);
error_region = expr->region;
free_filters(mathmap->filters);
mathmap->filters = 0;
JUMP(1);
}
}
mathmap->main_filter = mathmap->filters;
mathmap->flags = 0;
} WITH_JUMP_HANDLER {
if (need_end_scan)
endScanningFromString();
free_mathmap(mathmap);
mathmap = 0;
} END_JUMP_HANDLER;
the_mathmap = 0;
return mathmap;
}
int
check_mathmap (char *expression)
{
mathmap_t *mathmap = parse_mathmap(expression);
if (mathmap != 0)
{
free_mathmap(mathmap);
return 1;
}
else
return 0;
}
mathmap_t*
compile_mathmap (char *expression, char **support_paths, int timeout, gboolean no_backend)
{
volatile mathmap_t *mathmap = NULL;
char *template_filename, *include_path;
int i;
for (i = 0; support_paths[i] != NULL; ++i)
{
template_filename = g_strdup_printf("%s/%s", support_paths[i], MAIN_TEMPLATE_FILENAME);
if (g_access(template_filename, R_OK) == 0)
break;
g_free(template_filename);
}
if (support_paths[i] == NULL)
{
GString *str = g_string_new("Could not find template file ");
g_string_append_printf(str,
"`%s'.\nMust be in one of the following paths:",
MAIN_TEMPLATE_FILENAME);
for (i = 0; support_paths[i] != NULL; ++i)
g_string_append_printf(str, "\n`%s'", support_paths[i]);
g_string_append(str, ".");
strcpy(error_string, str->str);
error_region = scanner_null_region;
g_string_free(str, TRUE);
return NULL;
}
include_path = support_paths[i];
DO_JUMP_CODE {
filter_code_t **filter_codes;
mathmap = parse_mathmap(expression);
if (mathmap == 0)
{
JUMP(1);
}
filter_codes = compiler_compile_filters((mathmap_t*)mathmap, timeout);
if (no_backend)
{
compiler_free_pools((mathmap_t*)mathmap);
return NULL;
}
#ifdef USE_LLVM
gen_and_load_llvm_code((mathmap_t*)mathmap, template_filename, filter_codes);
#else
mathmap->initfunc = gen_and_load_c_code(mathmap, &mathmap->module_info,
template_filename, include_path, filter_codes);
#endif
compiler_free_pools((mathmap_t*)mathmap);
if (mathmap->initfunc == 0 && mathmap->mathfuncs == 0)
{
char *message = g_strdup_printf(_("The MathMap compiler failed, for the following reason:\n%s"), error_string);
strcpy(error_string, message);
error_region = scanner_null_region;
g_free(message);
JUMP(1);
}
delete_expression_marker();
} WITH_JUMP_HANDLER {
if (mathmap != 0)
{
free_mathmap((mathmap_t*)mathmap);
mathmap = 0;
}
} END_JUMP_HANDLER;
return (mathmap_t*)mathmap;
}
void
llvm_filter_init_frame (mathmap_frame_t *mmframe, image_t *closure)
{
mathmap_invocation_t *invocation = mmframe->invocation;
#ifdef POOLS_DEBUG_OUTPUT
printf("initing frame %p (pools %p)\n", mmframe, &mmframe->pools);
#endif
mmframe->xy_vars = closure->v.closure.funcs->llvm_init_frame_func(invocation, closure, mmframe->current_t, &mmframe->pools);
}
void
llvm_filter_init_slice (mathmap_slice_t *slice, image_t *closure)
{
mathmap_frame_t *mmframe = slice->frame;
float t = mmframe->current_t;
mathmap_pools_t *pools = &slice->pools;
int col;
#ifdef POOLS_DEBUG_OUTPUT
printf("initing slice %p (pools %p)\n", slice, pools);
#endif
slice->y_vars = mathmap_pools_alloc(pools, sizeof(void*) * slice->region_width);
for (col = 0; col < slice->region_width; ++col)
{
float x = CALC_VIRTUAL_X(col + slice->region_x, mmframe->frame_render_width, slice->sampling_offset_x);
void *y_vars = closure->v.closure.funcs->init_y_func(slice, closure, x, t);
((void**)slice->y_vars)[col] = y_vars;
}
}
void
llvm_filter_calc_lines (mathmap_slice_t *slice, image_t *closure, int first_row, int last_row, void *q, int floatmap)
{
mathmap_frame_t *mmframe = slice->frame;
mathmap_invocation_t *invocation = mmframe->invocation;
int row, col;
float t = mmframe->current_t;
float sampling_offset_x = slice->sampling_offset_x, sampling_offset_y = slice->sampling_offset_y;
int output_bpp = invocation->output_bpp;
int is_bw = output_bpp == 1 || output_bpp == 2;
int need_alpha = output_bpp == 2 || output_bpp == 4;
int alpha_index = output_bpp - 1;
mathmap_pools_t pixel_pools;
mathmap_pools_t *pools;
int region_x = slice->region_x;
int frame_render_width = mmframe->frame_render_width;
int frame_render_height = mmframe->frame_render_height;
mathmap_pools_init_local(&pixel_pools);
pools = &pixel_pools;
#ifdef POOLS_DEBUG_OUTPUT
printf("calcing lines in slice %p with pools %p\n", slice, pools);
#endif
first_row = MAX(0, first_row);
last_row = MIN(last_row, slice->region_y + slice->region_height);
for (row = first_row - slice->region_y; row < last_row - slice->region_y; ++row)
{
float y = CALC_VIRTUAL_Y(row + slice->region_y, frame_render_height, sampling_offset_y);
unsigned char *p = q;
float *fp = q;
void *x_vars;
#ifdef POOLS_DEBUG_OUTPUT
printf("calcing x_vars for row %d\n", row);
#endif
x_vars = closure->v.closure.funcs->init_x_func(slice, closure, y, t);
for (col = 0; col < slice->region_width; ++col)
{
void *y_vars = ((void**)slice->y_vars)[col];
float x = CALC_VIRTUAL_X(col + region_x, frame_render_width, sampling_offset_x);
float *return_tuple;
mathmap_pools_reset(pools);
#ifdef POOLS_DEBUG_OUTPUT
printf("calcing row %d col %d\n", row, col);
#endif
return_tuple = closure->v.closure.funcs->main_filter_func(slice, closure, x_vars, y_vars, x, y, t, pools);
#ifdef POOLS_DEBUG_OUTPUT
printf("got return tuple %p\n", return_tuple);
#endif
if (floatmap)
{
int i;
for (i = 0; i < NUM_FLOATMAP_CHANNELS; ++i)
fp[i] = return_tuple[i];
}
else
{
if (is_bw)
p[0] = (TUPLE_RED(return_tuple) * 0.299
+ TUPLE_GREEN(return_tuple) * 0.587
+ TUPLE_BLUE(return_tuple) * 0.114) * 255.0;
else
{
p[0] = TUPLE_RED(return_tuple) * 255.0;
p[1] = TUPLE_GREEN(return_tuple) * 255.0;
p[2] = TUPLE_BLUE(return_tuple) * 255.0;
}
if (need_alpha)
p[alpha_index] = TUPLE_ALPHA(return_tuple) * 255.0;
}
p += output_bpp;
fp += NUM_FLOATMAP_CHANNELS;
}
if (floatmap)
q = (float*)q + frame_render_width * NUM_FLOATMAP_CHANNELS;
else
q = (unsigned char*)q + invocation->row_stride;
if (!invocation->supersampling)
invocation->rows_finished[row] = 1;
}
mathmap_pools_free(&pixel_pools);
}
static void
init_invocation (mathmap_invocation_t *invocation)
{
if (invocation->mathmap->mathfuncs != NULL)
{
invocation->mathfuncs = *invocation->mathmap->mathfuncs;
#ifdef USE_LLVM
g_assert(invocation->mathfuncs.init_frame == NULL
&& invocation->mathfuncs.init_slice == NULL
&& invocation->mathfuncs.calc_lines == NULL);
invocation->mathfuncs.init_frame = llvm_filter_init_frame;
invocation->mathfuncs.init_slice = llvm_filter_init_slice;
invocation->mathfuncs.calc_lines = llvm_filter_calc_lines;
#endif
}
else
{
g_assert(invocation->mathmap->initfunc != NULL);
invocation->mathfuncs = invocation->mathmap->initfunc(invocation);
}
}
void
invocation_set_antialiasing (mathmap_invocation_t *invocation, gboolean antialiasing)
{
invocation->antialiasing = antialiasing;
if (antialiasing)
invocation->orig_val_func = get_orig_val_intersample_pixel;
else
invocation->orig_val_func = get_orig_val_pixel;
}
mathmap_invocation_t*
invoke_mathmap (mathmap_t *mathmap, mathmap_invocation_t *template, int img_width, int img_height,
gboolean copy_first_image)
{
mathmap_invocation_t *invocation = (mathmap_invocation_t*)malloc(sizeof(mathmap_invocation_t));
assert(invocation != 0);
memset(invocation, 0, sizeof(mathmap_invocation_t));
invocation->mathmap = mathmap;
//invocation->variables = instantiate_variables(mathmap->variables);
invocation_set_antialiasing(invocation, FALSE);
invocation->supersampling = 0;
invocation->output_bpp = 4;
invocation->edge_behaviour_x = invocation->edge_behaviour_y = EDGE_BEHAVIOUR_COLOR;
invocation->img_width = invocation->render_width = img_width;
invocation->img_height = invocation->render_height = img_height;
invocation->image_R = sqrt(2.0);
invocation->row_stride = img_width * 4;
invocation->rows_finished = (unsigned char*)malloc(img_height);
memset(invocation->rows_finished, 0, img_height);
invocation->do_debug = 0;
invocation->uservals = instantiate_uservals(mathmap->main_filter->userval_infos, invocation);
if (template != NULL)
carry_over_uservals_from_template(invocation, template, copy_first_image);
init_invocation(invocation);
if (!g_thread_supported())
g_thread_init (NULL);
mathmap_pools_init_global(&invocation->pools);
invocation->native_filter_cache_mutex = g_mutex_new();
invocation->native_filter_cache_cond = g_cond_new();
invocation->native_filter_cache = NULL;
return invocation;
}
mathmap_frame_t*
invocation_new_frame (mathmap_invocation_t *invocation, image_t *closure,
int current_frame, float current_t)
{
mathmap_frame_t *frame = g_new0(mathmap_frame_t, 1);
frame->invocation = invocation;
frame->frame_render_width = invocation->render_width;
frame->frame_render_height = invocation->render_height;
frame->current_frame = current_frame;
frame->current_t = current_t;
mathmap_pools_init_global(&frame->pools);
closure->v.closure.funcs->init_frame(frame, closure);
return frame;
}
void
invocation_free_frame (mathmap_frame_t *frame)
{
mathmap_pools_free(&frame->pools);
g_free(frame);
}
void
enable_debugging (mathmap_invocation_t *invocation)
{
invocation->do_debug = 1;
}
void
disable_debugging (mathmap_invocation_t *invocation)
{
invocation->do_debug = 0;
}
static void
calc_lines (mathmap_slice_t *slice, image_t *closure, int first_row, int last_row, unsigned char *q)
{
mathmap_frame_t *frame = slice->frame;
mathmap_invocation_t *invocation = frame->invocation;
assert(first_row >= 0 && last_row <= invocation->img_height + 1 && first_row <= last_row);
closure->v.closure.funcs->calc_lines(slice, closure, first_row, last_row, q, 0);
}
void
invocation_init_slice (mathmap_slice_t *slice, image_t *closure, mathmap_frame_t *frame, int region_x, int region_y,
int region_width, int region_height, float sampling_offset_x, float sampling_offset_y)
{
memset(slice, 0, sizeof(mathmap_slice_t));
slice->frame = frame;
slice->region_x = region_x;
slice->region_y = region_y;
slice->region_width = region_width;
slice->region_height = region_height;
slice->sampling_offset_x = sampling_offset_x;
slice->sampling_offset_y = sampling_offset_y;
mathmap_pools_init_local(&slice->pools);
closure->v.closure.funcs->init_slice(slice, closure);
}
void
invocation_deinit_slice (mathmap_slice_t *slice)
{
mathmap_pools_free(&slice->pools);
}
static void
call_invocation (mathmap_frame_t *frame, image_t *closure,
int region_x, int region_y, int region_width, int region_height,
unsigned char *q)
{
mathmap_invocation_t *invocation = frame->invocation;
if (invocation->supersampling)
{
guchar *line1, *line2, *line3;
int row, col;
mathmap_slice_t short_slice, long_slice;
line1 = (guchar*)malloc((region_width + 1) * invocation->output_bpp);
line2 = (guchar*)malloc(region_width * invocation->output_bpp);
line3 = (guchar*)malloc((region_width + 1) * invocation->output_bpp);
invocation_init_slice(&short_slice, closure, frame, region_x, region_y, region_width, region_height, 0.0, 0.0);
invocation_init_slice(&long_slice, closure, frame, region_x, region_y, region_width + 1, region_height, -0.5, -0.5);
calc_lines(&long_slice, closure, region_y, region_y + 1, line1);
for (row = region_y; row < region_y + region_height; ++row)
{
unsigned char *p = q;
calc_lines(&short_slice, closure, row, row + 1, line2);
calc_lines(&long_slice, closure, row + 1, row + 2, line3);
for (col = 0; col < region_width; ++col)
{
int i;
for (i = 0; i < invocation->output_bpp; ++i)
p[i] = (line1[col*invocation->output_bpp+i]
+ line1[(col+1)*invocation->output_bpp+i]
+ 2*line2[col*invocation->output_bpp+i]
+ line3[col*invocation->output_bpp+i]
+ line3[(col+1)*invocation->output_bpp+i]) / 6;
p += invocation->output_bpp;
}
memcpy(line1, line3, (region_width + 1) * invocation->output_bpp);
q += invocation->row_stride;
invocation->rows_finished[row] = 1;
}
free(line1);
free(line2);
free(line3);
invocation_deinit_slice(&short_slice);
invocation_deinit_slice(&long_slice);
}
else
{
mathmap_slice_t slice;
invocation_init_slice(&slice, closure, frame, region_x, region_y, region_width, region_height, 0.0, 0.0);
calc_lines(&slice, closure, region_y, region_y + region_height, q);
invocation_deinit_slice(&slice);
}
}
#if defined(USE_PTHREADS) || defined(USE_GTHREADS)
typedef struct
{
thread_handle_t thread_handle;
mathmap_frame_t *frame;
image_t *closure;
int region_x, region_y;
int region_height, region_width;
unsigned char *q;
gboolean is_done;
} thread_data_t;
typedef struct
{
int num_threads;
thread_data_t datas[];
} invocation_call_t;
static void
call_invocation_thread_func (gpointer _data)
{
thread_data_t *data = (thread_data_t*)_data;
#ifdef USE_PTHREADS
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
#endif
call_invocation(data->frame, data->closure, data->region_x, data->region_y,
data->region_width, data->region_height, data->q);
data->is_done = TRUE;
}
gpointer
call_invocation_parallel (mathmap_frame_t *frame, image_t *closure,
int region_x, int region_y, int region_width, int region_height,
unsigned char *q, int num_threads)
{
mathmap_invocation_t *invocation = frame->invocation;
invocation_call_t *call;
int i;
int first_row = region_y;
int last_row = region_y + region_height;
g_assert(first_row >= 0 && last_row <= invocation->img_height && first_row <= last_row);
memset(invocation->rows_finished + first_row, 0, last_row - first_row);
call = g_malloc(sizeof(invocation_call_t) + sizeof(thread_data_t) * num_threads);
call->num_threads = num_threads;
for (i = 0; i < num_threads; ++i)
{
call->datas[i].frame = frame;
call->datas[i].closure = closure;
call->datas[i].region_x = region_x;
call->datas[i].region_width = region_width;
call->datas[i].region_y = first_row + (last_row - first_row) * i / num_threads;
call->datas[i].region_height = first_row + (last_row - first_row) * (i + 1) / num_threads - call->datas[i].region_y;
call->datas[i].q = q + (call->datas[i].region_y - region_y) * invocation->row_stride;
call->datas[i].is_done = FALSE;