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
|
#include <pthread.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/osmesa.h>
#include <Magick++.h>
#include <string>
#include <iostream>
#include <list>
#include <dds.h>
#include <LuaHandle.h>
#include <BLua.h>
#ifndef WIN32
#define WEAK __attribute__ ((weak))
#else
#define WEAK
#endif
OSMesaContext ctx = 0;
void *buffer = 0;
int width = -1;
int height = -1;
bool upscale = false;
class scene_t : public Base {
public:
std::list<Magick::Image> scene;
String filename, tmp_filename;
};
void empty_scene(scene_t scene) {
while (scene.scene.begin() != scene.scene.end()) {
scene.scene.pop_front();
}
}
scene_t * init_OSMesa_context(scene_t * scene, int _width, int _height) {
scene_t * r = scene;
if ((width > 2048) || (height > 2048)) {
printf("Image too wide!\n");
return NULL;
}
if (!r)
r = new scene_t();
glPushAttrib(GL_ALL_ATTRIB_BITS);
if ((width == _width) && (height == _height)) {
return r;
}
width = _width;
height = _height;
/* Bind the buffer to the context and make it current */
if (!OSMesaMakeCurrent(ctx, buffer, GL_UNSIGNED_BYTE, width, height)) {
printf("OSMesaMakeCurrent (8 bits/channel) failed!\n");
delete r;
return NULL;
}
return r;
}
void init_OSMesa() {
const GLint z = 32, stencil = 0, accum = 0;
ctx = OSMesaCreateContextExt(OSMESA_BGRA, z, stencil, accum, NULL);
if (!ctx) {
Base::printm(M_ERROR, "OSMesaCreateContextExt() failed!\n");
return;
}
buffer = malloc(2048 * 2048 * 4);
if (!buffer) {
Base::printm(M_ERROR, "Alloc image buffer failed!\n");
return;
}
delete init_OSMesa_context(0, 300, 300);
glPopAttrib();
}
void flip_scene(scene_t * scene, const char * buffer, int width, int height, bool upscale) {
/* Make sure buffered commands are finished! */
glFinish();
Magick::Image i(width, height, "BGRA", Magick::CharPixel, buffer);
i.flip();
i.animationDelay(6);
i.opacity(0);
if (upscale) {
i.resize(Magick::Geometry(width / 2, height / 2));
}
scene->scene.push_back(i);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void * dump_scene_threaded(void * arg) {
scene_t * scene = (scene_t *) arg;
char fname[2049];
writeImages(scene->scene.begin(), scene->scene.end(), scene->tmp_filename.to_charp());
unlink(scene->filename.to_charp());
rename(scene->tmp_filename.to_charp(), scene->filename.to_charp());
delete scene;
pthread_exit(0);
}
void dump_scene(scene_t * scene, const String & filename, Handle * out, const char * buffer, int width, int height, bool upscale) {
if (scene->scene.begin() == scene->scene.end())
flip_scene(scene, buffer, width, height, upscale);
if (out) {
Magick::Blob b;
scene->scene.begin()->magick("JPEG");
scene->scene.begin()->quality(95);
writeImages(scene->scene.begin(), scene->scene.end(), &b);
out->write(b.data(), b.length());
delete scene;
} else {
pthread_t thread;
scene->filename = filename;
scene->tmp_filename = "tmp-" + filename;
if (pthread_create(&thread, NULL, dump_scene_threaded, scene) == 0) {
pthread_detach(thread);
}
}
glPopAttrib();
return;
}
class plugin_osmesa : public Base {
};
class Luaplugin_osmesa : public LuaObject {
public:
static void pushstatics(Lua *) throw (GeneralException);
protected:
virtual void pushmembers(Lua *);
};
enum plugin_osmesa_functions_t {
PLUGIN_OSMESA_INITOSMESA,
PLUGIN_OSMESA_FLIPSCENE,
PLUGIN_OSMESA_DUMPSCENE,
PLUGIN_OSMESA_LOADTEX,
};
struct lua_functypes_t plugin_osmesa_functions[] = {
{ PLUGIN_OSMESA_INITOSMESA, "InitOSMesa", 3, 4, { BLUA_USERDATA | BLUA_NIL, BLUA_NUMBER, BLUA_NUMBER, BLUA_BOOLEAN } },
{ PLUGIN_OSMESA_FLIPSCENE, "FlipScene", 1, 1, { BLUA_USERDATA } },
{ PLUGIN_OSMESA_DUMPSCENE, "DumpScene", 2, 2, { BLUA_USERDATA, BLUA_STRING | BLUA_OBJECT } },
{ PLUGIN_OSMESA_LOADTEX, "LoadTex", 1, 1, { BLUA_STRING } },
{ -1, 0, 0, 0, 0 }
};
class sLua_plugin_osmesa : public Base {
public:
DECLARE_FUNCTION(plugin_osmesa, PLUGIN_OSMESA_INITOSMESA);
DECLARE_FUNCTION(plugin_osmesa, PLUGIN_OSMESA_FLIPSCENE);
DECLARE_FUNCTION(plugin_osmesa, PLUGIN_OSMESA_DUMPSCENE);
DECLARE_FUNCTION(plugin_osmesa, PLUGIN_OSMESA_LOADTEX);
private:
static int plugin_osmesa_proceed(Lua * L, int n, plugin_osmesa * obj, int caller);
static int plugin_osmesa_proceed_statics(Lua * L, int n, int caller);
};
void Luaplugin_osmesa::pushmembers(Lua * L) { }
void Luaplugin_osmesa::pushstatics(Lua * L) throw (GeneralException) {
//CHECK_METHODS(plugin_osmesa);
CHECK_FUNCTIONS(plugin_osmesa);
PUSH_FUNCTION(plugin_osmesa, PLUGIN_OSMESA_INITOSMESA);
PUSH_FUNCTION(plugin_osmesa, PLUGIN_OSMESA_FLIPSCENE);
PUSH_FUNCTION(plugin_osmesa, PLUGIN_OSMESA_DUMPSCENE);
PUSH_FUNCTION(plugin_osmesa, PLUGIN_OSMESA_LOADTEX);
}
int sLua_plugin_osmesa::plugin_osmesa_proceed(Lua * L, int n, plugin_osmesa * obj, int caller) {
return 0;
}
int sLua_plugin_osmesa::plugin_osmesa_proceed_statics(Lua * L, int n, int caller) {
int _width, _height, r = 0;
String filename;
const char * t;
DDS_IMAGE_DATA * dds;
scene_t * scene = 0;
switch (caller) {
case PLUGIN_OSMESA_INITOSMESA:
if (!L->isnil(1)) {
scene = (scene_t *) L->touserdata(1);
}
_width = L->tonumber(2);
_height = L->tonumber(3);
if (n == 4) {
upscale = L->toboolean(4);
} else {
upscale = false;
}
L->push(init_OSMesa_context(scene, _width, _height));
r = 1;
break;
case PLUGIN_OSMESA_FLIPSCENE:
flip_scene((scene_t *) L->touserdata(1), (char *) buffer, width, height, upscale);
break;
case PLUGIN_OSMESA_DUMPSCENE:
if (L->isstring(2)) {
filename = L->tostring(2);
dump_scene((scene_t *) L->touserdata(1), filename, 0, (char *) buffer, width, height, upscale);
} else {
Handle * t = (Handle *) LuaObject::getme(L, 2);
dump_scene((scene_t *) L->touserdata(1), "", t, (char *) buffer, width, height, upscale);
}
break;
case PLUGIN_OSMESA_LOADTEX:
filename = L->tostring(1);
t = filename.to_charp();
dds = loadDDSTextureFile(t);
L->push((lua_Number) loadCompressedTexture(dds));
L->push((lua_Number) dds->width);
L->push((lua_Number) dds->height);
destroyDDS(dds);
r = 3;
break;
}
return r;
}
static void _init_plugin(Lua * L) {
init_OSMesa();
Luaplugin_osmesa::pushstatics(L);
}
extern "C" {
WEAK void init_plugin(Lua * L) {
_init_plugin(L);
}
void luaosmesa_init(Lua * L) {
_init_plugin(L);
}
}
|