如何使用Direct2D处理像素 [英] How to work with pixels using Direct2D

查看:222
本文介绍了如何使用Direct2D处理像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供一个使用Direct2D处理像素的有效方法的示例吗?

例如,如何在渲染目标上将所有绿色像素(RGB = 0x00FF00)与红色像素(RGB = 0xFF0000)交换?标准方法是什么?可以使用ID2D1HwndRenderTarget吗?在这里,我假设使用某种硬件加速.我应该为直接像素操作创建其他对象吗?

使用DirectDraw我将在IDirectDrawSurface7上使用BltFast方法进行逻辑操作. Direct2D有类似的东西吗?

另一项任务是动态生成复杂的图像,其中每个点的位置和颜色都是数学函数的结果.为了举例说明,让我们简化所有内容并绘制Y = X ^ 2.如何使用Direct2D做到这一点?最终,我将需要绘制复杂的函数,但是如果有人可以给我一个Y = X ^ 2的简单示例.

解决方案

首先,将ID2D1Bitmap视为设备位图"会有所帮助.它可能存在或可能不存在于本地的可寻址CPU的内存中,并且它无法为您提供从总线CPU端读取/写入像素的任何便捷(或至少快速)的方式.因此,从这个角度出发可能是错误的做法.

我想您想要的是一个常规的WIC位图IWICBitmap,您可以使用IWICImagingFactory :: CreateBitmap()创建它.从那里,您可以调用Lock()来获取缓冲区,然后使用指针进行读取/写入并执行您想要的任何操作.然后,当您需要使用Direct2D在屏幕上绘制它时,请使用ID2D1RenderTarget :: CreateBitmap()创建一个新的设备位图,或使用ID2D1Bitmap :: CopyFromMemory()来更新一个现有的设备位图.您也可以通过使用ID2D1Factory :: CreateWicBitmapRenderTarget()渲染为IWICBitmap(不是硬件加速的).

对于这些类型的操作,您将不会获得硬件加速. Win8中更新的Direct2D(最终也应该在Win7中可用)对此有一些吸引人的功能,但外观相当复杂.

Could somebody provide an example of an efficient way to work with pixels using Direct2D?

For example, how can I swap all green pixels (RGB = 0x00FF00) with red pixels (RGB = 0xFF0000) on a render target? What is the standard approach? Is it possible to use ID2D1HwndRenderTarget for that? Here I assume using some kind of hardware acceleration. Should I create a different object for direct pixels manipulations?

Using DirectDraw I would use BltFast method on the IDirectDrawSurface7 with logical operation. Is there something similar with Direct2D?

Another task is to generate complex images dynamically where each point location and color is a result of a mathematical function. For the sake of an example let's simplify everything and draw Y = X ^ 2. How to do that with Direct2D? Ultimately I'm going to need to draw complex functions but if somebody could give me a simple example for Y = X ^ 2.

解决方案

First, it helps to think of ID2D1Bitmap as a "device bitmap". It may or may not live in local, CPU-addressable memory, and it doesn't give you any convenient (or at least fast) way to read/write the pixels from the CPU side of the bus. So approaching from that angle is probably the wrong approach.

What I think you want is a regular WIC bitmap, IWICBitmap, which you can create with IWICImagingFactory::CreateBitmap(). From there you can call Lock() to get at the buffer, and then read/write using pointers and do whatever you want. Then, when you need to draw it on-screen with Direct2D, use ID2D1RenderTarget::CreateBitmap() to create a new device bitmap, or ID2D1Bitmap::CopyFromMemory() to update an existing device bitmap. You can also render into an IWICBitmap by making use of ID2D1Factory::CreateWicBitmapRenderTarget() (not hardware accelerated).

You will not get hardware acceleration for these types of operations. The updated Direct2D in Win8 (should also be available for Win7 eventually) has some spiffy stuff for this but it's rather complex looking.

这篇关于如何使用Direct2D处理像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