90度旋转bmp图像在c中

moiiocjp  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(127)

我试图将一个bmp图像顺时针旋转90度。但是,我被卡住了。我只能旋转180度。下面是我的180度旋转代码

int row_size = ((width * 3 + 3) / 4) * 4;

    uint8_t* pixels = (uint8_t*)malloc(height * row_size);
    fread(pixels, sizeof(uint8_t), height * row_size, input);

    uint8_t* rotatedPixels = (uint8_t*)malloc(height * row_size);

    for (uint32_t i = 0; i < width; ++i) {
        for (uint32_t j = 0; j < height; ++j) {
            uint32_t new_i = height - 1 - j;
            uint32_t new_j = width - 1 - i;

            rotatedPixels[(new_i * row_size) + (new_j * 3)] = pixels[(j * row_size) + (i * 3)];
            rotatedPixels[(new_i * row_size) + (new_j * 3) + 1] = pixels[(j * row_size) + (i * 3) + 1];
            rotatedPixels[(new_i * row_size) + (new_j * 3) + 2] = pixels[(j * row_size) + (i * 3) + 2];
        }
    }

    fwrite(header, sizeof(uint8_t), 54, output);
    fwrite(rotatedPixels, sizeof(uint8_t), height * row_size, output);

    free(pixels);
    free(rotatedPixels);
}

字符串

tzcvj98z

tzcvj98z1#

#include <stdio.h>
#define SIZE 6
#define L (SIZE - 1)

char i[SIZE][SIZE] = {
    {'_', '_', '_', '_', '_', '_'},
    {'_', '+', 'O', 'O', '+', '_'},
    {'_', '+', 'O', '+', '+', '_'},
    {'_', '+', '+', '+', '+', '_'},
    {'_', '+', '+', '+', '+', '_'},
    {'_', '_', '_', '_', '_', '_'},

};

int main(int argc, char const *argv[])
{
    printf("\njust printed\n");
    for (int y = 0; y < SIZE; y++)
    {
        for (int x = 0; x < SIZE; x++)
        {
            printf("%c", i[y][x]);
        }
        printf("\n");
    }

    printf("\nrotated of +90*\n");
    for (int x = L; x >= 0; x--)
    {
        for (int y = 0; y < SIZE; y++)
        {
            printf("%c", i[y][x]);
        }
        printf("\n");
    }

    printf("\nrotated of +180*\n");

    for (int y = L; y >= 0; y--)
    {
        for (int x = L; x >= 0; x--)
        {
            printf("%c", i[y][x]);
            // printf("(%c,%c)", x, y);
        }
        printf("\n");
    }

    printf("\nrotated of (+270*|-90*)\n");

    for (int x = 0; x < SIZE; x++)
    {
        for (int y = SIZE - 1; y >= 0; y--)
        {
            printf("%c", i[y][x]);
        }
        printf("\n");
    }

    return 0;
}

字符串

相关问题