BYTE[]陣列轉化為灰度影象CBitimage顯示到mfc視窗中(opencv結果在mfc中的顯示)
阿新 • • 發佈:2019-02-07
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
#include "stdafx.h"
#include "ldi_dril.h"
/******************************************************************
* *
* DemoApp::DemoApp constructor *
* *
* Initialize member data. *
* *
******************************************************************/
DemoApp::DemoApp() :
m_hwnd(NULL),
m_pD2DFactory(NULL),
m_pRenderTarget(NULL),
m_pGDIRT(NULL),
m_pBlackBrush(NULL)
{
}
/******************************************************************
* *
* DemoApp::~DemoApp destructor *
* *
* Release resources. *
* *
******************************************************************/
DemoApp::~DemoApp()
{
SafeRelease(&m_pD2DFactory);
SafeRelease(&m_pRenderTarget);
SafeRelease(&m_pGDIRT);
SafeRelease(&m_pBlackBrush);
}
/******************************************************************
* *
* DemoApp::Initialize *
* *
* Create application window and device-independent resources. *
* *
******************************************************************/
HRESULT DemoApp::Initialize(HWND in_hwnd)
{
HRESULT hr;
m_hwnd = in_hwnd;
// Initialize device-indpendent resources, such
// as the Direct2D factory.
hr = CreateDeviceIndependentResources();
if (SUCCEEDED(hr))
{
hr = CreateDeviceResources();
}
return hr;
}
/******************************************************************
* *
* DemoApp::CreateDeviceIndependentResources *
* *
* This method is used to create resources which are not bound *
* to any device. Their lifetime effectively extends for the *
* duration of the app. *
* *
******************************************************************/
HRESULT DemoApp::CreateDeviceIndependentResources()
{
HRESULT hr = S_OK;
// Create a Direct2D factory.
hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&m_pD2DFactory
);
return hr;
}
/******************************************************************
* *
* DemoApp::CreateDeviceResources *
* *
* This method creates resources which are bound to a particular *
* D3D device. It's all centralized here, in case the resources *
* need to be recreated in case of D3D device loss (eg. display *
* change, remoting, removal of video card, etc). *
* *
******************************************************************/
HRESULT DemoApp::CreateDeviceResources()
{
HRESULT hr = S_OK;
if (!m_pRenderTarget)
{
RECT rc;
GetClientRect(m_hwnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU(
rc.right - rc.left,
rc.bottom - rc.top
);
D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties();
rtProps.usage = D2D1_RENDER_TARGET_USAGE_GDI_COMPATIBLE;
// Create a GDI compatible Hwnd render target.
hr = m_pD2DFactory->CreateHwndRenderTarget(
rtProps,
D2D1::HwndRenderTargetProperties(m_hwnd, size),
&m_pRenderTarget
);
if (SUCCEEDED(hr))
{
hr = m_pRenderTarget->QueryInterface(__uuidof(ID2D1GdiInteropRenderTarget), (void**)&m_pGDIRT);
}
if (SUCCEEDED(hr))
{
// Create a brush for drawing
hr = m_pRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Black),
&m_pBlackBrush
);
}
}
return hr;
}
/******************************************************************
* *
* DemoApp::DiscardDeviceResources *
* *
* Discard device-specific resources which need to be recreated *
* when a Direct3D device is lost. *
* *
******************************************************************/
void DemoApp::DiscardDeviceResources()
{
SafeRelease(&m_pRenderTarget);
SafeRelease(&m_pGDIRT);
SafeRelease(&m_pBlackBrush);
}
/******************************************************************
* *
* DemoApp::RunMessageLoop *
* *
* Main window message loop *
* *
******************************************************************/
void DemoApp::RunMessageLoop()
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
/******************************************************************
* *
* DemoApp::OnRender *
* *
* Called whenever the application needs to display the client *
* window. *
* Note that this function will not render anything if the window *
* is occluded (e.g. when the screen is locked). *
* Also, this function will automatically discard device-specific *
* resources if the Direct3D device disappears during function *
* invocation, and will recreate the resources the next time it's *
* invoked. *
* *
******************************************************************/
HRESULT DemoApp::OnRender()
{
HRESULT hr=S_OK;
if (SUCCEEDED(hr))
{
m_pRenderTarget->BeginDraw();
// Reset to the identity transform.
m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
// Clear the render target contents.
m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
m_pRenderTarget->DrawEllipse(
D2D1::Ellipse(
D2D1::Point2F(150.0f, 150.0f),
100.0f,
100.0f),
m_pBlackBrush,
3.0
);
m_pRenderTarget->DrawLine(
D2D1::Point2F(150.0f, 150.0f),
D2D1::Point2F(
(150.0f + 100.0f * 0.15425f),
(150.0f - 100.0f * 0.988f)),
m_pBlackBrush,
3.0
);
m_pRenderTarget->DrawLine(
D2D1::Point2F(150.0f, 150.0f),
D2D1::Point2F(
(150.0f + 100.0f * 0.525f),
(150.0f + 100.0f * 0.8509f)),
m_pBlackBrush,
3.0
);
m_pRenderTarget->DrawLine(
D2D1::Point2F(150.0f, 150.0f),
D2D1::Point2F(
(150.0f - 100.0f * 0.988f),
(150.0f - 100.0f * 0.15425f)),
m_pBlackBrush,
3.0
);
uchar m_src[128 * 96 * 4];
for (int i = 0; i < 128 * 96; i++)
{
m_src[i * 4 + 0] = 0;
m_src[i * 4 + 1] = 255;
m_src[i * 4 + 2] = 255;
m_src[i * 4 + 3] = 1;
}
CreateBitmpFromMemory(m_src, 128, 96, 512);
D2D1_RECT_F rectangle1 = D2D1::RectF(
128,
96,
256,
192
);
D2D1_RECT_F rectangle2 = D2D1::RectF(
0,
0,
128,
96
);
m_pRenderTarget->DrawBitmap(m_bitmap, &rectangle1, 1, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, &rectangle2);
m_pRenderTarget->EndDraw();
if (hr == D2DERR_RECREATE_TARGET)
{
hr = S_OK;
DiscardDeviceResources();
}
}
return hr;
}
// tranform function
BOOL DemoApp::CreateBitmpFromMemory(uchar * data, int w, int h, uint32_t pitch)
{
HRESULT hr = S_OK;
D2D1_SIZE_U size;
size.width = w;
size.height = h;
// Create a pixel format and initial its format
// and alphaMode fields.
//指定RGB格式
D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_IGNORE
);
//設定屬性
D2D1_BITMAP_PROPERTIES property;
property.pixelFormat = pixelFormat;
property.dpiX = 0;
property.dpiY = 0;
// Create a Direct2D bitmap from the memory.
hr = m_pRenderTarget->CreateBitmap(
size,
data,
pitch,
property,
&m_bitmap
);
if (SUCCEEDED(hr))
{
return TRUE;
}
return FALSE;
}