r_picture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 <commctrl.h>
  27. #include "resource.h"
  28. #include "r_defs.h"
  29. #include "../Agave/Language/api_language.h"
  30. #ifndef LASER
  31. #define MOD_NAME "Render / Picture"
  32. #define C_THISCLASS C_PictureClass
  33. class C_THISCLASS : public C_RBASE {
  34. protected:
  35. public:
  36. C_THISCLASS();
  37. virtual ~C_THISCLASS();
  38. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  39. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_RENDER_PICTURE,desc,128):desc); }
  40. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  41. virtual void load_config(unsigned char *data, int len);
  42. virtual int save_config(unsigned char *data);
  43. void loadPicture(char *name);
  44. void freePicture();
  45. int enabled;
  46. int width,height;
  47. HBITMAP hOldBitmap;
  48. HBITMAP hb;
  49. HBITMAP hb2;
  50. HDC hBitmapDC;
  51. HDC hBitmapDC2;
  52. int lastWidth,lastHeight;
  53. int blend, blendavg, adapt, persist;
  54. int ratio,axis_ratio;
  55. char ascName[MAX_PATH];
  56. int persistCount;
  57. };
  58. static C_THISCLASS *g_ConfigThis; // global configuration dialog pointer
  59. static HINSTANCE g_hDllInstance; // global DLL instance pointer (not needed in this example, but could be useful)
  60. // configuration read/write
  61. C_THISCLASS::C_THISCLASS() // set up default configuration
  62. {
  63. persistCount=0;
  64. enabled=1;
  65. blend=0;
  66. adapt=0;
  67. blendavg=1;
  68. persist=6;
  69. strcpy(ascName,"");
  70. hb=0; ratio=0; axis_ratio=0;
  71. hBitmapDC=0; hBitmapDC2=0;
  72. }
  73. C_THISCLASS::~C_THISCLASS()
  74. {
  75. freePicture();
  76. }
  77. void C_THISCLASS::loadPicture(char *name)
  78. {
  79. freePicture();
  80. char longName[MAX_PATH];
  81. wsprintf(longName,"%s\\%s",g_path,name);
  82. hb=(HBITMAP)LoadImage(0,longName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
  83. BITMAP bm;
  84. GetObject(hb, sizeof(bm), (LPSTR)&bm);
  85. width=bm.bmWidth;
  86. height=bm.bmHeight;
  87. lastWidth=lastHeight=0;
  88. }
  89. void C_THISCLASS::freePicture()
  90. {
  91. if(hb)
  92. {
  93. DeleteObject(hb);
  94. hb=0;
  95. }
  96. }
  97. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  98. void C_THISCLASS::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  99. {
  100. int pos=0;
  101. if (len-pos >= 4) { enabled=GET_INT(); pos+=4; }
  102. if (len-pos >= 4) { blend=GET_INT(); pos+=4; }
  103. if (len-pos >= 4) { blendavg=GET_INT(); pos+=4; }
  104. if (len-pos >= 4) { adapt=GET_INT(); pos+=4; }
  105. if (len-pos >= 4) { persist=GET_INT(); pos+=4; }
  106. char *p=ascName;
  107. while (data[pos] && len-pos > 0) *p++=data[pos++];
  108. *p=0; pos++;
  109. if (len-pos >= 4) { ratio=GET_INT(); pos+=4; }
  110. if (len-pos >= 4) { axis_ratio=GET_INT(); pos+=4; }
  111. if (*ascName)
  112. loadPicture(ascName);
  113. }
  114. #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
  115. int C_THISCLASS::save_config(unsigned char *data) // write configuration to data, return length. config data should not exceed 64k.
  116. {
  117. int pos=0;
  118. PUT_INT(enabled); pos+=4;
  119. PUT_INT(blend); pos+=4;
  120. PUT_INT(blendavg); pos+=4;
  121. PUT_INT(adapt); pos+=4;
  122. PUT_INT(persist); pos+=4;
  123. strcpy((char *)data+pos, ascName);
  124. pos+=strlen(ascName)+1;
  125. PUT_INT(ratio); pos+=4;
  126. PUT_INT(axis_ratio); pos+=4;
  127. return pos;
  128. }
  129. // render function
  130. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout. this is
  131. // used when you want to do something that you'd otherwise need to make a copy of the framebuffer.
  132. // w and h are the width and height of the screen, in pixels.
  133. // isBeat is 1 if a beat has been detected.
  134. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  135. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  136. {
  137. if (!enabled) return 0;
  138. if (!width || !height) return 0;
  139. if (lastWidth != w || lastHeight != h)
  140. {
  141. lastWidth = w;
  142. lastHeight = h;
  143. if(hBitmapDC2) {
  144. DeleteDC(hBitmapDC2);
  145. DeleteObject(hb2);
  146. hBitmapDC2=0;
  147. }
  148. // Copy the bitmap from hBitmapDC to hBitmapDC2 and stretch it
  149. hBitmapDC = CreateCompatibleDC (NULL);
  150. hOldBitmap = (HBITMAP) SelectObject (hBitmapDC, hb);
  151. hBitmapDC2 = CreateCompatibleDC (NULL);
  152. hb2=CreateCompatibleBitmap (hBitmapDC, w, h);
  153. SelectObject(hBitmapDC2,hb2);
  154. {
  155. HBRUSH b=CreateSolidBrush(0);
  156. HPEN p=CreatePen(PS_SOLID,0,0);
  157. HBRUSH bold;
  158. HPEN pold;
  159. bold=(HBRUSH)SelectObject(hBitmapDC2,b);
  160. pold=(HPEN)SelectObject(hBitmapDC2,p);
  161. Rectangle(hBitmapDC2,0,0,w,h);
  162. SelectObject(hBitmapDC2,bold);
  163. SelectObject(hBitmapDC2,pold);
  164. DeleteObject(b);
  165. DeleteObject(p);
  166. }
  167. SetStretchBltMode(hBitmapDC2,COLORONCOLOR);
  168. int final_height=h,start_height=0;
  169. int final_width=w,start_width=0;
  170. if (ratio)
  171. {
  172. if(axis_ratio==0) {
  173. // ratio on X axis
  174. final_height=height*w/width;
  175. start_height=(h/2)-(final_height/2);
  176. } else {
  177. // ratio on Y axis
  178. final_width=width*h/height;
  179. start_width=(w/2)-(final_width/2);
  180. }
  181. }
  182. StretchBlt(hBitmapDC2,start_width,start_height,final_width,final_height,hBitmapDC,0,0,width,height,SRCCOPY);
  183. DeleteDC(hBitmapDC);
  184. hBitmapDC=0;
  185. }
  186. if (isBeat&0x80000000) return 0;
  187. // Copy the stretched bitmap to fbout
  188. BITMAPINFO bi;
  189. bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  190. bi.bmiHeader.biWidth = w;
  191. bi.bmiHeader.biHeight = h;
  192. bi.bmiHeader.biPlanes = 1;
  193. bi.bmiHeader.biBitCount = 32;
  194. bi.bmiHeader.biCompression = BI_RGB;
  195. bi.bmiHeader.biSizeImage = 0;
  196. bi.bmiHeader.biXPelsPerMeter = 0;
  197. bi.bmiHeader.biYPelsPerMeter = 0;
  198. bi.bmiHeader.biClrUsed = 0;
  199. bi.bmiHeader.biClrImportant = 0;
  200. GetDIBits(hBitmapDC2, hb2, 0, h, (void *)fbout, &bi, DIB_RGB_COLORS);
  201. // Copy the bitmap from fbout to framebuffer applying replace/blend/etc...
  202. if (isBeat)
  203. persistCount=persist;
  204. else
  205. if (persistCount>0) persistCount--;
  206. int *p,*d;
  207. int i,j;
  208. p = fbout;
  209. d = framebuffer+w*(h-1);
  210. if (blend || (adapt && (isBeat || persistCount)))
  211. for (i=0;i<h;i++)
  212. {
  213. for (j=0;j<w;j++)
  214. {
  215. *d=BLEND(*p, *d);
  216. d++;
  217. p++;
  218. }
  219. d -= w*2;
  220. }
  221. else
  222. if (blendavg || adapt)
  223. for (i=0;i<h;i++)
  224. {
  225. for (j=0;j<w;j++)
  226. {
  227. *d=BLEND_AVG(*p, *d);
  228. d++;
  229. p++;
  230. }
  231. d -= w*2;
  232. }
  233. else
  234. for (i=0;i<h;i++)
  235. {
  236. memcpy(d, p, w*4);
  237. p+=w;
  238. d-=w;
  239. }
  240. return 0;
  241. }
  242. // configuration dialog stuff
  243. static void EnableWindows(HWND hwndDlg)
  244. {
  245. EnableWindow(GetDlgItem(hwndDlg,IDC_PERSIST),g_ConfigThis->adapt);
  246. EnableWindow(GetDlgItem(hwndDlg,IDC_PERSIST_TITLE),g_ConfigThis->adapt);
  247. EnableWindow(GetDlgItem(hwndDlg,IDC_PERSIST_TEXT1),g_ConfigThis->adapt);
  248. EnableWindow(GetDlgItem(hwndDlg,IDC_PERSIST_TEXT2),g_ConfigThis->adapt);
  249. EnableWindow(GetDlgItem(hwndDlg,IDC_X_RATIO),g_ConfigThis->ratio);
  250. EnableWindow(GetDlgItem(hwndDlg,IDC_Y_RATIO),g_ConfigThis->ratio);
  251. }
  252. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  253. {
  254. switch (uMsg)
  255. {
  256. case WM_INITDIALOG:
  257. SendDlgItemMessage(hwndDlg, IDC_PERSIST, TBM_SETRANGE, TRUE, MAKELONG(0, 32));
  258. SendDlgItemMessage(hwndDlg, IDC_PERSIST, TBM_SETPOS, TRUE, g_ConfigThis->persist);
  259. if (g_ConfigThis->enabled) CheckDlgButton(hwndDlg,IDC_ENABLED,BST_CHECKED);
  260. if (g_ConfigThis->blend) CheckDlgButton(hwndDlg,IDC_ADDITIVE,BST_CHECKED);
  261. if (g_ConfigThis->blendavg) CheckDlgButton(hwndDlg,IDC_5050,BST_CHECKED);
  262. if (g_ConfigThis->adapt) CheckDlgButton(hwndDlg,IDC_ADAPT,BST_CHECKED);
  263. if (!g_ConfigThis->adapt && !g_ConfigThis->blend && !g_ConfigThis->blendavg)
  264. CheckDlgButton(hwndDlg,IDC_REPLACE,BST_CHECKED);
  265. if (g_ConfigThis->ratio) CheckDlgButton(hwndDlg,IDC_RATIO,BST_CHECKED);
  266. if (!g_ConfigThis->axis_ratio) CheckDlgButton(hwndDlg,IDC_X_RATIO,BST_CHECKED);
  267. if (g_ConfigThis->axis_ratio) CheckDlgButton(hwndDlg,IDC_Y_RATIO,BST_CHECKED);
  268. EnableWindows(hwndDlg);
  269. loadComboBox(GetDlgItem(hwndDlg,OBJ_COMBO),"*.BMP",g_ConfigThis->ascName);
  270. return 1;
  271. case WM_NOTIFY:
  272. if (LOWORD(wParam) == IDC_PERSIST)
  273. g_ConfigThis->persist = SendDlgItemMessage(hwndDlg, IDC_PERSIST, TBM_GETPOS, 0, 0);
  274. return 0;
  275. case WM_COMMAND:
  276. if ((LOWORD(wParam) == IDC_ENABLED) ||
  277. (LOWORD(wParam) == IDC_ADDITIVE) ||
  278. (LOWORD(wParam) == IDC_REPLACE) ||
  279. (LOWORD(wParam) == IDC_ADAPT) ||
  280. (LOWORD(wParam) == IDC_5050) ) {
  281. g_ConfigThis->enabled=IsDlgButtonChecked(hwndDlg,IDC_ENABLED)?1:0;
  282. g_ConfigThis->blend=IsDlgButtonChecked(hwndDlg,IDC_ADDITIVE)?1:0;
  283. g_ConfigThis->blendavg=IsDlgButtonChecked(hwndDlg,IDC_5050)?1:0;
  284. g_ConfigThis->adapt=IsDlgButtonChecked(hwndDlg,IDC_ADAPT)?1:0;
  285. EnableWindows(hwndDlg);
  286. }
  287. if(LOWORD(wParam) == IDC_RATIO || LOWORD(wParam) == IDC_X_RATIO || LOWORD(wParam) == IDC_Y_RATIO) {
  288. g_ConfigThis->ratio=IsDlgButtonChecked(hwndDlg,IDC_RATIO)?1:0;
  289. g_ConfigThis->axis_ratio=IsDlgButtonChecked(hwndDlg,IDC_Y_RATIO)?1:0;
  290. g_ConfigThis->lastWidth=-1; g_ConfigThis->lastHeight=-1;
  291. EnableWindows(hwndDlg);
  292. }
  293. if (HIWORD(wParam) == CBN_SELCHANGE && LOWORD(wParam) == OBJ_COMBO) // handle clicks to combo box
  294. {
  295. int sel = SendDlgItemMessage(hwndDlg, OBJ_COMBO, CB_GETCURSEL, 0, 0);
  296. if (sel != -1)
  297. {
  298. SendDlgItemMessage(hwndDlg, OBJ_COMBO, CB_GETLBTEXT, sel, (LPARAM)g_ConfigThis->ascName);
  299. if (*(g_ConfigThis->ascName))
  300. g_ConfigThis->loadPicture(g_ConfigThis->ascName);
  301. }
  302. }
  303. return 0;
  304. case WM_HSCROLL:
  305. return 0;
  306. }
  307. return 0;
  308. }
  309. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  310. {
  311. g_ConfigThis = this;
  312. return WASABI_API_CREATEDIALOG(IDD_CFG_PICTURE,hwndParent,g_DlgProc);
  313. }
  314. // export stuff
  315. C_RBASE *R_Picture(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  316. {
  317. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  318. return (C_RBASE *) new C_THISCLASS();
  319. }
  320. #else
  321. C_RBASE *R_Picture(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  322. {
  323. return NULL;
  324. }
  325. #endif