-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadBMP.h
58 lines (51 loc) · 1.62 KB
/
loadBMP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//=====================================================================
// LoadBMP.h
// Minimal image loader for files in BMP format.
// Assumption: 24 bits per pixel
//
// Author:
// R. Mukundan, Department of Computer Science and Software Engineering
// University of Canterbury, Christchurch, New Zealand.
//=====================================================================
#if !defined(H_BMP)
#define H_BMP
#include <iostream>
#include <fstream>
#include <GL/freeglut.h>
using namespace std;
void loadBMP(const char* filename)
{
char* imageData;
char header1[18], header2[24];
short int planes, bpp;
int wid, hgt;
int nbytes, size, indx, temp;
ifstream file( filename, ios::in | ios::binary);
if(!file)
{
cout << "*** Error opening image file: " << filename << endl;
exit(1);
}
file.read (header1, 18); //Initial part of header
file.read ((char*)&wid, 4); //Width
file.read ((char*)&hgt, 4); //Height
file.read ((char*)&planes, 2); //Planes
file.read ((char*)&bpp, 2); //Bits per pixel
file.read (header2, 24); //Remaining part of header
// cout << "Width =" << wid << " Height = " << hgt << " Bpp = " << bpp << endl;
nbytes = bpp / 8; //No. of bytes per pixels
size = wid * hgt * nbytes; //Total number of bytes to be read
imageData = new char[size];
file.read(imageData, size);
//Swap r and b values
for(int i = 0; i < wid*hgt; i++)
{
indx = i*nbytes;
temp = imageData[indx];
imageData[indx] = imageData[indx+2];
imageData[indx+2] = temp;
}
glTexImage2D(GL_TEXTURE_2D, 0, 3, wid, hgt, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
delete imageData;
}
#endif