Created
January 15, 2019 21:16
-
-
Save biancarosa/2e55d2f20107d7407658f55e5a6fc18b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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