Well, this post is the reason for Why is memcpy() and memmove() faster than pointer increments?
I have a custom image structure with 4 channels and I like to extract 3 of them (RGB) into an openCV cv::MAt strcuture with 3 channels.
cv::Mat GetColorImgFromFrame(Image* pimg)
{
int iHeight = pimg->m_imageInfo.m_height;
int iWidth = pimg->m_imageInfo.m_width;
cv::Mat cvmColorImg = cv::Mat::zeros(iHeight, iWidth, CV_8UC3);
int iDestStep = cvmColorImg.channels(); // 3
uchar* pucSrc = pimg->m_data;
uchar* pucDest = cvmColorImg.data;
uchar* pucDestLimit = cvmColorImg.data + iHeight*iWidth*iDestStep;
for (;pucDest < pucDestLimit;)
{
*(pucDest++) = *(pucSrc++);
*(pucDest++) = *(pucSrc++);
*(pucDest++) = *(pucSrc++);
pucSrc++;
}
return cvmColorImg;
}
It doesn’t seem much helpful to replace the three inner assignments with memcpy(pucDest, pucSrc, 3). Any suggestions?
Before going into “assembler like” mode by using SIMD intrinsic, you can try to tell your compiler that the source and destination pointer do not alias, using for example the
__restrict__keyword with gcc or__restrictwith visual studio