简体   繁体   English

如何迭代行对齐像素的子集

[英]How to iterate over a subset of row-aligned pixels

I have a buffer of BGRA data from a PNG that's row-aligned.我有一个来自行对齐的 PNG 的 BGRA 数据缓冲区。

unsigned int *data_; // bgra buffer described by 
                     // image_.ximage_->width and 
                     // image_.ximage_->height

I'm interested in sampling a subimage within the image.我有兴趣在图像中采样子图像。 The subimage is defined by an origin and height and width:子图像由原点和高度和宽度定义:

struct Box {
        unsigned int x_;
        unsigned int y_;
        unsigned int width_;
        unsigned int height_;
};

I'm walking the subimage something like this::我正在走这样的子图像::

unsigned int *origin, p;
origin = data_ + ((image_.ximage_->width * box.y_) + box.x_);
for( unsigned int j = 0 ; j < box.height_; ++j )
{
    p = origin + (image_.ximage_->width * j);
    for( unsigned int i = 0 ; i < box.width_; ++i )
    {
        p++;
    }
}

But instead of walking every pixel in the subimage what I'd like to do is step through every n pixels.但是我想做的不是遍历子图像中的每个像素,而是遍历每 n 个像素。 For example, in looking at a 3x3 subimage originating at 0,0 of this 5x5 image, I'd like to walk through every second pixel:例如,在查看起源于该 5x5 图像的 0,0 处的 3x3 子图像时,我想每隔一个像素遍历一次:

X _ X _ _
_ X _ _ _
X _ X _ _
_ _ _ _ _
_ _ _ _ _

But I can't think of a good way to do that.但我想不出一个好的方法来做到这一点。 Any thoughts?有什么想法吗? It's important that I do this as fast as possible.重要的是我尽可能快地做这件事。

Hard to say what's faster or slower without testing, but a flat index would do the trick:未经测试很难说更快或更慢,但平坦的索引可以解决问题:

for (uint idx = 0; idx<reg.height*reg.width; idx+=step) {
    uint i = idx%reg.height + reg.x, j = idx/reg.width + reg.y;
    img(i,j) = 'X';
}

Testable here: https://godbolt.org/z/7jjGWj8rn可在此处测试: https : //godbolt.org/z/7jjGWj8rn

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

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