Subversion Repositories filter_foundry

Rev

Rev 300 | Rev 302 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 300 Rev 301
1
/*
1
/*
2
    This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
2
    This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
3
    Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
3
    Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
4
    Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft
4
    Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft
5
 
5
 
6
    This program is free software; you can redistribute it and/or modify
6
    This program is free software; you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation; either version 2 of the License, or
8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
9
    (at your option) any later version.
10
 
10
 
11
    This program is distributed in the hope that it will be useful,
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
14
    GNU General Public License for more details.
15
 
15
 
16
    You should have received a copy of the GNU General Public License
16
    You should have received a copy of the GNU General Public License
17
    along with this program; if not, write to the Free Software
17
    along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
*/
19
*/
20
 
20
 
21
// Strict compatibility to Filter Factory by using an alternative
21
// Strict compatibility to Filter Factory by using an alternative
22
// implementation which is a 100% replica of the Filter Factory 3.0.4
22
// implementation which is a 100% replica of the Filter Factory 3.0.4
23
// for Windows.
23
// for Windows.
24
#ifdef WIN_ENV
24
#ifdef WIN_ENV
25
        #define use_filterfactory_implementation_rad
25
        #define use_filterfactory_implementation_rad
26
        //#define use_filterfactory_implementation_i
26
        //#define use_filterfactory_implementation_i
27
        //#define use_filterfactory_implementation_u
27
        //#define use_filterfactory_implementation_u
28
        //#define use_filterfactory_implementation_v
28
        //#define use_filterfactory_implementation_v
29
        #define use_filterfactory_implementation_rnd
29
        #define use_filterfactory_implementation_rnd
30
        #define use_filterfactory_implementation_c2d
30
        #define use_filterfactory_implementation_c2d
31
        #define use_filterfactory_implementation_c2m
31
        #define use_filterfactory_implementation_c2m
32
        #define use_filterfactory_implementation_r2x
32
        #define use_filterfactory_implementation_r2x
33
        #define use_filterfactory_implementation_r2y
33
        #define use_filterfactory_implementation_r2y
34
        #define use_filterfactory_implementation_cos
34
        #define use_filterfactory_implementation_cos
35
        #define use_filterfactory_implementation_sin
35
        #define use_filterfactory_implementation_sin
36
        #define use_filterfactory_implementation_tan
36
        #define use_filterfactory_implementation_tan
37
        #define use_filterfactory_implementation_sqr
37
        #define use_filterfactory_implementation_sqr
38
        #define use_filterfactory_implementation_d
38
        #define use_filterfactory_implementation_d
39
        #define use_filterfactory_implementation_m
39
        #define use_filterfactory_implementation_m
40
        #define use_filterfactory_implementation_M
40
        #define use_filterfactory_implementation_M
41
        //#define use_filterfactory_implementation_get
41
        //#define use_filterfactory_implementation_get
42
#endif
42
#endif
43
 
43
 
44
#ifdef MAC_ENV
44
#ifdef MAC_ENV
45
        #include <fp.h>
45
        #include <fp.h>
46
#endif
46
#endif
47
 
47
 
48
#include <math.h>
48
#include <math.h>
49
#include <stdlib.h>
49
#include <stdlib.h>
50
 
50
 
51
#ifndef PARSERTEST
51
#ifndef PARSERTEST
52
        #include "ff.h"
52
        #include "ff.h"
53
#else
53
#else
54
        #define value_type int
54
        #define value_type int
55
        #define uint16_t int
55
        #define uint16_t int
56
        #define uint32_t int
56
        #define uint32_t int
57
        #define int32_t int
57
        #define int32_t int
58
#endif
58
#endif
59
 
59
 
60
#include "funcs.h"
60
#include "funcs.h"
61
#include "y.tab.h"
61
#include "y.tab.h"
62
 
62
 
63
#include "node.h" // for symbol "var[]"
63
#include "node.h" // for symbol "var[]"
64
 
64
 
65
#define RINT //no rounding for now
65
#define RINT //no rounding for now
66
 
66
 
67
//#if TARGET_API_MAC_CARBON
67
//#if TARGET_API_MAC_CARBON
68
// this is another incompatibility between Classic stdclib and OS X stdclib
68
// this is another incompatibility between Classic stdclib and OS X stdclib
69
// ***FIXME: need to access real OS X includes for Carbon build
69
// ***FIXME: need to access real OS X includes for Carbon build
70
//#undef RAND_MAX
70
//#undef RAND_MAX
71
//#define RAND_MAX    0x7fffffff
71
//#define RAND_MAX    0x7fffffff
72
//#endif
72
//#endif
73
 
73
 
74
const int FACTORY_COS_LOOKUP[1024] = {
74
const int FACTORY_COS_LOOKUP[1024] = {
75
        16384, 16383, 16382, 16381, 16379, 16376, 16372, 16368, 16364, 16359, 16353, 16346, 16339, 16331, 16323,
75
        16384, 16383, 16382, 16381, 16379, 16376, 16372, 16368, 16364, 16359, 16353, 16346, 16339, 16331, 16323,
76
        16314, 16305, 16294, 16284, 16272, 16260, 16248, 16234, 16221, 16206, 16191, 16175, 16159, 16142, 16125,
76
        16314, 16305, 16294, 16284, 16272, 16260, 16248, 16234, 16221, 16206, 16191, 16175, 16159, 16142, 16125,
77
        16107, 16088, 16069, 16049, 16028, 16007, 15985, 15963, 15940, 15917, 15893, 15868, 15842, 15817, 15790,
77
        16107, 16088, 16069, 16049, 16028, 16007, 15985, 15963, 15940, 15917, 15893, 15868, 15842, 15817, 15790,
78
        15763, 15735, 15707, 15678, 15649, 15618, 15588, 15557, 15525, 15492, 15459, 15426, 15392, 15357, 15322,
78
        15763, 15735, 15707, 15678, 15649, 15618, 15588, 15557, 15525, 15492, 15459, 15426, 15392, 15357, 15322,
79
        15286, 15249, 15212, 15175, 15136, 15098, 15058, 15018, 14978, 14937, 14895, 14853, 14811, 14767, 14723,
79
        15286, 15249, 15212, 15175, 15136, 15098, 15058, 15018, 14978, 14937, 14895, 14853, 14811, 14767, 14723,
80
        14679, 14634, 14589, 14543, 14496, 14449, 14401, 14353, 14304, 14255, 14205, 14155, 14104, 14053, 14001,
80
        14679, 14634, 14589, 14543, 14496, 14449, 14401, 14353, 14304, 14255, 14205, 14155, 14104, 14053, 14001,
81
        13948, 13895, 13842, 13788, 13733, 13678, 13622, 13566, 13510, 13452, 13395, 13337, 13278, 13219, 13159,
81
        13948, 13895, 13842, 13788, 13733, 13678, 13622, 13566, 13510, 13452, 13395, 13337, 13278, 13219, 13159,
82
        13099, 13039, 12977, 12916, 12854, 12791, 12728, 12665, 12601, 12536, 12471, 12406, 12340, 12273, 12207,
82
        13099, 13039, 12977, 12916, 12854, 12791, 12728, 12665, 12601, 12536, 12471, 12406, 12340, 12273, 12207,
83
        12139, 12072, 12003, 11935, 11866, 11796, 11726, 11656, 11585, 11513, 11442, 11370, 11297, 11224, 11151,
83
        12139, 12072, 12003, 11935, 11866, 11796, 11726, 11656, 11585, 11513, 11442, 11370, 11297, 11224, 11151,
84
        11077, 11003, 10928, 10853, 10777, 10701, 10625, 10548, 10471, 10394, 10316, 10237, 10159, 10080, 10000,
84
        11077, 11003, 10928, 10853, 10777, 10701, 10625, 10548, 10471, 10394, 10316, 10237, 10159, 10080, 10000,
85
        9920, 9840, 9760, 9679, 9597, 9516, 9434, 9351, 9269, 9186, 9102, 9018, 8934, 8850, 8765, 8680, 8595,
85
        9920, 9840, 9760, 9679, 9597, 9516, 9434, 9351, 9269, 9186, 9102, 9018, 8934, 8850, 8765, 8680, 8595,
86
        8509, 8423, 8336, 8250, 8163, 8075, 7988, 7900, 7812, 7723, 7634, 7545, 7456, 7366, 7276, 7186, 7096,
86
        8509, 8423, 8336, 8250, 8163, 8075, 7988, 7900, 7812, 7723, 7634, 7545, 7456, 7366, 7276, 7186, 7096,
87
        7005, 6914, 6822, 6731, 6639, 6547, 6455, 6362, 6270, 6176, 6083, 5990, 5896, 5802, 5708, 5614, 5519,
87
        7005, 6914, 6822, 6731, 6639, 6547, 6455, 6362, 6270, 6176, 6083, 5990, 5896, 5802, 5708, 5614, 5519,
88
        5424, 5329, 5234, 5139, 5043, 4948, 4852, 4756, 4659, 4563, 4466, 4369, 4273, 4175, 4078, 3981, 3883,
88
        5424, 5329, 5234, 5139, 5043, 4948, 4852, 4756, 4659, 4563, 4466, 4369, 4273, 4175, 4078, 3981, 3883,
89
        3785, 3687, 3589, 3491, 3393, 3295, 3196, 3097, 2998, 2900, 2801, 2702, 2602, 2503, 2404, 2304, 2205,
89
        3785, 3687, 3589, 3491, 3393, 3295, 3196, 3097, 2998, 2900, 2801, 2702, 2602, 2503, 2404, 2304, 2205,
90
        2105, 2005, 1905, 1806, 1706, 1606, 1505, 1405, 1305, 1205, 1105, 1004, 904, 804, 703, 603, 502, 402,
90
        2105, 2005, 1905, 1806, 1706, 1606, 1505, 1405, 1305, 1205, 1105, 1004, 904, 804, 703, 603, 502, 402,
91
        301, 201, 100, -100, -201, -301, -402, -502, -603, -703, -804, -904, -1004, -1105, -1205, -1305, -1405,
91
        301, 201, 100, -100, -201, -301, -402, -502, -603, -703, -804, -904, -1004, -1105, -1205, -1305, -1405,
92
        -1505, -1606, -1706, -1806, -1905, -2005, -2105, -2205, -2304, -2404, -2503, -2602, -2702, -2801, -2900,
92
        -1505, -1606, -1706, -1806, -1905, -2005, -2105, -2205, -2304, -2404, -2503, -2602, -2702, -2801, -2900,
93
        -2998, -3097, -3196, -3295, -3393, -3491, -3589, -3687, -3785, -3883, -3981, -4078, -4175, -4273, -4369,
93
        -2998, -3097, -3196, -3295, -3393, -3491, -3589, -3687, -3785, -3883, -3981, -4078, -4175, -4273, -4369,
94
        -4466, -4563, -4659, -4756, -4852, -4948, -5043, -5139, -5234, -5329, -5424, -5519, -5614, -5708, -5802,
94
        -4466, -4563, -4659, -4756, -4852, -4948, -5043, -5139, -5234, -5329, -5424, -5519, -5614, -5708, -5802,
95
        -5896, -5990, -6083, -6176, -6270, -6362, -6455, -6547, -6639, -6731, -6822, -6914, -7005, -7096, -7186,
95
        -5896, -5990, -6083, -6176, -6270, -6362, -6455, -6547, -6639, -6731, -6822, -6914, -7005, -7096, -7186,
96
        -7276, -7366, -7456, -7545, -7634, -7723, -7812, -7900, -7988, -8075, -8163, -8250, -8336, -8423, -8509,
96
        -7276, -7366, -7456, -7545, -7634, -7723, -7812, -7900, -7988, -8075, -8163, -8250, -8336, -8423, -8509,
97
        -8595, -8680, -8765, -8850, -8934, -9018, -9102, -9186, -9269, -9351, -9434, -9516, -9597, -9679, -9760,
97
        -8595, -8680, -8765, -8850, -8934, -9018, -9102, -9186, -9269, -9351, -9434, -9516, -9597, -9679, -9760,
98
        -9840, -9920, -10000, -10080, -10159, -10237, -10316, -10394, -10471, -10548, -10625, -10701, -10777,
98
        -9840, -9920, -10000, -10080, -10159, -10237, -10316, -10394, -10471, -10548, -10625, -10701, -10777,
99
        -10853, -10928, -11003, -11077, -11151, -11224, -11297, -11370, -11442, -11513, -11585, -11656, -11726,
99
        -10853, -10928, -11003, -11077, -11151, -11224, -11297, -11370, -11442, -11513, -11585, -11656, -11726,
100
        -11796, -11866, -11935, -12003, -12072, -12139, -12207, -12273, -12340, -12406, -12471, -12536, -12601,
100
        -11796, -11866, -11935, -12003, -12072, -12139, -12207, -12273, -12340, -12406, -12471, -12536, -12601,
101
        -12665, -12728, -12791, -12854, -12916, -12977, -13039, -13099, -13159, -13219, -13278, -13337, -13395,
101
        -12665, -12728, -12791, -12854, -12916, -12977, -13039, -13099, -13159, -13219, -13278, -13337, -13395,
102
        -13452, -13510, -13566, -13622, -13678, -13733, -13788, -13842, -13895, -13948, -14001, -14053, -14104,
102
        -13452, -13510, -13566, -13622, -13678, -13733, -13788, -13842, -13895, -13948, -14001, -14053, -14104,
103
        -14155, -14205, -14255, -14304, -14353, -14401, -14449, -14496, -14543, -14589, -14634, -14679, -14723,
103
        -14155, -14205, -14255, -14304, -14353, -14401, -14449, -14496, -14543, -14589, -14634, -14679, -14723,
104
        -14767, -14811, -14853, -14895, -14937, -14978, -15018, -15058, -15098, -15136, -15175, -15212, -15249,
104
        -14767, -14811, -14853, -14895, -14937, -14978, -15018, -15058, -15098, -15136, -15175, -15212, -15249,
105
        -15286, -15322, -15357, -15392, -15426, -15459, -15492, -15525, -15557, -15588, -15618, -15649, -15678,
105
        -15286, -15322, -15357, -15392, -15426, -15459, -15492, -15525, -15557, -15588, -15618, -15649, -15678,
106
        -15707, -15735, -15763, -15790, -15817, -15842, -15868, -15893, -15917, -15940, -15963, -15985, -16007,
106
        -15707, -15735, -15763, -15790, -15817, -15842, -15868, -15893, -15917, -15940, -15963, -15985, -16007,
107
        -16028, -16049, -16069, -16088, -16107, -16125, -16142, -16159, -16175, -16191, -16206, -16221, -16234,
107
        -16028, -16049, -16069, -16088, -16107, -16125, -16142, -16159, -16175, -16191, -16206, -16221, -16234,
108
        -16248, -16260, -16272, -16284, -16294, -16305, -16314, -16323, -16331, -16339, -16346, -16353, -16359,
108
        -16248, -16260, -16272, -16284, -16294, -16305, -16314, -16323, -16331, -16339, -16346, -16353, -16359,
109
        -16364, -16368, -16372, -16376, -16379, -16381, -16382, -16383, -16384, -16384, -16383, -16382, -16381,
109
        -16364, -16368, -16372, -16376, -16379, -16381, -16382, -16383, -16384, -16384, -16383, -16382, -16381,
110
        -16379, -16376, -16372, -16368, -16364, -16359, -16353, -16346, -16339, -16331, -16323, -16314, -16305,
110
        -16379, -16376, -16372, -16368, -16364, -16359, -16353, -16346, -16339, -16331, -16323, -16314, -16305,
111
        -16294, -16284, -16272, -16260, -16248, -16234, -16221, -16206, -16191, -16175, -16159, -16142, -16125,
111
        -16294, -16284, -16272, -16260, -16248, -16234, -16221, -16206, -16191, -16175, -16159, -16142, -16125,
112
        -16107, -16088, -16069, -16049, -16028, -16007, -15985, -15963, -15940, -15917, -15893, -15868, -15842,
112
        -16107, -16088, -16069, -16049, -16028, -16007, -15985, -15963, -15940, -15917, -15893, -15868, -15842,
113
        -15817, -15790, -15763, -15735, -15707, -15678, -15649, -15618, -15588, -15557, -15525, -15492, -15459,
113
        -15817, -15790, -15763, -15735, -15707, -15678, -15649, -15618, -15588, -15557, -15525, -15492, -15459,
114
        -15426, -15392, -15357, -15322, -15286, -15249, -15212, -15175, -15136, -15098, -15058, -15018, -14978,
114
        -15426, -15392, -15357, -15322, -15286, -15249, -15212, -15175, -15136, -15098, -15058, -15018, -14978,
115
        -14937, -14895, -14853, -14811, -14767, -14723, -14679, -14634, -14589, -14543, -14496, -14449, -14401,
115
        -14937, -14895, -14853, -14811, -14767, -14723, -14679, -14634, -14589, -14543, -14496, -14449, -14401,
116
        -14353, -14304, -14255, -14205, -14155, -14104, -14053, -14001, -13948, -13895, -13842, -13788, -13733,
116
        -14353, -14304, -14255, -14205, -14155, -14104, -14053, -14001, -13948, -13895, -13842, -13788, -13733,
117
        -13678, -13622, -13566, -13510, -13452, -13395, -13337, -13278, -13219, -13159, -13099, -13039, -12977,
117
        -13678, -13622, -13566, -13510, -13452, -13395, -13337, -13278, -13219, -13159, -13099, -13039, -12977,
118
        -12916, -12854, -12791, -12728, -12665, -12601, -12536, -12471, -12406, -12340, -12273, -12207, -12139,
118
        -12916, -12854, -12791, -12728, -12665, -12601, -12536, -12471, -12406, -12340, -12273, -12207, -12139,
119
        -12072, -12003, -11935, -11866, -11796, -11726, -11656, -11585, -11513, -11442, -11370, -11297, -11224,
119
        -12072, -12003, -11935, -11866, -11796, -11726, -11656, -11585, -11513, -11442, -11370, -11297, -11224,
120
        -11151, -11077, -11003, -10928, -10853, -10777, -10701, -10625, -10548, -10471, -10394, -10316, -10237,
120
        -11151, -11077, -11003, -10928, -10853, -10777, -10701, -10625, -10548, -10471, -10394, -10316, -10237,
121
        -10159, -10080, -10000, -9920, -9840, -9760, -9679, -9597, -9516, -9434, -9351, -9269, -9186, -9102,
121
        -10159, -10080, -10000, -9920, -9840, -9760, -9679, -9597, -9516, -9434, -9351, -9269, -9186, -9102,
122
        -9018, -8934, -8850, -8765, -8680, -8595, -8509, -8423, -8336, -8250, -8163, -8075, -7988, -7900, -7812,
122
        -9018, -8934, -8850, -8765, -8680, -8595, -8509, -8423, -8336, -8250, -8163, -8075, -7988, -7900, -7812,
123
        -7723, -7634, -7545, -7456, -7366, -7276, -7186, -7096, -7005, -6914, -6822, -6731, -6639, -6547, -6455,
123
        -7723, -7634, -7545, -7456, -7366, -7276, -7186, -7096, -7005, -6914, -6822, -6731, -6639, -6547, -6455,
124
        -6362, -6270, -6176, -6083, -5990, -5896, -5802, -5708, -5614, -5519, -5424, -5329, -5234, -5139, -5043,
124
        -6362, -6270, -6176, -6083, -5990, -5896, -5802, -5708, -5614, -5519, -5424, -5329, -5234, -5139, -5043,
125
        -4948, -4852, -4756, -4659, -4563, -4466, -4369, -4273, -4175, -4078, -3981, -3883, -3785, -3687, -3589,
125
        -4948, -4852, -4756, -4659, -4563, -4466, -4369, -4273, -4175, -4078, -3981, -3883, -3785, -3687, -3589,
126
        -3491, -3393, -3295, -3196, -3097, -2998, -2900, -2801, -2702, -2602, -2503, -2404, -2304, -2205, -2105,
126
        -3491, -3393, -3295, -3196, -3097, -2998, -2900, -2801, -2702, -2602, -2503, -2404, -2304, -2205, -2105,
127
        -2005, -1905, -1806, -1706, -1606, -1505, -1405, -1305, -1205, -1105, -1004, -904, -804, -703, -603, -502,
127
        -2005, -1905, -1806, -1706, -1606, -1505, -1405, -1305, -1205, -1105, -1004, -904, -804, -703, -603, -502,
128
        -402, -301, -201, -100, 100, 201, 301, 402, 502, 603, 703, 804, 904, 1004, 1105, 1205, 1305, 1405, 1505,
128
        -402, -301, -201, -100, 100, 201, 301, 402, 502, 603, 703, 804, 904, 1004, 1105, 1205, 1305, 1405, 1505,
129
        1606, 1706, 1806, 1905, 2005, 2105, 2205, 2304, 2404, 2503, 2602, 2702, 2801, 2900, 2998, 3097, 3196,
129
        1606, 1706, 1806, 1905, 2005, 2105, 2205, 2304, 2404, 2503, 2602, 2702, 2801, 2900, 2998, 3097, 3196,
130
        3295, 3393, 3491, 3589, 3687, 3785, 3883, 3981, 4078, 4175, 4273, 4369, 4466, 4563, 4659, 4756, 4852,
130
        3295, 3393, 3491, 3589, 3687, 3785, 3883, 3981, 4078, 4175, 4273, 4369, 4466, 4563, 4659, 4756, 4852,
131
        4948, 5043, 5139, 5234, 5329, 5424, 5519, 5614, 5708, 5802, 5896, 5990, 6083, 6176, 6270, 6362, 6455,
131
        4948, 5043, 5139, 5234, 5329, 5424, 5519, 5614, 5708, 5802, 5896, 5990, 6083, 6176, 6270, 6362, 6455,
132
        6547, 6639, 6731, 6822, 6914, 7005, 7096, 7186, 7276, 7366, 7456, 7545, 7634, 7723, 7812, 7900, 7988,
132
        6547, 6639, 6731, 6822, 6914, 7005, 7096, 7186, 7276, 7366, 7456, 7545, 7634, 7723, 7812, 7900, 7988,
133
        8075, 8163, 8250, 8336, 8423, 8509, 8595, 8680, 8765, 8850, 8934, 9018, 9102, 9186, 9269, 9351, 9434,
133
        8075, 8163, 8250, 8336, 8423, 8509, 8595, 8680, 8765, 8850, 8934, 9018, 9102, 9186, 9269, 9351, 9434,
134
        9516, 9597, 9679, 9760, 9840, 9920, 10000, 10080, 10159, 10237, 10316, 10394, 10471, 10548, 10625, 10701,
134
        9516, 9597, 9679, 9760, 9840, 9920, 10000, 10080, 10159, 10237, 10316, 10394, 10471, 10548, 10625, 10701,
135
        10777, 10853, 10928, 11003, 11077, 11151, 11224, 11297, 11370, 11442, 11513, 11585, 11656, 11726, 11796,
135
        10777, 10853, 10928, 11003, 11077, 11151, 11224, 11297, 11370, 11442, 11513, 11585, 11656, 11726, 11796,
136
        11866, 11935, 12003, 12072, 12139, 12207, 12273, 12340, 12406, 12471, 12536, 12601, 12665, 12728, 12791,
136
        11866, 11935, 12003, 12072, 12139, 12207, 12273, 12340, 12406, 12471, 12536, 12601, 12665, 12728, 12791,
137
        12854, 12916, 12977, 13039, 13099, 13159, 13219, 13278, 13337, 13395, 13452, 13510, 13566, 13622, 13678,
137
        12854, 12916, 12977, 13039, 13099, 13159, 13219, 13278, 13337, 13395, 13452, 13510, 13566, 13622, 13678,
138
        13733, 13788, 13842, 13895, 13948, 14001, 14053, 14104, 14155, 14205, 14255, 14304, 14353, 14401, 14449,
138
        13733, 13788, 13842, 13895, 13948, 14001, 14053, 14104, 14155, 14205, 14255, 14304, 14353, 14401, 14449,
139
        14496, 14543, 14589, 14634, 14679, 14723, 14767, 14811, 14853, 14895, 14937, 14978, 15018, 15058, 15098,
139
        14496, 14543, 14589, 14634, 14679, 14723, 14767, 14811, 14853, 14895, 14937, 14978, 15018, 15058, 15098,
140
        15136, 15175, 15212, 15249, 15286, 15322, 15357, 15392, 15426, 15459, 15492, 15525, 15557, 15588, 15618,
140
        15136, 15175, 15212, 15249, 15286, 15322, 15357, 15392, 15426, 15459, 15492, 15525, 15557, 15588, 15618,
141
        15649, 15678, 15707, 15735, 15763, 15790, 15817, 15842, 15868, 15893, 15917, 15940, 15963, 15985, 16007,
141
        15649, 15678, 15707, 15735, 15763, 15790, 15817, 15842, 15868, 15893, 15917, 15940, 15963, 15985, 16007,
142
        16028, 16049, 16069, 16088, 16107, 16125, 16142, 16159, 16175, 16191, 16206, 16221, 16234, 16248, 16260,
142
        16028, 16049, 16069, 16088, 16107, 16125, 16142, 16159, 16175, 16191, 16206, 16221, 16234, 16248, 16260,
143
        16272, 16284, 16294, 16305, 16314, 16323, 16331, 16339, 16346, 16353, 16359, 16364, 16368, 16372, 16376,
143
        16272, 16284, 16294, 16305, 16314, 16323, 16331, 16339, 16346, 16353, 16359, 16364, 16368, 16372, 16376,
144
        16379, 16381, 16382, 16383, 16384 };
144
        16379, 16381, 16382, 16383, 16384 };
