简体   繁体   English

在 C++ 中查找像素 RGB 数据的最快方法是什么?

[英]What is the fastest method to lookup pixel RGB data in C++?

I want to look at and store in an array the RGB data of each pixel in a 100 X 100 square of my screen.我想查看屏幕上 100 X 100 正方形中每个像素的 RGB 数据并将其存储在数组中。 I got this concept working using GetPixel() in a for loop, and then converting the output to hexadecimal and storing the RGB data in an array, but this method takes much longer than I'd like.我在 for 循环中使用 GetPixel() 得到了这个概念,然后将输出转换为十六进制并将 RGB 数据存储在数组中,但这种方法需要的时间比我想要的要长得多。 What is the fastest way of reading pixel RGB data with c++?用 C++ 读取像素 RGB 数据的最快方法是什么?

Yes, GetPixel and SetPixel are relatively slow API calls.是的, GetPixelSetPixel是相对较慢的 API 调用。 Manipulating individual pixels requires copying the contents of the DC (Device Context) into a temporary bitmap, mapping and locating the pixel, retrieving (or setting) its color value, and then (if the pixel's color is being set) blitting the temporary bitmap back to the device context.操作单个像素需要将 DC(设备上下文)的内容复制到临时位图、映射和定位像素、检索(或设置)其颜色值,然后(如果正在设置像素的颜色)将临时位图传回到设备上下文。 Once you understand all the work that is going on behind the scenes for such an apparently simple operation, you can see why it is slow.一旦您了解了为这样一个看似简单的操作在幕后进行的所有工作,您就会明白为什么它很慢。 Worse, the overhead must be paid for each call to GetPixel and/or SetPixel , so you can see why these are not commonly used for painting operations.更糟糕的是,必须为每次调用GetPixel和/或SetPixel支付开销,因此您可以看到为什么这些不常用于绘制操作。

The replacement approach involves the use of GetDIBits , which is the fastest possible way of determining the values of multiple pixels.替换方法涉及使用GetDIBits ,这是确定多个像素值的最快方法。 This function essentially copies the contents of the device context (DC) into a temporary device-independent bitmap (DIB), which you can then view or manipulate as desired.该函数本质上是将设备上下文 (DC) 的内容复制到一个临时的设备无关位图 (DIB) 中,然后您可以根据需要查看或操作该位图。 Determining the color value of an individual pixel becomes as simple as indexing into that pixel's location in memory.确定单个像素的颜色值就像索引该像素在内存中的位置一样简单。 With this approach, you can literally process millions of pixels per second.使用这种方法,您可以每秒处理数百万个像素。 The overhead of retrieving the pixel color values is paid only once, at the very beginning.检索像素颜色值的开销仅在开始时支付一次。

To simulate SetPixel , you'd manipulate the desired pixel values, then call SetDIBits to update the device context (eg, the screen).要模拟SetPixel ,您需要操作所需的像素值,然后调用SetDIBits来更新设备上下文(例如,屏幕)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM