c++ - 将效果应用于输出而不是位图?

标签 c++ effects direct2d

假设我有一个高斯模糊效果 ID2D1Effect *blur;,我想对效果应用位图以获得模糊图像。我可以使用 blur->SetInput(0, bitmap); 将位图传递给效果,并使用 blur->Get() 获取输出。现在我不想模糊存储在磁盘上的图像,我想模糊我在 renderTarget->BeginDraw()renderTarget->EndDraw() 之间创建的输出>。我的第一个想法是获取输出的位图,但我找不到用于检索已渲染输出的函数,也找不到将效果应用于 renderTarget 而不是加载位图的方法。

小例子:

ID2D1SolidColorBrush *brush;
ID2D1HwndRenderTarget *renderTarget; // Instantiated somewhere else in code

renderTarget->CreateSolidColorBrush(D2D1::ColorF(0.0f, 0.6f, 1.0f), &bush);

renderTarget->BeginDraw();
renderTarget->Clear(D2D1::ColorF(0.0f, 0.0f, 0.0f));
renderTarget->FillRectangle(D2D1::RectF(50.0f, 50.0f, 100.0f, 100.0f), brush);
// Apply gaussian blur?
renderTarget->EndDraw();

brush->Release();
// renderTarget gets released somewhere else

如何模糊本例中的矩形?


编辑(感谢enhzflep):

我可以使用以下代码段将已呈现的输出作为位图获取:

ID2D1BitmapRenderTarget *bitmapRenderTarget;
hwndRenderTarget->CreateCompatibleRenderTarget(&bitmapRenderTarget);

ID2D1Bitmap *bitmap;
bitmapRenderTarget->GetBitmap(&bitmap);

在我的代码中,我已经通过获取位图、在其上绘制并最终将此位图绘制到 hwndRenderTarget 上对其进行了测试,并且成功了。在将位图绘制回 hwndRenderTarget 之前,我想对其应用效果(例如高斯模糊)。但这导致了下一个问题(我不知道是否应该开一个新主题,所以我在这里发布):一个ID2D1Effect接口(interface)只能从ID2D1DeviceContext的实例实例化 仅适用于 Windows 8。我无法想象 Microsoft 将 ID2D1Effect 限制为 Windows 8,因此必须有另一种方式。 有人知道另一种实例化 ID2D1Effect 接口(interface)的方法吗?


编辑 2:

我也在 MSDN 上发布了这个问题,最后得到了一个 answer .我必须安装 Platform Update for Win7 SP1 (KB2670838)Win8 SDK (也适用于 Win7)以便在 Win7 上使用 Win8 header 。

(下面的最终解决方案作为答案)

最佳答案

这是我的最终解决方案。它比我想象的要简单:

// Obtain already initialized ID2D1HwndRenderTarget
ID2D1HwndRenderTarget *hwndRenderTarget = Game::GetInstance()->gfx->hwndRenderTarget;

// COMs
ID2D1Bitmap *bitmap;
ID2D1BitmapRenderTarget *bitmapRenderTarget;
ID2D1DeviceContext *deviceContext;
ID2D1Effect *gaussianBlur;
ID2D1SolidColorBrush *solidColorBrush;

// Create bitmapRenderTarget
hwndRenderTarget->CreateCompatibleRenderTarget(&bitmapRenderTarget);

// Create solidColorBrush
bitmapRenderTarget->CreateSolidColorBrush(D2D1::ColorF(0.0f, 0.6f, 1.0f), &solidColorBrush);

// Draw onto bitmapRenderTarget
bitmapRenderTarget->BeginDraw();
bitmapRenderTarget->Clear(D2D1::ColorF(0.0f, 0.0f, 0.0f));
bitmapRenderTarget->FillEllipse(D2D1::Ellipse(D2D1::Point2F(400.0f, 300.0f), 100.0f, 100.0f), solidColorBrush);
bitmapRenderTarget->EndDraw();

// Obtain hwndRenderTarget's deviceContext
hwndRenderTarget->QueryInterface(&deviceContext);

// Create and apply gaussian blur
deviceContext->CreateEffect(CLSID_D2D1GaussianBlur, &gaussianBlur);

bitmapRenderTarget->GetBitmap(&bitmap);
gaussianBlur->SetInput(0, bitmap);
gaussianBlur->SetValue(D2D1_GAUSSIANBLUR_PROP_BORDER_MODE, D2D1_BORDER_MODE_SOFT);
gaussianBlur->SetValue(D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION, 5.0f);

// Draw resulting bitmap
deviceContext->DrawImage(
    gaussianBlur,
    D2D1_INTERPOLATION_MODE_LINEAR);

// Release
bitmap->Release();
bitmapRenderTarget->Release();
deviceContext->Release();
gaussianBlur->Release();
solidColorBrush->Release();

关于c++ - 将效果应用于输出而不是位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26689110/

相关文章:

javascript - 在 Javascript 中退出 setInterval

c++ - 在 direct2D 中释放渐变画笔时可能会发生内存泄漏?

language-agnostic - 2D运动模糊解决方案

JavaScript,html5 : how to dynamically create a Vignette effect?

c++ - 在 C++ 中使用引用和指针

c++ - 如何使用 g++47 允许 -z multidefs

c++ - 如何防止direct2d “stretching”窗口大小改变时的 View ?

c++ - Windows API 从文件渲染图像(初学者)

c++ - 避免使用派生类进行 dynamic_cast (Cast Derived class)

c++ - 如何在 QPainter 上获取渲染文本的大小?