Subversion Repositories filter_foundry

Rev

Rev 536 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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