r_shift.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #include <windows.h>
  25. #include <commctrl.h>
  26. #include <math.h>
  27. #include "r_defs.h"
  28. #include "resource.h"
  29. #include "avs_eelif.h"
  30. #include "timing.h"
  31. #include "../Agave/Language/api_language.h"
  32. #ifndef LASER
  33. #define C_THISCLASS C_ShiftClass
  34. #define MOD_NAME "Trans / Dynamic Shift"
  35. class C_THISCLASS : public C_RBASE {
  36. protected:
  37. public:
  38. C_THISCLASS();
  39. virtual ~C_THISCLASS();
  40. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  41. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_DYNAMIC_SHIFT,desc,128):desc); }
  42. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  43. virtual void load_config(unsigned char *data, int len);
  44. virtual int save_config(unsigned char *data);
  45. RString effect_exp[3];
  46. int blend,subpixel;
  47. int m_lastw,m_lasth;
  48. NSEEL_VMCTX AVS_EEL_CONTEXTNAME;
  49. double *var_x, *var_y, *var_w, *var_h, *var_b, *var_alpha;
  50. double max_d;
  51. int inited;
  52. NSEEL_CODEHANDLE codehandle[3];
  53. int need_recompile;
  54. CRITICAL_SECTION rcs;
  55. };
  56. #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
  57. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  58. void C_THISCLASS::load_config(unsigned char *data, int len)
  59. {
  60. int pos=0;
  61. if (data[pos] == 1)
  62. {
  63. pos++;
  64. load_string(effect_exp[0],data,pos,len);
  65. load_string(effect_exp[1],data,pos,len);
  66. load_string(effect_exp[2],data,pos,len);
  67. }
  68. else
  69. {
  70. char buf[769];
  71. if (len-pos >= 768)
  72. {
  73. memcpy(buf,data+pos,768);
  74. pos+=768;
  75. buf[768]=0;
  76. effect_exp[2].assign(buf+512);
  77. buf[512]=0;
  78. effect_exp[1].assign(buf+256);
  79. buf[256]=0;
  80. effect_exp[0].assign(buf);
  81. }
  82. }
  83. if (len-pos >= 4) { blend=GET_INT(); pos+=4; }
  84. if (len-pos >= 4) { subpixel=GET_INT(); pos+=4; }
  85. }
  86. int C_THISCLASS::save_config(unsigned char *data)
  87. {
  88. int pos=0;
  89. data[pos++]=1;
  90. save_string(data,pos,effect_exp[0]);
  91. save_string(data,pos,effect_exp[1]);
  92. save_string(data,pos,effect_exp[2]);
  93. PUT_INT(blend); pos+=4;
  94. PUT_INT(subpixel); pos+=4;
  95. return pos;
  96. }
  97. C_THISCLASS::C_THISCLASS()
  98. {
  99. InitializeCriticalSection(&rcs);
  100. AVS_EEL_INITINST();
  101. memset(codehandle,0,sizeof(codehandle));
  102. m_lasth=m_lastw=0;
  103. effect_exp[0].assign("d=0;"); // init
  104. effect_exp[1].assign("x=sin(d)*1.4; y=1.4*cos(d); d=d+0.01;"); // frame
  105. effect_exp[2].assign("d=d+2.0");
  106. blend=0;
  107. subpixel=1;
  108. need_recompile=1;
  109. var_b=0;
  110. }
  111. C_THISCLASS::~C_THISCLASS()
  112. {
  113. int x;
  114. for (x = 0; x < 3; x ++)
  115. {
  116. freeCode(codehandle[x]);
  117. }
  118. AVS_EEL_QUITINST();
  119. DeleteCriticalSection(&rcs);
  120. }
  121. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  122. {
  123. //pow(sin(d),dpos)*1.7
  124. if (need_recompile)
  125. {
  126. int err=0;
  127. int x;
  128. EnterCriticalSection(&rcs);
  129. if (!var_b || g_reset_vars_on_recompile)
  130. {
  131. clearVars();
  132. var_x = registerVar("x");
  133. var_y = registerVar("y");
  134. var_w = registerVar("w");
  135. var_h = registerVar("h");
  136. var_b = registerVar("b");
  137. var_alpha = registerVar("alpha");
  138. inited=0;
  139. }
  140. need_recompile=0;
  141. for (x = 0; x < 3; x ++)
  142. {
  143. freeCode(codehandle[x]);
  144. codehandle[x]=compileCode(effect_exp[x].get());
  145. }
  146. LeaveCriticalSection(&rcs);
  147. }
  148. *var_w=w;
  149. *var_h=h;
  150. *var_b=isBeat?1.0:0.0;
  151. if (isBeat&0x80000000) return 0;
  152. if (codehandle[0] && (!inited || m_lasth != h || m_lastw != w))
  153. {
  154. m_lastw=w;
  155. m_lasth=h;
  156. *var_x=0; *var_y=0; *var_alpha=0.5;
  157. executeCode(codehandle[0],visdata);
  158. inited=1;
  159. }
  160. executeCode(codehandle[1],visdata);
  161. if (isBeat) executeCode(codehandle[2],visdata);
  162. int doblend=blend;
  163. int ialpha=127;
  164. if (doblend)
  165. {
  166. ialpha=(int)(*var_alpha*255.0);
  167. if (ialpha <= 0) return 0;
  168. if (ialpha >= 255) doblend=0;
  169. }
  170. int *inptr=framebuffer;
  171. int *blendptr=framebuffer;
  172. int *outptr=fbout;
  173. int xa=(int)*var_x;
  174. int ya=(int)*var_y;
  175. // var_x, var_y at this point tell us how to shift, and blend also tell us what to do.
  176. if (!subpixel)
  177. {
  178. int endy=h+ya;
  179. int endx=w+xa;
  180. int x,y;
  181. if (endx > w) endx=w;
  182. if (endy > h) endy=h;
  183. if (ya < 0) inptr += -ya*w;
  184. if (ya > h) ya=h;
  185. if (xa > w) xa=w;
  186. for (y = 0; y < ya; y ++)
  187. {
  188. x=w;
  189. if (!doblend) while (x--) *outptr++ = 0;
  190. else while (x--) *outptr++ = BLEND_ADJ(0,*blendptr++,ialpha);
  191. }
  192. for (; y < endy; y ++)
  193. {
  194. int *ip=inptr;
  195. if (xa < 0) inptr += -xa;
  196. if (!doblend)
  197. {
  198. for (x = 0; x < xa; x ++) *outptr++=0;
  199. for (; x < endx; x ++) *outptr++=*inptr++;
  200. for (; x < w; x ++) *outptr++=0;
  201. }
  202. else
  203. {
  204. for (x = 0; x < xa; x ++) *outptr++ = BLEND_ADJ(0,*blendptr++,ialpha);
  205. for (; x < endx; x ++) *outptr++ = BLEND_ADJ(*inptr++,*blendptr++,ialpha);
  206. for (; x < w; x ++) *outptr++ = BLEND_ADJ(0,*blendptr++,ialpha);
  207. }
  208. inptr=ip+w;
  209. }
  210. for (; y < h; y ++)
  211. {
  212. x=w;
  213. if (!doblend) while (x--) *outptr++ = 0;
  214. else while (x--) *outptr++ = BLEND_ADJ(0,*blendptr++,ialpha);
  215. }
  216. }
  217. else // bilinear filtering version
  218. {
  219. int xpart,ypart;
  220. {
  221. double vx=*var_x;
  222. double vy=*var_y;
  223. xpart=(int) ((vx - (int)vx)*255.0);
  224. if (xpart < 0) xpart=-xpart;
  225. else { xa++; xpart=255-xpart; }
  226. if (xpart < 0) xpart=0;
  227. if (xpart > 255) xpart=255;
  228. ypart=(int) ((vy - (int)vy)*255.0);
  229. if (ypart < 0) ypart=-ypart;
  230. else { ya++; ypart=255-ypart; }
  231. if (ypart < 0) ypart=0;
  232. if (ypart > 255) ypart=255;
  233. }
  234. int x,y;
  235. if (ya < 1-h) ya=1-h;
  236. if (xa < 1-w) xa=1-w;
  237. if (ya > h-1) ya=h-1;
  238. if (xa > w-1) xa=w-1;
  239. if (ya < 0) inptr += -ya*w;
  240. int endy=h-1+ya;
  241. int endx=w-1+xa;
  242. if (endx > w-1) endx=w-1;
  243. if (endy > h-1) endy=h-1;
  244. if (endx < 0) endx=0;
  245. if (endy < 0) endy=0;
  246. for (y = 0; y < ya; y ++)
  247. {
  248. x=w;
  249. if (!doblend) while (x--) *outptr++ = 0;
  250. else while (x--) *outptr++ = BLEND_ADJ(0,*blendptr++,ialpha);
  251. }
  252. for (; y < endy; y ++)
  253. {
  254. int *ip=inptr;
  255. if (xa < 0) inptr += -xa;
  256. if (!doblend)
  257. {
  258. for (x = 0; x < xa; x ++) *outptr++=0;
  259. for (; x < endx; x ++) *outptr++=BLEND4((unsigned int *)inptr++,w,xpart,ypart);
  260. for (; x < w; x ++) *outptr++=0;
  261. }
  262. else
  263. {
  264. for (x = 0; x < xa; x ++) *outptr++ = BLEND_ADJ(0,*blendptr++,ialpha);
  265. for (; x < endx; x ++) *outptr++ = BLEND_ADJ(BLEND4((unsigned int *)inptr++,w,xpart,ypart),*blendptr++,ialpha);
  266. for (; x < w; x ++) *outptr++ = BLEND_ADJ(0,*blendptr++,ialpha);
  267. }
  268. inptr=ip+w;
  269. }
  270. for (; y < h; y ++)
  271. {
  272. x=w;
  273. if (!doblend) while (x--) *outptr++ = 0;
  274. else while (x--) *outptr++ = BLEND_ADJ(0,*blendptr++,ialpha);
  275. }
  276. }
  277. #ifndef NO_MMX
  278. __asm emms;
  279. #endif
  280. return 1;
  281. }
  282. C_RBASE *R_Shift(char *desc)
  283. {
  284. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  285. return (C_RBASE *) new C_THISCLASS();
  286. }
  287. static C_THISCLASS *g_this;
  288. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  289. {
  290. static int isstart;
  291. switch (uMsg)
  292. {
  293. case WM_INITDIALOG:
  294. isstart=1;
  295. SetDlgItemText(hwndDlg,IDC_EDIT1,g_this->effect_exp[0].get());
  296. SetDlgItemText(hwndDlg,IDC_EDIT2,g_this->effect_exp[1].get());
  297. SetDlgItemText(hwndDlg,IDC_EDIT3,g_this->effect_exp[2].get());
  298. isstart=0;
  299. if (g_this->blend)
  300. CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  301. if (g_this->subpixel)
  302. CheckDlgButton(hwndDlg,IDC_CHECK2,BST_CHECKED);
  303. return 1;
  304. case WM_COMMAND:
  305. if (LOWORD(wParam) == IDC_HELPBTN)
  306. {
  307. char *text="Dynamic Shift\0"
  308. "better Dynamic shift help goes here (send me some :)\r\n";
  309. "Variables:\r\n"
  310. "x,y = amount to shift (in pixels - set these)\r\n"
  311. "w,h = width, height (in pixels)\r\n"
  312. "b = isBeat\r\n"
  313. "alpha = alpha value (0.0-1.0) for blend\r\n"
  314. ;
  315. compilerfunctionlist(hwndDlg,text);
  316. }
  317. if (LOWORD(wParam)==IDC_CHECK1)
  318. {
  319. g_this->blend=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  320. }
  321. if (LOWORD(wParam)==IDC_CHECK2)
  322. {
  323. g_this->subpixel=IsDlgButtonChecked(hwndDlg,IDC_CHECK2)?1:0;
  324. }
  325. if (!isstart && (LOWORD(wParam) == IDC_EDIT1||LOWORD(wParam) == IDC_EDIT2||LOWORD(wParam) == IDC_EDIT3) && HIWORD(wParam) == EN_CHANGE)
  326. {
  327. EnterCriticalSection(&g_this->rcs);
  328. g_this->effect_exp[0].get_from_dlgitem(hwndDlg,IDC_EDIT1);
  329. g_this->effect_exp[1].get_from_dlgitem(hwndDlg,IDC_EDIT2);
  330. g_this->effect_exp[2].get_from_dlgitem(hwndDlg,IDC_EDIT3);
  331. g_this->need_recompile=1;
  332. if (LOWORD(wParam) == IDC_EDIT1) g_this->inited = 0;
  333. LeaveCriticalSection(&g_this->rcs);
  334. }
  335. return 0;
  336. }
  337. return 0;
  338. }
  339. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent)
  340. {
  341. g_this = this;
  342. return WASABI_API_CREATEDIALOG(IDD_CFG_SHIFT,hwndParent,g_DlgProc);
  343. }
  344. #else
  345. C_RBASE *R_Shift(char *desc)
  346. {
  347. return NULL;
  348. }
  349. #endif