C++: What’s the simplest way to read and write BMP files using C++ on Windows?
When developing just for Windows I usually just use the ATL CImage class
When developing just for Windows I usually just use the ATL CImage class
Look my simple c implementation function to load texture. GLuint LoadTexture( const char * filename ) { GLuint texture; int width, height; unsigned char * data; FILE * file; file = fopen( filename, “rb” ); if ( file == NULL ) return 0; width = 1024; height = 512; data = (unsigned char *)malloc( width … Read more
(I’m answering my own question) Here is my current solution. It is derived from this source : https://github.com/ultrakain/AndroidBitmapUtil (thanks to ultrakain and @Francescoverheye ) I just fix a little bug in computation of the dummy bytes that must be added to each row (so that the length of each row in bytes is a multiple … Read more
and ready to go code, tested with g++ (not Windows, but may help someone): #pragma pack(1) #include <iostream> #include <fstream> #include <vector> using namespace std; #include “bmp.h” vector<char> buffer; PBITMAPFILEHEADER file_header; PBITMAPINFOHEADER info_header; void fill() { std::ifstream file(“data.bmp”); if (file) { file.seekg(0,std::ios::end); streampos length = file.tellg(); file.seekg(0,std::ios::beg); buffer.resize(length); file.read(&buffer[0],length); file_header = (PBITMAPFILEHEADER)(&buffer[0]); info_header = (PBITMAPINFOHEADER)(&buffer[0] … Read more
The following code snippet is not complete, and contains lots of hidden assumptions and bugs. I wrote it from scratch for a university course project from mere observation, where it minimally fulfilled all the requirements. I didn’t work on it any more, because there must be libraries that would do the job way better. Here … Read more
See if this works for you… In this code, I had 3 2-dimensional arrays, called red,green and blue. Each one was of size [width][height], and each element corresponded to a pixel – I hope this makes sense! FILE *f; unsigned char *img = NULL; int filesize = 54 + 3*w*h; //w is your image width, … Read more