c语言旋转bmp图片程序,C语言实现BMP图像处理(任意角度旋转)

原理参考:https://blog.csdn.net/liyuan02/article/details/6750828

实现对对任意角度的旋转,具体数学推导网上找。如果各位读者需要使用,只需要将打开文件的位置改为你的位置,输入不同的角度即可:

#include #include #include #include #define CV_PI 3.1415926

int main(){

FILE *fp = fopen("./01.bmp", "rb");

if (fp == 0){

printf("文件打开失败\n");

return 0;

}

BITMAPFILEHEADER fileHead;

BITMAPINFOHEADER infoHead;

fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);

fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp);

int width = infoHead.biWidth;

int height = infoHead.biHeight;

int biCount = infoHead.biBitCount;

int lineByte = (width*biCount / 8 + 3) / 4 * 4;

RGBQUAD *pColorTable;

pColorTable = new RGBQUAD[256];

fread(pColorTable, sizeof(RGBQUAD), 256, fp)

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个用 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 字节对齐,程序还需要计算源文件和新文件的填充字节数,并在读写像素行时跳过这些填充字节。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值