1
0

r_ddm.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #define M_PI 3.14159265358979323846
  25. #include <windows.h>
  26. #include <commctrl.h>
  27. #include <math.h>
  28. #include "r_defs.h"
  29. #include "resource.h"
  30. #include "avs_eelif.h"
  31. #include "timing.h"
  32. #include "../Agave/Language/api_language.h"
  33. #ifndef LASER
  34. #define C_THISCLASS C_PulseClass
  35. #define MOD_NAME "Trans / Dynamic Distance Modifier"
  36. // Integer Square Root function
  37. // Uses factoring to find square root
  38. // A 256 entry table used to work out the square root of the 7 or 8 most
  39. // significant bits. A power of 2 used to approximate the rest.
  40. // Based on an 80386 Assembly implementation by Arne Steinarson
  41. static unsigned const char sq_table[]=
  42. {0, 16, 22, 27, 32, 35, 39, 42, 45, 48, 50, 53, 55, 57, 59, 61, 64, 65,
  43. 67, 69, 71, 73, 75, 76, 78, 80, 81, 83, 84, 86, 87, 89, 90, 91, 93, 94,
  44. 96, 97, 98, 99, 101, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113,
  45. 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128,
  46. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
  47. 142, 143, 144, 144, 145, 146, 147, 148, 149, 150, 150, 151, 152, 153,
  48. 154, 155, 155, 156, 157, 158, 159, 160, 160, 161, 162, 163, 163, 164,
  49. 165, 166, 167, 167, 168, 169, 170, 170, 171, 172, 173, 173, 174, 175,
  50. 176, 176, 177, 178, 178, 179, 180, 181, 181, 182, 183, 183, 184, 185,
  51. 185, 186, 187, 187, 188, 189, 189, 190, 191, 192, 192, 193, 193, 194,
  52. 195, 195, 196, 197, 197, 198, 199, 199, 200, 201, 201, 202, 203, 203,
  53. 204, 204, 205, 206, 206, 207, 208, 208, 209, 209, 210, 211, 211, 212,
  54. 212, 213, 214, 214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220,
  55. 221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 227, 228,
  56. 229, 229, 230, 230, 231, 231, 232, 232, 233, 234, 234, 235, 235, 236,
  57. 236, 237, 237, 238, 238, 239, 240, 240, 241, 241, 242, 242, 243, 243,
  58. 244, 244, 245, 245, 246, 246, 247, 247, 248, 248, 249, 249, 250, 250,
  59. 251, 251, 252, 252, 253, 253, 254, 254, 255};
  60. static __inline unsigned long isqrt(unsigned long n)
  61. {
  62. if (n >= 0x10000)
  63. if (n >= 0x1000000)
  64. if (n >= 0x10000000)
  65. if (n >= 0x40000000) return(sq_table[n >> 24] << 8);
  66. else return(sq_table[n >> 22] << 7);
  67. else
  68. if (n >= 0x4000000) return(sq_table[n >> 20] << 6);
  69. else return(sq_table[n >> 18] << 5);
  70. else
  71. if (n >= 0x100000)
  72. if (n >= 0x400000) return(sq_table[n >> 16] << 4);
  73. else return(sq_table[n >> 14] << 3);
  74. else
  75. if (n >= 0x40000) return(sq_table[n >> 12] << 2);
  76. else return(sq_table[n >> 10] << 1);
  77. else
  78. if (n >= 0x100)
  79. if (n >= 0x1000)
  80. if (n >= 0x4000) return(sq_table[n >> 8]);
  81. else return(sq_table[n >> 6] >> 1);
  82. else
  83. if (n >= 0x400) return(sq_table[n >> 4] >> 2);
  84. else return(sq_table[n >> 2] >> 3);
  85. else
  86. if (n >= 0x10)
  87. if (n >= 0x40) return(sq_table[n] >> 4);
  88. else return(sq_table[n << 2] << 5);
  89. else
  90. if (n >= 0x4) return(sq_table[n >> 4] << 6);
  91. else return(sq_table[n >> 6] << 7);
  92. }
  93. class C_THISCLASS : public C_RBASE {
  94. protected:
  95. public:
  96. C_THISCLASS();
  97. virtual ~C_THISCLASS();
  98. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  99. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_DYNAMIC_DISTANCE_MODIFIER,desc,128):desc); }
  100. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  101. virtual void load_config(unsigned char *data, int len);
  102. virtual int save_config(unsigned char *data);
  103. RString effect_exp[4];
  104. int blend;
  105. int m_wt;
  106. int m_lastw,m_lasth;
  107. int *m_wmul;
  108. int *m_tab;
  109. NSEEL_VMCTX AVS_EEL_CONTEXTNAME;
  110. double *var_d, *var_b;
  111. double max_d;
  112. int inited;
  113. NSEEL_CODEHANDLE codehandle[4];
  114. int need_recompile;
  115. int subpixel;
  116. CRITICAL_SECTION rcs;
  117. };
  118. #define PUT_INT(y) data[pos]=(y)&255; data[pos+1]=(y>>8)&255; data[pos+2]=(y>>16)&255; data[pos+3]=(y>>24)&255
  119. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  120. void C_THISCLASS::load_config(unsigned char *data, int len)
  121. {
  122. int pos=0;
  123. if (data[pos] == 1)
  124. {
  125. pos++;
  126. load_string(effect_exp[0],data,pos,len);
  127. load_string(effect_exp[1],data,pos,len);
  128. load_string(effect_exp[2],data,pos,len);
  129. load_string(effect_exp[3],data,pos,len);
  130. }
  131. else
  132. {
  133. char buf[513];
  134. if (len-pos >= 256*2)
  135. {
  136. memcpy(buf,data+pos,256*2);
  137. pos+=256*2;
  138. buf[512]=0;
  139. effect_exp[1].assign(buf+256);
  140. buf[256]=0;
  141. effect_exp[0].assign(buf);
  142. }
  143. if (len-pos >= 256*2)
  144. {
  145. memcpy(buf,data+pos,256*2);
  146. pos+=256*2;
  147. buf[512]=0;
  148. effect_exp[3].assign(buf+256);
  149. buf[256]=0;
  150. effect_exp[2].assign(buf);
  151. }
  152. }
  153. if (len-pos >= 4) { blend=GET_INT(); pos+=4; }
  154. if (len-pos >= 4) { subpixel=GET_INT(); pos+=4; }
  155. }
  156. int C_THISCLASS::save_config(unsigned char *data)
  157. {
  158. int pos=0;
  159. data[pos++]=1;
  160. save_string(data,pos,effect_exp[0]);
  161. save_string(data,pos,effect_exp[1]);
  162. save_string(data,pos,effect_exp[2]);
  163. save_string(data,pos,effect_exp[3]);
  164. PUT_INT(blend); pos+=4;
  165. PUT_INT(subpixel); pos+=4;
  166. return pos;
  167. }
  168. C_THISCLASS::C_THISCLASS()
  169. {
  170. AVS_EEL_INITINST();
  171. InitializeCriticalSection(&rcs);
  172. need_recompile=1;
  173. memset(codehandle,0,sizeof(codehandle));
  174. m_lasth=m_lastw=0;
  175. m_wmul=0;
  176. m_tab=0;
  177. m_wt=0;
  178. effect_exp[0].assign("d=d-sigmoid((t-50)/100,2)");
  179. effect_exp[3].assign("u=1;t=0");
  180. effect_exp[1].assign("t=t+u;t=min(100,t);t=max(0,t);u=if(equal(t,100),-1,u);u=if(equal(t,0),1,u)");
  181. effect_exp[2].assign("");
  182. blend=0;
  183. subpixel=0;
  184. var_b=0;
  185. }
  186. C_THISCLASS::~C_THISCLASS()
  187. {
  188. int x;
  189. for (x = 0; x < 4; x ++)
  190. {
  191. freeCode(codehandle[x]);
  192. codehandle[x]=0;
  193. }
  194. if (m_wmul) GlobalFree(m_wmul);
  195. if (m_tab) GlobalFree(m_tab);
  196. AVS_EEL_QUITINST();
  197. m_tab=0;
  198. m_wmul=0;
  199. DeleteCriticalSection(&rcs);
  200. }
  201. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  202. {
  203. int *fbin=framebuffer;
  204. if (m_lasth != h || m_lastw != w || !m_tab || !m_wmul)
  205. {
  206. int y;
  207. m_lastw=w; // jf 121100 - added (oops)
  208. m_lasth=h;
  209. max_d=sqrt((w*w+h*h)/4.0);
  210. if (m_wmul) GlobalFree(m_wmul);
  211. m_wmul=(int*)GlobalAlloc(GMEM_FIXED,sizeof(int)*h);
  212. for (y = 0; y < h; y ++) m_wmul[y]=y*w;
  213. if (m_tab) GlobalFree(m_tab);
  214. m_tab=0;
  215. }
  216. int imax_d=(int)(max_d + 32.9);
  217. if (imax_d < 33) imax_d=33;
  218. if (!m_tab)
  219. m_tab=(int*)GlobalAlloc(GMEM_FIXED,sizeof(int)*imax_d);
  220. int x;
  221. //pow(sin(d),dpos)*1.7
  222. if (need_recompile)
  223. {
  224. EnterCriticalSection(&rcs);
  225. if (!var_b || g_reset_vars_on_recompile)
  226. {
  227. clearVars();
  228. var_d = registerVar("d");
  229. var_b = registerVar("b");
  230. inited=0;
  231. }
  232. need_recompile=0;
  233. for (x = 0; x < 4; x ++)
  234. {
  235. freeCode(codehandle[x]);
  236. codehandle[x]=compileCode(effect_exp[x].get());
  237. }
  238. LeaveCriticalSection(&rcs);
  239. }
  240. if (isBeat&0x80000000) return 0;
  241. *var_b=isBeat?1.0:0.0;
  242. if (codehandle[3] && !inited) { executeCode(codehandle[3],visdata); inited=1; }
  243. executeCode(codehandle[1],visdata);
  244. if (isBeat) executeCode(codehandle[2],visdata);
  245. if (codehandle[0])
  246. {
  247. for (x = 0; x < imax_d-32; x ++)
  248. {
  249. *var_d=x/(max_d-1);
  250. executeCode(codehandle[0],visdata);
  251. m_tab[x]=(int) (*var_d*256.0*max_d/(x+1));
  252. }
  253. for (; x < imax_d; x ++)
  254. {
  255. m_tab[x]=m_tab[x-1];
  256. }
  257. }
  258. else for (x = 0; x < imax_d; x ++) m_tab[x]=0;
  259. m_wt++;
  260. m_wt&=63;
  261. {
  262. int w2=w/2;
  263. int h2=h/2;
  264. int y;
  265. for (y = 0; y < h; y ++)
  266. {
  267. int ty=y-h2;
  268. int x2=w2*w2+w2+ty*ty+256;
  269. int dx2=-2*w2;
  270. int yysc=ty;
  271. int xxsc=-w2;
  272. int x=w;
  273. if (subpixel)
  274. {
  275. if (blend)
  276. while (x--)
  277. {
  278. int qd=m_tab[isqrt(x2)];
  279. int ow,oh;
  280. int xpart,ypart;
  281. x2+=dx2;
  282. dx2+=2;
  283. xpart=(qd*xxsc+128);
  284. ypart=(qd*yysc+128);
  285. ow = w2 + (xpart>>8);
  286. oh = h2 + (ypart>>8);
  287. xpart&=0xff;
  288. ypart&=0xff;
  289. xxsc++;
  290. if (ow < 0) ow=0;
  291. else if (ow >= w-1) ow=w-2;
  292. if (oh < 0) oh=0;
  293. else if (oh >= h-1) oh=h-2;
  294. *fbout++=BLEND_AVG(BLEND4((unsigned int *)framebuffer+ow+m_wmul[oh],w,xpart,ypart),*fbin++);
  295. }
  296. else
  297. while (x--)
  298. {
  299. int qd=m_tab[isqrt(x2)];
  300. int ow,oh;
  301. int xpart,ypart;
  302. x2+=dx2;
  303. dx2+=2;
  304. xpart=(qd*xxsc+128);
  305. ypart=(qd*yysc+128);
  306. ow = w2 + (xpart>>8);
  307. oh = h2 + (ypart>>8);
  308. xpart&=0xff;
  309. ypart&=0xff;
  310. xxsc++;
  311. if (ow < 0) ow=0;
  312. else if (ow >= w-1) ow=w-2;
  313. if (oh < 0) oh=0;
  314. else if (oh >= h-1) oh=h-2;
  315. *fbout++=BLEND4((unsigned int *)framebuffer+ow+m_wmul[oh],w,xpart,ypart);
  316. }
  317. }
  318. else
  319. {
  320. if (blend)
  321. while (x--)
  322. {
  323. int qd=m_tab[isqrt(x2)];
  324. int ow,oh;
  325. x2+=dx2;
  326. dx2+=2;
  327. ow = w2 + ((qd*xxsc+128)>>8);
  328. xxsc++;
  329. oh = h2 + ((qd*yysc+128)>>8);
  330. if (ow < 0) ow=0;
  331. else if (ow >= w) ow=w-1;
  332. if (oh < 0) oh=0;
  333. else if (oh >= h) oh=h-1;
  334. *fbout++=BLEND_AVG(framebuffer[ow+m_wmul[oh]],*fbin++);
  335. }
  336. else
  337. while (x--)
  338. {
  339. int qd=m_tab[isqrt(x2)];
  340. int ow,oh;
  341. x2+=dx2;
  342. dx2+=2;
  343. ow = w2 + ((qd*xxsc+128)>>8);
  344. xxsc++;
  345. oh = h2 + ((qd*yysc+128)>>8);
  346. if (ow < 0) ow=0;
  347. else if (ow >= w) ow=w-1;
  348. if (oh < 0) oh=0;
  349. else if (oh >= h) oh=h-1;
  350. *fbout++=framebuffer[ow+m_wmul[oh]];
  351. }
  352. }
  353. }
  354. }
  355. #ifndef NO_MMX
  356. if (subpixel) __asm emms;
  357. #endif
  358. return 1;
  359. }
  360. C_RBASE *R_DDM(char *desc)
  361. {
  362. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  363. return (C_RBASE *) new C_THISCLASS();
  364. }
  365. static C_THISCLASS *g_this;
  366. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  367. {
  368. static int isstart;
  369. switch (uMsg)
  370. {
  371. case WM_INITDIALOG:
  372. isstart=1;
  373. SetDlgItemText(hwndDlg,IDC_EDIT1,g_this->effect_exp[0].get());
  374. SetDlgItemText(hwndDlg,IDC_EDIT2,g_this->effect_exp[1].get());
  375. SetDlgItemText(hwndDlg,IDC_EDIT3,g_this->effect_exp[2].get());
  376. SetDlgItemText(hwndDlg,IDC_EDIT4,g_this->effect_exp[3].get());
  377. isstart=0;
  378. if (g_this->blend)
  379. CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  380. if (g_this->subpixel)
  381. CheckDlgButton(hwndDlg,IDC_CHECK2,BST_CHECKED);
  382. return 1;
  383. case WM_COMMAND:
  384. if (LOWORD(wParam) == IDC_BUTTON1)
  385. {
  386. /* char text[4096];
  387. WASABI_API_LNGSTRING_BUF(IDS_DYNAMIC_DISTANCE_MODIFIER,text,4096);
  388. int titlelen = lstrlen(text)+1;
  389. lstrcpyn(text+titlelen,GetTextResource(IDR_DYNAMIC_DISTANCE_MODIFIER),4095-titlelen);
  390. */
  391. char *text="Dynamic Distance Modifier\0"
  392. "The dynamic distance modifier allows you to dynamically (once per frame)\r\n"
  393. "change the source pixels for each ring of pixels out from the center.\r\n"
  394. "In the 'pixel' code section, 'd' represents the distance in pixels\r\n"
  395. "the current ring is from the center, and code can modify it to\r\n"
  396. "change the distance from the center where the source pixels for\r\n"
  397. "that ring would be read. This is a terrible explanation, and if\r\n"
  398. "you want to make a better one send it to me. \r\n"
  399. "\r\n"
  400. "Examples:\r\n"
  401. "Zoom in: 'd=d*0.9'\r\n"
  402. "Zoom out: 'd=d*1.1'\r\n"
  403. "Back and forth: pixel='d=d*(1.0+0.1*cos(t));', frame='t=t+0.1'\r\n"
  404. ;
  405. compilerfunctionlist(hwndDlg,text);
  406. }
  407. if (LOWORD(wParam)==IDC_CHECK1)
  408. {
  409. g_this->blend=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  410. }
  411. if (LOWORD(wParam)==IDC_CHECK2)
  412. {
  413. g_this->subpixel=IsDlgButtonChecked(hwndDlg,IDC_CHECK2)?1:0;
  414. }
  415. if (!isstart && (LOWORD(wParam) == IDC_EDIT1||LOWORD(wParam) == IDC_EDIT2||LOWORD(wParam) == IDC_EDIT3||LOWORD(wParam) == IDC_EDIT4) && HIWORD(wParam) == EN_CHANGE)
  416. {
  417. EnterCriticalSection(&g_this->rcs);
  418. g_this->effect_exp[0].get_from_dlgitem(hwndDlg,IDC_EDIT1);
  419. g_this->effect_exp[1].get_from_dlgitem(hwndDlg,IDC_EDIT2);
  420. g_this->effect_exp[2].get_from_dlgitem(hwndDlg,IDC_EDIT3);
  421. g_this->effect_exp[3].get_from_dlgitem(hwndDlg,IDC_EDIT4);
  422. g_this->need_recompile=1;
  423. if (LOWORD(wParam) == IDC_EDIT4) g_this->inited = 0;
  424. LeaveCriticalSection(&g_this->rcs);
  425. }
  426. return 0;
  427. }
  428. return 0;
  429. }
  430. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent)
  431. {
  432. g_this = this;
  433. return WASABI_API_CREATEDIALOG(IDD_CFG_DDM,hwndParent,g_DlgProc);
  434. }
  435. #else
  436. C_RBASE *R_DDM(char *desc) { return NULL; }
  437. #endif