I have a buffer of BGRA data from a PNG that's row-aligned.
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. 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:
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