145
 
145
 
146
const uint16_t FACTORY_C2D_LOOKUP[1024] = {
146
const uint16_t FACTORY_C2D_LOOKUP[1024] = {
147
        64, 128, 191, 255, 319, 383, 447, 511, 575, 639, 703, 767, 831, 895, 959, 1023, 1087, 1151, 1215, 1279,
147
        64, 128, 191, 255, 319, 383, 447, 511, 575, 639, 703, 767, 831, 895, 959, 1023, 1087, 1151, 1215, 1279,
148
        1343, 1407, 1471, 1535, 1599, 1663, 1727, 1791, 1855, 1919, 1983, 2047, 2111, 2175, 2239, 2303, 2366,
148
        1343, 1407, 1471, 1535, 1599, 1663, 1727, 1791, 1855, 1919, 1983, 2047, 2111, 2175, 2239, 2303, 2366,
149
        2430, 2494, 2558, 2622, 2686, 2750, 2814, 2878, 2942, 3005, 3069, 3133, 3197, 3261, 3325, 3388, 3452,
149
        2430, 2494, 2558, 2622, 2686, 2750, 2814, 2878, 2942, 3005, 3069, 3133, 3197, 3261, 3325, 3388, 3452,
150
        3516, 3580, 3644, 3708, 3771, 3835, 3899, 3963, 4026, 4090, 4154, 4218, 4281, 4345, 4409, 4473, 4536,
150
        3516, 3580, 3644, 3708, 3771, 3835, 3899, 3963, 4026, 4090, 4154, 4218, 4281, 4345, 4409, 4473, 4536,
151
        4600, 4664, 4727, 4791, 4855, 4918, 4982, 5046, 5109, 5173, 5236, 5300, 5364, 5427, 5491, 5554, 5618,
151
        4600, 4664, 4727, 4791, 4855, 4918, 4982, 5046, 5109, 5173, 5236, 5300, 5364, 5427, 5491, 5554, 5618,
152
        5681, 5745, 5808, 5872, 5935, 5999, 6062, 6126, 6189, 6252, 6316, 6379, 6443, 6506, 6569, 6633, 6696,
152
        5681, 5745, 5808, 5872, 5935, 5999, 6062, 6126, 6189, 6252, 6316, 6379, 6443, 6506, 6569, 6633, 6696,
153
        6759, 6823, 6886, 6949, 7013, 7076, 7139, 7202, 7266, 7329, 7392, 7455, 7518, 7582, 7645, 7708, 7771,
153
        6759, 6823, 6886, 6949, 7013, 7076, 7139, 7202, 7266, 7329, 7392, 7455, 7518, 7582, 7645, 7708, 7771,
154
        7834, 7897, 7960, 8023, 8086, 8149, 8212, 8275, 8338, 8401, 8464, 8527, 8590, 8653, 8716, 8779, 8841,
154
        7834, 7897, 7960, 8023, 8086, 8149, 8212, 8275, 8338, 8401, 8464, 8527, 8590, 8653, 8716, 8779, 8841,
155
        8904, 8967, 9030, 9093, 9155, 9218, 9281, 9344, 9406, 9469, 9532, 9594, 9657, 9720, 9782, 9845, 9907,
155
        8904, 8967, 9030, 9093, 9155, 9218, 9281, 9344, 9406, 9469, 9532, 9594, 9657, 9720, 9782, 9845, 9907,
156
        9970, 10032, 10095, 10157, 10220, 10282, 10345, 10407, 10470, 10532, 10594, 10657, 10719, 10781, 10843,
156
        9970, 10032, 10095, 10157, 10220, 10282, 10345, 10407, 10470, 10532, 10594, 10657, 10719, 10781, 10843,
157
        10906, 10968, 11030, 11092, 11155, 11217, 11279, 11341, 11403, 11465, 11527, 11589, 11651, 11713, 11775,
157
        10906, 10968, 11030, 11092, 11155, 11217, 11279, 11341, 11403, 11465, 11527, 11589, 11651, 11713, 11775,
158
        11837, 11899, 11961, 12023, 12085, 12146, 12208, 12270, 12332, 12394, 12455, 12517, 12579, 12640, 12702,
158
        11837, 11899, 11961, 12023, 12085, 12146, 12208, 12270, 12332, 12394, 12455, 12517, 12579, 12640, 12702,
159
        12764, 12825, 12887, 12948, 13010, 13071, 13133, 13194, 13256, 13317, 13379, 13440, 13501, 13563, 13624,
159
        12764, 12825, 12887, 12948, 13010, 13071, 13133, 13194, 13256, 13317, 13379, 13440, 13501, 13563, 13624,
160
        13685, 13746, 13808, 13869, 13930, 13991, 14052, 14113, 14174, 14235, 14296, 14357, 14418, 14479, 14540,
160
        13685, 13746, 13808, 13869, 13930, 13991, 14052, 14113, 14174, 14235, 14296, 14357, 14418, 14479, 14540,
161
        14601, 14662, 14723, 14784, 14844, 14905, 14966, 15027, 15087, 15148, 15208, 15269, 15330, 15390, 15451,
161
        14601, 14662, 14723, 14784, 14844, 14905, 14966, 15027, 15087, 15148, 15208, 15269, 15330, 15390, 15451,
162
        15511, 15572, 15632, 15693, 15753, 15813, 15874, 15934, 15994, 16054, 16115, 16175, 16235, 16295, 16355,
162
        15511, 15572, 15632, 15693, 15753, 15813, 15874, 15934, 15994, 16054, 16115, 16175, 16235, 16295, 16355,
163
        16415, 16475, 16535, 16595, 16655, 16715, 16775, 16835, 16895, 16955, 17015, 17074, 17134, 17194, 17254,
163
        16415, 16475, 16535, 16595, 16655, 16715, 16775, 16835, 16895, 16955, 17015, 17074, 17134, 17194, 17254,
164
        17313, 17373, 17432, 17492, 17551, 17611, 17670, 17730, 17789, 17849, 17908, 17967, 18027, 18086, 18145,
164
        17313, 17373, 17432, 17492, 17551, 17611, 17670, 17730, 17789, 17849, 17908, 17967, 18027, 18086, 18145,
165
        18204, 18264, 18323, 18382, 18441, 18500, 18559, 18618, 18677, 18736, 18795, 18854, 18912, 18971, 19030,
165
        18204, 18264, 18323, 18382, 18441, 18500, 18559, 18618, 18677, 18736, 18795, 18854, 18912, 18971, 19030,
166
        19089, 19147, 19206, 19265, 19323, 19382, 19440, 19499, 19557, 19616, 19674, 19733, 19791, 19849, 19908,
166
        19089, 19147, 19206, 19265, 19323, 19382, 19440, 19499, 19557, 19616, 19674, 19733, 19791, 19849, 19908,
167
        19966, 20024, 20082, 20141, 20199, 20257, 20315, 20373, 20431, 20489, 20547, 20605, 20663, 20720, 20778,
167
        19966, 20024, 20082, 20141, 20199, 20257, 20315, 20373, 20431, 20489, 20547, 20605, 20663, 20720, 20778,
168
        20836, 20894, 20951, 21009, 21067, 21124, 21182, 21239, 21297, 21354, 21412, 21469, 21526, 21584, 21641,
168
        20836, 20894, 20951, 21009, 21067, 21124, 21182, 21239, 21297, 21354, 21412, 21469, 21526, 21584, 21641,
169
        21698, 21756, 21813, 21870, 21927, 21984, 22041, 22098, 22155, 22212, 22269, 22326, 22383, 22439, 22496,
169
        21698, 21756, 21813, 21870, 21927, 21984, 22041, 22098, 22155, 22212, 22269, 22326, 22383, 22439, 22496,
170
        22553, 22610, 22666, 22723, 22780, 22836, 22893, 22949, 23006, 23062, 23118, 23175, 23231, 23287, 23344,
170
        22553, 22610, 22666, 22723, 22780, 22836, 22893, 22949, 23006, 23062, 23118, 23175, 23231, 23287, 23344,
171
        23400, 23456, 23512, 23568, 23624, 23680, 23736, 23792, 23848, 23904, 23960, 24016, 24071, 24127, 24183,
171
        23400, 23456, 23512, 23568, 23624, 23680, 23736, 23792, 23848, 23904, 23960, 24016, 24071, 24127, 24183,
172
        24238, 24294, 24350, 24405, 24461, 24516, 24572, 24627, 24682, 24738, 24793, 24848, 24904, 24959, 25014,
172
        24238, 24294, 24350, 24405, 24461, 24516, 24572, 24627, 24682, 24738, 24793, 24848, 24904, 24959, 25014,
173
        25069, 25124, 25179, 25234, 25289, 25344, 25399, 25454, 25509, 25563, 25618, 25673, 25727, 25782, 25836,
173
        25069, 25124, 25179, 25234, 25289, 25344, 25399, 25454, 25509, 25563, 25618, 25673, 25727, 25782, 25836,
174
        25891, 25945, 26000, 26054, 26109, 26163, 26217, 26271, 26326, 26380, 26434, 26488, 26542, 26596, 26650,
174
        25891, 25945, 26000, 26054, 26109, 26163, 26217, 26271, 26326, 26380, 26434, 26488, 26542, 26596, 26650,
175
        26704, 26758, 26812, 26866, 26920, 26974, 27027, 27081, 27135, 27188, 27242, 27295, 27349, 27402, 27456,
175
        26704, 26758, 26812, 26866, 26920, 26974, 27027, 27081, 27135, 27188, 27242, 27295, 27349, 27402, 27456,
176
        27509, 27563, 27616, 27669, 27722, 27776, 27829, 27882, 27935, 27988, 28041, 28094, 28147, 28200, 28253,
176
        27509, 27563, 27616, 27669, 27722, 27776, 27829, 27882, 27935, 27988, 28041, 28094, 28147, 28200, 28253,
177
        28305, 28358, 28411, 28464, 28516, 28569, 28621, 28674, 28726, 28779, 28831, 28884, 28936, 28988, 29041,
177
        28305, 28358, 28411, 28464, 28516, 28569, 28621, 28674, 28726, 28779, 28831, 28884, 28936, 28988, 29041,
178
        29093, 29145, 29197, 29249, 29301, 29353, 29405, 29457, 29509, 29561, 29613, 29664, 29716, 29768, 29820,
178
        29093, 29145, 29197, 29249, 29301, 29353, 29405, 29457, 29509, 29561, 29613, 29664, 29716, 29768, 29820,
179
        29871, 29923, 29974, 30026, 30077, 30129, 30180, 30231, 30283, 30334, 30385, 30436, 30488, 30539, 30590,
179
        29871, 29923, 29974, 30026, 30077, 30129, 30180, 30231, 30283, 30334, 30385, 30436, 30488, 30539, 30590,
180
        30641, 30692, 30743, 30794, 30844, 30895, 30946, 30997, 31047, 31098, 31149, 31199, 31250, 31300, 31351,
180
        30641, 30692, 30743, 30794, 30844, 30895, 30946, 30997, 31047, 31098, 31149, 31199, 31250, 31300, 31351,
181
        31401, 31452, 31502, 31552, 31602, 31653, 31703, 31753, 31803, 31853, 31903, 31953, 32003, 32053, 32103,
181
        31401, 31452, 31502, 31552, 31602, 31653, 31703, 31753, 31803, 31853, 31903, 31953, 32003, 32053, 32103,
182
        32153, 32202, 32252, 32302, 32351, 32401, 32451, 32500, 32550, 32599, 32649, 32698, 32747, 32796, 32846,
182
        32153, 32202, 32252, 32302, 32351, 32401, 32451, 32500, 32550, 32599, 32649, 32698, 32747, 32796, 32846,
183
        32895, 32944, 32993, 33042, 33091, 33140, 33189, 33238, 33287, 33336, 33385, 33434, 33482, 33531, 33580,
183
        32895, 32944, 32993, 33042, 33091, 33140, 33189, 33238, 33287, 33336, 33385, 33434, 33482, 33531, 33580,
184
        33628, 33677, 33725, 33774, 33822, 33871, 33919, 33967, 34015, 34064, 34112, 34160, 34208, 34256, 34304,
184
        33628, 33677, 33725, 33774, 33822, 33871, 33919, 33967, 34015, 34064, 34112, 34160, 34208, 34256, 34304,
185
        34352, 34400, 34448, 34496, 34544, 34592, 34639, 34687, 34735, 34782, 34830, 34877, 34925, 34972, 35020,
185
        34352, 34400, 34448, 34496, 34544, 34592, 34639, 34687, 34735, 34782, 34830, 34877, 34925, 34972, 35020,
186
        35067, 35115, 35162, 35209, 35256, 35303, 35351, 35398, 35445, 35492, 35539, 35586, 35633, 35679, 35726,
186
        35067, 35115, 35162, 35209, 35256, 35303, 35351, 35398, 35445, 35492, 35539, 35586, 35633, 35679, 35726,
187
        35773, 35820, 35866, 35913, 35960, 36006, 36053, 36099, 36146, 36192, 36239, 36285, 36331, 36377, 36424,
187
        35773, 35820, 35866, 35913, 35960, 36006, 36053, 36099, 36146, 36192, 36239, 36285, 36331, 36377, 36424,
188
        36470, 36516, 36562, 36608, 36654, 36700, 36746, 36792, 36838, 36883, 36929, 36975, 37021, 37066, 37112,
188
        36470, 36516, 36562, 36608, 36654, 36700, 36746, 36792, 36838, 36883, 36929, 36975, 37021, 37066, 37112,
189
        37157, 37203, 37248, 37294, 37339, 37385, 37430, 37475, 37520, 37566, 37611, 37656, 37701, 37746, 37791,
189
        37157, 37203, 37248, 37294, 37339, 37385, 37430, 37475, 37520, 37566, 37611, 37656, 37701, 37746, 37791,
190
        37836, 37881, 37926, 37971, 38015, 38060, 38105, 38149, 38194, 38239, 38283, 38328, 38372, 38417, 38461,
190
        37836, 37881, 37926, 37971, 38015, 38060, 38105, 38149, 38194, 38239, 38283, 38328, 38372, 38417, 38461,
191
        38505, 38550, 38594, 38638, 38682, 38727, 38771, 38815, 38859, 38903, 38947, 38991, 39035, 39078, 39122,
191
        38505, 38550, 38594, 38638, 38682, 38727, 38771, 38815, 38859, 38903, 38947, 38991, 39035, 39078, 39122,
192
        39166, 39210, 39253, 39297, 39341, 39384, 39428, 39471, 39515, 39558, 39601, 39645, 39688, 39731, 39774,
192
        39166, 39210, 39253, 39297, 39341, 39384, 39428, 39471, 39515, 39558, 39601, 39645, 39688, 39731, 39774,
193
        39818, 39861, 39904, 39947, 39990, 40033, 40076, 40119, 40161, 40204, 40247, 40290, 40332, 40375, 40418,
193
        39818, 39861, 39904, 39947, 39990, 40033, 40076, 40119, 40161, 40204, 40247, 40290, 40332, 40375, 40418,
194
        40460, 40503, 40545, 40588, 40630, 40673, 40715, 40757, 40799, 40842, 40884, 40926, 40968, 41010, 41052,
194
        40460, 40503, 40545, 40588, 40630, 40673, 40715, 40757, 40799, 40842, 40884, 40926, 40968, 41010, 41052,
195
        41094, 41136, 41178, 41220, 41262, 41303, 41345, 41387, 41429, 41470, 41512, 41553, 41595, 41636, 41678,
195
        41094, 41136, 41178, 41220, 41262, 41303, 41345, 41387, 41429, 41470, 41512, 41553, 41595, 41636, 41678,
196
        41719, 41761, 41802, 41843, 41885, 41926, 41967, 42008, 42049, 42090, 42131, 42172, 42213, 42254, 42295,
196
        41719, 41761, 41802, 41843, 41885, 41926, 41967, 42008, 42049, 42090, 42131, 42172, 42213, 42254, 42295,
197
        42336, 42376, 42417, 42458, 42499, 42539, 42580, 42620, 42661, 42701, 42742, 42782, 42823, 42863, 42903,
197
        42336, 42376, 42417, 42458, 42499, 42539, 42580, 42620, 42661, 42701, 42742, 42782, 42823, 42863, 42903,
198
        42944, 42984, 43024, 43064, 43104, 43144, 43184, 43224, 43264, 43304, 43344, 43384, 43424, 43463, 43503,
198
        42944, 42984, 43024, 43064, 43104, 43144, 43184, 43224, 43264, 43304, 43344, 43384, 43424, 43463, 43503,
199
        43543, 43582, 43622, 43662, 43701, 43741, 43780, 43820, 43859, 43898, 43938, 43977, 44016, 44055, 44094,
199
        43543, 43582, 43622, 43662, 43701, 43741, 43780, 43820, 43859, 43898, 43938, 43977, 44016, 44055, 44094,
200
        44134, 44173, 44212, 44251, 44290, 44329, 44368, 44406, 44445, 44484, 44523, 44562, 44600, 44639, 44677,
200
        44134, 44173, 44212, 44251, 44290, 44329, 44368, 44406, 44445, 44484, 44523, 44562, 44600, 44639, 44677,
201
        44716, 44755, 44793, 44832, 44870, 44908, 44947, 44985, 45023, 45062, 45100, 45138, 45176, 45214, 45252,
201
        44716, 44755, 44793, 44832, 44870, 44908, 44947, 44985, 45023, 45062, 45100, 45138, 45176, 45214, 45252,
202
        45290, 45328, 45366, 45404, 45442, 45480, 45518, 45555, 45593, 45631, 45668, 45706, 45744, 45781, 45819,
202
        45290, 45328, 45366, 45404, 45442, 45480, 45518, 45555, 45593, 45631, 45668, 45706, 45744, 45781, 45819,
203
        45856, 45894, 45931, 45968, 46006, 46043, 46080, 46117, 46155, 46192, 46229, 46266, 46303, 46340, 46377,
203
        45856, 45894, 45931, 45968, 46006, 46043, 46080, 46117, 46155, 46192, 46229, 46266, 46303, 46340, 46377,
204
        46414, 46451, 46488, 46525, 46561, 46598, 46635, 46672, 46708, 46745, 46781, 46818, 46854, 46891, 46927,
204
        46414, 46451, 46488, 46525, 46561, 46598, 46635, 46672, 46708, 46745, 46781, 46818, 46854, 46891, 46927,
205
        46964, 47000, 47037, 47073, 47109, 47145, 47182, 47218, 47254, 47290, 47326, 47362, 47398, 47434, 47470,
205
        46964, 47000, 47037, 47073, 47109, 47145, 47182, 47218, 47254, 47290, 47326, 47362, 47398, 47434, 47470,
206
        47506, 47542, 47577, 47613, 47649, 47685, 47720, 47756, 47792, 47827, 47863, 47898, 47934, 47969, 48004,
206
        47506, 47542, 47577, 47613, 47649, 47685, 47720, 47756, 47792, 47827, 47863, 47898, 47934, 47969, 48004,
207
        48040, 48075, 48110, 48146, 48181, 48216, 48251, 48286, 48322, 48357, 48392, 48427, 48462, 48496, 48531,
207
        48040, 48075, 48110, 48146, 48181, 48216, 48251, 48286, 48322, 48357, 48392, 48427, 48462, 48496, 48531,
208
        48566, 48601, 48636, 48671, 48705, 48740, 48775, 48809, 48844, 48878, 48913, 48947, 48982, 49016, 49051,
208
        48566, 48601, 48636, 48671, 48705, 48740, 48775, 48809, 48844, 48878, 48913, 48947, 48982, 49016, 49051,
209
        49085, 49119, 49154, 49188, 49222, 49256, 49290, 49324, 49359, 49393, 49427, 49461, 49495, 49528, 49562,
209
        49085, 49119, 49154, 49188, 49222, 49256, 49290, 49324, 49359, 49393, 49427, 49461, 49495, 49528, 49562,
210
        49596, 49630, 49664, 49698, 49731, 49765, 49799, 49832, 49866, 49899, 49933, 49966, 50000, 50033, 50067,
210
        49596, 49630, 49664, 49698, 49731, 49765, 49799, 49832, 49866, 49899, 49933, 49966, 50000, 50033, 50067,
211
        50100, 50133, 50167, 50200, 50233, 50266, 50299, 50333, 50366, 50399, 50432, 50465, 50498, 50531, 50564,
211
        50100, 50133, 50167, 50200, 50233, 50266, 50299, 50333, 50366, 50399, 50432, 50465, 50498, 50531, 50564,
212
        50597, 50629, 50662, 50695, 50728, 50760, 50793, 50826, 50858, 50891, 50924, 50956, 50989, 51021, 51053,
212
        50597, 50629, 50662, 50695, 50728, 50760, 50793, 50826, 50858, 50891, 50924, 50956, 50989, 51021, 51053,
213
        51086, 51118, 51151, 51183, 51215, 51247, 51280, 51312, 51344, 51376, 51408, 51440, 51472 };
213
        51086, 51118, 51151, 51183, 51215, 51247, 51280, 51312, 51344, 51376, 51408, 51440, 51472 };
