texturec.cpp 15 KB

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