texturec.cpp 12 KB

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