r_onetone.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 / Unique tone"
  33. #define C_THISCLASS C_OnetoneClass
  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_UNIQUE_TONE,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. void RebuildTable(void);
  45. int __inline depthof(int c);
  46. int enabled;
  47. int invert;
  48. int color;
  49. unsigned char tabler[256];
  50. unsigned char tableg[256];
  51. unsigned char tableb[256];
  52. int blend, blendavg;
  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()
  57. {
  58. }
  59. // configuration read/write
  60. C_THISCLASS::C_THISCLASS() // set up default configuration
  61. {
  62. color = 0xFFFFFF;
  63. invert=0;
  64. blend = 0;
  65. blendavg = 0;
  66. enabled=1;
  67. RebuildTable();
  68. }
  69. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  70. void C_THISCLASS::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  71. {
  72. int pos=0;
  73. if (len-pos >= 4) { enabled=GET_INT(); pos+=4; }
  74. if (len-pos >= 4) { color=GET_INT(); pos+=4; }
  75. if (len-pos >= 4) { blend=GET_INT(); pos+=4; }
  76. if (len-pos >= 4) { blendavg=GET_INT(); pos+=4; }
  77. if (len-pos >= 4) { invert=GET_INT(); pos+=4; }
  78. RebuildTable();
  79. }
  80. #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
  81. int C_THISCLASS::save_config(unsigned char *data) // write configuration to data, return length. config data should not exceed 64k.
  82. {
  83. int pos=0;
  84. PUT_INT(enabled); pos+=4;
  85. PUT_INT(color); pos+=4;
  86. PUT_INT(blend); pos+=4;
  87. PUT_INT(blendavg); pos+=4;
  88. PUT_INT(invert); pos+=4;
  89. return pos;
  90. }
  91. int __inline C_THISCLASS::depthof(int c)
  92. {
  93. int r= max(max((c & 0xFF), ((c & 0xFF00)>>8)), (c & 0xFF0000)>>16);
  94. return invert ? 255 - r : r;
  95. }
  96. void C_THISCLASS::RebuildTable(void)
  97. {
  98. int i;
  99. for (i=0;i<256;i++)
  100. tableb[i] = (unsigned char)((i / 255.0) * (float)(color & 0xFF));
  101. for (i=0;i<256;i++)
  102. tableg[i] = (unsigned char)((i / 255.0) * (float)((color & 0xFF00) >> 8));
  103. for (i=0;i<256;i++)
  104. tabler[i] = (unsigned char)((i / 255.0) * (float)((color & 0xFF0000) >> 16));
  105. }
  106. // render function
  107. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout. this is
  108. // used when you want to do something that you'd otherwise need to make a copy of the framebuffer.
  109. // w and h are the width and height of the screen, in pixels.
  110. // isBeat is 1 if a beat has been detected.
  111. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  112. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  113. {
  114. int i=w*h;
  115. int *p=framebuffer;
  116. int c,d;
  117. if (!enabled) return 0;
  118. if (isBeat&0x80000000) return 0;
  119. if (blend)
  120. {
  121. while (i--)
  122. {
  123. d = depthof(*p);
  124. c = tableb[d] | (tableg[d]<<8) | (tabler[d]<<16);
  125. *p++ = BLEND(*p, c);
  126. }
  127. }
  128. else if (blendavg)
  129. {
  130. while (i--)
  131. {
  132. d = depthof(*p);
  133. c = tableb[d] | (tableg[d]<<8) | (tabler[d]<<16);
  134. *p++ = BLEND_AVG(*p, c);
  135. }
  136. }
  137. else
  138. {
  139. while (i--)
  140. {
  141. d = depthof(*p);
  142. *p++ = tableb[d] | (tableg[d]<<8) | (tabler[d]<<16);
  143. }
  144. }
  145. return 0;
  146. }
  147. // configuration dialog stuff
  148. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  149. {
  150. switch (uMsg)
  151. {
  152. case WM_INITDIALOG:
  153. if (g_ConfigThis->enabled) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  154. if (g_ConfigThis->invert) CheckDlgButton(hwndDlg,IDC_INVERT,BST_CHECKED);
  155. if (g_ConfigThis->blend) CheckDlgButton(hwndDlg,IDC_ADDITIVE,BST_CHECKED);
  156. if (g_ConfigThis->blendavg) CheckDlgButton(hwndDlg,IDC_5050,BST_CHECKED);
  157. if (!g_ConfigThis->blend && !g_ConfigThis->blendavg)
  158. CheckDlgButton(hwndDlg,IDC_REPLACE,BST_CHECKED);
  159. return 1;
  160. case WM_DRAWITEM:
  161. {
  162. DRAWITEMSTRUCT *di=(DRAWITEMSTRUCT *)lParam;
  163. if (di->CtlID == IDC_DEFCOL) // paint nifty color button
  164. {
  165. int w=di->rcItem.right-di->rcItem.left;
  166. int _color=g_ConfigThis->color;
  167. _color = ((_color>>16)&0xff)|(_color&0xff00)|((_color<<16)&0xff0000);
  168. HPEN hPen,hOldPen;
  169. HBRUSH hBrush,hOldBrush;
  170. LOGBRUSH lb={ (COLORREF)BS_SOLID,(COLORREF)_color,(COLORREF)0};
  171. hPen = (HPEN)CreatePen(PS_SOLID,0,_color);
  172. hBrush = CreateBrushIndirect(&lb);
  173. hOldPen=(HPEN)SelectObject(di->hDC,hPen);
  174. hOldBrush=(HBRUSH)SelectObject(di->hDC,hBrush);
  175. Rectangle(di->hDC,di->rcItem.left,di->rcItem.top,di->rcItem.right,di->rcItem.bottom);
  176. SelectObject(di->hDC,hOldPen);
  177. SelectObject(di->hDC,hOldBrush);
  178. DeleteObject(hBrush);
  179. DeleteObject(hPen);
  180. }
  181. }
  182. return 0;
  183. case WM_COMMAND:
  184. if ((LOWORD(wParam) == IDC_CHECK1) ||
  185. (LOWORD(wParam) == IDC_ADDITIVE) ||
  186. (LOWORD(wParam) == IDC_REPLACE) ||
  187. (LOWORD(wParam) == IDC_5050) ||
  188. (LOWORD(wParam) == IDC_INVERT))
  189. {
  190. g_ConfigThis->enabled=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  191. g_ConfigThis->blend=IsDlgButtonChecked(hwndDlg,IDC_ADDITIVE)?1:0;
  192. g_ConfigThis->blendavg=IsDlgButtonChecked(hwndDlg,IDC_5050)?1:0;
  193. g_ConfigThis->invert=IsDlgButtonChecked(hwndDlg,IDC_INVERT)?1:0;
  194. }
  195. if (LOWORD(wParam) == IDC_DEFCOL) // handle clicks to nifty color button
  196. {
  197. int *a=&(g_ConfigThis->color);
  198. static COLORREF custcolors[16];
  199. CHOOSECOLOR cs;
  200. cs.lStructSize = sizeof(cs);
  201. cs.hwndOwner = hwndDlg;
  202. cs.hInstance = 0;
  203. cs.rgbResult=((*a>>16)&0xff)|(*a&0xff00)|((*a<<16)&0xff0000);
  204. cs.lpCustColors = custcolors;
  205. cs.Flags = CC_RGBINIT|CC_FULLOPEN;
  206. if (ChooseColor(&cs))
  207. {
  208. *a = ((cs.rgbResult>>16)&0xff)|(cs.rgbResult&0xff00)|((cs.rgbResult<<16)&0xff0000);
  209. g_ConfigThis->color = *a;
  210. g_ConfigThis->RebuildTable();
  211. }
  212. InvalidateRect(GetDlgItem(hwndDlg,IDC_DEFCOL),NULL,TRUE);
  213. }
  214. }
  215. return 0;
  216. }
  217. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  218. {
  219. g_ConfigThis = this;
  220. return WASABI_API_CREATEDIALOG(IDD_CFG_ONETONE,hwndParent,g_DlgProc);
  221. }
  222. // export stuff
  223. C_RBASE *R_Onetone(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  224. {
  225. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  226. return (C_RBASE *) new C_THISCLASS();
  227. }
  228. #else
  229. C_RBASE *R_Onetone(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  230. {
  231. return NULL;
  232. }
  233. #endif