PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Extra/AlphaBlend/AlphaBlend.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 324 lines | 220 code | 58 blank | 46 comment | 16 complexity | 7c1261f903aa8c674dcdbe629f3984a6 MD5 | raw file
  1. /+
  2. + Copyright Andrej Mitrovic 2011.
  3. + Distributed under the Boost Software License, Version 1.0.
  4. + (See accompanying file LICENSE_1_0.txt or copy at
  5. + http://www.boost.org/LICENSE_1_0.txt)
  6. +
  7. + Demonstrates alpha-blending.
  8. +/
  9. module AlphaBlend;
  10. import core.memory;
  11. import core.runtime;
  12. import core.thread;
  13. import std.conv;
  14. import std.math;
  15. import std.range;
  16. import std.string;
  17. import std.utf;
  18. auto toUTF16z(S) (S s)
  19. {
  20. return toUTFz!(const(wchar)*)(s);
  21. }
  22. pragma(lib, "gdi32.lib");
  23. import core.sys.windows.windef;
  24. import core.sys.windows.winuser;
  25. import core.sys.windows.wingdi;
  26. import core.sys.windows.winbase;
  27. extern (Windows) BOOL GdiAlphaBlend(HDC, int, int, int, int, HDC, int, int, int, int, BLENDFUNCTION);
  28. string appName = "AlphaBlend";
  29. string description = "AlphaBlend Demo";
  30. HINSTANCE hinst;
  31. extern (Windows)
  32. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  33. {
  34. int result;
  35. void exceptionHandler(Throwable e)
  36. {
  37. throw e;
  38. }
  39. try
  40. {
  41. Runtime.initialize();
  42. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  43. Runtime.terminate();
  44. }
  45. catch (Throwable o)
  46. {
  47. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  48. result = 0;
  49. }
  50. return result;
  51. }
  52. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  53. {
  54. hinst = hInstance;
  55. HACCEL hAccel;
  56. HWND hwnd;
  57. MSG msg;
  58. WNDCLASS wndclass;
  59. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  60. wndclass.lpfnWndProc = &WndProc;
  61. wndclass.cbClsExtra = 0;
  62. wndclass.cbWndExtra = 0;
  63. wndclass.hInstance = hInstance;
  64. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  65. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  66. //~ wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  67. wndclass.hbrBackground = null; // don't send WM_ERASEBKND messages
  68. wndclass.lpszMenuName = appName.toUTF16z;
  69. wndclass.lpszClassName = appName.toUTF16z;
  70. if (!RegisterClass(&wndclass))
  71. {
  72. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  73. return 0;
  74. }
  75. hwnd = CreateWindow(appName.toUTF16z, // window class name
  76. description.toUTF16z, // window caption
  77. WS_OVERLAPPEDWINDOW, // window style
  78. CW_USEDEFAULT, // initial x position
  79. CW_USEDEFAULT, // initial y position
  80. CW_USEDEFAULT, // initial x size
  81. CW_USEDEFAULT, // initial y size
  82. NULL, // parent window handle
  83. NULL, // window menu handle
  84. hInstance, // program instance handle
  85. NULL); // creation parameters
  86. ShowWindow(hwnd, iCmdShow);
  87. UpdateWindow(hwnd);
  88. while (GetMessage(&msg, NULL, 0, 0))
  89. {
  90. TranslateMessage(&msg);
  91. DispatchMessage(&msg);
  92. }
  93. return msg.wParam;
  94. }
  95. extern (Windows)
  96. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  97. {
  98. scope (failure) assert(0);
  99. static int cxClient, cyClient, cxSource, cySource;
  100. int x, y;
  101. HDC hdc;
  102. PAINTSTRUCT ps;
  103. static HDC hdcMem;
  104. static HBITMAP hbmMem;
  105. static HANDLE hOld;
  106. RECT rect;
  107. switch (message)
  108. {
  109. case WM_SIZE:
  110. cxClient = LOWORD(lParam);
  111. cyClient = HIWORD(lParam);
  112. return 0;
  113. // When you set 'hbrBackground = null' it prevents
  114. // the WM_ERASEBKND message to be sent.
  115. case WM_ERASEBKGND:
  116. return 1;
  117. case WM_PAINT:
  118. {
  119. // Get DC for window
  120. hdc = BeginPaint(hwnd, &ps);
  121. //~ // Create an off-screen DC for double-buffering
  122. hdcMem = CreateCompatibleDC(hdc);
  123. hbmMem = CreateCompatibleBitmap(hdc, cxClient, cyClient);
  124. hOld = SelectObject(hdcMem, hbmMem);
  125. // Draw into hdcMem
  126. GetClientRect(hwnd, &rect);
  127. FillRect(hdcMem, &rect, GetStockObject(BLACK_BRUSH));
  128. rect.left += 10;
  129. rect.top += 10;
  130. rect.right -= 10;
  131. rect.bottom -= 10;
  132. FillRect(hdcMem, &rect, GetStockObject(WHITE_BRUSH));
  133. DrawAlphaBlend(hwnd, hdcMem);
  134. // Transfer the off-screen DC to the screen
  135. BitBlt(hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, SRCCOPY);
  136. // Free-up the off-screen DC
  137. SelectObject(hdcMem, hOld);
  138. DeleteObject(hbmMem);
  139. DeleteDC(hdcMem);
  140. EndPaint(hwnd, &ps);
  141. return 0;
  142. }
  143. case WM_DESTROY:
  144. PostQuitMessage(0);
  145. return 0;
  146. default:
  147. }
  148. return DefWindowProc(hwnd, message, wParam, lParam);
  149. }
  150. void DrawAlphaBlend(HWND hWnd, HDC hdcwnd)
  151. {
  152. HDC hdc; // handle of the DC we will create
  153. BLENDFUNCTION bf; // structure for alpha blending
  154. HBITMAP hbitmap; // bitmap handle
  155. BITMAPINFO bmi; // bitmap header
  156. VOID* pvBits; // pointer to DIB section
  157. ULONG ulWindowWidth, ulWindowHeight; // window width/height
  158. ULONG ulBitmapWidth, ulBitmapHeight; // bitmap width/height
  159. RECT rt; // used for getting window dimensions
  160. UINT32 x, y; // stepping variables
  161. UCHAR ubAlpha; // used for doing transparent gradient
  162. UCHAR ubRed;
  163. UCHAR ubGreen;
  164. UCHAR ubBlue;
  165. float fAlphaFactor = 0; // used to do premultiply
  166. // get window dimensions
  167. GetClientRect(hWnd, &rt);
  168. // calculate window width/height
  169. ulWindowWidth = rt.right - rt.left;
  170. ulWindowHeight = rt.bottom - rt.top;
  171. // make sure we have at least some window size
  172. if (ulWindowWidth < 1 || ulWindowHeight < 1)
  173. return;
  174. // divide the window into 3 horizontal areas
  175. ulWindowHeight = ulWindowHeight / 3;
  176. // create a DC for our bitmap -- the source DC for GdiAlphaBlend
  177. hdc = CreateCompatibleDC(hdcwnd);
  178. // setup bitmap info
  179. // set the bitmap width and height to 60% of the width and height of each of the three horizontal areas. Later on, the blending will occur in the center of each of the three areas.
  180. bmi.bmiHeader.biSize = BITMAPINFOHEADER.sizeof;
  181. bmi.bmiHeader.biWidth = ulBitmapWidth = ulWindowWidth - (ulWindowWidth / 5) * 2;
  182. bmi.bmiHeader.biHeight = ulBitmapHeight = ulWindowHeight - (ulWindowHeight / 5) * 2;
  183. bmi.bmiHeader.biPlanes = 1;
  184. bmi.bmiHeader.biBitCount = 32; // four 8-bit components
  185. bmi.bmiHeader.biCompression = BI_RGB;
  186. bmi.bmiHeader.biSizeImage = ulBitmapWidth * ulBitmapHeight * 4;
  187. // create our DIB section and select the bitmap into the dc
  188. hbitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0x0);
  189. SelectObject(hdc, hbitmap);
  190. // in top window area, constant alpha = 50%, but no source alpha
  191. // the color format for each pixel is 0xaarrggbb
  192. // set all pixels to blue and set source alpha to zero
  193. for (y = 0; y < ulBitmapHeight; y++)
  194. for (x = 0; x < ulBitmapWidth; x++)
  195. (cast(UINT32*)pvBits)[x + y * ulBitmapWidth] = 0x000000ff;
  196. bf.BlendOp = AC_SRC_OVER;
  197. bf.BlendFlags = 0;
  198. bf.SourceConstantAlpha = 0x7f; // half of 0xff = 50% transparency
  199. bf.AlphaFormat = 0; // ignore source alpha channel
  200. if (!GdiAlphaBlend(hdcwnd, ulWindowWidth / 5, ulWindowHeight / 5,
  201. ulBitmapWidth, ulBitmapHeight,
  202. hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))
  203. return; // alpha blend failed
  204. // in middle window area, constant alpha = 100% (disabled), source
  205. // alpha is 0 in middle of bitmap and opaque in rest of bitmap
  206. for (y = 0; y < ulBitmapHeight; y++)
  207. {
  208. for (x = 0; x < ulBitmapWidth; x++)
  209. {
  210. if ((x > cast(int)(ulBitmapWidth / 5)) && (x < (ulBitmapWidth - ulBitmapWidth / 5)) &&
  211. (y > cast(int)(ulBitmapHeight / 5)) && (y < (ulBitmapHeight - ulBitmapHeight / 5)))
  212. //in middle of bitmap: source alpha = 0 (transparent).
  213. // This means multiply each color component by 0x00.
  214. // Thus, after GdiAlphaBlend, we have a, 0x00 * r,
  215. // 0x00 * g,and 0x00 * b (which is 0x00000000)
  216. // for now, set all pixels to red
  217. (cast(UINT32*)pvBits)[x + y * ulBitmapWidth] = 0x00ff0000;
  218. else
  219. // in the rest of bitmap, source alpha = 0xff (opaque)
  220. // and set all pixels to blue
  221. (cast(UINT32*)pvBits)[x + y * ulBitmapWidth] = 0xff0000ff;
  222. }
  223. }
  224. bf.BlendOp = AC_SRC_OVER;
  225. bf.BlendFlags = 0;
  226. bf.AlphaFormat = AC_SRC_ALPHA; // use source alpha
  227. bf.SourceConstantAlpha = 0xff; // opaque (disable constant alpha)
  228. if (!GdiAlphaBlend(hdcwnd, ulWindowWidth / 5, ulWindowHeight / 5 + ulWindowHeight, ulBitmapWidth, ulBitmapHeight, hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))
  229. return;
  230. // bottom window area, use constant alpha = 75% and a changing
  231. // source alpha. Create a gradient effect using source alpha, and
  232. // then fade it even more with constant alpha
  233. ubRed = 0x00;
  234. ubGreen = 0x00;
  235. ubBlue = 0xff;
  236. for (y = 0; y < ulBitmapHeight; y++)
  237. {
  238. for (x = 0; x < ulBitmapWidth; x++)
  239. {
  240. // for a simple gradient, base the alpha value on the x
  241. // value of the pixel
  242. ubAlpha = cast(UCHAR)(cast(float)x / cast(float)ulBitmapWidth * 255);
  243. //calculate the factor by which we multiply each component
  244. fAlphaFactor = cast(float)ubAlpha / cast(float)0xff;
  245. // multiply each pixel by fAlphaFactor, so each component
  246. // is less than or equal to the alpha value.
  247. (cast(UINT32*)pvBits)[x + y * ulBitmapWidth]
  248. = (ubAlpha << 24) | //0xaa000000
  249. (cast(UCHAR)(ubRed * fAlphaFactor) << 16) | //0x00rr0000
  250. (cast(UCHAR)(ubGreen * fAlphaFactor) << 8) | //0x0000gg00
  251. (cast(UCHAR)(ubBlue * fAlphaFactor)); //0x000000bb
  252. }
  253. }
  254. bf.BlendOp = AC_SRC_OVER;
  255. bf.BlendFlags = 0;
  256. bf.AlphaFormat = AC_SRC_ALPHA; // use source alpha
  257. bf.SourceConstantAlpha = 0xbf; // use constant alpha, with
  258. // 75% opaqueness
  259. GdiAlphaBlend(hdcwnd, ulWindowWidth / 5,
  260. ulWindowHeight / 5 + 2 * ulWindowHeight, ulBitmapWidth,
  261. ulBitmapHeight, hdc, 0, 0, ulBitmapWidth,
  262. ulBitmapHeight, bf);
  263. // do cleanup
  264. DeleteObject(hbitmap);
  265. DeleteDC(hdc);
  266. }