Repository navigation
Expand file tree
/
Copy pathVideoTexture.m
More file actions
312 lines (260 loc) · 8.92 KB
/
Copy pathVideoTexture.m
File metadata and controls
312 lines (260 loc) · 8.92 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
glgif
VideoTexture - wrapper for uploading OpenGLES textures.
Copyright (C) 2009 James S Urquhart
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#import "VideoTexture.h"
// GL_LINEAR == interpolates, GL_NEAREST == blocky
static GLint sMinFilter = GL_NEAREST;
static GLint sMagFilter = GL_NEAREST;
void VideoTexture_filter(GLint filter)
{
sMinFilter = sMagFilter = filter;
}
VideoTexture *VideoTexture_init(int width, int height, GLint fmt) {
VideoTexture *build = malloc(sizeof(VideoTexture));
build->width = width;
build->height = height;
build->format = fmt;
build->size = VideoTexture_sizeOfTexture(fmt, width, height, 0);
build->data = (char*)malloc(build->size);
// Clear base
int i;
int mipmaplevels = 0;
char *ptr = build->data;
int sw = width;
int sh = height;
int sz = VideoTexture_sizeOfTexture(fmt, sw, sh, 0);
memset(ptr, 0, sz);
ptr += sz;
// Clear mips
for (i=0; i<mipmaplevels; i++) {
sw >>= 1;
sh >>= 1;
sz = VideoTexture_sizeOfTexture(fmt, sw, sh, 0);
memset(ptr, 0, sz);
ptr += sz;
}
VideoTexture_load(build);
return build;
}
void VideoTexture_release(VideoTexture *tex)
{
if (tex->tex)
{
glDeleteTextures(1, &tex->tex);
tex->tex = 0;
}
if (tex->data) {
free(tex->data);
tex->data = NULL;
}
free(tex);
}
bool VideoTexture_lock(VideoTexture *tex) {
if (tex->data)
return false;
tex->size = VideoTexture_sizeOfTexture(tex->format, tex->width, tex->height, 0);
tex->data = malloc(tex->size);
return true;
}
bool VideoTexture_unlock(VideoTexture *tex) {
glBindTexture( GL_TEXTURE_2D, tex->tex );
{
// Re-load to card
if (VideoTexture_compressed(tex->format)) {
glCompressedTexImage2D ( GL_TEXTURE_2D,
0, // start level
tex->format,
tex->width,
tex->height,
0,
tex->size,
tex->data );
} else {
glTexSubImage2D( GL_TEXTURE_2D,
0, // level
0, 0, // x,y offset
tex->width,
tex->height,
tex->format,
GL_UNSIGNED_BYTE,
tex->data);
}
// Clean up data!
free(tex->data);
tex->data = NULL;
tex->size = 0;
}
return true;
}
bool VideoTexture_load(VideoTexture *tex) {
glGenTextures(1, &tex->tex);
glBindTexture( GL_TEXTURE_2D, tex->tex );
// Filters
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, sMagFilter );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, sMinFilter );
// Wrap
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if (VideoTexture_compressed(tex->format)) {
glCompressedTexImage2D ( GL_TEXTURE_2D,
0, // level
tex->format,
tex->width,
tex->height,
0, // border
tex->size,
tex->data );
} else {
glTexImage2D( GL_TEXTURE_2D,
0, // level
tex->format,
tex->width,
tex->height,
0, // levels
tex->format,
GL_UNSIGNED_BYTE,
tex->data);
}
free(tex->data);
tex->data = NULL;
tex->size = 0;
return true;
}
bool VideoTexture_compressed(GLint fmt)
{
if (fmt == GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG ||
fmt == GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG ||
fmt == GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ||
fmt == GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG ||
fmt == GL_PALETTE4_RGB8_OES ||
fmt == GL_PALETTE4_RGBA8_OES ||
fmt == GL_PALETTE4_R5_G6_B5_OES ||
fmt == GL_PALETTE4_RGBA4_OES ||
fmt == GL_PALETTE4_RGB5_A1_OES ||
fmt == GL_PALETTE8_RGB8_OES ||
fmt == GL_PALETTE8_RGBA8_OES ||
fmt == GL_PALETTE8_R5_G6_B5_OES ||
fmt == GL_PALETTE8_RGBA4_OES ||
fmt == GL_PALETTE8_RGB5_A1_OES)
return true;
else
return false;
}
int VideoTexture_sizeOfTexture(GLint format, int width, int height, int mipmaplevels)
{
int base = 0; // e.g. palette size
int bpp = 0;
/*
16 colours of format:
#define GL_PALETTE4_RGB8_OES 0x8B90 (16*3) + ((width*height)/2) // best
#define GL_PALETTE4_RGBA8_OES 0x8B91 (16*4) + ((width*height)/2)
#define GL_PALETTE4_R5_G6_B5_OES 0x8B92 (16*2) + ((width*height)/2) // second best
#define GL_PALETTE4_RGBA4_OES 0x8B93 (16*2) + ((width*height)/2)
#define GL_PALETTE4_RGB5_A1_OES 0x8B94 (16*2) + ((width*height)/2)
256 colours of format:
#define GL_PALETTE8_RGB8_OES 0x8B95 (256*3) + (width*height) // best
#define GL_PALETTE8_RGBA8_OES 0x8B96 (256*4) + (width*height)
#define GL_PALETTE8_R5_G6_B5_OES 0x8B97 (256*2) + (width*height) // second best
#define GL_PALETTE8_RGBA4_OES 0x8B98 (256*2) + (width*height)
#define GL_PALETTE8_RGB5_A1_OES 0x8B99 (256*2) + (width*height)
*/
switch (format)
{
// PVRTC compressed formats
case GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
bpp = 4;
case GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
bpp = 2;
break;
case GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
bpp = 4;
break;
case GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
bpp = 2;
break;
// Palette formats
case GL_PALETTE8_RGB8_OES:
bpp = 8;
base = 256*3;
break;
case GL_PALETTE8_RGBA8_OES:
bpp = 8;
base = 256*4;
break;
case GL_PALETTE8_R5_G6_B5_OES:
case GL_PALETTE8_RGBA4_OES:
case GL_PALETTE8_RGB5_A1_OES:
bpp = 8;
base = 256*2;
break;
case GL_PALETTE4_RGB8_OES:
bpp = 4;
base = 16*3;
break;
case GL_PALETTE4_RGBA8_OES:
bpp = 4;
base = 16*4;
break;
case GL_PALETTE4_R5_G6_B5_OES:
case GL_PALETTE4_RGBA4_OES:
case GL_PALETTE4_RGB5_A1_OES:
bpp = 4;
base = 16*2;
break;
// RGB
case GL_ALPHA:
case GL_LUMINANCE:
bpp = 8;
break;
case GL_LUMINANCE_ALPHA:
bpp = 16;
break;
case GL_RGB:
bpp = 24;
break;
case GL_RGBA:
case GL_BGRA:
bpp = 32;
break;
}
if (mipmaplevels > 0) {
// Calculate size including mipmaps
int sw = height;
int sh = height;
int sz = base + (width * height * bpp / 8);
int count = mipmaplevels;
if (count < 0)
return sz;
while (count-- != 0) {
sw >>= 1;
sh >>= 1;
if (sw < 1)
sw = 1;
if (sh < 1)
sh = 1;
sz += base + (sw * sh * bpp / 8);
}
return sz;
} else
return base + (width * height * bpp / 8);
}