-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgl-rgb.c
310 lines (241 loc) · 10.6 KB
/
gl-rgb.c
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
/* KallistiGL for KallistiOS ##version##
libgl/gl-rgb.c
Copyright (C) 2013-2015 Josh Pearson
A set of functions for working with ARGB pixel data, used by gluBuild2DMipmaps(...).
The pixel conversion functions are for submitting textures not supported by the PVR.
*/
#include "gl.h"
#include "gl-rgb.h"
//===================================================================================================//
//== ARGB Bit Masks ==//
static unsigned char ARGB1555_ALPHA(unsigned short c) {
return (c & ARGB1555_ALPHA_MASK) >> ARGB1555_ALPHA_SHIFT;
}
static unsigned char ARGB1555_RED(unsigned short c) {
return (c & ARGB1555_RED_MASK) >> ARGB1555_RED_SHIFT;
}
static unsigned char ARGB1555_GREEN(unsigned short c) {
return (c & ARGB1555_GREEN_MASK) >> ARGB1555_GREEN_SHIFT;
}
static unsigned char ARGB1555_BLUE(unsigned short c) {
return (c & ARGB1555_BLUE_MASK) >> ARGB1555_BLUE_SHIFT;
}
static unsigned char ARGB4444_ALPHA(unsigned short c) {
return (c & ARGB4444_ALPHA_MASK) >> ARGB4444_ALPHA_SHIFT;
}
static unsigned char ARGB4444_RED(unsigned short c) {
return (c & ARGB4444_RED_MASK) >> ARGB4444_RED_SHIFT;
}
static unsigned char ARGB4444_GREEN(unsigned short c) {
return (c & ARGB4444_GREEN_MASK) >> ARGB4444_GREEN_SHIFT;
}
static unsigned char ARGB4444_BLUE(unsigned short c) {
return (c & ARGB4444_BLUE_MASK) >> ARGB4444_BLUE_SHIFT;
}
static unsigned char RGB565_RED(unsigned short c) {
return (c & RGB565_RED_MASK) >> RGB565_RED_SHIFT;
}
static unsigned char RGB565_GREEN(unsigned short c) {
return (c & RGB565_GREEN_MASK) >> RGB565_GREEN_SHIFT;
}
static unsigned char RGB565_BLUE(unsigned short c) {
return c & RGB565_BLUE_MASK;
}
//===================================================================================================//
//== Block Compression ==//
uint16 __glKosAverageQuadPixelRGB565(uint16 p1, uint16 p2, uint16 p3, uint16 p4) {
uint8 R = (RGB565_RED(p1) + RGB565_RED(p2) + RGB565_RED(p3) + RGB565_RED(p4)) / 4;
uint8 G = (RGB565_GREEN(p1) + RGB565_GREEN(p2) + RGB565_GREEN(p3) + RGB565_GREEN(p4)) / 4;
uint8 B = (RGB565_BLUE(p1) + RGB565_BLUE(p2) + RGB565_BLUE(p3) + RGB565_BLUE(p4)) / 4;
return R << RGB565_RED_SHIFT | G << RGB565_GREEN_SHIFT | B;
}
uint16 __glKosAverageQuadPixelARGB1555(uint16 p1, uint16 p2, uint16 p3, uint16 p4) {
uint8 A = (ARGB1555_ALPHA(p1) + ARGB1555_ALPHA(p2) + ARGB1555_ALPHA(p3) + ARGB1555_ALPHA(p4)) / 4;
uint8 R = (ARGB1555_RED(p1) + ARGB1555_RED(p2) + ARGB1555_RED(p3) + ARGB1555_RED(p4)) / 4;
uint8 G = (ARGB1555_GREEN(p1) + ARGB1555_GREEN(p2) + ARGB1555_GREEN(p3) + ARGB1555_GREEN(p4)) / 4;
uint8 B = (ARGB1555_BLUE(p1) + ARGB1555_BLUE(p2) + ARGB1555_BLUE(p3) + ARGB1555_BLUE(p4)) / 4;
return ((A & RGB1_MAX) << ARGB1555_ALPHA_SHIFT) | ((R & RGB5_MAX) << ARGB1555_RED_SHIFT)
| ((G & RGB5_MAX) << ARGB1555_GREEN_SHIFT) | (B & RGB5_MAX);
}
uint16 __glKosAverageQuadPixelARGB4444(uint16 p1, uint16 p2, uint16 p3, uint16 p4) {
uint8 A = (ARGB4444_ALPHA(p1) + ARGB4444_ALPHA(p2) + ARGB4444_ALPHA(p3) + ARGB4444_ALPHA(p4)) / 4;
uint8 R = (ARGB4444_RED(p1) + ARGB4444_RED(p2) + ARGB4444_RED(p3) + ARGB4444_RED(p4)) / 4;
uint8 G = (ARGB4444_GREEN(p1) + ARGB4444_GREEN(p2) + ARGB4444_GREEN(p3) + ARGB4444_GREEN(p4)) / 4;
uint8 B = (ARGB4444_BLUE(p1) + ARGB4444_BLUE(p2) + ARGB4444_BLUE(p3) + ARGB4444_BLUE(p4)) / 4;
return ((A & RGB4_MAX) << ARGB4444_ALPHA_SHIFT) | ((R & RGB4_MAX) << ARGB4444_RED_SHIFT)
| ((G & RGB4_MAX) << ARGB4444_GREEN_SHIFT) | (B & RGB4_MAX);
}
uint16 __glKosAverageBiPixelRGB565(uint16 p1, uint16 p2) {
uint8 R = (RGB565_RED(p1) + RGB565_RED(p2)) / 2;
uint8 G = (RGB565_GREEN(p1) + RGB565_GREEN(p2)) / 2;
uint8 B = (RGB565_BLUE(p1) + RGB565_BLUE(p2)) / 2;
return R << RGB565_RED_SHIFT | G << RGB565_GREEN_SHIFT | B;
}
uint16 __glKosAverageBiPixelARGB1555(uint16 p1, uint16 p2) {
uint8 A = (ARGB1555_ALPHA(p1) + ARGB1555_ALPHA(p2)) / 2;
uint8 R = (ARGB1555_RED(p1) + ARGB1555_RED(p2)) / 2;
uint8 G = (ARGB1555_GREEN(p1) + ARGB1555_GREEN(p2)) / 2;
uint8 B = (ARGB1555_BLUE(p1) + ARGB1555_BLUE(p2)) / 2;
return ((A & RGB1_MAX) << ARGB1555_ALPHA_SHIFT) | ((R & RGB5_MAX) << ARGB1555_RED_SHIFT)
| ((G & RGB5_MAX) << ARGB1555_GREEN_SHIFT) | (B & RGB5_MAX);
}
uint16 __glKosAverageBiPixelARGB4444(uint16 p1, uint16 p2) {
uint8 A = (ARGB4444_ALPHA(p1) + ARGB4444_ALPHA(p2)) / 2;
uint8 R = (ARGB4444_RED(p1) + ARGB4444_RED(p2)) / 2;
uint8 G = (ARGB4444_GREEN(p1) + ARGB4444_GREEN(p2)) / 2;
uint8 B = (ARGB4444_BLUE(p1) + ARGB4444_BLUE(p2)) / 2;
return ((A & RGB4_MAX) << ARGB4444_ALPHA_SHIFT) | ((R & RGB4_MAX) << ARGB4444_RED_SHIFT)
| ((G & RGB4_MAX) << ARGB4444_GREEN_SHIFT) | (B & RGB4_MAX);
}
//===================================================================================================//
//== Colorspace Conversion ==//
static uint16 _glKosConvPixelRGBAU32(uint8 r, uint8 g, uint8 b, uint8 a) {
return (uint16)((a & RGB4_MAX) << ARGB4444_ALPHA_SHIFT) |
((r & RGB4_MAX) << ARGB4444_RED_SHIFT) |
((g & RGB4_MAX) << ARGB4444_GREEN_SHIFT) |
((b & RGB4_MAX));
}
static uint16 _glKosConvPixelRGBU24(uint8 r, uint8 g, uint8 b) {
return (uint16)((r & RGB5_MAX) << RGB565_RED_SHIFT) |
((g & RGB6_MAX) << RGB565_GREEN_SHIFT) |
((b & RGB5_MAX));
}
static void _glKosConvPixelsRGBF(int w, int h, float *src, uint16 *dst) {
int i;
for(i = 0; i < w * h; i++) {
dst[i] = _glKosConvPixelRGBU24((uint8)(src[i * 3 + 0] * RGB5_MAX),
(uint8)(src[i * 3 + 1] * RGB6_MAX),
(uint8)(src[i * 3 + 2] * RGB5_MAX));
}
}
static void _glKosConvPixelsRGBAF(int w, int h, float *src, uint16 *dst) {
int i;
for(i = 0; i < w * h; i++) {
dst[i] = _glKosConvPixelRGBAU32((uint8)(src[i * 4 + 0] * RGB4_MAX),
(uint8)(src[i * 4 + 1] * RGB4_MAX),
(uint8)(src[i * 4 + 2] * RGB4_MAX),
(uint8)(src[i * 4 + 3] * RGB4_MAX));
}
}
static void _glKosConvPixelsRGBU24(int w, int h, uint8 *src, uint16 *dst) {
unsigned char r, g, b;
int i;
for(i = 0; i < w * h; i++) {
r = (src[i * 3 + 0] * RGB5_MAX) / RGB8_MAX;
g = (src[i * 3 + 1] * RGB6_MAX) / RGB8_MAX;
b = (src[i * 3 + 2] * RGB5_MAX) / RGB8_MAX;
dst[i] = _glKosConvPixelRGBU24(r, g, b);
}
}
static void _glKosConvPixelsRGBAU32(int w, int h, uint8 *src, uint16 *dst) {
unsigned char r, g, b, a;
int i;
for(i = 0; i < w * h; i++) {
r = (src[i * 4 + 0] * RGB4_MAX) / RGB8_MAX;
g = (src[i * 4 + 1] * RGB4_MAX) / RGB8_MAX;
b = (src[i * 4 + 2] * RGB4_MAX) / RGB8_MAX;
a = (src[i * 4 + 3] * RGB4_MAX) / RGB8_MAX;
dst[i] = _glKosConvPixelRGBAU32(r, g, b, a);
}
}
static void _glKosConvPixelsRGBS24(int w, int h, int8 *src, uint16 *dst) {
unsigned char r, g, b;
int i;
for(i = 0; i < w * h; i++) {
r = ((src[i * 3 + 0] + S8_NEG_OFT) * RGB5_MAX) / RGB8_MAX;
g = ((src[i * 3 + 1] + S8_NEG_OFT) * RGB6_MAX) / RGB8_MAX;
b = ((src[i * 3 + 2] + S8_NEG_OFT) * RGB5_MAX) / RGB8_MAX;
dst[i] = _glKosConvPixelRGBU24(r, g, b);
}
}
static void _glKosConvPixelsRGBAS32(int w, int h, int8 *src, uint16 *dst) {
unsigned char r, g, b, a;
int i;
for(i = 0; i < w * h; i++) {
r = ((src[i * 4 + 0] + S8_NEG_OFT) * RGB4_MAX) / RGB8_MAX;
g = ((src[i * 4 + 1] + S8_NEG_OFT) * RGB4_MAX) / RGB8_MAX;
b = ((src[i * 4 + 2] + S8_NEG_OFT) * RGB4_MAX) / RGB8_MAX;
a = ((src[i * 4 + 3] + S8_NEG_OFT) * RGB4_MAX) / RGB8_MAX;
dst[i] = _glKosConvPixelRGBAU32(r, g, b, a);
}
}
static void _glKosConvPixelsRGBS48(int w, int h, int16 *src, uint16 *dst) {
unsigned char r, g, b;
int i;
for(i = 0; i < w * h; i++) {
r = ((src[i * 3 + 0] + S16_NEG_OFT) * RGB5_MAX) / RGB16_MAX;
g = ((src[i * 3 + 1] + S16_NEG_OFT) * RGB6_MAX) / RGB16_MAX;
b = ((src[i * 3 + 2] + S16_NEG_OFT) * RGB5_MAX) / RGB16_MAX;
dst[i] = _glKosConvPixelRGBU24(r, g, b);
}
}
static void _glKosConvPixelsRGBAS64(int w, int h, int16 *src, uint16 *dst) {
unsigned char r, g, b, a;
int i;
for(i = 0; i < w * h; i++) {
r = ((src[i * 4 + 0] + S16_NEG_OFT) * RGB4_MAX) / RGB16_MAX;
g = ((src[i * 4 + 1] + S16_NEG_OFT) * RGB4_MAX) / RGB16_MAX;
b = ((src[i * 4 + 2] + S16_NEG_OFT) * RGB4_MAX) / RGB16_MAX;
a = ((src[i * 4 + 3] + S16_NEG_OFT) * RGB4_MAX) / RGB16_MAX;
dst[i] = _glKosConvPixelRGBAU32(r, g, b, a);
}
}
static void _glKosConvPixelsRGBU48(int w, int h, uint16 *src, uint16 *dst) {
unsigned char r, g, b;
int i;
for(i = 0; i < w * h; i++) {
r = ((src[i * 3 + 0]) * RGB5_MAX) / RGB16_MAX;
g = ((src[i * 3 + 1]) * RGB6_MAX) / RGB16_MAX;
b = ((src[i * 3 + 2]) * RGB5_MAX) / RGB16_MAX;
dst[i] = _glKosConvPixelRGBU24(r, g, b);
}
}
static void _glKosConvPixelsRGBAU64(int w, int h, uint16 *src, uint16 *dst) {
unsigned char r, g, b, a;
int i;
for(i = 0; i < w * h; i++) {
r = (src[i * 4 + 0] * RGB4_MAX) / RGB16_MAX;
g = (src[i * 4 + 1] * RGB4_MAX) / RGB16_MAX;
b = (src[i * 4 + 2] * RGB4_MAX) / RGB16_MAX;
a = (src[i * 4 + 3] * RGB4_MAX) / RGB16_MAX;
dst[i] = _glKosConvPixelRGBAU32(r, g, b, a);
}
}
void _glKosPixelConvertRGB(int format, int w, int h, void *src, uint16 *dst) {
switch(format) {
case GL_BYTE:
_glKosConvPixelsRGBS24(w, h, (int8 *)src, dst);
break;
case GL_UNSIGNED_BYTE:
_glKosConvPixelsRGBU24(w, h, (uint8 *)src, dst);
break;
case GL_SHORT:
_glKosConvPixelsRGBS48(w, h, (int16 *)src, dst);
break;
case GL_UNSIGNED_SHORT:
_glKosConvPixelsRGBU48(w, h, (uint16 *)src, dst);
break;
case GL_FLOAT:
_glKosConvPixelsRGBF(w, h, (float *)src, dst);
break;
}
}
void _glKosPixelConvertRGBA(int format, int w, int h, void *src, uint16 *dst) {
switch(format) {
case GL_BYTE:
_glKosConvPixelsRGBAS32(w, h, (int8 *)src, dst);
break;
case GL_UNSIGNED_BYTE:
_glKosConvPixelsRGBAU32(w, h, (uint8 *)src, dst);
break;
case GL_SHORT:
_glKosConvPixelsRGBAS64(w, h, (int16 *)src, dst);
break;
case GL_UNSIGNED_SHORT:
_glKosConvPixelsRGBAU64(w, h, (uint16 *)src, dst);
break;
case GL_FLOAT:
_glKosConvPixelsRGBAF(w, h, (float *)src, dst);
break;
}
}