forked from michal1000w/GPUSmoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadTexture.h
61 lines (47 loc) · 1.4 KB
/
LoadTexture.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
58
59
60
61
#pragma once
#include "Common.h"
#include <iostream>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
unsigned char* loadBMPTexture(const char* imagepath, unsigned int& Width,
unsigned int& Height, unsigned int& BPP, unsigned int CHANNELS) {
unsigned char header[54];
unsigned int dataPos;
unsigned int width, height;
unsigned int imageSize;
unsigned char* data;
FILE* file = fopen(imagepath, "rb");
if (!file) {
printf("Error loading texture\n");
return 0;
}
if (fread(header, 1, 54, file) != 54) {
printf("Error: BMP file not correct!!\n");
return 0;
}
if (header[0] != 'B' || header[1] != 'M') {
printf("Error: BMP file not correct!!\n");
return 0;
}
// Read ints from the byte array
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (imageSize == 0) imageSize = width * height * CHANNELS; // 3 : one byte for each Red, Green and Blue component
if (dataPos == 0) dataPos = 54; // The BMP header is done that way
// Create a buffer
data = new unsigned char[imageSize];
// Read the actual data from the file into the buffer
fread(data, 1, imageSize, file);
//Everything is in memory now, the file can be closed
fclose(file);
//std::cout << "Texture loaded successfully";
Width = width;
Height = height;
BPP = imageSize;
return data;
}