In CVB C++, there are multiple ways to access pixel data:
When an image is stored in a contiguous memory layout, pixels can be accessed most efficiently through array access, which allows for direct memory access without any gaps between rows. To achieve this optimal access pattern, it is recommended to use array access whenever possible, as demonstrated in the following code example:
(1) A monochrome 16-bit unsigned image is created. For monochrome images, the Create
function generates an image with a contiguous layout.
(2) Array access is obtained for the first (contiguous) image plane.
(3) If the array access object is valid (i.e., array access was successful), proceed with pixel access.
(4) arrayAccess(x, y)
returns a void pointer to the pixel. Cast it to the correct type.
If array access is not available, the next best option is to use linear access:
If neither array access nor linear access is possible, the last resort is to use the slowest method: accessing the image via VPAT.
When neither array access nor linear access is available, the next option is to access the image through the slower method: VPAT-based access, as described in VPAT Memory Layout.
(1) Obtain VPA table.
(2) vpat(x, y)
returns a void pointer to the pixel. Cast it to the correct type.
CVB provides iterators for each access type, allowing you to iterate over pixel data efficiently. The following code snippet demonstrates how to use iterators with different access methods:
(1) First, attempt to obtain the most efficient contiguous access and use iterators to traverse the pixels in contiguous memory.
(2) If contiguous access is unavailable, attempt linear access and use iterators for linear iteration.
(3) If both fast access methods fail, iterators can be used for VPAT access, the slowest method.
In practice, your code may need to support different data types and memory layouts. To minimize the overhead of handling these variations, CVB++ provides modern C++ infrastructure that enables you to write algorithms only once. The most efficient access pattern (contiguous > linear > vpat) is automatically selected. This can be achieved using Cvb::Visit(), which provides an efficient and convenient way to access images and point clouds.
The following example demonstrates a simple case of visiting a single image plane using a lambda function. More advanced examples, including the use of function objects and simultaneous access to multiple planes, can be found in Efficiently write image and point cloud algorithms using Visit.
Cvb::Vist
takes one or more image planes as input, along with a lambda function or a function object that defines the operation to be performed on each image pixel. The only requirement is that the lambda function must be able to handle a Cvb::Block object.