#pragma pack(1)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef short WORD;
typedef unsigned int DWORD;
typedef unsigned char BYTE;
typedef struct {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfoffBits;
}BITMAPFILEHEADER;
typedef struct {
DWORD biSize;
DWORD biWidth;
DWORD biHeight;
WORD biPlane;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
DWORD biXPelsPerMeter;
DWORD biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}BITMAPINFOHEADER;
typedef struct {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
}RGBQUAD;
int getFileSize(char* filename) {
FILE* fp = fopen(filename, "rb");
if (fp == NULL) {
printf("文件不存在\n");
return 0;
}
fseek(fp, 0, SEEK_END);
int length = ftell(fp);
fclose(fp);
return length;
}
int LoadInBuf(char* filename, BYTE** buf) {
FILE* fp = fopen(filename, "rb");
if (fp == NULL) {
printf("文件不存在\n");
return 0;
}
int length = getFileSize(filename);
*buf = (BYTE*)malloc(sizeof(BYTE) * length);
fread(*buf, sizeof(BYTE), length, fp);
fclose(fp);
return 1;
}
int SaveInFile(char* filename, BYTE* buf, int size) {
FILE* fp = fopen(filename, "wb");
if (fp == NULL) {
printf("无法创建新的文件\n");
return 0;
}
fwrite(buf, sizeof(BYTE), size, fp);
fclose(fp);
return 1;
}
int main() {
char filename[] = "D://CCode//code//位图旋转.bmp";
int length = getFileSize(filename);
BYTE* buf;
LoadInBuf(filename, &buf);
BYTE* buf_copy;
LoadInBuf(filename, &buf_copy);
BITMAPFILEHEADER* fileHeader = (BITMAPFILEHEADER*)buf;
BITMAPINFOHEADER* infoHeader = (BITMAPINFOHEADER*)(buf + sizeof(BITMAPFILEHEADER));
int height = infoHeader->biHeight;
int width = infoHeader->biWidth;
printf("bfSize: %d\n", fileHeader->bfSize);
printf("bfoffBits %d\n", fileHeader->bfoffBits);
printf("位图的大小是%d\n", length);
printf("文件的高度是%d\n", infoHeader->biHeight);
printf("文件的宽度是%d\n", infoHeader->biWidth);
printf("信息头的大小??%d\n", infoHeader->biSize);
printf("biBitCount: %d\n", infoHeader->biBitCount);
printf("biSizeImage: %d\n", infoHeader->biSizeImage);
printf("biXPelsPerMeter: %d\n", infoHeader->biXPelsPerMeter);
printf("biYPelsPerMeter: %d\n", infoHeader->biYPelsPerMeter);
BITMAPINFOHEADER* infoHeader1 = (BITMAPINFOHEADER*)(buf_copy + sizeof(BITMAPFILEHEADER));
BITMAPFILEHEADER* fileHeader1 = (BITMAPFILEHEADER*)buf_copy; infoHeader1->biSizeImage = fileHeader->bfSize - fileHeader->bfoffBits;
infoHeader1->biWidth = height;
infoHeader1->biHeight = width;
int addByte = 0;
int addOld = 0;
int addNow = 0;
int imageSize = fileHeader->bfSize - fileHeader->bfoffBits;
BYTE* img1 = buf + fileHeader->bfoffBits;
BYTE* img2 = buf_copy + fileHeader1->bfoffBits;
int row_old = width * infoHeader->biBitCount / 8;
int row_now = height * infoHeader->biBitCount / 8;
int byteOfPixel = infoHeader->biBitCount / 8;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int k = 0;
for (; k < byteOfPixel; k++) {
*(img2 + i * (row_now + addNow) + j*(byteOfPixel) + k)=*(img1 + j * (row_old + addOld)+height*(byteOfPixel) - i*(byteOfPixel) + k);
}
for (int l = 0; l < addNow; l++) {
*(img2 + j * (row_now + addNow) +row_now + l) = 0x00;
}
}
}
char filename1[] = "D://CCode//code//位图旋转结果.bmp";
SaveInFile(filename1, buf_copy, fileHeader1->bfSize);
free(buf);
free(buf_copy);
system("pause");
return 0;
}
|