我正在尝试编写 OpenCV 的 cv::resize() 的更好版本,我遇到了一个交叉代码:https ://github.com/rmaz/NEON-Image-Downscaling/blob/master /ImageResize/BDPViewController.m 该代码用于将图像下采样 2,但我无法获得算法。我想首先将该算法转换为 C 然后尝试修改它以用于学习目的。将其转换为任何大小的下采样是否也很容易?
功能是:
static void inline resizeRow(uint32_t *dst, uint32_t *src, uint32_t pixelsPerRow)
{
const uint32_t * rowB = src + pixelsPerRow;
// force the number of pixels per row to a multiple of 8
pixelsPerRow = 8 * (pixelsPerRow / 8);
__asm__ volatile("Lresizeloop: \n" // start loop
"vld1.32 {d0-d3}, [%1]! \n" // load 8 pixels from the top row
"vld1.32 {d4-d7}, [%2]! \n" // load 8 pixels from the bottom row
"vhadd.u8 q0, q0, q2 \n" // average the pixels vertically
"vhadd.u8 q1, q1, q3 \n"
"vtrn.32 q0, q2 \n" // transpose to put the horizontally adjacent pixels in different registers
"vtrn.32 q1, q3 \n"
"vhadd.u8 q0, q0, q2 \n" // average the pixels horizontally
"vhadd.u8 q1, q1, q3 \n"
"vtrn.32 d0, d1 \n" // fill the registers with pixels
"vtrn.32 d2, d3 \n"
"vswp d1, d2 \n"
"vst1.64 {d0-d1}, [%0]! \n" // store the result
"subs %3, %3, #8 \n" // subtract 8 from the pixel count
"bne Lresizeloop \n" // repeat until the row is complete
: "=r"(dst), "=r"(src), "=r"(rowB), "=r"(pixelsPerRow)
: "0"(dst), "1"(src), "2"(rowB), "3"(pixelsPerRow)
: "q0", "q1", "q2", "q3", "cc"
);
}
To call it:
// downscale the image in place
for (size_t rowIndex = 0; rowIndex < height; rowIndex+=2)
{
void *sourceRow = (uint8_t *)buffer + rowIndex * bytesPerRow;
void *destRow = (uint8_t *)buffer + (rowIndex / 2) * bytesPerRow;
resizeRow(destRow, sourceRow, width);
}