refactor image decoders

This commit is contained in:
Ishotihadus
2019-12-19 01:46:47 +09:00
parent e683b3c91a
commit 39ae15d9aa
16 changed files with 992 additions and 979 deletions
+96 -146
View File
@@ -1,75 +1,45 @@
#include "etc.h"
#include "common.h"
#include <stdint.h>
#include <string.h>
#include "color.h"
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 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 };
{{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] = {
{ -3, -6, -9, -15, 2, 5, 8, 14 },
{ -3, -7, -10, -13, 2, 6, 9, 12 },
{ -2, -5, -8, -13, 1, 4, 7, 12 },
{ -2, -4, -6, -13, 1, 3, 5, 12 },
{ -3, -6, -8, -12, 2, 5, 7, 11 },
{ -3, -7, -9, -11, 2, 6, 8, 10 },
{ -4, -7, -8, -11, 3, 6, 7, 10 },
{ -3, -5, -8, -11, 2, 4, 7, 10 },
{ -2, -6, -8, -10, 1, 5, 7, 9 },
{ -2, -5, -8, -10, 1, 4, 7, 9 },
{ -2, -4, -8, -10, 1, 3, 7, 9 },
{ -2, -5, -7, -10, 1, 4, 6, 9 },
{ -3, -4, -7, -10, 2, 3, 6, 9 },
{ -1, -2, -3, -10, 0, 1, 2, 9 },
{ -4, -6, -8, -9, 3, 5, 7, 8 },
{ -3, -5, -7, -9, 2, 4, 6, 8 }
};
{-3, -6, -9, -15, 2, 5, 8, 14}, {-3, -7, -10, -13, 2, 6, 9, 12}, {-2, -5, -8, -13, 1, 4, 7, 12},
{-2, -4, -6, -13, 1, 3, 5, 12}, {-3, -6, -8, -12, 2, 5, 7, 11}, {-3, -7, -9, -11, 2, 6, 8, 10},
{-4, -7, -8, -11, 3, 6, 7, 10}, {-3, -5, -8, -11, 2, 4, 7, 10}, {-2, -6, -8, -10, 1, 5, 7, 9},
{-2, -5, -8, -10, 1, 4, 7, 9}, {-2, -4, -8, -10, 1, 3, 7, 9}, {-2, -5, -7, -10, 1, 4, 6, 9},
{-3, -4, -7, -10, 2, 3, 6, 9}, {-1, -2, -3, -10, 0, 1, 2, 9}, {-4, -6, -8, -9, 3, 5, 7, 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
return r | g << 8 | b << 16 | a << 24;
#else
return a | b << 8 | g << 16 | r << 24;
#endif
}
static inline uint_fast8_t clamp(const int n)
{
static inline uint_fast8_t clamp(const int n) {
return n < 0 ? 0 : n > 255 ? 255 : n;
}
static inline uint32_t applicate_color(uint_fast8_t c[3], int_fast16_t m)
{
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)
{
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])
{
static inline uint32_t applicate_color_raw(uint_fast8_t c[3]) {
return color(c[0], c[1], c[2], 255);
}
static inline void decode_etc1_block(const uint8_t* data, uint32_t* outbuf)
{
const uint_fast8_t code[2] = { data[3] >> 5, data[3] >> 2 & 7 }; // Table codewords
const uint_fast8_t* table = Etc1SubblockTable[data[3] & 1];
static void decode_etc1_block(const uint8_t *data, uint32_t *outbuf) {
const uint_fast8_t code[2] = {data[3] >> 5, data[3] >> 2 & 7}; // Table codewords
const uint_fast8_t *table = Etc1SubblockTable[data[3] & 1];
uint_fast8_t c[2][3];
if (data[3] & 2) {
// diff bit == 1
@@ -95,8 +65,8 @@ static inline void decode_etc1_block(const uint8_t* data, uint32_t* outbuf)
c[1][2] = (data[2] & 0x0f) | data[2] << 4;
}
uint_fast16_t j = data[6] << 8 | data[7]; // less significant pixel index bits
uint_fast16_t k = data[4] << 8 | data[5]; // more significant pixel index bits
uint_fast16_t j = data[6] << 8 | data[7]; // less significant pixel index bits
uint_fast16_t k = data[4] << 8 | data[5]; // more significant pixel index bits
for (int i = 0; i < 16; i++, j >>= 1, k >>= 1) {
uint_fast8_t s = table[i];
uint_fast8_t m = Etc1ModifierTable[code[s]][j & 1];
@@ -104,29 +74,9 @@ static inline void decode_etc1_block(const uint8_t* data, uint32_t* outbuf)
}
}
void decode_etc1(const void* data, const int w, const int h, uint32_t* image)
{
int num_blocks_x = (w + 3) / 4;
int num_blocks_y = (h + 3) / 4;
int copy_length_last = (w + 3) % 4 + 1;
uint32_t buf[16];
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 += 8, x += 4) {
decode_etc1_block(d, buf);
int copy_length = (bx < num_blocks_x - 1 ? 4 : copy_length_last) * 4;
uint32_t* b = buf;
for (int y = h - 1 - by * 4; b < buf_end && y >= 0; y--, b += 4)
memcpy(image + y * w + x, b, copy_length);
}
}
}
static inline void decode_etc2_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
static void decode_etc2_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] = {};
if (data[3] & 2) {
@@ -146,7 +96,8 @@ static inline void decode_etc2_block(const uint8_t* data, uint32_t* outbuf)
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) };
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)
outbuf[WriteOrderTable[i]] = color_set[(k & 2) | (j & 1)];
@@ -162,10 +113,12 @@ static inline void decode_etc2_block(const uint8_t* data, uint32_t* outbuf)
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]))))
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) };
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)
outbuf[WriteOrderTable[i]] = color_set[(k & 2) | (j & 1)];
@@ -192,8 +145,8 @@ static inline void decode_etc2_block(const uint8_t* data, uint32_t* outbuf)
}
} else {
// differential
const uint_fast8_t code[2] = { data[3] >> 5, data[3] >> 2 & 7 };
const uint_fast8_t* table = Etc1SubblockTable[data[3] & 1];
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;
@@ -211,8 +164,8 @@ static inline void decode_etc2_block(const uint8_t* data, uint32_t* outbuf)
}
} else {
// individual (diff bit == 0)
const uint_fast8_t code[2] = { data[3] >> 5, data[3] >> 2 & 7 };
const uint_fast8_t* table = Etc1SubblockTable[data[3] & 1];
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] = (data[0] & 0xf0) | data[0] >> 4;
c[1][0] = (data[0] & 0x0f) | data[0] << 4;
c[0][1] = (data[1] & 0xf0) | data[1] >> 4;
@@ -227,10 +180,9 @@ 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
static 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;
@@ -251,7 +203,8 @@ static inline void decode_etc2a1_block(const uint8_t* data, uint32_t* outbuf)
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) };
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);
@@ -271,10 +224,12 @@ static inline void decode_etc2a1_block(const uint8_t* data, uint32_t* outbuf)
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]))))
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) };
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);
@@ -305,8 +260,8 @@ static inline void decode_etc2a1_block(const uint8_t* data, uint32_t* outbuf)
}
} else {
// differential
const uint_fast8_t code[2] = { data[3] >> 5, data[3] >> 2 & 7 };
const uint_fast8_t* table = Etc1SubblockTable[data[3] & 1];
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;
@@ -324,76 +279,71 @@ static inline void decode_etc2a1_block(const uint8_t* data, uint32_t* outbuf)
}
}
static inline void decode_etc2a8_block(const uint8_t* data, uint32_t* outbuf)
{
static void decode_etc2a8_block(const uint8_t *data, uint32_t *outbuf) {
if (data[1] & 0xf0) {
// multiplier != 0
const uint_fast8_t multiplier = data[1] >> 4;
const int_fast8_t* table = Etc2AlphaModTable[data[1] & 0xf];
uint_fast64_t l = data[7] | (uint_fast16_t)data[6] << 8 | (uint_fast32_t)data[5] << 16 | (uint_fast32_t)data[4] << 24 | (uint_fast64_t)data[3] << 32 | (uint_fast64_t)data[2] << 40;
const int_fast8_t *table = Etc2AlphaModTable[data[1] & 0xf];
uint_fast64_t l = data[7] | (uint_fast16_t)data[6] << 8 | (uint_fast32_t)data[5] << 16 |
(uint_fast32_t)data[4] << 24 | (uint_fast64_t)data[3] << 32 | (uint_fast64_t)data[2] << 40;
for (int i = 0; i < 16; i++, l >>= 3)
((uint8_t*)(outbuf + WriteOrderTableRev[i]))[3] = clamp(data[0] + multiplier * table[l & 7]);
((uint8_t *)(outbuf + WriteOrderTableRev[i]))[3] = clamp(data[0] + multiplier * table[l & 7]);
} else {
// multiplier == 0 (always same as base codeword)
for (int i = 0; i < 16; i++, outbuf++)
((uint8_t*)outbuf)[3] = data[0];
((uint8_t *)outbuf)[3] = data[0];
}
}
void decode_etc2(const void* data, const int w, const int h, uint32_t* image)
{
int num_blocks_x = (w + 3) / 4;
int num_blocks_y = (h + 3) / 4;
int copy_length_last = (w + 3) % 4 + 1;
uint32_t buf[16];
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 += 8, x += 4) {
decode_etc2_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)
memcpy(image + y * w + x, b, copy_length);
int decode_etc1(const uint8_t *data, const long w, const long h, uint32_t *image) {
long num_blocks_x = (w + 3) / 4;
long num_blocks_y = (h + 3) / 4;
uint32_t buffer[16];
for (long by = 0; by < num_blocks_y; by++) {
for (long bx = 0; bx < num_blocks_x; bx++, data += 8) {
decode_etc1_block(data, buffer);
copy_block_buffer(bx, by, w, h, 4, 4, buffer, image);
}
}
return 1;
}
void decode_etc2a1(const void* data, const int w, const int h, uint32_t* image)
{
int num_blocks_x = (w + 3) / 4;
int num_blocks_y = (h + 3) / 4;
int copy_length_last = (w + 3) % 4 + 1;
uint32_t buf[16];
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 += 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)
memcpy(image + y * w + x, b, copy_length);
int decode_etc2(const uint8_t *data, const long w, const long h, uint32_t *image) {
long num_blocks_x = (w + 3) / 4;
long num_blocks_y = (h + 3) / 4;
uint32_t buffer[16];
for (long by = 0; by < num_blocks_y; by++) {
for (long bx = 0; bx < num_blocks_x; bx++, data += 8) {
decode_etc2_block(data, buffer);
copy_block_buffer(bx, by, w, h, 4, 4, buffer, image);
}
}
return 1;
}
void decode_etc2a8(const void* data, const int w, const int h, uint32_t* image)
{
int num_blocks_x = (w + 3) / 4;
int num_blocks_y = (h + 3) / 4;
int copy_length_last = (w + 3) % 4 + 1;
uint32_t buf[16];
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 += 16, x += 4) {
decode_etc2_block(d + 8, buf);
decode_etc2a8_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)
memcpy(image + y * w + x, b, copy_length);
int decode_etc2a1(const uint8_t *data, const long w, const long h, uint32_t *image) {
long num_blocks_x = (w + 3) / 4;
long num_blocks_y = (h + 3) / 4;
uint32_t buffer[16];
for (long by = 0; by < num_blocks_y; by++) {
for (long bx = 0; bx < num_blocks_x; bx++, data += 8) {
decode_etc2a1_block(data, buffer);
copy_block_buffer(bx, by, w, h, 4, 4, buffer, image);
}
}
return 1;
}
int decode_etc2a8(const uint8_t *data, const long w, const long h, uint32_t *image) {
long num_blocks_x = (w + 3) / 4;
long num_blocks_y = (h + 3) / 4;
uint32_t buffer[16];
for (long by = 0; by < num_blocks_y; by++) {
for (long bx = 0; bx < num_blocks_x; bx++, data += 16) {
decode_etc2_block(data + 8, buffer);
decode_etc2a8_block(data, buffer);
copy_block_buffer(bx, by, w, h, 4, 4, buffer, image);
}
}
return 1;
}