在C中将ppm图像向右旋转90度

在C中将ppm图像向右旋转90度,c,image,rotation,ppm,C,Image,Rotation,Ppm,将PPM图像向右旋转时出现以下问题 结果图像中的前两行是黑色(或彩虹中的某种颜色) 下面是设置图像缓冲区的代码(变量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);

将PPM图像向右旋转时出现以下问题 结果图像中的前两行是黑色(或彩虹中的某种颜色)

下面是设置图像缓冲区的代码(变量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;
}
void rotate90(结构像素*img){
int i,j,大小,th;
大小=大小(结构像素)*g_宽度*g_高度;
结构像素*缓冲区=malloc(大小);
if(buffer==NULL){
fprintf(stderr,“无法分配内存”);
退出(退出失败);
}
对于(i=0;i
如果我打印图像缓冲区,它会很好地显示出来,但是如果我旋转它,它会像这样显示出来(注意前两行像素)

好像最后两行根本没有被交换,请帮忙

编辑:我至少解决了第二条黑线,但我仍然需要帮助
最后一行

如前所述,您混合了第一行(和溢出)

void rotate90(结构像素*img){
int i,j,大小,th;
大小=大小(结构像素)*g_宽度*g_高度;
结构像素*缓冲区=malloc(大小);
if(buffer==NULL){
fprintf(stderr,“无法分配内存”);
退出(退出失败);
}
对于(i=0;i
这将使其单向旋转(移除不必要的支架)


您必须初始化所有缓冲区。例如,未设置
缓冲区[0]
。也许
buffer[(g_height*j)+(g_height-i-1)]
工作得很好,谢谢:)
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;
}
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;
}
for (i=0; i<g_height; i++) {
    for (j=0; j<g_width; j++) {
        buffer[g_height * j + i] = img[g_width * i + j];
    }
}
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];
    }
}