【问题标题】:How to iterate over a subset of row-aligned pixels如何迭代行对齐像素的子集
【发布时间】:2021-10-02 16:24:22
【问题描述】:

我有一个来自行对齐的 PNG 的 BGRA 数据缓冲区。

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

我有兴趣对图像中的子图像进行采样。子图像由原点和高度和宽度定义:

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

我正在像这样走子图像::

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++;
    }
}

但是,我不想遍历子图像中的每个像素,而是每 n 个像素遍历一次。例如,在查看一个 3x3 子图像时,该子图像起源于这个 5x5 图像的 0,0,我想遍历每个第二个像素:

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

但我想不出一个好的方法来做到这一点。有什么想法吗?尽快完成这项工作很重要。

【问题讨论】:

  • 不应该只是将++i 更改为i+=n 就足够了吗?

标签: c++ png x11


【解决方案1】:

如果不进行测试,很难说什么是更快或更慢,但平面索引可以解决问题:

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';
}

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

【讨论】:

  • 小修正,我认为应该是uint i = idx%reg.width + reg.x, j = idx/reg.width + reg.y;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多