texturec.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. bool 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. return true;
  65. case TextureFormat::BC6H:
  66. nvtt::compressBC6H( (const uint8_t*)_src, _width, _height, 4, _dst);
  67. return true;
  68. case TextureFormat::BC7:
  69. nvtt::compressBC7( (const uint8_t*)_src, _width, _height, 4, _dst);
  70. return true;
  71. case TextureFormat::ETC1:
  72. etc1_encode_image( (const uint8_t*)_src, _width, _height, 4, _width*4, (uint8_t*)_dst);
  73. return true;
  74. case TextureFormat::PTC14:
  75. {
  76. using namespace Javelin;
  77. RgbBitmap bmp;
  78. bmp.width = _width;
  79. bmp.height = _height;
  80. bmp.data = (uint8_t*)const_cast<void*>(_src);
  81. PvrTcEncoder::EncodeRgb4Bpp(_dst, bmp);
  82. bmp.data = NULL;
  83. }
  84. return true;
  85. case TextureFormat::PTC14A:
  86. {
  87. using namespace Javelin;
  88. RgbaBitmap bmp;
  89. bmp.width = _width;
  90. bmp.height = _height;
  91. bmp.data = (uint8_t*)const_cast<void*>(_src);
  92. PvrTcEncoder::EncodeRgba4Bpp(_dst, bmp);
  93. bmp.data = NULL;
  94. }
  95. return true;
  96. case TextureFormat::BGRA8:
  97. imageSwizzleBgra8(_width, _height, _width*4, _src, _dst);
  98. return true;
  99. case TextureFormat::RGBA8:
  100. memcpy(_dst, _src, _width*_height*4);
  101. return true;
  102. default:
  103. return imageConvert(_dst, format, _src, TextureFormat::RGBA8, _width, _height);
  104. }
  105. return false;
  106. }
  107. bool imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
  108. {
  109. TextureFormat::Enum format = TextureFormat::Enum(_format);
  110. const uint8_t* src = (const uint8_t*)_src;
  111. switch (format)
  112. {
  113. case TextureFormat::RGBA8:
  114. {
  115. uint8_t* dst = (uint8_t*)_dst;
  116. for (uint32_t yy = 0; yy < _height; ++yy)
  117. {
  118. for (uint32_t xx = 0; xx < _width; ++xx)
  119. {
  120. const uint32_t offset = yy*_width + xx;
  121. const float* input = (const float*)&src[offset * 16];
  122. uint8_t* output = &dst[offset * 4];
  123. output[0] = uint8_t(input[0]*255.0f + 0.5f);
  124. output[1] = uint8_t(input[1]*255.0f + 0.5f);
  125. output[2] = uint8_t(input[2]*255.0f + 0.5f);
  126. output[3] = uint8_t(input[3]*255.0f + 0.5f);
  127. }
  128. }
  129. }
  130. return true;
  131. case TextureFormat::BC5:
  132. {
  133. uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*4);
  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 = &temp[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. imageEncodeFromRgba8(_dst, temp, _width, _height, _format);
  148. BX_FREE(_allocator, temp);
  149. }
  150. return true;
  151. default:
  152. return imageConvert(_dst, format, _src, TextureFormat::RGBA32F, _width, _height);
  153. }
  154. return false;
  155. }
  156. void imageRgba32f11to01(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src)
  157. {
  158. const uint8_t* src = (const uint8_t*)_src;
  159. uint8_t* dst = (uint8_t*)_dst;
  160. for (uint32_t yy = 0; yy < _height; ++yy)
  161. {
  162. for (uint32_t xx = 0; xx < _width; ++xx)
  163. {
  164. const uint32_t offset = yy*_pitch + xx * 16;
  165. const float* input = (const float*)&src[offset];
  166. float* output = (float*)&dst[offset];
  167. output[0] = input[0]*0.5f + 0.5f;
  168. output[1] = input[1]*0.5f + 0.5f;
  169. output[2] = input[2]*0.5f + 0.5f;
  170. output[3] = input[3]*0.5f + 0.5f;
  171. }
  172. }
  173. }
  174. } // namespace bgfx
  175. void help(const char* _error = NULL)
  176. {
  177. if (NULL != _error)
  178. {
  179. fprintf(stderr, "Error:\n%s\n\n", _error);
  180. }
  181. fprintf(stderr
  182. , "texturec, bgfx texture compiler tool\n"
  183. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  184. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  185. );
  186. fprintf(stderr
  187. , "Usage: texturec -f <in> -o <out> -t <format>\n"
  188. "\n"
  189. "Supported input file types:\n"
  190. " *.png Portable Network Graphics\n"
  191. " *.tga Targa\n"
  192. " *.dds Direct Draw Surface\n"
  193. " *.ktx Khronos Texture\n"
  194. " *.pvr PowerVR\n"
  195. "\n"
  196. "Options:\n"
  197. " -f <file path> Input file path.\n"
  198. " -o <file path> Output file path (file will be written in KTX format).\n"
  199. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  200. " -m, --mips Generate mip-maps.\n"
  201. " -n, --normalmap Input texture is normal map.\n"
  202. "\n"
  203. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  204. );
  205. }
  206. int main(int _argc, const char* _argv[])
  207. {
  208. bx::CommandLine cmdLine(_argc, _argv);
  209. if (cmdLine.hasArg('h', "help") )
  210. {
  211. help();
  212. return EXIT_FAILURE;
  213. }
  214. const char* inputFileName = cmdLine.findOption('f');
  215. if (NULL == inputFileName)
  216. {
  217. help("Input file must be specified.");
  218. return EXIT_FAILURE;
  219. }
  220. const char* outputFileName = cmdLine.findOption('o');
  221. if (NULL == outputFileName)
  222. {
  223. help("Output file must be specified.");
  224. return EXIT_FAILURE;
  225. }
  226. bx::CrtFileReader reader;
  227. if (0 != bx::open(&reader, inputFileName) )
  228. {
  229. help("Failed to open input file.");
  230. return EXIT_FAILURE;
  231. }
  232. const char* type = cmdLine.findOption('t');
  233. bgfx::TextureFormat::Enum format = bgfx::TextureFormat::BGRA8;
  234. if (NULL != type)
  235. {
  236. format = bgfx::getFormat(type);
  237. if (!isValid(format) )
  238. {
  239. help("Invalid format specified.");
  240. return EXIT_FAILURE;
  241. }
  242. }
  243. const bool mips = cmdLine.hasArg('m', "mips");
  244. const bool normalMap = cmdLine.hasArg('n', "normalmap");
  245. uint32_t size = (uint32_t)bx::getSize(&reader);
  246. const bgfx::Memory* mem = bgfx::alloc(size);
  247. bx::read(&reader, mem->data, mem->size);
  248. bx::close(&reader);
  249. {
  250. using namespace bgfx;
  251. uint8_t* decodedImage = NULL;
  252. ImageContainer imageContainer;
  253. bool loaded = imageParse(imageContainer, mem->data, mem->size);
  254. if (!loaded)
  255. {
  256. int width = 0;
  257. int height = 0;
  258. int comp = 0;
  259. decodedImage = stbi_load_from_memory( (uint8_t*)mem->data, mem->size, &width, &height, &comp, 4);
  260. loaded = NULL != decodedImage;
  261. if (loaded)
  262. {
  263. release(mem);
  264. mem = makeRef(decodedImage, width*height*4);
  265. imageContainer.m_data = mem->data;
  266. imageContainer.m_size = mem->size;
  267. imageContainer.m_offset = 0;
  268. imageContainer.m_width = width;
  269. imageContainer.m_height = height;
  270. imageContainer.m_depth = 1;
  271. imageContainer.m_format = bgfx::TextureFormat::RGBA8;
  272. imageContainer.m_numMips = 1;
  273. imageContainer.m_hasAlpha = true;
  274. imageContainer.m_cubeMap = false;
  275. imageContainer.m_ktx = false;
  276. imageContainer.m_ktxLE = false;
  277. imageContainer.m_srgb = false;
  278. }
  279. }
  280. if (loaded)
  281. {
  282. bx::CrtAllocator allocator;
  283. const Memory* output = NULL;
  284. ImageMip mip;
  285. if (imageGetRawData(imageContainer, 0, 0, mem->data, mem->size, mip) )
  286. {
  287. uint8_t numMips = mips
  288. ? imageGetNumMips(format, mip.m_width, mip.m_height)
  289. : 1
  290. ;
  291. void* temp = NULL;
  292. if (normalMap)
  293. {
  294. uint32_t size = imageGetSize(TextureFormat::RGBA32F, mip.m_width, mip.m_height);
  295. temp = BX_ALLOC(&allocator, size);
  296. float* rgba = (float*)temp;
  297. float* rgbaDst = (float*)BX_ALLOC(&allocator, size);
  298. imageDecodeToRgba32f(&allocator
  299. , rgba
  300. , mip.m_data
  301. , mip.m_width
  302. , mip.m_height
  303. , mip.m_width*mip.m_bpp/8
  304. , mip.m_format
  305. );
  306. if (TextureFormat::BC5 != mip.m_format)
  307. {
  308. for (uint32_t yy = 0; yy < mip.m_height; ++yy)
  309. {
  310. for (uint32_t xx = 0; xx < mip.m_width; ++xx)
  311. {
  312. const uint32_t offset = (yy*mip.m_width + xx) * 4;
  313. float* inout = &rgba[offset];
  314. inout[0] = inout[0] * 2.0f/255.0f - 1.0f;
  315. inout[1] = inout[1] * 2.0f/255.0f - 1.0f;
  316. inout[2] = inout[2] * 2.0f/255.0f - 1.0f;
  317. inout[3] = inout[3] * 2.0f/255.0f - 1.0f;
  318. }
  319. }
  320. }
  321. output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips);
  322. imageRgba32f11to01(rgbaDst, mip.m_width, mip.m_height, mip.m_width*16, rgba);
  323. imageEncodeFromRgba32f(&allocator, output->data, rgbaDst, mip.m_width, mip.m_height, format);
  324. for (uint8_t lod = 1; lod < numMips; ++lod)
  325. {
  326. imageRgba32fDownsample2x2NormalMap(mip.m_width, mip.m_height, mip.m_width*16, rgba, rgba);
  327. imageRgba32f11to01(rgbaDst, mip.m_width, mip.m_height, mip.m_width*16, rgba);
  328. ImageMip dstMip;
  329. imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
  330. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  331. imageEncodeFromRgba32f(&allocator, data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
  332. }
  333. BX_FREE(&allocator, rgbaDst);
  334. }
  335. else
  336. {
  337. uint32_t size = imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height);
  338. temp = BX_ALLOC(&allocator, size);
  339. uint8_t* rgba = (uint8_t*)temp;
  340. imageDecodeToRgba8(rgba
  341. , mip.m_data
  342. , mip.m_width
  343. , mip.m_height
  344. , mip.m_width*mip.m_bpp/8
  345. , mip.m_format
  346. );
  347. output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips);
  348. imageEncodeFromRgba8(output->data, rgba, mip.m_width, mip.m_height, format);
  349. for (uint8_t lod = 1; lod < numMips; ++lod)
  350. {
  351. imageRgba8Downsample2x2(mip.m_width, mip.m_height, mip.m_width*4, rgba, rgba);
  352. ImageMip dstMip;
  353. imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
  354. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  355. imageEncodeFromRgba8(data, rgba, dstMip.m_width, dstMip.m_height, format);
  356. }
  357. }
  358. BX_FREE(&allocator, temp);
  359. }
  360. if (NULL != output)
  361. {
  362. bx::CrtFileWriter writer;
  363. if (0 == bx::open(&writer, outputFileName) )
  364. {
  365. if (NULL != bx::stristr(outputFileName, ".ktx") )
  366. {
  367. imageWriteKtx(&writer, imageContainer, output->data, output->size);
  368. }
  369. bx::close(&writer);
  370. }
  371. else
  372. {
  373. help("Failed to open output file.");
  374. return EXIT_FAILURE;
  375. }
  376. imageFree(output);
  377. }
  378. }
  379. release(mem);
  380. }
  381. return EXIT_SUCCESS;
  382. }