summaryrefslogtreecommitdiff
path: root/src/static_libs/rg_etc
diff options
context:
space:
mode:
authorJean-Philippe Andre <jp.andre@samsung.com>2014-05-07 14:47:45 +0900
committerJean-Philippe Andre <jp.andre@samsung.com>2014-05-27 12:21:37 +0900
commit03505d1f81535f8cdb295c6fb7b1af3a60b0daa3 (patch)
tree5213734a829f95e22fc5b98c83d4a4dae5694851 /src/static_libs/rg_etc
parent4e358a582e17114d3bd047af759d261edfee0219 (diff)
Evas rg_etc2: Complete support for ETC2 with RGBA8_EAC
Add support for alpha in the ETC2 decoder.
Diffstat (limited to 'src/static_libs/rg_etc')
-rw-r--r--src/static_libs/rg_etc/rg_etc2.c150
1 files changed, 124 insertions, 26 deletions
diff --git a/src/static_libs/rg_etc/rg_etc2.c b/src/static_libs/rg_etc/rg_etc2.c
index 92044026e4..0aa43b0838 100644
--- a/src/static_libs/rg_etc/rg_etc2.c
+++ b/src/static_libs/rg_etc/rg_etc2.c
@@ -51,6 +51,26 @@ static const int kSigned3bit[8] = {
51 0, 1, 2, 3, -4, -3, -2, -1 51 0, 1, 2, 3, -4, -3, -2, -1
52}; 52};
53 53
54// For alpha support
55static const int kAlphaModifiers[16][8] = {
56 { -3, -6, -9, -15, 2, 5, 8, 14},
57 { -3, -7, -10, -13, 2, 6, 9, 12},
58 { -2, -5, -8, -13, 1, 4, 7, 12},
59 { -2, -4, -6, -13, 1, 3, 5, 12},
60 { -3, -6, -8, -12, 2, 5, 7, 11},
61 { -3, -7, -9, -11, 2, 6, 8, 10},
62 { -4, -7, -8, -11, 3, 6, 7, 10},
63 { -3, -5, -8, -11, 2, 4, 7, 10},
64 { -2, -6, -8, -10, 1, 5, 7, 9},
65 { -2, -5, -8, -10, 1, 4, 7, 9},
66 { -2, -4, -8, -10, 1, 3, 7, 9},
67 { -2, -5, -7, -10, 1, 4, 6, 9},
68 { -3, -4, -7, -10, 2, 3, 6, 9},
69 { -1, -2, -3, -10, 0, 1, 2, 9},
70 { -4, -6, -8, -9, 3, 5, 7, 8},
71 { -3, -5, -7, -9, 2, 4, 6, 8}
72};
73
54// Use with static constants so the compiler can optimize everything 74// Use with static constants so the compiler can optimize everything
55#define BITS(byteval, lowbit, highbit) \ 75#define BITS(byteval, lowbit, highbit) \
56 (((byteval) >> (lowbit)) & ((1 << ((highbit) - (lowbit) + 1)) - 1)) 76 (((byteval) >> (lowbit)) & ((1 << ((highbit) - (lowbit) + 1)) - 1))
@@ -67,6 +87,9 @@ static const int kSigned3bit[8] = {
67// Real clamp 87// Real clamp
68#define CLAMP(a) ({ int _b = (a); (((_b) >= 0) ? (((_b) < 256) ? (_b) : 255) : 0); }) 88#define CLAMP(a) ({ int _b = (a); (((_b) >= 0) ? (((_b) < 256) ? (_b) : 255) : 0); })
69 89
90// Simple min
91#define MIN(a,b) ({ int _z = (a), _y = (b); ((_z <= _y) ? _z : _y); })
92
70// Write a BGRA value for output to Evas 93// Write a BGRA value for output to Evas
71#define BGRA(r,g,b,a) ((a << 24) | (r << 16) | (g << 8) | b) 94#define BGRA(r,g,b,a) ((a << 24) | (r << 16) | (g << 8) | b)
72 95
@@ -75,6 +98,20 @@ static const int kSigned3bit[8] = {
75#define _6to8(a) ({ int _a = (a) & ((1 << 6) - 1); ((_a << 2) | ((_a >> 4) & 0x3)); }) 98#define _6to8(a) ({ int _a = (a) & ((1 << 6) - 1); ((_a << 2) | ((_a >> 4) & 0x3)); })
76#define _7to8(a) ({ int _a = (a) & ((1 << 7) - 1); ((_a << 1) | ((_a >> 6) & 0x1)); }) 99#define _7to8(a) ({ int _a = (a) & ((1 << 7) - 1); ((_a << 1) | ((_a >> 6) & 0x1)); })
77 100
101#ifndef WORDS_BIGENDIAN
102/* x86 */
103#define A_VAL(p) (((uint8_t *)(p))[3])
104#define R_VAL(p) (((uint8_t *)(p))[2])
105#define G_VAL(p) (((uint8_t *)(p))[1])
106#define B_VAL(p) (((uint8_t *)(p))[0])
107#else
108/* ppc */
109#define A_VAL(p) (((uint8_t *)(p))[0])
110#define R_VAL(p) (((uint8_t *)(p))[1])
111#define G_VAL(p) (((uint8_t *)(p))[2])
112#define B_VAL(p) (((uint8_t *)(p))[3])
113#endif
114
78 115
79static inline void 116static inline void
80_T_mode_color_read(const uint8_t *etc, uint32_t *paint_colors, int alpha) 117_T_mode_color_read(const uint8_t *etc, uint32_t *paint_colors, int alpha)
@@ -176,6 +213,36 @@ _planar_mode_color_read(const uint8_t *etc, uint32_t *bgra, int alpha)
176 } 213 }
177} 214}
178 215
216static inline void
217_TH_paint(const uint8_t *etc, uint32_t paint_colors[4], uint32_t *bgra)
218{
219 // Common code for modes T and H.
220
221 // a,b,c,d
222 bgra[ 0] = paint_colors[(BIT(etc[5], 0) << 1) | (BIT(etc[7], 0))];
223 bgra[ 4] = paint_colors[(BIT(etc[5], 1) << 1) | (BIT(etc[7], 1))];
224 bgra[ 8] = paint_colors[(BIT(etc[5], 2) << 1) | (BIT(etc[7], 2))];
225 bgra[12] = paint_colors[(BIT(etc[5], 3) << 1) | (BIT(etc[7], 3))];
226
227 // e,f,g,h
228 bgra[ 1] = paint_colors[(BIT(etc[5], 4) << 1) | (BIT(etc[7], 4))];
229 bgra[ 5] = paint_colors[(BIT(etc[5], 5) << 1) | (BIT(etc[7], 5))];
230 bgra[ 9] = paint_colors[(BIT(etc[5], 6) << 1) | (BIT(etc[7], 6))];
231 bgra[13] = paint_colors[(BIT(etc[5], 7) << 1) | (BIT(etc[7], 7))];
232
233 // i,j,k,l
234 bgra[ 2] = paint_colors[(BIT(etc[4], 0) << 1) | (BIT(etc[6], 0))];
235 bgra[ 6] = paint_colors[(BIT(etc[4], 1) << 1) | (BIT(etc[6], 1))];
236 bgra[10] = paint_colors[(BIT(etc[4], 2) << 1) | (BIT(etc[6], 2))];
237 bgra[14] = paint_colors[(BIT(etc[4], 3) << 1) | (BIT(etc[6], 3))];
238
239 // m,n,o,p
240 bgra[ 3] = paint_colors[(BIT(etc[4], 4) << 1) | (BIT(etc[6], 4))];
241 bgra[ 7] = paint_colors[(BIT(etc[4], 5) << 1) | (BIT(etc[6], 5))];
242 bgra[11] = paint_colors[(BIT(etc[4], 6) << 1) | (BIT(etc[6], 6))];
243 bgra[15] = paint_colors[(BIT(etc[4], 7) << 1) | (BIT(etc[6], 7))];
244}
245
179void 246void
180rg_etc2_rgb8_decode_block(const uint8_t *etc, uint32_t *bgra) 247rg_etc2_rgb8_decode_block(const uint8_t *etc, uint32_t *bgra)
181{ 248{
@@ -196,13 +263,15 @@ rg_etc2_rgb8_decode_block(const uint8_t *etc, uint32_t *bgra)
196 { 263 {
197 // T mode 264 // T mode
198 _T_mode_color_read(etc, paint_colors, 255); 265 _T_mode_color_read(etc, paint_colors, 255);
199 goto th_mode; 266 _TH_paint(etc, paint_colors, bgra);
267 return;
200 } 268 }
201 if ((G + dG) < 0 || (G + dG) >= 32) 269 if ((G + dG) < 0 || (G + dG) >= 32)
202 { 270 {
203 // H mode 271 // H mode
204 _H_mode_color_read(etc, paint_colors, 255); 272 _H_mode_color_read(etc, paint_colors, 255);
205 goto th_mode; 273 _TH_paint(etc, paint_colors, bgra);
274 return;
206 } 275 }
207 if ((B + dB) < 0 || (B + dB) >= 32) 276 if ((B + dB) < 0 || (B + dB) >= 32)
208 { 277 {
@@ -215,33 +284,62 @@ etc1:
215 // Valid differential mode or individual mode: ETC1 284 // Valid differential mode or individual mode: ETC1
216 if (!rg_etc1_unpack_block(etc, bgra, 0)) 285 if (!rg_etc1_unpack_block(etc, bgra, 0))
217 fprintf(stderr, "ETC2: Something very strange is happening here!\n"); 286 fprintf(stderr, "ETC2: Something very strange is happening here!\n");
218 return; 287}
219 288
220th_mode: 289void
221 // Common code for modes T and H. 290rg_etc2_rgba8_decode_block(const uint8_t *etc, uint32_t *bgra)
291{
292 const uint8_t zeros[7] = {0};
293 uint32_t table_index;
294 int base_codeword;
295 int multiplier;
222 296
223 // a,b,c,d 297 base_codeword = etc[0];
224 bgra[ 0] = paint_colors[(BIT(etc[5], 0) << 1) | (BIT(etc[7], 0))];
225 bgra[ 4] = paint_colors[(BIT(etc[5], 1) << 1) | (BIT(etc[7], 1))];
226 bgra[ 8] = paint_colors[(BIT(etc[5], 2) << 1) | (BIT(etc[7], 2))];
227 bgra[12] = paint_colors[(BIT(etc[5], 3) << 1) | (BIT(etc[7], 3))];
228 298
229 // e,f,g,h 299 // Fast path if alpha is the same for all pixels
230 bgra[ 1] = paint_colors[(BIT(etc[5], 4) << 1) | (BIT(etc[7], 4))]; 300 if (!memcmp(etc + 1, zeros, 7))
231 bgra[ 5] = paint_colors[(BIT(etc[5], 5) << 1) | (BIT(etc[7], 5))]; 301 {
232 bgra[ 9] = paint_colors[(BIT(etc[5], 6) << 1) | (BIT(etc[7], 6))]; 302 if (!base_codeword)
233 bgra[13] = paint_colors[(BIT(etc[5], 7) << 1) | (BIT(etc[7], 7))]; 303 memset(bgra, 0, 64);
304 else
305 {
306 rg_etc2_rgb8_decode_block(etc + 8, bgra);
307 if (base_codeword != 255)
308 for (int k = 0; k < 16; k++)
309 {
310 const uint32_t rgb = *bgra;
311 const int R = MIN(R_VAL(&rgb), base_codeword);
312 const int G = MIN(G_VAL(&rgb), base_codeword);
313 const int B = MIN(B_VAL(&rgb), base_codeword);
314 *bgra++ = BGRA(R, G, B, base_codeword);
315 }
316 }
317 return;
318 }
234 319
235 // i,j,k,l 320 rg_etc2_rgb8_decode_block(etc + 8, bgra);
236 bgra[ 2] = paint_colors[(BIT(etc[4], 0) << 1) | (BIT(etc[6], 0))];
237 bgra[ 6] = paint_colors[(BIT(etc[4], 1) << 1) | (BIT(etc[6], 1))];
238 bgra[10] = paint_colors[(BIT(etc[4], 2) << 1) | (BIT(etc[6], 2))];
239 bgra[14] = paint_colors[(BIT(etc[4], 3) << 1) | (BIT(etc[6], 3))];
240 321
241 // m,n,o,p 322 multiplier = BITS(etc[1], 4, 7);
242 bgra[ 3] = paint_colors[(BIT(etc[4], 4) << 1) | (BIT(etc[6], 4))]; 323 table_index = BITS(etc[1], 0, 3);
243 bgra[ 7] = paint_colors[(BIT(etc[4], 5) << 1) | (BIT(etc[6], 5))]; 324
244 bgra[11] = paint_colors[(BIT(etc[4], 6) << 1) | (BIT(etc[6], 6))]; 325 for (int x = 0, k = 0; x < 4; x++)
245 bgra[15] = paint_colors[(BIT(etc[4], 7) << 1) | (BIT(etc[6], 7))]; 326 for (int y = 0; y < 4; y++, k += 3)
246 return; 327 {
328 const uint32_t byte = (k >> 3); // = [k/8]
329 const uint32_t bit = k - (byte << 3); // = k%8
330 const uint32_t rgb = bgra[(y << 2) + x];
331 uint32_t index, alpha, R, G, B;
332
333 if (bit < 6)
334 index = BITS(etc[byte + 2], 5 - bit, 7 - bit);
335 else if (bit == 6)
336 index = (BITS(etc[byte + 2], 0, 1) << 1) | BIT(etc[byte + 3], 7);
337 else // bit == 7
338 index = (BIT(etc[byte + 2], 0) << 2) | BITS(etc[byte + 3], 6, 7);
339 alpha = CLAMP(base_codeword + kAlphaModifiers[table_index][index] * multiplier);
340 R = MIN(R_VAL(&rgb), alpha);
341 G = MIN(G_VAL(&rgb), alpha);
342 B = MIN(B_VAL(&rgb), alpha);
343 bgra[(y << 2) + x] = BGRA(R, G, B, alpha);
344 }
247} 345}