image_etc.cpp 5.6 KB

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