以下代码主要是实现一个二维数组旋转90的功能,你觉得谁更牛?
// newbing的示例代码
unsigned char* buf_t = nullptr;
int m = (n_hight_s * 3 + 3) / 4 * 4;
int n = n_width_s*m;
Space_New(buf_t, n);
int x, y;
unsigned char *p1, *p2;
for (x = 0; x < n_hight_s; ++x) {
p1 = buf_s + x*n_width_s;
p2 = buf_t + x;
for (y = 0; y < n_width_s; ++y) {
*p2 = *p1++;
p2 += m;
}
}
ccymemcpy(buf, n, buf_t, n);
Space_Del(buf_t);
//GPT的示例代码
unsigned char* buf_t = nullptr;
int m = (n_hight_s * 3 + 3) / 4 * 4;//旋转90度需要考虑4字节对齐
int n = n_width_s * m;
Space_New(buf_t, n);
unsigned char* p_src = buf_s;
unsigned char* p_dst = buf_t + (n_width_s - 1) * 3;
for (int i = 0; i < n_width_s * n_hight_s; i++) {
*p_dst-- = *p_src++;
*p_dst-- = *p_src++;
*p_dst-- = *p_src++;
if ((i + 1) % n_width_s == 0) {
p_dst += m * 3 + 3;
p_src -= n_width_s * 3;
}
}
ccymemcpy(buf, n, buf_t, n);
Space_Del(buf_t);