我正在試圖查找文件大小,文件頭大小寬度和bmp文件的高度。我研究了bmp文件的格式和文件中字節的排列方式。 當我嘗試此代碼時,它顯示不同的文件寬度和高度不對。 到目前爲止,我已經嘗試了三種圖像。這一個圖像產生正確的測量結果。閱讀bmp文件頭大小
這一次沒有:
我不明白,我哪裏錯了,但比特深度表現出對所有三個圖像的正確值。
這裏是我的代碼:通過讀取BMP頭爲一系列字節,而不是整數的
#include<iostream>
#include<fstream>
#include<math.h>
using namespace std;
int main() {
ifstream inputfile("bmp.bmp",ios::binary);
char c; int imageheader[1024];
double filesize=0; int width=0; int height=0;int bitCount = 0;
for(int i=0; i<1024; i++) {
inputfile.get(c); imageheader[i]=int(c);
}
filesize=filesize+(imageheader[2])*pow(2,0)+(imageheader[3])*pow(2,8)+(imageheader[4])*pow(2,16)+(imageheader[5])*pow(2,24);
cout<<endl<<endl<<"File Size: "<<(filesize/1024)<<" Kilo Bytes"<<endl;
width=width+(imageheader[18])*pow(2,0)+(imageheader[19])*pow(2,8)+(imageheader[20])*pow(2,16)+(imageheader[21])*pow(2,24);
cout<<endl<<"Width: "<<endl<<(width)<<endl;
height=height+(imageheader[22])*pow(2,0)+(imageheader[23])*pow(2,8)+(imageheader[24])*pow(2,16)+(imageheader[25])*pow(2,24);
cout<<endl<<"Height: "<<endl<<(height)<<endl;
bitCount=bitCount+(imageheader[28])*pow(2,0)+(imageheader[29])*pow(2,8);
cout<<endl<<"Bit Depth: "<<endl<<(bitCount)<<endl;
}
通常,經過一段時間或逗號後,用英文填寫空格。此外,大寫字母應該用於句子中的第一個單詞。最後,對代碼進行格式化,使其易讀,並且我們不會浪費時間來破解它。 – nbro
不便之處 – USERRR5
代碼建議。不要使用'double','pow'函數或任何浮點數來表示一些本來就是不可或缺的東西 – selbie