Here is a working C++ version of the answer:
#include
#include
#include
#include
#include
#include
std::vector readBMP(const std::string &file)
{
static constexpr size_t HEADER_SIZE = 54;
std::ifstream bmp(file, std::ios::binary);
std::array header;
bmp.read(header.data(), header.size());
auto fileSize = *reinterpret_cast(&header[2]);
auto dataOffset = *reinterpret_cast(&header[10]);
auto width = *reinterpret_cast(&header[18]);
auto height = *reinterpret_cast(&header[22]);
auto depth = *reinterpret_cast(&header[28]);
std::cout << "fileSize: " << fileSize << std::endl;
std::cout << "dataOffset: " << dataOffset << std::endl;
std::cout << "width: " << width << std::endl;
std::cout << "height: " << height << std::endl;
std::cout << "depth: " << depth << "-bit" << std::endl;
std::vector img(dataOffset - HEADER_SIZE);
bmp.read(img.data(), img.size());
auto dataSize = ((width * 3 + 3) & (~3)) * height;
img.resize(dataSize);
bmp.read(img.data(), img.size());
char temp = 0;
for (auto i = dataSize - 4; i >= 0; i -= 3)
{
temp = img[i];
img[i] = img[i+2];
img[i+2] = temp;
std::cout << "R: " << int(img[i] & 0xff) << " G: " << int(img[i+1] & 0xff) << " B: " << int(img[i+2] & 0xff) << std::endl;
}
return img;
}