简体   繁体   中英

Rotating a 2D PGM Image Array in C

I am working on a homework project to rotate a simple 2D array holding RGB values for a PGM file.

I've read many posts in these forums about how to do this in C, and I've almost got it working. My output file has the correct dimensions and is rotated, but there is a thick black border around the top and sides. I just can't figure out what I'm doing wrong.

Any help would be greatly appreciated!

I modified the code presented here to get started, and this is the rotate90 function I'm working on now:

PGMImage* rotate90(PGMImage *old_img)
{
    int x, y;
    PGMImage *new_img = malloc(sizeof(PGMImage));
    new_img->maxVal = old_img->maxVal;
    new_img->width = old_img->height;
    new_img->height = old_img->width;
    for (x = 0; x < old_img->width; x++)
    {
        for (y = 0; y < old_img->height; y++)
        {
            new_img->data[old_img->height - 1 - y][x].r = old_img->data[x][y].r;
            new_img->data[old_img->height - 1 - y][x].g = old_img->data[x][y].g;
            new_img->data[old_img->height - 1 - y][x].b = old_img->data[x][y].b;
        }
    }
    return new_img;
}

void main()
{
    PGMImage* img = malloc(sizeof(PGMImage));
    getPGMfile("columns.pgm", img);
    save("columns_new.ppm", rotate90(img));
}  

The save() and getPGMfile() functions work perfectly on their own.
It's only when I pass the result of my rotate90() function to save() that I get the funky results.

怎么样在malloc语句后尝试memcpy(new_img, old_img, sizeof(PGMImage) 。也许除了宽度和高度之外还没有初始化其他一些属性。而且,如果data变量是一个指针,您是否为data分配了一块内存为new_img对象?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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