-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.cpp
executable file
·315 lines (266 loc) · 7.78 KB
/
image.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "image.hpp"
#include <cassert>
#include <SDL_image.h>
#include <glut.h>
static bool operator==(const SDL_PixelFormat &a, const SDL_PixelFormat &b);
class ImagePool
{
public:
ImagePool();
bool ready();
void deferLoad(std::string filename, Image *img);
void copyDeferredLoad(const Image *from, Image *to);
void cancelDeferredLoad(Image *img);
void loadImage(std::string filename, GLuint texID);
GLuint getTexID(std::string filename);
void loadImages();
ICoord imageSize(GLuint texID) const;
Color averageColor(GLuint texID);
protected:
Color averageColor(void *pixels, unsigned w, unsigned h);
typedef std::pair<std::string, Image*> StringImagePair;
typedef std::vector< StringImagePair > UnloadedPool;
typedef std::map< std::string, GLuint> TexturePool;
typedef std::map<Image*, std::string> ReverseUnloadedPool;
typedef std::map<GLuint, ICoord> ImageSizes;
bool loaded;
UnloadedPool unloadedImages;
ReverseUnloadedPool pendingImages;
ImageSizes imageSizes;
TexturePool texIDs;
std::map<GLuint, Color> averageColors;
};
ImagePool *imagePool = NULL;
void initImagePool()
{
if(!imagePool)
imagePool = new ImagePool();
imagePool->loadImages();
}
Image::Image()
{
textureID = 0;
}
Image::Image(std::string filename)
{
if(!imagePool) {
imagePool = new ImagePool();
}
if(imagePool->ready())
textureID = imagePool->getTexID(filename);
else
imagePool->deferLoad(filename, this);
}
Image::Image(const Image ©)
{
if(imagePool->ready()) {
textureID = copy.textureID;
} else {
imagePool->copyDeferredLoad(©, this);
}
}
Image::~Image()
{
if(!imagePool->ready()) {
imagePool->cancelDeferredLoad(this);
}
}
void Image::bind() const
{
glBindTexture(GL_TEXTURE_2D, textureID);
}
unsigned Image::getWidth() const
{
if(!imagePool || !imagePool->ready())
return 0;
else
return imagePool->imageSize(textureID).x;
}
unsigned Image::getHeight() const
{
if(!imagePool || !imagePool->ready())
return 0;
else
return imagePool->imageSize(textureID).y;
}
Color Image::averageColor() const
{
if(!imagePool || !imagePool->ready())
return Color(0,0,0,0);
else
return imagePool->averageColor(textureID);
}
ImagePool::ImagePool()
{
loaded = false;
}
bool ImagePool::ready()
{
return loaded;
}
/// Convert an image to textureFormat.
/// Returns a new SDL_Surface containing the reformatted image. The original
/// surface is freed. Returns NULL if the conversion failed, or #image if no
/// conversion is necessary.
SDL_Surface *convertImage(SDL_Surface *image)
{
if(!image)
return NULL;
else if(textureFormat == *(image->format))
return image;
else
{
SDL_Surface *newImage;
newImage = SDL_ConvertSurface(image, &textureFormat, SDL_SWSURFACE);
SDL_FreeSurface(image);
return newImage;
}
}
void ImagePool::loadImage(std::string filename, GLuint textureID)
{
if(filename.length()==0) {
textureID = 0;
return;
}
filename = std::string("images/") + filename;
int MinFilter=GL_LINEAR, MagFilter=GL_LINEAR;
bool use_mipmap=false;
SDL_Surface *image;
SDL_PixelFormat *desired_format = &textureFormat;
// Load image using SDL_image
image = IMG_Load(filename.c_str());
if (image==NULL) {
throw TextureLoadError(retprintf("IMG_Load failed for %s: %s\n", filename.c_str(), SDL_GetError()));
}
// Convert to the appropriate pixel format if necessary
image = convertImage(image);
if(!image) {
throw TextureLoadError(retprintf(
"Failed converting %s to an appropriate format.\n", filename.c_str()));
}
// Make a texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, MagFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, MinFilter);
imageSizes[textureID] = ICoord(image->w, image->h);
if((image->w != 64 && image->w != 128 && image->w != 256) ||
(image->h != 64 && image->h != 128 && image->h != 256))
use_mipmap = true; //force mipmap for non-standard textures
int numChannels;
int channelOrder;
int channelType;
channelOrder = GL_RGBA;
channelType = GL_UNSIGNED_BYTE;
numChannels = 4;
if (use_mipmap) {
gluBuild2DMipmaps(GL_TEXTURE_2D, //type of texture
numChannels, //
image->w, image->h, //dimensions
channelOrder, //format
channelType, //channel type
image->pixels); //data
} else {
glTexImage2D(GL_TEXTURE_2D, //type of texture
0, //level of detail (mipmap)
numChannels, //components per pixel
image->w, image->h, //dimensions
0, //image border
channelOrder, //colors order
channelType, //components data type
image->pixels); //pixel data
}
averageColors[textureID] = averageColor(image->pixels, image->w, image->h);
// Clean up
SDL_FreeSurface(image);
}
Color ImagePool::averageColor(void *pixels, unsigned w, unsigned h)
{
unsigned long r=0,g=0,b=0;
unsigned char *bytes = (unsigned char*)pixels;
unsigned numBytes = w*h*4;
unsigned ii;
for(ii=0; ii<numBytes; ii+=4)
{
r += bytes[ii];
g += bytes[ii+1];
b += bytes[ii+2];
}
return Color( (double)r/(w*h), (double)g/(w*h), (double)b/(w*h), 255 );
}
Color ImagePool::averageColor(GLuint textureID)
{
return averageColors[textureID];
}
GLuint ImagePool::getTexID(std::string filename)
{
TexturePool::iterator ii = texIDs.find(filename);
if(ii==texIDs.end()) {
GLuint newID;
glGenTextures(1, &newID);
loadImage(filename, newID);
texIDs[filename] = newID;
return newID;
} else {
return ii->second;
}
}
ICoord ImagePool::imageSize(GLuint texID) const
{
ImageSizes::const_iterator ii=imageSizes.find(texID);
if(ii==imageSizes.end())
return ICoord(0,0);
else
return ii->second;
}
void ImagePool::deferLoad(std::string filename, Image *img)
{
unloadedImages.push_back( StringImagePair(filename, img) );
pendingImages[img] = filename;
}
void ImagePool::copyDeferredLoad(const Image *from, Image *to)
{
pendingImages[to] = pendingImages[(Image*)from];
unloadedImages.push_back( StringImagePair(pendingImages[to], to) );
}
void ImagePool::cancelDeferredLoad(Image *img)
{
pendingImages.erase(img);
for(UnloadedPool::iterator ii=unloadedImages.begin(); ii!=unloadedImages.end(); ii++) {
if(ii->second == img) {
unloadedImages.erase(ii);
break;
}
}
}
void ImagePool::loadImages()
{
loaded = true;
for(UnloadedPool::iterator ii = unloadedImages.begin(); ii!=unloadedImages.end(); ii++)
*(ii->second) = Image(ii->first.c_str());
unloadedImages.clear();
pendingImages.clear();
}
void drawImage(float x, float y, float size_x, float size_y, Image image)
{
image.bind();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(x, y );
glTexCoord2f(1.0, 0.0); glVertex2f(x+size_x, y );
glTexCoord2f(1.0, 1.0); glVertex2f(x+size_x, y+size_y);
glTexCoord2f(0.0, 1.0); glVertex2f(x, y+size_y);
glEnd();
}
static bool operator==(const SDL_PixelFormat &a, const SDL_PixelFormat &b)
{
return a.palette == b.palette &&
a.BitsPerPixel == b.BitsPerPixel &&
a.BytesPerPixel == b.BytesPerPixel &&
a.Rloss == b.Rloss && a.Gloss == b.Gloss &&
a.Bloss == b.Bloss && a.Aloss == b.Aloss &&
a.Ashift == b.Ashift && a.Rshift == b.Rshift &&
a.Gshift == b.Gshift && a.Bshift == b.Bshift &&
a.Amask == b.Amask && a.Rmask == b.Rmask &&
a.Gmask == b.Gmask && a.Bmask == b.Bmask &&
a.colorkey == b.colorkey && a.alpha == b.alpha;
}