improve image decoders
This commit is contained in:
+114
-4
@@ -6,6 +6,10 @@
|
||||
const uint_fast8_t WriteOrderTable[16] = { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 };
|
||||
const uint_fast8_t WriteOrderTableRev[16] = { 15, 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0 };
|
||||
const uint_fast8_t Etc1ModifierTable[8][2] = { { 2, 8 }, { 5, 17 }, { 9, 29 }, { 13, 42 }, { 18, 60 }, { 24, 80 }, { 33, 106 }, { 47, 183 } };
|
||||
const uint_fast8_t Etc2aModifierTable[2][8][2] = {
|
||||
{ { 0, 8 }, { 0, 17 }, { 0, 29 }, { 0, 42 }, { 0, 60 }, { 0, 80 }, { 0, 106 }, { 0, 183 } },
|
||||
{ { 2, 8 }, { 5, 17 }, { 9, 29 }, { 13, 42 }, { 18, 60 }, { 24, 80 }, { 33, 106 }, { 47, 183 } }
|
||||
};
|
||||
const uint_fast8_t Etc1SubblockTable[2][16] = { { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, { 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1 } };
|
||||
const uint_fast8_t Etc2DistanceTable[8] = { 3, 6, 11, 16, 23, 32, 41, 64 };
|
||||
const int_fast8_t Etc2AlphaModTable[16][8] = {
|
||||
@@ -27,6 +31,12 @@ const int_fast8_t Etc2AlphaModTable[16][8] = {
|
||||
{ -3, -5, -7, -9, 2, 4, 6, 8 }
|
||||
};
|
||||
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
static const uint_fast32_t TRANSPARENT_MASK = 0x00ffffff;
|
||||
#else
|
||||
static const uint_fast32_t TRANSPARENT_MASK = 0xffffff00;
|
||||
#endif
|
||||
|
||||
static inline uint_fast32_t color(uint_fast8_t r, uint_fast8_t g, uint_fast8_t b, uint_fast8_t a)
|
||||
{
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
@@ -46,6 +56,11 @@ static inline uint32_t applicate_color(uint_fast8_t c[3], int_fast16_t m)
|
||||
return color(clamp(c[0] + m), clamp(c[1] + m), clamp(c[2] + m), 255);
|
||||
}
|
||||
|
||||
static inline uint32_t applicate_color_alpha(uint_fast8_t c[3], int_fast16_t m, int transparent)
|
||||
{
|
||||
return color(clamp(c[0] + m), clamp(c[1] + m), clamp(c[2] + m), transparent ? 0 : 255);
|
||||
}
|
||||
|
||||
static inline uint32_t applicate_color_raw(uint_fast8_t c[3])
|
||||
{
|
||||
return color(c[0], c[1], c[2], 255);
|
||||
@@ -212,6 +227,103 @@ static inline void decode_etc2_block(const uint8_t* data, uint32_t* outbuf)
|
||||
}
|
||||
}
|
||||
|
||||
static inline void decode_etc2a1_block(const uint8_t* data, uint32_t* outbuf)
|
||||
{
|
||||
uint_fast16_t j = data[6] << 8 | data[7]; // 15 -> 0
|
||||
uint_fast32_t k = data[4] << 8 | data[5]; // 31 -> 16
|
||||
uint_fast8_t c[3][3] = {};
|
||||
|
||||
int obaq = data[3] >> 1 & 1;
|
||||
|
||||
// diff bit == 1
|
||||
uint_fast8_t r = data[0] & 0xf8;
|
||||
int_fast16_t dr = (data[0] << 3 & 0x18) - (data[0] << 3 & 0x20);
|
||||
uint_fast8_t g = data[1] & 0xf8;
|
||||
int_fast16_t dg = (data[1] << 3 & 0x18) - (data[1] << 3 & 0x20);
|
||||
uint_fast8_t b = data[2] & 0xf8;
|
||||
int_fast16_t db = (data[2] << 3 & 0x18) - (data[2] << 3 & 0x20);
|
||||
if (r + dr < 0 || r + dr > 255) {
|
||||
// T
|
||||
c[0][0] = (data[0] << 3 & 0xc0) | (data[0] << 4 & 0x30) | (data[0] >> 1 & 0xc) | (data[0] & 3);
|
||||
c[0][1] = (data[1] & 0xf0) | data[1] >> 4;
|
||||
c[0][2] = (data[1] & 0x0f) | data[1] << 4;
|
||||
c[1][0] = (data[2] & 0xf0) | data[2] >> 4;
|
||||
c[1][1] = (data[2] & 0x0f) | data[2] << 4;
|
||||
c[1][2] = (data[3] & 0xf0) | data[3] >> 4;
|
||||
const uint_fast8_t d = Etc2DistanceTable[(data[3] >> 1 & 6) | (data[3] & 1)];
|
||||
uint_fast32_t color_set[4] = { applicate_color_raw(c[0]), applicate_color(c[1], d), applicate_color_raw(c[1]), applicate_color(c[1], -d) };
|
||||
k <<= 1;
|
||||
for (int i = 0; i < 16; i++, j >>= 1, k >>= 1) {
|
||||
int index = (k & 2) | (j & 1);
|
||||
outbuf[WriteOrderTable[i]] = color_set[index];
|
||||
if (!obaq && index == 2)
|
||||
outbuf[WriteOrderTable[i]] &= TRANSPARENT_MASK;
|
||||
}
|
||||
} else if (g + dg < 0 || g + dg > 255) {
|
||||
// H
|
||||
c[0][0] = (data[0] << 1 & 0xf0) | (data[0] >> 3 & 0xf);
|
||||
c[0][1] = (data[0] << 5 & 0xe0) | (data[1] & 0x10);
|
||||
c[0][1] |= c[0][1] >> 4;
|
||||
c[0][2] = (data[1] & 8) | (data[1] << 1 & 6) | data[2] >> 7;
|
||||
c[0][2] |= c[0][2] << 4;
|
||||
c[1][0] = (data[2] << 1 & 0xf0) | (data[2] >> 3 & 0xf);
|
||||
c[1][1] = (data[2] << 5 & 0xe0) | (data[3] >> 3 & 0x10);
|
||||
c[1][1] |= c[1][1] >> 4;
|
||||
c[1][2] = (data[3] << 1 & 0xf0) | (data[3] >> 3 & 0xf);
|
||||
uint_fast8_t d = (data[3] & 4) | (data[3] << 1 & 2);
|
||||
if (c[0][0] > c[1][0] || (c[0][0] == c[1][0] && (c[0][1] > c[1][1] || (c[0][1] == c[1][1] && c[0][2] >= c[1][2]))))
|
||||
++d;
|
||||
d = Etc2DistanceTable[d];
|
||||
uint_fast32_t color_set[4] = { applicate_color(c[0], d), applicate_color(c[0], -d), applicate_color(c[1], d), applicate_color(c[1], -d) };
|
||||
k <<= 1;
|
||||
for (int i = 0; i < 16; i++, j >>= 1, k >>= 1) {
|
||||
int index = (k & 2) | (j & 1);
|
||||
outbuf[WriteOrderTable[i]] = color_set[index];
|
||||
if (!obaq && index == 2)
|
||||
outbuf[WriteOrderTable[i]] &= TRANSPARENT_MASK;
|
||||
}
|
||||
} else if (b + db < 0 || b + db > 255) {
|
||||
// planar
|
||||
c[0][0] = (data[0] << 1 & 0xfc) | (data[0] >> 5 & 3);
|
||||
c[0][1] = (data[0] << 7 & 0x80) | (data[1] & 0x7e) | (data[0] & 1);
|
||||
c[0][2] = (data[1] << 7 & 0x80) | (data[2] << 2 & 0x60) | (data[2] << 3 & 0x18) | (data[3] >> 5 & 4);
|
||||
c[0][2] |= c[0][2] >> 6;
|
||||
c[1][0] = (data[3] << 1 & 0xf8) | (data[3] << 2 & 4) | (data[3] >> 5 & 3);
|
||||
c[1][1] = (data[4] & 0xfe) | data[4] >> 7;
|
||||
c[1][2] = (data[4] << 7 & 0x80) | (data[5] >> 1 & 0x7c);
|
||||
c[1][2] |= c[1][2] >> 6;
|
||||
c[2][0] = (data[5] << 5 & 0xe0) | (data[6] >> 3 & 0x1c) | (data[5] >> 1 & 3);
|
||||
c[2][1] = (data[6] << 3 & 0xf8) | (data[7] >> 5 & 0x6) | (data[6] >> 4 & 1);
|
||||
c[2][2] = data[7] << 2 | (data[7] >> 4 & 3);
|
||||
for (int y = 0, i = 0; y < 4; y++) {
|
||||
for (int x = 0; x < 4; x++, i++) {
|
||||
uint8_t r = clamp((x * (c[1][0] - c[0][0]) + y * (c[2][0] - c[0][0]) + 4 * c[0][0] + 2) >> 2);
|
||||
uint8_t g = clamp((x * (c[1][1] - c[0][1]) + y * (c[2][1] - c[0][1]) + 4 * c[0][1] + 2) >> 2);
|
||||
uint8_t b = clamp((x * (c[1][2] - c[0][2]) + y * (c[2][2] - c[0][2]) + 4 * c[0][2] + 2) >> 2);
|
||||
outbuf[i] = color(r, g, b, 255);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// differential
|
||||
const uint_fast8_t code[2] = { data[3] >> 5, data[3] >> 2 & 7 };
|
||||
const uint_fast8_t* table = Etc1SubblockTable[data[3] & 1];
|
||||
c[0][0] = r | r >> 5;
|
||||
c[0][1] = g | g >> 5;
|
||||
c[0][2] = b | b >> 5;
|
||||
c[1][0] = r + dr;
|
||||
c[1][1] = g + dg;
|
||||
c[1][2] = b + db;
|
||||
c[1][0] |= c[1][0] >> 5;
|
||||
c[1][1] |= c[1][1] >> 5;
|
||||
c[1][2] |= c[1][2] >> 5;
|
||||
for (int i = 0; i < 16; i++, j >>= 1, k >>= 1) {
|
||||
uint_fast8_t s = table[i];
|
||||
uint_fast8_t m = Etc2aModifierTable[obaq][code[s]][j & 1];
|
||||
outbuf[WriteOrderTable[i]] = applicate_color_alpha(c[s], k & 1 ? -m : m, !obaq && (k & 1) && !(j & 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void decode_etc2a8_block(const uint8_t* data, uint32_t* outbuf)
|
||||
{
|
||||
if (data[1] & 0xf0) {
|
||||
@@ -256,10 +368,8 @@ void decode_etc2a1(const void* data, const int w, const int h, uint32_t* image)
|
||||
uint32_t* buf_end = buf + 16;
|
||||
const uint8_t* d = (uint8_t*)data;
|
||||
for (int by = 0; by < num_blocks_y; by++) {
|
||||
for (int bx = 0, x = 0; bx < num_blocks_x; bx++, d += 9, x += 4) {
|
||||
decode_etc2_block(d + 1, buf);
|
||||
for (int i = 0; i < 16; i++)
|
||||
((uint8_t*)(buf + i))[3] = d[0];
|
||||
for (int bx = 0, x = 0; bx < num_blocks_x; bx++, d += 8, x += 4) {
|
||||
decode_etc2a1_block(d, buf);
|
||||
int copy_length = (bx < num_blocks_x - 1 ? 4 : copy_length_last) * 4;
|
||||
uint32_t* b = buf;
|
||||
for (int y = h - by * 4 - 1; b < buf_end && y >= 0; y--, b += 4)
|
||||
|
||||
Reference in New Issue
Block a user