r_grain.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <stdlib.h>
  26. #include <vfw.h>
  27. #include <commctrl.h>
  28. #include "resource.h"
  29. #include "r_defs.h"
  30. #include "../Agave/Language/api_language.h"
  31. #ifndef LASER
  32. #define MOD_NAME "Trans / Grain"
  33. #define C_THISCLASS C_GrainClass
  34. class C_THISCLASS : public C_RBASE {
  35. protected:
  36. public:
  37. C_THISCLASS();
  38. virtual ~C_THISCLASS();
  39. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  40. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_GRAIN,desc,128):desc); }
  41. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  42. virtual void load_config(unsigned char *data, int len);
  43. virtual int save_config(unsigned char *data);
  44. unsigned char __inline fastrandbyte(void);
  45. void reinit(int w, int h);
  46. int enabled;
  47. int blend, blendavg, smax;
  48. unsigned char *depthBuffer;
  49. int oldx, oldy;
  50. int staticgrain;
  51. unsigned char randtab[491];
  52. int randtab_pos;
  53. };
  54. static C_THISCLASS *g_ConfigThis; // global configuration dialog pointer
  55. static HINSTANCE g_hDllInstance; // global DLL instance pointer (not needed in this example, but could be useful)
  56. C_THISCLASS::~C_THISCLASS() // set up default configuration
  57. {
  58. if (depthBuffer)
  59. GlobalFree(depthBuffer);
  60. }
  61. // configuration read/write
  62. C_THISCLASS::C_THISCLASS() // set up default configuration
  63. {
  64. blend=0;
  65. blendavg=0;
  66. smax=100;
  67. enabled=1;
  68. oldx=0;
  69. oldy=0;
  70. staticgrain=0;
  71. depthBuffer=NULL;
  72. int x;
  73. for (x = 0 ;x < sizeof(randtab); x ++)
  74. randtab[x]=rand()&255;
  75. randtab_pos=rand()%sizeof(randtab);
  76. }
  77. unsigned char __inline C_THISCLASS::fastrandbyte(void)
  78. {
  79. unsigned char r=randtab[randtab_pos];
  80. randtab_pos++;
  81. if (!(randtab_pos&15))
  82. {
  83. randtab_pos+=rand()%73;
  84. }
  85. if (randtab_pos >= 491) randtab_pos-=491;
  86. return r;
  87. }
  88. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  89. void C_THISCLASS::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  90. {
  91. int pos=0;
  92. if (len-pos >= 4) { enabled=GET_INT(); pos+=4; }
  93. if (len-pos >= 4) { blend=GET_INT(); pos+=4; }
  94. if (len-pos >= 4) { blendavg=GET_INT(); pos+=4; }
  95. if (len-pos >= 4) { smax=GET_INT(); pos+=4; }
  96. if (len-pos >= 4) { staticgrain=GET_INT(); pos+=4; }
  97. }
  98. #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
  99. int C_THISCLASS::save_config(unsigned char *data) // write configuration to data, return length. config data should not exceed 64k.
  100. {
  101. int pos=0;
  102. PUT_INT(enabled); pos+=4;
  103. PUT_INT(blend); pos+=4;
  104. PUT_INT(blendavg); pos+=4;
  105. PUT_INT(smax); pos+=4;
  106. PUT_INT(staticgrain); pos+=4;
  107. return pos;
  108. }
  109. void C_THISCLASS::reinit(int w, int h)
  110. {
  111. int x,y;
  112. unsigned char *p;
  113. if (depthBuffer)
  114. GlobalFree(depthBuffer);
  115. depthBuffer = (unsigned char *)GlobalAlloc(GMEM_FIXED,w*h*2);
  116. p = depthBuffer;
  117. if (p)
  118. for (y=0;y<h;y++)
  119. for (x=0;x<w;x++)
  120. {
  121. *p++ = (rand()%255);
  122. *p++ = (rand()%100);
  123. }
  124. }
  125. // render function
  126. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout. this is
  127. // used when you want to do something that you'd otherwise need to make a copy of the framebuffer.
  128. // w and h are the width and height of the screen, in pixels.
  129. // isBeat is 1 if a beat has been detected.
  130. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  131. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  132. {
  133. if (isBeat&0x80000000) return 0;
  134. if (!enabled) return 0;
  135. int smax_sc = (smax*255)/100;
  136. int *p;
  137. unsigned char *q;
  138. int l=w*h;
  139. if (w != oldx || h != oldy)
  140. {
  141. reinit(w, h);
  142. oldx = w;
  143. oldy = h;
  144. }
  145. randtab_pos+=rand()%300;
  146. if (randtab_pos >= 491) randtab_pos-=491;
  147. p = framebuffer;
  148. q = depthBuffer;
  149. if (staticgrain)
  150. {
  151. if (blend)
  152. {
  153. while (l--)
  154. {
  155. if (*p)
  156. {
  157. int c=0;
  158. if (q[1] < smax_sc)
  159. {
  160. int s = q[0];
  161. int r=(((p[0]&0xff0000)*s)>>8);
  162. if (r > 0xff0000) r=0xff0000;
  163. c|=r&0xff0000;
  164. r=(((p[0]&0xff00)*s)>>8);
  165. if (r > 0xff00) r=0xff00;
  166. c|=r&0xff00;
  167. r=(((p[0]&0xff)*s)>>8);
  168. if (r > 0xff) r=0xff;
  169. c|=r;
  170. }
  171. *p = BLEND(*p, c);
  172. }
  173. p++;
  174. q+=2;
  175. }
  176. }
  177. else if (blendavg)
  178. {
  179. while (l--)
  180. {
  181. if (*p)
  182. {
  183. int c=0;
  184. if (q[1] < smax_sc)
  185. {
  186. int s = q[0];
  187. int r=(((p[0]&0xff0000)*s)>>8);
  188. if (r > 0xff0000) r=0xff0000;
  189. c|=r&0xff0000;
  190. r=(((p[0]&0xff00)*s)>>8);
  191. if (r > 0xff00) r=0xff00;
  192. c|=r&0xff00;
  193. r=(((p[0]&0xff)*s)>>8);
  194. if (r > 0xff) r=0xff;
  195. c|=r;
  196. }
  197. *p = BLEND_AVG(*p, c);
  198. }
  199. p++;
  200. q+=2;
  201. }
  202. }
  203. else
  204. {
  205. while (l--)
  206. {
  207. if (*p)
  208. {
  209. int c=0;
  210. if (q[1] < smax_sc)
  211. {
  212. int s = q[0];
  213. int r=(((p[0]&0xff0000)*s)>>8);
  214. if (r > 0xff0000) r=0xff0000;
  215. c|=r&0xff0000;
  216. r=(((p[0]&0xff00)*s)>>8);
  217. if (r > 0xff00) r=0xff00;
  218. c|=r&0xff00;
  219. r=(((p[0]&0xff)*s)>>8);
  220. if (r > 0xff) r=0xff;
  221. c|=r;
  222. }
  223. *p = c;
  224. }
  225. p++;
  226. q+=2;
  227. }
  228. }
  229. }
  230. else
  231. {
  232. if (blend)
  233. {
  234. while (l--)
  235. {
  236. if (*p)
  237. {
  238. int c=0;
  239. if (fastrandbyte() < smax_sc)
  240. {
  241. int s = fastrandbyte();
  242. int r=(((p[0]&0xff0000)*s)>>8);
  243. if (r > 0xff0000) r=0xff0000;
  244. c|=r&0xff0000;
  245. r=(((p[0]&0xff00)*s)>>8);
  246. if (r > 0xff00) r=0xff00;
  247. c|=r&0xff00;
  248. r=(((p[0]&0xff)*s)>>8);
  249. if (r > 0xff) r=0xff;
  250. c|=r;
  251. }
  252. *p = BLEND(*p, c);
  253. }
  254. p++;
  255. q+=2;
  256. }
  257. }
  258. else if (blendavg)
  259. {
  260. while (l--)
  261. {
  262. if (*p)
  263. {
  264. int c=0;
  265. if (fastrandbyte() < smax_sc)
  266. {
  267. int s = fastrandbyte();
  268. int r=(((p[0]&0xff0000)*s)>>8);
  269. if (r > 0xff0000) r=0xff0000;
  270. c|=r&0xff0000;
  271. r=(((p[0]&0xff00)*s)>>8);
  272. if (r > 0xff00) r=0xff00;
  273. c|=r&0xff00;
  274. r=(((p[0]&0xff)*s)>>8);
  275. if (r > 0xff) r=0xff;
  276. c|=r;
  277. }
  278. *p = BLEND_AVG(*p, c);
  279. }
  280. p++;
  281. q+=2;
  282. }
  283. }
  284. else
  285. {
  286. while (l--)
  287. {
  288. if (*p)
  289. {
  290. int c=0;
  291. if (fastrandbyte() < smax_sc)
  292. {
  293. int s = fastrandbyte();
  294. int r=(((p[0]&0xff0000)*s)>>8);
  295. if (r > 0xff0000) r=0xff0000;
  296. c|=r&0xff0000;
  297. r=(((p[0]&0xff00)*s)>>8);
  298. if (r > 0xff00) r=0xff00;
  299. c|=r&0xff00;
  300. r=(((p[0]&0xff)*s)>>8);
  301. if (r > 0xff) r=0xff;
  302. c|=r;
  303. }
  304. *p = c;
  305. }
  306. p++;
  307. q+=2;
  308. }
  309. }
  310. }
  311. return 0;
  312. }
  313. // configuration dialog stuff
  314. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  315. {
  316. switch (uMsg)
  317. {
  318. case WM_INITDIALOG:
  319. SendDlgItemMessage(hwndDlg, IDC_MAX, TBM_SETTICFREQ, 10, 0);
  320. SendDlgItemMessage(hwndDlg, IDC_MAX, TBM_SETRANGE, TRUE, MAKELONG(0, 100));
  321. SendDlgItemMessage(hwndDlg, IDC_MAX, TBM_SETPOS, TRUE, g_ConfigThis->smax);
  322. if (g_ConfigThis->enabled) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  323. if (g_ConfigThis->staticgrain) CheckDlgButton(hwndDlg,IDC_STATGRAIN,BST_CHECKED);
  324. if (g_ConfigThis->blend) CheckDlgButton(hwndDlg,IDC_ADDITIVE,BST_CHECKED);
  325. if (g_ConfigThis->blendavg) CheckDlgButton(hwndDlg,IDC_5050,BST_CHECKED);
  326. if (!g_ConfigThis->blend && !g_ConfigThis->blendavg)
  327. CheckDlgButton(hwndDlg,IDC_REPLACE,BST_CHECKED);
  328. return 1;
  329. case WM_NOTIFY:
  330. {
  331. if (LOWORD(wParam) == IDC_MAX)
  332. g_ConfigThis->smax = SendDlgItemMessage(hwndDlg, IDC_MAX, TBM_GETPOS, 0, 0);
  333. return 0;
  334. }
  335. case WM_COMMAND:
  336. if ((LOWORD(wParam) == IDC_CHECK1) ||
  337. (LOWORD(wParam) == IDC_STATGRAIN) ||
  338. (LOWORD(wParam) == IDC_ADDITIVE) ||
  339. (LOWORD(wParam) == IDC_REPLACE) ||
  340. (LOWORD(wParam) == IDC_5050) )
  341. {
  342. g_ConfigThis->enabled=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  343. g_ConfigThis->blend=IsDlgButtonChecked(hwndDlg,IDC_ADDITIVE)?1:0;
  344. g_ConfigThis->blendavg=IsDlgButtonChecked(hwndDlg,IDC_5050)?1:0;
  345. g_ConfigThis->staticgrain=IsDlgButtonChecked(hwndDlg,IDC_STATGRAIN)?1:0;
  346. }
  347. return 0;
  348. }
  349. return 0;
  350. }
  351. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  352. {
  353. g_ConfigThis = this;
  354. return WASABI_API_CREATEDIALOG(IDD_CFG_GRAIN,hwndParent,g_DlgProc);
  355. }
  356. // export stuff
  357. C_RBASE *R_Grain(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  358. {
  359. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  360. return (C_RBASE *) new C_THISCLASS();
  361. }
  362. #else
  363. C_RBASE *R_Grain(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  364. {
  365. return NULL;
  366. }
  367. #endif