简体   繁体   English

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

[英]Rotating a ppm image 90 degrees to the right in C

I have the following problem with rotating the PPM image to the right The first two lines in the result image are black (or some color from the rainbow) 将PPM图像向右旋转时,我遇到以下问题:结果图像中的前两行为黑色(或彩虹中的某些颜色)

Here's the code that sets the buffer for the image (the variables g_Width and g_height are set by a function) 这是设置图像缓冲区的代码(变量g_Width和g_height由函数设置)

struct pixel *image = malloc(sizeof(struct pixel) * g_width * g_height);

here's the function with the pointer passed into it 这是传递指针的函数

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;
}

If I print the image buffer it comes out just fine, but if I rotate it it comes out like this (note the first 2 lines of pixels) 如果我打印图像缓冲区,它就很好了,但是如果我旋转它,它就这样了(注意前两行像素)

https://www.dropbox.com/s/vh8l6s26enbxj42/t3.png?dl=0 https://www.dropbox.com/s/vh8l6s26enbxj42/t3.png?dl=0

it's as if the last 2 lines aren't being swapped at all, please help 好像最后两行根本没有交换,请帮助

EDIT: I solved the second black line at least, but I still need help with the last line 编辑:我至少解决了第二条黑线,但我仍然需要最后一行的帮助

As said you mix the first line (and overflow) 如前所述,您混合第一行(并溢出)

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;
}

This would rotate it one way (removing unnecessary brackets) 这将使其旋转一种方式(除去不必要的括号)

for (i=0; i<g_height; i++) {
    for (j=0; j<g_width; j++) {
        buffer[g_height * j + i] = img[g_width * i + j];
    }
}

But your code suggest you want it the other way, and the code lacks a -1 , resulting in clipping a line at one edge and an undefined line at the other edge. 但是您的代码建议您以其他方式使用它,而该代码缺少-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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM