Skip to content

Instantly share code, notes, and snippets.

@biancarosa
Created January 15, 2019 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biancarosa/2e55d2f20107d7407658f55e5a6fc18b to your computer and use it in GitHub Desktop.
Save biancarosa/2e55d2f20107d7407658f55e5a6fc18b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <GL/glut.h>
class Image
{
public:
int width;
int height;
unsigned char *data;
void readBMP(char* filename);
GLint toTexture();
};
void Image::readBMP(char* filename)
{
int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
width = *(int*)&info[18];
height = *(int*)&info[22];
int size = 3 * width * height;
data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
for(i = 0; i < size; i += 3)
{
unsigned char tmp = data[i];
data[i] = data[i+2];
data[i+2] = tmp;
}
width = width;
height = height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment