diff --git a/README.md b/README.md index 90e4528..4c18eea 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ obj.key You can get image data directly from Texture2D object. Output object's class is `ChunkyPNG::Image`. -Some basic texture formats (1–5, 7, 9, 13–20, 22, 62, and 63), DXT1 (10), DXT5 (12), ETC_RGB4 (34), ETC2 (45, 47), and ASTC (48–59) are available. +Some basic texture formats (1–5, 7, 9, 13–20, 22, 62, and 63), DXT1 (10), DXT5 (12), ETC_RGB4 (34), ETC2 (45–47), and ASTC (48–59) are available. ```ruby require 'mikunyan/decoders' diff --git a/ext/decoders/native/etc.c b/ext/decoders/native/etc.c new file mode 100644 index 0000000..1d88c01 --- /dev/null +++ b/ext/decoders/native/etc.c @@ -0,0 +1,271 @@ +#include +#include +#include "etc.h" + +uint_fast8_t WriteOrderTable[16] = { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 }; +uint_fast8_t WriteOrderTableRev[16] = { 15, 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0 }; +uint_fast8_t Etc1ModifierTable[8][2] = {{2, 8}, {5, 17}, {9, 29}, {13, 42}, {18, 60}, {24, 80}, {33, 106}, {47, 183}}; +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}}; +uint_fast8_t Etc2DistanceTable[8] = {3, 6, 11, 16, 23, 32, 41, 64}; +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} +}; + +static inline uint_fast32_t color(uint_fast32_t r, uint_fast32_t g, uint_fast32_t b, uint_fast32_t a) { + return r | g << 8 | b << 16 | a << 24; +} + +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) { + return color(clamp(c[0] + m), clamp(c[1] + m), clamp(c[2] + m), 255); +} + +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) { + uint_fast8_t code[2] = { data[3] >> 5, data[3] >> 2 & 7 }; + uint_fast8_t *table = Etc1SubblockTable[data[3] & 1]; + uint_fast8_t c[2][3]; + if (data[3] & 2) { + c[0][0] = data[0] & 0xf8; + c[0][1] = data[1] & 0xf8; + c[0][2] = data[2] & 0xf8; + c[1][0] = c[0][0] + (data[0] << 3 & 0x18) - (data[0] << 3 & 0x20); + c[1][1] = c[0][1] + (data[1] << 3 & 0x18) - (data[1] << 3 & 0x20); + c[1][2] = c[0][2] + (data[2] << 3 & 0x18) - (data[2] << 3 & 0x20); + c[0][0] |= c[0][0] >> 5; + c[0][1] |= c[0][1] >> 5; + c[0][2] |= c[0][2] >> 5; + c[1][0] |= c[1][0] >> 5; + c[1][1] |= c[1][1] >> 5; + c[1][2] |= c[1][2] >> 5; + } else { + 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; + c[1][1] = data[1] & 0x0f | data[1] << 4; + c[0][2] = data[2] & 0xf0 | data[2] >> 4; + c[1][2] = data[2] & 0x0f | data[2] << 4; + } + + uint_fast16_t j = data[6] << 8 | data[7]; + uint_fast16_t k = data[4] << 8 | data[5]; + 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]; + outbuf[WriteOrderTable[i]] = applicate_color(c[s], k & 1 ? -m : m); + } +} + +void decode_etc1(const void *data, const int w, const int h, uint32_t *image) { + int bcw = (w + 3) / 4; + int bch = (h + 3) / 4; + int clen_last = (w + 3) % 4 + 1; + uint32_t buf[16]; + const uint8_t *d = (uint8_t*)data; + for (int t = 0; t < bch; t++) { + for (int s = 0; s < bcw; s++, d += 8) { + decode_etc1_block(d, buf); + int clen = (s < bcw - 1 ? 4 : clen_last) * 4; + for (int i = 0, y = h - t * 4 - 1; i < 4 && y >= 0; i++, y--) + memcpy(image + y * w + s * 4, buf + i * 4, clen); + } + } +} + +static inline void decode_etc2_block(const uint8_t *data, uint32_t *outbuf) { + uint_fast16_t j = data[6] << 8 | data[7]; + uint_fast16_t k = data[4] << 8 | data[5]; + uint_fast8_t c[3][3] = {}; + + if (data[3] & 2) { + 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; + 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) + }; + for (int i = 0; i < 16; i++, j >>= 1, k >>= 1) + outbuf[WriteOrderTable[i]] = color_set[k << 1 & 2 | j & 1]; + } 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) + }; + for (int i = 0; i < 16; i++, j >>= 1, k >>= 1) + outbuf[WriteOrderTable[i]] = color_set[k << 1 & 2 | j & 1]; + } 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 x = 3, i = 0; x >= 0; x--) { + for (int y = 3; y >= 0; y--, 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 + uint_fast8_t code[2] = { data[3] >> 5, data[3] >> 2 & 7 }; + 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 = Etc1ModifierTable[code[s]][j & 1]; + outbuf[WriteOrderTable[i]] = applicate_color(c[s], k & 1 ? -m : m); + } + } + } else { + // individual + uint_fast8_t code[2] = { data[3] >> 5, data[3] >> 2 & 7 }; + 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; + c[1][1] = data[1] & 0x0f | data[1] << 4; + c[0][2] = data[2] & 0xf0 | data[2] >> 4; + c[1][2] = data[2] & 0x0f | data[2] << 4; + 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]; + outbuf[WriteOrderTable[i]] = applicate_color(c[s], k & 1 ? -m : m); + } + } +} + +static inline void decode_etc2a8_block(const uint8_t *data, uint32_t *outbuf) { + if (data[2] & 0xf0) { + uint_fast8_t mult = data[1] >> 4; + 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] + mult * table[l & 7]); + } else { + for (int i = 0; i < 16; i++) + ((uint8_t*)(outbuf + i))[3] = data[0]; + } +} + +void decode_etc2(const void *data, const int w, const int h, uint32_t *image) { + int bcw = (w + 3) / 4; + int bch = (h + 3) / 4; + int clen_last = (w + 3) % 4 + 1; + uint32_t buf[16]; + const uint8_t *d = (uint8_t*)data; + for (int t = 0; t < bch; t++) { + for (int s = 0; s < bcw; s++, d += 8) { + decode_etc2_block(d, buf); + int clen = (s < bcw - 1 ? 4 : clen_last) * 4; + for (int i = 0, y = h - t * 4 - 1; i < 4 && y >= 0; i++, y--) + memcpy(image + y * w + s * 4, buf + i * 4, clen); + } + } +} + +void decode_etc2a1(const void *data, const int w, const int h, uint32_t *image) { + int bcw = (w + 3) / 4; + int bch = (h + 3) / 4; + int clen_last = (w + 3) % 4 + 1; + uint32_t buf[16]; + const uint8_t *d = (uint8_t*)data; + for (int t = 0; t < bch; t++) { + for (int s = 0; s < bcw; s++, d += 9) { + decode_etc2_block(d + 1, buf); + for (int i = 0; i < 16; i++) + ((uint8_t*)(buf + i))[3] = d[0]; + int clen = (s < bcw - 1 ? 4 : clen_last) * 4; + for (int i = 0, y = h - t * 4 - 1; i < 4 && y >= 0; i++, y--) + memcpy(image + y * w + s * 4, buf + i * 4, clen); + } + } +} + +void decode_etc2a8(const void *data, const int w, const int h, uint32_t *image) { + int bcw = (w + 3) / 4; + int bch = (h + 3) / 4; + int clen_last = (w + 3) % 4 + 1; + uint32_t buf[16]; + const uint8_t *d = (uint8_t*)data; + for (int t = 0; t < bch; t++) { + for (int s = 0; s < bcw; s++, d += 16) { + decode_etc2_block(d + 8, buf); + decode_etc2a8_block(d, buf); + int clen = (s < bcw - 1 ? 4 : clen_last) * 4; + for (int i = 0, y = h - t * 4 - 1; i < 4 && y >= 0; i++, y--) + memcpy(image + y * w + s * 4, buf + i * 4, clen); + } + } +} diff --git a/ext/decoders/native/etc.h b/ext/decoders/native/etc.h new file mode 100644 index 0000000..9057114 --- /dev/null +++ b/ext/decoders/native/etc.h @@ -0,0 +1,11 @@ +#ifndef ETC_H +#define ETC_H + +#include + +void decode_etc1(const void*, const int, const int, uint32_t*); +void decode_etc2(const void*, const int, const int, uint32_t*); +void decode_etc2a1(const void*, const int, const int, uint32_t*); +void decode_etc2a8(const void*, const int, const int, uint32_t*); + +#endif /* end of include guard: ETC_H */ diff --git a/ext/decoders/native/main.c b/ext/decoders/native/main.c index f507e82..d43fa49 100644 --- a/ext/decoders/native/main.c +++ b/ext/decoders/native/main.c @@ -2,6 +2,7 @@ #include #include #include "rgb.h" +#include "etc.h" #include "astc.h" #include "dxtc.h" @@ -15,6 +16,46 @@ static VALUE rb_decode_rgb565(VALUE self, VALUE rb_data, VALUE size, VALUE big) return ret; } +static VALUE rb_decode_etc1(VALUE self, VALUE rb_data, VALUE w, VALUE h) { + if (RSTRING_LEN(rb_data) < ((FIX2LONG(w) + 3) / 4) * ((FIX2LONG(h) + 3) / 4) * 8) + rb_raise(rb_eStandardError, "Data size is not enough."); + uint32_t *image = (uint32_t*)calloc(FIX2LONG(w) * FIX2LONG(h), sizeof(uint32_t)); + decode_etc1((uint64_t*)RSTRING_PTR(rb_data), FIX2INT(w), FIX2INT(h), image); + VALUE ret = rb_str_new((char*)image, FIX2LONG(w) * FIX2LONG(h) * sizeof(uint32_t)); + free(image); + return ret; +} + +static VALUE rb_decode_etc2(VALUE self, VALUE rb_data, VALUE w, VALUE h) { + if (RSTRING_LEN(rb_data) < ((FIX2LONG(w) + 3) / 4) * ((FIX2LONG(h) + 3) / 4) * 8) + rb_raise(rb_eStandardError, "Data size is not enough."); + uint32_t *image = (uint32_t*)calloc(FIX2LONG(w) * FIX2LONG(h), sizeof(uint32_t)); + decode_etc2((uint64_t*)RSTRING_PTR(rb_data), FIX2INT(w), FIX2INT(h), image); + VALUE ret = rb_str_new((char*)image, FIX2LONG(w) * FIX2LONG(h) * sizeof(uint32_t)); + free(image); + return ret; +} + +static VALUE rb_decode_etc2a1(VALUE self, VALUE rb_data, VALUE w, VALUE h) { + if (RSTRING_LEN(rb_data) < ((FIX2LONG(w) + 3) / 4) * ((FIX2LONG(h) + 3) / 4) * 9) + rb_raise(rb_eStandardError, "Data size is not enough."); + uint32_t *image = (uint32_t*)calloc(FIX2LONG(w) * FIX2LONG(h), sizeof(uint32_t)); + decode_etc2a8((uint64_t*)RSTRING_PTR(rb_data), FIX2INT(w), FIX2INT(h), image); + VALUE ret = rb_str_new((char*)image, FIX2LONG(w) * FIX2LONG(h) * sizeof(uint32_t)); + free(image); + return ret; +} + +static VALUE rb_decode_etc2a8(VALUE self, VALUE rb_data, VALUE w, VALUE h) { + if (RSTRING_LEN(rb_data) < ((FIX2LONG(w) + 3) / 4) * ((FIX2LONG(h) + 3) / 4) * 16) + rb_raise(rb_eStandardError, "Data size is not enough."); + uint32_t *image = (uint32_t*)calloc(FIX2LONG(w) * FIX2LONG(h), sizeof(uint32_t)); + decode_etc2a8((uint64_t*)RSTRING_PTR(rb_data), FIX2INT(w), FIX2INT(h), image); + VALUE ret = rb_str_new((char*)image, FIX2LONG(w) * FIX2LONG(h) * sizeof(uint32_t)); + free(image); + return ret; +} + static VALUE rb_decode_astc(VALUE self, VALUE rb_data, VALUE w, VALUE h, VALUE bw, VALUE bh) { if (RSTRING_LEN(rb_data) < ((FIX2LONG(w) + FIX2LONG(bw) - 1) / FIX2LONG(bw)) * ((FIX2LONG(h) + FIX2LONG(bh) - 1) / FIX2LONG(bh)) * 16) rb_raise(rb_eStandardError, "Data size is not enough."); @@ -50,6 +91,10 @@ void Init_native() { VALUE mMikunyan = rb_define_module("Mikunyan"); VALUE mDecodeHelper = rb_define_module_under(mMikunyan, "DecodeHelper"); rb_define_module_function(mDecodeHelper, "decode_rgb565", rb_decode_rgb565, 3); + rb_define_module_function(mDecodeHelper, "decode_etc1", rb_decode_etc1, 3); + rb_define_module_function(mDecodeHelper, "decode_etc2", rb_decode_etc2, 3); + rb_define_module_function(mDecodeHelper, "decode_etc2a1", rb_decode_etc2a1, 3); + rb_define_module_function(mDecodeHelper, "decode_etc2a8", rb_decode_etc2a8, 3); rb_define_module_function(mDecodeHelper, "decode_astc", rb_decode_astc, 5); rb_define_module_function(mDecodeHelper, "decode_dxt1", rb_decode_dxt1, 3); rb_define_module_function(mDecodeHelper, "decode_dxt5", rb_decode_dxt5, 3); diff --git a/lib/mikunyan/decoders/image_decoder.rb b/lib/mikunyan/decoders/image_decoder.rb index edf7cd9..105736b 100644 --- a/lib/mikunyan/decoders/image_decoder.rb +++ b/lib/mikunyan/decoders/image_decoder.rb @@ -64,6 +64,8 @@ module Mikunyan decode_etc1(width, height, bin) when 45 decode_etc2rgb(width, height, bin) + when 46 + decode_etc2rgba1(width, height, bin) when 47 decode_etc2rgba8(width, height, bin) when 48, 54 @@ -370,16 +372,7 @@ module Mikunyan # @param [String] bin binary to decode # @return [ChunkyPNG::Image] decoded image def self.decode_etc1(width, height, bin) - bw = (width + 3) / 4 - bh = (height + 3) / 4 - ret = ChunkyPNG::Image.new(bh * 4, bw * 4) - bh.times do |by| - bw.times do |bx| - block = decode_etc1_block(BinUtils.get_sint64_be(bin, (bx + by * bw) * 8)) - ret.replace!(ChunkyPNG::Image.from_rgb_stream(4, 4, block), by * 4, bx * 4) - end - end - ret.crop(0, 0, height, width).rotate_left + ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_etc1(bin, width, height)) end # Decode image from ETC2 compressed binary @@ -388,16 +381,16 @@ module Mikunyan # @param [String] bin binary to decode # @return [ChunkyPNG::Image] decoded image def self.decode_etc2rgb(width, height, bin) - bw = (width + 3) / 4 - bh = (height + 3) / 4 - ret = ChunkyPNG::Image.new(bh * 4, bw * 4) - bh.times do |by| - bw.times do |bx| - block = decode_etc2_block(BinUtils.get_sint64_be(bin, (bx + by * bw) * 8)) - ret.replace!(ChunkyPNG::Image.from_rgb_stream(4, 4, block), by * 4, bx * 4) - end - end - ret.crop(0, 0, height, width).rotate_left + ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_etc2(bin, width, height)) + end + + # Decode image from ETC2 Alpha1 compressed binary + # @param [Integer] width image width + # @param [Integer] height image height + # @param [String] bin binary to decode + # @return [ChunkyPNG::Image] decoded image + def self.decode_etc2rgba1(width, height, bin) + ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_etc2a1(bin, width, height)) end # Decode image from ETC2 Alpha8 compressed binary @@ -406,19 +399,7 @@ module Mikunyan # @param [String] bin binary to decode # @return [ChunkyPNG::Image] decoded image def self.decode_etc2rgba8(width, height, bin) - bw = (width + 3) / 4 - bh = (height + 3) / 4 - ret = ChunkyPNG::Image.new(bh * 4, bw * 4) - bh.times do |by| - bw.times do |bx| - alpha = decode_etc2alpha_block(BinUtils.get_int64_be(bin, (bx + by * bw) * 16)) - block = decode_etc2_block(BinUtils.get_int64_be(bin, (bx + by * bw) * 16 + 8)) - mem = String.new(capacity: 64) - 16.times{|i| BinUtils.append_string!(mem, block[i * 3, 3] + alpha[15 - i])} - ret.replace!(ChunkyPNG::Image.from_rgba_stream(4, 4, mem), by * 4, bx * 4) - end - end - ret.crop(0, 0, height, width).rotate_left + ChunkyPNG::Image.from_rgba_stream(width, height, DecodeHelper.decode_etc2a8(bin, width, height)) end # Decode image from ASTC compressed binary @@ -461,167 +442,6 @@ module Mikunyan private - Etc1ModifierTable = [[2, 8], [5, 17], [9, 29], [13, 42], [18, 60], [24, 80], [33, 106], [47, 183]] - Etc1SubblockTable = [[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]] - Etc2DistanceTable = [3, 6, 11, 16, 23, 32, 41, 64] - Etc2AlphaModTable = [ - [-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] - ] - - def self.decode_etc1_block(bin) - colors = [] - codes = [bin >> 37 & 7, bin >> 34 & 7] - subblocks = Etc1SubblockTable[bin[32]] - if bin[33] == 0 - colors[0] = bin >> 40 & 0xf0f0f0 - colors[0] = colors[0] | colors[0] >> 4 - colors[1] = bin >> 36 & 0xf0f0f0 - colors[1] = colors[1] | colors[1] >> 4 - else - colors[0] = bin >> 40 & 0xf8f8f8 - dr = (bin >> 56 & 3) - (bin >> 56 & 4) - dg = (bin >> 48 & 3) - (bin >> 48 & 4) - db = (bin >> 40 & 3) - (bin >> 40 & 4) - colors[1] = colors[0] + (dr << 19) + (dg << 11) + (db << 3) - colors[0] = colors[0] | (colors[0] >> 5 & 0x70707) - colors[1] = colors[1] | (colors[1] >> 5 & 0x70707) - end - - mem = String.new(capacity: 48) - 16.times do |i| - modifier = Etc1ModifierTable[codes[subblocks[i]]][bin[i]] - etc1colormod_append(mem, colors[subblocks[i]], bin[i + 16] == 0 ? modifier : -modifier) - end - mem - end - - def self.etc1colormod_append(str, color, modifier) - r = (color >> 16 & 0xff) + modifier - g = (color >> 8 & 0xff) + modifier - b = (color & 0xff) + modifier - BinUtils.append_int8!(str, r.clamp(0, 255), g.clamp(0, 255), b.clamp(0, 255)) - end - - def self.etc1colormod(color, modifier) - r = (color >> 16 & 0xff) + modifier - g = (color >> 8 & 0xff) + modifier - b = (color & 0xff) + modifier - r.clamp(0, 255).chr + g.clamp(0, 255).chr + b.clamp(0, 255).chr - end - - def self.decode_etc2_block(bin) - mem = String.new(capacity: 48) - if bin[33] == 0 - # individual - colors = [0, 0] - colors[0] = bin >> 40 & 0xf0f0f0 - colors[0] = colors[0] | colors[0] >> 4 - colors[1] = bin >> 36 & 0xf0f0f0 - colors[1] = colors[1] | colors[1] >> 4 - codes = [bin >> 37 & 7, bin >> 34 & 7] - subblocks = Etc1SubblockTable[bin[32]] - 16.times do |i| - modifier = Etc1ModifierTable[codes[subblocks[i]]][bin[i]] - etc1colormod_append(mem, colors[subblocks[i]], bin[i + 16] == 0 ? modifier : -modifier) - end - else - r = bin >> 59 - dr = (bin >> 56 & 3) - (bin >> 56 & 4) - g = bin >> 51 & 0x1f - dg = (bin >> 48 & 3) - (bin >> 48 & 4) - b = bin >> 43 & 0x1f - db = (bin >> 40 & 3) - (bin >> 40 & 4) - if r + dr < 0 || r + dr > 31 - # T mode - base1 = (bin >> 49 & 0xc00) | (bin >> 48 & 0x3ff) - base1 = (base1 & 0xf00) << 8 | (base1 & 0xf0) << 4 | (base1 & 0xf) - base1 = (base1 << 4) | base1 - base2 = bin >> 36 & 0xfff - base2 = (base2 & 0xf00) << 8 | (base2 & 0xf0) << 4 | (base2 & 0xf) - base2 = (base2 << 4) | base2 - d = Etc2DistanceTable[(bin >> 33 & 6) + bin[32]] - colors = [[base1].pack('N')[1,3], etc1colormod(base2, d), [base2].pack('N')[1,3], etc1colormod(base2, -d)] - 16.times do |i| - BinUtils.append_string!(mem, colors[bin[i] + bin[i + 16] * 2]) - end - elsif g + dg < 0 || g + dg > 31 - # H mode - base1 = (bin >> 51 & 0xfe0) | (bin >> 48 & 0x18) | (bin >> 47 & 7) - base1 = (base1 & 0xf00) << 8 | (base1 & 0xf0) << 4 | (base1 & 0xf) - base1 = (base1 << 4) | base1 - base2 = bin >> 35 & 0xfff - base2 = (base2 & 0xf00) << 8 | (base2 & 0xf0) << 4 | (base2 & 0xf) - base2 = (base2 << 4) | base2 - d = Etc2DistanceTable[bin[34] * 2 + bin[32]] - colors = [etc1colormod(base1, d), etc1colormod(base1, -d), etc1colormod(base2, d), etc1colormod(base2, -d)] - 16.times do |i| - BinUtils.append_string!(mem, colors[bin[i] + bin[i + 16] * 2]) - end - elsif b + db < 0 || b + db > 31 - # planar mode - color_or = (bin >> 55 & 0xfc) | (bin >> 61 & 0x03) - color_og = (bin >> 49 & 0x80) | (bin >> 48 & 0x7e) | bin[56] - color_ob = (bin >> 41 & 0x80) | (bin >> 38 & 0x60) | (bin >> 37 & 0x1c) | (bin >> 47 & 2) | bin[44] - color_hr = (bin >> 31 & 0xf8) | (bin >> 30 & 0x04) | (bin >> 37 & 0x03) - color_hg = (bin >> 24 & 0xfe) | bin[31] - color_hb = (bin >> 17 & 0xfc) | (bin >> 23 & 0x03) - color_vr = (bin >> 11 & 0xfc) | (bin >> 17 & 0x03) - color_vg = (bin >> 5 & 0xfe) | bin[12] - color_vb = (bin << 2 & 0xfc) | (bin >> 4 & 0x03) - 16.times do |i| - x = i / 4 - y = i % 4 - r = (x * (color_hr - color_or) + y * (color_vr - color_or) + 4 * color_or + 2) >> 2 - g = (x * (color_hg - color_og) + y * (color_vg - color_og) + 4 * color_og + 2) >> 2 - b = (x * (color_hb - color_ob) + y * (color_vb - color_ob) + 4 * color_ob + 2) >> 2 - BinUtils.append_int8!(mem, r.clamp(0, 255), g.clamp(0, 255), b.clamp(0, 255)) - end - else - # differential mode - colors = [0, 0] - colors[0] = bin >> 40 & 0xf8f8f8 - colors[1] = colors[0] + (dr << 19) + (dg << 11) + (db << 3) - colors[0] = colors[0] | (colors[0] >> 5 & 0x70707) - colors[1] = colors[1] | (colors[1] >> 5 & 0x70707) - codes = [bin >> 37 & 7, bin >> 34 & 7] - subblocks = Etc1SubblockTable[bin[32]] - 16.times do |i| - modifier = Etc1ModifierTable[codes[subblocks[i]]][bin[i]] - etc1colormod_append(mem, colors[subblocks[i]], bin[i + 16] == 0 ? modifier : -modifier) - end - end - end - mem - end - - def self.decode_etc2alpha_block(bin) - if bin & 0xf0000000000000 == 0 - ((bin >> 56).chr) * 16 - else - mem = String.new(capacity: 16) - base = bin >> 56 - mult = bin >> 52 & 0xf - table = Etc2AlphaModTable[bin >> 48 & 0xf] - (0...16).each{|i| BinUtils.append_int8!(mem, (base + table[bin >> i*3 & 7] * mult).clamp(0, 255))} - mem - end - end - # convert 16bit float def self.n2f(n) case n