Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Last active July 6, 2016 14:56
Show Gist options
  • Select an option

  • Save JossWhittle/fcf89e51afb93e8ccebf95d927795b17 to your computer and use it in GitHub Desktop.

Select an option

Save JossWhittle/fcf89e51afb93e8ccebf95d927795b17 to your computer and use it in GitHub Desktop.
static inline unsigned char* LoadBMP_24(const std::string &path) {
int w, h;
unsigned char *img = nullptr;
std::ifstream inFile(path, std::ios::in | std::ios::binary);
if (inFile.is_open()) {
/// Header
unsigned char hdr[0x36];
inFile.read((char*) hdr, 0x12);
inFile.read((char*) &w, 4);
inFile.read((char*) &h, 4);
inFile.read((char*) hdr, 0x1C);
/// Use wR for image width as BMP files require the
/// width be padded to a multiple of four bytes
const int tR = ((w * 3) % 4), R = (tR == 0 ? 0 : (4 - tR)), wR = (w * 3) + R;
/// Data
img = new unsigned char[w * h * 3];
unsigned char *raw = new unsigned char[wR * h];
inFile.read((char*) raw, wR * h);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
const int i = (x + (y * w)) * 3;
const int j = ((x * 3) + (y * wR));
img[i+0] = raw[j+0];
img[i+1] = raw[j+1];
img[i+2] = raw[j+2];
}
}
delete [] raw;
printf("Loading: %s [SUCCESS]\n", path.c_str());
inFile.close();
}
else {
printf("Loading: %s [ ERROR ]\n", path.c_str());
}
return img;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment