c语言写一个旋转bmp图像90度的程序
时间: 2024-05-05 13:23:02 浏览: 16
以下是一个用 C 语言编写的旋转 BMP 图像 90 度的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma pack(2)
typedef struct BITMAPFILEHEADER {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} BMPFILEHEADER;
typedef struct BITMAPINFOHEADER {
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BMPINFOHEADER;
typedef struct RGBQUAD {
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
} RGBQUAD;
typedef struct BITMAPINFO {
BMPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BMPINFO;
#pragma pack()
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s [input_filename] [output_filename]\n", argv[0]);
return 1;
}
char *input_filename = argv[1];
char *output_filename = argv[2];
FILE *fp_in = fopen(input_filename, "rb");
if (fp_in == NULL) {
printf("Cannot open input file: %s\n", input_filename);
return 1;
}
FILE *fp_out = fopen(output_filename, "wb");
if (fp_out == NULL) {
printf("Cannot open output file: %s\n", output_filename);
return 1;
}
BMPFILEHEADER file_header;
BMPINFOHEADER info_header;
fread(&file_header, sizeof(BMPFILEHEADER), 1, fp_in);
fread(&info_header, sizeof(BMPINFOHEADER), 1, fp_in);
int width = info_header.biWidth;
int height = info_header.biHeight;
BMPINFOHEADER new_info_header = info_header;
new_info_header.biWidth = height;
new_info_header.biHeight = width;
fwrite(&file_header, sizeof(BMPFILEHEADER), 1, fp_out);
fwrite(&new_info_header, sizeof(BMPINFOHEADER), 1, fp_out);
int padding = (4 - ((width * 3) % 4)) % 4;
int new_padding = (4 - ((height * 3) % 4)) % 4;
RGBQUAD *palette = NULL;
if (new_info_header.biBitCount <= 8) {
int num_colors = new_info_header.biClrUsed ? new_info_header.biClrUsed : (1 << new_info_header.biBitCount);
palette = (RGBQUAD *)malloc(num_colors * sizeof(RGBQUAD));
fread(palette, sizeof(RGBQUAD), num_colors, fp_in);
fwrite(palette, sizeof(RGBQUAD), num_colors, fp_out);
}
int i, j;
unsigned char pixel[3];
unsigned char *row = (unsigned char *)malloc((width * 3 + padding) * sizeof(unsigned char));
unsigned char *new_row = (unsigned char *)malloc((height * 3 + new_padding) * sizeof(unsigned char));
for (i = 0; i < height; i++) {
fread(row, sizeof(unsigned char), width * 3 + padding, fp_in);
for (j = 0; j < width; j++) {
pixel[0] = row[j * 3];
pixel[1] = row[j * 3 + 1];
pixel[2] = row[j * 3 + 2];
memcpy(&new_row[(height - 1 - i) * 3 + j * 3], pixel, 3);
}
}
fwrite(new_row, sizeof(unsigned char), height * 3 + new_padding, fp_out);
free(row);
free(new_row);
if (palette != NULL) {
free(palette);
}
fclose(fp_in);
fclose(fp_out);
return 0;
}
```
这个程序通过读取 BMP 文件头和信息头,计算出图像的宽度和高度,并生成新的信息头,将旋转后的图像写入新的文件。在旋转过程中,程序按照从左到右、从上到下的顺序读取源文件的像素,将其存储到新文件的相应位置。由于 BMP 文件的像素行必须按照 4 字节对齐,程序还需要计算源文件和新文件的填充字节数,并在读写像素行时跳过这些填充字节。