image_encode.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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/encode.h>
  6. #include "bimg_p.h"
  7. #include <libsquish/squish.h>
  8. #include <etc1/etc1.h>
  9. #include <etc2/ProcessRGB.hpp>
  10. #include <nvtt/nvtt.h>
  11. #include <pvrtc/PvrTcEncoder.h>
  12. #include <edtaa3/edtaa3func.h>
  13. BX_PRAGMA_DIAGNOSTIC_PUSH();
  14. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // warning C4100: 'alloc_context': unreferenced formal parameter
  15. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4702) // warning C4702: unreachable code
  16. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-parameter") // warning: unused parameter ‘alloc_context’ [-Wunused-parameter]
  17. #define STB_IMAGE_RESIZE_IMPLEMENTATION
  18. #include <stb/stb_image_resize.h>
  19. BX_PRAGMA_DIAGNOSTIC_POP();
  20. extern "C" {
  21. #include <iqa.h>
  22. }
  23. namespace bimg
  24. {
  25. static uint32_t s_squishQuality[] =
  26. {
  27. squish::kColourClusterFit, // Default
  28. squish::kColourIterativeClusterFit, // Highest
  29. squish::kColourRangeFit, // Fastest
  30. };
  31. BX_STATIC_ASSERT(Quality::Count == BX_COUNTOF(s_squishQuality) );
  32. void imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, TextureFormat::Enum _format, Quality::Enum _quality, bx::Error* _err)
  33. {
  34. const uint8_t* src = (const uint8_t*)_src;
  35. uint8_t* dst = (uint8_t*)_dst;
  36. const uint32_t srcPitch = _width*4;
  37. const uint32_t srcSlice = _height*srcPitch;
  38. const uint32_t dstBpp = getBitsPerPixel(_format);
  39. const uint32_t dstPitch = _width*dstBpp/8;
  40. const uint32_t dstSlice = _height*dstPitch;
  41. for (uint32_t zz = 0; zz < _depth && _err->isOk(); ++zz, src += srcSlice, dst += dstSlice)
  42. {
  43. switch (_format)
  44. {
  45. case TextureFormat::BC1:
  46. case TextureFormat::BC2:
  47. case TextureFormat::BC3:
  48. case TextureFormat::BC4:
  49. case TextureFormat::BC5:
  50. squish::CompressImage(src, _width, _height, dst
  51. , s_squishQuality[_quality]
  52. | (_format == TextureFormat::BC2 ? squish::kDxt3
  53. : _format == TextureFormat::BC3 ? squish::kDxt5
  54. : _format == TextureFormat::BC4 ? squish::kBc4
  55. : _format == TextureFormat::BC5 ? squish::kBc5
  56. : squish::kDxt1)
  57. );
  58. break;
  59. case TextureFormat::BC6H:
  60. case TextureFormat::BC7:
  61. BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
  62. break;
  63. case TextureFormat::ETC1:
  64. etc1_encode_image(src, _width, _height, 4, _width*4, dst);
  65. break;
  66. case TextureFormat::ETC2:
  67. {
  68. const uint32_t blockWidth = (_width +3)/4;
  69. const uint32_t blockHeight = (_height+3)/4;
  70. uint64_t* dstBlock = (uint64_t*)dst;
  71. for (uint32_t yy = 0; yy < blockHeight; ++yy)
  72. {
  73. for (uint32_t xx = 0; xx < blockWidth; ++xx)
  74. {
  75. uint8_t block[4*4*4];
  76. const uint8_t* ptr = &src[(yy*srcPitch+xx*4)*4];
  77. for (uint32_t ii = 0; ii < 16; ++ii)
  78. { // BGRx
  79. bx::memCopy(&block[ii*4], &ptr[(ii%4)*srcPitch + (ii&~3)], 4);
  80. bx::xchg(block[ii*4+0], block[ii*4+2]);
  81. }
  82. *dstBlock++ = ProcessRGB_ETC2(block);
  83. }
  84. }
  85. }
  86. break;
  87. case TextureFormat::PTC14:
  88. {
  89. using namespace Javelin;
  90. RgbaBitmap bmp;
  91. bmp.width = _width;
  92. bmp.height = _height;
  93. bmp.data = const_cast<uint8_t*>(src);
  94. PvrTcEncoder::EncodeRgb4Bpp(dst, bmp);
  95. bmp.data = NULL;
  96. }
  97. break;
  98. case TextureFormat::PTC14A:
  99. {
  100. using namespace Javelin;
  101. RgbaBitmap bmp;
  102. bmp.width = _width;
  103. bmp.height = _height;
  104. bmp.data = const_cast<uint8_t*>(src);
  105. PvrTcEncoder::EncodeRgba4Bpp(dst, bmp);
  106. bmp.data = NULL;
  107. }
  108. break;
  109. case TextureFormat::BGRA8:
  110. imageSwizzleBgra8(dst, dstPitch, _width, _height, src, srcPitch);
  111. break;
  112. case TextureFormat::RGBA8:
  113. bx::memCopy(_dst, _src, srcPitch, _height, srcPitch, dstPitch);
  114. break;
  115. default:
  116. if (!imageConvert(dst, _format, src, TextureFormat::RGBA8, _width, _height, 1) )
  117. {
  118. BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
  119. }
  120. break;
  121. }
  122. }
  123. }
  124. void imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, TextureFormat::Enum _dstFormat, Quality::Enum _quality, bx::Error* _err)
  125. {
  126. BX_ERROR_SCOPE(_err);
  127. const uint8_t* src = (const uint8_t*)_src;
  128. switch (_dstFormat)
  129. {
  130. case TextureFormat::BC6H:
  131. nvtt::compressBC6H(src, _width, _height, _width*16, _dst);
  132. break;
  133. case TextureFormat::BC7:
  134. nvtt::compressBC7(src, _width, _height, _width*16, _dst);
  135. break;
  136. default:
  137. if (!imageConvert(_dst, _dstFormat, _src, TextureFormat::RGBA32F, _width, _height, _depth) )
  138. {
  139. uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*_depth*4);
  140. if (imageConvert(temp, TextureFormat::RGBA8, _src, TextureFormat::RGBA32F, _width, _height, _depth) )
  141. {
  142. for (uint32_t zz = 0; zz < _depth; ++zz)
  143. {
  144. const uint32_t zoffset = zz*_width*_height;
  145. for (uint32_t yy = 0; yy < _height; ++yy)
  146. {
  147. const uint32_t yoffset = zoffset + yy*_width;
  148. for (uint32_t xx = 0; xx < _width; ++xx)
  149. {
  150. const uint32_t offset = yoffset + xx;
  151. const float* input = (const float*)&src[offset * 16];
  152. uint8_t* output = &temp[offset * 4];
  153. output[0] = uint8_t(bx::clamp(input[0], 0.0f, 1.0f)*255.0f + 0.5f);
  154. output[1] = uint8_t(bx::clamp(input[1], 0.0f, 1.0f)*255.0f + 0.5f);
  155. output[2] = uint8_t(bx::clamp(input[2], 0.0f, 1.0f)*255.0f + 0.5f);
  156. output[3] = uint8_t(bx::clamp(input[3], 0.0f, 1.0f)*255.0f + 0.5f);
  157. }
  158. }
  159. }
  160. imageEncodeFromRgba8(_dst, temp, _width, _height, _depth, _dstFormat, _quality, _err);
  161. }
  162. else
  163. {
  164. BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
  165. }
  166. BX_FREE(_allocator, temp);
  167. }
  168. break;
  169. }
  170. }
  171. void imageEncode(bx::AllocatorI* _allocator, void* _dst, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth, TextureFormat::Enum _dstFormat, Quality::Enum _quality, bx::Error* _err)
  172. {
  173. switch (_dstFormat)
  174. {
  175. case bimg::TextureFormat::BC1:
  176. case bimg::TextureFormat::BC2:
  177. case bimg::TextureFormat::BC3:
  178. case bimg::TextureFormat::BC4:
  179. case bimg::TextureFormat::BC5:
  180. case bimg::TextureFormat::ETC1:
  181. case bimg::TextureFormat::ETC2:
  182. case bimg::TextureFormat::PTC14:
  183. case bimg::TextureFormat::PTC14A:
  184. {
  185. uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*_depth*4);
  186. imageDecodeToRgba8(temp, _src, _width, _height, _width*4, _srcFormat);
  187. imageEncodeFromRgba8(_dst, temp, _width, _height, _depth, _dstFormat, _quality, _err);
  188. BX_FREE(_allocator, temp);
  189. }
  190. break;
  191. case bimg::TextureFormat::BC6H:
  192. case bimg::TextureFormat::BC7:
  193. {
  194. uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*_depth*16);
  195. imageDecodeToRgba32f(_allocator, temp, _src, _width, _height, _depth, _width*16, _srcFormat);
  196. imageEncodeFromRgba32f(_allocator, _dst, temp, _width, _height, _depth, _dstFormat, _quality, _err);
  197. BX_FREE(_allocator, temp);
  198. }
  199. break;
  200. default:
  201. BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
  202. break;
  203. }
  204. }
  205. ImageContainer* imageEncode(bx::AllocatorI* _allocator, TextureFormat::Enum _dstFormat, Quality::Enum _quality, const ImageContainer& _input)
  206. {
  207. ImageContainer* output = imageAlloc(_allocator
  208. , _dstFormat
  209. , uint16_t(_input.m_width)
  210. , uint16_t(_input.m_height)
  211. , uint16_t(_input.m_depth)
  212. , _input.m_numLayers
  213. , _input.m_cubeMap
  214. , 1 < _input.m_numMips
  215. );
  216. const uint16_t numSides = _input.m_numLayers * (_input.m_cubeMap ? 6 : 1);
  217. bx::Error err;
  218. for (uint16_t side = 0; side < numSides && err.isOk(); ++side)
  219. {
  220. for (uint8_t lod = 0, num = _input.m_numMips; lod < num && err.isOk(); ++lod)
  221. {
  222. ImageMip mip;
  223. if (imageGetRawData(_input, side, lod, _input.m_data, _input.m_size, mip) )
  224. {
  225. ImageMip dstMip;
  226. imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  227. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  228. imageEncode(_allocator
  229. , dstData
  230. , mip.m_data
  231. , mip.m_format
  232. , mip.m_width
  233. , mip.m_height
  234. , mip.m_depth
  235. , _dstFormat
  236. , _quality
  237. , &err
  238. );
  239. }
  240. }
  241. }
  242. if (err.isOk() )
  243. {
  244. return output;
  245. }
  246. imageFree(output);
  247. return NULL;
  248. }
  249. void imageRgba32f11to01(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _pitch, const void* _src)
  250. {
  251. const uint8_t* src = (const uint8_t*)_src;
  252. uint8_t* dst = (uint8_t*)_dst;
  253. for (uint32_t zz = 0; zz < _depth; ++zz)
  254. {
  255. for (uint32_t yy = 0; yy < _height; ++yy)
  256. {
  257. for (uint32_t xx = 0; xx < _width; ++xx)
  258. {
  259. const uint32_t offset = yy*_pitch + xx * 16;
  260. const float* input = (const float*)&src[offset];
  261. float* output = (float*)&dst[offset];
  262. output[0] = input[0]*0.5f + 0.5f;
  263. output[1] = input[1]*0.5f + 0.5f;
  264. output[2] = input[2]*0.5f + 0.5f;
  265. output[3] = input[3]*0.5f + 0.5f;
  266. }
  267. }
  268. }
  269. }
  270. static void edtaa3(bx::AllocatorI* _allocator, double* _dst, uint32_t _width, uint32_t _height, double* _src)
  271. {
  272. const uint32_t numPixels = _width*_height;
  273. short* xdist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  274. short* ydist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  275. double* gx = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  276. double* gy = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  277. ::computegradient(_src, _width, _height, gx, gy);
  278. ::edtaa3(_src, gx, gy, _width, _height, xdist, ydist, _dst);
  279. for (uint32_t ii = 0; ii < numPixels; ++ii)
  280. {
  281. if (_dst[ii] < 0.0)
  282. {
  283. _dst[ii] = 0.0;
  284. }
  285. }
  286. BX_FREE(_allocator, xdist);
  287. BX_FREE(_allocator, ydist);
  288. BX_FREE(_allocator, gx);
  289. BX_FREE(_allocator, gy);
  290. }
  291. void imageMakeDist(bx::AllocatorI* _allocator, void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, float _edge, const void* _src)
  292. {
  293. const uint32_t numPixels = _width*_height;
  294. double* imgIn = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  295. double* outside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  296. double* inside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  297. for (uint32_t yy = 0; yy < _height; ++yy)
  298. {
  299. const uint8_t* src = (const uint8_t*)_src + yy*_srcPitch;
  300. double* dst = &imgIn[yy*_width];
  301. for (uint32_t xx = 0; xx < _width; ++xx)
  302. {
  303. dst[xx] = double(src[xx])/255.0;
  304. }
  305. }
  306. edtaa3(_allocator, outside, _width, _height, imgIn);
  307. for (uint32_t ii = 0; ii < numPixels; ++ii)
  308. {
  309. imgIn[ii] = 1.0 - imgIn[ii];
  310. }
  311. edtaa3(_allocator, inside, _width, _height, imgIn);
  312. BX_FREE(_allocator, imgIn);
  313. uint8_t* dst = (uint8_t*)_dst;
  314. double edgeOffset = _edge*0.5;
  315. double invEdge = 1.0/_edge;
  316. for (uint32_t ii = 0; ii < numPixels; ++ii)
  317. {
  318. double dist = bx::clamp( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge, 0.0, 1.0);
  319. dst[ii] = 255-uint8_t(dist * 255.0);
  320. }
  321. BX_FREE(_allocator, inside);
  322. BX_FREE(_allocator, outside);
  323. }
  324. static const iqa_ssim_args s_iqaArgs =
  325. {
  326. 0.39f, // alpha
  327. 0.731f, // beta
  328. 1.12f, // gamma
  329. 187, // L
  330. 0.025987f, // K1
  331. 0.0173f, // K2
  332. 1 // factor
  333. };
  334. float imageQualityRgba8(
  335. const void* _reference
  336. , const void* _data
  337. , uint16_t _width
  338. , uint16_t _height
  339. )
  340. {
  341. float result = iqa_ssim( (const uint8_t*)_reference
  342. , (const uint8_t*)_data
  343. , _width
  344. , _height
  345. , _width*4
  346. , 0
  347. , &s_iqaArgs
  348. );
  349. return result;
  350. }
  351. bool imageResizeRgba32fLinear(ImageContainer* _dst, const ImageContainer* _src)
  352. {
  353. const uint16_t numSides = _src->m_numLayers * (_src->m_cubeMap ? 6 : 1);
  354. for (uint16_t side = 0; side < numSides; ++side)
  355. {
  356. bimg::ImageMip srcMip;
  357. bimg::imageGetRawData(*_src, side, 0, _src->m_data, _src->m_size, srcMip);
  358. const float* srcData = (const float*)(srcMip.m_data);
  359. bimg::ImageMip dstMip;
  360. bimg::imageGetRawData(*_dst, side, 0, _dst->m_data, _dst->m_size, dstMip);
  361. float* dstData = (float*)(dstMip.m_data);
  362. int result = stbir_resize_float_generic(
  363. (const float*)srcData, _src->m_width, _src->m_height, _src->m_width*16
  364. , ( float*)dstData, _dst->m_width, _dst->m_height, _dst->m_width*16
  365. , 4, 3
  366. , STBIR_FLAG_ALPHA_PREMULTIPLIED
  367. , STBIR_EDGE_CLAMP
  368. , STBIR_FILTER_DEFAULT
  369. , STBIR_COLORSPACE_LINEAR
  370. , NULL
  371. );
  372. if (1 != result)
  373. {
  374. return false;
  375. }
  376. }
  377. return true;
  378. }
  379. static float getAlpha(UnpackFn _unpack, const void* _data)
  380. {
  381. float rgba[4];
  382. _unpack(rgba, _data);
  383. return rgba[3];
  384. }
  385. float imageAlphaTestCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, float _alphaRef, float _scale)
  386. {
  387. UnpackFn unpack = getUnpack(_format);
  388. if (NULL == unpack)
  389. {
  390. return 0.0f;
  391. }
  392. float coverage = 0.0f;
  393. const uint8_t* src = (const uint8_t*)_src;
  394. const uint32_t xstep = getBitsPerPixel(_format) / 8;
  395. const float numSamples = 8.0f;
  396. for (uint32_t yy = 0, ystep = _srcPitch; yy < _height-1; ++yy, src += ystep)
  397. {
  398. const uint8_t* data = src;
  399. for (uint32_t xx = 0; xx < _width-1; ++xx, data += xstep)
  400. {
  401. float alpha00 = _scale * getAlpha(unpack, data);
  402. float alpha10 = _scale * getAlpha(unpack, data+xstep);
  403. float alpha01 = _scale * getAlpha(unpack, data+ystep);
  404. float alpha11 = _scale * getAlpha(unpack, data+ystep+xstep);
  405. for (float fy = 0.5f/numSamples; fy < 1.0f; fy += 1.0f)
  406. {
  407. for (float fx = 0.5f/numSamples; fx < 1.0f; fx += 1.0f)
  408. {
  409. float alpha = 0.0f
  410. + alpha00 * (1.0f - fx) * (1.0f - fy)
  411. + alpha10 * ( fx) * (1.0f - fy)
  412. + alpha01 * (1.0f - fx) * ( fy)
  413. + alpha11 * ( fx) * ( fy)
  414. ;
  415. if (alpha > _alphaRef)
  416. {
  417. coverage += 1.0f;
  418. }
  419. }
  420. }
  421. }
  422. }
  423. return coverage / float(_width*_height*numSamples*numSamples);
  424. }
  425. void imageScaleAlphaToCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, void* _src, float _desiredCoverage, float _alphaRef)
  426. {
  427. PackFn pack = getPack(_format);
  428. UnpackFn unpack = getUnpack(_format);
  429. if (NULL == pack
  430. || NULL == unpack)
  431. {
  432. return;
  433. }
  434. float min = 0.0f;
  435. float max = 4.0f;
  436. float scale = 1.0f;
  437. for (uint32_t ii = 0; ii < 8; ++ii)
  438. {
  439. float coverage = imageAlphaTestCoverage(
  440. _format
  441. , _width
  442. , _height
  443. , _srcPitch
  444. , _src
  445. , _alphaRef
  446. , scale
  447. );
  448. if (coverage < _desiredCoverage)
  449. {
  450. min = scale;
  451. }
  452. else if (coverage > _desiredCoverage)
  453. {
  454. max = scale;
  455. }
  456. else
  457. {
  458. break;
  459. }
  460. scale = (min + max) * 0.5f;
  461. }
  462. uint8_t* src = (uint8_t*)_src;
  463. const uint32_t xstep = getBitsPerPixel(_format) / 8;
  464. for (uint32_t yy = 0, ystep = _srcPitch; yy < _height; ++yy, src += ystep)
  465. {
  466. uint8_t* data = src;
  467. for (uint32_t xx = 0; xx < _width; ++xx, data += xstep)
  468. {
  469. float rgba[4];
  470. unpack(rgba, data);
  471. rgba[3] = bx::clamp(rgba[3]*scale, 0.0f, 1.0f);
  472. pack(data, rgba);
  473. }
  474. }
  475. }
  476. } // namespace bimg