我正在试图查找文件大小,文件头大小宽度和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