214
 
214
 
215
const uint16_t FACTORY_C2M_LOOKUP[1024] = {
215
const uint16_t FACTORY_C2M_LOOKUP[1024] = {
216
        32, 32, 32, 32, 32, 32, 33, 33, 33, 34, 35, 35, 36, 37, 38, 39, 39, 41, 42, 43, 44, 45, 47, 48, 49, 51,
216
        32, 32, 32, 32, 32, 32, 33, 33, 33, 34, 35, 35, 36, 37, 38, 39, 39, 41, 42, 43, 44, 45, 47, 48, 49, 51,
217
        53, 54, 56, 58, 60, 62, 63, 66, 68, 70, 72, 74, 77, 79, 81, 84, 87, 89, 92, 95, 98, 100, 103, 106, 110,
217
        53, 54, 56, 58, 60, 62, 63, 66, 68, 70, 72, 74, 77, 79, 81, 84, 87, 89, 92, 95, 98, 100, 103, 106, 110,
218
        113, 116, 119, 123, 126, 129, 133, 137, 140, 144, 148, 152, 155, 159, 163, 167, 172, 176, 180, 184, 189,
218
        113, 116, 119, 123, 126, 129, 133, 137, 140, 144, 148, 152, 155, 159, 163, 167, 172, 176, 180, 184, 189,
219
        193, 198, 202, 207, 212, 217, 221, 226, 231, 236, 241, 246, 252, 257, 262, 268, 273, 279, 284, 290, 295,
219
        193, 198, 202, 207, 212, 217, 221, 226, 231, 236, 241, 246, 252, 257, 262, 268, 273, 279, 284, 290, 295,
220
        301, 307, 313, 319, 325, 331, 337, 343, 350, 356, 362, 369, 375, 382, 388, 395, 402, 409, 415, 422, 429,
220
        301, 307, 313, 319, 325, 331, 337, 343, 350, 356, 362, 369, 375, 382, 388, 395, 402, 409, 415, 422, 429,
221
        436, 443, 451, 458, 465, 473, 480, 487, 495, 503, 510, 518, 526, 534, 542, 549, 558, 566, 574, 582, 590,
221
        436, 443, 451, 458, 465, 473, 480, 487, 495, 503, 510, 518, 526, 534, 542, 549, 558, 566, 574, 582, 590,
222
        599, 607, 615, 624, 633, 641, 650, 659, 667, 676, 685, 694, 703, 712, 722, 731, 740, 750, 759, 768, 778,
222
        599, 607, 615, 624, 633, 641, 650, 659, 667, 676, 685, 694, 703, 712, 722, 731, 740, 750, 759, 768, 778,
223
        788, 797, 807, 817, 827, 837, 847, 857, 867, 877, 887, 897, 908, 918, 928, 939, 950, 960, 971, 982, 992,
223
        788, 797, 807, 817, 827, 837, 847, 857, 867, 877, 887, 897, 908, 918, 928, 939, 950, 960, 971, 982, 992,
224
        1003, 1014, 1025, 1036, 1047, 1059, 1070, 1081, 1092, 1104, 1115, 1127, 1138, 1150, 1162, 1174, 1185, 1197,
224
        1003, 1014, 1025, 1036, 1047, 1059, 1070, 1081, 1092, 1104, 1115, 1127, 1138, 1150, 1162, 1174, 1185, 1197,
225
        1209, 1221, 1233, 1245, 1258, 1270, 1282, 1294, 1307, 1319, 1332, 1344, 1357, 1370, 1383, 1395, 1408, 1421,
225
        1209, 1221, 1233, 1245, 1258, 1270, 1282, 1294, 1307, 1319, 1332, 1344, 1357, 1370, 1383, 1395, 1408, 1421,
226
        1434, 1447, 1460, 1474, 1487, 1500, 1514, 1527, 1540, 1554, 1568, 1581, 1595, 1609, 1622, 1636, 1650, 1664,
226
        1434, 1447, 1460, 1474, 1487, 1500, 1514, 1527, 1540, 1554, 1568, 1581, 1595, 1609, 1622, 1636, 1650, 1664,
227
        1678, 1692, 1707, 1721, 1735, 1749, 1764, 1778, 1793, 1807, 1822, 1837, 1852, 1866, 1881, 1896, 1911, 1926,
227
        1678, 1692, 1707, 1721, 1735, 1749, 1764, 1778, 1793, 1807, 1822, 1837, 1852, 1866, 1881, 1896, 1911, 1926,
228
        1941, 1956, 1972, 1987, 2002, 2018, 2033, 2048, 2064, 2080, 2095, 2111, 2127, 2143, 2159, 2174, 2190, 2207,
228
        1941, 1956, 1972, 1987, 2002, 2018, 2033, 2048, 2064, 2080, 2095, 2111, 2127, 2143, 2159, 2174, 2190, 2207,
229
        2223, 2239, 2255, 2271, 2288, 2304, 2321, 2337, 2354, 2370, 2387, 2404, 2420, 2437, 2454, 2471, 2488, 2505,
229
        2223, 2239, 2255, 2271, 2288, 2304, 2321, 2337, 2354, 2370, 2387, 2404, 2420, 2437, 2454, 2471, 2488, 2505,
230
        2522, 2540, 2557, 2574, 2592, 2609, 2626, 2644, 2662, 2679, 2697, 2715, 2732, 2750, 2768, 2786, 2804, 2822,
230
        2522, 2540, 2557, 2574, 2592, 2609, 2626, 2644, 2662, 2679, 2697, 2715, 2732, 2750, 2768, 2786, 2804, 2822,
231
        2840, 2859, 2877, 2895, 2913, 2932, 2950, 2969, 2987, 3006, 3025, 3043, 3062, 3081, 3100, 3119, 3138, 3157,
231
        2840, 2859, 2877, 2895, 2913, 2932, 2950, 2969, 2987, 3006, 3025, 3043, 3062, 3081, 3100, 3119, 3138, 3157,
232
        3176, 3195, 3214, 3234, 3253, 3272, 3292, 3311, 3331, 3351, 3370, 3390, 3410, 3430, 3449, 3469, 3489, 3509,
232
        3176, 3195, 3214, 3234, 3253, 3272, 3292, 3311, 3331, 3351, 3370, 3390, 3410, 3430, 3449, 3469, 3489, 3509,
233
        3529, 3550, 3570, 3590, 3610, 3631, 3651, 3672, 3692, 3713, 3733, 3754, 3775, 3795, 3816, 3837, 3858, 3879,
233
        3529, 3550, 3570, 3590, 3610, 3631, 3651, 3672, 3692, 3713, 3733, 3754, 3775, 3795, 3816, 3837, 3858, 3879,
234
        3900, 3921, 3942, 3964, 3985, 4006, 4027, 4049, 4070, 4092, 4113, 4135, 4157, 4178, 4200, 4222, 4244, 4266,
234
        3900, 3921, 3942, 3964, 3985, 4006, 4027, 4049, 4070, 4092, 4113, 4135, 4157, 4178, 4200, 4222, 4244, 4266,
235
        4288, 4310, 4332, 4354, 4376, 4399, 4421, 4443, 4466, 4488, 4510, 4533, 4556, 4578, 4601, 4624, 4647, 4669,
235
        4288, 4310, 4332, 4354, 4376, 4399, 4421, 4443, 4466, 4488, 4510, 4533, 4556, 4578, 4601, 4624, 4647, 4669,
236
        4692, 4715, 4738, 4761, 4784, 4808, 4831, 4854, 4877, 4901, 4924, 4948, 4971, 4995, 5018, 5042, 5066, 5089,
236
        4692, 4715, 4738, 4761, 4784, 4808, 4831, 4854, 4877, 4901, 4924, 4948, 4971, 4995, 5018, 5042, 5066, 5089,
237
        5113, 5137, 5161, 5185, 5209, 5233, 5257, 5281, 5306, 5330, 5354, 5378, 5403, 5427, 5452, 5476, 5501, 5526,
237
        5113, 5137, 5161, 5185, 5209, 5233, 5257, 5281, 5306, 5330, 5354, 5378, 5403, 5427, 5452, 5476, 5501, 5526,
238
        5550, 5575, 5600, 5625, 5650, 5675, 5700, 5725, 5750, 5775, 5800, 5825, 5851, 5876, 5901, 5927, 5952, 5978,
238
        5550, 5575, 5600, 5625, 5650, 5675, 5700, 5725, 5750, 5775, 5800, 5825, 5851, 5876, 5901, 5927, 5952, 5978,
239
        6003, 6029, 6055, 6080, 6106, 6132, 6158, 6184, 6210, 6236, 6262, 6288, 6314, 6340, 6367, 6393, 6419, 6446,
239
        6003, 6029, 6055, 6080, 6106, 6132, 6158, 6184, 6210, 6236, 6262, 6288, 6314, 6340, 6367, 6393, 6419, 6446,
240
        6472, 6499, 6525, 6552, 6578, 6605, 6632, 6658, 6685, 6712, 6739, 6766, 6793, 6820, 6847, 6874, 6901, 6929,
240
        6472, 6499, 6525, 6552, 6578, 6605, 6632, 6658, 6685, 6712, 6739, 6766, 6793, 6820, 6847, 6874, 6901, 6929,
241
        6956, 6983, 7011, 7038, 7066, 7093, 7121, 7148, 7176, 7204, 7231, 7259, 7287, 7315, 7343, 7371, 7399, 7427,
241
        6956, 6983, 7011, 7038, 7066, 7093, 7121, 7148, 7176, 7204, 7231, 7259, 7287, 7315, 7343, 7371, 7399, 7427,
242
        7455, 7483, 7511, 7539, 7568, 7596, 7624, 7653, 7681, 7710, 7738, 7767, 7796, 7824, 7853, 7882, 7911, 7940,
242
        7455, 7483, 7511, 7539, 7568, 7596, 7624, 7653, 7681, 7710, 7738, 7767, 7796, 7824, 7853, 7882, 7911, 7940,
243
        7968, 7997, 8026, 8055, 8085, 8114, 8143, 8172, 8201, 8231, 8260, 8289, 8319, 8348, 8378, 8407, 8437, 8467,
243
        7968, 7997, 8026, 8055, 8085, 8114, 8143, 8172, 8201, 8231, 8260, 8289, 8319, 8348, 8378, 8407, 8437, 8467,
244
        8496, 8526, 8556, 8586, 8616, 8646, 8675, 8705, 8736, 8766, 8796, 8826, 8856, 8886, 8917, 8947, 8977, 9008,
244
        8496, 8526, 8556, 8586, 8616, 8646, 8675, 8705, 8736, 8766, 8796, 8826, 8856, 8886, 8917, 8947, 8977, 9008,
245
        9038, 9069, 9099, 9130, 9161, 9191, 9222, 9253, 9284, 9315, 9345, 9376, 9407, 9438, 9469, 9501, 9532, 9563,
245
        9038, 9069, 9099, 9130, 9161, 9191, 9222, 9253, 9284, 9315, 9345, 9376, 9407, 9438, 9469, 9501, 9532, 9563,
246
        9594, 9625, 9657, 9688, 9719, 9751, 9782, 9814, 9845, 9877, 9909, 9940, 9972, 10004, 10036, 10068, 10099,
246
        9594, 9625, 9657, 9688, 9719, 9751, 9782, 9814, 9845, 9877, 9909, 9940, 9972, 10004, 10036, 10068, 10099,
247
        10131, 10163, 10195, 10227, 10259, 10292, 10324, 10356, 10388, 10421, 10453, 10485, 10518, 10550, 10583,
247
        10131, 10163, 10195, 10227, 10259, 10292, 10324, 10356, 10388, 10421, 10453, 10485, 10518, 10550, 10583,
248
        10615, 10648, 10680, 10713, 10746, 10778, 10811, 10844, 10877, 10910, 10943, 10976, 11009, 11042, 11075,
248
        10615, 10648, 10680, 10713, 10746, 10778, 10811, 10844, 10877, 10910, 10943, 10976, 11009, 11042, 11075,
249
        11108, 11141, 11174, 11208, 11241, 11274, 11308, 11341, 11374, 11408, 11441, 11475, 11509, 11542, 11576,
249
        11108, 11141, 11174, 11208, 11241, 11274, 11308, 11341, 11374, 11408, 11441, 11475, 11509, 11542, 11576,
250
        11610, 11643, 11677, 11711, 11745, 11779, 11813, 11847, 11881, 11915, 11949, 11983, 12017, 12051, 12085,
250
        11610, 11643, 11677, 11711, 11745, 11779, 11813, 11847, 11881, 11915, 11949, 11983, 12017, 12051, 12085,
251
        12120, 12154, 12188, 12223, 12257, 12292, 12326, 12361, 12395, 12430, 12465, 12499, 12534, 12569, 12604,
251
        12120, 12154, 12188, 12223, 12257, 12292, 12326, 12361, 12395, 12430, 12465, 12499, 12534, 12569, 12604,
252
        12638, 12673, 12708, 12743, 12778, 12813, 12848, 12883, 12918, 12954, 12989, 13024, 13059, 13095, 13130,
252
        12638, 12673, 12708, 12743, 12778, 12813, 12848, 12883, 12918, 12954, 12989, 13024, 13059, 13095, 13130,
253
        13165, 13201, 13236, 13272, 13307, 13343, 13379, 13414, 13450, 13486, 13521, 13557, 13593, 13629, 13665,
253
        13165, 13201, 13236, 13272, 13307, 13343, 13379, 13414, 13450, 13486, 13521, 13557, 13593, 13629, 13665,
254
        13701, 13736, 13772, 13809, 13845, 13881, 13917, 13953, 13989, 14025, 14062, 14098, 14134, 14171, 14207,
254
        13701, 13736, 13772, 13809, 13845, 13881, 13917, 13953, 13989, 14025, 14062, 14098, 14134, 14171, 14207,
255
        14244, 14280, 14317, 14353, 14390, 14426, 14463, 14500, 14536, 14573, 14610, 14647, 14684, 14721, 14758,
255
        14244, 14280, 14317, 14353, 14390, 14426, 14463, 14500, 14536, 14573, 14610, 14647, 14684, 14721, 14758,
256
        14794, 14831, 14869, 14906, 14943, 14980, 15017, 15054, 15091, 15129, 15166, 15203, 15241, 15278, 15316,
256
        14794, 14831, 14869, 14906, 14943, 14980, 15017, 15054, 15091, 15129, 15166, 15203, 15241, 15278, 15316,
257
        15353, 15391, 15428, 15466, 15503, 15541, 15579, 15616, 15654, 15692, 15730, 15767, 15805, 15843, 15881,
257
        15353, 15391, 15428, 15466, 15503, 15541, 15579, 15616, 15654, 15692, 15730, 15767, 15805, 15843, 15881,
258
        15919, 15957, 15995, 16033, 16071, 16109, 16147, 16186, 16224, 16262, 16300, 16339, 16377, 16416, 16454,
258
        15919, 15957, 15995, 16033, 16071, 16109, 16147, 16186, 16224, 16262, 16300, 16339, 16377, 16416, 16454,
259
        16492, 16531, 16569, 16608, 16646, 16685, 16724, 16762, 16801, 16840, 16879, 16917, 16956, 16995, 17034,
259
        16492, 16531, 16569, 16608, 16646, 16685, 16724, 16762, 16801, 16840, 16879, 16917, 16956, 16995, 17034,
260
        17073, 17112, 17151, 17190, 17229, 17268, 17307, 17346, 17385, 17425, 17464, 17503, 17542, 17582, 17621,
260
        17073, 17112, 17151, 17190, 17229, 17268, 17307, 17346, 17385, 17425, 17464, 17503, 17542, 17582, 17621,
261
        17660, 17700, 17739, 17779, 17818, 17858, 17897, 17937, 17977, 18016, 18056, 18096, 18135, 18175, 18215,
261
        17660, 17700, 17739, 17779, 17818, 17858, 17897, 17937, 17977, 18016, 18056, 18096, 18135, 18175, 18215,
262
        18255, 18295, 18335, 18375, 18415, 18455, 18495, 18535, 18575, 18615, 18655, 18695, 18735, 18776, 18816,
262
        18255, 18295, 18335, 18375, 18415, 18455, 18495, 18535, 18575, 18615, 18655, 18695, 18735, 18776, 18816,
263
        18856, 18896, 18937, 18977, 19017, 19058, 19098, 19139, 19179, 19220, 19260, 19301, 19342, 19382, 19423,
263
        18856, 18896, 18937, 18977, 19017, 19058, 19098, 19139, 19179, 19220, 19260, 19301, 19342, 19382, 19423,
264
        19464, 19505, 19545, 19586, 19627, 19668, 19709, 19750, 19791, 19832, 19873, 19914, 19955, 19996, 20037,
264
        19464, 19505, 19545, 19586, 19627, 19668, 19709, 19750, 19791, 19832, 19873, 19914, 19955, 19996, 20037,
265
        20078, 20119, 20160, 20202, 20243, 20284, 20325, 20367, 20408, 20450, 20491, 20532, 20574, 20615, 20657,
265
        20078, 20119, 20160, 20202, 20243, 20284, 20325, 20367, 20408, 20450, 20491, 20532, 20574, 20615, 20657,
266
        20699, 20740, 20782, 20823, 20865, 20907, 20949, 20990, 21032, 21074, 21116, 21158, 21199, 21241, 21283,
266
        20699, 20740, 20782, 20823, 20865, 20907, 20949, 20990, 21032, 21074, 21116, 21158, 21199, 21241, 21283,
267
        21325, 21367, 21409, 21451, 21493, 21536, 21578, 21620, 21662, 21704, 21746, 21789, 21831, 21873, 21916,
267
        21325, 21367, 21409, 21451, 21493, 21536, 21578, 21620, 21662, 21704, 21746, 21789, 21831, 21873, 21916,
268
        21958, 22000, 22043, 22085, 22128, 22170, 22213, 22255, 22298, 22341, 22383, 22426, 22468, 22511, 22554,
268
        21958, 22000, 22043, 22085, 22128, 22170, 22213, 22255, 22298, 22341, 22383, 22426, 22468, 22511, 22554,
269
        22597, 22639, 22682, 22725, 22768, 22811, 22854, 22897, 22940, 22983, 23026, 23069, 23112, 23155, 23198,
269
        22597, 22639, 22682, 22725, 22768, 22811, 22854, 22897, 22940, 22983, 23026, 23069, 23112, 23155, 23198,
270
        23241, 23284, 23328, 23371, 23414, 23457, 23501, 23544, 23587, 23631, 23674, 23717, 23761, 23804, 23848,
270
        23241, 23284, 23328, 23371, 23414, 23457, 23501, 23544, 23587, 23631, 23674, 23717, 23761, 23804, 23848,
271
        23891, 23935, 23978, 24022, 24066, 24109, 24153, 24197, 24240, 24284, 24328, 24372, 24415, 24459, 24503,
271
        23891, 23935, 23978, 24022, 24066, 24109, 24153, 24197, 24240, 24284, 24328, 24372, 24415, 24459, 24503,
272
        24547, 24591, 24635, 24679, 24723, 24767, 24811, 24855, 24899, 24943, 24987, 25031, 25075, 25120, 25164,
272
        24547, 24591, 24635, 24679, 24723, 24767, 24811, 24855, 24899, 24943, 24987, 25031, 25075, 25120, 25164,
273
        25208, 25252, 25297, 25341, 25385, 25430, 25474, 25518, 25563, 25607, 25652, 25696, 25741, 25785, 25830,
273
        25208, 25252, 25297, 25341, 25385, 25430, 25474, 25518, 25563, 25607, 25652, 25696, 25741, 25785, 25830,
274
        25874, 25919, 25964, 26008, 26053, 26098, 26142, 26187, 26232, 26277, 26322, 26366, 26411, 26456, 26501,
274
        25874, 25919, 25964, 26008, 26053, 26098, 26142, 26187, 26232, 26277, 26322, 26366, 26411, 26456, 26501,
275
        26546, 26591, 26636, 26681, 26726, 26771, 26816, 26861, 26906, 26951, 26997, 27042, 27087, 27132 };
275
        26546, 26591, 26636, 26681, 26726, 26771, 26816, 26861, 26906, 26951, 26997, 27042, 27087, 27132 };
