image_etc.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "image_etc.h"
  2. #include "image.h"
  3. #include "rg_etc1.h"
  4. #include "print_string.h"
  5. #include "os/copymem.h"
  6. static void _decompress_etc(Image *p_img) {
  7. ERR_FAIL_COND(p_img->get_format()!=Image::FORMAT_ETC);
  8. int imgw = p_img->get_width();
  9. int imgh = p_img->get_height();
  10. DVector<uint8_t> src=p_img->get_data();
  11. DVector<uint8_t> dst;
  12. DVector<uint8_t>::Read r = src.read();
  13. int mmc=p_img->get_mipmaps();
  14. for(int i=0;i<=mmc;i++) {
  15. dst.resize(dst.size()+imgw*imgh*3);
  16. const uint8_t *srcbr=&r[p_img->get_mipmap_offset(i)];
  17. DVector<uint8_t>::Write w = dst.write();
  18. uint8_t *wptr = &w[dst.size()-imgw*imgh*3];
  19. int bw=MAX(imgw/4,1);
  20. int bh=MAX(imgh/4,1);
  21. for(int y=0;y<bh;y++) {
  22. for(int x=0;x<bw;x++) {
  23. uint8_t block[4*4*4];
  24. rg_etc1::unpack_etc1_block(srcbr,(unsigned int*)block);
  25. srcbr+=8;
  26. int maxx=MIN(imgw,4);
  27. int maxy=MIN(imgh,4);
  28. for(int yy=0;yy<maxy;yy++) {
  29. for(int xx=0;xx<maxx;xx++) {
  30. uint32_t src_ofs = (yy*4+xx)*4;
  31. uint32_t dst_ofs = ((y*4+yy)*imgw+x*4+xx)*3;
  32. wptr[dst_ofs+0]=block[src_ofs+0];
  33. wptr[dst_ofs+1]=block[src_ofs+1];
  34. wptr[dst_ofs+2]=block[src_ofs+2];
  35. }
  36. }
  37. }
  38. }
  39. imgw=MAX(1,imgw/2);
  40. imgh=MAX(1,imgh/2);
  41. }
  42. r=DVector<uint8_t>::Read();
  43. //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()));
  44. *p_img=Image(p_img->get_width(),p_img->get_height(),p_img->get_mipmaps(),Image::FORMAT_RGB,dst);
  45. if (p_img->get_mipmaps())
  46. p_img->generate_mipmaps(-1,true);
  47. }
  48. static void _compress_etc(Image *p_img) {
  49. Image img = *p_img;
  50. int imgw=img.get_width(),imgh=img.get_height();
  51. ERR_FAIL_COND( nearest_power_of_2(imgw)!=imgw || nearest_power_of_2(imgh)!=imgh );
  52. if (img.get_format()!=Image::FORMAT_RGB)
  53. img.convert(Image::FORMAT_RGB);
  54. int mmc=img.get_mipmaps();
  55. if (mmc==0)
  56. img.generate_mipmaps(); // force mipmaps, so it works on most hardware
  57. DVector<uint8_t> res_data;
  58. DVector<uint8_t> dst_data;
  59. DVector<uint8_t>::Read r = img.get_data().read();
  60. int mc=0;
  61. rg_etc1::etc1_pack_params pp;
  62. pp.m_quality=rg_etc1::cLowQuality;
  63. for(int i=0;i<=mmc;i++) {
  64. int bw=MAX(imgw/4,1);
  65. int bh=MAX(imgh/4,1);
  66. const uint8_t *src = &r[img.get_mipmap_offset(i)];
  67. int mmsize = MAX(bw,1)*MAX(bh,1)*8;
  68. dst_data.resize(dst_data.size()+mmsize);
  69. DVector<uint8_t>::Write w=dst_data.write();
  70. uint8_t *dst = &w[dst_data.size()-mmsize];
  71. // print_line("bh: "+itos(bh)+" bw: "+itos(bw));
  72. for(int y=0;y<bh;y++) {
  73. for(int x=0;x<bw;x++) {
  74. // print_line("x: "+itos(x)+" y: "+itos(y));
  75. uint8_t block[4*4*4];
  76. zeromem(block,4*4*4);
  77. uint8_t cblock[8];
  78. int maxy = MIN(imgh,4);
  79. int maxx = MIN(imgw,4);
  80. for(int yy=0;yy<maxy;yy++) {
  81. for(int xx=0;xx<maxx;xx++) {
  82. uint32_t dst_ofs = (yy*4+xx)*4;
  83. uint32_t src_ofs = ((y*4+yy)*imgw+x*4+xx)*3;
  84. block[dst_ofs+0]=src[src_ofs+0];
  85. block[dst_ofs+1]=src[src_ofs+1];
  86. block[dst_ofs+2]=src[src_ofs+2];
  87. block[dst_ofs+3]=255;
  88. }
  89. }
  90. rg_etc1::pack_etc1_block(cblock, (const unsigned int*)block, pp);
  91. for(int j=0;j<8;j++) {
  92. dst[j]=cblock[j];
  93. }
  94. dst+=8;
  95. }
  96. }
  97. imgw=MAX(1,imgw/2);
  98. imgh=MAX(1,imgh/2);
  99. mc++;
  100. }
  101. *p_img=Image(p_img->get_width(),p_img->get_height(),mc-1,Image::FORMAT_ETC,dst_data);
  102. }
  103. void _register_etc1_compress_func() {
  104. rg_etc1::pack_etc1_block_init();
  105. Image::_image_compress_etc_func=_compress_etc;
  106. Image::_image_decompress_etc=_decompress_etc;
  107. }