-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageLoader.hpp
174 lines (135 loc) · 3.42 KB
/
ImageLoader.hpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifndef IMAGELOADER_H_
#define IMAGELOADER_H_
#include <sstream>
#include "FreeImage.h"
#include <GL/glew.h>
#include "../../common/logger/Logger.hpp"
#include "serialize/Serialization.hpp"
#include "serialize/std/Vector.hpp"
namespace utilities
{
enum Format
{
FORMAT_UNKNOWN = -1,
FORMAT_RGB,
FORMAT_RGBA
};
struct Image
{
Image()
{
data = std::vector<char>();
width = 0;
height = 0;
format = Format::FORMAT_UNKNOWN;
}
Image(const Image& image)
{
data = image.data;
width = image.width;
height = image.height;
format = image.format;
}
std::vector<char> data;
int width;
int height;
Format format;
};
/**
* Will return the OpenGL compatible format of the given image Format 'format'.
*
* If no known compatible OpenGL format is found, FORMAT_UNKNOWN is returned.
*
* @param format
*
* @return The OpenGL compatible format of the given image Format 'format', or FORMAT_UNKNOWN if no known compatible OpenGL format is found.
*/
inline GLint getOpenGlImageFormat( Format format )
{
switch (format)
{
case Format::FORMAT_RGB:
return GL_RGB;
case Format::FORMAT_RGBA:
return GL_RGBA;
case Format::FORMAT_UNKNOWN:
return (GLint)FORMAT_UNKNOWN;
}
return (GLint)FORMAT_UNKNOWN;
}
class ImageLoader
{
public:
ImageLoader()
{
};
virtual ~ImageLoader()
{
};
std::unique_ptr<Image> loadImageData(const std::string filename, bool hasAlpha = true)
{
return loadImageData(filename.c_str());
};
std::unique_ptr<Image> loadImageData(const char* filename, bool hasAlpha = true)
{
LOG_DEBUG( "Loading image." );
FREE_IMAGE_FORMAT format = FreeImage_GetFileType(filename, 0);
FIBITMAP* imageBitmap = FreeImage_Load(format, filename);
LOG_DEBUG( "image loaded." );
FIBITMAP* temp = imageBitmap;
if ( hasAlpha )
imageBitmap = FreeImage_ConvertTo32Bits(imageBitmap);
else
imageBitmap = FreeImage_ConvertTo24Bits(imageBitmap);
FreeImage_Unload(temp);
int w = FreeImage_GetWidth(imageBitmap);
int h = FreeImage_GetHeight(imageBitmap);
std::stringstream msg;
msg << "The size of the image '" << filename << "' is: " << w << "*" << h;
LOG_DEBUG( msg.str() );
char* pixels = (char*) FreeImage_GetBits(imageBitmap);
auto retImage = std::unique_ptr<Image>();
if ( pixels != nullptr )
{
int pixelSize = 3;
if ( hasAlpha )
pixelSize = 4;
// FreeImage loads in BGR format, so you need to swap some bytes (Or use GL_BGR)
for ( int j = 0; j < w * h; j++ )
{
char swap = pixels[j * pixelSize + 2];
pixels[j * pixelSize + 2] = pixels[j * pixelSize + 0];
pixels[j * pixelSize + 0] = swap;
}
retImage = std::unique_ptr<Image>(new Image());
int imageBytesLength = pixelSize * w * h;
// Transfer raw data into a vector
retImage->data = std::vector<char>(pixels, pixels+imageBytesLength);
retImage->width = w;
retImage->height = h;
if (hasAlpha)
retImage->format = FORMAT_RGBA;
else
retImage->format = FORMAT_RGB;
}
FreeImage_Unload(imageBitmap);
return retImage;
};
};
}
BOOST_SERIALIZATION_SPLIT_FREE(utilities::Image)
namespace boost
{
namespace serialization
{
template<class Archive> void save(Archive& ar, const utilities::Image& img, unsigned int version)
{
ar & img.data & img.width & img.height & img.format;
}
template<class Archive> void load(Archive& ar, utilities::Image& img, unsigned int version)
{
ar & img.data & img.width & img.height & img.format;
}
}
}
#endif /* IMAGELOADER_H_ */