image_etc.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*************************************************************************/
  2. /* image_etc.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "image_etc.h"
  31. #include "image.h"
  32. #include "os/copymem.h"
  33. #include "print_string.h"
  34. #include "rg_etc1.h"
  35. static void _decompress_etc(Image *p_img) {
  36. ERR_FAIL_COND(p_img->get_format() != Image::FORMAT_ETC);
  37. int imgw = p_img->get_width();
  38. int imgh = p_img->get_height();
  39. DVector<uint8_t> src = p_img->get_data();
  40. DVector<uint8_t> dst;
  41. DVector<uint8_t>::Read r = src.read();
  42. int mmc = p_img->get_mipmaps();
  43. for (int i = 0; i <= mmc; i++) {
  44. dst.resize(dst.size() + imgw * imgh * 3);
  45. const uint8_t *srcbr = &r[p_img->get_mipmap_offset(i)];
  46. DVector<uint8_t>::Write w = dst.write();
  47. uint8_t *wptr = &w[dst.size() - imgw * imgh * 3];
  48. int bw = MAX(imgw / 4, 1);
  49. int bh = MAX(imgh / 4, 1);
  50. for (int y = 0; y < bh; y++) {
  51. for (int x = 0; x < bw; x++) {
  52. uint8_t block[4 * 4 * 4];
  53. rg_etc1::unpack_etc1_block(srcbr, (unsigned int *)block);
  54. srcbr += 8;
  55. int maxx = MIN(imgw, 4);
  56. int maxy = MIN(imgh, 4);
  57. for (int yy = 0; yy < maxy; yy++) {
  58. for (int xx = 0; xx < maxx; xx++) {
  59. uint32_t src_ofs = (yy * 4 + xx) * 4;
  60. uint32_t dst_ofs = ((y * 4 + yy) * imgw + x * 4 + xx) * 3;
  61. wptr[dst_ofs + 0] = block[src_ofs + 0];
  62. wptr[dst_ofs + 1] = block[src_ofs + 1];
  63. wptr[dst_ofs + 2] = block[src_ofs + 2];
  64. }
  65. }
  66. }
  67. }
  68. imgw = MAX(1, imgw / 2);
  69. imgh = MAX(1, imgh / 2);
  70. }
  71. r = DVector<uint8_t>::Read();
  72. //print_line("Re Creating ETC into regular image: w "+itos(p_img->get_width())+" h "+itos(p_img->get_height())+" mm "+itos(p_img->get_mipmaps()));
  73. *p_img = Image(p_img->get_width(), p_img->get_height(), p_img->get_mipmaps(), Image::FORMAT_RGB, dst);
  74. if (p_img->get_mipmaps())
  75. p_img->generate_mipmaps(-1, true);
  76. }
  77. static void _compress_etc(Image *p_img) {
  78. Image img = *p_img;
  79. int imgw = img.get_width(), imgh = img.get_height();
  80. ERR_FAIL_COND(next_power_of_2(imgw) != imgw || next_power_of_2(imgh) != imgh);
  81. if (img.get_format() != Image::FORMAT_RGB)
  82. img.convert(Image::FORMAT_RGB);
  83. int mmc = img.get_mipmaps();
  84. if (mmc == 0)
  85. img.generate_mipmaps(); // force mipmaps, so it works on most hardware
  86. DVector<uint8_t> res_data;
  87. DVector<uint8_t> dst_data;
  88. DVector<uint8_t>::Read r = img.get_data().read();
  89. int mc = 0;
  90. rg_etc1::etc1_pack_params pp;
  91. pp.m_quality = rg_etc1::cLowQuality;
  92. for (int i = 0; i <= mmc; i++) {
  93. int bw = MAX(imgw / 4, 1);
  94. int bh = MAX(imgh / 4, 1);
  95. const uint8_t *src = &r[img.get_mipmap_offset(i)];
  96. int mmsize = MAX(bw, 1) * MAX(bh, 1) * 8;
  97. dst_data.resize(dst_data.size() + mmsize);
  98. DVector<uint8_t>::Write w = dst_data.write();
  99. uint8_t *dst = &w[dst_data.size() - mmsize];
  100. // print_line("bh: "+itos(bh)+" bw: "+itos(bw));
  101. for (int y = 0; y < bh; y++) {
  102. for (int x = 0; x < bw; x++) {
  103. // print_line("x: "+itos(x)+" y: "+itos(y));
  104. uint8_t block[4 * 4 * 4];
  105. zeromem(block, 4 * 4 * 4);
  106. uint8_t cblock[8];
  107. int maxy = MIN(imgh, 4);
  108. int maxx = MIN(imgw, 4);
  109. for (int yy = 0; yy < maxy; yy++) {
  110. for (int xx = 0; xx < maxx; xx++) {
  111. uint32_t dst_ofs = (yy * 4 + xx) * 4;
  112. uint32_t src_ofs = ((y * 4 + yy) * imgw + x * 4 + xx) * 3;
  113. block[dst_ofs + 0] = src[src_ofs + 0];
  114. block[dst_ofs + 1] = src[src_ofs + 1];
  115. block[dst_ofs + 2] = src[src_ofs + 2];
  116. block[dst_ofs + 3] = 255;
  117. }
  118. }
  119. rg_etc1::pack_etc1_block(cblock, (const unsigned int *)block, pp);
  120. for (int j = 0; j < 8; j++) {
  121. dst[j] = cblock[j];
  122. }
  123. dst += 8;
  124. }
  125. }
  126. imgw = MAX(1, imgw / 2);
  127. imgh = MAX(1, imgh / 2);
  128. mc++;
  129. }
  130. *p_img = Image(p_img->get_width(), p_img->get_height(), mc - 1, Image::FORMAT_ETC, dst_data);
  131. }
  132. void _register_etc1_compress_func() {
  133. rg_etc1::pack_etc1_block_init();
  134. Image::_image_compress_etc_func = _compress_etc;
  135. Image::_image_decompress_etc = _decompress_etc;
  136. }