image_encode.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg#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(bx::AllocatorI* _allocator, 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(_allocator, 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(_allocator, _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(_allocator, 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(_allocator, _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(_allocator, temp, _src, _width, _height, _width*4, _srcFormat);
  187. imageEncodeFromRgba8(_allocator, _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. if (!imageConvert(_allocator, _dst, _dstFormat, _src, _srcFormat, _width, _height, 1) )
  202. {
  203. BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
  204. }
  205. break;
  206. }
  207. }
  208. ImageContainer* imageEncode(bx::AllocatorI* _allocator, TextureFormat::Enum _dstFormat, Quality::Enum _quality, const ImageContainer& _input)
  209. {
  210. ImageContainer* output = imageAlloc(_allocator
  211. , _dstFormat
  212. , uint16_t(_input.m_width)
  213. , uint16_t(_input.m_height)
  214. , uint16_t(_input.m_depth)
  215. , _input.m_numLayers
  216. , _input.m_cubeMap
  217. , 1 < _input.m_numMips
  218. );
  219. const uint16_t numSides = _input.m_numLayers * (_input.m_cubeMap ? 6 : 1);
  220. bx::Error err;
  221. for (uint16_t side = 0; side < numSides && err.isOk(); ++side)
  222. {
  223. for (uint8_t lod = 0, num = _input.m_numMips; lod < num && err.isOk(); ++lod)
  224. {
  225. ImageMip mip;
  226. if (imageGetRawData(_input, side, lod, _input.m_data, _input.m_size, mip) )
  227. {
  228. ImageMip dstMip;
  229. imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  230. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  231. imageEncode(
  232. _allocator
  233. , dstData
  234. , mip.m_data
  235. , mip.m_format
  236. , mip.m_width
  237. , mip.m_height
  238. , mip.m_depth
  239. , _dstFormat
  240. , _quality
  241. , &err
  242. );
  243. }
  244. }
  245. }
  246. if (err.isOk() )
  247. {
  248. return output;
  249. }
  250. imageFree(output);
  251. return NULL;
  252. }
  253. void imageRgba32f11to01(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _pitch, const void* _src)
  254. {
  255. const uint8_t* src = (const uint8_t*)_src;
  256. uint8_t* dst = (uint8_t*)_dst;
  257. for (uint32_t zz = 0; zz < _depth; ++zz)
  258. {
  259. for (uint32_t yy = 0; yy < _height; ++yy)
  260. {
  261. for (uint32_t xx = 0; xx < _width; ++xx)
  262. {
  263. const uint32_t offset = yy*_pitch + xx * 16;
  264. const float* input = (const float*)&src[offset];
  265. float* output = (float*)&dst[offset];
  266. output[0] = input[0]*0.5f + 0.5f;
  267. output[1] = input[1]*0.5f + 0.5f;
  268. output[2] = input[2]*0.5f + 0.5f;
  269. output[3] = input[3]*0.5f + 0.5f;
  270. }
  271. }
  272. }
  273. }
  274. static void edtaa3(bx::AllocatorI* _allocator, double* _dst, uint32_t _width, uint32_t _height, double* _src)
  275. {
  276. const uint32_t numPixels = _width*_height;
  277. short* xdist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  278. short* ydist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  279. double* gx = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  280. double* gy = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  281. ::computegradient(_src, _width, _height, gx, gy);
  282. ::edtaa3(_src, gx, gy, _width, _height, xdist, ydist, _dst);
  283. for (uint32_t ii = 0; ii < numPixels; ++ii)
  284. {
  285. if (_dst[ii] < 0.0)
  286. {
  287. _dst[ii] = 0.0;
  288. }
  289. }
  290. BX_FREE(_allocator, xdist);
  291. BX_FREE(_allocator, ydist);
  292. BX_FREE(_allocator, gx);
  293. BX_FREE(_allocator, gy);
  294. }
  295. void imageMakeDist(bx::AllocatorI* _allocator, void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, float _edge, const void* _src)
  296. {
  297. const uint32_t numPixels = _width*_height;
  298. double* imgIn = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  299. double* outside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  300. double* inside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  301. for (uint32_t yy = 0; yy < _height; ++yy)
  302. {
  303. const uint8_t* src = (const uint8_t*)_src + yy*_srcPitch;
  304. double* dst = &imgIn[yy*_width];
  305. for (uint32_t xx = 0; xx < _width; ++xx)
  306. {
  307. dst[xx] = double(src[xx])/255.0;
  308. }
  309. }
  310. edtaa3(_allocator, outside, _width, _height, imgIn);
  311. for (uint32_t ii = 0; ii < numPixels; ++ii)
  312. {
  313. imgIn[ii] = 1.0 - imgIn[ii];
  314. }
  315. edtaa3(_allocator, inside, _width, _height, imgIn);
  316. BX_FREE(_allocator, imgIn);
  317. uint8_t* dst = (uint8_t*)_dst;
  318. double edgeOffset = _edge*0.5;
  319. double invEdge = 1.0/_edge;
  320. for (uint32_t ii = 0; ii < numPixels; ++ii)
  321. {
  322. double dist = bx::clamp( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge, 0.0, 1.0);
  323. dst[ii] = 255-uint8_t(dist * 255.0);
  324. }
  325. BX_FREE(_allocator, inside);
  326. BX_FREE(_allocator, outside);
  327. }
  328. static const iqa_ssim_args s_iqaArgs =
  329. {
  330. 0.39f, // alpha
  331. 0.731f, // beta
  332. 1.12f, // gamma
  333. 187, // L
  334. 0.025987f, // K1
  335. 0.0173f, // K2
  336. 1 // factor
  337. };
  338. float imageQualityRgba8(
  339. const void* _reference
  340. , const void* _data
  341. , uint16_t _width
  342. , uint16_t _height
  343. )
  344. {
  345. float result = iqa_ssim( (const uint8_t*)_reference
  346. , (const uint8_t*)_data
  347. , _width
  348. , _height
  349. , _width*4
  350. , 0
  351. , &s_iqaArgs
  352. );
  353. return result;
  354. }
  355. bool imageResizeRgba32fLinear(ImageContainer* _dst, const ImageContainer* _src)
  356. {
  357. const uint16_t numSides = _src->m_numLayers * (_src->m_cubeMap ? 6 : 1);
  358. for (uint16_t side = 0; side < numSides; ++side)
  359. {
  360. bimg::ImageMip srcMip;
  361. bimg::imageGetRawData(*_src, side, 0, _src->m_data, _src->m_size, srcMip);
  362. bimg::ImageMip dstMip;
  363. bimg::imageGetRawData(*_dst, side, 0, _dst->m_data, _dst->m_size, dstMip);
  364. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  365. const uint32_t srcPitch = _src->m_width*16;
  366. const uint32_t srcSlice = _src->m_height*srcPitch;
  367. const uint32_t dstPitch = _dst->m_width*16;
  368. const uint32_t dstSlice = _dst->m_height*dstPitch;
  369. for (uint32_t zz = 0, depth = _dst->m_depth; zz < depth; ++zz, dstData += dstSlice)
  370. {
  371. const uint32_t srcDataStep = uint32_t(bx::floor(zz * _src->m_depth / float(_dst->m_depth) ) );
  372. const uint8_t* srcData = &srcMip.m_data[srcDataStep*srcSlice];
  373. int result = stbir_resize_float_generic(
  374. (const float*)srcData, _src->m_width, _src->m_height, srcPitch
  375. , ( float*)dstData, _dst->m_width, _dst->m_height, dstPitch
  376. , 4, 3
  377. , STBIR_FLAG_ALPHA_PREMULTIPLIED
  378. , STBIR_EDGE_CLAMP
  379. , STBIR_FILTER_CUBICBSPLINE
  380. , STBIR_COLORSPACE_LINEAR
  381. , NULL
  382. );
  383. if (1 != result)
  384. {
  385. return false;
  386. }
  387. }
  388. }
  389. return true;
  390. }
  391. static float getAlpha(UnpackFn _unpack, const void* _data)
  392. {
  393. float rgba[4];
  394. _unpack(rgba, _data);
  395. return rgba[3];
  396. }
  397. float imageAlphaTestCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, float _alphaRef, float _scale)
  398. {
  399. UnpackFn unpack = getUnpack(_format);
  400. if (NULL == unpack)
  401. {
  402. return 0.0f;
  403. }
  404. float coverage = 0.0f;
  405. const uint8_t* src = (const uint8_t*)_src;
  406. const uint32_t xstep = getBitsPerPixel(_format) / 8;
  407. const float numSamples = 8.0f;
  408. for (uint32_t yy = 0, ystep = _srcPitch; yy < _height-1; ++yy, src += ystep)
  409. {
  410. const uint8_t* data = src;
  411. for (uint32_t xx = 0; xx < _width-1; ++xx, data += xstep)
  412. {
  413. float alpha00 = _scale * getAlpha(unpack, data);
  414. float alpha10 = _scale * getAlpha(unpack, data+xstep);
  415. float alpha01 = _scale * getAlpha(unpack, data+ystep);
  416. float alpha11 = _scale * getAlpha(unpack, data+ystep+xstep);
  417. for (float fy = 0.5f/numSamples; fy < 1.0f; fy += 1.0f)
  418. {
  419. for (float fx = 0.5f/numSamples; fx < 1.0f; fx += 1.0f)
  420. {
  421. float alpha = 0.0f
  422. + alpha00 * (1.0f - fx) * (1.0f - fy)
  423. + alpha10 * ( fx) * (1.0f - fy)
  424. + alpha01 * (1.0f - fx) * ( fy)
  425. + alpha11 * ( fx) * ( fy)
  426. ;
  427. if (alpha > _alphaRef)
  428. {
  429. coverage += 1.0f;
  430. }
  431. }
  432. }
  433. }
  434. }
  435. return coverage / float(_width*_height*numSamples*numSamples);
  436. }
  437. void imageScaleAlphaToCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, void* _src, float _desiredCoverage, float _alphaRef)
  438. {
  439. PackFn pack = getPack(_format);
  440. UnpackFn unpack = getUnpack(_format);
  441. if (NULL == pack
  442. || NULL == unpack)
  443. {
  444. return;
  445. }
  446. float min = 0.0f;
  447. float max = 4.0f;
  448. float scale = 1.0f;
  449. for (uint32_t ii = 0; ii < 8; ++ii)
  450. {
  451. float coverage = imageAlphaTestCoverage(
  452. _format
  453. , _width
  454. , _height
  455. , _srcPitch
  456. , _src
  457. , _alphaRef
  458. , scale
  459. );
  460. if (coverage < _desiredCoverage)
  461. {
  462. min = scale;
  463. }
  464. else if (coverage > _desiredCoverage)
  465. {
  466. max = scale;
  467. }
  468. else
  469. {
  470. break;
  471. }
  472. scale = (min + max) * 0.5f;
  473. }
  474. uint8_t* src = (uint8_t*)_src;
  475. const uint32_t xstep = getBitsPerPixel(_format) / 8;
  476. for (uint32_t yy = 0, ystep = _srcPitch; yy < _height; ++yy, src += ystep)
  477. {
  478. uint8_t* data = src;
  479. for (uint32_t xx = 0; xx < _width; ++xx, data += xstep)
  480. {
  481. float rgba[4];
  482. unpack(rgba, data);
  483. rgba[3] = bx::clamp(rgba[3]*scale, 0.0f, 1.0f);
  484. pack(data, rgba);
  485. }
  486. }
  487. }
  488. } // namespace bimg