c 中 bmp 图像旋转 90 度

问题描述 投票:0回答:1

我正在尝试将 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);
}
c image-rotation
1个回答
0
投票
#include <stdio.h>
#define SIZE 4

int i[SIZE][SIZE] = {
    {0, 1, 1, 0},
    {0, 1, 1, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
};

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

    printf("\n");

    // rotated of 90*
    for (int x = 0; x < SIZE; x++)
    {
        for (int y = 0; y < SIZE; y++)
        {
            printf("%d", i[y][x]);
        }
        printf("\n");
    }

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.