276
 
276
 
277
// -------------------------------------------------------------------------------------------
277
// -------------------------------------------------------------------------------------------
278
 
278
 
279
extern value_type slider[],cell[],var[],map[][0x100];
279
extern uint8_t slider[],map[][0x100];
-
 
280
extern value_type cell[], var[];
280
extern unsigned char *image_ptr;
281
extern unsigned char *image_ptr;
281
 
282
 
282
// -------------------------------------------------------------------------------------------
283
// -------------------------------------------------------------------------------------------
283
 
284
 
284
double costab[COSTABSIZE];
285
double costab[COSTABSIZE];
285
double tantab[TANTABSIZE];
286
double tantab[TANTABSIZE];
286
void init_trigtab(){
287
void init_trigtab(){
287
#ifdef PARSERTEST
288
#ifdef PARSERTEST
288
        return 0;
289
        return 0;
289
#else
290
#else
290
        int i;
291
        int i;
291
        for(i=0;i<COSTABSIZE;++i){
292
        for(i=0;i<COSTABSIZE;++i){
292
                costab[i] = cos(FFANGLE(i));
293
                costab[i] = cos(FFANGLE(i));
293
        }
294
        }
294
        for(i=0;i<TANTABSIZE;++i){
295
        for(i=0;i<TANTABSIZE;++i){
295
                if (i>=TANTABSIZE/2) {
296
                if (i>=TANTABSIZE/2) {
296
                        /* the last '-1' in the expression '512-i-1' is for FilterFactory compatibility, and to avoid the undefined pi/2 area */
297
                        /* the last '-1' in the expression '512-i-1' is for FilterFactory compatibility, and to avoid the undefined pi/2 area */
297
                        tantab[i] = -tantab[TANTABSIZE-i-1];
298
                        tantab[i] = -tantab[TANTABSIZE-i-1];
298
                } else {
299
                } else {
299
                        tantab[i] = tan(FFANGLE(i));
300
                        tantab[i] = tan(FFANGLE(i));
300
                }
301
                }
301
        }
302
        }
302
#endif
303
#endif
303
}
304
}
304
 
305
 
305
// -------------------------------------------------------------------------------------------
306
// -------------------------------------------------------------------------------------------
306
 
307
 
307
/* Channel z for the input pixel at coordinates x,y.
308
/* Channel z for the input pixel at coordinates x,y.
308
 * Coordinates are relative to the input image data (pb->inData) */
309
 * Coordinates are relative to the input image data (pb->inData) */
309
static value_type rawsrc(value_type x,value_type y,value_type z){
310
static value_type rawsrc(value_type x,value_type y,value_type z){
310
#ifdef PARSERTEST
311
#ifdef PARSERTEST
311
        return 0;
312
        return 0;
312
#else
313
#else
313
        if (HAS_BIG_DOC(gpb)) {
314
        if (HAS_BIG_DOC(gpb)) {
314
                if (x < BIGDOC_IN_RECT(gpb).left)
315
                if (x < BIGDOC_IN_RECT(gpb).left)
315
                        x = BIGDOC_IN_RECT(gpb).left;
316
                        x = BIGDOC_IN_RECT(gpb).left;
316
                else if (x >= BIGDOC_IN_RECT(gpb).right)
317
                else if (x >= BIGDOC_IN_RECT(gpb).right)
317
                        x = BIGDOC_IN_RECT(gpb).right - 1;
318
                        x = BIGDOC_IN_RECT(gpb).right - 1;
318
                if (y < BIGDOC_IN_RECT(gpb).top)
319
                if (y < BIGDOC_IN_RECT(gpb).top)
319
                        y = BIGDOC_IN_RECT(gpb).top;
320
                        y = BIGDOC_IN_RECT(gpb).top;
320
                else if (y >= BIGDOC_IN_RECT(gpb).bottom)
321
                else if (y >= BIGDOC_IN_RECT(gpb).bottom)
321
                        y = BIGDOC_IN_RECT(gpb).bottom - 1;
322
                        y = BIGDOC_IN_RECT(gpb).bottom - 1;
322
                return ((unsigned char*)gpb->inData)[(long)gpb->inRowBytes * (y - BIGDOC_IN_RECT(gpb).top)
323
                return ((unsigned char*)gpb->inData)[(long)gpb->inRowBytes * (y - BIGDOC_IN_RECT(gpb).top)
323
                        + (long)nplanes * (x - BIGDOC_IN_RECT(gpb).left) + z];
324
                        + (long)nplanes * (x - BIGDOC_IN_RECT(gpb).left) + z];
324
        } else {
325
        } else {
325
                if (x < IN_RECT(gpb).left)
326
                if (x < IN_RECT(gpb).left)
326
                        x = IN_RECT(gpb).left;
327
                        x = IN_RECT(gpb).left;
327
                else if (x >= IN_RECT(gpb).right)
328
                else if (x >= IN_RECT(gpb).right)
328
                        x = IN_RECT(gpb).right - 1;
329
                        x = IN_RECT(gpb).right - 1;
329
                if (y < IN_RECT(gpb).top)
330
                if (y < IN_RECT(gpb).top)
330
                        y = IN_RECT(gpb).top;
331
                        y = IN_RECT(gpb).top;
331
                else if (y >= IN_RECT(gpb).bottom)
332
                else if (y >= IN_RECT(gpb).bottom)
332
                        y = IN_RECT(gpb).bottom - 1;
333
                        y = IN_RECT(gpb).bottom - 1;
333
                return ((unsigned char*)gpb->inData)[(long)gpb->inRowBytes * (y - IN_RECT(gpb).top)
334
                return ((unsigned char*)gpb->inData)[(long)gpb->inRowBytes * (y - IN_RECT(gpb).top)
334
                        + (long)nplanes * (x - IN_RECT(gpb).left) + z];
335
                        + (long)nplanes * (x - IN_RECT(gpb).left) + z];
335
        }
336
        }
336
#endif
337
#endif
337
}
338
}
338
 
339
 
339
// -------------------------------------------------------------------------------------------
340
// -------------------------------------------------------------------------------------------
340
 
341
 
341
/* src(x,y,z) Channel z for the pixel at coordinates x,y.
342
/* src(x,y,z) Channel z for the pixel at coordinates x,y.
342
 * Coordinates are relative to filtered area (selection). */
343
 * Coordinates are relative to filtered area (selection). */
343
 
344
 
344
value_type ff_src(value_type x,value_type y,value_type z){
345
value_type ff_src(value_type x,value_type y,value_type z){
345
#ifdef PARSERTEST
346
#ifdef PARSERTEST
346
        return 0;
347
        return 0;
347
#else
348
#else
348
        if(x < 0)
349
        if(x < 0)
349
                x = 0;
350
                x = 0;
350
        else if(x >= var['X'])
351
        else if(x >= var['X'])
351
                x = var['X']-1;
352
                x = var['X']-1;
352
        if(y < 0)
353
        if(y < 0)
353
                y = 0;
354
                y = 0;
354
        else if(y >= var['Y'])
355
        else if(y >= var['Y'])
355
                y = var['Y']-1;
356
                y = var['Y']-1;
356
        return z >= 0 && z < var['Z'] ?
357
        return z >= 0 && z < var['Z'] ?
357
                image_ptr[(long)gpb->inRowBytes*y + (long)nplanes*x + z] : 0;
358
                image_ptr[(long)gpb->inRowBytes*y + (long)nplanes*x + z] : 0;
358
#endif
359
#endif
359
}
360
}
360
 
361
 
361
// -------------------------------------------------------------------------------------------
362
// -------------------------------------------------------------------------------------------
362
 
363
 
363
/* r2x(d,m) x displacement of the pixel m units away, at an angle of d,
364
/* r2x(d,m) x displacement of the pixel m units away, at an angle of d,
364
   from an arbitrary center */
365
   from an arbitrary center */
365
 
366
 
366
value_type factory_r2x(value_type d, value_type m) {
367
value_type factory_r2x(value_type d, value_type m) {
367
        // https://misc.daniel-marschall.de/projects/filter_factory/function_r2x.html
368
        // https://misc.daniel-marschall.de/projects/filter_factory/function_r2x.html
368
#ifdef PARSERTEST
369
#ifdef PARSERTEST
369
        return 0;
370
        return 0;
370
#else
371
#else
371
        int eax = m;
372
        int eax = m;
372
        int ebx = d;
373
        int ebx = d;
373
        ebx &= 1023;
374
        ebx &= 1023;
374
        ebx = FACTORY_COS_LOOKUP[ebx];
375
        ebx = FACTORY_COS_LOOKUP[ebx];
375
        return (((int64_t)eax * (int64_t)ebx) + 8191) >> 14;
376
        return (((int64_t)eax * (int64_t)ebx) + 8191) >> 14;
376
#endif
377
#endif
377
}
378
}
378
 
379
 
379
value_type foundry_r2x(value_type d, value_type m) {
380
value_type foundry_r2x(value_type d, value_type m) {
380
#ifdef PARSERTEST
381
#ifdef PARSERTEST
381
        return 0;
382
        return 0;
382
#else
383
#else
383
        return (value_type)RINT(m * costab[abs(d) % COSTABSIZE]);
384
        return (value_type)RINT(m * costab[abs(d) % COSTABSIZE]);
384
#endif
385
#endif
385
}
386
}
386
 
387
 
387
value_type ff_r2x(value_type d, value_type m) {
388
value_type ff_r2x(value_type d, value_type m) {
388
#ifdef use_filterfactory_implementation_r2x
389
#ifdef use_filterfactory_implementation_r2x
389
        return factory_r2x(d, m);
390
        return factory_r2x(d, m);
390
#else
391
#else
391
        return foundry_r2x(d, m);
392
        return foundry_r2x(d, m);
392
#endif
393
#endif
393
}
394
}
394
 
395
 
395
// -------------------------------------------------------------------------------------------
396
// -------------------------------------------------------------------------------------------
396
 
397
 
397
/* r2y(d,m) y displacement of the pixel m units away, at an angle of d,
398
/* r2y(d,m) y displacement of the pixel m units away, at an angle of d,
398
   from an arbitrary center */
399
   from an arbitrary center */
399
 
400
 
400
value_type factory_r2y(value_type d, value_type m) {
401
value_type factory_r2y(value_type d, value_type m) {
401
#ifdef PARSERTEST
402
#ifdef PARSERTEST
402
        return 0;
403
        return 0;
403
#else
404
#else
404
        // https://misc.daniel-marschall.de/projects/filter_factory/function_r2y.html
405
        // https://misc.daniel-marschall.de/projects/filter_factory/function_r2y.html
405
        return factory_r2x(d - 256, m);
406
        return factory_r2x(d - 256, m);
406
#endif
407
#endif
407
}
408
}
408
 
409
 
409
value_type foundry_r2y(value_type d, value_type m) {
410
value_type foundry_r2y(value_type d, value_type m) {
410
#ifdef PARSERTEST
411
#ifdef PARSERTEST
411
        return 0;
412
        return 0;
412
#else
413
#else
413
        return (value_type)RINT(m * costab[abs(d - 256) % COSTABSIZE]);
414
        return (value_type)RINT(m * costab[abs(d - 256) % COSTABSIZE]);
414
#endif
415
#endif
415
}
416
}
416
 
417
 
417
value_type ff_r2y(value_type d, value_type m) {
418
value_type ff_r2y(value_type d, value_type m) {
418
#ifdef use_filterfactory_implementation_r2y
419
#ifdef use_filterfactory_implementation_r2y
419
        return factory_r2y(d, m);
420
        return factory_r2y(d, m);
420
#else
421
#else
421
        return foundry_r2y(d, m);
422
        return foundry_r2y(d, m);
422
#endif
423
#endif
423
}
424
}
424
 
425
 
425
// -------------------------------------------------------------------------------------------
426
// -------------------------------------------------------------------------------------------
426
 
427
 
427
/* rad(d,m,z) Channel z in the source image, which is m units away,
428
/* rad(d,m,z) Channel z in the source image, which is m units away,
428
        at an angle of d, from the center of the image */
429
        at an angle of d, from the center of the image */
429
 
430
 
430
value_type factory_rad(value_type d, value_type m, value_type z) {
431
value_type factory_rad(value_type d, value_type m, value_type z) {
431
#ifdef PARSERTEST
432
#ifdef PARSERTEST
432
        return 0;
433
        return 0;
433
#else
434
#else
434
        // https://misc.daniel-marschall.de/projects/filter_factory/function_rad.html
435
        // https://misc.daniel-marschall.de/projects/filter_factory/function_rad.html
435
 
436
 
436
        const int xmin = 0;
437
        const int xmin = 0;
437
        const int ymin = 0;
438
        const int ymin = 0;
438
        const int zmin = 0;
439
        const int zmin = 0;
439
 
440
 
440
        int eax, ebx, ecx;
441
        int eax, ebx, ecx;
441
 
442
 
442
        ebx = FACTORY_COS_LOOKUP[d & 1023];
443
        ebx = FACTORY_COS_LOOKUP[d & 1023];
443
        eax = (((int64_t)m * (int64_t)ebx) + 8191) >> 14;
444
        eax = (((int64_t)m * (int64_t)ebx) + 8191) >> 14;
444
        ecx = ((var['X'] - xmin) >> 1) + eax;
445
        ecx = ((var['X'] - xmin) >> 1) + eax;
445
        if (ecx < 0) {
446
        if (ecx < 0) {
446
                ecx = 0;
447
                ecx = 0;
447
        }
448
        }
448
        else {
449
        else {
449
                eax = var['X'];
450
                eax = var['X'];
450
                if (ecx >= eax) {
451
                if (ecx >= eax) {
451
                        ecx = eax - 1;
452
                        ecx = eax - 1;
452
                }
453
                }
453
        }
454
        }
454
 
455
 
455
        ebx = FACTORY_COS_LOOKUP[(d - 256) & 1023];
456
        ebx = FACTORY_COS_LOOKUP[(d - 256) & 1023];
456
        eax = (((int64_t)m * (int64_t)ebx) + 8191) >> 14;
457
        eax = (((int64_t)m * (int64_t)ebx) + 8191) >> 14;
457
        ebx = ((var['Y'] - ymin) >> 1) + eax;
458
        ebx = ((var['Y'] - ymin) >> 1) + eax;
458
        if (ebx < 0) {
459
        if (ebx < 0) {
459
                ebx = 0;
460
                ebx = 0;
460
        }
461
        }
461
        else {
462
        else {
462
                eax = var['Y'];
463
                eax = var['Y'];
463
                if (ebx >= eax) {
464
                if (ebx >= eax) {
464
                        ebx = eax - 1;
465
                        ebx = eax - 1;
465
                }
466
                }
466
        }
467
        }
467
 
468
 
468
        // Now return pixel [x=ecx,y=ebx,z=esi] from the source image!
469
        // Now return pixel [x=ecx,y=ebx,z=esi] from the source image!
469
        //return ff_src(ecx, ebx, z);
470
        //return ff_src(ecx, ebx, z);
470
        ebx *= gpb->inRowBytes;
471
        ebx *= gpb->inRowBytes;
471
        ecx *= var['Z'] - zmin;
472
        ecx *= var['Z'] - zmin;
472
        return image_ptr[z+ebx+ecx];
473
        return image_ptr[z+ebx+ecx];
473
#endif
474
#endif
474
}
475
}
475
 
476
 
476
value_type foundry_rad(value_type d, value_type m, value_type z) {
477
value_type foundry_rad(value_type d, value_type m, value_type z) {
477
#ifdef PARSERTEST
478
#ifdef PARSERTEST
478
        return 0;
479
        return 0;
479
#else
480
#else
480
        return ff_src(foundry_r2x(d, m) + var['X'] / 2, foundry_r2y(d, m) + var['Y'] / 2, z);
481
        return ff_src(foundry_r2x(d, m) + var['X'] / 2, foundry_r2y(d, m) + var['Y'] / 2, z);
481
#endif
482
#endif
482
}
483
}
483
 
484
 
484
value_type ff_rad(value_type d,value_type m,value_type z){
485
value_type ff_rad(value_type d,value_type m,value_type z){
485
#ifdef use_filterfactory_implementation_rad
486
#ifdef use_filterfactory_implementation_rad
486
        return factory_rad(d, m, z);
487
        return factory_rad(d, m, z);
487
#else
488
#else
488
        return foundry_rad(d, m, z);
489
        return foundry_rad(d, m, z);
489
#endif
490
#endif
490
}
491
}
491
 
492
 
492
// -------------------------------------------------------------------------------------------
493
// -------------------------------------------------------------------------------------------
493
 
494
 
494
/* ctl(i) Value of slider i, where i is an integer between 0 and 7, inclusive */
495
/* ctl(i) Value of slider i, where i is an integer between 0 and 7, inclusive */
495
value_type ff_ctl(value_type i){
496
value_type ff_ctl(value_type i){
496
#ifdef PARSERTEST
497
#ifdef PARSERTEST
497
        return 0;
498
        return 0;
498
#else
499
#else
499
        return i>=0 && i<=7 ? slider[i] : 0;
500
        return i>=0 && i<=7 ? slider[i] : 0;
500
#endif
501
#endif
501
}
502
}
502
 
503
 
503
// -------------------------------------------------------------------------------------------
504
// -------------------------------------------------------------------------------------------
504
 
505
 
505
/* val(i,a,b) Value of slider i, mapped onto the range a to b */
506
/* val(i,a,b) Value of slider i, mapped onto the range a to b */
506
value_type ff_val(value_type i,value_type a,value_type b){
507
value_type ff_val(value_type i,value_type a,value_type b){
507
#ifdef PARSERTEST
508
#ifdef PARSERTEST
508
        return 0;
509
        return 0;
509
#else
510
#else
510
        return ((long)ff_ctl(i)*(b-a))/255 + a;
511
        return ((long)ff_ctl(i)*(b-a))/255 + a;
511
#endif
512
#endif
512
}
513
}
513
 
514
 
514
// -------------------------------------------------------------------------------------------
515
// -------------------------------------------------------------------------------------------
515
 
516
 
516
/* map(i,n) Item n from mapping table i, where i is an integer between
517
/* map(i,n) Item n from mapping table i, where i is an integer between
517
        0 and 3, inclusive, and n is and integer between 0 and 255,
518
        0 and 3, inclusive, and n is and integer between 0 and 255,
518
        inclusive */
519
        inclusive */
519
value_type ff_map(value_type i,value_type n){
520
value_type ff_map(value_type i,value_type n){
520
#ifdef PARSERTEST
521
#ifdef PARSERTEST
521
        return 0;
522
        return 0;
522
#else
523
#else
523
        /*
524
        /*
524
        if( i>=0 && i<=3 && n>=0 && n<=255 ){
525
        if( i>=0 && i<=3 && n>=0 && n<=255 ){
525
                int H = slider[i*2],L = slider[i*2+1];
526
                int H = slider[i*2],L = slider[i*2+1];
526
                return n<=L || H==L ? 0 : ( n>=H ? 255 : ((n-L)*255L)/(H-L) );
527
                return n<=L || H==L ? 0 : ( n>=H ? 255 : ((n-L)*255L)/(H-L) );
527
        }else
528
        }else
528
                return 0;
529
                return 0;
529
*/
530
*/
530
        // this code is from GIMP User Filter
531
        // this code is from GIMP User Filter
531
        value_type x = ff_ctl(i*2),
532
        value_type x = ff_ctl(i*2),
532
                           y = ff_ctl(i*2+1);
533
                           y = ff_ctl(i*2+1);
533
        return abs(((long)n*(y-x) / 255)+x);
534
        return abs(((long)n*(y-x) / 255)+x);
534
#endif
535
#endif
535
}
536
}
536
 
537
 
537
// -------------------------------------------------------------------------------------------
538
// -------------------------------------------------------------------------------------------
538
 
539
 
