我目前正在使用这种方法复制一些字节值:
for (int i = 0; i < (iLen + 1); i++) { *(pBuffer + i) = Image.pVid[i]; }
我想问一下,是否有办法一次性复制这些值,也许通过使用memcopy来获得更多的速度。
整个代码是:
extern "C" __declspec(dllexport) int __stdcall GetCameraImage(BYTE pBuffer[], int Type, int uWidth, int uHeight) { CameraImage Image; int ret; Image.pVid = (unsigned int*)malloc(4 * uWidth*uHeight); ret = stGetCameraImage(&Image, 1, uWidth, uHeight); if (ret == ERR_SUCCESS) { int iLen = (4 * uWidth * uHeight); for (int i = 0; i < (iLen + 1); i++) { *(pBuffer + i) = Image.pVid[i]; } ////print(“ImageType = %d, width = %d, height = %d”, Image.Type, Image.Width, //// Image.Height); ////print(“First Pixel : B = %d, G = %d, R = %d”, Image.pVid[0], Image.pVid[1], //// Image.pVid[2]); ////print(“Second Pixel : B = %d, G = %d, R = %d”, Image.pVid[4], Image.pVid[5], //// Image.pVid[6]); } free(Image.pVid); return ret; }
编辑:
* pVid是这样的:
unsigned int *pVid; // pointer to image data (Format RGB32...)
您的代码当前写入的方式,循环中的每个赋值都会溢出,并在pBuffer
给您一些垃圾值,因为您试图将一个unsigned int
赋值给BYTE
。 最重要的是,你将跑掉Image.pVid
数组的末尾,因为i
正在计数字节,而不是unsigned int
s
你可以通过这样来修复你的代码:
*(pBuffer + i) = ((BYTE*)Image.pVid)[i];
但是这是非常低效的。 最好一次移动整个单词,或者只使用memcpy:
memcpy(pBuffer,Image.pVid,iLen) //pBuffer must be at least iLen bytes long