Width is a 4-byte integer starting at offset 16 in the file. Height is another 4-byte integer starting at offset 20. They are both in network order, so you need to convert to the host order in order to interpret them correctly.
#include <fstream>
#include <iostream>
#include <winsock.h>
int main(int argc, char **argv) {
std::ifstream in(argv[1]);
unsigned int width, height;
in.seekg(16);
in.read((char *)&width, 4);
in.read((char *)&height, 4);
width = ntohl(width);
height = ntohl(height);
std::cout << argv[1] << " is " << width << " pixels wide and " << height << " pixels high.\n";
return 0;
}
source
share