539
/* min(a,b) Lesser of a and b */
540
/* min(a,b) Lesser of a and b */
540
value_type ff_min(value_type a,value_type b){
541
value_type ff_min(value_type a,value_type b){
541
#ifdef PARSERTEST
542
#ifdef PARSERTEST
542
        return 0;
543
        return 0;
543
#else
544
#else
544
        return a < b ? a : b;
545
        return a < b ? a : b;
545
#endif
546
#endif
546
}
547
}
547
 
548
 
548
// -------------------------------------------------------------------------------------------
549
// -------------------------------------------------------------------------------------------
549
 
550
 
550
/* max(a,b) Greater of a and b */
551
/* max(a,b) Greater of a and b */
551
value_type ff_max(value_type a,value_type b){
552
value_type ff_max(value_type a,value_type b){
552
#ifdef PARSERTEST
553
#ifdef PARSERTEST
553
        return 0;
554
        return 0;
554
#else
555
#else
555
        return a > b ? a : b;
556
        return a > b ? a : b;
556
#endif
557
#endif
557
}
558
}
558
 
559
 
559
// -------------------------------------------------------------------------------------------
560
// -------------------------------------------------------------------------------------------
560
 
561
 
561
/* abs(a) Absolute value of a */
562
/* abs(a) Absolute value of a */
562
value_type ff_abs(value_type a){
563
value_type ff_abs(value_type a){
563
#ifdef PARSERTEST
564
#ifdef PARSERTEST
564
        return 0;
565
        return 0;
565
#else
566
#else
566
        return abs(a);
567
        return abs(a);
567
#endif
568
#endif
568
}
569
}
569
 
570
 
570
// -------------------------------------------------------------------------------------------
571
// -------------------------------------------------------------------------------------------
571
 
572
 
572
/* add(a,b,c) Sum of a and b, or c, whichever is lesser */
573
/* add(a,b,c) Sum of a and b, or c, whichever is lesser */
573
value_type ff_add(value_type a,value_type b,value_type c){
574
value_type ff_add(value_type a,value_type b,value_type c){
574
#ifdef PARSERTEST
575
#ifdef PARSERTEST
575
        return 0;
576
        return 0;
576
#else
577
#else
577
        return ff_min(a+b,c);
578
        return ff_min(a+b,c);
578
#endif
579
#endif
579
}
580
}
580
 
581
 
581
// -------------------------------------------------------------------------------------------
582
// -------------------------------------------------------------------------------------------
582
 
583
 
583
/* sub(a,b,c) Difference of a and b, or c, whichever is greater */
584
/* sub(a,b,c) Difference of a and b, or c, whichever is greater */
584
value_type ff_sub(value_type a,value_type b,value_type c){
585
value_type ff_sub(value_type a,value_type b,value_type c){
585
#ifdef PARSERTEST
586
#ifdef PARSERTEST
586
        return 0;
587
        return 0;
587
#else
588
#else
588
        return ff_max(ff_dif(a,b),c);
589
        return ff_max(ff_dif(a,b),c);
589
#endif
590
#endif
590
}
591
}
591
 
592
 
592
// -------------------------------------------------------------------------------------------
593
// -------------------------------------------------------------------------------------------
593
 
594
 
594
/* dif(a,b) Absolute value of the difference of a and b */
595
/* dif(a,b) Absolute value of the difference of a and b */
595
value_type ff_dif(value_type a,value_type b){
596
value_type ff_dif(value_type a,value_type b){
596
#ifdef PARSERTEST
597
#ifdef PARSERTEST
597
        return 0;
598
        return 0;
598
#else
599
#else
599
        return abs(a-b);
600
        return abs(a-b);
600
#endif
601
#endif
601
}
602
}
602
 
603
 
603
// -------------------------------------------------------------------------------------------
604
// -------------------------------------------------------------------------------------------
604
 
605
 
605
/* rnd(a,b) Random number between a and b, inclusive */
606
/* rnd(a,b) Random number between a and b, inclusive */
606
 
607
 
607
struct factoryRngState {
608
struct factoryRngState {
608
        uint16_t index1;
609
        uint16_t index1;
609
        uint16_t index2;
610
        uint16_t index2;
610
        uint32_t seedTable[56];
611
        uint32_t seedTable[56];
611
        uint32_t seed;
612
        uint32_t seed;
612
        uint32_t seedSave;
613
        uint32_t seedSave;
613
} gFactoryRngState;
614
} gFactoryRngState;
614
 
615
 
615
void factory_fill_rnd_lookup(uint32_t seed, struct factoryRngState* state) {
616
void factory_fill_rnd_lookup(uint32_t seed, struct factoryRngState* state) {
616
#ifdef PARSERTEST
617
#ifdef PARSERTEST
617
        return 0;
618
        return 0;
618
#else
619
#else
619
        // Algorithm of Filter Factory
620
        // Algorithm of Filter Factory
620
        // Filter Factory uses Donald E.Knuth's subtractive
621
        // Filter Factory uses Donald E.Knuth's subtractive
621
        // random number generator algorithm ("ran3"), which has been published
622
        // random number generator algorithm ("ran3"), which has been published
622
        // in Page 283 of "The Art of Computer Programming, volume 2: Seminumerical Algorithms",
623
        // in Page 283 of "The Art of Computer Programming, volume 2: Seminumerical Algorithms",
623
        // Addison-Wesley, Reading, MA, second edition, 1981.
624
        // Addison-Wesley, Reading, MA, second edition, 1981.
624
        // https://www.cec.uchile.cl/cinetica/pcordero/MC_libros/NumericalRecipesinC.pdf (PDF Page 307)
625
        // https://www.cec.uchile.cl/cinetica/pcordero/MC_libros/NumericalRecipesinC.pdf (PDF Page 307)
625
 
626
 
626
        long mj, mk;
627
        long mj, mk;
627
        int i, ii, k;
628
        int i, ii, k;
628
 
629
 
629
        // 161803398 = 1.61803398 * 10^8 ~= phi * 10^8
630
        // 161803398 = 1.61803398 * 10^8 ~= phi * 10^8
630
        mj = 161803398 - (seed & 0x7fff);
631
        mj = 161803398 - (seed & 0x7fff);
631
        state->seedTable[55] = mj;
632
        state->seedTable[55] = mj;
632
 
633
 
633
        mk = 1;
634
        mk = 1;
634
        ii = 0;
635
        ii = 0;
635
        for (i=1; i<=54; ++i) {
636
        for (i=1; i<=54; ++i) {
636
                if ((ii += 21) >= 55) ii -= 55; // ii = (21*i)%55;
637
                if ((ii += 21) >= 55) ii -= 55; // ii = (21*i)%55;
637
                state->seedTable[ii] = mk;
638
                state->seedTable[ii] = mk;
638
                mk = mj - mk;
639
                mk = mj - mk;
639
                mj = state->seedTable[ii];
640
                mj = state->seedTable[ii];
640
        }
641
        }
641
 
642
 
642
        for (k=1; k<=4; ++k) {
643
        for (k=1; k<=4; ++k) {
643
                ii = 30;
644
                ii = 30;
644
                for (i=1; i<=55; ++i) {
645
                for (i=1; i<=55; ++i) {
645
                        if ((ii += 1) >= 55) ii -= 55;
646
                        if ((ii += 1) >= 55) ii -= 55;
646
                        state->seedTable[i] -= state->seedTable[1 + ii]; // 1 + (i+30)%55
647
                        state->seedTable[i] -= state->seedTable[1 + ii]; // 1 + (i+30)%55
647
                }
648
                }
648
        }
649
        }
649
 
650
 
650
        state->seedSave = seed;
651
        state->seedSave = seed;
651
 
652
 
652
        return;
653
        return;
653
#endif
654
#endif
654
}
655
}
655
 
656
 
656
uint32_t factory_rnd(uint32_t a, uint32_t b, struct factoryRngState* state) {
657
uint32_t factory_rnd(uint32_t a, uint32_t b, struct factoryRngState* state) {
657
#ifdef PARSERTEST
658
#ifdef PARSERTEST
658
        return 0;
659
        return 0;
659
#else
660
#else
660
        uint32_t mj; // Note: This must be "uint32_t". With "long" (as described by Knuth), it won't match FilterFactory's algorithm
661
        uint32_t mj; // Note: This must be "uint32_t". With "long" (as described by Knuth), it won't match FilterFactory's algorithm
661
        int range;
662
        int range;
662
 
663
 
663
        if (state->seed != state->seedSave) {
664
        if (state->seed != state->seedSave) {
664
                // (Intentional) behavior of Filter Foundry
665
                // (Intentional) behavior of Filter Foundry
665
                factory_fill_rnd_lookup(state->seed, &gFactoryRngState);
666
                factory_fill_rnd_lookup(state->seed, &gFactoryRngState);
666
                state->index1 = 0;
667
                state->index1 = 0;
667
                state->index2 = 31;
668
                state->index2 = 31;
668
        }
669
        }
669
 
670
 
670
        // Algorithm of Filter Factory
671
        // Algorithm of Filter Factory
671
        // Filter Factory uses Donald E.Knuth's subtractive
672
        // Filter Factory uses Donald E.Knuth's subtractive
672
        // random number generator algorithm ("ran3"), which has been published
673
        // random number generator algorithm ("ran3"), which has been published
673
        // in Page 283 of "The Art of Computer Programming, volume 2: Seminumerical Algorithms",
674
        // in Page 283 of "The Art of Computer Programming, volume 2: Seminumerical Algorithms",
674
        // Addison-Wesley, Reading, MA, second edition, 1981.
675
        // Addison-Wesley, Reading, MA, second edition, 1981.
675
        // https://www.cec.uchile.cl/cinetica/pcordero/MC_libros/NumericalRecipesinC.pdf (PDF Page 307)
676
        // https://www.cec.uchile.cl/cinetica/pcordero/MC_libros/NumericalRecipesinC.pdf (PDF Page 307)
676
 
677
 
677
        if (++state->index1 == 56) state->index1 = 1;
678
        if (++state->index1 == 56) state->index1 = 1;
678
        if (++state->index2 == 56) state->index2 = 1;
679
        if (++state->index2 == 56) state->index2 = 1;
679
 
680
 
680
        mj = state->seedTable[state->index1] -
681
        mj = state->seedTable[state->index1] -
681
             state->seedTable[state->index2];
682
             state->seedTable[state->index2];
682
        state->seedTable[state->index1] = mj;
683
        state->seedTable[state->index1] = mj;
683
 
684
 
684
        // This is Filter Factory specific:
685
        // This is Filter Factory specific:
685
        // Reduce result into interval [a..b] by applying (a + (mj % (b - a + 1))
686
        // Reduce result into interval [a..b] by applying (a + (mj % (b - a + 1))
686
        // Try to avoid modulo in order to increase performance
687
        // Try to avoid modulo in order to increase performance
687
        range = b - a;
688
        range = b - a;
688
        if (range < 0) return 0;
689
        if (range < 0) return 0;
689
        switch (range) {
690
        switch (range) {
690
                case 255:
691
                case 255:
691
                        return a + (mj & 0xFF);
692
                        return a + (mj & 0xFF);
692
                case 127:
693
                case 127:
693
                        return a + (mj & 0x7F);
694
                        return a + (mj & 0x7F);
694
                case 63:
695
                case 63:
695
                        return a + (mj & 0x3F);
696
                        return a + (mj & 0x3F);
696
                case 31:
697
                case 31:
697
                        return a + (mj & 0x1F);
698
                        return a + (mj & 0x1F);
698
                case 15:
699
                case 15:
699
                        return a + (mj & 0xF);
700
                        return a + (mj & 0xF);
700
                case 7:
701
                case 7:
701
                        return a + (mj & 0x7);
702
                        return a + (mj & 0x7);
702
                case 3:
703
                case 3:
703
                        return a + (mj & 0x3);
704
                        return a + (mj & 0x3);
704
                case 1:
705
                case 1:
705
                        return a + (mj & 0x1);
706
                        return a + (mj & 0x1);
706
                case 0:
707
                case 0:
707
                        return a;
708
                        return a;
708
                default:
709
                default:
709
                        return a + (mj % (range + 1));
710
                        return a + (mj % (range + 1));
710
        }
711
        }
711
#endif
712
#endif
712
}
713
}
713
 
714
 
714
value_type foundry_rnd(value_type a, value_type b) {
715
value_type foundry_rnd(value_type a, value_type b) {
715
#ifdef PARSERTEST
716
#ifdef PARSERTEST
716
        return 0;
717
        return 0;
717
#else
718
#else
718
        return (int)((abs(a-b)+1)*(rand()/(RAND_MAX+1.))) + ff_min(a,b);
719
        return (int)((abs(a-b)+1)*(rand()/(RAND_MAX+1.))) + ff_min(a,b);
719
        //      return ((unsigned)rand() % (ff_dif(a,b)+1)) + ff_min(a,b);
720
        //      return ((unsigned)rand() % (ff_dif(a,b)+1)) + ff_min(a,b);
720
#endif
721
#endif
721
}
722
}
722
 
723
 
723
value_type ff_rnd(value_type a,value_type b){
724
value_type ff_rnd(value_type a,value_type b){
724
#ifdef use_filterfactory_implementation_rnd
725
#ifdef use_filterfactory_implementation_rnd
725
        return factory_rnd(a, b, &gFactoryRngState);
726
        return factory_rnd(a, b, &gFactoryRngState);
726
#else
727
#else
727
        return foundry_rnd(a, b);
728
        return foundry_rnd(a, b);
728
#endif
729
#endif
729
}
730
}
730
 
731
 
731
// -------------------------------------------------------------------------------------------
732
// -------------------------------------------------------------------------------------------
732
 
733
 
733
/* rst(i) sets a random seed and returns 0. (undocumented Filter Factory function).
734
/* rst(i) sets a random seed and returns 0. (undocumented Filter Factory function).
734
   Added by DM, 18 Dec 2018 */
735
   Added by DM, 18 Dec 2018 */
735
 
736
 
736
int32_t factory_rst(uint32_t seed, struct factoryRngState* state) {
737
int32_t factory_rst(uint32_t seed, struct factoryRngState* state) {
737
#ifdef PARSERTEST
738
#ifdef PARSERTEST
738
        return 0;
739
        return 0;
739
#else
740
#else
740
        // Attention: This is NOT the FilterFactory rst() implementation!
741
        // Attention: This is NOT the FilterFactory rst() implementation!
741
 
742
 
742
        // We implement rst(i) completely differently in Filter Foundry:
743
        // We implement rst(i) completely differently in Filter Foundry:
743
        // Every call of rst() will renew the lookup table.
744
        // Every call of rst() will renew the lookup table.
744
        // In Filter Factory, there are strange/buggy things going
745
        // In Filter Factory, there are strange/buggy things going
745
        // on: rst(i) only sets a seed and the lookup table is renewed
746
        // on: rst(i) only sets a seed and the lookup table is renewed
746
        // at the NEXT invocation of the filter. Furthermore, in FilterFactory,
747
        // at the NEXT invocation of the filter. Furthermore, in FilterFactory,
747
        // the state is not reset between invocations, therefore, the preview image
748
        // the state is not reset between invocations, therefore, the preview image
748
        // will influence the PRNG state of the final image...
749
        // will influence the PRNG state of the final image...
749
        // More information at "Filter Factory Compatibility.md"
750
        // More information at "Filter Factory Compatibility.md"
750
 
751
 
751
        state->seed = seed;
752
        state->seed = seed;
752
 
753
 
753
        // Force renewal of the PRNG state in the next rnd(a,b) call.
754
        // Force renewal of the PRNG state in the next rnd(a,b) call.
754
        // This allows us to use:
755
        // This allows us to use:
755
        //    (x==0?rst(1):0), rnd(0,255)
756
        //    (x==0?rst(1):0), rnd(0,255)
756
        // But it is slower and this won't work anymore:
757
        // But it is slower and this won't work anymore:
757
        //    rst(0), rnd(0,255)
758
        //    rst(0), rnd(0,255)
758
        state->seedSave = seed + 1;
759
        state->seedSave = seed + 1;
759
 
760
 
760
        return 0;
761
        return 0;
761
#endif
762
#endif
762
}
763
}
763
 
764
 
764
value_type foundry_rst(value_type seed) {
765
value_type foundry_rst(value_type seed) {
765
#ifdef PARSERTEST
766
#ifdef PARSERTEST
766
        return 0;
767
        return 0;
767
#else
768
#else
768
        srand(seed);
769
        srand(seed);
769
        return 0;
770
        return 0;
770
#endif
771
#endif
771
}
772
}
772
 
773
 
773
value_type ff_rst(value_type seed) {
774
value_type ff_rst(value_type seed) {
774
#ifdef use_filterfactory_implementation_rnd
775
#ifdef use_filterfactory_implementation_rnd
775
        return factory_rst(seed, &gFactoryRngState);
776
        return factory_rst(seed, &gFactoryRngState);
776
#else
777
#else
777
        return foundry_rst(seed);
778
        return foundry_rst(seed);
778
#endif
779
#endif
779
}
780
}
780
 
781
 
781
// -------------------------------------------------------------------------------------------
782
// -------------------------------------------------------------------------------------------
782
 
783
 
783
void factory_initialize_rnd_variables() {
784
void factory_initialize_rnd_variables() {
784
#ifdef PARSERTEST
785
#ifdef PARSERTEST
785
        return 0;
786
        return 0;
786
#else
787
#else
787
        gFactoryRngState.seed = 0; // default seed
788
        gFactoryRngState.seed = 0; // default seed
788
        gFactoryRngState.seedSave = gFactoryRngState.seed + 1; // force rnd() to call factory_fill_rnd_lookup()
789
        gFactoryRngState.seedSave = gFactoryRngState.seed + 1; // force rnd() to call factory_fill_rnd_lookup()
789
#endif
790
#endif
790
}
791
}
791
 
792
 
792
void foundry_initialize_rnd_variables() {
793
void foundry_initialize_rnd_variables() {
793
#ifdef PARSERTEST
794
#ifdef PARSERTEST
794
        return 0;
795
        return 0;
795
#else
796
#else
796
        foundry_rst(691204);
797
        foundry_rst(691204);
797
#endif
798
#endif
798
}
799
}
799
 
800
 
800
void initialize_rnd_variables() {
801
void initialize_rnd_variables() {
801
#ifdef use_filterfactory_implementation_rnd
802
#ifdef use_filterfactory_implementation_rnd
802
        factory_initialize_rnd_variables();
803
        factory_initialize_rnd_variables();
803
#else
804
#else
804
        foundry_initialize_rnd_variables();
805
        foundry_initialize_rnd_variables();
805
#endif
806
#endif
806
}
807
}
807
 
808
 
808
// -------------------------------------------------------------------------------------------
809
// -------------------------------------------------------------------------------------------
809
 
810
 
810
/* mix(a,b,n,d) Mixture of a and b by fraction n/d, a*n/d+b*(d-n)/d */
811
/* mix(a,b,n,d) Mixture of a and b by fraction n/d, a*n/d+b*(d-n)/d */
811
value_type ff_mix(value_type a,value_type b,value_type n,value_type d){
812
value_type ff_mix(value_type a,value_type b,value_type n,value_type d){
812
#ifdef PARSERTEST
813
#ifdef PARSERTEST
813
        return 0;
814
        return 0;
814
#else
815
#else
815
        return d ? ((long)a*n)/d + ((long)b*(d-n))/d : 0;
816
        return d ? ((long)a*n)/d + ((long)b*(d-n))/d : 0;
816
#endif
817
#endif
817
}
818
}
818
 
819
 
819
// -------------------------------------------------------------------------------------------
820
// -------------------------------------------------------------------------------------------
820
 
821
 
821
/* scl(a,il,ih,ol,oh) Scale a from input range (il to ih)
822
/* scl(a,il,ih,ol,oh) Scale a from input range (il to ih)
822
                      to output range (ol to oh) */
823
                      to output range (ol to oh) */
823
value_type ff_scl(value_type a, value_type il, value_type ih,
824
value_type ff_scl(value_type a, value_type il, value_type ih,
824
        value_type ol, value_type oh) {
825
        value_type ol, value_type oh) {
825
#ifdef PARSERTEST
826
#ifdef PARSERTEST
826
        return 0;
827
        return 0;
827
#else
828
#else
828
        return ih == il ? 0 : ol + ((long)(oh - ol) * (a - il)) / (ih - il);
829
        return ih == il ? 0 : ol + ((long)(oh - ol) * (a - il)) / (ih - il);
829
#endif
830
#endif
830
}
831
}
831
 
832
 
832
// -------------------------------------------------------------------------------------------
833
// -------------------------------------------------------------------------------------------
833
 
834
 
834
/* pow(b,e) Calculates the base to the exponent power, that is, b^e. */
835
/* pow(b,e) Calculates the base to the exponent power, that is, b^e. */
835
 
836
 
836
value_type ff_pow(value_type b, value_type e) {
837
value_type ff_pow(value_type b, value_type e) {
837
#ifdef PARSERTEST
838
#ifdef PARSERTEST
838
        return 0;
839
        return 0;
839
#else
840
#else
840
        return (value_type)round(pow((double)b, (double)e));
841
        double a = pow((double)b, (double)e);
-
 
842
        if (a - floor(a) >= 0.5)
-
 
843
                return (value_type)(floor(a) + 1);
-
 
844
        else
-
 
845
                return (value_type)(floor(a));
841
#endif
846
#endif
842
}
847
}
843
 
848
 
844
// -------------------------------------------------------------------------------------------
849
// -------------------------------------------------------------------------------------------
845
 
850
 
846
/* sqr(x) Square root of x */
851
/* sqr(x) Square root of x */
847
 
852
 
