texturec.cpp 13 KB

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