向右旋转PPM图像时出现以下问题 结果图像中的前两行是黑色(或彩虹中的某种颜色)
这里是设置图像缓冲区的代码(变量g_Width和g_height由函数设置)
struct pixel *image = malloc(sizeof(struct pixel) * g_width * g_height);
这里是指针传递给它的函数
void rotate90(struct pixel *img) {
int i, j, size, th;
size = sizeof(struct pixel) * g_width * g_height;
struct pixel *buffer = malloc(size);
if (buffer == NULL) {
fprintf(stderr, "Unable to allocate memory\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < g_height; i++) {
for (j=0; j < g_width; j++) {
buffer[(g_height*j)+(g_height-i)] = img[(g_width*i) + j];
}
}
//copy the buffer into the image pointer
memcpy(img, buffer, size);
//free the buffer and swap the width and height around
free(buffer);
th = g_height;
g_height = g_width;
g_width = th;
}
如果我打印图像缓冲区它会很好,但是如果我旋转它会像这样出现(注意前两行像素)
https://www.dropbox.com/s/vh8l6s26enbxj42/t3.png?dl=0
好像最后两行根本没有交换,请帮忙
编辑:我至少解决了第二条黑线,但我仍然需要帮助 最后一行答案 0 :(得分:0)
如你所说混合第一行(和溢出)
void rotate90(struct pixel *img) {
int i, j, size, th;
size = sizeof(struct pixel) * g_width * g_height;
struct pixel *buffer = malloc(size);
if (buffer == NULL) {
fprintf(stderr, "Unable to allocate memory\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < g_height; i++) {
for (j=0; j < g_width; j++) {
buffer[(g_height*j)+(g_height-i -- 1)] = img[(g_width*i) + j];
}
}
//copy the buffer into the image pointer
memcpy(img, buffer, size);
//free the buffer and swap the width and height around
free(buffer);
th = g_height;
g_height = g_width;
g_width = th;
}
答案 1 :(得分:0)
这将以一种方式旋转(删除不必要的括号)
for (i=0; i<g_height; i++) {
for (j=0; j<g_width; j++) {
buffer[g_height * j + i] = img[g_width * i + j];
}
}
但是你的代码建议你想要另一种方式,代码缺少-1
,导致在一条边剪裁一条线,在另一条边剪裁一条未定义的线。
for (i=0; i<g_height; i++) {
for (j=0; j<g_width; j++) {
buffer[g_height * j + g_height - i - 1] = img[g_width * i + j];
}
}