848
static uint32_t isqrt(uint32_t x) {
853
static uint32_t isqrt(uint32_t x) {
849
#ifdef PARSERTEST
854
#ifdef PARSERTEST
850
        return 0;
855
        return 0;
851
#else
856
#else
852
        // based on https://gist.github.com/orlp/3481770
857
        // based on https://gist.github.com/orlp/3481770
853
 
858
 
854
        static uint32_t lkpSquares[65535];
859
        static uint32_t lkpSquares[65535];
855
        static int lkpInitialized = 0;
860
        static int lkpInitialized = 0;
856
        const uint32_t *p;
861
        const uint32_t *p;
857
        int i;
862
        int i;
858
 
863
 
859
        while (lkpInitialized == 1) { /* If other thread is currently creating the lookup table, then wait */ }
864
        while (lkpInitialized == 1) { /* If other thread is currently creating the lookup table, then wait */ }
860
        if (!lkpInitialized) {
865
        if (!lkpInitialized) {
861
                lkpInitialized = 1;
866
                lkpInitialized = 1;
862
                for (i = 0; i < 65535; ++i) {
867
                for (i = 0; i < 65535; ++i) {
863
                        lkpSquares[i] = i * i;
868
                        lkpSquares[i] = i * i;
864
                }
869
                }
865
                lkpInitialized = 2;
870
                lkpInitialized = 2;
866
        }
871
        }
867
 
872
 
868
        p = lkpSquares;
873
        p = lkpSquares;
869
 
874
 
870
        if (p[32768] <= x) p += 32768;
875
        if (p[32768] <= x) p += 32768;
871
        if (p[16384] <= x) p += 16384;
876
        if (p[16384] <= x) p += 16384;
872
        if (p[8192] <= x) p += 8192;
877
        if (p[8192] <= x) p += 8192;
873
        if (p[4096] <= x) p += 4096;
878
        if (p[4096] <= x) p += 4096;
874
        if (p[2048] <= x) p += 2048;
879
        if (p[2048] <= x) p += 2048;
875
        if (p[1024] <= x) p += 1024;
880
        if (p[1024] <= x) p += 1024;
876
        if (p[512] <= x) p += 512;
881
        if (p[512] <= x) p += 512;
877
        if (p[256] <= x) p += 256;
882
        if (p[256] <= x) p += 256;
878
        if (p[128] <= x) p += 128;
883
        if (p[128] <= x) p += 128;
879
        if (p[64] <= x) p += 64;
884
        if (p[64] <= x) p += 64;
880
        if (p[32] <= x) p += 32;
885
        if (p[32] <= x) p += 32;
881
        if (p[16] <= x) p += 16;
886
        if (p[16] <= x) p += 16;
882
        if (p[8] <= x) p += 8;
887
        if (p[8] <= x) p += 8;
883
        if (p[4] <= x) p += 4;
888
        if (p[4] <= x) p += 4;
884
        if (p[2] <= x) p += 2;
889
        if (p[2] <= x) p += 2;
885
        if (p[1] <= x) p += 1;
890
        if (p[1] <= x) p += 1;
886
 
891
 
887
        return (uint32_t)(p - lkpSquares);
892
        return (uint32_t)(p - lkpSquares);
888
#endif
893
#endif
889
}
894
}
890
 
895
 
891
value_type factory_sqr(value_type x) {
896
value_type factory_sqr(value_type x) {
892
#ifdef PARSERTEST
897
#ifdef PARSERTEST
893
        return 0;
898
        return 0;
894
#else
899
#else
895
        // https://misc.daniel-marschall.de/projects/filter_factory/function_sqr.html
900
        // https://misc.daniel-marschall.de/projects/filter_factory/function_sqr.html
896
 
901
 
897
        int eax, ebx, ecx;
902
        int eax, ebx, ecx;
898
 
903
 
899
        ebx = x;
904
        ebx = x;
900
        if (ebx > 1) {
905
        if (ebx > 1) {
901
                ecx = ebx;
906
                ecx = ebx;
902
                ebx = ebx >> 1;
907
                ebx = ebx >> 1;
903
                eax = 2;
908
                eax = 2;
904
                while (ebx > eax) {
909
                while (ebx > eax) {
905
                        eax = ecx;
910
                        eax = ecx;
906
                        eax /= ebx;
911
                        eax /= ebx;
907
                        ebx += eax;
912
                        ebx += eax;
908
                        ebx = ebx >> 1;
913
                        ebx = ebx >> 1;
909
                }
914
                }
910
        }
915
        }
911
        return ebx;
916
        return ebx;
912
#endif
917
#endif
913
}
918
}
914
 
919
 
915
value_type foundry_sqr(value_type x) {
920
value_type foundry_sqr(value_type x) {
916
#ifdef PARSERTEST
921
#ifdef PARSERTEST
917
        return 0;
922
        return 0;
918
#else
923
#else
919
        // Note: FilterFactory has sqr(x)=x if x<0 . Here we set sqr(x)=0 for x<0
924
        // Note: FilterFactory has sqr(x)=x if x<0 . Here we set sqr(x)=0 for x<0
920
        return x < 0 ? 0 : isqrt(x);
925
        return x < 0 ? 0 : isqrt(x);
921
#endif
926
#endif
922
}
927
}
923
 
928
 
924
value_type ff_sqr(value_type x){
929
value_type ff_sqr(value_type x){
925
#ifdef use_filterfactory_implementation_sqr
930
#ifdef use_filterfactory_implementation_sqr
926
        return factory_sqr(x);
931
        return factory_sqr(x);
927
#else
932
#else
928
        return foundry_sqr(x);
933
        return foundry_sqr(x);
929
#endif
934
#endif
930
}
935
}
931
 
936
 
932
// -------------------------------------------------------------------------------------------
937
// -------------------------------------------------------------------------------------------
933
 
938
 
934
/* cos(x) Cosine function of x, where x is an integer between 0 and
939
/* cos(x) Cosine function of x, where x is an integer between 0 and
935
   1024, inclusive, and the value returned is an integer
940
   1024, inclusive, and the value returned is an integer
936
   between -512 and 512, inclusive (Windows) or -1024 and
941
   between -512 and 512, inclusive (Windows) or -1024 and
937
   1024, inclusive (Mac OS) */
942
   1024, inclusive (Mac OS) */
938
 
943
 
939
value_type factory_cos(value_type x) {
944
value_type factory_cos(value_type x) {
940
#ifdef PARSERTEST
945
#ifdef PARSERTEST
941
        return 0;
946
        return 0;
942
#else
947
#else
943
        // https://misc.daniel-marschall.de/projects/filter_factory/function_cos.html
948
        // https://misc.daniel-marschall.de/projects/filter_factory/function_cos.html
944
        int res;
949
        int res;
945
        if (x < 0) x = -x;
950
        if (x < 0) x = -x;
946
        x &= 0x3ff; // 1023
951
        x &= 0x3ff; // 1023
947
        res = FACTORY_COS_LOOKUP[x];
952
        res = FACTORY_COS_LOOKUP[x];
948
        return res >= 0 ? (res / 32) : res / 32 - 1;
953
        return res >= 0 ? (res / 32) : res / 32 - 1;
949
#endif
954
#endif
950
}
955
}
951
 
956
 
952
value_type foundry_cos(value_type x) {
957
value_type foundry_cos(value_type x) {
953
#ifdef PARSERTEST
958
#ifdef PARSERTEST
954
        return 0;
959
        return 0;
955
#else
960
#else
956
        //return RINT(TRIGAMP*cos(FFANGLE(x)));
961
        //return RINT(TRIGAMP*cos(FFANGLE(x)));
957
        return (value_type)RINT(TRIGAMP * costab[abs(x) % COSTABSIZE]);
962
        return (value_type)RINT(TRIGAMP * costab[abs(x) % COSTABSIZE]);
958
#endif
963
#endif
959
}
964
}
960
 
965
 
961
value_type ff_cos(value_type x){
966
value_type ff_cos(value_type x){
962
#ifdef use_filterfactory_implementation_cos
967
#ifdef use_filterfactory_implementation_cos
963
        return factory_cos(x);
968
        return factory_cos(x);
964
#else
969
#else
965
        return foundry_cos(x);
970
        return foundry_cos(x);
966
#endif
971
#endif
967
}
972
}
968
 
973
 
969
// -------------------------------------------------------------------------------------------
974
// -------------------------------------------------------------------------------------------
970
 
975
 
971
/* sin(x) Sine function of x, where x is an integer between 0 and
976
/* sin(x) Sine function of x, where x is an integer between 0 and
972
   1024, inclusive, and the value returned is an integer
977
   1024, inclusive, and the value returned is an integer
973
   between -512 and 512, inclusive (Windows) or -1024 and
978
   between -512 and 512, inclusive (Windows) or -1024 and
974
   1024, inclusive (Mac OS) */
979
   1024, inclusive (Mac OS) */
975
 
980
 
976
value_type factory_sin(value_type x) {
981
value_type factory_sin(value_type x) {
977
#ifdef PARSERTEST
982
#ifdef PARSERTEST
978
        return 0;
983
        return 0;
979
#else
984
#else
980
        // https://misc.daniel-marschall.de/projects/filter_factory/function_sin.html
985
        // https://misc.daniel-marschall.de/projects/filter_factory/function_sin.html
981
        return factory_cos(x - 256);
986
        return factory_cos(x - 256);
982
#endif
987
#endif
983
}
988
}
984
 
989
 
985
value_type foundry_sin(value_type x) {
990
value_type foundry_sin(value_type x) {
986
#ifdef PARSERTEST
991
#ifdef PARSERTEST
987
        return 0;
992
        return 0;
988
#else
993
#else
989
        //return RINT(TRIGAMP*sin(FFANGLE(x)));
994
        //return RINT(TRIGAMP*sin(FFANGLE(x)));
990
        return foundry_cos(x - 256);
995
        return foundry_cos(x - 256);
991
#endif
996
#endif
992
}
997
}
993
 
998
 
994
value_type ff_sin(value_type x) {
999
value_type ff_sin(value_type x) {
995
#ifdef use_filterfactory_implementation_sin
1000
#ifdef use_filterfactory_implementation_sin
996
        return factory_sin(x);
1001
        return factory_sin(x);
997
#else
1002
#else
998
        return foundry_sin(x);
1003
        return foundry_sin(x);
999
#endif
1004
#endif
1000
}
1005
}
1001
 
1006
 
1002
// -------------------------------------------------------------------------------------------
1007
// -------------------------------------------------------------------------------------------
1003
 
1008
 
1004
/* tan(x) Tangent function of x, where x is an integer
1009
/* tan(x) Tangent function of x, where x is an integer
1005
   between -256 and 256, inclusive. Althought the Filter Factory manual
1010
   between -256 and 256, inclusive. Althought the Filter Factory manual
1006
   stated that the return value is bounded to -512 and 512, inclusive (Windows) or
1011
   stated that the return value is bounded to -512 and 512, inclusive (Windows) or
1007
   -1024 and 1024, inclusive (Mac OS), the output is actually NOT bounded! */
1012
   -1024 and 1024, inclusive (Mac OS), the output is actually NOT bounded! */
1008
 
1013
 
1009
value_type factory_tan(value_type x) {
1014
value_type factory_tan(value_type x) {
1010
#ifdef PARSERTEST
1015
#ifdef PARSERTEST
1011
        return 0;
1016
        return 0;
1012
#else
1017
#else
1013
        // https://misc.daniel-marschall.de/projects/filter_factory/function_tan.html
1018
        // https://misc.daniel-marschall.de/projects/filter_factory/function_tan.html
1014
        int v1 = x;
1019
        int v1 = x;
1015
        int v2 = v1 < 0 ? -v1 : v1;
1020
        int v2 = v1 < 0 ? -v1 : v1;
1016
        v2 &= 0x3ff; // 1023
1021
        v2 &= 0x3ff; // 1023
1017
        v2 = FACTORY_COS_LOOKUP[v2];
1022
        v2 = FACTORY_COS_LOOKUP[v2];
1018
        v1 -= 256;
1023
        v1 -= 256;
1019
        if (v1 < 0) v1 = -v1;
1024
        if (v1 < 0) v1 = -v1;
1020
        v1 &= 0x3ff; // 1023
1025
        v1 &= 0x3ff; // 1023
1021
        v1 = FACTORY_COS_LOOKUP[v1];
1026
        v1 = FACTORY_COS_LOOKUP[v1];
1022
        if (v2 == 0) return 0;
1027
        if (v2 == 0) return 0;
1023
        v1 = v1 << 10; // v1 *= 1024;
1028
        v1 = v1 << 10; // v1 *= 1024;
1024
        return v1 / v2;
1029
        return v1 / v2;
1025
#endif
1030
#endif
1026
}
1031
}
1027
 
1032
 
1028
value_type foundry_tan(value_type x){
1033
value_type foundry_tan(value_type x){
1029
#ifdef PARSERTEST
1034
#ifdef PARSERTEST
1030
        return 0;
1035
        return 0;
1031
#else
1036
#else
1032
        // Following filter shows that the Filter Factory manual differs from the implementation.
1037
        // Following filter shows that the Filter Factory manual differs from the implementation.
1033
        //     R = cos(x) > 1024 || cos(x) < -1024 || cos(-x) > 1024 || cos(-x) < -1024 ? 255 : 0
1038
        //     R = cos(x) > 1024 || cos(x) < -1024 || cos(-x) > 1024 || cos(-x) < -1024 ? 255 : 0
1034
        //     G = tan(x) > 1024 || tan(x) < -1024 || tan(-x) > 1024 || tan(-x) < -1024 ? 255 : 0
1039
        //     G = tan(x) > 1024 || tan(x) < -1024 || tan(-x) > 1024 || tan(-x) < -1024 ? 255 : 0
1035
        //     B = sin(x) > 1024 || sin(x) < -1024 || sin(-x) > 1024 || sin(-x) < -1024 ? 255 : 0
1040
        //     B = sin(x) > 1024 || sin(x) < -1024 || sin(-x) > 1024 || sin(-x) < -1024 ? 255 : 0
1036
        // It outputs green stripes, showing that the output of tan() is not bounded.
1041
        // It outputs green stripes, showing that the output of tan() is not bounded.
1037
        // So, we do it the same way to stay compatible.
1042
        // So, we do it the same way to stay compatible.
1038
        if (x < 0) x--; /* required for Filter Factory compatibility */
1043
        if (x < 0) x--; /* required for Filter Factory compatibility */
1039
        while (x < 0) x += TANTABSIZE;
1044
        while (x < 0) x += TANTABSIZE;
1040
        return (value_type)RINT(2*TRIGAMP*tantab[x % TANTABSIZE]); // We need the x2 multiplicator for some reason
1045
        return (value_type)RINT(2*TRIGAMP*tantab[x % TANTABSIZE]); // We need the x2 multiplicator for some reason
1041
#endif
1046
#endif
1042
}
1047
}
1043
 
1048
 
1044
value_type ff_tan(value_type x) {
1049
value_type ff_tan(value_type x) {
1045
#ifdef use_filterfactory_implementation_tan
1050
#ifdef use_filterfactory_implementation_tan
1046
        return factory_tan(x);
1051
        return factory_tan(x);
1047
#else
1052
#else
1048
        return foundry_tan(x);
1053
        return foundry_tan(x);
1049
#endif
1054
#endif
1050
}
1055
}
1051
 
1056
 
1052
// -------------------------------------------------------------------------------------------
1057
// -------------------------------------------------------------------------------------------
1053
 
1058
 
1054
/* c2d(x,y) Angle displacement of the pixel at coordinates x,y */
1059
/* c2d(x,y) Angle displacement of the pixel at coordinates x,y */
1055
 
1060
 
1056
value_type factory_c2d(value_type x, value_type y) {
1061
value_type factory_c2d(value_type x, value_type y) {
1057
#ifdef PARSERTEST
1062
#ifdef PARSERTEST
1058
        return 0;
1063
        return 0;
1059
#else
1064
#else
1060
        // https://misc.daniel-marschall.de/projects/filter_factory/function_c2d.html
1065
        // https://misc.daniel-marschall.de/projects/filter_factory/function_c2d.html
1061
        int32_t eax, ebx, ecx;
1066
        int32_t eax, ebx, ecx;
1062
        eax = y;
1067
        eax = y;
1063
        ebx = x;
1068
        ebx = x;
1064
        ecx = 0;
1069
        ecx = 0;
1065
        if (eax < 0) {
1070
        if (eax < 0) {
1066
                eax = -eax;
1071
                eax = -eax;
1067
                ecx |= 4/*0b0100*/;
1072
                ecx |= 4/*0b0100*/;
1068
        }
1073
        }
1069
        if (ebx < 0) {
1074
        if (ebx < 0) {
1070
                ebx = -ebx;
1075
                ebx = -ebx;
1071
                ecx |= 3/*0b0011*/;
1076
                ecx |= 3/*0b0011*/;
1072
        }
1077
        }
1073
        if (eax > ebx) {
1078
        if (eax > ebx) {
1074
                int tmp;
1079
                int tmp;
1075
                ecx ^= 1/*0b0001*/;
1080
                ecx ^= 1/*0b0001*/;
1076
                tmp = eax;
1081
                tmp = eax;
1077
                eax = ebx;
1082
                eax = ebx;
1078
                ebx = tmp;
1083
                ebx = tmp;
1079
        }
1084
        }
1080
        if (eax > 0) {
1085
        if (eax > 0) {
1081
                eax = eax << 10;
1086
                eax = eax << 10;
1082
                eax /= ebx;
1087
                eax /= ebx;
1083
                if (eax != 0) {
1088
                if (eax != 0) {
1084
                        eax = (eax & 0xFFFF0000) | (FACTORY_C2D_LOOKUP[eax - 1] & 0xFFFF);
1089
                        eax = (eax & 0xFFFF0000) | (FACTORY_C2D_LOOKUP[eax - 1] & 0xFFFF);
1085
                        eax = eax << 9;
1090
                        eax = eax << 9;
1086
                        ebx = 205888; // 205888/65536 == pi
1091
                        ebx = 205888; // 205888/65536 == pi
1087
                        eax /= ebx;
1092
                        eax /= ebx;
1088
                }
1093
                }
1089
        }
1094
        }
1090
        if ((ecx & 1/*0b0001*/) != 0) {
1095
        if ((ecx & 1/*0b0001*/) != 0) {
1091
                eax = -eax;
1096
                eax = -eax;
1092
                eax += 256;
1097
                eax += 256;
1093
        }
1098
        }
1094
        if ((ecx & 2/*0b0010*/) != 0) {
1099
        if ((ecx & 2/*0b0010*/) != 0) {
1095
                eax += 256;
1100
                eax += 256;
1096
        }
1101
        }
1097
        if ((ecx & 4/*0b0100*/) != 0) {
1102
        if ((ecx & 4/*0b0100*/) != 0) {
1098
                eax = -eax;
1103
                eax = -eax;
1099
        }
1104
        }
1100
        return eax;
1105
        return eax;
1101
#endif
1106
#endif
1102
}
1107
}
1103
 
1108
 
1104
value_type foundry_c2d(value_type x, value_type y) {
1109
value_type foundry_c2d(value_type x, value_type y) {
1105
#ifdef PARSERTEST
1110
#ifdef PARSERTEST
1106
        return 0;
1111
        return 0;
1107
#else
1112
#else
1108
        // Behavior of FilterFoundry <1.7:
1113
        // Behavior of FilterFoundry <1.7:
1109
        //return foundry_c2d_negated(x,y);
1114
        //return foundry_c2d_negated(x,y);
1110
 
1115
 
1111
        // Behavior in FilterFoundry 1.7+: Matches FilterFactory
1116
        // Behavior in FilterFoundry 1.7+: Matches FilterFactory
1112
        return (value_type)RINT(TO_FFANGLE(atan2(y, x)));
1117
        return (value_type)RINT(TO_FFANGLE(atan2(y, x)));
1113
#endif
1118
#endif
1114
}
1119
}
1115
 
1120
 
1116
value_type ff_c2d(value_type x, value_type y) {
1121
value_type ff_c2d(value_type x, value_type y) {
1117
#ifdef use_filterfactory_implementation_c2d
1122
#ifdef use_filterfactory_implementation_c2d
1118
        return factory_c2d(x, y);
1123
        return factory_c2d(x, y);
1119
#else
1124
#else
1120
        return foundry_c2d(x, y);
1125
        return foundry_c2d(x, y);
1121
#endif
1126
#endif
1122
}
1127
}
1123
 
1128
 
1124
// -------------------------------------------------------------------------------------------
1129
// -------------------------------------------------------------------------------------------
1125
 
1130
 
1126
/* c2m(x,y) Magnitude displacement of the pixel at coordinates x,y */
1131
/* c2m(x,y) Magnitude displacement of the pixel at coordinates x,y */
1127
 
1132
 
