texturec.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. // Just hacking DDS loading code in here.
  9. #include "bgfx_p.h"
  10. #include "image.h"
  11. #include <libsquish/squish.h>
  12. #include <etc1/etc1.h>
  13. #include <etc2/ProcessRGB.hpp>
  14. #include <nvtt/nvtt.h>
  15. #include <pvrtc/PvrTcEncoder.h>
  16. #include <tinyexr/tinyexr.h>
  17. #include <edtaa3/edtaa3func.h>
  18. #define STB_IMAGE_IMPLEMENTATION
  19. #include <stb/stb_image.c>
  20. #if 0
  21. # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  22. #endif // DEBUG
  23. #include <bx/bx.h>
  24. #include <bx/commandline.h>
  25. #include <bx/crtimpl.h>
  26. #include <bx/uint32_t.h>
  27. namespace bgfx
  28. {
  29. const Memory* alloc(uint32_t _size)
  30. {
  31. Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) + _size);
  32. mem->size = _size;
  33. mem->data = (uint8_t*)mem + sizeof(Memory);
  34. return mem;
  35. }
  36. const Memory* makeRef(const void* _data, uint32_t _size, ReleaseFn _releaseFn, void* _userData)
  37. {
  38. BX_UNUSED(_releaseFn, _userData);
  39. Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) );
  40. mem->size = _size;
  41. mem->data = (uint8_t*)_data;
  42. return mem;
  43. }
  44. void release(const Memory* _mem)
  45. {
  46. Memory* mem = const_cast<Memory*>(_mem);
  47. ::free(mem);
  48. }
  49. bool imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
  50. {
  51. TextureFormat::Enum format = TextureFormat::Enum(_format);
  52. switch (format)
  53. {
  54. case TextureFormat::BC1:
  55. case TextureFormat::BC2:
  56. case TextureFormat::BC3:
  57. case TextureFormat::BC4:
  58. case TextureFormat::BC5:
  59. squish::CompressImage( (const uint8_t*)_src, _width, _height, _dst
  60. , format == TextureFormat::BC2 ? squish::kDxt3
  61. : format == TextureFormat::BC3 ? squish::kDxt5
  62. : format == TextureFormat::BC4 ? squish::kBc4
  63. : format == TextureFormat::BC5 ? squish::kBc5
  64. : squish::kDxt1
  65. );
  66. return true;
  67. case TextureFormat::BC6H:
  68. nvtt::compressBC6H( (const uint8_t*)_src, _width, _height, 4, _dst);
  69. return true;
  70. case TextureFormat::BC7:
  71. nvtt::compressBC7( (const uint8_t*)_src, _width, _height, 4, _dst);
  72. return true;
  73. case TextureFormat::ETC1:
  74. etc1_encode_image( (const uint8_t*)_src, _width, _height, 4, _width*4, (uint8_t*)_dst);
  75. return true;
  76. case TextureFormat::ETC2:
  77. {
  78. const uint32_t blockWidth = (_width +3)/4;
  79. const uint32_t blockHeight = (_height+3)/4;
  80. const uint32_t pitch = _width*4;
  81. const uint8_t* src = (const uint8_t*)_src;
  82. uint64_t* dst = (uint64_t*)_dst;
  83. for (uint32_t yy = 0; yy < blockHeight; ++yy)
  84. {
  85. for (uint32_t xx = 0; xx < blockWidth; ++xx)
  86. {
  87. uint8_t block[4*4*4];
  88. const uint8_t* ptr = &src[(yy*pitch+xx*4)*4];
  89. for (uint32_t ii = 0; ii < 16; ++ii)
  90. { // BGRx
  91. memcpy(&block[ii*4], &ptr[(ii%4)*pitch + (ii&~3)], 4);
  92. bx::xchg(block[ii*4+0], block[ii*4+2]);
  93. }
  94. *dst++ = ProcessRGB_ETC2(block);
  95. }
  96. }
  97. }
  98. return true;
  99. case TextureFormat::PTC14:
  100. {
  101. using namespace Javelin;
  102. RgbaBitmap bmp;
  103. bmp.width = _width;
  104. bmp.height = _height;
  105. bmp.data = (uint8_t*)const_cast<void*>(_src);
  106. PvrTcEncoder::EncodeRgb4Bpp(_dst, bmp);
  107. bmp.data = NULL;
  108. }
  109. return true;
  110. case TextureFormat::PTC14A:
  111. {
  112. using namespace Javelin;
  113. RgbaBitmap bmp;
  114. bmp.width = _width;
  115. bmp.height = _height;
  116. bmp.data = (uint8_t*)const_cast<void*>(_src);
  117. PvrTcEncoder::EncodeRgba4Bpp(_dst, bmp);
  118. bmp.data = NULL;
  119. }
  120. return true;
  121. case TextureFormat::BGRA8:
  122. imageSwizzleBgra8(_width, _height, _width*4, _src, _dst);
  123. return true;
  124. case TextureFormat::RGBA8:
  125. memcpy(_dst, _src, _width*_height*4);
  126. return true;
  127. default:
  128. return imageConvert(_dst, format, _src, TextureFormat::RGBA8, _width, _height);
  129. }
  130. return false;
  131. }
  132. bool imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
  133. {
  134. TextureFormat::Enum format = TextureFormat::Enum(_format);
  135. const uint8_t* src = (const uint8_t*)_src;
  136. switch (format)
  137. {
  138. case TextureFormat::RGBA8:
  139. {
  140. uint8_t* dst = (uint8_t*)_dst;
  141. for (uint32_t yy = 0; yy < _height; ++yy)
  142. {
  143. for (uint32_t xx = 0; xx < _width; ++xx)
  144. {
  145. const uint32_t offset = yy*_width + xx;
  146. const float* input = (const float*)&src[offset * 16];
  147. uint8_t* output = &dst[offset * 4];
  148. output[0] = uint8_t(input[0]*255.0f + 0.5f);
  149. output[1] = uint8_t(input[1]*255.0f + 0.5f);
  150. output[2] = uint8_t(input[2]*255.0f + 0.5f);
  151. output[3] = uint8_t(input[3]*255.0f + 0.5f);
  152. }
  153. }
  154. }
  155. return true;
  156. case TextureFormat::BC5:
  157. {
  158. uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*4);
  159. for (uint32_t yy = 0; yy < _height; ++yy)
  160. {
  161. for (uint32_t xx = 0; xx < _width; ++xx)
  162. {
  163. const uint32_t offset = yy*_width + xx;
  164. const float* input = (const float*)&src[offset * 16];
  165. uint8_t* output = &temp[offset * 4];
  166. output[0] = uint8_t(input[0]*255.0f + 0.5f);
  167. output[1] = uint8_t(input[1]*255.0f + 0.5f);
  168. output[2] = uint8_t(input[2]*255.0f + 0.5f);
  169. output[3] = uint8_t(input[3]*255.0f + 0.5f);
  170. }
  171. }
  172. imageEncodeFromRgba8(_dst, temp, _width, _height, _format);
  173. BX_FREE(_allocator, temp);
  174. }
  175. return true;
  176. default:
  177. return imageConvert(_dst, format, _src, TextureFormat::RGBA32F, _width, _height);
  178. }
  179. return false;
  180. }
  181. void imageRgba32f11to01(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src)
  182. {
  183. const uint8_t* src = (const uint8_t*)_src;
  184. uint8_t* dst = (uint8_t*)_dst;
  185. for (uint32_t yy = 0; yy < _height; ++yy)
  186. {
  187. for (uint32_t xx = 0; xx < _width; ++xx)
  188. {
  189. const uint32_t offset = yy*_pitch + xx * 16;
  190. const float* input = (const float*)&src[offset];
  191. float* output = (float*)&dst[offset];
  192. output[0] = input[0]*0.5f + 0.5f;
  193. output[1] = input[1]*0.5f + 0.5f;
  194. output[2] = input[2]*0.5f + 0.5f;
  195. output[3] = input[3]*0.5f + 0.5f;
  196. }
  197. }
  198. }
  199. static void edtaa3(bx::AllocatorI* _allocator, double* _dst, uint32_t _width, uint32_t _height, double* _src)
  200. {
  201. const uint32_t numPixels = _width*_height;
  202. short* xdist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  203. short* ydist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  204. double* gx = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  205. double* gy = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  206. ::computegradient(_src, _width, _height, gx, gy);
  207. ::edtaa3(_src, gx, gy, _width, _height, xdist, ydist, _dst);
  208. for (uint32_t ii = 0; ii < numPixels; ++ii)
  209. {
  210. if (_dst[ii] < 0.0)
  211. {
  212. _dst[ii] = 0.0;
  213. }
  214. }
  215. BX_FREE(_allocator, xdist);
  216. BX_FREE(_allocator, ydist);
  217. BX_FREE(_allocator, gx);
  218. BX_FREE(_allocator, gy);
  219. }
  220. inline double min(double _a, double _b)
  221. {
  222. return _a > _b ? _b : _a;
  223. }
  224. inline double max(double _a, double _b)
  225. {
  226. return _a > _b ? _a : _b;
  227. }
  228. inline double clamp(double _val, double _min, double _max)
  229. {
  230. return max(min(_val, _max), _min);
  231. }
  232. void imageMakeDist(bx::AllocatorI* _allocator, void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, float _edge, const void* _src)
  233. {
  234. const uint32_t numPixels = _width*_height;
  235. double* imgIn = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  236. double* outside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  237. double* inside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  238. for (uint32_t yy = 0; yy < _height; ++yy)
  239. {
  240. const uint8_t* src = (const uint8_t*)_src + yy*_pitch;
  241. double* dst = &imgIn[yy*_width];
  242. for (uint32_t xx = 0; xx < _width; ++xx)
  243. {
  244. dst[xx] = double(src[xx])/255.0;
  245. }
  246. }
  247. edtaa3(_allocator, outside, _width, _height, imgIn);
  248. for (uint32_t ii = 0; ii < numPixels; ++ii)
  249. {
  250. imgIn[ii] = 1.0 - imgIn[ii];
  251. }
  252. edtaa3(_allocator, inside, _width, _height, imgIn);
  253. BX_FREE(_allocator, imgIn);
  254. uint8_t* dst = (uint8_t*)_dst;
  255. double edgeOffset = _edge*0.5;
  256. double invEdge = 1.0/_edge;
  257. for (uint32_t ii = 0; ii < numPixels; ++ii)
  258. {
  259. double dist = clamp( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge, 0.0, 1.0);
  260. dst[ii] = 255-uint8_t(dist * 255.0);
  261. }
  262. BX_FREE(_allocator, inside);
  263. BX_FREE(_allocator, outside);
  264. }
  265. } // namespace bgfx
  266. void help(const char* _error = NULL)
  267. {
  268. if (NULL != _error)
  269. {
  270. fprintf(stderr, "Error:\n%s\n\n", _error);
  271. }
  272. fprintf(stderr
  273. , "texturec, bgfx texture compiler tool\n"
  274. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  275. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  276. );
  277. fprintf(stderr
  278. , "Usage: texturec -f <in> -o <out> -t <format>\n"
  279. "\n"
  280. "Supported input file types:\n"
  281. " *.png Portable Network Graphics\n"
  282. " *.tga Targa\n"
  283. " *.dds Direct Draw Surface\n"
  284. " *.ktx Khronos Texture\n"
  285. " *.pvr PowerVR\n"
  286. "\n"
  287. "Options:\n"
  288. " -f <file path> Input file path.\n"
  289. " -o <file path> Output file path (file will be written in KTX format).\n"
  290. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  291. " -m, --mips Generate mip-maps.\n"
  292. " -n, --normalmap Input texture is normal map.\n"
  293. " --sdf <edge> Compute SDF texture.\n"
  294. "\n"
  295. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  296. );
  297. }
  298. int main(int _argc, const char* _argv[])
  299. {
  300. bx::CommandLine cmdLine(_argc, _argv);
  301. if (cmdLine.hasArg('h', "help") )
  302. {
  303. help();
  304. return EXIT_FAILURE;
  305. }
  306. const char* inputFileName = cmdLine.findOption('f');
  307. if (NULL == inputFileName)
  308. {
  309. help("Input file must be specified.");
  310. return EXIT_FAILURE;
  311. }
  312. const char* outputFileName = cmdLine.findOption('o');
  313. if (NULL == outputFileName)
  314. {
  315. help("Output file must be specified.");
  316. return EXIT_FAILURE;
  317. }
  318. bool sdf = false;
  319. double edge = 16.0;
  320. const char* edgeOpt = cmdLine.findOption("sdf");
  321. if (NULL != edgeOpt)
  322. {
  323. sdf = true;
  324. edge = atof(edgeOpt);
  325. }
  326. BX_UNUSED(sdf, edge);
  327. bx::CrtFileReader reader;
  328. if (!bx::open(&reader, inputFileName) )
  329. {
  330. help("Failed to open input file.");
  331. return EXIT_FAILURE;
  332. }
  333. const char* type = cmdLine.findOption('t');
  334. bgfx::TextureFormat::Enum format = bgfx::TextureFormat::BGRA8;
  335. if (NULL != type)
  336. {
  337. format = bgfx::getFormat(type);
  338. if (!isValid(format) )
  339. {
  340. help("Invalid format specified.");
  341. return EXIT_FAILURE;
  342. }
  343. }
  344. const bool mips = cmdLine.hasArg('m', "mips");
  345. const bool normalMap = cmdLine.hasArg('n', "normalmap");
  346. uint32_t size = (uint32_t)bx::getSize(&reader);
  347. const bgfx::Memory* mem = bgfx::alloc(size);
  348. bx::read(&reader, mem->data, mem->size);
  349. bx::close(&reader);
  350. {
  351. using namespace bgfx;
  352. uint8_t* decodedImage = NULL;
  353. ImageContainer imageContainer;
  354. bool loaded = imageParse(imageContainer, mem->data, mem->size);
  355. if (!loaded)
  356. {
  357. int width = 0;
  358. int height = 0;
  359. int comp = 0;
  360. decodedImage = stbi_load_from_memory( (uint8_t*)mem->data, mem->size, &width, &height, &comp, 4);
  361. loaded = NULL != decodedImage;
  362. if (loaded)
  363. {
  364. release(mem);
  365. mem = makeRef(decodedImage, width*height*4);
  366. imageContainer.m_data = mem->data;
  367. imageContainer.m_size = mem->size;
  368. imageContainer.m_offset = 0;
  369. imageContainer.m_width = width;
  370. imageContainer.m_height = height;
  371. imageContainer.m_depth = 1;
  372. imageContainer.m_format = bgfx::TextureFormat::RGBA8;
  373. imageContainer.m_numMips = 1;
  374. imageContainer.m_hasAlpha = true;
  375. imageContainer.m_cubeMap = false;
  376. imageContainer.m_ktx = false;
  377. imageContainer.m_ktxLE = false;
  378. imageContainer.m_srgb = false;
  379. }
  380. }
  381. if (loaded)
  382. {
  383. bx::CrtAllocator allocator;
  384. const Memory* output = NULL;
  385. ImageMip mip;
  386. if (imageGetRawData(imageContainer, 0, 0, mem->data, mem->size, mip) )
  387. {
  388. uint8_t numMips = mips
  389. ? imageGetNumMips(format, mip.m_width, mip.m_height)
  390. : 1
  391. ;
  392. void* temp = NULL;
  393. if (normalMap)
  394. {
  395. uint32_t size = imageGetSize(TextureFormat::RGBA32F, mip.m_width, mip.m_height);
  396. temp = BX_ALLOC(&allocator, size);
  397. float* rgba = (float*)temp;
  398. float* rgbaDst = (float*)BX_ALLOC(&allocator, size);
  399. imageDecodeToRgba32f(&allocator
  400. , rgba
  401. , mip.m_data
  402. , mip.m_width
  403. , mip.m_height
  404. , mip.m_width*mip.m_bpp/8
  405. , mip.m_format
  406. );
  407. if (TextureFormat::BC5 != mip.m_format)
  408. {
  409. for (uint32_t yy = 0; yy < mip.m_height; ++yy)
  410. {
  411. for (uint32_t xx = 0; xx < mip.m_width; ++xx)
  412. {
  413. const uint32_t offset = (yy*mip.m_width + xx) * 4;
  414. float* inout = &rgba[offset];
  415. inout[0] = inout[0] * 2.0f/255.0f - 1.0f;
  416. inout[1] = inout[1] * 2.0f/255.0f - 1.0f;
  417. inout[2] = inout[2] * 2.0f/255.0f - 1.0f;
  418. inout[3] = inout[3] * 2.0f/255.0f - 1.0f;
  419. }
  420. }
  421. }
  422. output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips);
  423. imageRgba32f11to01(rgbaDst, mip.m_width, mip.m_height, mip.m_width*16, rgba);
  424. imageEncodeFromRgba32f(&allocator, output->data, rgbaDst, mip.m_width, mip.m_height, format);
  425. for (uint8_t lod = 1; lod < numMips; ++lod)
  426. {
  427. imageRgba32fDownsample2x2NormalMap(mip.m_width, mip.m_height, mip.m_width*16, rgba, rgba);
  428. imageRgba32f11to01(rgbaDst, mip.m_width, mip.m_height, mip.m_width*16, rgba);
  429. mip.m_width = bx::uint32_max(1, mip.m_width >> 1);
  430. mip.m_height = bx::uint32_max(1, mip.m_height >> 1);
  431. ImageMip dstMip;
  432. imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
  433. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  434. imageEncodeFromRgba32f(&allocator, data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
  435. }
  436. BX_FREE(&allocator, rgbaDst);
  437. }
  438. else
  439. {
  440. uint32_t size = imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height);
  441. temp = BX_ALLOC(&allocator, size);
  442. uint8_t* rgba = (uint8_t*)temp;
  443. imageDecodeToRgba8(rgba
  444. , mip.m_data
  445. , mip.m_width
  446. , mip.m_height
  447. , mip.m_width*mip.m_bpp/8
  448. , mip.m_format
  449. );
  450. output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips);
  451. imageEncodeFromRgba8(output->data, rgba, mip.m_width, mip.m_height, format);
  452. for (uint8_t lod = 1; lod < numMips; ++lod)
  453. {
  454. imageRgba8Downsample2x2(mip.m_width, mip.m_height, mip.m_width*4, rgba, rgba);
  455. mip.m_width = bx::uint32_max(1, mip.m_width >> 1);
  456. mip.m_height = bx::uint32_max(1, mip.m_height >> 1);
  457. ImageMip dstMip;
  458. imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
  459. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  460. imageEncodeFromRgba8(data, rgba, dstMip.m_width, dstMip.m_height, format);
  461. }
  462. }
  463. BX_FREE(&allocator, temp);
  464. }
  465. if (NULL != output)
  466. {
  467. bx::CrtFileWriter writer;
  468. if (bx::open(&writer, outputFileName) )
  469. {
  470. if (NULL != bx::stristr(outputFileName, ".ktx") )
  471. {
  472. imageWriteKtx(&writer, imageContainer, output->data, output->size);
  473. }
  474. bx::close(&writer);
  475. }
  476. else
  477. {
  478. help("Failed to open output file.");
  479. return EXIT_FAILURE;
  480. }
  481. imageFree(output);
  482. }
  483. }
  484. release(mem);
  485. }
  486. return EXIT_SUCCESS;
  487. }