1 #include <iostream> 2 #include <fstream> 3 #include <fstream> 4 #include <Windows.h> 5 6 using namespace std; 7 8 int main(int argc,char*argv[]) 9 { 10 tagBITMAPFILEHEADER a;//头信息块 11 tagBITMAPINFOHEADER b;//描述块 12 ifstream fin(argv[1], ios::binary | ios::in);//打开argv1用于读取,原文件 13 if (!fin)cout << "Source file open error." << endl; 14 ofstream fout(argv[2], ios::binary | ios::out | ios::trunc); 15 if (!fout)cout << "New file open error. "; 16 fin.read((char*)&a, sizeof(tagBITMAPFILEHEADER)); 17 fin.read((char*)&b, sizeof(tagBITMAPINFOHEADER)); 18 fout.write((char*)&a, sizeof(a));//写入头信息块 19 int tmp; 20 tmp = b.biWidth; b.biWidth = b.biHeight; b.biHeight = tmp;//交换宽高 21 tmp = b.biXPelsPerMeter; b.biXPelsPerMeter = b.biYPelsPerMeter; b.biYPelsPerMeter = tmp;//交换xy分辨率 22 fout.write((char*)&b, sizeof(b));//写入描述信息块 23 int pixw = 3;//1像素3字节 24 RGBTRIPLE *s = new RGBTRIPLE[b.biWidth*b.biHeight]; 25 int skip = (4 - ((b.biHeight*3) % 4)) % 4; 26 for (LONG i = 0; i <b.biWidth; i++) 27 { 28 fin.read((char*)s + i*b.biHeight*3, b.biHeight*3);//转化成char 所以变成了*3……吧 29 fin.seekg(skip, ios::cur); 30 } 31 int bu_n = (4 - (b.biWidth*pixw)%4) % 4; 32 char *bu = new char[bu_n]; 33 for (int i = 0; i < bu_n; i++) 34 bu[i] = 0; 35 for (LONG i = b.biHeight - 1; i >= 0; i--) { 36 for (LONG j = 0; j < b.biWidth; j++) 37 fout.write((char*)&s[j*b.biHeight+i], sizeof(tagRGBTRIPLE)); 38 fout.write(bu, bu_n); 39 } 40 delete[]s; 41 delete[]bu; 42 fin.close(); 43 fout.close(); 44 return 0; 45 }
好像有点问题,虽然看上去好像成功了,但旋转后的图片会比原图多1kb
感觉1kb其实还挺多的,不知道是不是正常的
先存着……做错题导致我都不想做魔兽了tut