1128
value_type factory_c2m(value_type x, value_type y) {
1133
value_type factory_c2m(value_type x, value_type y) {
1129
#ifdef PARSERTEST
1134
#ifdef PARSERTEST
1130
        return 0;
1135
        return 0;
1131
#else
1136
#else
1132
        // https://misc.daniel-marschall.de/projects/filter_factory/function_c2m.html
1137
        // https://misc.daniel-marschall.de/projects/filter_factory/function_c2m.html
1133
        int32_t eax, ebx;
1138
        int32_t eax, ebx;
1134
        ebx = y < 0 ? -y : y;
1139
        ebx = y < 0 ? -y : y;
1135
        eax = x < 0 ? -x : x;
1140
        eax = x < 0 ? -x : x;
1136
        if (eax == ebx) {
1141
        if (eax == ebx) {
1137
                eax = 27146; // 27146/65536 == sqrt(1)-1
1142
                eax = 27146; // 27146/65536 == sqrt(1)-1
1138
        }
1143
        }
1139
        else {
1144
        else {
1140
                if (eax > ebx) {
1145
                if (eax > ebx) {
1141
                        int tmp = eax;
1146
                        int tmp = eax;
1142
                        eax = ebx;
1147
                        eax = ebx;
1143
                        ebx = tmp;
1148
                        ebx = tmp;
1144
                }
1149
                }
1145
                eax = eax << 10;
1150
                eax = eax << 10;
1146
                eax /= ebx;
1151
                eax /= ebx;
1147
                eax = FACTORY_C2M_LOOKUP[eax];
1152
                eax = FACTORY_C2M_LOOKUP[eax];
1148
        }
1153
        }
1149
        eax = ((int64_t)eax * (int64_t)ebx) >> 16;
1154
        eax = ((int64_t)eax * (int64_t)ebx) >> 16;
1150
        eax += ebx;
1155
        eax += ebx;
1151
        return eax;
1156
        return eax;
1152
#endif
1157
#endif
1153
}
1158
}
1154
 
1159
 
1155
value_type foundry_c2m(value_type x, value_type y) {
1160
value_type foundry_c2m(value_type x, value_type y) {
1156
#ifdef PARSERTEST
1161
#ifdef PARSERTEST
1157
        return 0;
1162
        return 0;
1158
#else
1163
#else
1159
        return isqrt((long)x * x + (long)y * y);
1164
        return isqrt((long)x * x + (long)y * y);
1160
#endif
1165
#endif
1161
}
1166
}
1162
 
1167
 
1163
value_type ff_c2m(value_type x, value_type y) {
1168
value_type ff_c2m(value_type x, value_type y) {
1164
#ifdef use_filterfactory_implementation_c2m
1169
#ifdef use_filterfactory_implementation_c2m
1165
        return factory_c2m(x, y);
1170
        return factory_c2m(x, y);
1166
#else
1171
#else
1167
        return foundry_c2m(x, y);
1172
        return foundry_c2m(x, y);
1168
#endif
1173
#endif
1169
}
1174
}
1170
 
1175
 
1171
// -------------------------------------------------------------------------------------------
1176
// -------------------------------------------------------------------------------------------
1172
 
1177
 
1173
/* Range of angles within the image, where D is always 1024 */
1178
/* Range of angles within the image, where D is always 1024 */
1174
 
1179
 
1175
value_type ff_D() {
1180
value_type ff_D() {
1176
        return 1024;
1181
        return 1024;
1177
}
1182
}
1178
 
1183
 
1179
// -------------------------------------------------------------------------------------------
1184
// -------------------------------------------------------------------------------------------
1180
 
1185
 
1181
/* Direction(angle) of the current pixel from the center of the image,
1186
/* Direction(angle) of the current pixel from the center of the image,
1182
   where d is an integer between 0 and 1024 inclusive */
1187
   where d is an integer between 0 and 1024 inclusive */
1183
 
1188
 
1184
value_type factory_d() {
1189
value_type factory_d() {
1185
#ifdef PARSERTEST
1190
#ifdef PARSERTEST
1186
        return 0;
1191
        return 0;
1187
#else
1192
#else
1188
        // https://misc.daniel-marschall.de/projects/filter_factory/symbol_d_lowercase.html
1193
        // https://misc.daniel-marschall.de/projects/filter_factory/symbol_d_lowercase.html
1189
 
1194
 
1190
        int eax, ebx, ecx;
1195
        int eax, ebx, ecx;
1191
        const int xmin = 0, ymin = 0;
1196
        const int xmin = 0, ymin = 0;
1192
 
1197
 
1193
        eax = -(var['Y'] - ymin) / 2;
1198
        eax = -(var['Y'] - ymin) / 2;
1194
        ebx = -(var['X'] - xmin) / 2;
1199
        ebx = -(var['X'] - xmin) / 2;
1195
        ecx = 0;
1200
        ecx = 0;
1196
        eax += var['y'];
1201
        eax += var['y'];
1197
        if (eax < 0) {
1202
        if (eax < 0) {
1198
                eax = -eax;
1203
                eax = -eax;
1199
                ecx |= 4/*0b0100*/;
1204
                ecx |= 4/*0b0100*/;
1200
        }
1205
        }
1201
        ebx += var['x'];
1206
        ebx += var['x'];
1202
        if (ebx < 0) {
1207
        if (ebx < 0) {
1203
                ebx = -ebx;
1208
                ebx = -ebx;
1204
                ecx |= 3/*0b0011*/;
1209
                ecx |= 3/*0b0011*/;
1205
        }
1210
        }
1206
        if (eax > ebx) {
1211
        if (eax > ebx) {
1207
                int tmp;
1212
                int tmp;
1208
                ecx ^= 1/*0b0001*/;
1213
                ecx ^= 1/*0b0001*/;
1209
                tmp = eax;
1214
                tmp = eax;
1210
                eax = ebx;
1215
                eax = ebx;
1211
                ebx = tmp;
1216
                ebx = tmp;
1212
        }
1217
        }
1213
        if (eax > 0) {
1218
        if (eax > 0) {
1214
                eax = eax << 10;
1219
                eax = eax << 10;
1215
                eax /= ebx;
1220
                eax /= ebx;
1216
                if (eax != 0) { // C2D_LOOKUP[-1] will never be called. Good!
1221
                if (eax != 0) { // C2D_LOOKUP[-1] will never be called. Good!
1217
                        eax = (eax & 0xFFFF0000) + (FACTORY_C2D_LOOKUP[eax - 1] & 0xFFFF);
1222
                        eax = (eax & 0xFFFF0000) + (FACTORY_C2D_LOOKUP[eax - 1] & 0xFFFF);
1218
                        eax = eax << 9;
1223
                        eax = eax << 9;
1219
                        ebx = 205888; // 205888/65536 == pi
1224
                        ebx = 205888; // 205888/65536 == pi
1220
                        eax /= ebx;
1225
                        eax /= ebx;
1221
                }
1226
                }
1222
        }
1227
        }
1223
        if ((ecx & 1/*0b0001*/) != 0) {
1228
        if ((ecx & 1/*0b0001*/) != 0) {
1224
                eax = -eax;
1229
                eax = -eax;
1225
                eax += 256;
1230
                eax += 256;
1226
        }
1231
        }
1227
        if ((ecx & 2/*0b0010*/) != 0) {
1232
        if ((ecx & 2/*0b0010*/) != 0) {
1228
                eax += 256;
1233
                eax += 256;
1229
        }
1234
        }
1230
        if ((ecx & 4/*0b0100*/) != 0) {
1235
        if ((ecx & 4/*0b0100*/) != 0) {
1231
                eax = -eax;
1236
                eax = -eax;
1232
        }
1237
        }
1233
        return eax;
1238
        return eax;
1234
#endif
1239
#endif
1235
}
1240
}
1236
 
1241
 
1237
value_type foundry_c2d_negated(int x, int y) {
1242
value_type foundry_c2d_negated(int x, int y) {
1238
#ifdef PARSERTEST
1243
#ifdef PARSERTEST
1239
        return 0;
1244
        return 0;
1240
#else
1245
#else
1241
        return (value_type)RINT(TO_FFANGLE(atan2(-y, -x)));
1246
        return (value_type)RINT(TO_FFANGLE(atan2(-y, -x)));
1242
#endif
1247
#endif
1243
}
1248
}
1244
 
1249
 
1245
value_type foundry_d() {
1250
value_type foundry_d() {
1246
#ifdef PARSERTEST
1251
#ifdef PARSERTEST
1247
        return 0;
1252
        return 0;
1248
#else
1253
#else
1249
        // NOTE: FilterFactory uses c2d(x,y):=atan2(y,x), but d:=atan2(-y,-x)
1254
        // NOTE: FilterFactory uses c2d(x,y):=atan2(y,x), but d:=atan2(-y,-x)
1250
        // Due to compatibility reasons, we implement it the same way!
1255
        // Due to compatibility reasons, we implement it the same way!
1251
        // Sign of y difference is negated, as we are dealing with top-down coordinates angle is "observed"
1256
        // Sign of y difference is negated, as we are dealing with top-down coordinates angle is "observed"
1252
        int x = var['X'] / 2 - var['x'];
1257
        int x = var['X'] / 2 - var['x'];
1253
        int y = var['Y'] / 2 - var['y'];
1258
        int y = var['Y'] / 2 - var['y'];
1254
        return foundry_c2d_negated(x, y);
1259
        return foundry_c2d_negated(x, y);
1255
#endif
1260
#endif
1256
}
1261
}
1257
 
1262
 
1258
value_type ff_d() {
1263
value_type ff_d() {
1259
#ifdef use_filterfactory_implementation_d
1264
#ifdef use_filterfactory_implementation_d
1260
        return factory_d();
1265
        return factory_d();
1261
#else
1266
#else
1262
        return foundry_d();
1267
        return foundry_d();
1263
#endif
1268
#endif
1264
}
1269
}
1265
 
1270
 
1266
// -------------------------------------------------------------------------------------------
1271
// -------------------------------------------------------------------------------------------
1267
 
1272
 
1268
/* Range of magnitudes with the image, where M is one half the diagonal size of the image */
1273
/* Range of magnitudes with the image, where M is one half the diagonal size of the image */
1269
 
1274
 
1270
value_type factory_M() {
1275
value_type factory_M() {
1271
#ifdef PARSERTEST
1276
#ifdef PARSERTEST
1272
        return 0;
1277
        return 0;
1273
#else
1278
#else
1274
        // https://misc.daniel-marschall.de/projects/filter_factory/symbol_m_uppercase.html
1279
        // https://misc.daniel-marschall.de/projects/filter_factory/symbol_m_uppercase.html
1275
 
1280
 
1276
        int eax, ebx;
1281
        int eax, ebx;
1277
        const int xmin = 0, ymin = 0;
1282
        const int xmin = 0, ymin = 0;
1278
 
1283
 
1279
        eax = (var['X'] - xmin) >> 1;
1284
        eax = (var['X'] - xmin) >> 1;
1280
        ebx = (var['Y'] - ymin) >> 1;
1285
        ebx = (var['Y'] - ymin) >> 1;
1281
        if (eax == ebx) {
1286
        if (eax == ebx) {
1282
                eax = 27146; // 27146/65536 == sqrt(1)-1
1287
                eax = 27146; // 27146/65536 == sqrt(1)-1
1283
        }
1288
        }
1284
        else {
1289
        else {
1285
                if (eax > ebx) {
1290
                if (eax > ebx) {
1286
                        int tmp = eax;
1291
                        int tmp = eax;
1287
                        eax = ebx;
1292
                        eax = ebx;
1288
                        ebx = tmp;
1293
                        ebx = tmp;
1289
                }
1294
                }
1290
                eax = eax << 10;
1295
                eax = eax << 10;
1291
                eax /= ebx;
1296
                eax /= ebx;
1292
                eax = FACTORY_C2M_LOOKUP[eax];
1297
                eax = FACTORY_C2M_LOOKUP[eax];
1293
        }
1298
        }
1294
        eax = ((int64_t)eax * (int64_t)ebx) >> 16;
1299
        eax = ((int64_t)eax * (int64_t)ebx) >> 16;
1295
        eax += ebx;
1300
        eax += ebx;
1296
        return eax;
1301
        return eax;
1297
#endif
1302
#endif
1298
}
1303
}
1299
 
1304
 
1300
value_type foundry_M() {
1305
value_type foundry_M() {
1301
#ifdef PARSERTEST
1306
#ifdef PARSERTEST
1302
        return 0;
1307
        return 0;
1303
#else
1308
#else
1304
        return foundry_c2m(var['X'], var['Y']) / 2;
1309
        return foundry_c2m(var['X'], var['Y']) / 2;
1305
#endif
1310
#endif
1306
}
1311
}
1307
 
1312
 
1308
value_type ff_M() {
1313
value_type ff_M() {
1309
#ifdef use_filterfactory_implementation_M
1314
#ifdef use_filterfactory_implementation_M
1310
        return factory_M();
1315
        return factory_M();
1311
#else
1316
#else
1312
        return foundry_M();
1317
        return foundry_M();
1313
#endif
1318
#endif
1314
}
1319
}
1315
 
1320
 
1316
// -------------------------------------------------------------------------------------------
1321
// -------------------------------------------------------------------------------------------
1317
 
1322
 
1318
/* Distance (magnitude) from the center of the image to the current pixel */
1323
/* Distance (magnitude) from the center of the image to the current pixel */
1319
 
1324
 
1320
value_type factory_m() {
1325
value_type factory_m() {
1321
#ifdef PARSERTEST
1326
#ifdef PARSERTEST
1322
        return 0;
1327
        return 0;
1323
#else
1328
#else
1324
        // https://misc.daniel-marschall.de/projects/filter_factory/symbol_m_lowercase.html
1329
        // https://misc.daniel-marschall.de/projects/filter_factory/symbol_m_lowercase.html
1325
 
1330
 
1326
        int eax, ebx;
1331
        int eax, ebx;
1327
        const int xmin = 0, ymin = 0;
1332
        const int xmin = 0, ymin = 0;
1328
 
1333
 
1329
        eax = ((xmin - var['X']) >> 1) + var['x'];
1334
        eax = ((xmin - var['X']) >> 1) + var['x'];
1330
        ebx = ((ymin - var['Y']) >> 1) + var['y'];
1335
        ebx = ((ymin - var['Y']) >> 1) + var['y'];
1331
        eax = eax < 0 ? -eax : eax;
1336
        eax = eax < 0 ? -eax : eax;
1332
        ebx = ebx < 0 ? -ebx : ebx;
1337
        ebx = ebx < 0 ? -ebx : ebx;
1333
 
1338
 
1334
        if (eax == ebx) {
1339
        if (eax == ebx) {
1335
                eax = 27146; // 27146/65536 == sqrt(1)-1
1340
                eax = 27146; // 27146/65536 == sqrt(1)-1
1336
        }
1341
        }
1337
        else {
1342
        else {
1338
                if (eax > ebx) {
1343
                if (eax > ebx) {
1339
                        int tmp = eax;
1344
                        int tmp = eax;
1340
                        eax = ebx;
1345
                        eax = ebx;
1341
                        ebx = tmp;
1346
                        ebx = tmp;
1342
                }
1347
                }
1343
                eax = FACTORY_C2M_LOOKUP[1024 * eax / ebx];
1348
                eax = FACTORY_C2M_LOOKUP[1024 * eax / ebx];
1344
        }
1349
        }
1345
        eax = ((int64_t)eax * (int64_t)ebx) >> 16;
1350
        eax = ((int64_t)eax * (int64_t)ebx) >> 16;
1346
        eax += ebx;
1351
        eax += ebx;
1347
        return eax;
1352
        return eax;
1348
#endif
1353
#endif
1349
}
1354
}
1350
 
1355
 
1351
value_type foundry_m() {
1356
value_type foundry_m() {
1352
#ifdef PARSERTEST
1357
#ifdef PARSERTEST
1353
        return 0;
1358
        return 0;
1354
#else
1359
#else
1355
        return foundry_c2m(var['X'] / 2 - var['x'], var['Y'] / 2 - var['y']);
1360
        return foundry_c2m(var['X'] / 2 - var['x'], var['Y'] / 2 - var['y']);
1356
#endif
1361
#endif
1357
}
1362
}
1358
 
1363
 
1359
value_type ff_m() {
1364
value_type ff_m() {
1360
#ifdef use_filterfactory_implementation_m
1365
#ifdef use_filterfactory_implementation_m
1361
        return factory_m();
1366
        return factory_m();
1362
#else
1367
#else
1363
        return foundry_m();
1368
        return foundry_m();
1364
#endif
1369
#endif
1365
}
1370
}
1366
 
1371
 
1367
// -------------------------------------------------------------------------------------------
1372
// -------------------------------------------------------------------------------------------
1368
 
1373
 
1369
/* "Y" value of the YUV color-space */
1374
/* "Y" value of the YUV color-space */
1370
 
1375
 
1371
value_type factory_i() {
1376
value_type factory_i() {
1372
#ifdef PARSERTEST
1377
#ifdef PARSERTEST
1373
        return 0;
1378
        return 0;
1374
#else
1379
#else
1375
        return ((76L * var['r']) + (150L * var['g']) + (29L * var['b'])) / 256; // range: [0..254]
1380
        return ((76L * var['r']) + (150L * var['g']) + (29L * var['b'])) / 256; // range: [0..254]
1376
#endif
1381
#endif
1377
}
1382
}
1378
 
1383
 
1379
value_type foundry_i() {
1384
value_type foundry_i() {
1380
#ifdef PARSERTEST
1385
#ifdef PARSERTEST
1381
        return 0;
1386
        return 0;
1382
#else
1387
#else
1383
        // These formulas are more accurate, e.g. pure white has now i=255 instead of 254
1388
        // These formulas are more accurate, e.g. pure white has now i=255 instead of 254
1384
        return ((299L * var['r']) + (587L * var['g']) + (114L * var['b'])) / 1000;    // range: [0..255]
1389
        return ((299L * var['r']) + (587L * var['g']) + (114L * var['b'])) / 1000;    // range: [0..255]
1385
#endif
1390
#endif
1386
}
1391
}
1387
 
1392
 
1388
value_type ff_i() {
1393
value_type ff_i() {
1389
#ifdef use_filterfactory_implementation_i
1394
#ifdef use_filterfactory_implementation_i
1390
        return factory_i();
1395
        return factory_i();
1391
#else
1396
#else
1392
        return foundry_i();
1397
        return foundry_i();
1393
#endif
1398
#endif
1394
}
1399
}
1395
 
1400
 
1396
// -------------------------------------------------------------------------------------------
1401
// -------------------------------------------------------------------------------------------
1397
 
1402
 
1398
/* "U" value of the YUV color-space */
1403
/* "U" value of the YUV color-space */
1399
 
1404
 
1400
value_type factory_u() {
1405
value_type factory_u() {
1401
#ifdef PARSERTEST
1406
#ifdef PARSERTEST
1402
        return 0;
1407
        return 0;
1403
#else
1408
#else
1404
        return ((-19L * var['r']) + (-37L * var['g']) + (56L * var['b'])) / 256; // range: [-55..55]
1409
        return ((-19L * var['r']) + (-37L * var['g']) + (56L * var['b'])) / 256; // range: [-55..55]
1405
#endif
1410
#endif
1406
}
1411
}
1407
 
1412
 
1408
value_type foundry_u() {
1413
value_type foundry_u() {
1409
#ifdef PARSERTEST
1414
#ifdef PARSERTEST
1410
        return 0;
1415
        return 0;
1411
#else
1416
#else
1412
        // These formulas are more accurate, e.g. pure white has now i=255 instead of 254
1417
        // These formulas are more accurate, e.g. pure white has now i=255 instead of 254
1413
        return ((-147407L * var['r']) + (-289391L * var['g']) + (436798L * var['b'])) / 2000000; // range: [-55..55]
1418
        return ((-147407L * var['r']) + (-289391L * var['g']) + (436798L * var['b'])) / 2000000; // range: [-55..55]
1414
#endif
1419
#endif
1415
}
1420
}
1416
 
1421
 
1417
value_type ff_u() {
1422
value_type ff_u() {
1418
#ifdef use_filterfactory_implementation_u
1423
#ifdef use_filterfactory_implementation_u
1419
        return factory_u();
1424
        return factory_u();
1420
#else
1425
#else
1421
        return foundry_u();
1426
        return foundry_u();
1422
#endif
1427
#endif
1423
}
1428
}
1424
 
1429
 
1425
// -------------------------------------------------------------------------------------------
1430
// -------------------------------------------------------------------------------------------
1426
 
1431
 
1427
/* "V" value of the YUV color-space */
1432
/* "V" value of the YUV color-space */
1428
 
1433
 
1429
value_type factory_v() {
1434
value_type factory_v() {
1430
#ifdef PARSERTEST
1435
#ifdef PARSERTEST
1431
        return 0;
1436
        return 0;
1432
#else
1437
#else
1433
        return ((78L * var['r']) + (-65L * var['g']) + (-13L * var['b'])) / 256; // range: [-77..77]
1438
        return ((78L * var['r']) + (-65L * var['g']) + (-13L * var['b'])) / 256; // range: [-77..77]
1434
#endif
1439
#endif
1435
}
1440
}
1436
 
1441
 
1437
value_type foundry_v() {
1442
value_type foundry_v() {
1438
#ifdef PARSERTEST
1443
#ifdef PARSERTEST
1439
        return 0;
1444
        return 0;
1440
#else
1445
#else
1441
        // These formulas are more accurate, e.g. pure white has now i=255 instead of 254
1446
        // These formulas are more accurate, e.g. pure white has now i=255 instead of 254
1442
        return ((614777L * var['r']) + (-514799L * var['g']) + (-99978L * var['b'])) / 2000000; // range: [-78..78]
1447
        return ((614777L * var['r']) + (-514799L * var['g']) + (-99978L * var['b'])) / 2000000; // range: [-78..78]
1443
#endif
1448
#endif
1444
}
1449
}
1445
 
1450
 
1446
value_type ff_v() {
1451
value_type ff_v() {
1447
#ifdef use_filterfactory_implementation_v
1452
#ifdef use_filterfactory_implementation_v
1448
        return factory_v();
1453
        return factory_v();
1449
#else
1454
#else
1450
        return foundry_v();
1455
        return foundry_v();
1451
#endif
1456
#endif
1452
}
1457
}
1453
 
1458
 
1454
// -------------------------------------------------------------------------------------------
1459
// -------------------------------------------------------------------------------------------
1455
 
1460
 
