获取作为区域副本的新图像。空白区域返回空指针 。如果内存分配失败,则返回一个空指针 。调用者负责释放返回的数组。复制图像中的像素区域
该区域包括[left, right-1]
(包含)在内的所有列, 以及从[top, bottom-1]
(含)的所有行。
在任何情况下,您都可以假设left <= right
和top <= bottom
: 不需要为此进行测试。
的区域的面积是(right-left) * (bottom-top)
像素,这 意味着如果left == right
或top == bottom
,该区域不具有 区域并且被定义为“空”。每个功能都注意如何处理空白区域。
此解决方案在终端中引发了一个称为“内存损坏”的错误,它指向我的malloc函数调用,然后沿着0x00001dec880
的行输入一个非常奇怪的数字,每次编译时都会有所不同。我不知道这是为什么,并帮助将不胜感激
uint8_t* region_copy(const uint8_t array[], unsigned int cols, unsigned int rows,
unsigned int left, unsigned int top, unsigned int right, unsigned int bottom) {
unsigned int corner1 = left + (top*cols);
unsigned int corner2 = right + (bottom*cols);
unsigned int newsize = (right - left) * (bottom - top);
if(left==right || top == bottom) {
return NULL;
}
uint8_t* newimg = malloc(newsize * sizeof(uint8_t));
if(newimg == NULL){
return NULL;
}
memset(newimg, 0, newsize);
for(int i = corner1; i < corner2; i++) {
newimg[i] = array[i];
}
return newimg;
}