image_encode.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "bimg_p.h"
  6. #include <libsquish/squish.h>
  7. #include <etc1/etc1.h>
  8. #include <etc2/ProcessRGB.hpp>
  9. #include <nvtt/nvtt.h>
  10. #include <pvrtc/PvrTcEncoder.h>
  11. #include <edtaa3/edtaa3func.h>
  12. namespace bimg
  13. {
  14. bool imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format)
  15. {
  16. switch (_format)
  17. {
  18. case TextureFormat::BC1:
  19. case TextureFormat::BC2:
  20. case TextureFormat::BC3:
  21. case TextureFormat::BC4:
  22. case TextureFormat::BC5:
  23. squish::CompressImage( (const uint8_t*)_src, _width, _height, _dst
  24. , _format == TextureFormat::BC2 ? squish::kDxt3
  25. : _format == TextureFormat::BC3 ? squish::kDxt5
  26. : _format == TextureFormat::BC4 ? squish::kBc4
  27. : _format == TextureFormat::BC5 ? squish::kBc5
  28. : squish::kDxt1
  29. );
  30. return true;
  31. case TextureFormat::BC6H:
  32. nvtt::compressBC6H( (const uint8_t*)_src, _width, _height, 4, _dst);
  33. return true;
  34. case TextureFormat::BC7:
  35. nvtt::compressBC7( (const uint8_t*)_src, _width, _height, 4, _dst);
  36. return true;
  37. case TextureFormat::ETC1:
  38. etc1_encode_image( (const uint8_t*)_src, _width, _height, 4, _width*4, (uint8_t*)_dst);
  39. return true;
  40. case TextureFormat::ETC2:
  41. {
  42. const uint32_t blockWidth = (_width +3)/4;
  43. const uint32_t blockHeight = (_height+3)/4;
  44. const uint32_t pitch = _width*4;
  45. const uint8_t* src = (const uint8_t*)_src;
  46. uint64_t* dst = (uint64_t*)_dst;
  47. for (uint32_t yy = 0; yy < blockHeight; ++yy)
  48. {
  49. for (uint32_t xx = 0; xx < blockWidth; ++xx)
  50. {
  51. uint8_t block[4*4*4];
  52. const uint8_t* ptr = &src[(yy*pitch+xx*4)*4];
  53. for (uint32_t ii = 0; ii < 16; ++ii)
  54. { // BGRx
  55. bx::memCopy(&block[ii*4], &ptr[(ii%4)*pitch + (ii&~3)], 4);
  56. bx::xchg(block[ii*4+0], block[ii*4+2]);
  57. }
  58. *dst++ = ProcessRGB_ETC2(block);
  59. }
  60. }
  61. }
  62. return true;
  63. case TextureFormat::PTC14:
  64. {
  65. using namespace Javelin;
  66. RgbaBitmap bmp;
  67. bmp.width = _width;
  68. bmp.height = _height;
  69. bmp.data = (uint8_t*)const_cast<void*>(_src);
  70. PvrTcEncoder::EncodeRgb4Bpp(_dst, bmp);
  71. bmp.data = NULL;
  72. }
  73. return true;
  74. case TextureFormat::PTC14A:
  75. {
  76. using namespace Javelin;
  77. RgbaBitmap bmp;
  78. bmp.width = _width;
  79. bmp.height = _height;
  80. bmp.data = (uint8_t*)const_cast<void*>(_src);
  81. PvrTcEncoder::EncodeRgba4Bpp(_dst, bmp);
  82. bmp.data = NULL;
  83. }
  84. return true;
  85. case TextureFormat::BGRA8:
  86. imageSwizzleBgra8(_dst, _width, _height, _width*4, _src);
  87. return true;
  88. case TextureFormat::RGBA8:
  89. bx::memCopy(_dst, _src, _width*_height*4);
  90. return true;
  91. default:
  92. break;
  93. }
  94. return imageConvert(_dst, _format, _src, TextureFormat::RGBA8, _width, _height);
  95. }
  96. bool imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format)
  97. {
  98. const uint8_t* src = (const uint8_t*)_src;
  99. switch (_format)
  100. {
  101. case TextureFormat::RGBA8:
  102. {
  103. uint8_t* dst = (uint8_t*)_dst;
  104. for (uint32_t yy = 0; yy < _height; ++yy)
  105. {
  106. for (uint32_t xx = 0; xx < _width; ++xx)
  107. {
  108. const uint32_t offset = yy*_width + xx;
  109. const float* input = (const float*)&src[offset * 16];
  110. uint8_t* output = &dst[offset * 4];
  111. output[0] = uint8_t(input[0]*255.0f + 0.5f);
  112. output[1] = uint8_t(input[1]*255.0f + 0.5f);
  113. output[2] = uint8_t(input[2]*255.0f + 0.5f);
  114. output[3] = uint8_t(input[3]*255.0f + 0.5f);
  115. }
  116. }
  117. }
  118. return true;
  119. case TextureFormat::BC5:
  120. {
  121. uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*4);
  122. for (uint32_t yy = 0; yy < _height; ++yy)
  123. {
  124. for (uint32_t xx = 0; xx < _width; ++xx)
  125. {
  126. const uint32_t offset = yy*_width + xx;
  127. const float* input = (const float*)&src[offset * 16];
  128. uint8_t* output = &temp[offset * 4];
  129. output[0] = uint8_t(input[0]*255.0f + 0.5f);
  130. output[1] = uint8_t(input[1]*255.0f + 0.5f);
  131. output[2] = uint8_t(input[2]*255.0f + 0.5f);
  132. output[3] = uint8_t(input[3]*255.0f + 0.5f);
  133. }
  134. }
  135. imageEncodeFromRgba8(_dst, temp, _width, _height, _format);
  136. BX_FREE(_allocator, temp);
  137. }
  138. return true;
  139. default:
  140. break;
  141. }
  142. return imageConvert(_dst, _format, _src, TextureFormat::RGBA32F, _width, _height);
  143. }
  144. void imageRgba32f11to01(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src)
  145. {
  146. const uint8_t* src = (const uint8_t*)_src;
  147. uint8_t* dst = (uint8_t*)_dst;
  148. for (uint32_t yy = 0; yy < _height; ++yy)
  149. {
  150. for (uint32_t xx = 0; xx < _width; ++xx)
  151. {
  152. const uint32_t offset = yy*_pitch + xx * 16;
  153. const float* input = (const float*)&src[offset];
  154. float* output = (float*)&dst[offset];
  155. output[0] = input[0]*0.5f + 0.5f;
  156. output[1] = input[1]*0.5f + 0.5f;
  157. output[2] = input[2]*0.5f + 0.5f;
  158. output[3] = input[3]*0.5f + 0.5f;
  159. }
  160. }
  161. }
  162. static void edtaa3(bx::AllocatorI* _allocator, double* _dst, uint32_t _width, uint32_t _height, double* _src)
  163. {
  164. const uint32_t numPixels = _width*_height;
  165. short* xdist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  166. short* ydist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  167. double* gx = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  168. double* gy = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  169. ::computegradient(_src, _width, _height, gx, gy);
  170. ::edtaa3(_src, gx, gy, _width, _height, xdist, ydist, _dst);
  171. for (uint32_t ii = 0; ii < numPixels; ++ii)
  172. {
  173. if (_dst[ii] < 0.0)
  174. {
  175. _dst[ii] = 0.0;
  176. }
  177. }
  178. BX_FREE(_allocator, xdist);
  179. BX_FREE(_allocator, ydist);
  180. BX_FREE(_allocator, gx);
  181. BX_FREE(_allocator, gy);
  182. }
  183. inline double min(double _a, double _b)
  184. {
  185. return _a > _b ? _b : _a;
  186. }
  187. inline double max(double _a, double _b)
  188. {
  189. return _a > _b ? _a : _b;
  190. }
  191. inline double clamp(double _val, double _min, double _max)
  192. {
  193. return max(min(_val, _max), _min);
  194. }
  195. void imageMakeDist(bx::AllocatorI* _allocator, void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, float _edge, const void* _src)
  196. {
  197. const uint32_t numPixels = _width*_height;
  198. double* imgIn = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  199. double* outside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  200. double* inside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  201. for (uint32_t yy = 0; yy < _height; ++yy)
  202. {
  203. const uint8_t* src = (const uint8_t*)_src + yy*_pitch;
  204. double* dst = &imgIn[yy*_width];
  205. for (uint32_t xx = 0; xx < _width; ++xx)
  206. {
  207. dst[xx] = double(src[xx])/255.0;
  208. }
  209. }
  210. edtaa3(_allocator, outside, _width, _height, imgIn);
  211. for (uint32_t ii = 0; ii < numPixels; ++ii)
  212. {
  213. imgIn[ii] = 1.0 - imgIn[ii];
  214. }
  215. edtaa3(_allocator, inside, _width, _height, imgIn);
  216. BX_FREE(_allocator, imgIn);
  217. uint8_t* dst = (uint8_t*)_dst;
  218. double edgeOffset = _edge*0.5;
  219. double invEdge = 1.0/_edge;
  220. for (uint32_t ii = 0; ii < numPixels; ++ii)
  221. {
  222. double dist = clamp( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge, 0.0, 1.0);
  223. dst[ii] = 255-uint8_t(dist * 255.0);
  224. }
  225. BX_FREE(_allocator, inside);
  226. BX_FREE(_allocator, outside);
  227. }
  228. } // namespace bimg