1456
/* get(i) Returns the current cell value at i */
1461
/* get(i) Returns the current cell value at i */
1457
 
1462
 
1458
value_type factory_get(value_type i) {
1463
value_type factory_get(value_type i) {
1459
#ifdef PARSERTEST
1464
#ifdef PARSERTEST
1460
        return 0;
1465
        return 0;
1461
#else
1466
#else
1462
        return i>=0 && i<NUM_CELLS ? cell[i] : i;
1467
        return i>=0 && i<NUM_CELLS ? cell[i] : i;
1463
#endif
1468
#endif
1464
}
1469
}
1465
 
1470
 
1466
value_type foundry_get(value_type i) {
1471
value_type foundry_get(value_type i) {
1467
#ifdef PARSERTEST
1472
#ifdef PARSERTEST
1468
        return 0;
1473
        return 0;
1469
#else
1474
#else
1470
        return i >= 0 && i < NUM_CELLS ? cell[i] : 0;
1475
        return i >= 0 && i < NUM_CELLS ? cell[i] : 0;
1471
#endif
1476
#endif
1472
}
1477
}
1473
 
1478
 
1474
value_type ff_get(value_type i) {
1479
value_type ff_get(value_type i) {
1475
#ifdef use_filterfactory_implementation_get
1480
#ifdef use_filterfactory_implementation_get
1476
        return factory_get(i);
1481
        return factory_get(i);
1477
#else
1482
#else
1478
        return foundry_get(i);
1483
        return foundry_get(i);
1479
#endif
1484
#endif
1480
}
1485
}
1481
 
1486
 
1482
// -------------------------------------------------------------------------------------------
1487
// -------------------------------------------------------------------------------------------
1483
 
1488
 
1484
/* put(v,i) Puts the new value v into cell i */
1489
/* put(v,i) Puts the new value v into cell i */
1485
value_type ff_put(value_type v,value_type i){
1490
value_type ff_put(value_type v,value_type i){
1486
#ifdef PARSERTEST
1491
#ifdef PARSERTEST
1487
        return 0;
1492
        return 0;
1488
#else
1493
#else
1489
        if(i>=0 && i<NUM_CELLS)
1494
        if(i>=0 && i<NUM_CELLS)
1490
                cell[i] = v;
1495
                cell[i] = v;
1491
        return v;
1496
        return v;
1492
#endif
1497
#endif
1493
}
1498
}
1494
 
1499
 
1495
// -------------------------------------------------------------------------------------------
1500
// -------------------------------------------------------------------------------------------
1496
 
1501
 
1497
/* Convolve. Applies a convolution matrix and divides with d. */
1502
/* Convolve. Applies a convolution matrix and divides with d. */
1498
value_type ff_cnv(value_type m11,value_type m12,value_type m13,
1503
value_type ff_cnv(value_type m11,value_type m12,value_type m13,
1499
                  value_type m21,value_type m22,value_type m23,
1504
                  value_type m21,value_type m22,value_type m23,
1500
                  value_type m31,value_type m32,value_type m33,
1505
                  value_type m31,value_type m32,value_type m33,
1501
                  value_type d)
1506
                  value_type d)
1502
{
1507
{
1503
#ifdef PARSERTEST
1508
#ifdef PARSERTEST
1504
        return 0;
1509
        return 0;
1505
#else
1510
#else
1506
        long total;
1511
        long total;
1507
        int x, y, z;
1512
        int x, y, z;
1508
        // shift x,y from selection-relative to image relative
1513
        // shift x,y from selection-relative to image relative
1509
        if (HAS_BIG_DOC(gpb)) {
1514
        if (HAS_BIG_DOC(gpb)) {
1510
                x = var['x'] + BIGDOC_FILTER_RECT(gpb).left;
1515
                x = var['x'] + BIGDOC_FILTER_RECT(gpb).left;
1511
                y = var['y'] + BIGDOC_FILTER_RECT(gpb).top;
1516
                y = var['y'] + BIGDOC_FILTER_RECT(gpb).top;
1512
        } else {
1517
        } else {
1513
                x = var['x'] + FILTER_RECT(gpb).left;
1518
                x = var['x'] + FILTER_RECT(gpb).left;
1514
                y = var['y'] + FILTER_RECT(gpb).top;
1519
                y = var['y'] + FILTER_RECT(gpb).top;
1515
        }
1520
        }
1516
        z = var['z'];
1521
        z = var['z'];
1517
 
1522
 
1518
        if(z >= 0 && z < var['Z'])
1523
        if(z >= 0 && z < var['Z'])
1519
                total = m11*rawsrc(x-1,y-1,z) + m12*rawsrc(x,y-1,z) + m13*rawsrc(x+1,y-1,z)
1524
                total = m11*rawsrc(x-1,y-1,z) + m12*rawsrc(x,y-1,z) + m13*rawsrc(x+1,y-1,z)
1520
                          + m21*rawsrc(x-1,y,  z) + m22*rawsrc(x,y,  z) + m23*rawsrc(x+1,y,  z)
1525
                          + m21*rawsrc(x-1,y,  z) + m22*rawsrc(x,y,  z) + m23*rawsrc(x+1,y,  z)
1521
                          + m31*rawsrc(x-1,y+1,z) + m32*rawsrc(x,y+1,z) + m33*rawsrc(x+1,y+1,z);
1526
                          + m31*rawsrc(x-1,y+1,z) + m32*rawsrc(x,y+1,z) + m33*rawsrc(x+1,y+1,z);
1522
        else
1527
        else
1523
                total = 0;
1528
                total = 0;
1524
 
1529
 
1525
        return d ? total/d : 0;
1530
        return d ? total/d : 0;
1526
#endif
1531
#endif
1527
}
1532
}
1528
 
1533
 
1529
// -------------------------------------------------------------------------------------------
1534
// -------------------------------------------------------------------------------------------
1530
 
1535
 
1531
value_type zero_val = 0;
1536
value_type zero_val = 0;
1532
value_type one_val = 1;
1537
value_type one_val = 1;
1533
value_type max_channel_val = 255;
1538
value_type max_channel_val = 255;
1534
 
1539
 
1535
/* predefined symbols */
1540
/* predefined symbols */
1536
struct sym_rec predefs[]={
1541
struct sym_rec predefs[]={
1537
        /* functions */
1542
        /* functions */
1538
 
1543
 
1539
        {0,TOK_FN3,"src", (pfunc_type)ff_src, 0},
1544
        {0,TOK_FN3,"src", (pfunc_type)ff_src, 0},
1540
        {0,TOK_FN3,"rad", (pfunc_type)ff_rad, 0},
1545
        {0,TOK_FN3,"rad", (pfunc_type)ff_rad, 0},
1541
        {0,TOK_FN1,"ctl", (pfunc_type)ff_ctl, 0},
1546
        {0,TOK_FN1,"ctl", (pfunc_type)ff_ctl, 0},
1542
        {0,TOK_FN3,"val", (pfunc_type)ff_val, 0},
1547
        {0,TOK_FN3,"val", (pfunc_type)ff_val, 0},
1543
        {0,TOK_FN2,"map", (pfunc_type)ff_map, 0},
1548
        {0,TOK_FN2,"map", (pfunc_type)ff_map, 0},
1544
        {0,TOK_FN2,"min", (pfunc_type)ff_min, 0},
1549
        {0,TOK_FN2,"min", (pfunc_type)ff_min, 0},
1545
        {0,TOK_FN2,"max", (pfunc_type)ff_max, 0},
1550
        {0,TOK_FN2,"max", (pfunc_type)ff_max, 0},
1546
        {0,TOK_FN1,"abs", (pfunc_type)ff_abs, 0},
1551
        {0,TOK_FN1,"abs", (pfunc_type)ff_abs, 0},
1547
        {0,TOK_FN3,"add", (pfunc_type)ff_add, 0},
1552
        {0,TOK_FN3,"add", (pfunc_type)ff_add, 0},
1548
        {0,TOK_FN3,"sub", (pfunc_type)ff_sub, 0},
1553
        {0,TOK_FN3,"sub", (pfunc_type)ff_sub, 0},
1549
        {0,TOK_FN2,"dif", (pfunc_type)ff_dif, 0},
1554
        {0,TOK_FN2,"dif", (pfunc_type)ff_dif, 0},
1550
        {0,TOK_FN2,"rnd", (pfunc_type)ff_rnd, 0},
1555
        {0,TOK_FN2,"rnd", (pfunc_type)ff_rnd, 0},
1551
        {0,TOK_FN4,"mix", (pfunc_type)ff_mix, 0},
1556
        {0,TOK_FN4,"mix", (pfunc_type)ff_mix, 0},
1552
        {0,TOK_FN5,"scl", (pfunc_type)ff_scl, 0},
1557
        {0,TOK_FN5,"scl", (pfunc_type)ff_scl, 0},
1553
        {0,TOK_FN1,"sqr", (pfunc_type)ff_sqr, 0},
1558
        {0,TOK_FN1,"sqr", (pfunc_type)ff_sqr, 0},
1554
        {0,TOK_FN1,"sqrt", (pfunc_type)ff_sqr, 0}, // sqrt() is synonym to sqr() in Premiere
1559
        {0,TOK_FN1,"sqrt", (pfunc_type)ff_sqr, 0}, // sqrt() is synonym to sqr() in Premiere
1555
        {0,TOK_FN1,"sin", (pfunc_type)ff_sin, 0},
1560
        {0,TOK_FN1,"sin", (pfunc_type)ff_sin, 0},
1556
        {0,TOK_FN1,"cos", (pfunc_type)ff_cos, 0},
1561
        {0,TOK_FN1,"cos", (pfunc_type)ff_cos, 0},
1557
        {0,TOK_FN1,"tan", (pfunc_type)ff_tan, 0},
1562
        {0,TOK_FN1,"tan", (pfunc_type)ff_tan, 0},
1558
        {0,TOK_FN2,"r2x", (pfunc_type)ff_r2x, 0},
1563
        {0,TOK_FN2,"r2x", (pfunc_type)ff_r2x, 0},
1559
        {0,TOK_FN2,"r2y", (pfunc_type)ff_r2y, 0},
1564
        {0,TOK_FN2,"r2y", (pfunc_type)ff_r2y, 0},
1560
        {0,TOK_FN2,"c2d", (pfunc_type)ff_c2d, 0},
1565
        {0,TOK_FN2,"c2d", (pfunc_type)ff_c2d, 0},
1561
        {0,TOK_FN2,"c2m", (pfunc_type)ff_c2m, 0},
1566
        {0,TOK_FN2,"c2m", (pfunc_type)ff_c2m, 0},
1562
        {0,TOK_FN1,"get", (pfunc_type)ff_get, 0},
1567
        {0,TOK_FN1,"get", (pfunc_type)ff_get, 0},
1563
        {0,TOK_FN2,"put", (pfunc_type)ff_put, 0},
1568
        {0,TOK_FN2,"put", (pfunc_type)ff_put, 0},
1564
        {0,TOK_FN10,"cnv",(pfunc_type)ff_cnv, 0},
1569
        {0,TOK_FN10,"cnv",(pfunc_type)ff_cnv, 0},
1565
        {0,TOK_FN1,"rst", (pfunc_type)ff_rst, 0}, // undocumented FilterFactory function
1570
        {0,TOK_FN1,"rst", (pfunc_type)ff_rst, 0}, // undocumented FilterFactory function
1566
        {0,TOK_FN2,"pow", (pfunc_type)ff_pow, 0}, // new function, also added in inofficial Filter Factory 3.1.0 patch
1571
        {0,TOK_FN2,"pow", (pfunc_type)ff_pow, 0}, // new function, also added in inofficial Filter Factory 3.1.0 patch
1567
 
1572
 
1568
        /* predefined variables (names with more than 1 character); most of them are undocumented in FilterFactory */
1573
        /* predefined variables (names with more than 1 character); most of them are undocumented in FilterFactory */
1569
        /* the predefined variables with 1 character are defined in lexer.l and process.c */
1574
        /* the predefined variables with 1 character are defined in lexer.l and process.c */
1570
        /* in this table, you must not add TOK_VAR with only 1 character (since this case is not defined in parser.y) */
1575
        /* in this table, you must not add TOK_VAR with only 1 character (since this case is not defined in parser.y) */
1571
 
1576
 
1572
        {0,TOK_VAR,"rmax",0, &max_channel_val}, // alias of 'R' (defined in lexer.l, line 129)
1577
        {0,TOK_VAR,"rmax",0, &max_channel_val}, // alias of 'R' (defined in lexer.l, line 129)
1573
        {0,TOK_VAR,"gmax",0, &max_channel_val}, // alias of 'G' (defined in lexer.l, line 129)
1578
        {0,TOK_VAR,"gmax",0, &max_channel_val}, // alias of 'G' (defined in lexer.l, line 129)
1574
        {0,TOK_VAR,"bmax",0, &max_channel_val}, // alias of 'B' (defined in lexer.l, line 129)
1579
        {0,TOK_VAR,"bmax",0, &max_channel_val}, // alias of 'B' (defined in lexer.l, line 129)
1575
        {0,TOK_VAR,"amax",0, &max_channel_val}, // alias of 'A' (defined in lexer.l, line 129)
1580
        {0,TOK_VAR,"amax",0, &max_channel_val}, // alias of 'A' (defined in lexer.l, line 129)
1576
        {0,TOK_VAR,"cmax",0, &max_channel_val}, // alias of 'C' (defined in lexer.l, line 129)
1581
        {0,TOK_VAR,"cmax",0, &max_channel_val}, // alias of 'C' (defined in lexer.l, line 129)
1577
        {0,TOK_VAR,"imax",0, &max_channel_val}, // alias of 'I' (defined in lexer.l, line 129)
1582
        {0,TOK_VAR,"imax",0, &max_channel_val}, // alias of 'I' (defined in lexer.l, line 129)
1578
        {0,TOK_VAR,"umax",0, &max_channel_val}, // alias of 'U' (defined in lexer.l, line 129)
1583
        {0,TOK_VAR,"umax",0, &max_channel_val}, // alias of 'U' (defined in lexer.l, line 129)
1579
        {0,TOK_VAR,"vmax",0, &max_channel_val}, // alias of 'V' (defined in lexer.l, line 129)
1584
        {0,TOK_VAR,"vmax",0, &max_channel_val}, // alias of 'V' (defined in lexer.l, line 129)
1580
        {0,TOK_VAR,"dmax",0, &var['D']},
1585
        {0,TOK_VAR,"dmax",0, &var['D']},
1581
        {0,TOK_VAR,"mmax",0, &var['M']},
1586
        {0,TOK_VAR,"mmax",0, &var['M']},
1582
        {0,TOK_VAR,"pmax",0, &var['Z']},
1587
        {0,TOK_VAR,"pmax",0, &var['Z']},
1583
        {0,TOK_VAR,"xmax",0, &var['X']},
1588
        {0,TOK_VAR,"xmax",0, &var['X']},
1584
        {0,TOK_VAR,"ymax",0, &var['Y']},
1589
        {0,TOK_VAR,"ymax",0, &var['Y']},
1585
        {0,TOK_VAR,"zmax",0, &var['Z']},
1590
        {0,TOK_VAR,"zmax",0, &var['Z']},
1586
 
1591
 
1587
        {0,TOK_VAR,"rmin",0, &zero_val},
1592
        {0,TOK_VAR,"rmin",0, &zero_val},
1588
        {0,TOK_VAR,"gmin",0, &zero_val},
1593
        {0,TOK_VAR,"gmin",0, &zero_val},
1589
        {0,TOK_VAR,"bmin",0, &zero_val},
1594
        {0,TOK_VAR,"bmin",0, &zero_val},
1590
        {0,TOK_VAR,"amin",0, &zero_val},
1595
        {0,TOK_VAR,"amin",0, &zero_val},
1591
        {0,TOK_VAR,"cmin",0, &zero_val},
1596
        {0,TOK_VAR,"cmin",0, &zero_val},
1592
        {0,TOK_VAR,"imin",0, &zero_val},
1597
        {0,TOK_VAR,"imin",0, &zero_val},
1593
        {0,TOK_VAR,"umin",0, &zero_val},
1598
        {0,TOK_VAR,"umin",0, &zero_val},
1594
        {0,TOK_VAR,"vmin",0, &zero_val},
1599
        {0,TOK_VAR,"vmin",0, &zero_val},
1595
        {0,TOK_VAR,"dmin",0, &zero_val},
1600
        {0,TOK_VAR,"dmin",0, &zero_val},
1596
        {0,TOK_VAR,"mmin",0, &zero_val},
1601
        {0,TOK_VAR,"mmin",0, &zero_val},
1597
        {0,TOK_VAR,"pmin",0, &zero_val},
1602
        {0,TOK_VAR,"pmin",0, &zero_val},
1598
        {0,TOK_VAR,"xmin",0, &zero_val},
1603
        {0,TOK_VAR,"xmin",0, &zero_val},
1599
        {0,TOK_VAR,"ymin",0, &zero_val},
1604
        {0,TOK_VAR,"ymin",0, &zero_val},
1600
        {0,TOK_VAR,"zmin",0, &zero_val},
1605
        {0,TOK_VAR,"zmin",0, &zero_val},
1601
 
1606
 
1602
        /* Undocumented synonyms of FilterFactory for compatibility with Premiere */
1607
        /* Undocumented synonyms of FilterFactory for compatibility with Premiere */
1603
        {0,TOK_FN10,"cnv0",(pfunc_type)ff_cnv, 0},
1608
        {0,TOK_FN10,"cnv0",(pfunc_type)ff_cnv, 0},
1604
        {0,TOK_FN3,"src0", (pfunc_type)ff_src, 0},
1609
        {0,TOK_FN3,"src0", (pfunc_type)ff_src, 0},
1605
        {0,TOK_FN3,"rad0", (pfunc_type)ff_rad, 0},
1610
        {0,TOK_FN3,"rad0", (pfunc_type)ff_rad, 0},
1606
        {0,TOK_FN10,"cnv1",(pfunc_type)ff_cnv, 0},
1611
        {0,TOK_FN10,"cnv1",(pfunc_type)ff_cnv, 0},
1607
        {0,TOK_FN3,"src1", (pfunc_type)ff_src, 0},
1612
        {0,TOK_FN3,"src1", (pfunc_type)ff_src, 0},
1608
        {0,TOK_FN3,"rad1", (pfunc_type)ff_rad, 0},
1613
        {0,TOK_FN3,"rad1", (pfunc_type)ff_rad, 0},
1609
        {0,TOK_VAR,"r0",0, &var['r']},
1614
        {0,TOK_VAR,"r0",0, &var['r']},
1610
        {0,TOK_VAR,"g0",0, &var['g']},
1615
        {0,TOK_VAR,"g0",0, &var['g']},
1611
        {0,TOK_VAR,"b0",0, &var['b']},
1616
        {0,TOK_VAR,"b0",0, &var['b']},
1612
        {0,TOK_VAR,"a0",0, &var['a']},
1617
        {0,TOK_VAR,"a0",0, &var['a']},
1613
        {0,TOK_VAR,"c0",0, &var['c']},
1618
        {0,TOK_VAR,"c0",0, &var['c']},
1614
        {0,TOK_VAR,"i0",0, &var['i']},
1619
        {0,TOK_VAR,"i0",0, &var['i']},
1615
        {0,TOK_VAR,"u0",0, &var['u']},
1620
        {0,TOK_VAR,"u0",0, &var['u']},
1616
        {0,TOK_VAR,"v0",0, &var['v']},
1621
        {0,TOK_VAR,"v0",0, &var['v']},
1617
        {0,TOK_VAR,"d0",0, &var['d']},
1622
        {0,TOK_VAR,"d0",0, &var['d']},
1618
        {0,TOK_VAR,"m0",0, &var['m']},
1623
        {0,TOK_VAR,"m0",0, &var['m']},
1619
        {0,TOK_VAR,"r1",0, &var['r']},
1624
        {0,TOK_VAR,"r1",0, &var['r']},
1620
        {0,TOK_VAR,"g1",0, &var['g']},
1625
        {0,TOK_VAR,"g1",0, &var['g']},
1621
        {0,TOK_VAR,"b1",0, &var['b']},
1626
        {0,TOK_VAR,"b1",0, &var['b']},
1622
        {0,TOK_VAR,"a1",0, &var['a']},
1627
        {0,TOK_VAR,"a1",0, &var['a']},
1623
        {0,TOK_VAR,"c1",0, &var['c']},
1628
        {0,TOK_VAR,"c1",0, &var['c']},
1624
        {0,TOK_VAR,"i1",0, &var['i']},
1629
        {0,TOK_VAR,"i1",0, &var['i']},
1625
        {0,TOK_VAR,"u1",0, &var['u']},
1630
        {0,TOK_VAR,"u1",0, &var['u']},
1626
        {0,TOK_VAR,"v1",0, &var['v']},
1631
        {0,TOK_VAR,"v1",0, &var['v']},
1627
        {0,TOK_VAR,"d1",0, &var['d']},
1632
        {0,TOK_VAR,"d1",0, &var['d']},
1628
        {0,TOK_VAR,"m1",0, &var['m']},
1633
        {0,TOK_VAR,"m1",0, &var['m']},
1629
        {0,TOK_VAR,"tmin",0, &zero_val},
1634
        {0,TOK_VAR,"tmin",0, &zero_val},
1630
        {0,TOK_VAR,"tmax",0, &one_val},
1635
        {0,TOK_VAR,"tmax",0, &one_val},
1631
        {0,TOK_VAR,"total",0, &one_val},
1636
        {0,TOK_VAR,"total",0, &one_val},
1632
 
1637
 
1633
        {0,0,0,0,0}
1638
        {0,0,0,0,0}
1634
};
1639
};
1635
 
1640