Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*------------------------------------------------------------------------
- HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
- (c) Charles Petzold, 1998
- -----------------------------------------------------------------------*/
- # include <windows.h>
- # include <d3d9.h>
- # include <d3dx9.h>
- # include <assert.h>
- # include <stdio.h>
- # include <stdint.h>
- static IDirect3D9 *sm_Direct3D9_root=0;
- static IDirect3DDevice9 *sm_Direct3D9_device = NULL; /* device object */
- static IDirect3DSurface9 *sm_Direct3D9_surface = NULL; /* vram object */
- static IDirect3DTexture9 *sm_Direct3D9_texture = NULL;
- static int img_h = 0;
- static int img_w = 0;
- extern "C"
- void __cdecl bilinear_x (DWORD *dstPtr, intptr_t dstPitch,
- DWORD *srcPtr, intptr_t srcPitch,
- intptr_t dstW, intptr_t dstH,
- intptr_t srcW, intptr_t srcH) ;
- # define FILENAME "C:\\Users\\nozaki\\Desktop\\main.bmp"
- # define MOD_SHIFT_ 11
- # define MOD_MAPPER_ ( (1 << MOD_SHIFT_))/* MOD_MAPPER is 2^n*/
- # define MOD_SHIFT_MUL_ (MOD_MAPPER_*MOD_MAPPER_)
- DWORD AlphaRGB32_S (DWORD RGB32, DWORD max, DWORD cur) {
- DWORD D0 = RGB32 & 0xFF;
- DWORD D1 = RGB32 & 0xFF00;
- DWORD D2 = RGB32 & 0xFF0000;
- D0 >>= 0;
- D1 >>= 8;
- D2 >>= 16;
- D0 *= cur;
- D1 *= cur;
- D2 *= cur;
- D0 /= max;
- D1 /= max;
- D2 /= max;
- D0 &= 0xFF;
- D1 &= 0xFF;
- D2 &= 0xFF;
- D0 <<= 0;
- D1 <<= 8;
- D2 <<= 16;
- return (D0 | D1 | D2);
- }
- DWORD SuratADD (DWORD U, DWORD V) {
- DWORD U0 = U & 0xFF;
- DWORD U1 = U & 0xFF00;
- DWORD U2 = U & 0xFF0000;
- DWORD V0 = V & 0xFF;
- DWORD V1 = V & 0xFF00;
- DWORD V2 = V & 0xFF0000;
- U0 += V0;
- U1 += V1;
- U2 += V2;
- if (U0 > 0xFF) U0 = 0xFF;
- if (U1 > 0xFF00) U1 = 0xFF00;
- if (U2 > 0xFF0000) U2 = 0xFF0000;
- return (U0 | U1 | U2);
- }
- # if 1
- void Bilinear (DWORD *pDstVPTR, DWORD dwDstPitch,
- DWORD *pSrcVPTR, DWORD dwSrcPitch,
- DWORD dwDstWidth, DWORD dwDstHeight,
- DWORD dwSrcWidth, DWORD dwSrcHeight)
- {
- DWORD dwMapX = (DWORD) ( ((float)MOD_MAPPER_) * (( (float) dwSrcWidth) / ( (float) dwDstWidth) )); // 2->3
- DWORD dwMapY = (DWORD) ( ((float)MOD_MAPPER_) * (( (float) dwSrcHeight) / ( (float) dwDstHeight))); // 2->3
- DWORD dwPosInSrcY = 0;
- DWORD dwPosInSrcX = 0;
- DWORD dwRTInSrcY;
- DWORD dwRTInSrcX;
- DWORD dwsPit = dwSrcPitch >> 2;
- DWORD dwdPit = dwDstPitch >> 2;
- DWORD dwMapTotal_X = 0;
- DWORD dwMapTotal_Y = 0;
- DWORD dwRVADst = dwDstPitch - dwDstWidth *4;
- DWORD dwCahceScanLine_Y = 0;
- DWORD dwCahceScanLine_Y2 = 0;
- for (DWORD dwScanY = 0; dwScanY != dwDstHeight; dwScanY++) {
- dwPosInSrcY = dwMapTotal_Y >> MOD_SHIFT_;
- dwRTInSrcY = dwMapTotal_Y & (MOD_MAPPER_ - 1);
- dwMapTotal_X = 0;
- dwCahceScanLine_Y = dwsPit*dwPosInSrcY;
- dwCahceScanLine_Y2 = dwdPit*dwScanY;
- for (DWORD dwScanX = 0; dwScanX != dwDstWidth; dwScanX++) {
- // fetch top-left, top-right, botton-left, bottom-right
- // source sample .
- dwPosInSrcX = dwMapTotal_X >> MOD_SHIFT_;
- dwRTInSrcX = dwMapTotal_X & (MOD_MAPPER_ - 1);
- const DWORD temp = dwCahceScanLine_Y+dwPosInSrcX;
- DWORD cpTopLeft = pSrcVPTR[temp];
- DWORD cpTopRight = pSrcVPTR[temp+1];
- DWORD cpBottomLeft = pSrcVPTR[temp+dwsPit];
- DWORD cpBottomRight = pSrcVPTR[temp+dwsPit+1];
- // Get X, Y ratio .
- DWORD apLeft = dwRTInSrcX;
- DWORD apRight = MOD_MAPPER_ - apLeft;
- DWORD apTop = dwRTInSrcY;
- DWORD apBottom = MOD_MAPPER_ - apTop;
- // Get Weighted ratio
- DWORD wtTopLeft = apRight * apBottom;
- DWORD wtTopRight = apLeft * apBottom;
- DWORD wtBottomLeft = apRight * apTop;
- DWORD wtBottomRight = apLeft * apTop;
- // Get RGB percent. Write Into Buffer.
- DWORD dbTopLeft = AlphaRGB32_S (cpTopLeft, MOD_MAPPER_, apRight);
- DWORD dbTopRight = AlphaRGB32_S (cpTopRight, MOD_MAPPER_, dwRTInSrcX);
- DWORD dbBottomLeft = AlphaRGB32_S (cpBottomLeft, MOD_SHIFT_MUL_, wtBottomLeft);
- DWORD dbBottomRight = AlphaRGB32_S (cpBottomRight, MOD_SHIFT_MUL_, wtBottomRight);
- DWORD dbTopMix = SuratADD ( dbTopLeft, dbTopRight);
- DWORD dbBottomMix = SuratADD ( dbBottomLeft, dbBottomRight);
- DWORD dbOutputMix = SuratADD ( dbTopMix, dbBottomMix);
- pDstVPTR[dwCahceScanLine_Y2+dwScanX] = dbTopMix;
- dwMapTotal_X += dwMapX;
- }
- dwMapTotal_Y += dwMapY;
- }
- }
- # else
- void zoom_sc (
- /* des pointer */ DWORD *d,
- /* src pointer */ DWORD *s,
- /* des pitch */ DWORD dp,
- /* src pitch */ DWORD sp,
- /* width */ DWORD w,
- /* height */ DWORD h,
- /* zoom count */ float cx, float cy)
- {
- #define GET_B(x)((float)(((x) & 0x000000FF) >> 0))
- #define GET_G(x)((float)(((x) & 0x0000FF00) >> 8))
- #define GET_R(x)((float)(((x) & 0x00FF0000) >> 16))
- DWORD X;
- DWORD Y;
- DWORD NewWidth = (DWORD)(cx * (float)w);
- DWORD NewHeight = (DWORD)(cy * (float)h);
- DWORD DesPitch;
- DWORD SrcPitch;
- DWORD BPosTX;
- DWORD BPosTY;
- DWORD TLeft;
- DWORD TRight;
- DWORD BLeft;
- DWORD BRight;
- DWORD R;
- DWORD G;
- DWORD B;
- float RevseRatioX;
- float RevseRatioY;
- float XOffsetRatio;
- float YOffsetRatio;
- float r0;
- float r1;
- float r2;
- float r3;
- if (!NewWidth || !NewHeight)
- return;
- RevseRatioX = 1.0/cx;
- RevseRatioY = 1.0/cy;
- SrcPitch = sp >> 2;
- DesPitch = dp >> 2;
- for (Y = 0; Y != NewHeight; Y ++) {
- YOffsetRatio = (RevseRatioY * (float)Y);
- BPosTY = (DWORD)YOffsetRatio;
- YOffsetRatio-=(float)BPosTY;
- for (X = 0; X != NewWidth; X ++) {
- XOffsetRatio = RevseRatioX * (float)X;
- BPosTX = (DWORD)XOffsetRatio;
- XOffsetRatio-=(float)BPosTX;
- TLeft = s[BPosTX + BPosTY * SrcPitch + 0];
- TRight= s[BPosTX + BPosTY * SrcPitch + 1];
- BLeft = s[BPosTX + BPosTY * SrcPitch + SrcPitch + 0];
- BRight= s[BPosTX + BPosTY * SrcPitch + SrcPitch + 1];
- r3 = XOffsetRatio * YOffsetRatio;
- r2 = (1.0 - XOffsetRatio) * YOffsetRatio;
- r1 = (1.0 - YOffsetRatio) * XOffsetRatio;
- r0 = (1.0 - XOffsetRatio) * (1.0 - YOffsetRatio);
- B = (DWORD)(((GET_B(TLeft) * r0)) + ((GET_B(TRight) * r1)) + ((GET_B(BLeft) * r2)) + ((GET_B(BRight) * r3))) & 255;
- G = (DWORD)(((GET_G(TLeft) * r0)) + ((GET_G(TRight) * r1)) + ((GET_G(BLeft) * r2)) + ((GET_G(BRight) * r3))) & 255;
- R = (DWORD)(((GET_R(TLeft) * r0)) + ((GET_R(TRight) * r1)) + ((GET_R(BLeft) * r2)) + ((GET_R(BRight) * r3))) & 255;
- d[X + Y * DesPitch] = ((R << 16) | (G << 8) | B);
- }
- }
- }
- void Bilinear (DWORD *pDstVPTR, DWORD dwDstPitch,
- DWORD *pSrcVPTR, DWORD dwSrcPitch,
- DWORD dwDstWidth, DWORD dwDstHeight,
- DWORD dwSrcWidth, DWORD dwSrcHeight)
- {
- zoom_sc (
- /* des pointer */ pDstVPTR,
- /* src pointer */ pSrcVPTR,
- /* des pitch */ dwDstPitch,
- /* src pitch */ dwSrcPitch,
- /* width */ dwSrcWidth,
- /* height */ dwSrcHeight,
- /* zoom count */ (float) dwDstWidth / (float) dwSrcWidth,
- (float) dwDstHeight / (float) dwSrcHeight
- );
- }
- # endif
- void LoadImage (void) {
- D3DXIMAGE_INFO t;
- D3DXCreateTextureFromFileExA (sm_Direct3D9_device, FILENAME,
- D3DX_DEFAULT_NONPOW2,
- D3DX_DEFAULT_NONPOW2,
- D3DX_FROM_FILE,
- 0,
- D3DFMT_FROM_FILE,
- D3DPOOL_MANAGED,
- D3DX_DEFAULT, // D3DX_DEFAULT
- D3DX_DEFAULT, // D3DX_DEFAULT
- 0, //D3DCOLOR_XRGB (0, 0, 0),
- & t, NULL, & sm_Direct3D9_texture);
- img_w = t.Width;
- img_h = t.Height;
- }
- void ResetD3dpp (D3DPRESENT_PARAMETERS &d3dpp, HWND window, int width, int height) {
- ZeroMemory (& d3dpp, sizeof (d3dpp));
- // d3dpp.BackBufferFormat = D3DFMT_X1R5G5B5;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.Flags = 0;
- d3dpp.hDeviceWindow = window;
- d3dpp.Windowed = TRUE;
- d3dpp.BackBufferWidth = GetSystemMetrics (SM_CXSCREEN);
- d3dpp.BackBufferHeight = GetSystemMetrics (SM_CYSCREEN);
- d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; /* Disable VSync */
- }
- LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
- int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
- PSTR szCmdLine, int iCmdShow)
- {
- static TCHAR szAppName[] = TEXT ("HelloWin") ;
- HWND hwnd ;
- MSG msg ;
- WNDCLASS wndclass ;
- wndclass.style = CS_HREDRAW | CS_VREDRAW ;
- wndclass.lpfnWndProc = WndProc ;
- wndclass.cbClsExtra = 0 ;
- wndclass.cbWndExtra = 0 ;
- wndclass.hInstance = hInstance ;
- wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
- wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
- wndclass.lpszMenuName = NULL ;
- wndclass.lpszClassName = szAppName ;
- if (!RegisterClass (&wndclass)) {
- MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
- szAppName, MB_ICONERROR) ;
- return 0 ;
- }
- hwnd = CreateWindow( szAppName, // window class name
- TEXT ("The Hello Program"), // window caption
- WS_OVERLAPPEDWINDOW, // window style
- CW_USEDEFAULT, // initial x position
- CW_USEDEFAULT, // initial y position
- CW_USEDEFAULT, // initial x size
- CW_USEDEFAULT, // initial y size
- NULL, // parent window handle
- NULL, // window menu handle
- hInstance, // program instance handle
- NULL) ; // creation parameters
- // create direct3d 9
- sm_Direct3D9_root = Direct3DCreate9 (DIRECT3D_VERSION);
- assert (sm_Direct3D9_root != NULL);
- // create device
- D3DPRESENT_PARAMETERS d3dpp;
- ResetD3dpp (d3dpp, hwnd, 0, 0);
- HRESULT si = sm_Direct3D9_root->CreateDevice (0,
- D3DDEVTYPE_HAL,
- hwnd,
- D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, & sm_Direct3D9_device);
- assert (SUCCEEDED (si));
- sm_Direct3D9_device->CreateOffscreenPlainSurface (GetSystemMetrics (SM_CXSCREEN),
- GetSystemMetrics (SM_CYSCREEN), D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM,
- & sm_Direct3D9_surface, NULL);
- LoadImage ();
- ShowWindow (hwnd, iCmdShow) ;
- UpdateWindow (hwnd) ;
- D3DLOCKED_RECT lock_rect2;
- sm_Direct3D9_texture->LockRect (0, & lock_rect2, NULL, 0);
- while (TRUE)
- {
- static DWORD t = 0;
- static DWORD ticks = 0;
- if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
- {
- if (msg.message == WM_QUIT)
- break ;
- TranslateMessage (&msg) ;
- DispatchMessage (&msg) ;
- }
- else
- {
- DWORD n = timeGetTime ();
- RECT rcClient;
- D3DLOCKED_RECT lock_rect;
- IDirect3DSurface9 *surface_temp;
- GetClientRect (hwnd, & rcClient);
- int w = abs (rcClient.left - rcClient.right);
- int h = abs (rcClient.bottom - rcClient.top);
- if (w <= 0 || h <= 0)
- return 0;
- si = sm_Direct3D9_device->GetBackBuffer (0, 0, D3DBACKBUFFER_TYPE_MONO, & surface_temp);
- assert (SUCCEEDED (si));
- si = sm_Direct3D9_surface->LockRect (& lock_rect, & rcClient, D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
- assert (SUCCEEDED (si));
- // bilinear entryu .
- bilinear_x ((DWORD *)lock_rect.pBits, lock_rect.Pitch,
- (DWORD *)lock_rect2.pBits, lock_rect2.Pitch,
- rcClient.right - rcClient.left,
- rcClient.bottom - rcClient.top, img_w, img_h);
- sm_Direct3D9_surface->UnlockRect ();
- sm_Direct3D9_device->UpdateSurface (sm_Direct3D9_surface, & rcClient, surface_temp, NULL);
- sm_Direct3D9_device->Present (& rcClient, NULL, NULL, NULL);
- printf ("Present::frame:%d!!!!\n", timeGetTime () - n);
- }
- }
- sm_Direct3D9_texture->UnlockRect (0);
- sm_Direct3D9_device->Release ();
- sm_Direct3D9_root->Release ();
- return msg.wParam ;
- }
- LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- HDC hdc ;
- PAINTSTRUCT ps ;
- switch (message)
- {
- case WM_PAINT:
- hdc = BeginPaint (hwnd, &ps) ;
- EndPaint (hwnd, &ps) ;
- return 0 ;
- case WM_DESTROY:
- PostQuitMessage (0) ;
- return 0 ;
- }
- return DefWindowProc (hwnd, message, wParam, lParam) ;
- }
- #include <smmintrin.h>
- int main (void) {
- _CRT_ALIGN(16) BYTE s[128];
- //__asm pshufw mm2, [s+1], 3
- WinMain (GetModuleHandle (NULL), NULL, NULL, SW_SHOWNORMAL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement